maplib 0.18.18__cp310-abi3-macosx_11_0_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
maplib/__init__.pyi ADDED
@@ -0,0 +1,910 @@
1
+ from pathlib import Path
2
+ from typing import Union, List, Dict, Optional, Callable, Tuple, Literal as LiteralType
3
+ from polars import DataFrame
4
+ from datetime import datetime, date
5
+
6
+ class RDFType:
7
+ """
8
+ The type of a column containing a RDF variable.
9
+ For instance, xsd:string is RDFType.Literal("http://www.w3.org/2001/XMLSchema#string")
10
+ """
11
+
12
+ IRI: Callable[[], "RDFType"]
13
+ BlankNode: Callable[[], "RDFType"]
14
+ Literal: Callable[[Union[str, "IRI"]], "RDFType"]
15
+ Multi: Callable[[List["RDFType"]], "RDFType"]
16
+ Nested: Callable[["RDFType"], "RDFType"]
17
+ Unknown: Callable[[], "RDFType"]
18
+
19
+ class SolutionMappings:
20
+ """
21
+ Detailed information about the solution mappings, the types of the variables and debugging for queries.
22
+ """
23
+
24
+ mappings: DataFrame
25
+ rdf_types: Dict[str, RDFType]
26
+ debug: Optional[str]
27
+
28
+ class Variable:
29
+ """
30
+ A variable in a template.
31
+ """
32
+
33
+ name: str
34
+
35
+ def __init__(self, name: str):
36
+ """
37
+ Create a new variable.
38
+ :param name: The name of the variable.
39
+ """
40
+ ...
41
+
42
+ class IRI:
43
+ iri: str
44
+ """
45
+ An IRI.
46
+ """
47
+
48
+ def __init__(self, iri: str):
49
+ """
50
+ Create a new IRI
51
+ :param iri: IRI (without < and >).
52
+ """
53
+
54
+ class BlankNode:
55
+ """
56
+ A Blank Node.
57
+ """
58
+
59
+ name: str
60
+
61
+ def __init__(self, name: str):
62
+ """
63
+ Create a new Blank Node
64
+ :param name: Name of blank node (without _:).
65
+ """
66
+
67
+ class Prefix:
68
+ """
69
+ A prefix that can be used to ergonomically build iris.
70
+ """
71
+
72
+ def __init__(self, iri, prefix_name=None):
73
+ """
74
+ Create a new prefix.
75
+ :param iri: The prefix IRI.
76
+ :param prefix_name: The name of the prefix
77
+ """
78
+
79
+ def suf(self, suffix: str) -> IRI:
80
+ """
81
+ Create an IRI by appending the suffix.
82
+ :param suffix: The suffix to append.
83
+ :return:
84
+ """
85
+
86
+ class Literal:
87
+ """
88
+ An RDF literal.
89
+ """
90
+
91
+ value: str
92
+ datatype: Optional[IRI]
93
+ language: Optional[str]
94
+
95
+ def __init__(self, value: str, data_type: IRI = None, language: str = None):
96
+ """
97
+ Create a new RDF Literal
98
+ :param value: The lexical representation of the value.
99
+ :param data_type: The data type of the value (an IRI).
100
+ :param language: The language tag of the value.
101
+ """
102
+
103
+ def to_native(self) -> Union[int, float, bool, str, datetime, date]:
104
+ """
105
+
106
+ :return:
107
+ """
108
+
109
+ class Parameter:
110
+ variable: Variable
111
+ optional: bool
112
+ allow_blank: bool
113
+ rdf_type: Optional[RDFType]
114
+ default_value: Optional[Union[Literal, IRI, BlankNode]]
115
+ """
116
+ Parameters for template signatures.
117
+ """
118
+
119
+ def __init__(
120
+ self,
121
+ variable: Variable,
122
+ optional: Optional[bool] = False,
123
+ allow_blank: Optional[bool] = True,
124
+ rdf_type: Optional[RDFType] = None,
125
+ default_value: Optional[Union[Literal, IRI, BlankNode]] = None,
126
+ ):
127
+ """
128
+ Create a new parameter for a Template.
129
+ :param variable: The variable.
130
+ :param optional: Can the variable be unbound?
131
+ :param allow_blank: Can the variable be bound to a blank node?
132
+ :param rdf_type: The type of the variable. Can be nested.
133
+ :param default_value: Default value when no value provided.
134
+ """
135
+
136
+ class Argument:
137
+ def __init__(
138
+ self, term: Union[Variable, IRI, Literal], list_expand: Optional[bool] = False
139
+ ):
140
+ """
141
+ An argument for a template instance.
142
+ :param term: The term.
143
+ :param list_expand: Should the argument be expanded? Used with the list_expander argument of instance.
144
+ """
145
+
146
+ class Instance:
147
+ def __init__(
148
+ self,
149
+ iri: IRI,
150
+ arguments: List[Union[Argument, Variable, IRI, Literal, BlankNode, None]],
151
+ list_expander: Optional[LiteralType["cross", "zipMin", "zipMax"]] = None,
152
+ ):
153
+ """
154
+ A template instance.
155
+ :param iri: The IRI of the template to be instantiated.
156
+ :param arguments: The arguments for template instantiation.
157
+ :param list_expander: (How) should we do list expansion?
158
+ """
159
+
160
+ class Template:
161
+ iri: str
162
+ parameters: List[Parameter]
163
+ instances: List[Instance]
164
+ """
165
+ An OTTR Template.
166
+ Note that accessing parameters- or instances-fields returns copies.
167
+ To change these fields, you must assign new lists of parameters or instances.
168
+ """
169
+
170
+ def __init__(
171
+ self,
172
+ iri: IRI,
173
+ parameters: List[Union[Parameter, Variable]],
174
+ instances: List[Instance],
175
+ ):
176
+ """
177
+ Create a new OTTR Template
178
+ :param iri: The IRI of the template
179
+ :param parameters:
180
+ :param instances:
181
+ """
182
+
183
+ def instance(
184
+ self,
185
+ arguments: List[Union[Argument, Variable, IRI, Literal, None]],
186
+ list_expander: LiteralType["cross", "zipMin", "zipMax"] = None,
187
+ ) -> Instance:
188
+ """
189
+
190
+ :param arguments: The arguments to the template.
191
+ :param list_expander: (How) should we list-expand?
192
+ :return:
193
+ """
194
+
195
+ def Triple(
196
+ subject: Union["Argument", IRI, Variable, BlankNode],
197
+ predicate: Union["Argument", IRI, Variable, BlankNode],
198
+ object: Union["Argument", IRI, Variable, Literal, BlankNode],
199
+ list_expander: Optional[LiteralType["cross", "zipMin", "zipMax"]] = None,
200
+ ):
201
+ """
202
+ An OTTR Triple Pattern used for creating templates.
203
+ This is the basis pattern which all template instances are rewritten into.
204
+ Equivalent to:
205
+
206
+ >>> ottr = Prefix("http://ns.ottr.xyz/0.4/")
207
+ ... Instance(ottr.suf("Triple"), subject, predicate, object, list_expander)
208
+
209
+ :param subject:
210
+ :param predicate:
211
+ :param object:
212
+ :param list_expander:
213
+ :return:
214
+ """
215
+
216
+ class XSD:
217
+ """
218
+ The xsd namespace, for convenience.
219
+ """
220
+
221
+ boolean: IRI
222
+ byte: IRI
223
+ date: IRI
224
+ dateTime: IRI
225
+ dateTimeStamp: IRI
226
+ decimal: IRI
227
+ double: IRI
228
+ duration: IRI
229
+ float: IRI
230
+ int_: IRI
231
+ integer: IRI
232
+ language: IRI
233
+ long: IRI
234
+ short: IRI
235
+ string: IRI
236
+
237
+ def __init__(self):
238
+ """
239
+ Create the xsd namespace helper.
240
+ """
241
+
242
+ def a() -> IRI:
243
+ """
244
+ :return: IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
245
+ """
246
+
247
+ # END COMMON WITH CHRONTEXT
248
+
249
+ class IndexingOptions:
250
+ """
251
+ Options for indexing
252
+ """
253
+
254
+ def __init__(
255
+ self,
256
+ object_sort_all: bool = None,
257
+ object_sort_some: List["IRI"] = None,
258
+ fts_path: str = None,
259
+ ):
260
+ """
261
+ Defaults to indexing on subjects and objects for select types (e.g. rdf:type and rdfs:label)
262
+
263
+ :param object_sort_all: Enable object-indexing for all suitable predicates (doubles memory requirement).
264
+ :param object_sort_some: Enable object-indexing for a selected list of predicates.
265
+ :param fts_path: Enable full text search, stored at the path
266
+ """
267
+
268
+ ParametersType = Dict[str, Tuple[DataFrame, Dict[str, RDFType]]]
269
+
270
+ class ValidationReport:
271
+ """
272
+ SHACL Validation report.
273
+ Only constructed by maplib.
274
+ """
275
+
276
+ conforms: bool
277
+ "Whether or not the validation report conforms to the shapes"
278
+
279
+ shape_targets: DataFrame
280
+ "A DataFrame containing the counts of the targets of each shape and constraint"
281
+
282
+ performance: DataFrame
283
+ "Performance statistics for the validation process"
284
+
285
+ def results(
286
+ self,
287
+ native_dataframe: bool = False,
288
+ include_datatypes: bool = False,
289
+ streaming: bool = False,
290
+ ) -> Optional[Union[DataFrame, "SolutionMappings"]]:
291
+ """
292
+ Return the results of the validation report, if they exist.
293
+
294
+ :param native_dataframe: Return columns with maplib-native formatting. Useful for round-trips.
295
+ :param include_datatypes: Return datatypes of the results DataFrame (returns SolutionMappings instead of DataFrame).
296
+ :param streaming: Use the Polars streaming functionality.
297
+ :return: The SHACL validation report, as a DataFrame
298
+ """
299
+
300
+ def details(
301
+ self,
302
+ native_dataframe: bool = False,
303
+ include_datatypes: bool = False,
304
+ streaming: bool = False,
305
+ ) -> Optional[DataFrame]:
306
+ """
307
+ Returns the details of the validation report.
308
+ Only available if validation was called with include_details=True.
309
+
310
+ :param native_dataframe: Return columns with maplib-native formatting. Useful for round-trips.
311
+ :param include_datatypes: Return datatypes of the results DataFrame (returns SolutionMappings instead of DataFrame).
312
+ :param streaming: Use the Polars streaming functionality.
313
+ :return: Details of the SHACL validation report, as a DataFrame
314
+ """
315
+
316
+ def graph(self) -> "Mapping":
317
+ """
318
+ Creates a new model object where the base graph is the validation report with results.
319
+ Includes the details of the validation report in the new graph if they exist.
320
+
321
+ :return:
322
+ """
323
+
324
+ class Model:
325
+ """
326
+ A model session allowing:
327
+
328
+ * Iterative model using OTTR templates
329
+ * Interactive SPARQL querying and enrichment
330
+ * SHACL validation
331
+
332
+ Usage:
333
+
334
+ >>> from maplib import Model
335
+ ... doc = '''
336
+ ... @prefix ex:<http://example.net/ns#>.
337
+ ... ex:ExampleTemplate [?MyValue] :: {
338
+ ... ottr:Triple(ex:myObject, ex:hasValue, ?MyValue)
339
+ ... } .'''
340
+ ... m = Model()
341
+ ... m.add_template(doc)
342
+
343
+ :param documents: a stOTTR document or a list of these
344
+ :param indexing_options: options for indexing
345
+ """
346
+
347
+ def __init__(
348
+ self,
349
+ indexing_options: "IndexingOptions" = None,
350
+ ) -> "Model": ...
351
+
352
+ def add_template(self, template: Union["Template", str]):
353
+ """
354
+ Add a template to the model. Overwrites any existing template with the same IRI.
355
+ :param template: The template to add, as a stOTTR string or as a programmatically constructed Template.
356
+ :return:
357
+ """
358
+
359
+ def add_prefixes(self, template: Dict[str, str]):
360
+ """
361
+ Add prefixes that will be used in parsing of SPARQL, Datalog and OTTR.
362
+
363
+ Usage:
364
+ >>> m.add_prefixes({"ex" : "http:://example.net/"})
365
+
366
+ :param prefixes: Known prefixes
367
+ :return:
368
+ """
369
+
370
+ def map(
371
+ self,
372
+ template: Union[str, "Template", IRI],
373
+ df: DataFrame = None,
374
+ graph: str = None,
375
+ types: Dict[str, RDFType] = None,
376
+ validate_iris: bool = True,
377
+ ) -> None:
378
+ """
379
+ Map a template using a DataFrame
380
+ Usage:
381
+
382
+ >>> m.map("ex:ExampleTemplate", df)
383
+
384
+ If the template has no arguments, the df argument is not necessary.
385
+
386
+ :param template: Template, IRI, IRI string or prefixed template name.
387
+ :param df: DataFrame where the columns have the same names as the template arguments
388
+ :param graph: The IRI of the graph to add triples to.
389
+ :param types: The types of the columns.
390
+ :param validate_iris: Validate any IRI-columns.
391
+ """
392
+
393
+ def map_triples(
394
+ self,
395
+ df: DataFrame = None,
396
+ predicate: str = None,
397
+ graph: str = None,
398
+ types: Dict[str, RDFType] = None,
399
+ validate_iris: bool = True,
400
+ ) -> None:
401
+ """
402
+ Map a template using a DataFrame with columns subject, object and predicate
403
+ The predicate column can also be supplied as a string if it is the same for all rows.
404
+ Usage:
405
+
406
+ >>> m.map_triples(df)
407
+
408
+ If the template has no arguments, the df argument is not necessary.
409
+
410
+ :param df: DataFrame where the columns are named subject and object. May also contain a verb-column.
411
+ :param verb: The uri of the verb.
412
+ :param graph: The IRI of the graph to add triples to.
413
+ :param types: The types of the columns.
414
+ :param validate_iris: Validate any IRI-columns.
415
+ """
416
+
417
+ def map_default(
418
+ self,
419
+ df: DataFrame,
420
+ primary_key_column: str,
421
+ dry_run: bool = False,
422
+ graph: str = None,
423
+ types: Dict[str, RDFType] = None,
424
+ validate_iris: bool = True,
425
+ ) -> str:
426
+ """
427
+ Create a default template and map it based on a dataframe.
428
+ Usage:
429
+
430
+ >>> template_string = m.map_default(df, "myKeyCol")
431
+ ... print(template_string)
432
+
433
+ :param df: DataFrame where the columns have the same names as the template arguments
434
+ :param primary_key_column: This column will be the subject of all triples in the generated template.
435
+ :param dry_run: Do not map the template, only return the string.
436
+ :param graph: The IRI of the graph to add triples to.
437
+ :param types: The types of the columns.
438
+ :param validate_iris: Validate any IRI-columns.
439
+ :return: The generated template
440
+ """
441
+
442
+ def query(
443
+ self,
444
+ query: str,
445
+ parameters: ParametersType = None,
446
+ include_datatypes: bool = False,
447
+ native_dataframe: bool = False,
448
+ graph: str = None,
449
+ streaming: bool = False,
450
+ return_json: bool = False,
451
+ include_transient: bool = True,
452
+ max_rows: int = None,
453
+ debug: bool = False,
454
+ ) -> Union[
455
+ DataFrame, SolutionMappings, List[Union[DataFrame, SolutionMappings, str]], None
456
+ ]:
457
+ """
458
+ Query the contained knowledge graph using SPARQL
459
+ Currently, SELECT, CONSTRUCT and INSERT are supported.
460
+ Usage:
461
+
462
+ >>> df = model.query('''
463
+ ... PREFIX ex:<http://example.net/ns#>
464
+ ... SELECT ?obj1 ?obj2 WHERE {
465
+ ... ?obj1 ex:hasObj ?obj2
466
+ ... }''')
467
+ ... print(df)
468
+
469
+ :param query: The SPARQL query string
470
+ :param parameters: PVALUES Parameters, a DataFrame containing the value bindings in the custom PVALUES construction.
471
+ :param native_dataframe: Return columns with maplib-native formatting. Useful for round-trips.
472
+ :param include_datatypes: Datatypes are not returned by default, set to true to return a dict with the solution mappings and the datatypes.
473
+ :param graph: The IRI of the graph to query.
474
+ :param streaming: Use Polars streaming
475
+ :param return_json: Return JSON string.
476
+ :param include_transient: Include transient triples when querying.
477
+ :param max_rows: Maximum estimated rows in result, helps avoid out-of-memory errors.
478
+ :param debug: Why does my query have no results?
479
+ :return: DataFrame (Select), list of DataFrames (Construct) containing results, None for Insert-queries, or SolutionMappings when debug or native_dataframe is set.
480
+
481
+ """
482
+
483
+ def update(
484
+ self,
485
+ update: str,
486
+ parameters: ParametersType = None,
487
+ streaming: bool = False,
488
+ include_transient: bool = True,
489
+ max_rows: int = None,
490
+ debug: bool = False,
491
+ ):
492
+ """
493
+ Insert the results of a Construct query in the graph.
494
+ Useful for being able to use the same query for inspecting what will be inserted and actually inserting.
495
+ Usage:
496
+
497
+ >>> m = Model(doc)
498
+ ... # Omitted
499
+ ... update_pizzas = '''
500
+ ... ...'''
501
+ ... m.update(update_pizzas)
502
+
503
+ :param update: The SPARQL Update string
504
+ :param parameters: PVALUES Parameters, a DataFrame containing the value bindings in the custom PVALUES construction.
505
+ :param streaming: Use Polars streaming
506
+ :param include_transient: Include transient triples when querying (but see "transient" above).
507
+ :param max_rows: Maximum estimated rows in result, helps avoid out-of-memory errors.
508
+ :param debug: Why does my query have no results?
509
+ :return: None
510
+ """
511
+
512
+ def insert(
513
+ self,
514
+ query: str,
515
+ parameters: ParametersType = None,
516
+ include_datatypes: bool = False,
517
+ native_dataframe: bool = False,
518
+ transient: bool = False,
519
+ streaming: bool = False,
520
+ source_graph: str = None,
521
+ target_graph: str = None,
522
+ include_transient: bool = True,
523
+ max_rows: int = None,
524
+ debug: bool = False,
525
+ ):
526
+ """
527
+ Insert the results of a Construct query in the graph.
528
+ Useful for being able to use the same query for inspecting what will be inserted and actually inserting.
529
+ Usage:
530
+
531
+ >>> m = Model(doc)
532
+ ... # Omitted
533
+ ... hpizzas = '''
534
+ ... PREFIX pizza:<https://github.com/magbak/maplib/pizza#>
535
+ ... PREFIX ing:<https://github.com/magbak/maplib/pizza/ingredients#>
536
+ ... CONSTRUCT { ?p a pizza:HeterodoxPizza }
537
+ ... WHERE {
538
+ ... ?p a pizza:Pizza .
539
+ ... ?p pizza:hasIngredient ing:Pineapple .
540
+ ... }'''
541
+ ... m.insert(hpizzas)
542
+
543
+ :param query: The SPARQL Insert query string
544
+ :param parameters: PVALUES Parameters, a DataFrame containing the value bindings in the custom PVALUES construction.
545
+ :param native_dataframe: Return columns with maplib-native formatting. Useful for round-trips.
546
+ :param include_datatypes: Datatypes are not returned by default, set to true to return a dict with the solution mappings and the datatypes.
547
+ :param transient: Should the inserted triples be transient?
548
+ :param source_graph: The IRI of the source graph to execute the construct query.
549
+ :param target_graph: The IRI of the target graph to insert into.
550
+ :param streaming: Use Polars streaming
551
+ :param include_transient: Include transient triples when querying (but see "transient" above).
552
+ :param max_rows: Maximum estimated rows in result, helps avoid out-of-memory errors.
553
+ :param debug: Why does my query have no results?
554
+ :return: None
555
+ """
556
+
557
+ def validate(
558
+ self,
559
+ shape_graph: str,
560
+ data_graph: str = None,
561
+ include_details: bool = False,
562
+ include_conforms: bool = False,
563
+ include_shape_graph: bool = True,
564
+ streaming: bool = False,
565
+ max_shape_constraint_results: int = None,
566
+ only_shapes: List[str] = None,
567
+ deactivate_shapes: List[str] = None,
568
+ dry_run: bool = False,
569
+ max_rows: int = None,
570
+ ) -> ValidationReport:
571
+ """
572
+ Validate the contained knowledge graph using SHACL
573
+ Assumes that the contained knowledge graph also contains SHACL Shapes.
574
+
575
+ :param shape_graph: The IRI of the Shape Graph.
576
+ :param data_graph: The IRI of the Data Graph (defaults to the default graph).
577
+ :param include_details: Include details of SHACL evaluation alongside the report. Currently uses a lot of memory.
578
+ :param include_conforms: Include those results that conformed. Also applies to details.
579
+ :param include_shape_graph: Include the shape graph in the report, useful when creating the graph from the report.
580
+ :param include_datatypes: Return the datatypes of the validation report (and details).
581
+ :param streaming: Use Polars streaming
582
+ :param max_shape_constraint_results: Maximum number of results per shape and constraint. Reduces the size of the result set.
583
+ :param only_shapes: Validate only these shapes, None means all shapes are validated (must be IRI, cannot be used with deactivate_shapes).
584
+ :param deactivate_shapes: Disable validation of these shapes (must be IRI, cannot be used with deactivate_shapes).
585
+ :param dry_run: Only find targets of shapes, but do not validate them.
586
+ :param max_rows: Maximum estimated rows in underlying SPARQL results, helps avoid out-of-memory errors.
587
+ :return: Validation report containing a report (report.df) and whether the graph conforms (report.conforms)
588
+ """
589
+
590
+ def read(
591
+ self,
592
+ file_path: Union[str, Path],
593
+ format: LiteralType["ntriples", "turtle", "rdf/xml", "xml", "rdfxml"] = None,
594
+ base_iri: str = None,
595
+ transient: bool = False,
596
+ parallel: bool = None,
597
+ checked: bool = True,
598
+ graph: str = None,
599
+ replace_graph: bool = False,
600
+ ) -> None:
601
+ """
602
+ Reads triples from a file path.
603
+ You can specify the format, or it will be derived using file extension, e.g. filename.ttl or filename.nt.
604
+ Specify transient if you only want the triples to be available for further querying and validation,
605
+ but not persisted using write-methods.
606
+
607
+ Usage:
608
+
609
+ >>> m.read("my_triples.ttl")
610
+
611
+ :param file_path: The path of the file containing triples
612
+ :param format: One of "ntriples", "turtle", "rdf/xml", otherwise it is inferred from the file extension.
613
+ :param base_iri: Base iri
614
+ :param transient: Should these triples be included when writing the graph to the file system?
615
+ :param parallel: Parse triples in parallel, currently only NTRiples and Turtle. Assumes all prefixes are in the beginning of the document. Defaults to true only for NTriples.
616
+ :param checked: Check IRIs etc.
617
+ :param graph: The IRI of the graph to read the triples into, if None, it will be the default graph.
618
+ :param replace_graph: Replace the graph with these triples? Will replace the default graph if no graph is specified.
619
+ """
620
+
621
+ def read_template(
622
+ self,
623
+ file_path: Union[str, Path],
624
+ ) -> None:
625
+ """
626
+ Reads template(s) from a file path.
627
+
628
+ Usage:
629
+
630
+ >>> m.read("templates.ttl")
631
+
632
+ :param file_path: The path of the file containing templates in stOTTR format
633
+ """
634
+
635
+ def reads(
636
+ self,
637
+ s: str,
638
+ format: LiteralType["ntriples", "turtle", "rdf/xml", "xml", "rdfxml"],
639
+ base_iri: str = None,
640
+ transient: bool = False,
641
+ parallel: bool = None,
642
+ checked: bool = True,
643
+ graph: str = None,
644
+ replace_graph: bool = False,
645
+ ) -> None:
646
+ """
647
+ Reads triples from a string.
648
+ Specify transient if you only want the triples to be available for further querying and validation,
649
+ but not persisted using write-methods.
650
+
651
+ Usage:
652
+
653
+ >>> m.reads(my_ntriples_string, format="ntriples")
654
+
655
+ :param s: String containing serialized triples.
656
+ :param format: One of "ntriples", "turtle", "rdf/xml".
657
+ :param base_iri: Base iri
658
+ :param transient: Should these triples be included when writing the graph to the file system?
659
+ :param parallel: Parse triples in parallel, currently only NTRiples and Turtle. Assumes all prefixes are in the beginning of the document. Defaults to true for NTriples.
660
+ :param checked: Check IRIs etc.
661
+ :param graph: The IRI of the graph to read the triples into.
662
+ :param replace_graph: Replace the graph with these triples? Will replace the default graph if no graph is specified.
663
+ """
664
+
665
+ def write_cim_xml(
666
+ self,
667
+ file_path: Union[str, Path],
668
+ profile_graph: str,
669
+ model_iri: str = None,
670
+ version: str = None,
671
+ description: str = None,
672
+ created: str = None,
673
+ scenario_time: str = None,
674
+ modeling_authority_set: str = None,
675
+ prefixes: Dict[str, str] = None,
676
+ graph: str = None,
677
+ ) -> None:
678
+ """
679
+ Write the legacy CIM XML format.
680
+
681
+ >>> PROFILE_GRAPH = "urn:graph:profiles"
682
+ >>> m = Model()
683
+ >>> m.read(model_path, base_iri=publicID, format="rdf/xml")
684
+ >>> m.read("61970-600-2_Equipment-AP-Voc-RDFS2020_v3-0-0.rdf", graph=PROFILE_GRAPH, format="rdf/xml")
685
+ >>> m.read("61970-600-2_Operation-AP-Voc-RDFS2020_v3-0-0.rdf", graph=PROFILE_GRAPH, format="rdf/xml")
686
+ >>> m.write_cim_xml(
687
+ >>> "model.xml",
688
+ >>> profile_graph=PROFILE_GRAPH,
689
+ >>> description = "MyModel",
690
+ >>> created = "2023-09-14T20:27:41",
691
+ >>> scenario_time = "2023-09-14T02:44:43",
692
+ >>> modeling_authority_set="www.westernpower.co.uk",
693
+ >>> version="22",
694
+ >>> )
695
+
696
+ :param file_path: The path of the file containing triples
697
+ :param profile_graph: The IRI of the graph containing the ontology of the CIM profile to write.
698
+ :param model_iri: model_iri a md:FullModel. Is generated if not provided.
699
+ :param version: model_iri md:Model.version version .
700
+ :param description: model_iri md:Model.description description .
701
+ :param created: model_iri md:Model.created created .
702
+ :param scenario_time: model_iri md:Model.scenarioTime scenario_time .
703
+ :param modeling_authority_set: model_iri md:Model.modelingAuthoritySet modeling_authority_set .
704
+ :param prefixes: Prefixes to be used in XML export.
705
+ :param graph: The graph to write, defaults to the default graph.
706
+ """
707
+
708
+ def write(
709
+ self,
710
+ file_path: Union[str, Path],
711
+ format=LiteralType["ntriples", "turtle", "rdf/xml"],
712
+ graph: str = None,
713
+ ) -> None:
714
+ """
715
+ Write the non-transient triples to the file path specified in the NTriples format.
716
+
717
+ Usage:
718
+
719
+ >>> m.write("my_triples.nt", format="ntriples")
720
+
721
+ :param file_path: The path of the file containing triples
722
+ :param format: One of "ntriples", "turtle", "rdf/xml".
723
+ :param graph: The IRI of the graph to write.
724
+ """
725
+
726
+ def writes(
727
+ self, format=LiteralType["ntriples", "turtle", "rdf/xml"], graph: str = None
728
+ ) -> str:
729
+ """
730
+ DEPRECATED: use writes with format="ntriples"
731
+ Write the non-transient triples to a string in memory.
732
+
733
+ Usage:
734
+
735
+ >>> s = m.write_ntriples_string(format="turtle")
736
+
737
+ :param format: One of "ntriples", "turtle", "rdf/xml".
738
+ :param graph: The IRI of the graph to write.
739
+ :return Triples in model in the NTriples format (potentially a large string)
740
+ """
741
+
742
+ def write_native_parquet(
743
+ self, folder_path: Union[str, Path], graph: str = None
744
+ ) -> None:
745
+ """
746
+ Write non-transient triples using the internal native Parquet format.
747
+
748
+ Usage:
749
+
750
+ >>> m.write_native_parquet("output_folder")
751
+
752
+ :param folder_path: The path of the folder to write triples in the native format.
753
+ :param graph: The IRI of the graph to write.
754
+ """
755
+
756
+ def create_sprout(self):
757
+ """
758
+ A sprout is a simplified way of dealing with multiple graphs.
759
+ See also `Model.insert_sprout` and `Model.detach_sprout`
760
+
761
+ :return:
762
+ """
763
+
764
+ def insert_sprout(
765
+ self,
766
+ query: str,
767
+ parameters: ParametersType = None,
768
+ include_datatypes: bool = False,
769
+ native_dataframe: bool = False,
770
+ transient: bool = False,
771
+ streaming: bool = False,
772
+ source_graph: str = None,
773
+ target_graph: str = None,
774
+ include_transient: bool = True,
775
+ max_rows:int = None,
776
+ debug:bool = None,
777
+ ):
778
+ """
779
+ Insert the results of a Construct query in a sprouted graph, which is created if no sprout is active.
780
+ Sprouts are simplified way of dealing with multiple graphs.
781
+ Useful for being able to use the same query for inspecting what will be inserted and actually inserting.
782
+ See also `Model.detach_sprout`
783
+
784
+ Usage:
785
+
786
+ >>> m = Model()
787
+ ... m.add_template(doc)
788
+ ... m.create_sprout()
789
+ ... # Omitted
790
+ ... hpizzas = '''
791
+ ... PREFIX pizza:<https://github.com/magbak/maplib/pizza#>
792
+ ... PREFIX ing:<https://github.com/magbak/maplib/pizza/ingredients#>
793
+ ... CONSTRUCT { ?p a pizza:HeterodoxPizza }
794
+ ... WHERE {
795
+ ... ?p a pizza:Pizza .
796
+ ... ?p pizza:hasIngredient ing:Pineapple .
797
+ ... }'''
798
+ ... m.insert_sprout(hpizzas)
799
+
800
+ :param query: The SPARQL Insert query string
801
+ :param parameters: PVALUES Parameters, a DataFrame containing the value bindings in the custom PVALUES construction.
802
+ :param native_dataframe: Return columns with maplib-native formatting. Useful for round-trips.
803
+ :param include_datatypes: Datatypes are not returned by default, set to true to return a dict with the solution mappings and the datatypes.
804
+ :param transient: Should the inserted triples be included in exports?
805
+ :param source_graph: The IRI of the source graph to execute the construct query.
806
+ :param target_graph: The IRI of the target graph to insert into.
807
+ :param streaming: Use Polars streaming
808
+ :param include_transient: Include transient triples when querying (see also "transient" above).
809
+ :param max_rows: Maximum estimated rows in result, helps avoid out-of-memory errors.
810
+ :param debug: Why does my query have no results?
811
+ :return: None
812
+ """
813
+
814
+ def detach_sprout(self) -> "Model":
815
+ """
816
+ Detaches and returns the sprout from the model.
817
+
818
+ :return: The sprout as its own Model.
819
+ """
820
+
821
+ def get_predicate_iris(
822
+ self, graph: str = None, include_transient: bool = False
823
+ ) -> List["IRI"]:
824
+ """
825
+ :param graph: The graph to get the predicate iris from.
826
+ :param include_transient: Should we include predicates only between transient triples?
827
+ :return: The IRIs of the predicates currently in the given graph.
828
+ """
829
+
830
+ def get_predicate(
831
+ self, iri: "IRI", graph: str = None, include_transient: bool = False
832
+ ) -> List["SolutionMappings"]:
833
+ """
834
+ :param iri: The predicate IRI
835
+ :param graph: The graph to get the predicate from.
836
+ :param include_transient: Should we include transient triples?
837
+ :return: A list of the underlying tables that store a given predicate.
838
+ """
839
+
840
+ def create_index(
841
+ self, options: "IndexingOptions" = None, all: bool = True, graph: str = None
842
+ ):
843
+ """
844
+ :param options: Indexing options
845
+ :param all: Apply to all existing and new graphs
846
+ :param graph: The graph where indexes should be added
847
+ :return:
848
+ """
849
+
850
+ def infer(
851
+ self,
852
+ ruleset: Union[str, List[str]],
853
+ graph: str = None,
854
+ include_datatypes: bool = False,
855
+ native_dataframe: bool = False,
856
+ max_iterations: int = 100_000,
857
+ max_results: int = 10_000_000,
858
+ include_transient: bool = True,
859
+ max_rows: int = 100_000_000,
860
+ debug: bool = False,
861
+ ) -> Optional[Dict[str, DataFrame]]:
862
+ """
863
+ Run the inference rules that are provided
864
+ :param ruleset: The Datalog ruleset (a string).
865
+ :param graph: Apply the ruleset to this graph, defaults to the default graph, or the graph specified in the rules.
866
+ :param native_dataframe: Return columns with maplib-native formatting. Useful for round-trips.
867
+ :param include_datatypes: Datatypes are not returned by default, set to true to return a dict with the solution mappings and the datatypes.
868
+ :param max_iterations: Maximum number of iterations.
869
+ :param max_results: Maximum number of results.
870
+ :param include_transient: Include transient triples when reasoning.
871
+ :param max_rows: Maximum estimated rows in result, helps avoid out-of-memory errors.
872
+ :param debug: Debugs rule bodies for executions that give no triples.
873
+ :return: The inferred N-Tuples.
874
+ """
875
+
876
+ class MaplibException(Exception): ...
877
+
878
+ def explore(
879
+ m: "Model",
880
+ host: str = "localhost",
881
+ port: int = 8000,
882
+ bind: str = "localhost",
883
+ popup=True,
884
+ fts=True,
885
+ ):
886
+ """Starts a graph explorer session.
887
+ To run from Jupyter Notebook use:
888
+ >>> from maplib import explore
889
+ >>>
890
+ >>> server = explore(m)
891
+ You can later stop the server with
892
+ >>> server.stop()
893
+
894
+ :param m: The Model to explore
895
+ :param host: The hostname that we will point the browser to.
896
+ :param port: The port where the graph explorer webserver listens on.
897
+ :param bind: Bind to the following host / ip.
898
+ :param popup: Pop up the browser window.
899
+ :param fts: Enable full text search indexing
900
+ """
901
+
902
+ def generate_templates(m: Model, graph: Optional[str]) -> Dict[str, Template]:
903
+ """Generate templates for instantiating the classes in an ontology
904
+
905
+ :param m: The model where the ontology is stored. We mainly rely on rdfs:subClassOf, rdfs:range and rdfs:domain.
906
+ :param graph: The named graph where the ontology is stored.
907
+
908
+ :return A dictionary of templates for instantiating the classes in the ontology, where the keys are the class URIs.
909
+
910
+ Usage example - note that it is important to add the templates to the Model you want to populate."""