def createAllLocalsCombinations(self, localDecls, exprArg, isDisjunct, isSymmetric):
     instances = exprArg.getInstances()
     my_range = list(instances.keys())
     integer_combinations = itertools.permutations(my_range, len(localDecls))
     
     localInstances = []
     ifConstraints = []
     
     
     for i in integer_combinations: 
         list_of_ints = list(i)
         set_of_ints = set(list_of_ints)
         if isDisjunct and (len(set_of_ints) != len(list_of_ints)):
             continue
         
         ifList = []
         locallocalInstances = []
         for (sort,index) in list_of_ints:
             localExprArg = ExprArg(instances={}, nonsupered=True)
             (e,p) = exprArg.getInstances()[(sort,index)]
             localExprArg.add((sort,index),(e,p))
             ifList.append(e)
             locallocalInstances.append(localExprArg)
         localInstances.append(locallocalInstances)
         ifConstraints.append(mAnd(*ifList))
         
     return (localInstances, ifConstraints)
Пример #2
0
    def createAllLocalsCombinations(self, localDecls, exprArg, isDisjunct,
                                    isSymmetric):
        instances = exprArg.getInstances()
        my_range = list(instances.keys())
        integer_combinations = itertools.permutations(my_range,
                                                      len(localDecls))

        localInstances = []
        ifConstraints = []

        for i in integer_combinations:
            list_of_ints = list(i)
            set_of_ints = set(list_of_ints)
            if isDisjunct and (len(set_of_ints) != len(list_of_ints)):
                continue

            ifList = []
            locallocalInstances = []
            for (sort, index) in list_of_ints:
                localExprArg = ExprArg(instances={}, nonsupered=True)
                (e, p) = exprArg.getInstances()[(sort, index)]
                localExprArg.add((sort, index), (e, p))
                ifList.append(e)
                locallocalInstances.append(localExprArg)
            localInstances.append(locallocalInstances)
            ifConstraints.append(mAnd(*ifList))

        return (localInstances, ifConstraints)
Пример #3
0
def joinWithParent(arg):
    instances = arg.getInstances(nonsupered=True)
    newInstances = {}
    for (sort, index) in instances.keys():
        (expr, pol) = instances[(sort, index)]
        if pol == Common.DEFINITELY_OFF:
            continue
        (lower, upper, _) = sort.instanceRanges[index]
        for i in range(lower, min(sort.parentInstances, upper + 1)):
            (old_expr, old_pol) = newInstances.get(
                (sort.parent, i),
                (SMTLib.SMT_BoolConst(False), Common.DEFINITELY_OFF))
            if pol == Common.DEFINITELY_ON and lower == upper:
                new_pol = Common.DEFINITELY_ON
                new_expr = SMTLib.SMT_BoolConst(True)
            else:
                new_pol = Common.aggregate_polarity(old_pol, Common.UNKNOWN)
                new_expr = mOr(
                    old_expr,
                    mAnd(
                        expr,
                        SMTLib.SMT_EQ(sort.instances[index],
                                      SMTLib.SMT_IntConst(i))))
            newInstances[(sort.parent, i)] = (new_expr, new_pol)
    return ExprArg(newInstances, nonsupered=True)
Пример #4
0
def joinWithClafer(left, right):
    newInstances = {}
    leftInstances = left.getInstances(nonsupered=True)
    rightInstances = right.getInstances(nonsupered=True)
    for (lsort, lindex) in leftInstances.keys():
        (lexpr, lpolarity) = leftInstances[(lsort, lindex)]
        if lpolarity == Common.DEFINITELY_OFF:
            continue
        for (rsort, rindex) in rightInstances.keys():
            (_rexpr, rpolarity) = rightInstances[(rsort, rindex)]
            if rpolarity == Common.DEFINITELY_OFF:
                continue
            noMatch = False
            while not (rsort in lsort.fields):
                if not lsort.superSort:
                    noMatch = True
                    break
                (lsort, lindex) = joinWithSuper(lsort, lindex)
            if noMatch:
                continue
            (lower, upper, _) = rsort.instanceRanges[rindex]

            (new_rexpr, new_rpol) = newInstances.get((rsort, rindex), ({}, {}))
            new_rpol[lindex] = lpolarity
            new_rexpr[lindex] = mAnd(
                lexpr,
                SMTLib.SMT_EQ(rsort.instances[rindex],
                              SMTLib.SMT_IntConst(lindex)))
            if lower <= lindex and lindex <= upper:
                newInstances[(rsort, rindex)] = (new_rexpr, new_rpol)
    newInstances = flattenInstances(newInstances)
    return ExprArg(newInstances, nonsupered=True)
Пример #5
0
def op_difference(left, right):
    '''
    :param left:
    :type left: :class:`~ExprArg`
    :param right:
    :type right: :class:`~ExprArg`
    :returns: :class:`~ExprArg` 
    
    Computes the set difference (left - - right)
    '''
    assert isinstance(left, ExprArg)
    assert isinstance(right, ExprArg)
    if left.getInts() or right.getInts():
        sys.exit("FIXME ints diff")
    matches = getSetInstancePairs(left, right)
    newInstances = {}
    for (sort, index) in matches.keys():
        key = (sort, index)
        ((lexpr, lpol), (rexpr, rpol)) = matches[(sort, index)]
        if rpol == Common.DEFINITELY_ON or lpol == Common.DEFINITELY_OFF:
            #cases (-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1)
            continue
        elif rpol == Common.DEFINITELY_OFF:
            #cases (0 , -1), (1, -1)
            newInstances[key] = (lexpr, lpol)
        else:
            #rpol is unknown, lpol is unknown or on => new_pol is UNKNOWN
            #cases (0, 0), (1, 0)
            #if right is not on, then left, else sort.isOff
            new_expr = SMTLib.SMT_If(SMTLib.createNot(rexpr), lexpr,
                                     sort.parentInstances)
            newInstances[key] = (new_expr, Common.UNKNOWN)
    return ExprArg(newInstances)
Пример #6
0
def op_intersection(left, right):
    '''
    :param left:
    :type left: :class:`~ExprArg`
    :param right:
    :type right: :class:`~ExprArg`
    :returns: :class:`~ExprArg` 
    
    Computes the set intersection (left & right)
    '''
    assert isinstance(left, ExprArg)
    assert isinstance(right, ExprArg)
    if left.getInts() or right.getInts():
        sys.exit("FIXME ints intersection")
    matches = getSetInstancePairs(left, right)
    newInstances = {}
    for (sort, index) in matches.keys():
        key = (sort, index)
        ((lexpr, lpol), (rexpr, rpol)) = matches[(sort, index)]
        if rpol == Common.DEFINITELY_OFF or lpol == Common.DEFINITELY_OFF:
            continue
        else:
            new_expr = mAnd(lexpr, rexpr)
            newInstances[key] = (new_expr,
                                 Common.aggregate_polarity(lpol, rpol))
    return ExprArg(newInstances)
Пример #7
0
def joinWithClaferRef(arg):
    instances = arg.getInstances(nonsupered=True)
    newArg = ExprArg(nonsupered=True)

    for (sort, index) in instances.keys():
        (expr, pol) = instances[(sort, index)]
        while not sort.refSort:
            (sort, index) = joinWithSuper(sort, index)
        if isinstance(sort.refSort, PrimitiveType):
            joinWithPrimitive(newArg, sort, index, expr, pol)
        else:
            for i in range(sort.refSort.numInstances):
                (prev_expr, _) = newArg.getInstances(nonsupered=True).get(
                    (sort.refSort, i),
                    (SMTLib.SMT_BoolConst(False), Common.DEFINITELY_OFF))
                newArg.getInstances(nonsupered=True)[(sort.refSort, i)] = (mOr(
                    prev_expr,
                    mAnd(
                        expr,
                        SMTLib.SMT_EQ(
                            sort.refs[index],
                            SMTLib.SMT_IntConst(i)))), Common.UNKNOWN)
    return newArg
Пример #8
0
def joinWithRef(arg):
    instances = arg.getInstances(nonsupered=True)
    newArg = ExprArg(nonsupered=True)

    for (sort, index) in instances:
        (expr, pol) = instances[(sort, index)]
        if pol == Common.DEFINITELY_OFF:
            continue
        else:
            if isinstance(sort.refSort, PrimitiveType):
                joinWithPrimitive(newArg, sort, index, expr, pol)
            else:
                #join on ref sort
                #needs to be more robust for multiple instanceSorts
                return joinWithClaferRef(arg)
    return newArg
Пример #9
0
 def claferidVisit(self, element):
     if (self.inConstraint):
         if element.id == "this":
             exprArgList = []
             for i in range(element.claferSort.numInstances):
                 exprArg = ExprArg(instances={}, nonsupered=True)
                 exprArg.addBasedOnPolarity(element.claferSort, i,
                                            element.claferSort.isOn(i))
                 exprArgList.append(exprArg)
             self.currentConstraint.addArg(exprArgList)
         elif element.id == "ref":
             self.currentConstraint.addArg([PrimitiveArg("ref")])
         elif element.id == "parent":
             self.currentConstraint.addArg([PrimitiveArg("parent")])
         elif element.claferSort:
             exprArg = ExprArg(instances={}, nonsupered=True)
             for i in range(element.claferSort.numInstances):
                 exprArg.addBasedOnPolarity(element.claferSort, i,
                                            element.claferSort.isOn(i))
             self.currentConstraint.addArg([exprArg])
         else:
             # localdecl case
             expr = self.currentConstraint.locals[element.id]
             self.currentConstraint.addArg(expr)
Пример #10
0
 def claferidVisit(self, element):
     if(self.inConstraint):
         if element.id == "this":
             exprArgList = []
             for i in range(element.claferSort.numInstances):
                 exprArg = ExprArg(instances={}, nonsupered=True)
                 exprArg.addBasedOnPolarity(element.claferSort, i, element.claferSort.isOn(i))
                 exprArgList.append(exprArg)
             self.currentConstraint.addArg(exprArgList)
         elif element.id == "ref":
             self.currentConstraint.addArg([PrimitiveArg("ref")])
         elif element.id == "parent":
             self.currentConstraint.addArg([PrimitiveArg("parent")])
         elif element.claferSort:  
             exprArg = ExprArg(instances={}, nonsupered=True)
             for i in range(element.claferSort.numInstances):
                 exprArg.addBasedOnPolarity(element.claferSort, i, element.claferSort.isOn(i))
             self.currentConstraint.addArg([exprArg])
         else:
             # localdecl case
             expr = self.currentConstraint.locals[element.id]
             self.currentConstraint.addArg(expr)
Пример #11
0
def op_eq(left, right, cacheJoins=False, bc=None):
    '''
    :param left:
    :type left: :class:`~ExprArg`
    :param right:
    :type right: :class:`~ExprArg`
    :returns: :class:`~BoolArg` 
    
    Ensures that the left = right.
    '''
    assert isinstance(left, ExprArg)
    assert isinstance(right, ExprArg)
    if cacheJoins and bc:
        #TODO CLEAN
        left_key = None
        right_key = None
        keys = []
        #asil allocation speedup, if both sides are sets, we can perform expression substitution in other constraints
        #bc is the bracketed constraint to put the cache

        for i in [left, right]:
            if isinstance(i, JoinArg):
                newkeys = Common.computeCacheKeys(i.flattenJoin())

                #print(tuple(key))
                keys = keys + newkeys
                #need to return all keys during the progress of join, add flag?
                #get the all keys
                all_keys = i.checkIfJoinIsComputed(nonsupered=True,
                                                   getAllKeys=True)
                #print(keys)
                #print(all_keys)
                keys = keys + all_keys
                #sys.exit()
                #print()
            #print("GGGG right" + str(right.__class__))
            #print(right.clafers)
        if len(left.clafers) != len(right.clafers):
            minJoinVal = left.clafers if len(left.clafers) < len(
                right.clafers) else right.clafers
            for i in keys:
                #TODO make more robust (e.g. if multiple equalities exist for the same join key, aggregate expressions
                bc.cache[i] = ExprArg(minJoinVal)
                #print(i)
                #print(minJoinVal)
                #print(str(len(minJoinVal)) + "  " + str(len(left.clafers)) + "  " + str(len(right.clafers)))
        #print(str(len(left.clafers)) + " " + str(len(right.clafers)))

    cond = []
    #int equality case
    lints = [(e, c) for (e, c) in left.getInts() if str(c) != "False"]
    rints = [(e, c) for (e, c) in right.getInts() if str(c) != "False"]
    if lints or rints:
        for (e, c) in lints:
            #exists r in R s.t. e == r
            expr = mOr(*[mAnd(rc, SMTLib.SMT_EQ(e, r)) for (r, rc) in rints])
            if str(c) != "True":
                expr = SMTLib.SMT_Implies(c, expr)
            cond.append(expr)
        for (e, c) in rints:
            #exists l in L s.t. e == l
            expr = mOr(*[mAnd(lc, SMTLib.SMT_EQ(e, l)) for (l, lc) in lints])
            if str(c) != "True":
                expr = SMTLib.SMT_Implies(c, expr)
            cond.append(expr)
    #clafer-set equality case
    matches = getSetInstancePairs(left, right)
    for ((lexpr, lpol), (rexpr, rpol)) in matches.values():
        if lpol == Common.DEFINITELY_OFF and rpol == Common.DEFINITELY_OFF:
            continue
        elif lpol == Common.DEFINITELY_OFF:
            cond.append(SMTLib.createNot(rexpr))
        elif rpol == Common.DEFINITELY_OFF:
            cond.append(SMTLib.createNot(lexpr))
        else:
            cond.append(SMTLib.SMT_Implies(lexpr, rexpr))
            cond.append(SMTLib.SMT_Implies(rexpr, lexpr))
    return BoolArg(mAnd(*cond))