예제 #1
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)
예제 #2
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()
예제 #3
0
    def test_unify_multi_var_deep(self):
        collection = RDFCollection()
        self.assertIsNotNone(collection)

        collection.add_entity("TEST1", "ISA", "TEST2", "TEST")
        collection.add_entity("TEST2", "ISA", "TEST3", "TEST")
        collection.add_entity("TEST3", "ISA", "TEST4", "TEST")
        collection.add_entity("TEST4", "ISA", "TEST5", "TEST")

        set1 = collection.match_to_vars("?x", "ISA", "?y")
        set2 = collection.match_to_vars("?y", "ISA", "?z")
        set3 = collection.match_to_vars("?z", "ISA", "?w")

        unified = collection.unify(("?x", "?y", "?z", "?w"),
                                   [set1, set2, set3])
        self.assertIsNotNone(unified)
        self.assertEqual(2, len(unified))
        self.assertTrue([
            ['?x', 'TEST1'],
            ['?y', 'TEST2'],
            ['?z', 'TEST3'],
            ['?w', 'TEST4'],
        ] in unified)
        self.assertTrue([['?x', 'TEST2'], ['?y', 'TEST3'], ['?z', 'TEST4'],
                         ['?w', 'TEST5']] in unified)
예제 #4
0
    def test_add_collection(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"))
예제 #5
0
    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"))
예제 #6
0
    def test_predicates(self):
        collection = RDFCollection()
        self.assertIsNotNone(collection)

        collection.add_entity("MONKEY", "legs", "2")
        collection.add_entity("ZEBRA", "legs", "4")

        predicates = collection.predicates("MONKEY")
        self.assertEquals(1, len(predicates))
        self.assertEquals("legs", predicates[0])
예제 #7
0
    def test_subjects(self):
        collection = RDFCollection()
        self.assertIsNotNone(collection)

        collection.add_entity("MONKEY", "legs", "2")
        collection.add_entity("ZEBRA", "legs", "4")

        subjects = collection.subjects()
        self.assertEquals(2, len(subjects))
        self.assertTrue("MONKEY" in subjects)
        self.assertTrue("ZEBRA" in subjects)
예제 #8
0
    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'))
예제 #9
0
    def test_add_collection_multiple_with_entityid(self):
        collection = RDFCollection()
        self.assertIsNotNone(collection)

        collection.add_entity("ACCOUNT", "hasSize", "0", "BANKING", entityid="1")
        collection.add_entity("ACCOUNT", "hasValue", "1", "BANKING", entityid="1")

        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'], 'HASVALUE': ['1']}, entities[0].predicates)
예제 #10
0
    def test_delete_collection_subject_predicate_no_exists_with_obj(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", "hasSizeX", "0")

        self.assertTrue(collection.has_subject('ACCOUNT'))
예제 #11
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"))
예제 #12
0
    def test_chungyilinxrspace_issue_175(self):
        collection = RDFCollection()
        self.assertIsNotNone(collection)

        collection.add_entity("ACTOR", "ISA", "PERSON", "TEST")
        collection.add_entity("ACTOR", "ISA", "MAN", "TEST")

        set1 = collection.match_to_vars("ACTOR", "ISA", "?x")

        self.assertTrue(
            [['subj', 'ACTOR'], ['pred', 'ISA'], ['?x', 'MAN']] in set1)
        self.assertTrue(
            [['subj', 'ACTOR'], ['pred', 'ISA'], ['?x', 'PERSON']] in set1)
예제 #13
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"))
예제 #14
0
    def test_objects(self):
        collection = RDFCollection()
        self.assertIsNotNone(collection)

        collection.add_entity("MONKEY", "legs", "2")
        collection.add_entity("MONKEY", "hasFur", "true")
        collection.add_entity("ZEBRA", "legs", "4")
        collection.add_entity("BIRD", "legs", "2")
        collection.add_entity("ELEPHANT", "trunk", "true")

        subjects = collection.subjects()
        self.assertEquals(4, len(subjects))

        object = collection.objects(subject="MONKEY", predicate="legs")
        self.assertEqual(["2"], object)
예제 #15
0
    def test_unify_on_multi_vars(self):
        collection = RDFCollection()
        self.assertIsNotNone(collection)

        collection.add_entity("TEST1", "ISA", "TEST2")
        collection.add_entity("TEST2", "ISA", "TEST3")

        set1 = collection.match_to_vars("?x", "ISA", "?y")
        set2 = collection.match_to_vars("?y", "ISA", "?z")

        unified = collection.unify(("?x", "?y", "?z"), [set1, set2])
        self.assertIsNotNone(unified)
        self.assertEquals(1, len(unified))
        self.assertTrue(["?x", "TEST1"] in unified[0])
        self.assertTrue(["?y", "TEST2"] in unified[0])
        self.assertTrue(["?z", "TEST3"] in unified[0])
예제 #16
0
    def test_unify_on_multi_vars(self):
        collection = RDFCollection()
        self.assertIsNotNone(collection)

        collection.add_entity("TEST1", "ISA", "TEST2", "TEST")
        collection.add_entity("TEST2", "ISA", "TEST3", "TEST")

        set1 = collection.match_to_vars("?x", "ISA", "?y")
        set2 = collection.match_to_vars("?y", "ISA", "?z")

        unified = collection.unify(("?x", "?y", "?z"), [set1, set2])
        self.assertIsNotNone(unified)
        self.assertEqual(1, len(unified))
        self.assertTrue(["?x", "TEST1"] in unified[0])
        self.assertTrue(["?y", "TEST2"] in unified[0])
        self.assertTrue(["?z", "TEST3"] in unified[0])
예제 #17
0
    def test_unify_on_single_var_with_not(self):
        collection = RDFCollection()
        self.assertIsNotNone(collection)

        collection.add_entity("MONKEY", "LEGS", "2")
        collection.add_entity("MONKEY", "HASFUR", "true")
        collection.add_entity("ZEBRA", "LEGS", "4")
        collection.add_entity("BIRD", "LEGS", "2")
        collection.add_entity("ELEPHANT", "TRUNK", "true")

        set1 = collection.match_to_vars("?x", "LEGS", "2")
        set2 = collection.not_match_to_vars("?x", "HASFUR", "true")

        unified = collection.unify(["?x"], [set1, set2])
        self.assertIsNotNone(unified)
        self.assertEquals(1, len(unified))
        self.assertTrue([['?x', 'BIRD']] in unified)
예제 #18
0
    def test_unify_on_single_var(self):
        collection = RDFCollection()
        self.assertIsNotNone(collection)

        collection.add_entity("MONKEY", "LEGS", "2", "ANIMAL")
        collection.add_entity("MONKEY", "HASFUR", "true", "ANIMAL")
        collection.add_entity("ZEBRA", "LEGS", "4", "ANIMAL")
        collection.add_entity("BIRD", "LEGS", "2", "ANIMAL")
        collection.add_entity("ELEPHANT", "TRUNK", "true", "ANIMAL")

        set1 = collection.match_to_vars("?x", "LEGS", "2")
        set2 = collection.match_to_vars("?x", "HASFUR", "true")

        unified = collection.unify(["?x"], [set1, set2])
        self.assertIsNotNone(unified)
        self.assertEqual(1, len(unified))
        self.assertTrue([['?x', 'MONKEY']] in unified)
예제 #19
0
    def test_unify_multi_var_deep(self):
        collection = RDFCollection()
        self.assertIsNotNone(collection)

        collection.add_entity("TEST1", "ISA", "TEST2")
        collection.add_entity("TEST2", "ISA", "TEST3")
        collection.add_entity("TEST3", "ISA", "TEST4")
        collection.add_entity("TEST4", "ISA", "TEST5")

        set1 = collection.match_to_vars("?x", "ISA", "?y")
        set2 = collection.match_to_vars("?y", "ISA", "?z")
        set3 = collection.match_to_vars("?z", "ISA", "?w")

        unified = collection.unify(("?x", "?y", "?z", "?w"), [set1, set2, set3])
        self.assertIsNotNone(unified)
        self.assertEquals(2, len(unified))
        self.assertTrue([['?x', 'TEST1'], ['?y', 'TEST2'], ['?z', 'TEST3'], ['?w', 'TEST4'], ] in unified)
        self.assertTrue([['?x', 'TEST2'], ['?y', 'TEST3'], ['?z', 'TEST4'], ['?w', 'TEST5']] in unified)
예제 #20
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'))
예제 #21
0
    def test_empty_by_name(self):
        collection = RDFCollection()
        self.assertIsNotNone(collection)

        collection.add_entity("ACCOUNT", "hasSize", "0", rdf_name="BANKING", rdf_store="TEST", entityid="1")
        collection.add_entity("BALANCE", "hasValue", "1", rdf_name="BANKING", rdf_store="TEST", entityid="2")

        collection.add_entity("WALKING", "hasSize", "0", rdf_name="ACTIVITY", rdf_store="TEST", entityid="3")
        collection.add_entity("RUNNING", "hasValue", "1", rdf_name="ACTIVITY", rdf_store="TEST", entityid="4")

        self.assertEquals("BANKING", collection.entities_to_stores['ACCOUNT'])
        self.assertEquals("ACTIVITY", collection.entities_to_stores['RUNNING'])

        self.assertTrue(collection.contains('BANKING'))
        self.assertTrue(collection.contains('ACTIVITY'))

        collection.empty("BANKING")

        self.assertFalse(collection.contains('BANKING'))
        self.assertTrue(collection.contains('ACTIVITY'))