def setUp(self): self.value = None self.env = Environment() router = LoggingRouter() router.add_to_environment(self.env) self.env.build(DEFCLASS) self.env.build(DEFFUNCTION) self.env.build(DEFRULE_FACT) self.env.build(DEFRULE_INSTANCE) self.env.define_function(python_function) self.env.define_function(self.python_method) self.env.define_function(self.python_fact_method)
def setUp(self): self.values = [] self.env = Environment() self.router = LoggingRouter() self.router.add_to_environment(self.env) self.env.define_function(python_function) self.env.define_function(python_function, name='python-function-renamed') self.env.define_function(python_types) self.env.define_function(self.python_method) self.env.define_function(self.python_fact_method) self.env.build(DEFCLASS) self.env.build(DEFFUNCTION) self.env.build(DEFRULE_FACT) self.env.build(DEFRULE_INSTANCE)
class TestEnvironment(unittest.TestCase): def setUp(self): self.values = [] self.env = Environment() self.router = LoggingRouter() self.router.add_to_environment(self.env) self.env.define_function(python_function) self.env.define_function(python_function, name='python-function-renamed') self.env.define_function(python_types) self.env.define_function(self.python_method) self.env.define_function(self.python_fact_method) self.env.build(DEFCLASS) self.env.build(DEFFUNCTION) self.env.build(DEFRULE_FACT) self.env.build(DEFRULE_INSTANCE) def TearDown(self): self.router.delete() def python_method(self, *values): self.values += values def python_fact_method(self): """Returns a list with one fact.""" template = self.env.find_template('test-fact') fact = template.new_fact() fact.append(5) return [fact] def test_eval_python_function(self): """Python function is evaluated correctly.""" expected = [0, 1.1, "2", Symbol('three'), InstanceName('four')] ret = self.env.eval('(python_function 0 1.1 "2" three [four])') self.assertEqual(ret, expected) expected = [0, 1.1, "2", Symbol('three')] ret = self.env.eval('(python-function-renamed 0 1.1 "2" three)') self.assertEqual(ret, expected) expected = [Symbol('nil'), Symbol('TRUE'), Symbol('FALSE')] ret = self.env.eval('(python_types)') self.assertEqual(ret, expected) def test_eval_python_method(self): """Python method is evaluated correctly.""" expected = [0, 1.1, "2", Symbol('three')] ret = self.env.eval('(python_method 0 1.1 "2" three)') print(self.values) self.assertEqual(ret, Symbol('nil')) self.assertEqual(self.values, expected) def test_rule_python_fact(self): """Facts are forwarded to Python """ fact = self.env.assert_string('(test-fact)') self.env.run() self.assertEqual(self.values[0], fact) def test_rule_python_instance(self): """Instances are forwarded to Python """ cl = self.env.find_class('TEST') inst = cl.new_instance('test') self.env.run() self.assertEqual(self.values[0], inst) self.assertEqual(self.values[1], inst.name) def test_facts_function(self): """Python functions can return list of facts""" function = self.env.find_function('test-fact-function') function() self.assertTrue(isinstance(self.values[0], ImpliedFact)) def test_batch_star(self): """Commands are evaluated from file.""" with TempFile() as tmp: with open(tmp.name, 'wb') as tmpfile: tmpfile.write(b"(assert (test-fact))\n") self.env.batch_star(tmp.name) self.assertTrue( 'test-fact' in (f.template.name for f in self.env.facts())) def test_save_load(self): """Constructs are saved and loaded.""" with TempFile() as tmp: self.env.save(tmp.name) self.env.clear() self.env.load(tmp.name) self.assertTrue('fact-rule' in (r.name for r in self.env.rules())) with TempFile() as tmp: self.env.save(tmp.name, binary=True) self.env.clear() self.env.load(tmp.name) self.assertTrue('fact-rule' in (r.name for r in self.env.rules()))
def setUp(self): self.env = Environment() router = LoggingRouter() router.add_to_environment(self.env) for defclass in DEFCLASSES: self.env.build(defclass)
def setUp(self): self.env = Environment() self.env.add_router(LoggingRouter()) for defclass in DEFCLASSES: self.env.build(defclass)