def test_operations_with_args(self): # create objects figvan = models.Obj(name='figvan') # set properties figvan.set_property(x=1, y=1) db.session.add(figvan) db.session.commit() # create operations move = models.Operation(name='move', formulas=[ 'subj.x = subj.x + step.x', 'subj.y = subj.y + step.y' ]) db.session.add(move) db.session.commit() # query from db figvan = models.Obj.query.get(1) kwargs = {'step.x': 1, 'step.y': 1} # perform operation # figvan.do_operation(move, **kwargs) queue.put([figvan, move, None, kwargs]) perform_operations() # query from db figvan = models.Obj.query.get(1) # assert assert figvan.x == 2 assert figvan.y == 2
def start(subj, operation, obj=None): subj = Obj.query.filter(Obj.name==subj).first() op = Operation.query.filter(Operation.name==operation).first() if obj is not None: obj = Obj.query.filter(Obj.name==obj).first() queue.put([subj, op, obj]) else: subj.do_operation(op) queue.put([subj, op]) interpret_situation() perform_operations() return 'Success'
def test_reaction(self): # чистим список событий, иначе будут мешать события из других тестов clear_event_list() # create objects figvan = models.Obj(name='figvan') troll = models.Obj(name='troll') # set properties kwargs = {'x': 1, 'y': 1, 'шаг': 1} figvan.set_property(**kwargs) db.session.add(figvan) db.session.commit() kwargs = {'x': 3, 'y': 3, 'шаг': 1} troll.set_property(**kwargs) db.session.add(troll) db.session.commit() # create operations move = models.Operation(name='move', formulas=[ "subj.x = subj.x + subj.gp('шаг')", "subj.y = subj.y + subj.gp('шаг')" ]) db.session.add(move) db.session.commit() # create interpretations obj_nearly = models.Interpretation(name="obj_nearly", conditions=[ "abs(subj.x - obj.x) <= 1", "abs(subj.y - obj.y) <= 1" ]) db.session.add(obj_nearly) db.session.commit() # create patterns escape = models.Pattern(name='escape', obj=troll, operation=move, interpretation=obj_nearly) db.session.add(escape) db.session.commit() # query from db figvan = models.Obj.query.get(1) # perform operation queue.put([figvan, move]) perform_operations() interpret_situation() perform_operations() # query from db figvan = models.Obj.query.get(1) troll = models.Obj.query.get(2) # assert assert figvan.x == 2 assert figvan.y == 2 assert troll.x == 4 assert troll.y == 4
def test_operations(self): # create objects figvan = models.Obj(name='figvan') troll = models.Obj(name='troll') # set properties figvan.set_property(energy=2) figvan.set_property(angry=1) figvan.set_property(health=5) figvan.set_property(power=5) db.session.add(figvan) db.session.commit() troll.set_property(health=20) db.session.add(troll) db.session.commit() # create operations hit = models.Operation(name='hit', formulas=[ 'obj.health = obj.health - (subj.energy * subj.power)', 'subj.energy = subj.energy - 1' ]) eat = models.Operation(name='eat', formulas=[ 'subj.health = subj.health + (subj.power * subj.angry)', 'obj.health = obj.health - (subj.power * subj.angry)' ]) db.session.add(hit) db.session.add(eat) db.session.commit() # query from db figvan = models.Obj.query.get(1) troll = models.Obj.query.get(2) # assert assert figvan.energy == 2 assert figvan.angry == 1 assert figvan.health == 5 assert figvan.power == 5 assert troll.health == 20 # create queue queue.put([figvan, hit, troll]) queue.put([figvan, eat, troll]) perform_operations() # query from db figvan = models.Obj.query.get(1) troll = models.Obj.query.get(2) # assert assert figvan.health == 10 assert figvan.energy == 1 assert troll.health == 5