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:
def add_template(self, /, template):
def add_prefixes(self, /, prefixes):
def detach_graph(self, /, graph=None, preserve_name=None):
def map( self, /, template, df=None, graph=None, types=None, validate_iris=None):
def map_triples( self, /, df, predicate=None, graph=None, types=None, validate_iris=None):
def map_default( self, /, df, primary_key_column, dry_run=None, graph=None, types=None, validate_iris=None):
def explore(unknown):

Starts a graph explorer session.

To run from Jupyter Notebook use:

from maplib import Model
m = Model()
...
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.
  • popup: Pop up the browser window.
  • fts: Enable full text search indexing
  • fts_path: Path to the fts index.
def query( self, /, query, parameters=None, include_datatypes=None, native_dataframe=None, graph=None, streaming=None, return_json=None, include_transient=None, max_rows=None, debug=None):
def update( self, /, update, parameters=None, graph=None, streaming=None, include_transient=None, max_rows=None, debug=None):
def create_index(self, /, options=None, all=None, graph=None):
def validate( self, /, shape_graph, data_graph=None, include_details=None, include_conforms=None, include_shape_graph=None, streaming=None, max_shape_constraint_results=None, only_shapes=None, deactivate_shapes=None, dry_run=None, max_rows=None, serial=None):
def insert( self, /, query, parameters=None, include_datatypes=None, native_dataframe=None, transient=None, streaming=None, source_graph=None, target_graph=None, include_transient=None, max_rows=None, debug=None):
def read( self, /, file_path, format=None, base_iri=None, transient=None, parallel=None, checked=None, graph=None, replace_graph=None):
def read_template(self, /, file_path):
def reads( self, /, s, format, base_iri=None, transient=None, parallel=None, checked=None, graph=None, replace_graph=None):
def write(self, /, file_path, format=None, graph=None, prefixes=None):
def write_cim_xml( self, /, file_path, profile_graph, model_iri=None, version=None, description=None, created=None, scenario_time=None, modeling_authority_set=None, prefixes=None, graph=None):
def writes(self, /, format=None, graph=None, prefixes=None):
def write_native_parquet(self, /, folder_path, graph=None):
def get_predicate_iris(self, /, graph=None, include_transient=None):
def get_predicate(self, /, iri, graph=None, include_transient=None):
def infer( self, /, rulesets, graph=None, include_datatypes=None, native_dataframe=None, max_iterations=100000, max_results=10000000, include_transient=None, max_rows=100000000, debug=None):
a = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#type)
def Triple(subject, predicate, object, list_expander=None):
class SolutionMappings:
pushdown_paths
rdf_types
mappings
debug
class IndexingOptions:
class ValidationReport:
def results( self, /, native_dataframe=None, include_datatypes=None, streaming=None):
def details( self, /, native_dataframe=None, include_datatypes=None, streaming=None):
def graph(self, /):
conforms
shape_targets
performance
class Instance:
iri
class Template:
def instance(self, /, arguments, list_expander=None):
instances
iri
parameters
class Argument:
variable
class Parameter:
rdf_type
optional
allow_blank
variable
default_value
class Variable:
name
class RDFType:
def Literal(iri):
def Nested(rdf_type):
def Multi(rdf_types):
BlankNode = RDFType.BlankNode()
Unknown = RDFType.None()
class xsd:
boolean = IRI(http://www.w3.org/2001/XMLSchema#boolean)
byte = IRI(http://www.w3.org/2001/XMLSchema#byte)
date = IRI(http://www.w3.org/2001/XMLSchema#date)
dateTime = IRI(http://www.w3.org/2001/XMLSchema#dateTime)
dateTimeStamp = IRI(http://www.w3.org/2001/XMLSchema#dateTimeStamp)
decimal = IRI(http://www.w3.org/2001/XMLSchema#decimal)
double = IRI(http://www.w3.org/2001/XMLSchema#double)
duration = IRI(http://www.w3.org/2001/XMLSchema#duration)
float = IRI(http://www.w3.org/2001/XMLSchema#float)
int_ = IRI(http://www.w3.org/2001/XMLSchema#int)
integer = IRI(http://www.w3.org/2001/XMLSchema#integer)
language = IRI(http://www.w3.org/2001/XMLSchema#language)
long = IRI(http://www.w3.org/2001/XMLSchema#long)
short = IRI(http://www.w3.org/2001/XMLSchema#short)
string = IRI(http://www.w3.org/2001/XMLSchema#string)
anyURI = IRI(http://www.w3.org/2001/XMLSchema#anyURI)
dayTimeDuration = IRI(http://www.w3.org/2001/XMLSchema#dayTimeDuration)
base64Binary = IRI(http://www.w3.org/2001/XMLSchema#base64Binary)
gDay = IRI(http://www.w3.org/2001/XMLSchema#gDay)
gMonthDay = IRI(http://www.w3.org/2001/XMLSchema#gMonthDay)
gMonth = IRI(http://www.w3.org/2001/XMLSchema#gMonth)
gYear = IRI(http://www.w3.org/2001/XMLSchema#gYear)
gYearMonth = IRI(http://www.w3.org/2001/XMLSchema#gYearMonth)
hexBinary = 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(http://www.w3.org/2001/XMLSchema#negativeInteger)
nonNegativeInteger = IRI(http://www.w3.org/2001/XMLSchema#nonNegativeInteger)
nonPositiveInteger = IRI(http://www.w3.org/2001/XMLSchema#nonPositiveInteger)
normalizedString = IRI(http://www.w3.org/2001/XMLSchema#normalizedString)
token = IRI(http://www.w3.org/2001/XMLSchema#token)
unsignedByte = IRI(http://www.w3.org/2001/XMLSchema#unsignedByte)
unsignedInt = IRI(http://www.w3.org/2001/XMLSchema#unsignedInt)
unsignedLong = IRI(http://www.w3.org/2001/XMLSchema#unsignedLong)
unsignedShort = IRI(http://www.w3.org/2001/XMLSchema#unsignedShort)
yearMonthDuration = IRI(http://www.w3.org/2001/XMLSchema#yearMonthDuration)
class rdf:
type = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#type)
Alt = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#Alt)
Bag = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag)
first = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#first)
HTML = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#HTML)
langString = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#langString)
List = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#List)
nil = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#nil)
object = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#object)
predicate = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate)
Property = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#Property)
rest = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#rest)
Seq = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#Seq)
Statement = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement)
subject = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#subject)
value = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#value)
XMLLiteral = IRI(http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral)
class rdfs:
Class = IRI(http://www.w3.org/2000/01/rdf-schema#Class)
comment = IRI(http://www.w3.org/2000/01/rdf-schema#comment)
Container = IRI(http://www.w3.org/2000/01/rdf-schema#Container)
Datatype = IRI(http://www.w3.org/2000/01/rdf-schema#Datatype)
domain = IRI(http://www.w3.org/2000/01/rdf-schema#domain)
ContainerMembershipProperty = IRI(http://www.w3.org/2000/01/rdf-schema#ContainerMembershipProperty)
isDefinedBy = IRI(http://www.w3.org/2000/01/rdf-schema#isDefinedBy)
label = IRI(http://www.w3.org/2000/01/rdf-schema#label)
Literal = IRI(http://www.w3.org/2000/01/rdf-schema#Literal)
member = IRI(http://www.w3.org/2000/01/rdf-schema#member)
range = IRI(http://www.w3.org/2000/01/rdf-schema#range)
seeAlso = IRI(http://www.w3.org/2000/01/rdf-schema#seeAlso)
subClassOf = IRI(http://www.w3.org/2000/01/rdf-schema#subClassOf)
subPropertyOf = IRI(http://www.w3.org/2000/01/rdf-schema#subPropertyOf)
Resource = IRI(http://www.w3.org/2000/01/rdf-schema#Resource)
class owl:
allValuesFrom = IRI(http://www.w3.org/2002/07/owl#allValuesFrom)
annotatedProperty = IRI(http://www.w3.org/2002/07/owl#annotatedProperty)
annotatedSource = IRI(http://www.w3.org/2002/07/owl#annotatedSource)
annotatedTarget = IRI(http://www.w3.org/2002/07/owl#annotatedTarget)
assertionProperty = IRI(http://www.w3.org/2002/07/owl#assertionProperty)
cardinality = IRI(http://www.w3.org/2002/07/owl#cardinality)
complementOf = IRI(http://www.w3.org/2002/07/owl#complementOf)
datatypeComplementOf = IRI(http://www.w3.org/2002/07/owl#datatypeComplementOf)
differentFrom = IRI(http://www.w3.org/2002/07/owl#differentFrom)
disjointUnionOf = IRI(http://www.w3.org/2002/07/owl#disjointUnionOf)
disjointWith = IRI(http://www.w3.org/2002/07/owl#disjointWith)
distinctMembers = IRI(http://www.w3.org/2002/07/owl#distinctMembers)
equivalentClass = IRI(http://www.w3.org/2002/07/owl#equivalentClass)
equivalentProperty = IRI(http://www.w3.org/2002/07/owl#equivalentProperty)
hasKey = IRI(http://www.w3.org/2002/07/owl#hasKey)
hasSelf = IRI(http://www.w3.org/2002/07/owl#hasSelf)
hasValue = IRI(http://www.w3.org/2002/07/owl#hasValue)
intersectionOf = IRI(http://www.w3.org/2002/07/owl#intersectionOf)
inverseOf = IRI(http://www.w3.org/2002/07/owl#inverseOf)
maxCardinality = IRI(http://www.w3.org/2002/07/owl#maxCardinality)
maxQualifiedCardinality = IRI(http://www.w3.org/2002/07/owl#maxQualifiedCardinality)
members = IRI(http://www.w3.org/2002/07/owl#members)
minCardinality = IRI(http://www.w3.org/2002/07/owl#minCardinality)
minQualifiedCardinality = IRI(http://www.w3.org/2002/07/owl#minQualifiedCardinality)
onClass = IRI(http://www.w3.org/2002/07/owl#onClass)
onDataRange = IRI(http://www.w3.org/2002/07/owl#onDataRange)
onDatatype = IRI(http://www.w3.org/2002/07/owl#onDatatype)
onProperties = IRI(http://www.w3.org/2002/07/owl#onProperties)
onProperty = IRI(http://www.w3.org/2002/07/owl#onProperty)
oneOf = IRI(http://www.w3.org/2002/07/owl#oneOf)
propertyChainAxiom = IRI(http://www.w3.org/2002/07/owl#propertyChainAxiom)
propertyDisjointWith = IRI(http://www.w3.org/2002/07/owl#propertyDisjointWith)
qualifiedCardinality = IRI(http://www.w3.org/2002/07/owl#qualifiedCardinality)
sameAs = IRI(http://www.w3.org/2002/07/owl#sameAs)
someValuesFrom = IRI(http://www.w3.org/2002/07/owl#someValuesFrom)
sourceIndividual = IRI(http://www.w3.org/2002/07/owl#sourceIndividual)
targetIndividual = IRI(http://www.w3.org/2002/07/owl#targetIndividual)
targetValue = IRI(http://www.w3.org/2002/07/owl#targetValue)
unionOf = IRI(http://www.w3.org/2002/07/owl#unionOf)
withRestrictions = IRI(http://www.w3.org/2002/07/owl#withRestrictions)
AllDifferent = IRI(http://www.w3.org/2002/07/owl#AllDifferent)
AllDisjointClasses = IRI(http://www.w3.org/2002/07/owl#AllDisjointClasses)
AllDisjointProperties = IRI(http://www.w3.org/2002/07/owl#AllDisjointProperties)
Annotation = IRI(http://www.w3.org/2002/07/owl#Annotation)
AnnotationProperty = IRI(http://www.w3.org/2002/07/owl#AnnotationProperty)
Axiom = IRI(http://www.w3.org/2002/07/owl#Axiom)
Class = IRI(http://www.w3.org/2002/07/owl#Class)
DataRange = IRI(http://www.w3.org/2002/07/owl#DataRange)
DatatypeProperty = IRI(http://www.w3.org/2002/07/owl#DatatypeProperty)
DeprecatedClass = IRI(http://www.w3.org/2002/07/owl#DeprecatedClass)
DeprecatedProperty = IRI(http://www.w3.org/2002/07/owl#DeprecatedProperty)
FunctionalProperty = IRI(http://www.w3.org/2002/07/owl#FunctionalProperty)
InverseFunctionalProperty = IRI(http://www.w3.org/2002/07/owl#InverseFunctionalProperty)
IrreflexiveProperty = IRI(http://www.w3.org/2002/07/owl#IrreflexiveProperty)
NamedIndividual = IRI(http://www.w3.org/2002/07/owl#NamedIndividual)
NegativePropertyAssertion = IRI(http://www.w3.org/2002/07/owl#NegativePropertyAssertion)
ObjectProperty = IRI(http://www.w3.org/2002/07/owl#ObjectProperty)
Ontology = IRI(http://www.w3.org/2002/07/owl#Ontology)
OntologyProperty = IRI(http://www.w3.org/2002/07/owl#OntologyProperty)
ReflexiveProperty = IRI(http://www.w3.org/2002/07/owl#ReflexiveProperty)
Restriction = IRI(http://www.w3.org/2002/07/owl#Restriction)
SymmetricProperty = IRI(http://www.w3.org/2002/07/owl#SymmetricProperty)
TransitiveProperty = IRI(http://www.w3.org/2002/07/owl#TransitiveProperty)
backwardCompatibleWith = IRI(http://www.w3.org/2002/07/owl#backwardCompatibleWith)
deprecated = IRI(http://www.w3.org/2002/07/owl#deprecated)
incompatibleWith = IRI(http://www.w3.org/2002/07/owl#incompatibleWith)
priorVersion = IRI(http://www.w3.org/2002/07/owl#priorVersion)
versionInfo = IRI(http://www.w3.org/2002/07/owl#versionInfo)
Nothing = IRI(http://www.w3.org/2002/07/owl#Nothing)
Thing = IRI(http://www.w3.org/2002/07/owl#Thing)
bottomDataProperty = IRI(http://www.w3.org/2002/07/owl#bottomDataProperty)
topDataProperty = IRI(http://www.w3.org/2002/07/owl#topDataProperty)
bottomObjectProperty = IRI(http://www.w3.org/2002/07/owl#bottomObjectProperty)
topObjectProperty = IRI(http://www.w3.org/2002/07/owl#topObjectProperty)
imports = IRI(http://www.w3.org/2002/07/owl#imports)
versionIRI = IRI(http://www.w3.org/2002/07/owl#versionIRI)
rational = IRI(http://www.w3.org/2002/07/owl#rational)
real = IRI(http://www.w3.org/2002/07/owl#real)
class IRI:
iri
class Literal:
def to_native(self, /):
value
language
datatype
class Prefix:
def suf(self, /, suffix):
class BlankNode:
name
def explore(*args, **kwargs):
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)

Deprecated way to start an explore session. Use the explore method on a Model object instead

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.

>>> from maplib import Model, create_templates
>>>
>>> m_ont = Model()
>>> m_ont.read("my_ontology.ttl")
>>> templates = generate_templates(m_ont)
>>> m = Model()
>>> for t in templates.values():
>>>     m.add_template(t)
>>> m.map("https://example.net/MyClass", df)
class MaplibException(builtins.Exception):

Common base class for all non-exit exceptions.