def test_reload_from_file(self): config = FileStorageConfiguration() config._rdf_storage = FileStoreConfiguration(dirs=[ os.path.dirname(__file__) + os.sep + "test_files" + os.sep + "rdfs" ]) factory = StorageFactory() storage_engine = FileStorageEngine(config) factory._storage_engines[StorageFactory.RDF] = storage_engine factory._store_to_engine_map[StorageFactory.RDF] = storage_engine storage_engine.initialise() collection = RDFCollection() self.assertIsNotNone(collection) collection.load(factory) self.assertTrue(collection.has_subject("TEST1")) self.assertTrue(collection.has_predicate("TEST1", "HASPURPOSE")) self.assertTrue(collection.has_object("TEST1", "HASPURPOSE", "to test")) collection.delete_entity("TEST1", "HASPURPOSE", "to test") self.assertFalse( collection.has_object("TEST1", "HASPURPOSE", "to test")) collection.reload(factory, "TESTDATA") self.assertTrue(collection.has_subject("TEST1")) self.assertTrue(collection.has_predicate("TEST1", "HASPURPOSE")) self.assertTrue(collection.has_object("TEST1", "HASPURPOSE", "to test"))
def test_collection_update_to_updates_file(self): config = FileStorageConfiguration() tmpdir = os.path.dirname(__file__) + os.sep + "rdf_updates" config.rdf_updates_storage._dirs = [tmpdir] config.rdf_updates_storage._has_single_file = True factory = StorageFactory() storage_engine = FileStorageEngine(config) factory._storage_engines[StorageFactory.RDF_UPDATES] = storage_engine factory._store_to_engine_map[ StorageFactory.RDF_UPDATES] = storage_engine updates_engine = factory.entity_storage_engine( StorageFactory.RDF_UPDATES) updates_store = updates_engine.rdf_updates_store() updates_store.empty() collection = RDFCollection() self.assertIsNotNone(collection) collection._storage_factory = factory collection.add_entity("ACCOUNT", "hasSize", "0", "BANKING", "BANIKING") self.assertTrue(collection.has_subject('ACCOUNT')) self.assertTrue(collection.has_predicate('ACCOUNT', 'hasSize')) self.assertTrue(collection.has_object('ACCOUNT', 'hasSize', "0")) collection.delete_entity("ACCOUNT", "hasSize", "0") self.assertFalse(collection.has_subject('ACCOUNT')) updates_store.empty()
def test_delete_collection_subject_predicate(self): collection = RDFCollection() self.assertIsNotNone(collection) collection.add_entity("ACCOUNT", "hasSize", "0") self.assertTrue(collection.has_subject('ACCOUNT')) self.assertTrue(collection.has_predicate('ACCOUNT', 'hasSize')) self.assertTrue(collection.has_object('ACCOUNT', 'hasSize', "0")) collection.delete_entity("ACCOUNT", "hasSize") self.assertFalse(collection.has_subject('ACCOUNT'))
def test_delete_collection_subject_predicate(self): collection = RDFCollection() self.assertIsNotNone(collection) collection.add_entity("ACCOUNT", "hasSize", "0", "BANIKING") self.assertTrue(collection.has_subject('ACCOUNT')) self.assertTrue(collection.has_predicate('ACCOUNT', 'hasSize')) self.assertTrue(collection.has_object('ACCOUNT', 'hasSize', "0")) collection.delete_entity("ACCOUNT", "hasSize") self.assertFalse(collection.has_subject('ACCOUNT'))
def test_apply_updates(self): config = FileStorageConfiguration() config._rdf_storage = FileStoreConfiguration(dirs=[ os.path.dirname(__file__) + os.sep + "test_files" + os.sep + "rdfs" ]) tmpdir = os.path.dirname(__file__) + os.sep + "rdf_updates" config.rdf_updates_storage._dirs = [tmpdir] config.rdf_updates_storage._has_single_file = True factory = StorageFactory() storage_engine = FileStorageEngine(config) factory._storage_engines[StorageFactory.RDF] = storage_engine factory._store_to_engine_map[StorageFactory.RDF] = storage_engine factory._storage_engines[StorageFactory.RDF_UPDATES] = storage_engine factory._store_to_engine_map[ StorageFactory.RDF_UPDATES] = storage_engine storage_engine.initialise() updates_engine = factory.entity_storage_engine( StorageFactory.RDF_UPDATES) updates_store = updates_engine.rdf_updates_store() updates_store.empty() collection = RDFCollection() self.assertIsNotNone(collection) collection.load(factory) self.assertTrue(collection.has_subject("TEST1")) self.assertTrue(collection.has_predicate("TEST1", "HASPURPOSE")) self.assertTrue(collection.has_object("TEST1", "HASPURPOSE", "to test")) collection.apply_updates()
def assert_upload_from_text(self, store): store.empty() store.upload_from_text( "ACTIVITY", """ ACT:hasPurpose:to entertain by performing ACT:hasSize:0 ACT:hasSyllables:1 ACT:isa:Activity ACT:lifeArea:Recreation ADVENTURE:hasPurpose:to provide new experience ADVENTURE:hasSize:0 ADVENTURE:hasSyllables:3 ADVENTURE:isa:Activity ADVENTURE:lifeArea:Recreation FISHING:hasPurpose:to hunt for fish FISHING:hasSize:0 FISHING:hasSyllables:2 FISHING:isa:Activity FISHING:lifeArea:Recreation """) rdf_collection = RDFCollection() store.load(rdf_collection, "ACTIVITY") self.assertTrue(rdf_collection.contains("ACTIVITY")) self.assertTrue(rdf_collection.has_subject('ACT')) self.assertTrue(rdf_collection.has_predicate('ACT', "hasPurpose")) self.assertTrue( rdf_collection.has_object('ACT', "hasPurpose", "to entertain by performing"))
def test_add_collection_with_rdfstore_entityid(self): collection = RDFCollection() self.assertIsNotNone(collection) collection.add_entity("ACCOUNT", "hasSize", "0", "BANKING", rdf_store="file", entityid="1") collection.add_entity("ACCOUNT", "hasSize", "0", "BANKING") self.assertTrue(collection.has_subject('ACCOUNT')) self.assertTrue(collection.has_predicate('ACCOUNT', 'hasSize')) self.assertTrue(collection.has_object('ACCOUNT', 'hasSize', "0")) self.assertFalse(collection.has_object('ACCOUNTX', 'hasSize', "0")) self.assertFalse(collection.has_object('ACCOUNT', 'hasSizeX', "0")) self.assertFalse(collection.has_object('ACCOUNT', 'hasSize', "§")) self.assertEquals("file", collection.storename("BANKING")) self.assertEquals("BANKING", collection.entities_to_stores["ACCOUNT"]) entities = collection.entities_to_ids["1"] self.assertIsNotNone(entities) self.assertEquals(1, len(entities)) self.assertEquals({'HASSIZE': ['0', '0']}, entities[0].predicates)
def assert_upload_csv_files_from_directory_with_subdir(self, store): store.empty() store.upload_from_directory(os.path.dirname(__file__)+os.sep+"data"+os.sep+"rdfs"+os.sep+"csv", subdir=True, fileformat=Store.CSV_FORMAT) rdf_collection = RDFCollection() store.load_all(rdf_collection) self.assertTrue(rdf_collection.contains("ACTIVITY")) self.assertTrue(rdf_collection.has_subject('ACT')) self.assertTrue(rdf_collection.has_predicate('ACT', "hasPurpose")) self.assertTrue(rdf_collection.has_object('ACT', "hasPurpose", "to entertain by performing")) self.assertTrue(rdf_collection.contains("ANIMAL")) self.assertTrue(rdf_collection.has_subject('ANT')) self.assertTrue(rdf_collection.has_predicate('ANT', "hasPurpose")) self.assertTrue(rdf_collection.has_object('ANT', "hasPurpose", "to make anthills"))
def test_add_collection(self): collection = RDFCollection() self.assertIsNotNone(collection) collection.add_entity("ACCOUNT", "hasSize", "0") self.assertTrue(collection.has_subject('ACCOUNT')) self.assertTrue(collection.has_predicate('ACCOUNT', 'hasSize')) self.assertTrue(collection.has_object('ACCOUNT', 'hasSize', "0"))
def test_apply_rdf_updates_intime(self): config = FileStorageConfiguration() config._rdf_storage = FileStoreConfiguration(dirs=[ os.path.dirname(__file__) + os.sep + "data" + os.sep + "rdfs" + os.sep + "text" ], extension="rdf", subdirs=True, format="text", encoding="utf-8", delete_on_start=False) tmpdir = os.path.dirname(__file__) + os.sep + "rdf_updates" config.rdf_updates_storage._dirs = [tmpdir] config.rdf_updates_storage._has_single_file = True engine = FileStorageEngine(config) engine.initialise() rdf_store = FileRDFStore(engine) updates_store = FileRDFUpdatesStore(engine) filepath = updates_store._updates_filename( updates_store._get_storage_path()) updates_store.empty_updates() self.assertFalse(os.path.exists(filepath)) map_collection = RDFCollection() rdf_store.load_all(map_collection) updates_store.add_entry("subject1", "predicate1", "object1") updates_store.add_entry("subject2", "predicate2", "object2") lastDate = datetime.now() sleep(1) updates_store.add_entry("subject3", "predicate3", "object3") updates_store.delete_entry("subject2", None, None) updates_store.apply_rdf_updates(map_collection, lastDate) self.assertFalse(map_collection.has_subject('SUBJECT1')) self.assertFalse(map_collection.has_subject('SUBJECT2')) self.assertTrue(map_collection.has_subject('SUBJECT3')) updates_store.empty_updates() self.assertFalse(os.path.exists(filepath))
def test_collection(self): collection = RDFCollection() self.assertIsNotNone(collection) count = collection.load_from_text(""" ACCOUNT:hasPurpose:to track money ACCOUNT:hasSize:0 ACCOUNT:hasSyllables:2 ACCOUNT:isa:Concept ACCOUNT:lifeArea:Finances ACT:hasPurpose:to entertain by performing """) self.assertEqual(count, 6) self.assertTrue(collection.has_subject('ACCOUNT')) self.assertTrue(collection.has_predicate('ACCOUNT', 'hasSize')) self.assertTrue(collection.has_object('ACCOUNT', 'ISA', 'Concept')) self.assertFalse(collection.has_subject('ACCOUNTX')) self.assertFalse(collection.has_predicate('ACCOUNT', 'hasSizeX')) self.assertFalse(collection.has_object('ACCOUNT', 'isa', 'ConceptX'))
def assert_upload_from_csv_file(self, store): store.empty() store.upload_from_file(os.path.dirname(__file__)+os.sep+"data"+os.sep+"rdfs"+os.sep+"csv"+os.sep+"activity.csv", fileformat=Store.CSV_FORMAT) rdf_collection = RDFCollection() store.load(rdf_collection, "ACTIVITY") self.assertTrue(rdf_collection.contains("ACTIVITY")) self.assertTrue(rdf_collection.has_subject('ACT')) self.assertTrue(rdf_collection.has_predicate('ACT', "hasPurpose")) self.assertTrue(rdf_collection.has_object('ACT', "hasPurpose", "to entertain by performing"))
def assert_load_all(self, store): store.empty() store.upload_from_file(os.path.dirname(__file__)+os.sep+"data"+os.sep+"rdfs"+os.sep+"text"+os.sep+"activity.rdf") rdf_collection = RDFCollection() store.load_all(rdf_collection) self.assertTrue(rdf_collection.contains("ACTIVITY")) self.assertTrue(rdf_collection.has_subject('ACT')) self.assertTrue(rdf_collection.has_predicate('ACT', "hasPurpose")) self.assertTrue(rdf_collection.has_object('ACT', "hasPurpose", "to entertain by performing"))
def assert_upload_text_files_from_directory_no_subdir(self, store): store.empty() store.upload_from_directory(os.path.dirname(__file__)+os.sep+"data"+os.sep+"rdfs"+os.sep+"text", subdir=False) rdf_collection = RDFCollection() store.load(rdf_collection, "ACTIVITY") self.assertTrue(rdf_collection.contains("ACTIVITY")) self.assertTrue(rdf_collection.has_subject('ACT')) self.assertTrue(rdf_collection.has_predicate('ACT', "hasPurpose")) self.assertTrue(rdf_collection.has_object('ACT', "hasPurpose", "to entertain by performing"))
def test_add_multi_object_collection(self): collection = RDFCollection() self.assertIsNotNone(collection) collection.add_entity("ACTOR", "ISA", "PERSON", "TEST") collection.add_entity("ACTOR", "ISA", "MAN", "TEST") self.assertTrue(collection.has_subject('ACTOR')) self.assertTrue(collection.has_predicate('ACTOR', 'ISA')) self.assertTrue(collection.has_object('ACTOR', 'ISA', "PERSON")) self.assertTrue(collection.has_object('ACTOR', 'ISA', "MAN"))
def test_collection_others(self): collection = RDFCollection() self.assertIsNotNone(collection) collection.add_entity("ACCOUNT", "hasSize", "0", "BANKING", "BANIKING") self.assertTrue(collection.has_subject('ACCOUNT')) self.assertTrue(collection.has_predicate('ACCOUNT', 'hasSize')) self.assertTrue(collection.has_object('ACCOUNT', 'hasSize', "0")) self.assertIsNone(collection.storename("BANKING1")) self.assertEqual(0, len(collection.predicates("account1"))) self.assertEqual(0, len(collection.objects("ACCOUNT1", "hasSize"))) self.assertEqual(0, len(collection.objects("ACCOUNT", "hasSize1"))) self.assertFalse(collection.has_object("ACCOUNT", "hasSize", "1"))
def assert_rdf_storage(self, store): store.empty() store.add_rdf("ACTIVITY", "ACT", "hasPurpose", "to entertain by performing") store.add_rdf("ACTIVITY", "ACT", "hasSize", "0") store.add_rdf("ACTIVITY", "ACT", "hasSyllables", "1") store.add_rdf("ACTIVITY", "ACT", "isa", "Activity0") store.add_rdf("ACTIVITY", "ACT", "lifeArea", "Recreation") store.commit() rdf_collection = RDFCollection() store.load(rdf_collection, "ACTIVITY") self.assertTrue(rdf_collection.contains("ACTIVITY")) self.assertTrue(rdf_collection.has_subject('ACT')) self.assertTrue(rdf_collection.has_predicate('ACT', "hasPurpose")) self.assertTrue(rdf_collection.has_object('ACT', "hasPurpose", "to entertain by performing"))
def test_add_collection(self): collection = RDFCollection() self.assertIsNotNone(collection) collection.add_entity("ACCOUNT", "hasSize", "0", "BANKING") self.assertTrue(collection.has_subject('ACCOUNT')) self.assertTrue(collection.has_predicate('ACCOUNT', 'hasSize')) self.assertTrue(collection.has_object('ACCOUNT', 'hasSize', "0")) self.assertIsNone(collection.storename("BANKING")) self.assertFalse(collection.has_object('ACCOUNTX', 'hasSize', "0")) self.assertFalse(collection.has_object('ACCOUNT', 'hasSizeX', "0")) self.assertFalse(collection.has_object('ACCOUNT', 'hasSize', "§")) self.assertEquals(['ACCOUNT'], collection.subjects()) self.assertEquals(['HASSIZE'], collection.predicates('ACCOUNT')) self.assertEquals([], collection.predicates('ACCOUNTXX')) self.assertEquals([['0']], collection.objects('ACCOUNT', 'HASSIZE')) self.assertEquals([], collection.objects('ACCOUNT', 'HASSIZEXX'))