def test_not_match_subject(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) matches = collection.not_match(subject="MONKEY") self.assertEquals(3, len(matches))
def test_not_match(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) matches = collection.not_match() self.assertEquals(0, len(matches))
def __init__(self, configuration: BrainConfiguration): self._configuration = configuration self._aiml_parser = AIMLParser(self) self._denormal_collection = DenormalCollection() self._normal_collection = NormalCollection() self._gender_collection = GenderCollection() self._person_collection = PersonCollection() self._person2_collection = PersonCollection() self._rdf_collection = RDFCollection() self._sets_collection = SetCollection() self._maps_collection = MapCollection() self._properties_collection = PropertiesCollection() self._preprocessors = ProcessorLoader() self._postprocessors = ProcessorLoader() self._authentication = None self._authorisation = None self._default_oob = None self._oob = {} self._regex_templates = {} self._dynamics_collection = DynamicsCollection() self.load(self._configuration)
def __init__(self, bot, configuration: BrainConfiguration): self._bot = bot self._configuration = configuration self._tokenizer = self.load_tokenizer() self._aiml_parser = self.load_aiml_parser() self._denormal_collection = DenormalCollection() self._normal_collection = NormalCollection() self._gender_collection = GenderCollection() self._person_collection = PersonCollection() self._person2_collection = PersonCollection() self._rdf_collection = RDFCollection() self._sets_collection = SetCollection() self._maps_collection = MapCollection() self._properties_collection = PropertiesCollection() self._variables_collection = PropertiesCollection() self._preprocessors = ProcessorLoader() self._postprocessors = ProcessorLoader() self._authentication = None self._authorisation = None self._default_oob = None self._oob = {} self._regex_templates = {} self._dynamics_collection = DynamicsCollection() self.load(self.configuration) self.dump_brain_tree()
def test_apply_rdf_updates_No_files(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.apply_rdf_updates(map_collection, None) updates_store.empty_updates() self.assertFalse(os.path.exists(filepath))
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_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_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 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 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)
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(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"))
def test_not_match_vars(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) matched = collection.not_match_to_vars() self.assertIsNotNone(matched) self.assertEqual(0, len(matched))
def test_not_match_vars_subject_predicate_object(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) matched = collection.not_match_to_vars("?x", "?y", "?z") self.assertIsNotNone(matched) self.assertEqual(0, len(matched))
def test_match_only_vars_no_matched(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) matched = collection.match_only_vars(subject="BEAR", obj="?x") self.assertIsNotNone(matched) self.assertEqual(0, len(matched))
def test_not_matched_as_tuples(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) all = collection.not_matched_as_tuples() self.assertIsNotNone(all) self.assertEqual(0, len(all))
def test_not_match_vars_subject(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) not_matched = collection.not_match_to_vars("?x") self.assertIsNotNone(not_matched) self.assertEqual(0, len(not_matched))
def test_not_match_subject_predicate_object(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) matches = collection.not_match(subject="MONKEY", predicate="legs", object="2") self.assertEquals(3, len(matches))
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])
def assert_empty_named(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")) store.empty_named("ACTIVITY") rdf_collection2 = RDFCollection() store.load_all(rdf_collection2) self.assertFalse(rdf_collection2.contains("ACTIVITY"))
def test_match_as_tuples_subject_object(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) matched = collection.matched_as_tuples(subject="MONKEY", obj="2") self.assertIsNotNone(matched) self.assertEqual(1, len(matched)) self.assertTrue(["MONKEY", "LEGS", "2"] in matched)
def test_match_subject_object(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) matches = collection.match(subject="MONKEY", object="2") self.assertEquals(1, len(matches)) self.assertEqual("MONKEY", matches[0].subject) self.assertEqual("legs", matches[0].predicate) self.assertEqual("2", matches[0].object)
def test_match_only_object_only(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) matched = collection.match_only_vars(obj="?x") self.assertIsNotNone(matched) self.assertEquals([[['?x', '2']], [['?x', 'true']], [['?x', '4']], [['?x', '2']], [['?x', 'true']]], matched)
def test_not_match_predicate(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) matches = collection.not_match(predicate="legs") self.assertEquals(1, len(matches)) self.assertEqual("ELEPHANT", matches[0].subject) self.assertEqual("trunk", matches[0].predicate) self.assertEqual("true", matches[0].object)
def test_match_all_as_tuples_predicate_object(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) matched = collection.matched_as_tuples(predicate="LEGS", obj="2") self.assertIsNotNone(matched) self.assertEqual(2, len(matched)) self.assertTrue(["MONKEY", "LEGS", "2"] in matched) self.assertTrue(["BIRD", "LEGS", "2"] in matched)
def test_match_only_predicate_only(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) matched = collection.match_only_vars(predicate="?x") self.assertIsNotNone(matched) self.assertEquals( [[['?x', 'LEGS']], [['?x', 'HASFUR']], [['?x', 'LEGS']], [['?x', 'LEGS']], [['?x', 'TRUNK']]], matched)
def test_match_only_vars_predicte_objectvar(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) matched = collection.match_only_vars(predicate="LEGS", obj="?x") self.assertIsNotNone(matched) self.assertEqual(3, len(matched)) self.assertTrue([['?x', '2']] in matched) self.assertTrue([['?x', '4']] in matched)
def test_load_from_test_dir_no_subdir(self): config = FileStorageConfiguration() config._rdf_storage = FileStoreConfiguration(dirs=[os.path.dirname(__file__) + os.sep + "data" + os.sep + "rdfs" + os.sep + "text"], extension="rdf", subdirs=False, format="text", encoding="utf-8", delete_on_start=False) engine = FileStorageEngine(config) engine.initialise() store = FileRDFStore(engine) map_collection = RDFCollection() store.load_all(map_collection) self.assertTrue(map_collection.contains('ACTIVITY'))
def test_match_only_vars_subject_predicate(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) matched = collection.match_only_vars(subject="MONKEY", predicate="?x") self.assertIsNotNone(matched) self.assertEqual(2, len(matched)) self.assertTrue([['?x', 'LEGS']] in matched) self.assertTrue([['?x', 'HASFUR']] in matched)
def match_only_vars(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) matched = collection.match_only_vars(subject="MONKEY", obj="?x") self.assertIsNotNone(matched) self.assertEqual(2, len(matched)) self.assertTrue([['?x', '2']] in matched) self.assertTrue([['?x', 'true']] in matched)