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_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_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_match_vars(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) matched = collection.not_match_to_vars() self.assertIsNotNone(matched) self.assertEquals(0, len(matched))
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.assertEquals(0, len(matched))
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_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.assertEquals(0, len(not_matched))
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.assertEquals(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_not_match_vars_subject_with_predicate_params(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) not_matched = collection.not_match_to_vars(subject="?x", predicate="LEGS") self.assertIsNotNone(not_matched) self.assertEquals(1, len(not_matched)) self.assertTrue([['?x', 'ELEPHANT'], ['pred', 'TRUNK'], ['obj', 'true']] in not_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_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_match_vars_object_with_subject_predicate_params(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) matched = collection.match_to_vars(subject="MONKEY", predicate="LEGS", obj="?x") self.assertIsNotNone(matched) self.assertEqual(1, len(matched)) self.assertTrue([['subj', 'MONKEY'], ['pred', 'LEGS'], ['?x', '2']] in matched)
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_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 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 test_match_only_subject_only(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) matched = collection.match_only_vars(subject="?x") self.assertIsNotNone(matched) self.assertEquals( [[['?x', 'MONKEY']], [['?x', 'MONKEY']], [['?x', 'ZEBRA']], [['?x', 'BIRD']], [['?x', 'ELEPHANT']]], 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 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.assertEquals(2, len(matched)) self.assertTrue([['?x', '2']] in matched) self.assertTrue([['?x', 'true']] 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_not_matched_as_tuples_predicate(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) all = collection.not_matched_as_tuples(predicate="LEGS") self.assertIsNotNone(all) self.assertEqual(2, len(all)) self.assertTrue(["MONKEY", "HASFUR", "true"] in all) self.assertTrue(["ELEPHANT", "TRUNK", "true"] in all)
def test_match_vars_subject_predicate_with_subject_params(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) matched = collection.match_to_vars(subject="MONKEY", predicate="?y") self.assertIsNotNone(matched) self.assertEquals(2, len(matched)) self.assertTrue([['subj', 'MONKEY'], ['?y', 'LEGS'], ['obj', '2']] in matched) self.assertTrue([['subj', 'MONKEY'], ['?y', 'HASFUR'], ['obj', 'true']] in matched)
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.assertEquals(2, len(matched)) self.assertTrue(["MONKEY", "LEGS", "2"] in matched) self.assertTrue(["BIRD", "LEGS", "2"] in matched)
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 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)
def test_not_match_vars_object_with_subject_predicate_params(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) matched = collection.not_match_to_vars(subject="MONKEY", predicate="LEGS", obj="?x") self.assertIsNotNone(matched) self.assertEqual(3, len(matched)) self.assertTrue([['subj', 'ELEPHANT'], ['pred', 'TRUNK'], ['?x', 'true']] in matched) self.assertTrue([['subj', 'ZEBRA'], ['pred', 'LEGS'], ['?x', '4']] in matched) self.assertTrue([['subj', 'BIRD'], ['pred', 'LEGS'], ['?x', '2']] in matched)
def test_match_only_predicate_and_object(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) matched = collection.match_only_vars(predicate="?x", obj="?y") self.assertIsNotNone(matched) self.assertEquals( [[['?x', 'LEGS'], ['?y', '2']], [['?x', 'HASFUR'], ['?y', 'true']], [['?x', 'LEGS'], ['?y', '4']], [['?x', 'LEGS'], ['?y', '2']], [['?x', 'TRUNK'], ['?y', 'true']]], matched)
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_match_only_vars_no_match(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) matched = collection.match_only_vars(subject="MONKEYX", predicate="?x", obj="2") self.assertIsNotNone(matched) self.assertEqual(0, len(matched)) self.assertEquals([], matched)
def test_not_matched_as_tuples_object(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) all = collection.not_matched_as_tuples(obj="2") self.assertIsNotNone(all) self.assertEqual(3, len(all)) self.assertTrue(["MONKEY", "HASFUR", "true"] in all) self.assertTrue(["ZEBRA", "LEGS", "4"] in all) self.assertTrue(["ELEPHANT", "TRUNK", "true"] in all)
def test_match_only_vars_subject_predictevar_object(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) matched = collection.match_only_vars(subject="MONKEY", predicate="?x", obj="2") self.assertIsNotNone(matched) self.assertEqual(1, len(matched)) self.assertEquals([[['?x', 'LEGS']]], matched)
def test_not_match_vars_subject_with_predicate_params(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) not_matched = collection.not_match_to_vars(subject="?x", predicate="LEGS") self.assertIsNotNone(not_matched) self.assertEqual(1, len(not_matched)) self.assertTrue([['?x', 'ELEPHANT'], ['pred', 'TRUNK'], ['obj', 'true']] in not_matched)
def test_match_vars_predicate_object(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) matched = collection.match_to_vars(predicate="?x", obj="?y") self.assertIsNotNone(matched) self.assertTrue([['subj', 'MONKEY'], ['?x', 'LEGS'], ['?y', '2']] in matched) self.assertTrue([['subj', 'MONKEY'], ['?x', 'HASFUR'], ['?y', 'true']] in matched) self.assertTrue([['subj', 'ELEPHANT'], ['?x', 'TRUNK'], ['?y', 'true']] in matched) self.assertTrue([['subj', 'ZEBRA'], ['?x', 'LEGS'], ['?y', '4']] in matched) self.assertTrue([['subj', 'BIRD'], ['?x', 'LEGS'], ['?y', '2']] in matched)
def test_match_all_as_tuples(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) matched = collection.matched_as_tuples() self.assertIsNotNone(matched) self.assertEquals(5, len(matched)) self.assertTrue(["MONKEY", "LEGS", "2"] in matched) self.assertTrue(["MONKEY", "HASFUR", "true"] in matched) self.assertTrue(["ZEBRA", "LEGS", "4"] in matched) self.assertTrue(["BIRD", "LEGS", "2"] in matched) self.assertTrue(["ELEPHANT", "TRUNK", "true"] in matched)
def test_remove_predicate(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) all = collection.all_as_tuples() remains = collection.remove(all, predicate='LEGS') self.assertIsNotNone(remains) self.assertEquals(2, len(remains)) self.assertTrue(["MONKEY", "HASFUR", "true"] in remains) self.assertTrue(["ELEPHANT", "TRUNK", "true"] in remains)
def test_match_vars_subject_object(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) matched = collection.match_to_vars(subject="?x",obj="?z") self.assertIsNotNone(matched) self.assertEquals(5, len(matched)) self.assertTrue([['?x', 'MONKEY'], ['pred', 'LEGS'], ['?z', '2']] in matched) self.assertTrue([['?x', 'MONKEY'], ['pred', 'HASFUR'], ['?z', 'true']] in matched) self.assertTrue([['?x', 'ELEPHANT'], ['pred', 'TRUNK'], ['?z', 'true']] in matched) self.assertTrue([['?x', 'ZEBRA'], ['pred', 'LEGS'], ['?z', '4']] in matched) self.assertTrue([['?x', 'BIRD'], ['pred', 'LEGS'], ['?z', '2']] in matched)
def test_remove_object(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) all = collection.all_as_tuples() remains = collection.remove(all, obj='2') self.assertIsNotNone(remains) self.assertEquals(3, len(remains)) self.assertTrue(["MONKEY", "HASFUR", "true"] in remains) self.assertTrue(["ZEBRA", "LEGS", "4"] in remains) self.assertTrue(["ELEPHANT", "TRUNK", "true"] in remains)
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 __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)
def test_remove_subject_predicate(self): collection = RDFCollection() self.assertIsNotNone(collection) self.add_data(collection) all = collection.all_as_tuples() remains = collection.remove(all, subject='MONKEY', predicate="LEGS") self.assertIsNotNone(remains) self.assertEquals(4, len(remains)) self.assertTrue(["MONKEY", "HASFUR", "true"] in remains) self.assertTrue(["ZEBRA", "LEGS", "4"] in remains) self.assertTrue(["BIRD", "LEGS", "2"] in remains) self.assertTrue(["ELEPHANT", "TRUNK", "true"] in remains)
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])
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)
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 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)
class Brain(object): 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) def ylogger_type(self): return "brain" @property def id(self): return self._configuration.section_name @property def bot(self): return self._bot @property def configuration(self): return self._configuration @property def aiml_parser(self): return self._aiml_parser @property def denormals(self): return self._denormal_collection @property def normals(self): return self._normal_collection @property def genders(self): return self._gender_collection @property def persons(self): return self._person_collection @property def person2s(self): return self._person2_collection @property def rdf(self): return self._rdf_collection @property def sets(self): return self._sets_collection @property def maps(self): return self._maps_collection @property def properties(self): return self._properties_collection @property def variables(self): return self._variables_collection @property def preprocessors(self): return self._preprocessors @property def postprocessors(self): return self._postprocessors @property def authentication(self): return self._authentication @property def authorisation(self): return self._authorisation @property def default_oob(self): return self._default_oob @property def oobs(self): return self._oob @property def regex_templates(self): return self._regex_templates @property def dynamics(self): return self._dynamics_collection @property def tokenizer(self): return self._tokenizer def load_tokenizer(self): if self.configuration is not None and self.configuration.tokenizer.classname is not None: YLogger.info(self, "Loading tokenizer from class [%s]", self.configuration.tokenizer.classname) tokenizer_class = ClassLoader.instantiate_class(self.configuration.tokenizer.classname) return tokenizer_class(self.configuration.tokenizer.split_chars) else: return Tokenizer(self.configuration.tokenizer.split_chars) def load_aiml_parser(self): return AIMLParser(self) def load_aiml(self, configuration): YLogger.info(self, "Loading aiml source brain") self._aiml_parser.load_aiml(configuration) def reload_aimls(self): YLogger.info(self, "Loading aiml source brain") self._aiml_parser.empty() self._aiml_parser.load_aiml(self.configuration) def load_binary(self, configuration): YLogger.info(self, "Loading binary brain from [%s]", configuration.binaries.binary_filename) try: start = datetime.datetime.now() gc.disable() bin_file = open(configuration.binaries.binary_filename, "rb") self._aiml_parser = pickle.load(bin_file) self._aiml_parser._brain = self gc.enable() bin_file.close() stop = datetime.datetime.now() diff = stop - start YLogger.info(self, "Brain load took a total of %.2f sec", diff.total_seconds()) return False # Tell caller, load succeeded and skip aiml load except Exception as excep: YLogger.exception(self, "Failed to load binary file", excep) if configuration.binaries.load_aiml_on_binary_fail is True: return True # Tell caller, load failed and to load aiml directly else: raise excep def save_binary(self, configuration): YLogger.info(self, "Saving binary brain to [%s]", configuration.binaries.binary_filename) start = datetime.datetime.now() bin_file = open(configuration.binaries.binary_filename, "wb") pickle.dump(self._aiml_parser, bin_file) bin_file.close() stop = datetime.datetime.now() diff = stop - start YLogger.info(self, "Brain save took a total of %.2f sec", diff.total_seconds()) def load(self, configuration: BrainConfiguration): load_aiml = True if self.configuration.binaries.load_binary is True: load_aiml = self.load_binary(configuration) if load_aiml is True: self.load_aiml(configuration) if configuration.binaries.save_binary is True: self.save_binary(configuration) YLogger.info(self, "Loading collections") self.load_collections(configuration) YLogger.info(self, "Loading services") self.load_services(configuration) YLogger.info(self, "Loading security services") self.load_security_services(configuration) YLogger.info(self, "Loading oob processors") self.load_oob_processors(configuration) YLogger.info(self, "Loading regex templates") self.load_regex_templates(configuration) YLogger.info(self, "Loading dynamics sets, maps and vars") self.load_dynamics(configuration) def dump_brain_tree(self): if self.configuration.braintree.file is not None: YLogger.debug(self, "Dumping AIML Graph as tree to [%s]", self._configuration.braintree.file) client_context = self.bot.client.create_client_context("system") self.aiml_parser.pattern_parser.save_braintree( client_context, self.configuration.braintree.file, self.configuration.braintree.content) def _load_denormals(self, configuration): if configuration.files.denormal is not None: self._denormal_collection.empty() total = self._denormal_collection.load_from_filename(configuration.files.denormal) YLogger.info(self, "Loaded a total of %d denormalisations", total) else: YLogger.warning(self, "No configuration setting for denormal") def _load_normals(self, configuration): if configuration.files.normal is not None: self._normal_collection.empty() total = self._normal_collection.load_from_filename(configuration.files.normal) YLogger.info(self, "Loaded a total of %d normalisations", total) else: YLogger.warning(self, "No configuration setting for normal") def _load_genders(self, configuration): if configuration.files.gender is not None: self._gender_collection.empty() total = self._gender_collection.load_from_filename(configuration.files.gender) YLogger.info(self, "Loaded a total of %d genderisations", total) else: YLogger.warning(self, "No configuration setting for gender") def _load_persons(self, configuration): if configuration.files.person is not None: self._person_collection.empty() total = self._person_collection.load_from_filename(configuration.files.person) YLogger.info(self, "Loaded a total of %d persons", total) else: YLogger.warning(self, "No configuration setting for person") def _load_person2s(self, configuration): if configuration.files.person2 is not None: self._person2_collection.empty() total = self._person2_collection.load_from_filename(configuration.files.person2) YLogger.info(self, "Loaded a total of %d person2s", total) else: YLogger.warning(self, "No configuration setting for person2") def _load_properties(self, configuration): if configuration.files.properties is not None: self._properties_collection.empty() total = self._properties_collection.load_from_filename(configuration.files.properties) YLogger.info(self, "Loaded a total of %d properties", total) else: YLogger.warning(self, "No configuration setting for properties") def _load_variables(self, configuration): if configuration.files.variables is not None: self._variables_collection.empty () total = self._variables_collection.load_from_filename(configuration.files.variables) YLogger.info(self, "Loaded a total of %d variables", total) else: YLogger.warning(self, "No configuration setting for variables") def _load_maps(self, configuration): self._maps_collection.empty() total = self._maps_collection.load(configuration.files.map_files) YLogger.info(self, "Loaded a total of %d maps files", total) def reload_map(self, mapname): if self._maps_collection.contains(mapname): filename = self._maps_collection.filename(mapname) self._maps_collection.reload_file(filename) def _load_sets(self, configuration): self._sets_collection.empty() total = self._sets_collection.load(configuration.files.set_files) YLogger.info(self, "Loaded a total of %d sets files", total) def reload_set(self, setname): if self._sets_collection.contains(setname): filename = self._sets_collection.filename(setname) self._sets_collection.reload_file(filename) def _load_rdfs(self, configuration): if configuration.files.rdf_files is not None and configuration.files.rdf_files.files: self._rdf_collection.empty() total = self._rdf_collection.load(configuration.files.rdf_files) YLogger.info(self, "Loaded a total of %d rdf files", total) elif configuration.files.triples is not None: self._rdf_collection.empty() total = self._rdf_collection.load_from_filename(configuration.files.triples) YLogger.info(self, "Loaded a total of %d triples", total) else: YLogger.warning(self, "No configuration setting for triples") def reload_rdf(self, rdfname): if self._rdf_collection.contains(rdfname): self._rdf_collection.reload_file(rdfname) def _load_preprocessors(self, configuration): if configuration.files.preprocessors is not None: self._preprocessors.empty() total = self._preprocessors.load(configuration.files.preprocessors) YLogger.info(self, "Loaded a total of %d pre processors", total) else: YLogger.warning(self, "No configuration setting for pre processors") def _load_postprocessors(self, configuration): if configuration.files.postprocessors is not None: self._postprocessors.empty() total = self._postprocessors.load(configuration.files.postprocessors) YLogger.info(self, "Loaded a total of %d post processors", total) else: YLogger.warning(self, "No configuration setting for post processors") def load_collections(self, configuration): self._load_denormals(configuration) self._load_normals(configuration) self._load_genders(configuration) self._load_persons(configuration) self._load_person2s(configuration) self._load_properties(configuration) self._load_variables(configuration) self._load_rdfs(configuration) self._load_sets(configuration) self._load_maps(configuration) self._load_preprocessors(configuration) self._load_postprocessors(configuration) def load_services(self, configuration): ServiceFactory.preload_services(configuration.services) def load_security_services(self, configuration): if configuration.security is not None: if configuration.security.authentication is not None: if configuration.security.authentication.classname is not None: try: classobject = ClassLoader.instantiate_class( configuration.security.authentication.classname) self._authentication = classobject(configuration.security.authentication) except Exception as excep: YLogger.exception(self, "Failed to load security services", excep) else: YLogger.debug(self, "No authentication configuration defined") if configuration.security.authorisation is not None: if configuration.security.authorisation.classname is not None: try: classobject = ClassLoader.instantiate_class( configuration.security.authorisation.classname) self._authorisation = classobject(configuration.security.authorisation) except Exception as excep: YLogger.exception(self, "Failed to instatiate authorisation class", excep) else: YLogger.debug(self, "No authorisation configuration defined") else: YLogger.debug(self, "No security configuration defined, running open...") def load_dynamics(self, configuration): if configuration.dynamics is not None: self._dynamics_collection.load_from_configuration(configuration.dynamics) else: YLogger.debug(self, "No dynamics configuration defined...") def pre_process_question(self, client_context, question): return self.preprocessors.process(client_context, question) def load_oob_processors(self, configuration): if configuration.oob is not None: if configuration.oob.default() is not None: try: YLogger.info(self, "Loading default oob") classobject = ClassLoader.instantiate_class(configuration.oob.default().classname) self._default_oob = classobject() except Exception as excep: YLogger.exception(self, "Failed to load OOB Processor", excep) for oob_name in configuration.oob.oobs(): try: YLogger.info(self, "Loading oob: %s", oob_name) classobject = ClassLoader.instantiate_class(configuration.oob.oob(oob_name).classname) self._oob[oob_name] = classobject() except Exception as excep: YLogger.exception(self, "Failed to load OOB", excep) def load_regex_templates(self, configuration): if configuration.files.regex_templates is not None: collection = PropertiesCollection() total = collection.load_from_filename(configuration.files.regex_templates) YLogger.info(self, "Loaded a total of %d regex templates", total) self._regex_templates.clear() for pair in collection.pairs: name = pair[0] pattern = pair[1] try: self._regex_templates[name] = re.compile(pattern, re.IGNORECASE) except Exception: YLogger.error(self, "Invalid regex template [%s]", pattern) def regex_template(self, name): if name in self._regex_templates: return self._regex_templates[name] return None def strip_oob(self, response): match = re.compile(r"(.*)(<\s*oob\s*>.*<\/\s*oob\s*>)(.*)") groupings = match.match(response) if groupings is not None: front = groupings.group(1).strip() back = groupings.group(3).strip() response = "" if front != "": response = front + " " response += back oob = groupings.group(2) return response, oob return response, None def process_oob(self, client_context, oob_command): oob_content = ET.fromstring(oob_command) if oob_content.tag == 'oob': for child in oob_content.findall('./'): if child.tag in self._oob: oob_class = self._oob[child.tag] return oob_class.process_out_of_bounds(client_context, child) return self._default_oob.process_out_of_bounds(client_context, child) return "" def post_process_response(self, client_context, response: str): return self.postprocessors.process(client_context, response) def failed_authentication(self, client_context): YLogger.error(client_context, "[%s] failed authentication!") # If we have an SRAI defined, then use that if self.authentication.configuration.denied_srai is not None: match_context = self._aiml_parser.match_sentence(client_context, Sentence(self._bot.brain.tokenizer, self.authentication.configuration.denied_srai), topic_pattern="*", that_pattern="*") # If the SRAI matched then return the result if match_context is not None: return self.resolve_matched_template(client_context, match_context) # Otherswise return the static text, which is either # User defined via config.yaml # Or use the default value BrainSecurityConfiguration.DEFAULT_ACCESS_DENIED return self.authentication.configuration.denied_text def authenticate_user(self, client_context): if self.authentication is not None: if self.authentication.authenticate(client_context) is False: return self.failed_authentication(client_context) return None def resolve_matched_template(self, client_context, match_context): template_node = match_context.template_node() YLogger.debug(client_context, "AIML Parser evaluating template [%s]", template_node.to_string()) response = template_node.template.resolve(client_context) if "<oob>" in response: response, oob = self.strip_oob(response) if oob is not None: oob_response = self.process_oob(client_context, oob) response = response + " " + oob_response return response def ask_question(self, client_context, sentence, srai=False): client_context.brain = self authenticated = self.authenticate_user(client_context) if authenticated is not None: return authenticated conversation = client_context.bot.get_conversation(client_context) topic_pattern = conversation.get_topic_pattern(client_context) that_pattern = conversation.get_that_pattern(client_context, srai) match_context = self._aiml_parser.match_sentence(client_context, sentence, topic_pattern=topic_pattern, that_pattern=that_pattern) if match_context is not None: return self.resolve_matched_template(client_context, match_context) return None