def test_method(self): """Defgeneric object test.""" restr = [ 2, 2, 2, 6, 9, Symbol('FALSE'), 1, Symbol('INTEGER'), Symbol('FALSE'), 1, Symbol('INTEGER') ] func = self.env.find_generic("generic-sum") method = tuple(func.methods())[0] self.assertTrue('defmethod' in str(method)) self.assertTrue('defmethod' in repr(method)) self.assertTrue(method.deletable) self.assertFalse(method.watch) self.assertEqual(method.description, "1 (INTEGER) (INTEGER)") self.assertEqual(method.restrictions, restr) method.watch = True self.assertTrue(method.watch) method.undefine() self.assertTrue(method not in func.methods())
def test_template_fact(self): """TemplateFacts are asserted.""" expected = { '': 'template-fact', 'int': 1, 'float': 2.2, 'str': '4', 'symbol': Symbol('five'), 'multifield': [1, 2] } template = self.env.facts.find_template('template-fact') fact = template.new_fact() fact['int'] = 1 fact.update({'float': 2.2, 'str': '4'}) fact.update((('symbol', Symbol('five')), ('multifield', [1, 2]))) fact.assertit() self.assertEqual(fact.index, 1) for asserted_fact in self.env.facts.facts(): if asserted_fact == fact: break self.assertEqual(len(asserted_fact), 6) self.assertEqual(asserted_fact.index, 1) self.assertEqual(asserted_fact['int'], 1) self.assertEqual(dict(asserted_fact), expected) self.assertEqual(str(asserted_fact), TMPL_STR) self.assertEqual(repr(asserted_fact), TMPL_RPR)
def test_instance(self): """Instance test.""" defclass = self.env.find_class('ConcreteClass') instance_name = self.env.eval( '(make-instance test-eval-instance of ConcreteClass)') self.assertEqual(instance_name, 'test-eval-instance') self.assertTrue(isinstance(instance_name, InstanceName)) instance = self.env.make_instance( '(test-make-instance of ConcreteClass (Slot value))') self.assertEqual(instance.name, 'test-make-instance') self.assertEqual(instance['Slot'], Symbol('value')) defclass.new_instance('some-instance') instance = defclass.new_instance('test-instance') instance['Slot'] = Symbol('value') self.assertTrue(instance in defclass.instances()) self.assertEqual(instance.name, 'test-instance') self.assertEqual(instance.instance_class, defclass) self.assertEqual(instance['Slot'], Symbol('value')) self.assertEqual(str(instance), '[test-instance] of ConcreteClass (Slot value)') self.assertEqual( repr(instance), 'Instance: [test-instance] of ConcreteClass (Slot value)') self.assertEqual(dict(instance), {'Slot': Symbol('value')}) instance.delete() instance = defclass.new_instance('test-instance') instance.unmake()
def test_eval_python_method(self): """Python method is evaluated correctly.""" expected = 0, 1.1, "2", Symbol('three') ret = self.env.eval('(python-function python_method 0 1.1 "2" three)') self.assertEqual(ret, Symbol('nil')) self.assertEqual(self.value, expected)
def test_eval_python_function(self): """Python function is evaluated correctly.""" expected = [0, 1.1, "2", Symbol('three')] ret = self.env.eval('(python_function 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_modify_instance(self): """Instance slot modification test.""" defclass = self.env.find_class('ConcreteClass') defclass.make_instance('some-instance') instance = defclass.make_instance('test-instance', Slot=Symbol('value')) instance.modify_slots(Slot=Symbol('another-value')) self.assertEqual(instance['Slot'], Symbol('another-value')) instance.delete()
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_modify_fact(self): """Asserted TemplateFacts can be modified.""" template = self.env.find_template('template-fact') fact = template.assert_fact( **{ 'int': 1, 'float': 2.2, 'str': '4', 'symbol': Symbol('five'), 'multifield': (1, 2) }) fact.modify_slots(symbol=Symbol('six')) self.assertEqual(fact['symbol'], Symbol('six'))
def add_emergency(): global emergency_count emergency = EMERGENCY_TEMPLATE.new_fact() emergency["id"] = emergency_count emergency["type"] = Symbol(request.form.get("emergencytype")) emergency["location"] = [ float(request.form.get("locx")), float(request.form.get("locy")) ] emergency["n_affected_people"] = int(request.form.get("affected")) emergency.assertit() if emergency.asserted: emergency_count += 1 env.run() data = {"id": emergency['id'], "type": emergency['type']} logs = get_current_responses() response = Success(callback=data, logs=logs, message="Emergency added successfully") json_data = json.dumps(response.__dict__, default=lambda o: o.__dict__, indent=4) return json_data response = Error(None, None, "Emergency could not be added. Try again later.") json_data = json.dumps(response.__dict__, default=lambda o: o.__dict__, indent=4) return json_data
def add_service(): global service_count service = SERVICE_TEMPLATE.new_fact() service["id"] = service_count service["name"] = Symbol(request.form.get("servicename")) service["location"] = [ float(request.form.get("locx")), float(request.form.get("locy")) ] service["n_members"] = int(request.form.get("members")) service["movement_speed"] = float(request.form.get("speed")) service["prep_time"] = float(request.form.get("preptime")) service.assertit() if service.asserted: service_count += 1 env.run() data = {"id": service['id'], "name": service['name']} logs = get_current_responses() response = Success(callback=data, logs=logs, message="Emergency added successfully") json_data = json.dumps(response.__dict__, default=lambda o: o.__dict__, indent=4) return json_data response = Error(None, None, "Service could not be added. Try again later.") json_data = json.dumps(response.__dict__, default=lambda o: o.__dict__, indent=4) return json_data
def test_make_instance_errors(self): """Instance errors.""" defclass = self.env.find_class('ConcreteClass') with self.assertRaises(KeyError): defclass.make_instance('some-instance', NonExistingSlot=1) with self.assertRaises(TypeError): defclass.make_instance('some-instance', Slot="wrong type") with self.assertRaises(ValueError): defclass.make_instance('some-instance', Slot=Symbol('wrong-value'))
def test_slot(self): """Slot test.""" defclass = self.env.find_class('ConcreteClass') slot = tuple(defclass.slots())[0] self.assertFalse(slot.public) self.assertTrue(slot.writable) self.assertTrue(slot.accessible) self.assertTrue(slot.initializable) self.assertEqual(slot.name, 'Slot') self.assertEqual(slot.types, ('SYMBOL', )) self.assertEqual(slot.sources, (defclass.name, )) self.assertEqual(slot.range, Symbol('FALSE')) self.assertEqual(slot.facets, ('SGL', 'STC', 'INH', 'RW', 'LCL', 'RCT', 'EXC', 'PRV', 'RW', 'put-Slot')) self.assertEqual(slot.cardinality, ()) self.assertEqual(slot.default_value, Symbol('value')) self.assertEqual(slot.allowed_values, ('value', 'another-value')) self.assertEqual(tuple(slot.allowed_classes()), ())
def test_implied_fact(self): """ImpliedFacts are asserted.""" expected = (1, 2.3, '4', Symbol('five')) fact = self.env.assert_string('(implied-fact 1 2.3 "4" five)') self.assertEqual(fact[0], 1) self.assertEqual(len(fact), 4) self.assertEqual(fact.index, 1) self.assertEqual(tuple(fact), expected) self.assertEqual(str(fact), IMPL_STR) self.assertEqual(repr(fact), IMPL_RPR) self.assertTrue(fact in tuple(self.env.facts()))
def test_implied_fact(self): """ImpliedFacts are asserted.""" self.env.facts.assert_string('(implied-fact)') expected = [Symbol('implied-fact'), 1, 2.3, '4', Symbol('five')] template = self.env.facts.find_template('implied-fact') fact = template.new_fact() fact.append(1) fact.extend((2.3, '4', Symbol('five'))) fact.assertit() for asserted_fact in self.env.facts.facts(): if asserted_fact == fact: break self.assertEqual(asserted_fact[1], 1) self.assertEqual(len(asserted_fact), 5) self.assertEqual(asserted_fact.index, 2) self.assertEqual(list(asserted_fact), expected) self.assertEqual(str(asserted_fact), IMPL_STR) self.assertEqual(repr(asserted_fact), IMPL_RPR)
def test_function_call(self): """Test function call.""" function = self.env.find_function('function-sum') self.assertEqual(function(1, 2), 3) function = self.env.find_generic('generic-sum') self.assertEqual(function(1, 2), 3) self.assertEqual(self.env.call('function-sum', 1, 2), 3) self.assertEqual(self.env.call('generic-sum', 1, 2), 3) self.assertEqual( self.env.call('create$', 1, 2.0, "three", Symbol('four')), (1, 2.0, 'three', 'four'))
def test_instance(self): """Instance test.""" defclass = self.env.classes.find_class('ConcreteClass') defclass.new_instance('some-instance') instance = defclass.new_instance('test-instance') instance['Slot'] = Symbol('value') self.assertTrue(instance in defclass.instances()) self.assertEqual(instance.name, 'test-instance') self.assertEqual(instance.instance_class, defclass) self.assertEqual(instance['Slot'], Symbol('value')) self.assertEqual(str(instance), '[test-instance] of ConcreteClass (Slot value)') self.assertEqual( repr(instance), 'Instance: [test-instance] of ConcreteClass (Slot value)') instance.delete() instance = defclass.new_instance('test-instance') instance.unmake()
def test_template_fact_already_asserted(self): """Asserted TemplateFacts cannot be modified or re-asserted.""" template = self.env.find_template('template-fact') fact = template.new_fact() fact.update({'int': 1, 'float': 2.2, 'str': '4', 'symbol': Symbol('five'), 'multifield': [1, 2]}) fact.assertit() self.assertTrue(fact.asserted) with self.assertRaises(RuntimeError): fact['int'] = 42 with self.assertRaises(RuntimeError): fact.assertit()
def test_make_instance(self): """Instance test.""" defclass = self.env.find_class('ConcreteClass') instance_name = self.env.eval( '(make-instance test-name-instance of ConcreteClass)') self.assertEqual(instance_name, 'test-name-instance') self.assertTrue(isinstance(instance_name, InstanceName)) instance = defclass.make_instance() self.assertEqual(instance.name, 'gen1') instance = defclass.make_instance('test-instance', Slot=Symbol('value')) self.assertTrue(instance in defclass.instances()) self.assertEqual(instance.name, 'test-instance') self.assertEqual(instance.instance_class, defclass) self.assertEqual(instance['Slot'], Symbol('value')) self.assertEqual(str(instance), '[test-instance] of ConcreteClass (Slot value)') self.assertEqual( repr(instance), 'Instance: [test-instance] of ConcreteClass (Slot value)') self.assertEqual(dict(instance), {'Slot': Symbol('value')}) instance.delete() with self.assertRaises(LookupError): self.env.find_instance('test-instance') instance = defclass.make_instance('test-instance') instance.unmake() with self.assertRaises(LookupError): self.env.find_instance('test-instance')
def test_implied_fact_already_asserted(self): """Asserted ImpliedFacts cannot be modified or re-asserted.""" self.env.facts.assert_string('(implied-fact)') template = self.env.facts.find_template('implied-fact') fact = template.new_fact() fact.extend((1, 2.3, '4', Symbol('five'))) fact.assertit() self.assertTrue(fact.asserted) with self.assertRaises(RuntimeError): fact.append(42) with self.assertRaises(RuntimeError): fact.assertit()
def test_retract_fact(self): """Retracted fact is not anymore in the fact list.""" self.env.facts.assert_string('(implied-fact)') template = self.env.facts.find_template('implied-fact') fact = template.new_fact() fact.extend((1, 2.3, '4', Symbol('five'))) fact.assertit() self.assertTrue(fact.asserted) self.assertTrue(fact in list(self.env.facts.facts())) fact.retract() self.assertFalse(fact.asserted) self.assertFalse(fact in list(self.env.facts.facts()))
def test_template_fact(self): """TemplateFacts are asserted.""" expected = { 'int': 1, 'float': 2.2, 'str': '4', 'symbol': Symbol('five'), 'multifield': (1, 2) } template = self.env.find_template('template-fact') fact = template.assert_fact(**expected) self.assertEqual(len(fact), 5) self.assertEqual(fact.index, 1) self.assertEqual(fact['int'], 1) self.assertEqual(dict(fact), expected) self.assertEqual(str(fact), TMPL_STR) self.assertEqual(repr(fact), TMPL_RPR) self.assertTrue(fact in tuple(self.env.facts()))
def test_slot(self): """Slot test.""" defclass = self.env.find_class('ConcreteClass') slot = tuple(defclass.slots())[0] self.assertFalse(slot.public) self.assertTrue(slot.writable) self.assertTrue(slot.accessible) self.assertTrue(slot.initializable) self.assertEqual(slot.name, 'Slot') self.assertEqual(slot.types, ('FLOAT', 'INTEGER', 'SYMBOL', 'STRING', 'EXTERNAL-ADDRESS', 'FACT-ADDRESS', 'INSTANCE-ADDRESS', 'INSTANCE-NAME')) self.assertEqual(slot.sources, (defclass.name, )) self.assertEqual(slot.range, ('-oo', '+oo')) self.assertEqual(slot.facets, ('SGL', 'STC', 'INH', 'RW', 'LCL', 'RCT', 'EXC', 'PRV', 'RW', 'put-Slot')) self.assertEqual(slot.cardinality, ()) self.assertEqual(slot.default_value, Symbol('nil')) self.assertEqual(slot.allowed_values, ()) self.assertEqual(tuple(slot.allowed_classes()), ())