Exemple #1
0
    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"))
Exemple #2
0
    def test_delete_collection_subject_predicate_diff_object(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"))

        collection.delete_entity("ACCOUNT", "hasSize", "1")

        self.assertTrue(collection.has_subject('ACCOUNT'))
        self.assertTrue(collection.has_predicate('ACCOUNT', 'hasSize'))
        self.assertTrue(collection.has_object('ACCOUNT', 'hasSize', "0"))
    def test_delete_rdf_collection_subject(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")

        self.assertFalse(collection.has_subject('ACCOUNT'))
        self.assertFalse(collection.has_predicate('ACCOUNT', 'hasSize'))
        self.assertFalse(collection.has_object('ACCOUNT', 'hasSize', "0"))
Exemple #4
0
    def test_delete_collection_subject_predicate_diff_object(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", "1")

        self.assertTrue(collection.has_subject('ACCOUNT'))
        self.assertTrue(collection.has_predicate('ACCOUNT', 'hasSize'))
        self.assertTrue(collection.has_object('ACCOUNT', 'hasSize', "0"))
Exemple #5
0
    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()
Exemple #6
0
    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_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_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()
Exemple #9
0
    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"))
Exemple #10
0
    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'))
Exemple #11
0
    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'))
Exemple #12
0
    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"))
Exemple #13
0
    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"))
Exemple #14
0
    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"))
Exemple #15
0
    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"))
Exemple #16
0
    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"))
Exemple #17
0
    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"))
Exemple #18
0
    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'))