Beispiel #1
0
def computeJoin(joinList, cfr=None, getAllKeys=False):
    #TODO CLEAN
    left = [joinList.pop(0)]
    all_keys = [
    ]  #used for caching need to do at bottom of function, not checkkeys
    while joinList:
        (val, rindex) = checkKeys(left + joinList, cfr)
        if rindex != -1:
            left = [val]
            for _ in range(rindex - 1):
                joinList.pop(0)
        #left = joinList.pop(0)
        if not joinList:
            break
        right = joinList.pop(0)
        if isinstance(right, PrimitiveArg):
            if right.getValue() == "parent":
                left = [joinWithParent(left[0])]
            elif right.getValue() == "ref":
                left = [joinWithRef(left[0])]
        else:
            left = [joinWithClafer(left[0], right)]
        if getAllKeys:
            all_keys = all_keys + Common.computeCacheKeys(left + joinList)
    #print(all_keys)
    if getAllKeys:
        return (left[0], all_keys)
    else:
        return left[0]
Beispiel #2
0
def checkKeys(joinList, cfr=None):
    cfr = None
    if cfr:
        cache = cfr.join_cache
        if len(joinList) > 4:
            print(joinList)
        for i in range(len(joinList), 0, -1):
            currList = joinList[0:i]
            keys = Common.computeCacheKeys(currList)
            #
            #print(keys)
            for k in keys:
                val = cache.get(k, False)
                if val:
                    #print("HIT")
                    #print(k)
                    #print(val)
                    #TODO CANNOT JUST RETURN, REMOVE FIRST I KEYS, AND DO BELOW
                    #FIGURE OUT THE != case
                    return (val, i)
    return (None, -1)
Beispiel #3
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))
Beispiel #4
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))