def __getitem__(self, args): assert len(self.variable_list)>1 # 1. map args to var-nodes arg_list = remove_tuples(args) self.env.push_new_frame(self.variable_list) unset = len(self.variable_list) for i in range(len(arg_list)): idNode = self.variable_list[i] atype = self.env.get_type_by_node(idNode) value = build_arg_by_type(atype, arg_list) self.env.set_value(idNode.idName, value) unset = unset -1 # TODO: 2. compute missing bound variables # only implemented one case: {P(x,y)}(x) returning y if not unset==1: print unset, "unset bound variables:", [x.idName for x in self.variable_list[unset:]] raise NotImplementedError() from enumeration import try_all_values result = None for possible in try_all_values(self.predicate, self.env, self.variable_list[unset:]): if possible: result = self.env.get_value(self.variable_list[-1].idName) self.env.pop_frame() # exit scope return result
def __contains__(self, args): args = remove_tuples(args) varList = self.variable_list self.env.push_new_frame(varList) #print len(varList), varList for i in range(len(varList)): idNode = varList[i] # type needed to map args to value atype = self.env.get_type_by_node(idNode) value = build_arg_by_type(atype, args) self.env.set_value(idNode.idName, value) result = self.interpret(self.predicate, self.env) self.env.pop_frame() # exit scope if USE_RPYTHON_CODE: return result.bvalue return result
def __contains__(self, args): args = remove_tuples(args) varList = self.variable_list self.env.push_new_frame(varList) for i in range(len(varList)): idNode = varList[i] atype = self.env.get_type_by_node(idNode) value = build_arg_by_type(atype, args) self.env.set_value(idNode.idName, value) domain_value = self.interpret(self.predicate, self.env) image_value = self.interpret(self.expression, self.env) self.env.pop_frame() # exit scope if USE_RPYTHON_CODE: return domain_value.bvalue and image_value.__eq__(args[-1]) else: return domain_value and image_value==args[-1]
def _revise(node, env, var_node): if USE_RPYTHON_CODE: from rpython_interp import interpret else: from interp import interpret if isinstance(node, AEqualPredicate): # if elements of the set are equal to some expression, a set has to be generated # e.g {x| x:NAT & x=42} results in {42} if isinstance(node.children[0], AIdentifierExpression) and node.children[0].idName == var_node.idName: value = interpret(node.children[1], env) # FIXME: TypeError: unhashable instance e.g. run C578.EML.014/CF_CV_1 # somehow a set type is returned! if isinstance(value, SymbolicSet): return frozenset([value.enumerate_all()]) return frozenset([value]) if isinstance(node.children[1], AIdentifierExpression) and node.children[1].idName == var_node.idName: value = interpret(node.children[0], env) if isinstance(value, SymbolicSet): return frozenset([value.enumerate_all()]) return frozenset([value]) if isinstance(node.children[0], ACoupleExpression): # 2.1 search n-tuple for matching var (to be constraint) element_list = couple_tree_to_conj_list(node.children[0]) index = 0 match = False for e in element_list: if isinstance(e, AIdentifierExpression) and e.idName == var_node.idName: match = True break index = index + 1 # 2.2. compute set if match found if match: # FIXME: no caching! Recomputation at every call aset = interpret(node.children[1], env) # print # assert isinstance(set, frozenset) # 2.3. return correct part of the set corresponding to position of # searched variable inside the tuple on the left side return frozenset([remove_tuples(aset)[index]]) elif isinstance(node, AMemberPredicate): # belong-case 1: left side is just an id if isinstance(node.children[0], AIdentifierExpression) and node.children[0].idName == var_node.idName: # print var_node.idName, node.children[1] aset = interpret(node.children[1], env) # print "xxx", aset # e.g. x:{1,2,3} or x:S # return finite set on the left as test_set/constraint domain # FIXME: isinstance('x', frozenset) -> find enumeration order! if not isinstance(aset, frozenset): return aset.enumerate_all() # explicit domain needed return aset # belong-case 2: n-tuple on left side elif isinstance(node.children[0], ACoupleExpression): # e.g. (x,y){x|->y:{(1,2),(3,4)}...} (only one id-constraint found in this pass) # e.g. (x,y,z){x|->(y|->z):{(1,(2,3)),(4,(5,6))..} # TODO: Handle ((x|->y),(z|->a)):S # 2.1 search n-tuple for matching var (to be constraint) element_list = couple_tree_to_conj_list(node.children[0]) index = 0 match = False for e in element_list: if isinstance(e, AIdentifierExpression) and e.idName == var_node.idName: match = True break index = index + 1 # 2.2. compute set if match found if match: aset = interpret(node.children[1], env) # FIXME: C578.EML.014/R_PLACE_MAINTENANCE_2 returns SymbolicUnionSet if isinstance(aset, SymbolicSet): return frozenset([remove_tuples(t)[index] for t in aset.enumerate_all()]) assert isinstance(aset, frozenset) # 2.3. return correct part of the set corresponding to position of # searched variable inside the tuple on the left side return frozenset([remove_tuples(t)[index] for t in aset]) # this is only called because both branches (node.childern[0] and node.childern[1]) # of the disjunction are computable in finite time. (as analysed by _analyze_predicates) elif isinstance(node, ADisjunctPredicate): set0 = _revise(node.children[0], env, var_node) set1 = _revise(node.children[1], env, var_node) return set0.union(set1) elif isinstance(node, AConjunctPredicate): try: set0 = _revise(node.children[0], env, var_node) try: set1 = _revise(node.children[1], env, var_node) return set0.intersection(set1) except PredicateDoesNotMatchException: return set0 except PredicateDoesNotMatchException: set1 = _revise(node.children[1], env, var_node) return set1 else: raise PredicateDoesNotMatchException()