def test_many_get_used(self): e1 = prov.Entity("test:e1") e2 = prov.Entity("test:e2") activity = prov.Activity("test:activity") activity.set_used(e1) activity.set_used(e2) used_entities = activity.get_used() used_entities = sorted(used_entities, key=lambda entity: entity.identifier) self.assertEqual(len(used_entities), 2) self.assertEqual(used_entities[0].identifier, e1.identifier) self.assertEqual(used_entities[1].identifier, e2.identifier)
def test_fully_qualified_derivation(self): e1 = prov.Entity("test:e1") e2 = prov.Entity("test:e2") activity = prov.Activity("test:activity") derivation = e2.derivation(e1, id="test:derivation") generation = e2.generation(activity, id="test:generation") usage = activity.usage(e1, id="test:usage") derivation.set_had_generation(generation) derivation.set_had_activity(activity) derivation.set_had_usage(usage) self.assertTrue(isinstance(derivation, prov.Derivation)) self.assertTrue(isinstance(generation, prov.Generation)) self.assertTrue(isinstance(usage, prov.Usage))
def test_set_was_generated_by(self): entity = prov.Entity("test:entity") entity.set_label("example entity") activity = prov.Activity("test:activity") activity.set_label("example activity") entity.set_was_generated_by(activity) ttl = prov.serialize(format="turtle") self.assertIsNotNone(ttl)
def test_serialize_jsonld(self): entity = prov.Entity("test:entity") entity.set_label("example entity") jsonld = prov.serialize(format="json-ld") self.assertIsNotNone(jsonld)
def test_serialize_unknown(self): entity = prov.Entity("test:entity") entity.set_label("example entity") self.assertRaises(PluginException, prov.serialize, format="unknown")
def test_serialize_rdfxml(self): entity = prov.Entity("test:entity") entity.set_label("example entity") rdfxml = prov.serialize(format="xml") self.assertIsNotNone(rdfxml)
__author__ = 'szednik' """ This example was taken from http://www.w3.org/TR/prov-primer """ from datetime import datetime import rdflib from kleio import prov DCT = prov.ns("dct", "http://purl.org/dc/terms/") FOAF = prov.ns("foaf", "http://xmlns.com/foaf/0.1/") prov.ns("ex", "http://www.example.org#") # Entities article = prov.Entity("ex:article") article.add(DCT.title, rdflib.Literal("Crime rises in cities")) dataset1 = prov.Entity("ex:dataset1") regionList = prov.Entity("ex:regionList") composition1 = prov.Entity("ex:composition1") chart1 = prov.Entity("ex:chart1") # Activities compile1 = prov.Activity("ex:compile1") compose1 = prov.Activity("ex:compose1")
def test_serialize_trig(self): entity = prov.Entity("test:entity") entity.set_label("example entity") trig = prov.serialize(format="trig") self.assertIsNotNone(trig)
def test_usage(self): activity = prov.Activity("test:activity") role = prov.Role("test:role") entity = prov.Entity("test:entity") usage = activity.usage(entity, role=role) self.assertTrue(isinstance(usage, prov.Usage))
def test_derivation(self): e1 = prov.Entity("test:e1") e2 = prov.Entity("test:e2") derivation = e2.derivation(e1) self.assertTrue(isinstance(derivation, prov.Derivation))
def test_entity_bnode(self): entity = prov.Entity() self.assertTrue(isinstance(entity, prov.Entity))
def test_entity_label(self): entity = prov.Entity("test:entity") entity.set_label("test label") self.assertEqual(entity.get_label()[0], "test label")
def test_get_generated_at_time(self): e1 = prov.Entity("test:e1") dt_str = "2014-05-01T00:00:00" e1.set_generated_at_time(self.get_datetime(dt_str)) dt = e1.get_generated_at_time() self.assertEqual(dt.isoformat(), dt_str)
def test_serialize_n3(self): entity = prov.Entity("test:entity") entity.set_label("example entity") n3 = prov.serialize(format="n3") self.assertIsNotNone(n3)
def test_get_had_primary_source(self): e1 = prov.Entity("test:e1") e2 = prov.Entity("test:e2") e2.set_had_primary_source(e1) primary_sources = e2.get_had_primary_source() self.assertEqual(primary_sources[0].identifier, e1.identifier)
def test_get_was_attributed_to(self): entity = prov.Entity("test:e1") agent = prov.Agent("test:agent") entity.set_was_attributed_to(agent) attributed_to = entity.get_was_attributed_to() self.assertEqual(attributed_to[0].identifier, agent.identifier)
def test_get_was_generated_by(self): entity = prov.Entity("test:e1") activity = prov.Activity("test:activity") entity.set_was_generated_by(activity) generated_by = entity.get_was_generated_by() self.assertEqual(generated_by[0].identifier, activity.identifier)
def test_attribution(self): entity = prov.Entity("test:entity") agent = prov.Agent("test:agent") attribution = entity.attribution(agent) self.assertTrue(isinstance(attribution, prov.Attribution))
def test_get_used(self): entity = prov.Entity("test:e1") activity = prov.Activity("test:activity") activity.set_used(entity) used_entity = activity.get_used() self.assertEqual(used_entity[0].identifier, entity.identifier)
def test_serialize_ntriples(self): entity = prov.Entity("test:entity") entity.set_label("example entity") ntriples = prov.serialize(format="nt") self.assertIsNotNone(ntriples)
def test_urn_entity(self): e1 = prov.Entity("urn:hdl:11121/8375-3759-1904-3620-CC") self.assertTrue(isinstance(e1, prov.Entity))
def test_bundled_entity(self): bundle = prov.bundle(id="test:bundle") e1 = prov.Entity(id="test:entity-in-bundle", bundle=bundle) e2 = prov.Entity(id="test:entity-not-in-bundle") self.assertTrue(e1.identifier in bundle.subjects()) self.assertFalse(e2.identifier in bundle.subjects())
def test_serialize_default(self): entity = prov.Entity("test:entity") entity.set_label("example entity") out = prov.serialize() self.assertIsNotNone(out)
def test_entity_no_label(self): entity = prov.Entity("test:entity") self.assertEqual(entity.get_label(), [])
def test_entity_constructor(self): entity = prov.Entity("test:entity") self.assertEqual(entity.identifier, entity.identifier)
def test_generation(self): entity = prov.Entity("test:entity") activity = prov.Activity("test:activity") generation = entity.generation(activity) self.assertTrue(isinstance(generation, prov.Generation))
def test_serialize_turtle(self): entity = prov.Entity("test:entity") entity.set_label("example entity") ttl = prov.serialize(format="turtle") self.assertIsNotNone(ttl)
__author__ = 'szednik' from kleio import prov # load the existing PROV-O graph prov.default_graph.load("load-example.json", format="json-ld") # get a reference to the existing entity with id="test:entity" entity = prov.Entity("test:entity") # define a new entity and say it was derived from "test:entity" derived_entity = prov.Entity("test:derived_entity") derived_entity.set_label("derived example entity") derived_entity.set_was_derived_from(entity) # note, right now the context of the import is not retained # add test and foaf namespace to prov.context for serialization prov.context.update({ "test": "http://tw.rpi.edu/ns/test#", "foaf": "http://xmlns.com/foaf/0.1/" }) # print out the updated provenance graph print(prov.serialize(format="json-ld"))
__author__ = 'szednik' from kleio import prov prov.ns("test", "http://test.com/ns#") my_bundle = prov.bundle(id="test:bundle") bundle_entity = prov.bundle_entity(bundle=my_bundle) bundle_entity.set_label("bundle entity") entity = prov.Entity(id="test:entity-in-bundle", bundle=my_bundle) entity.set_label("entity in bundle") entity_not_in_bundle = prov.Entity(id="test:entity-not-in-bundle") entity_not_in_bundle.set_label("entity not in bundle") # Serialize using TriG to see the bundle container as a named graph # and PROV assertions not associated with a bundle in the default graph print(prov.serialize(format="trig")) # Serialize using the bundle parameter to see only PROV # assertions associated with the specified bundle print(prov.serialize(format="trig", bundle=my_bundle))