def testToRdfWithCached(self): pred = "test-pred" obj = "test-obj" link = TagLink(pred=pred, obj=obj) bnode = BNode() graph = Graph() namespace = Namespace("test:") node_cache = NodeCache() link.to_rdf(subj=bnode, namespace=namespace, graph=graph, node_cache=node_cache) link.to_rdf(subj=bnode, namespace=namespace, graph=graph, node_cache=node_cache) tag_node = node_cache[f"{pred}:{obj}"] results = graph.query( "select ?s ?p ?o where { ?s a <test:tag> ; ?p ?o } order by ?p ?o") expected_result_tuples = [ ( tag_node, URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), URIRef("test:tag"), ), (tag_node, URIRef("test:key"), Literal("test-pred")), (tag_node, URIRef("test:value"), Literal("test-obj")), ] result_tuples = [] for result in results: s, p, o = result result_tuples.append((s, p, o)) self.assertEqual(expected_result_tuples, result_tuples)
def to_rdf(self) -> Graph: """Generate an rdflib.Graph from this ValidatedGraphSet. Returns: rdf.Graph object representing this ValidatedGraphSet. """ namespace = Namespace(f"{self.name}:") node_cache = NodeCache() graph = Graph() metadata_node = BNode() graph.add((metadata_node, RDF.type, getattr(namespace, "metadata"))) graph.add((metadata_node, getattr(namespace, "name"), Literal(self.name))) graph.add((metadata_node, getattr(namespace, "version"), Literal(self.version))) graph.add( (metadata_node, getattr(namespace, "start_time"), Literal(self.start_time))) graph.add((metadata_node, getattr(namespace, "end_time"), Literal(self.end_time))) for error in self.errors: graph.add((metadata_node, getattr(namespace, "error"), Literal(error))) for resource in self.resources: resource.to_rdf(namespace=namespace, graph=graph, node_cache=node_cache) return graph
def testToRdf(self): pred = "test-multi-pred" obj = [ SimpleLink(pred="test-simple-pred-1", obj="test-simple-obj-1"), SimpleLink(pred="test-simple-pred-2", obj="test-simple-obj-2"), SimpleLink(pred="test-simple-pred-3", obj="test-simple-obj-3"), ] link = MultiLink(pred=pred, obj=obj) bnode = BNode() graph = Graph() namespace = Namespace("test:") node_cache = NodeCache() link.to_rdf(subj=bnode, namespace=namespace, graph=graph, node_cache=node_cache) results = graph.query( "select ?p ?o where {?s a <test:test-multi-pred> ; ?p ?o} order by ?p ?o" ) result_tuples = [] for result in results: self.assertEqual(2, len(result)) result_tuples.append((str(result[0]), str(result[1]))) expected_result_tuples = [ ("http://www.w3.org/1999/02/22-rdf-syntax-ns#type", "test:test-multi-pred"), ("test:test-simple-pred-1", "test-simple-obj-1"), ("test:test-simple-pred-2", "test-simple-obj-2"), ("test:test-simple-pred-3", "test-simple-obj-3"), ] self.assertEqual(result_tuples, expected_result_tuples)
def to_rdf(self, subj: BNode, namespace: Namespace, graph: Graph, node_cache: NodeCache) -> None: """Graph this link on a BNode in a Graph using a given Namespace to create the full predicate. Args: subj: subject portion of triple - graph this link's pred, obj against it. namespace: RDF namespace to use for this triple's predicate graph: RDF graph node_cache: NodeCache to use to find cached nodes. """ link_node = node_cache.setdefault(self.obj, URIRef(self.obj)) graph.add((subj, getattr(namespace, self.pred), link_node))
def to_rdf(self, namespace: Namespace, graph: Graph, node_cache: NodeCache) -> None: """Graph this Resource as a URIRef on a Graph. Args: namespace: RDF namespace to use for predicates and objects when graphing this resource's links graph: RDF graph node_cache: NodeCache to use for any cached URIRef lookups """ node = node_cache.setdefault(self.resource_id, URIRef(self.resource_id)) graph.add((node, RDF.type, getattr(namespace, self.type_name))) graph.add((node, getattr(namespace, "id"), Literal(self.resource_id))) for link in self.links: link.to_rdf(subj=node, namespace=namespace, graph=graph, node_cache=node_cache)
def testToRdfSmallInt(self): pred = "test-pred" obj = 20 link = SimpleLink(pred=pred, obj=obj) bnode = BNode() graph = Graph() namespace = Namespace("test:") node_cache = NodeCache() link.to_rdf(subj=bnode, namespace=namespace, graph=graph, node_cache=node_cache) results = graph.query("select ?s ?p ?o where {?s ?p ?o}") self.assertEqual(1, len(results)) for result in results: _, _, o = result self.assertEqual( o.datatype, URIRef("http://www.w3.org/2001/XMLSchema#integer"))
def testToRdf(self): pred = "test-pred" obj = "test-obj" link = SimpleLink(pred=pred, obj=obj) bnode = BNode() graph = Graph() namespace = Namespace("test:") node_cache = NodeCache() link.to_rdf(subj=bnode, namespace=namespace, graph=graph, node_cache=node_cache) results = graph.query("select ?s ?p ?o where {?s ?p ?o}") self.assertEqual(1, len(results)) for result in results: s, p, o = result self.assertEqual(s, bnode) self.assertEqual(str(p), "test:test-pred") self.assertEqual(str(o), "test-obj")
def to_rdf(self, subj: BNode, namespace: Namespace, graph: Graph, node_cache: NodeCache) -> None: """Graph this link on a BNode in a Graph using a given Namespace to create the full predicate. Args: subj: subject portion of triple - graph this link's pred, obj against it. namespace: RDF namespace to use for this triple's predicate graph: RDF graph node_cache: NodeCache to use to find cached nodes. """ tag_id = f"{self.pred}:{self.obj}" tag_node = node_cache.get(tag_id) if tag_node is None: tag_node = BNode() graph.add((tag_node, namespace.key, Literal(self.pred))) graph.add((tag_node, namespace.value, Literal(self.obj))) graph.add((tag_node, RDF.type, getattr(namespace, "tag"))) node_cache[tag_id] = tag_node graph.add((subj, getattr(namespace, "tag"), tag_node))
def test_setitem_new_key(self): node_cache = NodeCache() node_cache["foo"] = "boo" self.assertEqual(node_cache["foo"], "boo")
def test_setitem_existing_key(self): node_cache = NodeCache() node_cache["foo"] = "boo" with self.assertRaises(KeyError): node_cache["foo"] = "goo"