Exemple #1
0
    def apply(self, args):
        f = self.evaluator.apply(args[0]).get_function_or_panic()
        C = self.evaluator.apply(args[1])
        C_m = []
        for e in C:
            if f.apply(e).bool_value():
                C_m.append(e)

        if isinstance(C, List):
            return List(C_m)
        else:
            return Set(C_m)
Exemple #2
0
 def apply(self, x):
     if not isinstance(x, Collection):
         return x
     return Set(self.evalDomain(x))
E = C.get_matching_entries(Variable("?M"), Variable("?T"),
                           Tuple([Symbolic("X"), Variable("_")]))

assert (len(E) == 5)

e = Entry(Symbolic("t"), Symbolic("a"), List([Integer(1), Symbolic("x")]))

C.set_entry(e, module=test_mod)

e_c = C.get_entry(Symbolic("a"))

assert (e == e_c)

e = C.get_entry(Symbolic("SR"))
t_c = Set([Symbolic("c"), Symbolic("d"), Symbolic("e")])
ref = e.get_value()
res = C.resolve_reference(ref)

assert (t_c == res)

req_mod = Symbolic("org.aiddl.test.example-module-req")

C.copy_entry(Symbolic("r"), test_mod, Symbolic("r_c"), req_mod)

e_1 = C.get_entry(Symbolic("r"), module=test_mod)
e_2 = C.get_entry(Symbolic("r_c"), module=req_mod)

assert (e_1.get_value() == e_2.get_value())

assert (C.get_working_module() == test_mod)
Exemple #4
0
def parse_string(s, aiddl_paths, freg):
    str_id = 0
    str_lookup = {}

    bs = False
    current_str_start = None
    slices = []
    s_new = ""
    for i in range(len(s)):
        c = s[i]
        if c == '"' and not bs and current_str_start is not None:
            str_lookup[str_id] = s[current_str_start:i]
            slices.append((current_str_start, i, str(str_id)))
            s_new += ' "%d"' % (str_id)
            current_str_start = None
            str_id += 1
        elif c == '"' and not bs:
            current_str_start = i + 1
        elif current_str_start is None:
            s_new += c

        if c == '\\':
            bs = True
        else:
            bs = False

    # for sl in slices:
    #     s = s[:sl[0]] + sl[2] + s[sl[1]:]

    s = s_new

    s = re.sub(r";;.*\n", "", s)
    s = s.replace("\n", " ")
    s = s.replace("\t", " ")
    for token in SPECIAL:
        s = s.replace(token, " " + token + " ")
    while "  " in s:
        s = s.replace("  ", " ")
    s = s.strip()
    stack = []
    module_name = None
    self_ref = None
    local_refs = {}
    tokens = s.split(" ")
    tuple_depth = 0
    for token in tokens:
        token = token.strip()
        if token == CLIST:
            assembled_list = []
            current = pop(stack)
            while current != OLIST:
                assembled_list.append(current)
                current = pop(stack)
            assembled_list.reverse()
            term = List(assembled_list)
            # push(stack, term)
        elif token == CTUPLE:
            tuple_depth -= 1
            assembled_list = []
            current = pop(stack)
            while current != OTUPLE:
                assembled_list.append(current)
                current = pop(stack)
            assembled_list.reverse()
            term = Tuple(assembled_list)
            # push(stack, term)
            if tuple_depth == 0:
                if term.get(0) == MOD:
                    self_ref = term.get(1)
                    module_name = term.get(2)
                elif term.get(0) == REQ or \
                     term.get(0) == NAMES or \
                     term.get(0) == NAMES_ALT:
                    local_refs[term.get(1)] = term.get(2)
                    if isinstance(term.get(2), String):
                        fname = term.get(2).get_string_value()
                        req_mod_n = get_mod_name_from_file(fname, aiddl_paths)
                        local_refs[term.get(1)] = req_mod_n
        elif token == CSET:
            assembled_set = []
            current = pop(stack)
            while current != OSET:
                assembled_set.append(current)
                current = pop(stack)
            term = Set(assembled_set)
        elif token in SPECIAL:
            push(stack, token)
            if token == OTUPLE:
                tuple_depth += 1
            continue
        else:
            term = None
            if token[0] == "?":
                term = Variable(name=token)
            elif token == "_":
                term = Variable()
            elif token[0] == '"':
                string = str_lookup[int(token[1:-1])]
                term = String(string)
            elif "/" in token:
                n = token.split("/")[0]
                d = token.split("/")[1]
                term = Rational(int(n), int(d))
            elif is_float(token):
                term = Real(float(token))
            elif is_int(token):
                term = Integer(int(token))
            elif is_bin(token):
                term = Integer(int(token[2:], 2))
            elif is_oct(token):
                term = Integer(int(token[2:], 8))
            elif is_hex(token):
                term = Integer(int(token[2:], 16))
            elif token in INFINITY:
                if "-" in token:
                    term = Infinity.neg()
                else:
                    term = Infinity.pos()
            else:
                term = Symbolic(token)
        if len(stack) > 0:
            back_resolve = True
            while back_resolve and len(stack) > 0:
                back_resolve = False
                if peek(stack) == SREF:
                    pop(stack)
                    term = Reference(term, module_name)
                    back_resolve = True
                elif peek(stack) == REF:
                    pop(stack)
                    name = pop(stack)
                    if not isinstance(name, FunctionReference):
                        if term == self_ref:
                            # print("Pointing to %s in module %s (aka %s)"
                            #       % (str(name), str(module_name), str(term)))
                            term = Reference(name, module_name, alias=term)
                        else:
                            # print("Pointing to %s in module %s (aka %s)"
                            #       % (str(name), str(module_name), str(term)))
                            term = Reference(name,
                                             local_refs[term],
                                             alias=term)
                    else:
                        bad_fref = name.get_fref()
                        if term == self_ref:
                            term = FunctionReference(bad_fref,
                                                     freg,
                                                     module=module_name)
                        else:
                            term = FunctionReference(bad_fref,
                                                     freg,
                                                     module=module_name)
                    back_resolve = True
                elif peek(stack) == FREF:
                    pop(stack)
                    term = FunctionReference(term, freg)
                    back_resolve = True
                elif peek(stack) == ASSOC:
                    pop(stack)
                    key = pop(stack)
                    if isinstance(key, KeyValue):
                        term = KeyValue(key._key, KeyValue(key._value, term))
                    else:
                        term = KeyValue(key, term)
                    back_resolve = True
        push(stack, term)
    return (stack, module_name, self_ref, local_refs)
Exemple #5
0
 def apply(self, x: Collection) -> Set:
     U = set()
     for s in x:
         for e in s:
             U.add(e)
     return Set(U)
Exemple #6
0
    def apply(self, arg):
        # if not self.evaluatable(arg):
        #     return arg
        # if arg in self.cache.keys():
        #     return self.cache[arg]

        if self.verbose:
            print(self.log_indent + str(arg))
            self.log_indent += "  "

        operator = None

        if not isinstance(arg, Tuple) or len(arg) == 0:
            if isinstance(arg, Collection):
                newCol = []
                for t in arg:
                    newCol.append(self.apply(t))

                if isinstance(arg, List):
                    result = List(newCol)
                else:
                    result = Set(newCol)
            elif isinstance(arg, KeyValue):
                result = KeyValue(self.apply(arg.get_key()),
                                  self.apply(arg.get_value()))
            elif isinstance(arg, Reference):
                #print("-----", arg, self.follow_references)
                if self.follow_references:
                    result = self.apply(self.db.resolve_reference(arg))
                else:
                    result = self.db.resolve_reference(arg)
                #print(arg, "resolved to", result)
            else:
                result = arg
        else:
            operator = arg.get(0)
            if isinstance(operator, Reference):
                target = operator.get_ref_target()
                if isinstance(target, Symbolic):
                    uri = operator.convert2uri()
                    if self.freg.has_function(uri):
                        operator = uri
                    else:
                        operator = self.db.resolve_reference(operator)
                else:
                    operator = self.db.resolve_reference(operator)

                    # if self.verbose:
            #     tail = str(operator)

            resolvedArguments = []
            processed_op = self.apply(operator)
            if not self.freg.has_function(processed_op):
                resolvedArguments.append(processed_op)
            for i in range(1, arg.size()):
                if operator not in self.delayedEval:
                    if isinstance(arg.get(i), Reference):
                        res_arg = self.db.resolve_reference(arg.get(i))
                    else:
                        res_arg = arg.get(i)

                    # if len(self.selfStack) > 0:
                    #     if operator != TYPE:
                    #         res_arg = res_arg.substitute(self.selfStack[-1])
                    #     elif i == 1:
                    #         res_arg = res_arg.substitute(self.selfStack[-1])

                    # if operator == TYPE and i == 2:
                    #     s = Substitution()
                    #     s.add(SELF, resolvedArguments[1])
                    #     res_arg = res_arg.substitute(s)
                    #     self.selfStack.append(s)
                    #     doPop = True

                    if operator not in self.delayedEval:
                        resolvedArguments.append(self.apply(res_arg))
                    else:
                        resolvedArguments.append(res_arg)
                else:
                    resolvedArguments.append(arg.get(i))
            if len(resolvedArguments) == 1:
                processedArgs = resolvedArguments[0]
            else:
                processedArgs = Tuple(resolvedArguments)
            if self.freg.has_function(operator):
                result = self.freg.get_function(operator).apply(processedArgs)
            else:
                result = processedArgs

        if self.verbose:
            if operator is None:
                operator = Symbolic("-")
            self.log_indent = self.log_indent[0:-2]
            print(self.log_indent + str(result) + "//" + str(operator))

        self.cache[arg] = result
        return result