def resolve_value(self, val): if not self.pool: self.resolve() maps = {deformat_constant_key(key): self.pool[key]['value'] for key in self.pool} return ConstantTemplate(val).resolve_data(maps)
def targets_meet_condition(self, data): targets = [] for condition in self.conditions: deformatted_data = { deformat_constant_key(key): value for key, value in data.items() } try: resolved_evaluate = ConstantTemplate( condition.evaluate).resolve_data(deformatted_data) result = BoolRule(resolved_evaluate).test(data) except Exception as e: raise EvaluationException( 'evaluate[%s] fail with data[%s] message: %s' % (condition.evaluate, json.dumps(deformatted_data), e.message)) if result: targets.append(condition.sequence_flow.target) if not targets: raise ConditionExhaustedException( 'all conditions of branches are False') return targets
def resolve(self): if self.pool: return refs = self.get_reference_info() nodes = refs.keys() flows = [] for node in nodes: for ref in refs[node]: if ref in nodes: flows.append([node, ref]) graph = Graph(nodes, flows) # circle reference check trace = graph.get_cycle() if trace: raise ConstantReferenceException( 'Exist circle reference between constants: %s' % '->'.join(trace)) # resolve the constants reference pool = {} temp_pool = copy.deepcopy(self.raw_pool) # get those constants which are referenced only(not refer other constants) referenced_only = ConstantPool._get_referenced_only(temp_pool) while temp_pool: for ref in referenced_only: value = temp_pool[ref]['value'] # resolve those constants which reference the 'ref' for key, info in temp_pool.iteritems(): maps = {deformat_constant_key(ref): value} temp_pool[key]['value'] = ConstantTemplate( info['value']).resolve_data(maps) pool[ref] = temp_pool[ref] temp_pool.pop(ref) referenced_only = ConstantPool._get_referenced_only(temp_pool) self.pool = pool
def _determine_next_flow_with_boolrule(self, data): """ 根据当前传入的数据判断下一个应该流向的 flow ( 不使用 eval 的版本) :param data: :return: """ for condition in self.conditions: deformatted_data = {deformat_constant_key(key): value for key, value in data.items()} try: resolved_evaluate = ConstantTemplate(condition.evaluate).resolve_data(deformatted_data) result = BoolRule(resolved_evaluate).test(data) except Exception as e: raise EvaluationException( 'evaluate[%s] fail with data[%s] message: %s' % ( condition.evaluate, json.dumps(deformatted_data), e.message ) ) if result: return condition.sequence_flow return None
def test_deformat_constant_key(self): self.assertEqual(deformat_constant_key('${a}'), 'a')