def __eq__(self, aset): if aset is None: return False if isinstance(aset, SymbolicLambda): if not len(self.variable_list)==len(aset.variable_list): return False # may throw a DontKnowIfEqualException if not check_syntactically_equal(self.predicate, aset.predicate): return False # may throw a DontKnowIfEqualException if not check_syntactically_equal(self.expression, aset.expression): return False return True if isinstance(aset, SymbolicSet): return aset.__eq__(self) # impl. use of symbolicSet.__eq__. This is NOT this method if isinstance(aset, frozenset): if not self.explicit_set_computed: self.explicit_set_repr = self.enumerate_all() self.explicit_set_computed = True return aset == self.explicit_set_repr # FAIL! pp_self = pretty_print(self.node) pp_other = pretty_print(aset.node) string = "lambda compare not implemented %s = %s" % (pp_self, pp_other) print string raise DontKnowIfEqualException(string)
def generate_json_file(sequence_number: int, input_path: str, output_path: str, pretty_printed_fields: List[str]): """ Perform parsing of a single document and serialization of the resultant JSON, with optional pretty-printing. """ max_read = 64 * 1024 * 1024 with open(input_path, "r", encoding="utf8") as file: plain_text = file.read(max_read) if len(plain_text) == max_read: raise MemoryError("File is too large") result = parse(plain_text) output = json.dumps(result, indent=4, ensure_ascii=False) if len(pretty_printed_fields) != 0: if sequence_number != 1: print(end="\n" * 2) print(f"{sequence_number}.", os.path.splitext(os.path.basename(input_path))[0]) pretty_printer.pretty_print(result, pretty_printed_fields) with open(output_path, "w", encoding="utf8") as file: file.write(output)
def test_pp_sets4(self): #TODO: FIN(S) and FIN1(S) # Build AST string_to_file("#PREDICATE UNION (z).(z<42|x)=INTER (z).(z<42|x)", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # PPrint out = pretty_print(root.children[0]) assert out=="UNION (z).(z<42|x)=INTER (z).(z<42|x)"
def test_pp_sequences2(self): # Build AST string_to_file("#PREDICATE s^t=r & E->s=r & s<-E=r & [E]=[E,F]", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # PPrint out = pretty_print(root.children[0]) assert out=="s^t=r & E->s=r & s<-E=r & [E]=[E,F]"
def test_pp_functions2(self): # Build AST string_to_file("#PREDICATE %(z).(z<4|z)=f(E)", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # PPrint out = pretty_print(root.children[0]) assert out=="%(z).(z<4|z)=f(E)"
def test_pp_relations3(self): # Build AST string_to_file("#PREDICATE p><q=(p||q) & closure(r)=closure1(r) & prj1(S,T)=prj2(S,T)", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # PPrint out = pretty_print(root.children[0]) assert out=="p><q=(p||q) & closure(r)=closure1(r) & prj1(S,T)=prj2(S,T)"
def test_pp_relations(self): # Build AST string_to_file("#PREDICATE r:S<->T & dom(r)=ran(r) & r=(p;q) & r:id(S)", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # PPrint out = pretty_print(root.children[0]) assert out=="r:S<->T & dom(r)=ran(r) & r=(p;q) & r:id(S)"
def test_pp_relations2(self): # Build AST string_to_file("#PREDICATE S<|r=S<<|r & r|>T=r|>>T & r~=r1<+r2 & iterate(r,n)=r[S]", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # PPrint out = pretty_print(root.children[0]) assert out=="S<|r=S<<|r & r|>T=r|>>T & r~=r1<+r2 & iterate(r,n)=r[S]"
def test_pp_sets(self): # Build AST string_to_file("#PREDICATE E:S & E/:S & S<:T & S/<:T & S<<:T & S/<<:T", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # PPrint out = pretty_print(root.children[0]) assert out=="E:S & E/:S & S<:T & S/<:T & S<<:T & S/<<:T"
def test_pp_sets2(self): # Build AST string_to_file("#PREDICATE {}<:{a} & S\/T<:S/\T & S-T<:S*T & {a,b}<:{x|x<7}", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # PPrint out = pretty_print(root.children[0]) assert out=="{}<:{a} & S\/T<:S/\T & S-T<:S*T & {a,b}<:{x,|x<7}"
def test_pp_sets3(self): # Build AST string_to_file("#PREDICATE E|->F:POW(S) & {S}:POW1(T) & card(S)=42 & union(U)=inter(u)", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # PPrint out = pretty_print(root.children[0]) assert out=="E|->F:POW(S) & {S}:POW1(T) & card(S)=42 & union(U)=inter(u)"
def test_pp_numbers(self): # Build AST string_to_file("#PREDICATE 1+2<3 & 4-5>6 & 7*8>=9 & 10/11<=12 & 13 mod 14=15 & 1/=-1", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # PPrint out = pretty_print(root.children[0]) assert out=="1+2<3 & 4-5>6 & 7*8>=9 & 10/11<=12 & 13 mod 14=15 & 1/=-1"
def test_pp_functions(self): # Build AST string_to_file("#PREDICATE S+->T=S-->T & S>+>T=S>->T & S+->>T=S-->>T & S>->>T=S>+>>T", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # PPrint out = pretty_print(root.children[0]) assert out=="S+->T=S-->T & S>+>T=S>->T & S+->>T=S-->>T & S>->>T=S>+>>T"
def test_pp_miscellaneous3(self): # Build AST string_to_file("#PREDICATE RES=rec(Mark:14,Good_enough:TRUE) & xx=RES'Mark", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # PPrint out = pretty_print(root.children[0]) assert out=="RES=rec(Mark:14,Good_enough:TRUE) & xx=RES'Mark"
def test_pp_miscellaneous2(self): # Build AST string_to_file("#PREDICATE rel(f)=r & func(r)=f & bool(1<2)=TRUE & succ(s)=pred(t)", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # PPrint out = pretty_print(root.children[0]) assert out=="rel(f)=r & func(r)=f & bool(1<2)=TRUE & succ(s)=pred(t)"
def test_pp_sequences3(self): # Build AST string_to_file("#PREDICATE size(s)=42 & rev(s)=r & s/|\\n=s\|/n & last(s)=first(s)", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # PPrint out = pretty_print(root.children[0]) assert out=="size(s)=42 & rev(s)=r & s/|\\n=s\|/n & last(s)=first(s)"
def test_pp_sequences4(self): # Build AST string_to_file("#PREDICATE conc(ss)=r & front(s)=tail(s)", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # PPrint out = pretty_print(root.children[0]) assert out=="conc(ss)=r & front(s)=tail(s)"
def test_pp_miscellaneous1(self): # Build AST string_to_file("#PREDICATE MAXINT/=MIINT & TRUE:BOOL & FALSE/:STRING & x.y=5", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # PPrint out = pretty_print(root.children[0]) assert out=="MAXINT/=MIINT & TRUE:BOOL & FALSE/:STRING & x.y=5"
def test_pp_numbers2(self): # Build AST string_to_file("#PREDICATE !(z).(z<9 => not (1=1) or 1=2) <=> #(z).(z<8 & z=5)", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # PPrint out = pretty_print(root.children[0]) assert out =="!(z).(z<9 => not (1=1) or 1=2) <=> #(z).(z<8 & z=5)"
def test_pp_numbers3(self): # Build AST string_to_file("#PREDICATE NAT<:NAT1 & NATURAL<:NATURAL1 & INT<:INTEGER", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # PPrint out = pretty_print(root.children[0]) assert out =="NAT<:NAT1 & NATURAL<:NATURAL1 & INT<:INTEGER"
def test_pp_sequences(self): # Build AST string_to_file("#PREDICATE <>=[] & seq(S)=seq1(S) & iseq(S)=iseq1(S) & x:perm(S)", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # PPrint out = pretty_print(root.children[0]) assert out=="[]=[] & seq(S)=seq1(S) & iseq(S)=iseq1(S) & x:perm(S)" #[] : lose of information
def test_pp_numbers4(self): # Build AST string_to_file("#PREDICATE SIGMA(z).(z<4|z)=5 & PI(z).(z<0|z)=5", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # PPrint out = pretty_print(root.children[0]) assert out =="SIGMA(z).(z<4|z)=5 & PI(z).(z<0|z)=5"
def test_pp_miscellaneous4(self): # Build AST string_to_file("#PREDICATE RES:struct(Mark:NAT,Good_enough:BOOL)", file_name) ast_string = file_to_AST_str(file_name) print ast_string root = str_ast_to_python_ast(ast_string) # PPrint out = pretty_print(root.children[0]) assert out=="RES:struct(Mark:NAT,Good_enough:BOOL)"
def write_solution_nodes_to_env(self, root): for node in root.children: if isinstance(node, AConjunctPredicate): #loop self.write_solution_nodes_to_env(node) elif isinstance(node, AEqualPredicate): try: #TODO: utlb_srv_mrtk__var_e32 --> utlb_srv_mrtk.var_e32 (underscore bug) if isinstance(node.children[0], AIdentifierExpression): self.solutions[node.children[0].idName] = node.children[1] #print "DEBUG: used:",node.children[0].idName ,"=" + pretty_print(node) except Exception: if VERBOSE: print "WARNING: PyB failed to use solution: " + pretty_print(node) continue
def all_values_by_type_RPYTHON(atype, env, node): if PRINT_WARNINGS: print "\033[1m\033[91mWARNING\033[00m:",pretty_print(node), "caused brute force enumeration. MIN_INT:%s MAX_INT:%s" % (env._min_int, env._max_int) if isinstance(atype, IntegerType): L = [] for i in range(env._min_int, env._max_int+1): L.append(W_Integer(i)) return L elif isinstance(atype, BoolType): return [W_Boolean(True), W_Boolean(False)] elif isinstance(atype, StringType): # FIXME:(#ISSUE 21) only some strings are returned here L = [] for s in env.all_strings: L.append(W_String(s)) return L elif isinstance(atype, SetType): type_name = atype.name #print type_name #env.state_space.get_state().print_bstate() value = env.get_value(type_name) assert isinstance(value, frozenset) return value.to_list() elif isinstance(atype, PowerSetType): from enumeration_lazy import generate_powerset if PRINT_WARNINGS: print "\033[1m\033[91mWARNING\033[00m: (bruteforce) computing powerset of %s %s" % (iterable,name) val_list = all_values_by_type_RPYTHON(atype.data, env, node) card = len(val_list) powerlist = [frozenset([])] i = 0 while i!=card: for lst in generate_powerset(frozenset(val_list), card=i+1, skip=0): assert len(lst)==i+1 powerlist.append(frozenset(lst)) i = i+1 #print powerlist return powerlist elif isinstance(atype, CartType): val_domain = all_values_by_type_RPYTHON(atype.left.data, env, node) val_image = all_values_by_type_RPYTHON(atype.right.data, env, node) L = [] for x in val_domain: for y in val_image: L.append(W_Tuple((x,y))) return L string = "Unknown Type / Not Implemented: %s" % atype #print string raise Exception(string)
def compute_constrained_domains(predicate, env, varList): assert isinstance(predicate, Predicate) assert isinstance(varList, list) for idNode in varList: assert isinstance(idNode, AIdentifierExpression) # check which kind of strategy: 3 cases # case 1: Using the PyB constraint solver # check if a solution-set (variable domain) is computable without a external contraint solver if USE_PYB_CONSTRAINT_SOLVER: try: generator = _compute_using_pyB_solver(predicate, env, varList) for d in generator: yield d raise StopIteration() except SpecialCaseEnumerationFailedException: # print "\033[1m\033[91mDEBUG\033[00m: PyB constraint solver failed. Case not implemented" raise StopIteration() # case 2: Using a external constraint solver # TODO: Handle OverflowError, print Error message and go on try: if USE_RPYTHON_CODE: # Using external constraint solver not supported by RPython yet raise ImportError() if PRINT_WARNINGS: print "\033[1m\033[91mWARNING\033[00m: External constraint solver called. Caused by: %s" % pretty_print( predicate ) iterator = compute_using_external_solver(predicate, env, varList) # constraint solving succeed. for d in iterator: yield d raise StopIteration() # TODO: Remove this case when case 1 used brute force enum on default for unconstraint vars # case 3: Using brute force enumeration except (ConstraintNotImplementedException, ImportError): if PRINT_WARNINGS: print "\033[1m\033[91mWARNING\033[00m: Brute force enumeration caused by: %s! enumerating: %s" % ( pretty_print(predicate), [v.idName for v in varList], ) # TODO: maybe case 2 was able to constrain the domain of some variables but not all # this computation is thrown away in this step. This makes no senese. Fix it! # constraint solving failed, enumerate all values (may cause a pyB fail or a timeout) generator = gen_all_values(env, varList) for d in generator: yield d raise StopIteration()
def all_values_by_type(atype, env, node): if PRINT_WARNINGS: print "\033[1m\033[91mWARNING\033[00m:",pretty_print(node), "caused brute force enumeration. MIN_INT:%s MAX_INT:%s" % (env._min_int, env._max_int) if isinstance(atype, IntegerType): #print env._min_int, env._max_int return range(env._min_int, env._max_int+1) elif isinstance(atype, BoolType): return [True, False] elif isinstance(atype, StringType): # FIXME:(#ISSUE 21) only some strings are returned here return frozenset(env.all_strings) elif isinstance(atype, SetType): type_name = atype.name #print type_name #env.state_space.get_state().print_bstate() value = env.get_value(type_name) assert isinstance(value, frozenset) return value elif isinstance(atype, PowerSetType): val_list = all_values_by_type(atype.data, env, node) res = powerset(val_list, node.idName) powerlist = list(res) lst = [frozenset(e) for e in powerlist] #print lst return lst elif isinstance(atype, CartType): val_pi = all_values_by_type(atype.left.data, env, node) val_i = all_values_by_type(atype.right.data, env, node) # TODO: test for realtions, seams incomplete lst = frozenset([(x,y) for x in val_pi for y in val_i]) return lst elif isinstance(atype, StructType): value_dict = {} for name in atype.dictionary: rec_type = atype.dictionary[name] values = all_values_by_type(rec_type, env, node) value_dict[name]=values res = all_records(value_dict) lst = [] for dic in res: rec = [] for entry in dic: rec.append(tuple([entry,dic[entry]])) lst.append(frozenset(rec)) return frozenset(lst) string = "Unknown Type / Not Implemented: %s" % atype #print string raise Exception(string)
def check_syntactically_equal(predicate0, predicate1): if predicate0._same_class_(predicate1): try: length = range(len(predicate0.children)) except AttributeError: return True #clase check was successful and no more children to check for index in length: child0 = predicate0.children[index] child1 = predicate0.children[index] if not check_syntactically_equal(child0, child1): return False return True else: message = "ERROR: failed to check if predicates are equal: '%s' and '%s'" %(pretty_print(predicate0),pretty_print(predicate1)) print message raise DontKnowIfEqualException(message) return False
def __repr__(self): return "@symbolic quantified union " + pretty_print(self.node)
import pretty_printer import sys import lexer import parser #import code_generator #import interpreter filename = sys.argv[1] tokens = lexer.tokenize(filename) #print tokens goal = parser.parse(tokens) pretty_printer.pretty_print(goal) code_generator.generate(goal) assembler.assemble(asm_file) linker.link(o_file)
def __repr__(self): return "@symbolic set comprehension" + pretty_print(self.node)