Ejemplo n.º 1
0
def PrepareSipCollection(adornedRuleset):
    """
    Takes adorned ruleset and returns an RDF dataset
    formed from the sips associated with each adorned
    rule as named graphs.  Also returns a mapping from
    the head predicates of each rule to the rules that match
    it - for efficient retrieval later
    """
    headToRule = {}
    graphs = []
    secondOrderRules = set()
    for rule in adornedRuleset:
        ruleHead = GetOp(rule.formula.head)
        if isinstance(ruleHead,Variable):
            #We store second order rules (i.e., rules whose head is a
            #predicate occurrence whose predicate symbol is a variable) aside
            secondOrderRules.add(rule)
        headToRule.setdefault(ruleHead,set()).add(rule)
        if hasattr(rule,'sip'):
            graphs.append(rule.sip)
    #Second order rules are mapped from a None key (in order to indicate they are wildcards)
    headToRule[None]=secondOrderRules
    if not graphs:
        return
    graph = ReadOnlyGraphAggregate(graphs)
    graph.headToRule = headToRule
    return graph
class GraphAggregates1(unittest.TestCase):
    def setUp(self):
        memStore = plugin.get('IOMemory', Store)()
        self.graph1 = Graph(memStore)
        self.graph2 = Graph(memStore)
        self.graph3 = Graph(memStore)

        for n3Str, graph in [(testGraph1N3, self.graph1),
                             (testGraph2N3, self.graph2),
                             (testGraph3N3, self.graph3)]:
            graph.parse(StringIO(n3Str), format='n3')

        self.G = ReadOnlyGraphAggregate([self.graph1, self.graph2, self.graph3])

    def testAggregateRaw(self):
        # Test triples
        assert len(list(self.G.triples((None, RDF.type, None)))) == 4
        assert len(list(self.G.triples((URIRef("http://test/bar"), None, None)))) == 2
        assert len(list(self.G.triples((None, URIRef("http://test/d"), None)))) == 3

        # Test __len__
        assert len(self.G) == 8

        # assert context iteration
        for g in self.G.contexts():
            assert isinstance(g, Graph)

        # Test __contains__
        assert (URIRef("http://test/foo"), RDF.type, RDFS.Resource) in self.G

        barPredicates = [URIRef("http://test/d"), RDFS.isDefinedBy]
        assert len(list(self.G.triples_choices((URIRef("http://test/bar"), barPredicates, None)))) == 2
Ejemplo n.º 3
0
    def __call__(self, tNode, inferredTriple, token, binding, debug=False):
        """
        Called when a (EDB) query literal is triggered with
        given bindings.
        """
        assert len(tNode.consequent) == 1
        key = (self.queryLiteral, tNode, token)
        if key not in self.bfp.firedEDBQueries:
            self.bfp.firedEDBQueries.add(key)
            for binding in token.bindings:
                _bindings = dict([(k, v) for k, v in list(binding.items()) if v != None])

                closure = ReadOnlyGraphAggregate([self.factGraph, self.bfp.metaInterpNetwork.inferredFacts])
                closure.templateMap = self.factGraph.templateMap
                # For each mapping that unifies with theory
                if self.edbConj:
                    _vars = set()
                    for lit in self.edbConj:
                        _vars.update(list(GetVariables(lit, secondOrder=True)))
                    _qLit = EDBQuery(
                        [copy.deepcopy(lit) for lit in self.edbConj],
                        self.factGraph,  # closure,
                        _vars,
                        specialBNodeHandling=self.bfp.specialBNodeHandling,
                    )
                else:
                    _qLit = copy.deepcopy(self.queryLiteral)
                    _qLit = EDBQuery(
                        [_qLit],
                        self.factGraph,  # closure,
                        list(GetVariables(_qLit, secondOrder=True)),
                        specialBNodeHandling=self.bfp.specialBNodeHandling,
                    )
                origQuery = _qLit.copy()
                _qLit.ground(_bindings)
                if self.bfp.debug:
                    print(
                        "%sQuery triggered for " % (" maximal db conjunction " if self.edbConj else ""),
                        tNode.clauseRepresentation(),
                    )
                self.bfp.edbQueries.add(_qLit)
                # queryVars = origQuery.getOpenVars()

                # tokens2Propagate=[
                #     t for t in token.tokens
                #         if [
                #             v for v in t.getVarBindings()
                #                 if v not in queryVars
                #         ]
                # ]
                isGround = not _qLit.returnVars
                rt = self.tabledQuery(_qLit)
                if isGround:
                    if first(rt):
                        self.handleQueryAnswer(origQuery, token, self.bfp.debug, ({}, binding))
                else:
                    for ans in rt:
                        if self.bfp.debug:
                            pprint(ans)
                        self.handleQueryAnswer(origQuery, token, self.bfp.debug, (ans, binding))
    def setUp(self):
        memStore = plugin.get('IOMemory', Store)()
        self.graph1 = Graph(memStore)
        self.graph2 = Graph(memStore)
        self.graph3 = Graph(memStore)

        for n3Str, graph in [(testGraph1N3, self.graph1),
                             (testGraph2N3, self.graph2),
                             (testGraph3N3, self.graph3)]:
            graph.parse(StringIO(n3Str), format='n3')

        self.G = ReadOnlyGraphAggregate([self.graph1, self.graph2, self.graph3])
Ejemplo n.º 5
0
    def __init__(self, graphs, labels_identifier=None):
        super().__init__(labels_identifier=labels_identifier)

        self.labels_graph = Graph(identifier=labels_identifier)
        self.labels_identifier = str(self.labels_graph.identifier)

        graphs = graphs + [self.labels_graph]

        self.graph = ReadOnlyGraphAggregate(graphs)
        # self.identifiers=[g.identifier for g in graphs]
        # self.identifiers = [self.graph.identifier]
        # print(self.get_identifiers())
        self.type = 'memory'
Ejemplo n.º 6
0
    def setUp(self):
        memStore = plugin.get('SQLAlchemy', Store)(identifier="rdflib_test",
                                                   configuration=self.dburi)
        self.graph1 = Graph(memStore)
        self.graph2 = Graph(memStore)
        self.graph3 = Graph(memStore)

        for n3Str, graph in [(testGraph1N3, self.graph1),
                             (testGraph2N3, self.graph2),
                             (testGraph3N3, self.graph3)]:
            graph.parse(StringIO(n3Str), format='n3')

        self.G = ReadOnlyGraphAggregate(
            [self.graph1, self.graph2, self.graph3])
Ejemplo n.º 7
0
    def _validate_entity(self, entity_name, entity_doc):
        """Validate the yaml definition of an entity.

        Will check for the special keywords of the different entity types.

        Args:
            entity_name (str): The name of the entity
            entity_doc (dict): The YAML doc describing the entity.

        Raises:
            RuntimeError: Could not deterimine type of entity.

        Returns:
            str: Type of entity
        """
        iri = self._get_iri(entity_name)
        class_triple = (iri, RDF.type, OWL.Class)
        rel_triple = (iri, RDF.type, OWL.ObjectProperty)
        attr_triple = (iri, RDF.type, OWL.DatatypeProperty)
        status = (
            class_triple
            in ReadOnlyGraphAggregate(
                [self._graph, namespace_registry._graph]
            ),
            rel_triple
            in ReadOnlyGraphAggregate(
                [self._graph, namespace_registry._graph]
            ),
            attr_triple
            in ReadOnlyGraphAggregate(
                [self._graph, namespace_registry._graph]
            ),
        )
        if sum(status) != 1:
            raise RuntimeError(f"Couldn't determine type of {entity_name}")
        if status[0]:
            t = "class"
        elif status[1]:
            t = "relationship"
        else:
            t = "attribute"

        validate(
            entity_doc,
            t + "_def",
            context="<%s>/ONTOLOGY/%s"
            % (os.path.basename(self._file_path), entity_name),
        )
        return t
Ejemplo n.º 8
0
 def closureGraph(self, sourceGraph, readOnly=True, store=None):
     if readOnly:
         if store is None and not sourceGraph:
             store = Graph().store
         store = store is None and sourceGraph.store or store
         roGraph = ReadOnlyGraphAggregate([sourceGraph, self.inferredFacts], store=store)
         roGraph.namespace_manager = NamespaceManager(roGraph)
         for srcGraph in [sourceGraph, self.inferredFacts]:
             for prefix, uri in srcGraph.namespaces():
                 roGraph.namespace_manager.bind(prefix, uri)
         return roGraph
     else:
         cg = ConjunctiveGraph()
         cg += sourceGraph
         cg += self.inferredFacts
         return cg
Ejemplo n.º 9
0
    def _add_superclass(self, entity_name, iri, superclass_doc):
        """Add superclass triples to the graph.

        Args:
            entity_name (str): The name of the entity that has superclasses.
            iri (URIRef): The iri of the entity
            superclass_doc (dict): The YAML document describing the
                superclasses.
        """
        if isinstance(superclass_doc, str):
            namespace, superclass_name = self.split_name(superclass_doc)
            superclass_iri = self._get_iri(
                superclass_name, namespace, entity_name
            )
            predicate = RDFS.subPropertyOf
            graph = ReadOnlyGraphAggregate(
                [self._graph, namespace_registry._graph]
            )
            if (iri, RDF.type, OWL.Class) in graph or (
                iri,
                RDF.type,
                RDFS.Class,
            ) in graph:
                predicate = RDFS.subClassOf

            self._graph.add((iri, predicate, superclass_iri))
Ejemplo n.º 10
0
 def closureGraph(self, sourceGraph, readOnly=True, store=None):
     if readOnly:
         if store is None and not sourceGraph:
             store = Graph().store
         store = store is None and sourceGraph.store or store
         roGraph = ReadOnlyGraphAggregate([sourceGraph, self.inferredFacts],
                                          store=store)
         roGraph.namespace_manager = NamespaceManager(roGraph)
         for srcGraph in [sourceGraph, self.inferredFacts]:
             for prefix, uri in srcGraph.namespaces():
                 roGraph.namespace_manager.bind(prefix, uri)
         return roGraph
     else:
         cg = ConjunctiveGraph()
         cg += sourceGraph
         cg += self.inferredFacts
         return cg
Ejemplo n.º 11
0
def to_canonical_graph(g1):
    """
    Creates a canonical, read-only graph where all bnode id:s are based on
    deterministical MD5 checksums, correlated with the graph contents.
    """
    graph = Graph()
    graph += _TripleCanonicalizer(g1, _md5_hash).canonical_triples()
    return ReadOnlyGraphAggregate([graph])
Ejemplo n.º 12
0
def to_canonical_graph(g1, stats=None):
    """Creates a canonical, read-only graph.

    Creates a canonical, read-only graph where all bnode id:s are based on
    deterministical SHA-256 checksums, correlated with the graph contents.
    """
    graph = Graph()
    graph += _TripleCanonicalizer(g1).canonical_triples(stats=stats)
    return ReadOnlyGraphAggregate([graph])
    def setUp(self):
        memStore = plugin.get("SQLAlchemy", Store)(identifier="rdflib_test", configuration=self.dburi)
        self.graph1 = Graph(memStore)
        self.graph2 = Graph(memStore)
        self.graph3 = Graph(memStore)

        for n3Str, graph in [(testGraph1N3, self.graph1), (testGraph2N3, self.graph2), (testGraph3N3, self.graph3)]:
            graph.parse(StringIO(n3Str), format="n3")

        self.G = ReadOnlyGraphAggregate([self.graph1, self.graph2, self.graph3])
Ejemplo n.º 14
0
 def setUp(self):
     self.store = plugin.get('IOMemory', Store)()
     self.graph1 = Graph(
         self.store, identifier=URIRef('http://example.org/foaf/aliceFoaf'))
     self.graph1.parse(data=test_graph_a, format="n3")
     self.graph2 = Graph(
         self.store, identifier=URIRef('http://example.org/foaf/bobFoaf'))
     self.graph2.parse(data=test_graph_b, format="n3")
     self.unionGraph = ReadOnlyGraphAggregate(
         graphs=[self.graph1, self.graph2], store=self.store)
class GraphAggregates1(unittest.TestCase):
    dburi = Literal("sqlite://")

    def setUp(self):
        memStore = plugin.get("SQLAlchemy", Store)(identifier="rdflib_test", configuration=self.dburi)
        self.graph1 = Graph(memStore)
        self.graph2 = Graph(memStore)
        self.graph3 = Graph(memStore)

        for n3Str, graph in [(testGraph1N3, self.graph1), (testGraph2N3, self.graph2), (testGraph3N3, self.graph3)]:
            graph.parse(StringIO(n3Str), format="n3")

        self.G = ReadOnlyGraphAggregate([self.graph1, self.graph2, self.graph3])

    def testAggregateRaw(self):
        # Test triples
        assert len(list(self.G.triples((None, RDF.type, None)))) == 4
        assert len(list(self.G.triples((URIRef("http://test/bar"), None, None)))) == 2
        assert len(list(self.G.triples((None, URIRef("http://test/d"), None)))) == 3

        # Test __len__
        # assert len(self.G) == 8, self.G.serialize(format="nt")
        assert len(list(self.G.triples((None, None, None)))) == 8

        # assert context iteration
        for g in self.G.contexts():
            assert isinstance(g, Graph)

        # Test __contains__
        assert (URIRef("http://test/foo"), RDF.type, RDFS.Resource) in self.G

        barPredicates = [URIRef("http://test/d"), RDFS.isDefinedBy]
        assert len(list(self.G.triples_choices((URIRef("http://test/bar"), barPredicates, None)))) == 2
Ejemplo n.º 16
0
def PrepareSipCollection(adornedRuleset):
    """
    Takes adorned ruleset and returns an RDF dataset
    formed from the sips associated with each adorned
    rule as named graphs.  Also returns a mapping from
    the head predicates of each rule to the rules that match
    it - for efficient retrieval later
    """
    headToRule = {}
    graphs = []
    secondOrderRules = set()

    for rule in adornedRuleset:
        ruleHead = GetOp(rule.formula.head)

        if isinstance(ruleHead, Variable):
            #We store second order rules (i.e., rules whose head is a
            #predicate occurrence whose predicate symbol is a variable) aside
            secondOrderRules.add(rule)

        headToRule.setdefault(ruleHead, set()).add(rule)

        if hasattr(rule, 'sip'):
            graphs.append(rule.sip)

    # Second order rules are mapped from a None key (in order
    # to indicate they are wildcards)

    headToRule[None] = secondOrderRules

    if not graphs:
        return

    graph = ReadOnlyGraphAggregate(graphs)
    graph.headToRule = headToRule

    return graph
Ejemplo n.º 17
0
class RdflibKGQueryInterfaceExtended(KGQueryInterfaceExtended):
    def __init__(self, graphs, labels_identifier=None):
        super().__init__(labels_identifier=labels_identifier)

        self.labels_graph = Graph(identifier=labels_identifier)
        self.labels_identifier = str(self.labels_graph.identifier)

        graphs = graphs + [self.labels_graph]

        self.graph = ReadOnlyGraphAggregate(graphs)
        # self.identifiers=[g.identifier for g in graphs]
        # self.identifiers = [self.graph.identifier]
        # print(self.get_identifiers())
        self.type = 'memory'

    def execute(self, query):
        res = self.graph.query(query)
        return list(res)
Ejemplo n.º 18
0
    def _add_type_triple(self, entity_name, iri):
        """Add a triple describing the type of an entity.

        Args:
            entity_name (str): The name of the entity
            iri (URIRef): The IRI of the entity

        Raises:
            ValueError: Could not determine the type of the given entity.
        """
        queue = [(self._namespace, entity_name)]
        types = set()
        while queue:
            namespace, name = queue.pop()

            # same type as parent
            superclass_iri = self._get_iri(name, namespace, entity_name)
            triple = (superclass_iri, RDF.type, None)
            for _, _, o in ReadOnlyGraphAggregate(
                [self._graph, namespace_registry._graph]
            ).triples(triple):
                if o in {
                    OWL.Class,
                    RDFS.Class,
                    OWL.ObjectProperty,
                    OWL.DatatypeProperty,
                    OWL.FunctionalProperty,
                }:
                    types.add(o)

            if namespace == self._namespace:
                queue += [
                    self.split_name(x)
                    for x in self._ontology_doc[name][
                        keywords.SUPERCLASSES_KEY
                    ]
                    if isinstance(x, str)
                ]
        if not types:
            raise ValueError(f"Could not determine type of {entity_name}")
        for t in types:
            self._graph.add((iri, RDF.type, t))
Ejemplo n.º 19
0
    def _add_attributes(self, entity_name, entity_doc):
        """Add a attribute to an ontology class.

        Args:
            entity_name (str): The name of the entity to add attributes to.
            entity_doc (dict): The YAML document describing the entity.

        Raises:
            ValueError: Invalid attribute specified.
        """
        iri = self._get_iri(entity_name)
        attributes_def = None
        if keywords.ATTRIBUTES_KEY in entity_doc:
            attributes_def = entity_doc[keywords.ATTRIBUTES_KEY]

        if attributes_def is None:
            return

        # Add the attributes one by one
        for attribute_name, default in attributes_def.items():
            attribute_namespace, attribute_name = self.split_name(
                attribute_name
            )

            attribute_iri = self._get_iri(
                attribute_name, attribute_namespace, entity_name
            )
            x = (attribute_iri, RDF.type, OWL.DatatypeProperty)
            if x not in ReadOnlyGraphAggregate(
                [self._graph, namespace_registry._graph]
            ):
                raise ValueError(
                    f"Invalid attribute {attribute_namespace}."
                    f"{attribute_name} of entity {entity_name}"
                )
            self._add_attribute(iri, attribute_iri, default)
Ejemplo n.º 20
0
    def _get_iri(
        self,
        entity_name=None,
        namespace=None,
        current_entity=None,
        _case_sensitive=False,
        _for_default_rel=False,
    ):
        """Get the iri of the given entity and namespace.

        Args:
            entity_name (str, optional): The entity name. Defaults to None.
            namespace (str, optional): The namespace. Defaults to None.
            current_entity (str, optional): Name of the entity that is
                currently parsed (For printing meaningful warnings).
                Defaults to None.
            _case_sensitive (bool, optional): Whether entities are case
                sensitive. Defaults to False.

        Raises:
            AttributeError: Reference to undefined entity

        Returns:
            URIRef: The iri of the given entity
        """
        namespace = namespace or self._namespace
        namespace = namespace.lower()
        entity_name = entity_name or ""
        try:  # Namespace already in graph?
            ns_iri = next(
                iri
                for name, iri in self._graph.namespaces()
                if name == namespace
            )
        except StopIteration:
            ns_iri = URIRef(f"http://www.osp-core.com/{namespace}#")

        if not entity_name:
            return ns_iri

        iri = ns_iri + entity_name
        # in case of reference by label (EMMO)
        if self.reference_style:
            literal = Literal(entity_name, lang="en")
            try:
                iri = next(
                    s
                    for s, p, o in self._graph.triples(
                        (None, SKOS.prefLabel, literal)
                    )
                    if s.startswith(ns_iri)
                )
            except StopIteration:
                pass

        # check if constructed iri exists
        if (
            namespace == self._namespace
            and entity_name not in self._ontology_doc
        ) or (
            namespace != self._namespace
            and (iri, None, None)
            not in ReadOnlyGraphAggregate(
                [self._graph, namespace_registry._graph]
            )
        ):
            if _case_sensitive and not _for_default_rel:
                raise AttributeError(
                    f"Reference to undefined entity {namespace}.{entity_name} "
                    f"in definition of {current_entity}"
                )
            else:
                case_sensitive_result = self._get_iri_case_insensitive(
                    entity_name,
                    namespace,
                    current_entity,
                    _for_default_rel=_for_default_rel,
                )
            iri = case_sensitive_result if case_sensitive_result else iri
        return iri
Ejemplo n.º 21
0
iri_redirects = 'http://dbpedia.org/redirects'
iri_disamb = 'http://dbpedia.org/disambiguations'
iri_field = 'field'
iri_more = 'field:more'
dbo = Namespace('http://dbpedia.org/ontology/')
dbr = Namespace('http://dbpedia.org/resource/')

# NB: for existing graphs use 'ds.get_context(iri)', for new graphs use 'ds.graph(iri)'
# errors or silent ignoring of actions (on non-existing graphs) otherwise should be expected...
# remove_graph also requires already existing graph
gdb = ds.get_context(iri_dbpedia)
gdbo = ds.get_context(iri_dbo)
glabels = ds.get_context(iri_labels)
gredirects = ds.get_context(iri_redirects)
gdisamb = ds.get_context(iri_disamb)
glo = ReadOnlyGraphAggregate([gdbo, glabels])
gall = ReadOnlyGraphAggregate([gdb, gdbo])
gf = ds.get_context(iri_field)
gmore = ds.get_context(iri_more)
gfall = ReadOnlyGraphAggregate([gf, gmore])


dir_articles = config['data']['articles_dir']


# It can happen that Virtuoso server is at the process of making a checkpoint, which will result in the following exceptions.
# Checkpoint takes few seconds, so, the easy way is just to wait few seconds and try again. Decorator does exactly that.
@except_safe(EndPointNotFound, HTTPError)
def get_article(subject):
    """Fail-safe when article is not present."""
    try:
Ejemplo n.º 22
0
g1.add((stmt1, RDF.object, Literal("Conjunctive Graph")))
g2.add((stmt2, RDF.type, RDF.Statement))
g2.add(
    (stmt2, RDF.subject, URIRef('http://rdflib.net/store/ConjunctiveGraph')))
g2.add((stmt2, RDF.predicate, RDF.type))
g2.add((stmt2, RDF.object, RDFS.Class))
g3.add((stmt3, RDF.type, RDF.Statement))
g3.add(
    (stmt3, RDF.subject, URIRef('http://rdflib.net/store/ConjunctiveGraph')))
g3.add((stmt3, RDF.predicate, RDFS.comment))
g3.add((stmt3, RDF.object,
        Literal("The top-level aggregate graph - The sum " +
                "of all named graphs within a Store")))
len(list(ConjunctiveGraph(store).subjects(RDF.type, RDF.Statement)))
print(list(ConjunctiveGraph(store).subjects(RDF.type, RDF.Statement)))
len(list(ReadOnlyGraphAggregate([g1, g2]).subjects(RDF.type, RDF.Statement)))
print(list(ReadOnlyGraphAggregate([g1, g2]).subjects(RDF.type, RDF.Statement)))

uniqueGraphNames = set([
    graph.identifier
    for s, p, o, graph in ConjunctiveGraph(store).quads((None, RDF.predicate,
                                                         None))
])

print(uniqueGraphNames)

unionGraph = ReadOnlyGraphAggregate([g1, g2])

uniqueGraphNames = set([
    graph.identifier
    for s, p, o, graph in unionGraph.quads((None, RDF.predicate, None))
Ejemplo n.º 23
0
g3.add((stmt3, RDF.object, Literal(
   "The top-level aggregate graph - The sum " +
    "of all named graphs within a Store")))
len(list(ConjunctiveGraph(store).subjects(RDF.type, RDF.Statement)))
print(list(ConjunctiveGraph(store).subjects(RDF.type, RDF.Statement)))
len(list(ReadOnlyGraphAggregate([g1,g2]).subjects(RDF.type, RDF.Statement)))
print(list(ReadOnlyGraphAggregate([g1,g2]).subjects(RDF.type, RDF.Statement)))


uniqueGraphNames = set(
     [graph.identifier for s, p, o, graph in ConjunctiveGraph(store
   ).quads((None, RDF.predicate, None))])

print(uniqueGraphNames)

unionGraph = ReadOnlyGraphAggregate([g1, g2])

uniqueGraphNames = set(
 [graph.identifier for s, p, o, graph in unionGraph.quads(
  (None, RDF.predicate, None))])

print(uniqueGraphNames)


RDFLib = Namespace('http://rdflib.net/')

RDFLib.gswewf
#rdflib.term.URIRef('http://rdflib.net/gswewf')

RDFLib['中文']
#rdflib.term.URIRef('http://rdflib.net/中文')