maplib

 1# r'''
 2# # Overview
 3#
 4# '''
 5
 6import logging
 7logger = logging.getLogger(__name__)
 8
 9__all__ = [
10    "Model",
11    "a",
12    "Triple",
13    "SolutionMappings",
14    "IndexingOptions",
15    "ValidationReport",
16    "Instance",
17    "Template",
18    "Argument",
19    "Parameter",
20    "Variable",
21    "RDFType",
22    "xsd",
23    "rdf",
24    "rdfs",
25    "owl",
26    "IRI",
27    "Literal",
28    "Prefix",
29    "BlankNode",
30    "explore",
31    "add_triples",
32    "generate_templates",
33    "MaplibException",
34]
35
36import pathlib
37from importlib.metadata import version
38from .maplib import *
39from .adding_triples import add_triples
40from .template_generator import generate_templates
41
42"""
43http://www.w3.org/1999/02/22-rdf-syntax-ns#type
44"""
45a = rdf.type
46
47if (pathlib.Path(__file__).parent.resolve() / "graph_explorer").exists():
48    from .graph_explorer import explore as _explore
49else:
50
51    def _explore(
52        m: "Model",
53        host: str = "localhost",
54        port: int = 8000,
55        bind: str = "localhost",
56        popup=True,
57        fts=True,
58        fts_path:str="fts",
59    ):
60        print("Contact Data Treehouse to try!")
61
62
63def explore(*args, **kwargs):
64    """Deprecated way to start an explore session.
65Use the explore method on a Model object instead
66"""
67    logger.warn("Calling `maplib.explore` is deprecated, use `m.explore()` on a `Model` object instead")
68    if kwargs.get("popup") == None or kwargs.get("popup") == True:
69        logger.warn("""Calling explore without a popup argument defaults to it being on.
70The popup argument is deprecated, so if you are relying on explore() opening a browser window
71please change this to something like
72
73```
74import webbrowser
75from maplib import Model
76
77m = Model()
78...
79s = m.explore()
80webbrowser.open(s.url, new=2)
81```
82""")
83        kwargs["popup"] = True
84    elif kwargs.get("popup") == False:
85        logger.warn("The new explore function on a Model, no longer defaults to popping up the browser ")
86
87
88    return _explore(*args, **kwargs)
89
90__version__ = version("maplib")
class Model:

A model session allowing:

  • Iterative model using OTTR templates
  • Interactive SPARQL querying and enrichment
  • SHACL validation

Usage:

>>> from maplib import Model
... doc = '''
... @prefix ex:<http://example.net/ns#>.
... ex:ExampleTemplate [?MyValue] :: {
...    ottr:Triple(ex:myObject, ex:hasValue, ?MyValue)
... } .'''
... m = Model()
... m.add_template(doc)
Parameters
  • documents: a stOTTR document or a list of these
  • indexing_options: options for indexing
Model(indexing_options: IndexingOptions = None)
def add_template(self, template: Union[Template, str]):

Add a template to the model. Overwrites any existing template with the same IRI.

Parameters
  • template: The template to add, as a stOTTR string or as a programmatically constructed Template.
Returns
def add_prefixes(self, prefixes: Dict[str, str]):

Add prefixes that will be used in parsing of SPARQL, Datalog and OTTR.

Usage:

>>> m.add_prefixes({"ex" : "http:://example.net/"})
Parameters
  • prefixes: Known prefixes
Returns
def detach_graph(self, graph: str = None, preserve_name: bool = False) -> Model:

Detaches and returns a named graph as their own Model object. The named graph is removed from the original Model.

Parameters
  • graph: The name of the graph to detach. Defaults to the default graph.
  • preserve_name: Preserve the name of the graph in the new Model, defaults to False.
Returns

A model.

def map( self, template: Union[str, Template, IRI], df: polars.dataframe.frame.DataFrame = None, graph: str = None, types: Dict[str, RDFType] = None, validate_iris: bool = True) -> None:

Map a template using a DataFrame Usage:

>>> m.map("ex:ExampleTemplate", df)

If the template has no arguments, the df argument is not necessary.

Parameters
  • template: Template, IRI, IRI string or prefixed template name.
  • df: DataFrame where the columns have the same names as the template arguments
  • graph: The IRI of the graph to add triples to.
  • types: The types of the columns.
  • validate_iris: Validate any IRI-columns.
def map_json( self, path_or_string: str, graph: str = None, transient: bool = True) -> None:

Map a JSON file or string to triples. Usage:

>>> m.map_json("my_doc.json")

or:

>>> m.map_json('{"my_key":[true, "abc"]}')
Parameters
  • path_or_string: Path to a JSON document or a JSON string.
  • graph: The IRI of the graph to add triples to. None is the default graph.
  • transient: Should the triples be included when serializing the graph?
def map_triples( self, df: polars.dataframe.frame.DataFrame = None, predicate: str = None, graph: str = None, types: Dict[str, RDFType] = None, validate_iris: bool = True) -> None:

Map a template using a DataFrame with columns subject, object and predicate The predicate column can also be supplied as a string if it is the same for all rows. Usage:

>>> m.map_triples(df)

If the template has no arguments, the df argument is not necessary.

Parameters
  • df: DataFrame where the columns are named subject and object. May also contain a verb-column.
  • verb: The uri of the verb.
  • graph: The IRI of the graph to add triples to.
  • types: The types of the columns.
  • validate_iris: Validate any IRI-columns.
def map_default( self, df: polars.dataframe.frame.DataFrame, primary_key_column: str, dry_run: bool = False, graph: str = None, types: Dict[str, RDFType] = None, validate_iris: bool = True) -> str:

Create a default template and map it based on a dataframe. Usage:

>>> template_string = m.map_default(df, "myKeyCol")
... print(template_string)
Parameters
  • df: DataFrame where the columns have the same names as the template arguments
  • primary_key_column: This column will be the subject of all triples in the generated template.
  • dry_run: Do not map the template, only return the string.
  • graph: The IRI of the graph to add triples to.
  • types: The types of the columns.
  • validate_iris: Validate any IRI-columns.
Returns

The generated template

def explore( self, host: str = 'localhost', port: int = 8000, bind: str = 'localhost', popup=False, fts=True, fts_path: str = 'fts') -> 'ExploreServer':

Starts a graph explorer session. To run from Jupyter Notebook use:

>>> server = m.explore()
You can later stop the server with
>>> server.stop()
Parameters
  • host: The hostname that we will point the browser to.
  • port: The port where the graph explorer webserver listens on.
  • bind: Bind to the following host / ip.
  • fts: Enable full text search indexing
def query( self, query: str, parameters: Dict[str, Tuple[polars.dataframe.frame.DataFrame, Dict[str, RDFType]]] = None, include_datatypes: bool = False, native_dataframe: bool = False, graph: str = None, streaming: bool = False, return_json: bool = False, include_transient: bool = True, max_rows: int = None, debug: bool = False) -> Union[polars.dataframe.frame.DataFrame, SolutionMappings, List[Union[polars.dataframe.frame.DataFrame, SolutionMappings, str]], NoneType]:

Query the contained knowledge graph using SPARQL Currently, SELECT, CONSTRUCT and INSERT are supported. Usage:

>>> df = model.query('''
... PREFIX ex:<http://example.net/ns#>
... SELECT ?obj1 ?obj2 WHERE {
...    ?obj1 ex:hasObj ?obj2
... }''')
... print(df)
Parameters
  • query: The SPARQL query string
  • parameters: PVALUES Parameters, a DataFrame containing the value bindings in the custom PVALUES construction.
  • native_dataframe: Return columns with maplib-native formatting. Useful for round-trips.
  • include_datatypes: Datatypes are not returned by default, set to true to return a dict with the solution mappings and the datatypes.
  • graph: The IRI of the graph to query.
  • streaming: Use Polars streaming
  • return_json: Return JSON string.
  • include_transient: Include transient triples when querying.
  • max_rows: Maximum estimated rows in result, helps avoid out-of-memory errors.
  • debug: Why does my query have no results?
Returns

DataFrame (Select), list of DataFrames (Construct) containing results, None for Insert-queries, or SolutionMappings when include_datatypes is set.

def update( self, update: str, parameters: Dict[str, Tuple[polars.dataframe.frame.DataFrame, Dict[str, RDFType]]] = None, streaming: bool = False, include_transient: bool = True, max_rows: int = None, debug: bool = False):

Insert the results of a Construct query in the graph. Useful for being able to use the same query for inspecting what will be inserted and actually inserting. Usage:

>>> m = Model(doc)
... # Omitted
... update_pizzas = '''
... ...'''
... m.update(update_pizzas)
Parameters
  • update: The SPARQL Update string
  • parameters: PVALUES Parameters, a DataFrame containing the value bindings in the custom PVALUES construction.
  • streaming: Use Polars streaming
  • include_transient: Include transient triples when querying (but see "transient" above).
  • max_rows: Maximum estimated rows in result, helps avoid out-of-memory errors.
  • debug: Why does my query have no results?
Returns

None

def create_index( self, options: IndexingOptions = None, all: bool = True, graph: str = None):
Parameters
  • options: Indexing options
  • all: Apply to all existing and new graphs
  • graph: The graph where indexes should be added
Returns
def validate( self, shape_graph: str, data_graph: str = None, include_details: bool = False, include_conforms: bool = False, include_shape_graph: bool = True, streaming: bool = False, max_shape_constraint_results: int = None, only_shapes: List[str] = None, deactivate_shapes: List[str] = None, dry_run: bool = False, max_rows: int = None, serial: bool = False) -> ValidationReport:

Validate the contained knowledge graph using SHACL Assumes that the contained knowledge graph also contains SHACL Shapes.

Parameters
  • shape_graph: The IRI of the Shape Graph.
  • data_graph: The IRI of the Data Graph (defaults to the default graph).
  • include_details: Include details of SHACL evaluation alongside the report. Currently uses a lot of memory.
  • include_conforms: Include those results that conformed. Also applies to details.
  • include_shape_graph: Include the shape graph in the report, useful when creating the graph from the report.
  • include_datatypes: Return the datatypes of the validation report (and details).
  • streaming: Use Polars streaming
  • max_shape_constraint_results: Maximum number of results per shape and constraint. Reduces the size of the result set.
  • only_shapes: Validate only these shapes, None means all shapes are validated (must be IRI, cannot be used with deactivate_shapes).
  • deactivate_shapes: Disable validation of these shapes (must be IRI, cannot be used with deactivate_shapes).
  • dry_run: Only find targets of shapes, but do not validate them.
  • max_rows: Maximum estimated rows in underlying SPARQL results, helps avoid out-of-memory errors.
  • serial: Turns off most parallell validation of shapes.
Returns

Validation report containing a report (report.df) and whether the graph conforms (report.conforms)

def insert( self, query: str, parameters: Dict[str, Tuple[polars.dataframe.frame.DataFrame, Dict[str, RDFType]]] = None, include_datatypes: bool = False, native_dataframe: bool = False, transient: bool = False, streaming: bool = False, source_graph: str = None, target_graph: str = None, include_transient: bool = True, max_rows: int = None, debug: bool = False):

Insert the results of a Construct query in the graph. Useful for being able to use the same query for inspecting what will be inserted and actually inserting. Usage:

>>> m = Model(doc)
... # Omitted
... hpizzas = '''
... PREFIX pizza:<https://github.com/magbak/maplib/pizza#>
... PREFIX ing:<https://github.com/magbak/maplib/pizza/ingredients#>
... CONSTRUCT { ?p a pizza:HeterodoxPizza }
... WHERE {
... ?p a pizza:Pizza .
... ?p pizza:hasIngredient ing:Pineapple .
... }'''
... m.insert(hpizzas)
Parameters
  • query: The SPARQL Insert query string
  • parameters: PVALUES Parameters, a DataFrame containing the value bindings in the custom PVALUES construction.
  • native_dataframe: Return columns with maplib-native formatting. Useful for round-trips.
  • include_datatypes: Datatypes are not returned by default, set to true to return a dict with the solution mappings and the datatypes.
  • transient: Should the inserted triples be transient?
  • source_graph: The IRI of the source graph to execute the construct query.
  • target_graph: The IRI of the target graph to insert into.
  • streaming: Use Polars streaming
  • include_transient: Include transient triples when querying (but see "transient" above).
  • max_rows: Maximum estimated rows in result, helps avoid out-of-memory errors.
  • debug: Why does my query have no results?
Returns

None

def read( self, file_path: Union[str, pathlib.Path], format: Literal['ntriples', 'turtle', 'rdf/xml', 'cim/xml', 'json-ld'] = None, base_iri: str = None, transient: bool = False, parallel: bool = None, checked: bool = True, graph: str = None, replace_graph: bool = False, known_contexts: Dict[str, str] = None) -> None:

Reads triples from a file path. You can specify the format, or it will be derived using file extension, e.g. filename.ttl or filename.nt. Specify transient if you only want the triples to be available for further querying and validation, but not persisted using write-methods.

Usage:

>>> m.read("my_triples.ttl")
Parameters
  • file_path: The path of the file containing triples
  • format: One of "ntriples", "turtle", "rdf/xml", "json-ld" or "cim/xml", otherwise it is inferred from the file extension.
  • base_iri: Base iri
  • transient: Should these triples be included when writing the graph to the file system?
  • 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.
  • checked: Check IRIs etc.
  • graph: The IRI of the graph to read the triples into, if None, it will be the default graph.
  • replace_graph: Replace the graph with these triples? Will replace the default graph if no graph is specified.
  • known_contexts: Contexts in JSON-LD documents are resolved towards this dict.
def read_template(self, file_path: Union[str, pathlib.Path]) -> None:

Reads template(s) from a file path.

Usage:

>>> m.read("templates.ttl")
Parameters
  • file_path: The path of the file containing templates in stOTTR format
def reads( self, s: str, format: Literal['ntriples', 'turtle', 'rdf/xml', 'cim/xml', 'json-ld'], base_iri: str = None, transient: bool = False, parallel: bool = None, checked: bool = True, graph: str = None, replace_graph: bool = False, known_contexts: Dict[str, str] = None) -> None:

Reads triples from a string. Specify transient if you only want the triples to be available for further querying and validation, but not persisted using write-methods.

Usage:

>>> m.reads(my_ntriples_string, format="ntriples")
Parameters
  • s: String containing serialized triples.
  • format: One of "ntriples", "turtle", "rdf/xml", "json-ld" or "cim/xml".
  • base_iri: Base iri
  • transient: Should these triples be included when writing the graph to the file system?
  • 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.
  • checked: Check IRIs etc.
  • graph: The IRI of the graph to read the triples into.
  • replace_graph: Replace the graph with these triples? Will replace the default graph if no graph is specified.
  • known_contexts: Contexts in JSON-LD documents are resolved towards this dict.
def write( self, file_path: Union[str, pathlib.Path], format=typing.Literal['ntriples', 'turtle', 'rdf/xml'], graph: str = None, prefixes: Dict[str, str] = None) -> None:

Write the non-transient triples to the file path specified in the NTriples format.

Usage:

>>> m.write("my_triples.nt", format="ntriples")
Parameters
  • file_path: The path of the file containing triples
  • format: One of "ntriples", "turtle", "rdf/xml".
  • graph: The IRI of the graph to write.
  • prefixes: The prefixes that will be used in turtle serialization.
def write_cim_xml( self, file_path: Union[str, pathlib.Path], profile_graph: str, model_iri: str = None, version: str = None, description: str = None, created: str = None, scenario_time: str = None, modeling_authority_set: str = None, prefixes: Dict[str, str] = None, graph: str = None) -> None:

Write the legacy CIM XML format.

>>> PROFILE_GRAPH = "urn:graph:profiles"
>>> m = Model()
>>> m.read(model_path, base_iri=publicID, format="rdf/xml")
>>> m.read("61970-600-2_Equipment-AP-Voc-RDFS2020_v3-0-0.rdf", graph=PROFILE_GRAPH, format="rdf/xml")
>>> m.read("61970-600-2_Operation-AP-Voc-RDFS2020_v3-0-0.rdf", graph=PROFILE_GRAPH, format="rdf/xml")
>>> m.write_cim_xml(
>>>     "model.xml",
>>>     profile_graph=PROFILE_GRAPH,
>>>     description = "MyModel",
>>>     created = "2023-09-14T20:27:41",
>>>     scenario_time = "2023-09-14T02:44:43",
>>>     modeling_authority_set="www.westernpower.co.uk",
>>>     version="22",
>>> )
Parameters
  • file_path: The path of the file containing triples
  • profile_graph: The IRI of the graph containing the ontology of the CIM profile to write.
  • model_iri: model_iri a md: FullModel. Is generated if not provided.
  • version: model_iri md: Model.version version .
  • description: model_iri md: Model.description description .
  • created: model_iri md: Model.created created .
  • scenario_time: model_iri md: Model.scenarioTime scenario_time .
  • modeling_authority_set: model_iri md: Model.modelingAuthoritySet modeling_authority_set .
  • prefixes: Prefixes to be used in XML export.
  • graph: The graph to write, defaults to the default graph.
def writes( self, format=typing.Literal['ntriples', 'turtle', 'rdf/xml'], graph: str = None, prefixes: Dict[str, str] = None) -> str:

Write the non-transient triples to a string in memory.

Usage:

>>> s = m.writes(format="turtle")
Parameters
  • format: One of "ntriples", "turtle", "rdf/xml".
  • graph: The IRI of the graph to write.
  • prefixes: The prefixes used for turtle serialization. :return Triples in model in the NTriples format (potentially a large string)
def write_native_parquet(self, folder_path: Union[str, pathlib.Path], graph: str = None) -> None:

Write non-transient triples using the internal native Parquet format.

Usage:

>>> m.write_native_parquet("output_folder")
Parameters
  • folder_path: The path of the folder to write triples in the native format.
  • graph: The IRI of the graph to write.
def get_predicate_iris( self, graph: str = None, include_transient: bool = False) -> List[IRI]:
Parameters
  • graph: The graph to get the predicate iris from.
  • include_transient: Should we include predicates only between transient triples?
Returns

The IRIs of the predicates currently in the given graph.

def get_predicate( self, iri: IRI, graph: str = None, include_transient: bool = False) -> List[SolutionMappings]:
Parameters
  • iri: The predicate IRI
  • graph: The graph to get the predicate from.
  • include_transient: Should we include transient triples?
Returns

A list of the underlying tables that store a given predicate.

def infer( self, ruleset: Union[str, List[str]], graph: str = None, include_datatypes: bool = False, native_dataframe: bool = False, max_iterations: int = 100000, max_results: int = 10000000, include_transient: bool = True, max_rows: int = 100000000, debug: bool = False) -> Optional[Dict[str, polars.dataframe.frame.DataFrame]]:

Run the inference rules that are provided

Parameters
  • ruleset: The Datalog ruleset (a string).
  • graph: Apply the ruleset to this graph, defaults to the default graph, or the graph specified in the rules.
  • native_dataframe: Return columns with maplib-native formatting. Useful for round-trips.
  • include_datatypes: Datatypes are not returned by default, set to true to return a dict with the solution mappings and the datatypes.
  • max_iterations: Maximum number of iterations.
  • max_results: Maximum number of results.
  • include_transient: Include transient triples when reasoning.
  • max_rows: Maximum estimated rows in result, helps avoid out-of-memory errors.
  • debug: Debugs rule bodies for executions that give no triples.
Returns

The inferred N-Tuples.

a = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#type)
def Triple( subject: Union[Argument, IRI, Variable, BlankNode], predicate: Union[Argument, IRI, Variable, BlankNode], object: Union[Argument, IRI, Variable, Literal, BlankNode], list_expander: Optional[Literal['cross', 'zipMin', 'zipMax']] = None):

An OTTR Triple Pattern used for creating templates. This is the basis pattern which all template instances are rewritten into. Equivalent to:

>>> ottr = Prefix("http://ns.ottr.xyz/0.4/")
... Instance(ottr.suf("Triple"), subject, predicate, object, list_expander)
Parameters
  • subject:
  • predicate:
  • object:
  • list_expander:
Returns
class SolutionMappings:

Detailed information about the solution mappings, the types of the variables and debugging for queries.

debug: Optional[str]
mappings: polars.dataframe.frame.DataFrame
rdf_types: Dict[str, RDFType]
pushdown_paths
class IndexingOptions:

Options for indexing

IndexingOptions( object_sort_all: bool = None, object_sort_some: List[IRI] = None, fts_path: str = None, subject_object_index: bool = None)

Defaults to indexing on subjects and objects for select types (e.g. rdf:type and rdfs:label)

Parameters
  • object_sort_all: Enable object-indexing for all suitable predicates (doubles memory requirement).
  • object_sort_some: Enable object-indexing for a selected list of predicates.
  • fts_path: Enable full text search, stored at the path
  • subject_object_index: An index used to deduplicate before insertion, speeds up mapping at a moderate memory cost
class ValidationReport:

SHACL Validation report. Only constructed by maplib.

def results( self, native_dataframe: bool = False, include_datatypes: bool = False, streaming: bool = False) -> Union[polars.dataframe.frame.DataFrame, SolutionMappings, NoneType]:

Return the results of the validation report, if they exist.

Parameters
  • native_dataframe: Return columns with maplib-native formatting. Useful for round-trips.
  • include_datatypes: Return datatypes of the results DataFrame (returns SolutionMappings instead of DataFrame).
  • streaming: Use the Polars streaming functionality.
Returns

The SHACL validation report, as a DataFrame

def details( self, native_dataframe: bool = False, include_datatypes: bool = False, streaming: bool = False) -> Optional[polars.dataframe.frame.DataFrame]:

Returns the details of the validation report. Only available if validation was called with include_details=True.

Parameters
  • native_dataframe: Return columns with maplib-native formatting. Useful for round-trips.
  • include_datatypes: Return datatypes of the results DataFrame (returns SolutionMappings instead of DataFrame).
  • streaming: Use the Polars streaming functionality.
Returns

Details of the SHACL validation report, as a DataFrame

def graph(self) -> 'Mapping':

Creates a new model object where the base graph is the validation report with results. Includes the details of the validation report in the new graph if they exist.

Returns
conforms: bool

Whether or not the validation report conforms to the shapes

shape_targets: polars.dataframe.frame.DataFrame

A DataFrame containing the counts of the targets of each shape and constraint

performance: polars.dataframe.frame.DataFrame

Performance statistics for the validation process

class Instance:
Instance( iri: IRI, arguments: List[Union[Argument, Variable, IRI, Literal, BlankNode, NoneType]], list_expander: Optional[Literal['cross', 'zipMin', 'zipMax']] = None)

A template instance.

Parameters
  • iri: The IRI of the template to be instantiated.
  • arguments: The arguments for template instantiation.
  • list_expander: (How) should we do list expansion?
iri
class Template:
Template( iri: IRI, parameters: List[Union[Parameter, Variable]], instances: List[Instance])

Create a new OTTR Template

Parameters
  • iri: The IRI of the template
  • parameters:
  • instances:
def instance( self, arguments: List[Union[Argument, Variable, IRI, Literal, NoneType]], list_expander: Literal['cross', 'zipMin', 'zipMax'] = None) -> Instance:
Parameters
  • arguments: The arguments to the template.
  • list_expander: (How) should we list-expand?
Returns
instances: List[Instance]

An OTTR Template. Note that accessing parameters- or instances-fields returns copies. To change these fields, you must assign new lists of parameters or instances.

parameters: List[Parameter]
iri: str
class Argument:
Argument( term: Union[Variable, IRI, Literal], list_expand: Optional[bool] = False)

An argument for a template instance.

Parameters
  • term: The term.
  • list_expand: Should the argument be expanded? Used with the list_expander argument of instance.
variable
class Parameter:
Parameter( variable: Variable, optional: Optional[bool] = False, allow_blank: Optional[bool] = True, rdf_type: Optional[RDFType] = None, default_value: Union[Literal, IRI, BlankNode, NoneType] = None)

Create a new parameter for a Template.

Parameters
  • variable: The variable.
  • optional: Can the variable be unbound?
  • allow_blank: Can the variable be bound to a blank node?
  • rdf_type: The type of the variable. Can be nested.
  • default_value: Default value when no value provided.
optional: bool
rdf_type: Optional[RDFType]
variable: Variable
default_value: Union[Literal, IRI, BlankNode, NoneType]

Parameters for template signatures.

allow_blank: bool
class Variable:

A variable in a template.

Variable(name: str)

Create a new variable.

Parameters
  • name: The name of the variable.
name: str
class RDFType:

The type of a column containing a RDF variable. For instance, IRIs are RDFType.IRI and a string literal is RDFType.Literal("http://www.w3.org/2001/XMLSchema#string")

def Literal(iri):
def Nested(rdf_type):
def Multi(rdf_types):
Unknown: RDFType = RDFType.None()
class xsd:
boolean: IRI = IRI(http://www.w3.org/2001/XMLSchema#boolean)
byte: IRI = IRI(http://www.w3.org/2001/XMLSchema#byte)
date: IRI = IRI(http://www.w3.org/2001/XMLSchema#date)
dateTime: IRI = IRI(http://www.w3.org/2001/XMLSchema#dateTime)
dateTimeStamp: IRI = IRI(http://www.w3.org/2001/XMLSchema#dateTimeStamp)
decimal: IRI = IRI(http://www.w3.org/2001/XMLSchema#decimal)
double: IRI = IRI(http://www.w3.org/2001/XMLSchema#double)
duration: IRI = IRI(http://www.w3.org/2001/XMLSchema#duration)
float: IRI = IRI(http://www.w3.org/2001/XMLSchema#float)
int_: IRI = IRI(http://www.w3.org/2001/XMLSchema#int)
integer: IRI = IRI(http://www.w3.org/2001/XMLSchema#integer)
language: IRI = IRI(http://www.w3.org/2001/XMLSchema#language)
long: IRI = IRI(http://www.w3.org/2001/XMLSchema#long)
short: IRI = IRI(http://www.w3.org/2001/XMLSchema#short)
string: IRI = IRI(http://www.w3.org/2001/XMLSchema#string)
anyURI: IRI = IRI(http://www.w3.org/2001/XMLSchema#anyURI)
dayTimeDuration: IRI = IRI(http://www.w3.org/2001/XMLSchema#dayTimeDuration)
base64Binary: IRI = IRI(http://www.w3.org/2001/XMLSchema#base64Binary)
gDay: IRI = IRI(http://www.w3.org/2001/XMLSchema#gDay)
gMonthDay: IRI = IRI(http://www.w3.org/2001/XMLSchema#gMonthDay)
gMonth: IRI = IRI(http://www.w3.org/2001/XMLSchema#gMonth)
gYear: IRI = IRI(http://www.w3.org/2001/XMLSchema#gYear)
gYearMonth: IRI = IRI(http://www.w3.org/2001/XMLSchema#gYearMonth)
hexBinary: IRI = IRI(http://www.w3.org/2001/XMLSchema#hexBinary)
Name = IRI(http://www.w3.org/2001/XMLSchema#Name)
NCName = IRI(http://www.w3.org/2001/XMLSchema#NCName)
NMTOKEN = IRI(http://www.w3.org/2001/XMLSchema#NMTOKEN)
negativeInteger: IRI = IRI(http://www.w3.org/2001/XMLSchema#negativeInteger)
nonNegativeInteger: IRI = IRI(http://www.w3.org/2001/XMLSchema#nonNegativeInteger)
nonPositiveInteger: IRI = IRI(http://www.w3.org/2001/XMLSchema#nonPositiveInteger)
normalizedString: IRI = IRI(http://www.w3.org/2001/XMLSchema#normalizedString)
token: IRI = IRI(http://www.w3.org/2001/XMLSchema#token)
unsignedByte: IRI = IRI(http://www.w3.org/2001/XMLSchema#unsignedByte)
unsignedInt: IRI = IRI(http://www.w3.org/2001/XMLSchema#unsignedInt)
unsignedLong: IRI = IRI(http://www.w3.org/2001/XMLSchema#unsignedLong)
unsignedShort: IRI = IRI(http://www.w3.org/2001/XMLSchema#unsignedShort)
yearMonthDuration: IRI = IRI(http://www.w3.org/2001/XMLSchema#yearMonthDuration)
class rdf:
type: IRI = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#type)
Alt: IRI = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#Alt)
Bag: IRI = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag)
first: IRI = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#first)
HTML: IRI = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#HTML)
langString: IRI = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#langString)
List: IRI = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#List)
nil: IRI = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#nil)
object: IRI = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#object)
predicate: IRI = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate)
Property: IRI = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#Property)
rest: IRI = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#rest)
Seq: IRI = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#Seq)
Statement: IRI = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement)
subject: IRI = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#subject)
value: IRI = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#value)
XMLLiteral: IRI = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral)
class rdfs:
Class: IRI = IRI(http://www.w3.org/2000/01/rdf-schema#Class)
comment: IRI = IRI(http://www.w3.org/2000/01/rdf-schema#comment)
Container: IRI = IRI(http://www.w3.org/2000/01/rdf-schema#Container)
Datatype: IRI = IRI(http://www.w3.org/2000/01/rdf-schema#Datatype)
domain: IRI = IRI(http://www.w3.org/2000/01/rdf-schema#domain)
ContainerMembershipProperty: IRI = IRI(http://www.w3.org/2000/01/rdf-schema#ContainerMembershipProperty)
isDefinedBy: IRI = IRI(http://www.w3.org/2000/01/rdf-schema#isDefinedBy)
label: IRI = IRI(http://www.w3.org/2000/01/rdf-schema#label)
Literal: IRI = IRI(http://www.w3.org/2000/01/rdf-schema#Literal)
member: IRI = IRI(http://www.w3.org/2000/01/rdf-schema#member)
range: IRI = IRI(http://www.w3.org/2000/01/rdf-schema#range)
seeAlso: IRI = IRI(http://www.w3.org/2000/01/rdf-schema#seeAlso)
subClassOf: IRI = IRI(http://www.w3.org/2000/01/rdf-schema#subClassOf)
subPropertyOf: IRI = IRI(http://www.w3.org/2000/01/rdf-schema#subPropertyOf)
Resource: IRI = IRI(http://www.w3.org/2000/01/rdf-schema#Resource)
class owl:

The owl namespace: http://www.w3.org/2002/07/owl#

allValuesFrom: IRI = IRI(http://www.w3.org/2002/07/owl#allValuesFrom)
annotatedProperty: IRI = IRI(http://www.w3.org/2002/07/owl#annotatedProperty)
annotatedSource: IRI = IRI(http://www.w3.org/2002/07/owl#annotatedSource)
annotatedTarget: IRI = IRI(http://www.w3.org/2002/07/owl#annotatedTarget)
assertionProperty: IRI = IRI(http://www.w3.org/2002/07/owl#assertionProperty)
cardinality: IRI = IRI(http://www.w3.org/2002/07/owl#cardinality)
complementOf: IRI = IRI(http://www.w3.org/2002/07/owl#complementOf)
datatypeComplementOf: IRI = IRI(http://www.w3.org/2002/07/owl#datatypeComplementOf)
differentFrom: IRI = IRI(http://www.w3.org/2002/07/owl#differentFrom)
disjointUnionOf: IRI = IRI(http://www.w3.org/2002/07/owl#disjointUnionOf)
disjointWith: IRI = IRI(http://www.w3.org/2002/07/owl#disjointWith)
distinctMembers: IRI = IRI(http://www.w3.org/2002/07/owl#distinctMembers)
equivalentClass: IRI = IRI(http://www.w3.org/2002/07/owl#equivalentClass)
equivalentProperty: IRI = IRI(http://www.w3.org/2002/07/owl#equivalentProperty)
hasKey: IRI = IRI(http://www.w3.org/2002/07/owl#hasKey)
hasSelf: IRI = IRI(http://www.w3.org/2002/07/owl#hasSelf)
hasValue: IRI = IRI(http://www.w3.org/2002/07/owl#hasValue)
intersectionOf: IRI = IRI(http://www.w3.org/2002/07/owl#intersectionOf)
inverseOf: IRI = IRI(http://www.w3.org/2002/07/owl#inverseOf)
maxCardinality: IRI = IRI(http://www.w3.org/2002/07/owl#maxCardinality)
maxQualifiedCardinality: IRI = IRI(http://www.w3.org/2002/07/owl#maxQualifiedCardinality)
members: IRI = IRI(http://www.w3.org/2002/07/owl#members)
minCardinality: IRI = IRI(http://www.w3.org/2002/07/owl#minCardinality)
minQualifiedCardinality: IRI = IRI(http://www.w3.org/2002/07/owl#minQualifiedCardinality)
onClass: IRI = IRI(http://www.w3.org/2002/07/owl#onClass)
onDataRange: IRI = IRI(http://www.w3.org/2002/07/owl#onDataRange)
onDatatype: IRI = IRI(http://www.w3.org/2002/07/owl#onDatatype)
onProperties: IRI = IRI(http://www.w3.org/2002/07/owl#onProperties)
onProperty: IRI = IRI(http://www.w3.org/2002/07/owl#onProperty)
oneOf: IRI = IRI(http://www.w3.org/2002/07/owl#oneOf)
propertyChainAxiom: IRI = IRI(http://www.w3.org/2002/07/owl#propertyChainAxiom)
propertyDisjointWith: IRI = IRI(http://www.w3.org/2002/07/owl#propertyDisjointWith)
qualifiedCardinality: IRI = IRI(http://www.w3.org/2002/07/owl#qualifiedCardinality)
sameAs: IRI = IRI(http://www.w3.org/2002/07/owl#sameAs)
someValuesFrom: IRI = IRI(http://www.w3.org/2002/07/owl#someValuesFrom)
sourceIndividual: IRI = IRI(http://www.w3.org/2002/07/owl#sourceIndividual)
targetIndividual: IRI = IRI(http://www.w3.org/2002/07/owl#targetIndividual)
targetValue: IRI = IRI(http://www.w3.org/2002/07/owl#targetValue)
unionOf: IRI = IRI(http://www.w3.org/2002/07/owl#unionOf)
withRestrictions: IRI = IRI(http://www.w3.org/2002/07/owl#withRestrictions)
AllDifferent: IRI = IRI(http://www.w3.org/2002/07/owl#AllDifferent)
AllDisjointClasses: IRI = IRI(http://www.w3.org/2002/07/owl#AllDisjointClasses)
AllDisjointProperties: IRI = IRI(http://www.w3.org/2002/07/owl#AllDisjointProperties)
Annotation: IRI = IRI(http://www.w3.org/2002/07/owl#Annotation)
AnnotationProperty: IRI = IRI(http://www.w3.org/2002/07/owl#AnnotationProperty)
Axiom: IRI = IRI(http://www.w3.org/2002/07/owl#Axiom)
Class: IRI = IRI(http://www.w3.org/2002/07/owl#Class)
DataRange: IRI = IRI(http://www.w3.org/2002/07/owl#DataRange)
DatatypeProperty: IRI = IRI(http://www.w3.org/2002/07/owl#DatatypeProperty)
DeprecatedClass: IRI = IRI(http://www.w3.org/2002/07/owl#DeprecatedClass)
DeprecatedProperty: IRI = IRI(http://www.w3.org/2002/07/owl#DeprecatedProperty)
FunctionalProperty: IRI = IRI(http://www.w3.org/2002/07/owl#FunctionalProperty)
InverseFunctionalProperty: IRI = IRI(http://www.w3.org/2002/07/owl#InverseFunctionalProperty)
IrreflexiveProperty: IRI = IRI(http://www.w3.org/2002/07/owl#IrreflexiveProperty)
NamedIndividual: IRI = IRI(http://www.w3.org/2002/07/owl#NamedIndividual)
NegativePropertyAssertion: IRI = IRI(http://www.w3.org/2002/07/owl#NegativePropertyAssertion)
ObjectProperty: IRI = IRI(http://www.w3.org/2002/07/owl#ObjectProperty)
Ontology: IRI = IRI(http://www.w3.org/2002/07/owl#Ontology)
OntologyProperty: IRI = IRI(http://www.w3.org/2002/07/owl#OntologyProperty)
ReflexiveProperty: IRI = IRI(http://www.w3.org/2002/07/owl#ReflexiveProperty)
Restriction: IRI = IRI(http://www.w3.org/2002/07/owl#Restriction)
SymmetricProperty: IRI = IRI(http://www.w3.org/2002/07/owl#SymmetricProperty)
TransitiveProperty: IRI = IRI(http://www.w3.org/2002/07/owl#TransitiveProperty)
backwardCompatibleWith: IRI = IRI(http://www.w3.org/2002/07/owl#backwardCompatibleWith)
deprecated: IRI = IRI(http://www.w3.org/2002/07/owl#deprecated)
incompatibleWith: IRI = IRI(http://www.w3.org/2002/07/owl#incompatibleWith)
priorVersion: IRI = IRI(http://www.w3.org/2002/07/owl#priorVersion)
versionInfo: IRI = IRI(http://www.w3.org/2002/07/owl#versionInfo)
Nothing: IRI = IRI(http://www.w3.org/2002/07/owl#Nothing)
Thing: IRI = IRI(http://www.w3.org/2002/07/owl#Thing)
bottomDataProperty: IRI = IRI(http://www.w3.org/2002/07/owl#bottomDataProperty)
topDataProperty: IRI = IRI(http://www.w3.org/2002/07/owl#topDataProperty)
bottomObjectProperty: IRI = IRI(http://www.w3.org/2002/07/owl#bottomObjectProperty)
topObjectProperty: IRI = IRI(http://www.w3.org/2002/07/owl#topObjectProperty)
imports: IRI = IRI(http://www.w3.org/2002/07/owl#imports)
versionIRI: IRI = IRI(http://www.w3.org/2002/07/owl#versionIRI)
rational: IRI = IRI(http://www.w3.org/2002/07/owl#rational)
real: IRI = IRI(http://www.w3.org/2002/07/owl#real)
class IRI:
IRI(iri: str)

Create a new IRI

Parameters
  • iri: IRI (without < and >).
iri: str

An IRI.

class Literal:

An RDF literal.

Literal(value: str, data_type: IRI = None, language: str = None)

Create a new RDF Literal

Parameters
  • value: The lexical representation of the value.
  • data_type: The data type of the value (an IRI).
  • language: The language tag of the value.
def to_native(self) -> Union[int, float, bool, str, datetime.datetime, datetime.date]:
Returns
value: str
datatype: Optional[IRI]
language: Optional[str]
class Prefix:

A prefix that can be used to ergonomically build iris.

Prefix(iri, prefix_name=None)

Create a new prefix.

Parameters
  • iri: The prefix IRI.
  • prefix_name: The name of the prefix
def suf(self, suffix: str) -> IRI:

Create an IRI by appending the suffix.

Parameters
  • suffix: The suffix to append.
Returns
class BlankNode:

A Blank Node.

BlankNode(name: str)

Create a new Blank Node

Parameters
  • name: Name of blank node (without _: ).
name: str
def explore( m: Model, host: str = 'localhost', port: int = 8000, bind: str = 'localhost', popup=True, fts=True):
64def explore(*args, **kwargs):
65    """Deprecated way to start an explore session.
66Use the explore method on a Model object instead
67"""
68    logger.warn("Calling `maplib.explore` is deprecated, use `m.explore()` on a `Model` object instead")
69    if kwargs.get("popup") == None or kwargs.get("popup") == True:
70        logger.warn("""Calling explore without a popup argument defaults to it being on.
71The popup argument is deprecated, so if you are relying on explore() opening a browser window
72please change this to something like
73
74```
75import webbrowser
76from maplib import Model
77
78m = Model()
79...
80s = m.explore()
81webbrowser.open(s.url, new=2)
82```
83""")
84        kwargs["popup"] = True
85    elif kwargs.get("popup") == False:
86        logger.warn("The new explore function on a Model, no longer defaults to popping up the browser ")
87
88
89    return _explore(*args, **kwargs)

Starts a graph explorer session. To run from Jupyter Notebook use:

>>> from maplib import explore
>>>
>>> server = explore(m)
You can later stop the server with
>>> server.stop()
Parameters
  • m: The Model to explore
  • host: The hostname that we will point the browser to.
  • port: The port where the graph explorer webserver listens on.
  • bind: Bind to the following host / ip.
  • popup: Pop up the browser window.
  • fts: Enable full text search indexing
def add_triples( source: Model, target: Model, source_graph: str = None, target_graph: str = None):
 5def add_triples(
 6    source: Model, target: Model, source_graph: str = None, target_graph: str = None
 7):
 8    """(Zero) copy the triples from one Model into another.
 9
10    :param source: The source model
11    :param target: The target model
12    :param source_graph: The named graph in the source model to copy from. None means default graph.
13    :param target_graph: The named graph in the target model to copy into. None means default graph.
14    """
15    for p in source.get_predicate_iris(source_graph):
16        subject = Variable("subject")
17        object = Variable("object")
18        template = Template(
19            iri=IRI("urn:maplib:tmp"),
20            parameters=[subject, object],
21            instances=[Triple(subject, p, object)],
22        )
23        sms = source.get_predicate(p, source_graph)
24        for sm in sms:
25            target.map(
26                template,
27                sm.mappings,
28                types=sm.rdf_types,
29                graph=target_graph,
30            )

(Zero) copy the triples from one Model into another.

Parameters
  • source: The source model
  • target: The target model
  • source_graph: The named graph in the source model to copy from. None means default graph.
  • target_graph: The named graph in the target model to copy into. None means default graph.
def generate_templates(m: Model, graph: Optional[str]) -> Dict[str, Template]:
 9def generate_templates(m: Model, graph: Optional[str]) -> Dict[str, Template]:
10    """Generate templates for instantiating the classes in an ontology
11
12    :param m: The model where the ontology is stored. We mainly rely on rdfs:subClassOf, rdfs:range and rdfs:domain.
13    :param graph: The named graph where the ontology is stored.
14
15    :return A dictionary of templates for instantiating the classes in the ontology, where the keys are the class URIs.
16
17    Usage example - note that it is important to add the templates to the Model you want to populate.
18    >>> from maplib import Model, create_templates
19    >>>
20    >>> m_ont = Model()
21    >>> m_ont.read("my_ontology.ttl")
22    >>> templates = generate_templates(m_ont)
23    >>> m = Model()
24    >>> for t in templates.values():
25    >>>     m.add_template(t)
26    >>> m.map("https://example.net/MyClass", df)
27    """
28
29    properties = get_properties(m, graph=graph)
30    properties_by_domain = {}
31    properties_by_range = {}
32    for r in properties.iter_rows(named=True):
33        dom = r["domain"]
34        if dom in properties_by_domain:
35            properties_by_domain[dom].append(r)
36        else:
37            properties_by_domain[dom] = [r]
38
39        ran = r["range"]
40        if ran in properties_by_range:
41            properties_by_range[ran].append(r)
42        else:
43            properties_by_range[ran] = [r]
44
45    subclasses = get_subclasses(m, graph=graph)
46
47    subclass_of = {}
48    for r in (
49        subclasses.group_by("child")
50        .agg(pl.col("parent").alias("parents"))
51        .iter_rows(named=True)
52    ):
53        subclass_of[r["child"]] = r["parents"]
54
55    class_ordering = topological_sort(subclasses)
56
57    templates_without_typing = generate_templates_without_typing(
58        properties_by_domain, properties_by_range, class_ordering, subclass_of
59    )
60    templates_with_typing = generate_templates_with_typing(templates_without_typing)
61    templates = {}
62    for t, template in templates_without_typing.items():
63
64        templates[t + "_notype"] = template
65    for t, template in templates_with_typing.items():
66        templates[t] = template
67
68    return templates

Generate templates for instantiating the classes in an ontology

Parameters
  • m: The model where the ontology is stored. We mainly rely on rdfs:subClassOf, rdfs:range and rdfs: domain.
  • graph: The named graph where the ontology is stored.

:return A dictionary of templates for instantiating the classes in the ontology, where the keys are the class URIs.

Usage example - note that it is important to add the templates to the Model you want to populate.

class MaplibException(builtins.Exception):

Common base class for all non-exit exceptions.