def determineResidualUtility(compromisedComponents,rD,debug=True):
    rMax = addRisksToLogic(rD)
    maxUtility = sumAllUtilities()
    print(("Maximum Utility with No Vulnerabilities: " + str(sumAllUtilities())))
    #query = "worstCasePathFromSource[SourceService,TotalC]"
    #utilitiesByAttackerCapability = pyDatalog.ask(query).answers
    combos = compromisedCombos(compromisedComponents)
    expectedValue = 0
    for combo,p in combos:
        print("Evaluating compromise combination:" + str(combo))
        for ccs in combo: #ccs is the component stripped out of the list
            #print "ccs:" + str(ccs)
            #Add in compromised components to logic
            #+ componentCompromisedWithAttributes('vpn',0.1,False,False,False)
            pyDatalog.assert_fact("componentCompromisedWithAttributes",str(ccs),str(p),"False","False","False")
        if debug:
            print("Probability for this combination: " + str(p))
            #query = "cToWithPrivileges(IntermediateService1,TargetService,VulnType,C)"
            #print("Connections in Graph:")
            #pprint.pprint(pyDatalog.ask(query).answers)
            #print("Probability for this combination: " + str(p))
            #query = "consumesPath(FunctionA,TargetService,Data,P)"
            #print("Consumption Paths in Graph:")
            #pprint.pprint(pyDatalog.ask(query).answers)
        expectedValue += determineResidualUtilityHelper(rD,maxUtility) * p
        for ccs in combo:
            #Remove compromised components from logic
            pyDatalog.retract_fact("componentCompromisedWithAttributes",str(ccs),str(p),"False","False","False")
    print("Final Expected Value:" + str(expectedValue))
    return expectedValue
示例#2
0
def retractAll(pname, arity):
  """
  retract all facts of given predicate name and arity from the LogKB.
  """
  vnames  = ["V" + str(x) for x in range(0,arity)]
  varlist = "(" + ",".join(vnames) + ")"
  query = pname + varlist
  ans = pyDatalog.ask(query)
  for r in ans.answers:
    pyDatalog.retract_fact(pname,*list(r))
示例#3
0
def retractAll(pname, arity):
    """
  retract all facts of given predicate name and arity from the LogKB.
  """
    vnames = ["V" + str(x) for x in range(0, arity)]
    varlist = "(" + ",".join(vnames) + ")"
    query = pname + varlist
    ans = pyDatalog.ask(query)
    for r in ans.answers:
        pyDatalog.retract_fact(pname, *list(r))
示例#4
0
 def retractPatient(self, name):
     Logic(self.first)
     #Método para olvidar el paciente, recupera sus datos de la base de conocimiento y lo elimina
     returnvalue = ''
     try:
         tq1 = "age(" + name + ",Y)"
         tq2 = "sick_of(" + name + ",Y)"
         q1 = pyDatalog.ask(tq1)
         q2 = pyDatalog.ask(tq2)
         pyDatalog.retract_fact('age', name, q1.answers[0][0])
         pyDatalog.retract_fact('sick_of', name, q2.answers[0][0])
         return 'retracted'
     except:
         returnvalue = "NOPATIENT"
     return returnvalue
示例#5
0
def eliminar(nombre):
    W = pyDatalog.ask("is_person('" + nombre + "')")

    if (str(W) == 'None'):
        print("Este nombre no lo conozco")
    else:
        pyDatalog.retract_fact('is_sport_person', nombre)
        pyDatalog.retract_fact('is_person', nombre)
        pyDatalog.retract_fact('is_exited', nombre, '3', '600')
        pyDatalog.retract_fact('has_interest_in_sports', nombre, '4', '800')
示例#6
0
    def retractpreferences(self):
        Logic(self.first)
        returnvalue = ''
        try:
            db = self.con.conexion()
            coleccion = db['sensor_temp']
            query = {"activo": "t"}
            req = list(coleccion.find(query))[0]

            tq1 = "prefer_temperature(" + req['enfermedad'] + "," + req[
                'stage'] + ",X)"
            q1 = pyDatalog.ask(tq1)

            pyDatalog.retract_fact('prefer_temperature', req['enfermedad'],
                                   req['stage'], q1.answers[0], [0])
            pyDatalog.assert_fact('prefer_temperature', req['enfermedad'],
                                  req['stage'], req['temp_pref'])
            return 'retracted'
        except:
            returnvalue = "NOPATIENT"
        return returnvalue
示例#7
0
def main():
    r = RedisManager(host=RedisConfig['host'], port=RedisConfig['port'], db=RedisConfig['db'],
                     password=RedisConfig['password'], decodedResponses=RedisConfig['decodedResponses'])
    sub = r.getRedisPubSub()
    sub.subscribe(RedisConfig['VocalChannel'])
    learn()
    while True:
        newMsg = sub.get_message()
        if newMsg:
            if newMsg['type'] == 'message':
                now = getTimestamp()
                msgContent = newMsg['data'].decode()
                print("Vocal Result: " + str(newMsg))  # Test
                facialRes = getAverageEmotionsFromRedisQueue(r, queue=RedisConfig['FacialQueue'],
                                                           emotions=DMAConfig['emotions'])
                if msgContent == str(RedisConfig['voidMsg']):
                    facialVocal = facialRes
                else:
                    vocalRes = ast.literal_eval(msgContent)
                    facialVocal = facialVocalCompare(facialRes, vocalRes, emotions=DMAConfig['emotions'],
                                                 facialW=DMAConfig['facialWeight'], vocalW=DMAConfig['vocalWeight'])
                print("FACIAL VOCAL:" + str(facialVocal))  # Test
                if facialVocal:
                    sortedEmotions = {k: v for k, v in sorted(facialVocal.items(), key=lambda item: item[1])}
                    print(sortedEmotions)  # Test
                    sortedEmoList = list(sortedEmotions.items())
                    firstEmotion = sortedEmoList[-1]
                    secondEmotion = sortedEmoList[-2]
                    topEmotions = dict([firstEmotion, secondEmotion])
                    decisionWithPer = firstEmotion
                    diff = 0
                    for k in topEmotions:
                        diff -= topEmotions[k]
                    diff = abs(diff)
                    attitude = getAverageAttitudeFromRedisQueue(r, queue=RedisConfig['PoseQueue'])
                    print("DATA: " + str(firstEmotion) + ", " + str(secondEmotion) + ", " + str(attitude))
                    if diff <= DMAConfig['poseTestThreshold']:
                        if attitude:
                            pyDatalog.assert_fact('firstEmoPercept', firstEmotion[0])
                            pyDatalog.assert_fact('secondEmoPercept', secondEmotion[0])
                            pyDatalog.assert_fact('poseAttitudePercept', attitude)
                            decision = str(pyDatalog.ask("takeDecision(D)")).replace("{('", "").replace("'", "").split(",")[0]
                            decisionWithPer = (decision, str(topEmotions[decision]))
                            pyDatalog.retract_fact('firstEmoPercept', firstEmotion[0])
                            pyDatalog.retract_fact('secondEmoPercept', secondEmotion[0])
                            pyDatalog.retract_fact('poseAttitudePercept', attitude)
                    r.setOnRedis(key=RedisConfig['DecisionSet'], value=str(decisionWithPer))
                    r.publishOnRedis(channel=RedisConfig['newDecisionPubSubChannel'], msg=str(decisionWithPer))
                    print("Decision: " + str(decisionWithPer))  # Test
                r.deleteRedisElemsByKeyPatternAndTimestamp(RedisConfig['imageHsetRoot'] + '*', now,
                                                           DMAConfig['timeThreshold'])
                r.deleteRedisElemsByKeyPatternAndTimestamp(RedisConfig['audioHsetRoot'] + '*', now,
                                                           DMAConfig['timeThreshold'])
示例#8
0
def determineResidualUtility(debug=False):
    rMax = riskDistribution.maxRisk()
    #Uncomment this after debugging
    #Time hog?
    #if debug:
    print "Calculating attack scenarios..."
    costsPlus = pyDatalog.ask("allAttackerPathsCostPlus(SourceService,TargetService,P,E,F,U,TotalC," + str(rMax) + ")")
    #if debug:
    print "Scenarios calculated."
    #if debug:
    #    print "Query: " + "allAttackerPathsCostPlus(SourceService,TargetService,P,E,F,U,TotalC," + str(rMax) + ")"
    #costsPlus = pyDatalog.ask("allAttackerPathsCostPlus(SourceService,TargetService,P,E,F,U,TotalC," + str(1) + ")")
    if costsPlus != None:
        costsPlusSorted = sorted(costsPlus.answers, key=itemgetter(5))
        if debug:
            print "No attack traces"
    else:
        costsPlusSorted = []
        if debug:
            print "Attack traces:"
            pprint.pprint(costsPlusSorted)

    #qFs = pyDatalog.ask("functionQuestionable(FuncName,Util)")
    #print("Questionable functions:")
    #print(qFs.answers)

    compromisedComponents = pyDatalog.ask("probCompromised(SourceService,Prob)")
    if compromisedComponents != None:
        #if debug:
        #    print "Compromised Components x:"
        #    print compromisedComponents.answers
        combos = getCombinations(compromisedComponents.answers,debug)
    else:
        #if debug:
        print "Nothing compromised"

    sumOverCombos = 0
    compromisedAllPossibleAnswer = pyDatalog.ask("compromised(SourceService)").answers
    compromisedAllPossible = [] #List of all possible compromised components
    if compromisedAllPossibleAnswer != None:
        for c in compromisedAllPossibleAnswer:
            compromisedAllPossible.append(c)

    #Loop over each possible combination of compromised components
    #print "Combos: " + str(combos)
    for compromiseCombo in combos:
        compromisedComponents = compromiseCombo[0]
        prob = compromiseCombo[1]
        if debug:
            print "Compromised Components:"
            print compromisedComponents
            print "Prob:"
            print prob


        #New Code to remove from database things that aren't compromised this iteration
        #There are probably better ways to do this for performance
        for cc in compromisedAllPossible:
            if cc[0] not in compromisedComponents:
                if debug:
                    print "Remove " + str(cc[0])
                pyDatalog.retract_fact("compromised",str(cc[0]))
        #Was this right to comment out?
        #costsPlus = pyDatalog.ask("allAttackerPathsCostPlus(SourceService,TargetService,P,E,F,U,TotalC," + str(rMax) + ")")
        #End New Code

        costsPlusSortedLimited = costsPlusSorted #These are just the attack traces with the currently compromised components
        #print costsPlusSorted
        #possibleConnections = itertools.ifilter(lambda connectionPair: connectionDoesNotExist(connectionPair),allPossibleConnections)
        #print CostPlusSorted[0][0]
        costsPlusSortedLimited = [trace for trace in costsPlusSorted if trace[0] in compromisedComponents]
        if debug:
            print "CostPlusSorted Length: "
            print len(costsPlusSorted)
            print len(costsPlusSortedLimited)
        #Find the worst case scenario for any given level of attacker capability
        riskUtilDict = {}
        if debug:
            fDict = {}
            #print "BROKEN CONNECTIONS:"
            #pprint.pprint(pyDatalog.ask("transitiveConnectionBroken(SourceService,TargetService)").answers)
            #print "RTU CONNECTIONS"
            #pprint.pprint(pyDatalog.ask("transitiveConnects('rtus',TargetService)").answers)
        downOrCompUtil = 0

        if False:
            downFunctions = pyDatalog.ask("functionDown(FunctionA,U)") # Required connections are down
            compromisedFunctions = pyDatalog.ask("functionCompromised(FunctionA,U)")
            if downFunctions != None:
                downUtils = downFunctions.answers
                #if debug:
                print "Down Utilities:"
                print downUtils
                #for [f,u] in downUtils:
                    #print u
                    #downUtil += u
            if compromisedFunctions != None:
                compromisedFs = compromisedFunctions.answers
                #if debug:
                print "Compromised Utilities:"
                print compromisedFs
        #Time hog?
        #if debug:
        print "Calculating Down and Compromised Functions"
        downOrCompromisedFunctions = pyDatalog.ask("functionDownOrCompromised(FunctionA,U)")
        #if debug:
        print "Down and Compromised Functions Calculated"
        if downOrCompromisedFunctions == None:
            if debug:
                print "Nothing Down or Compromised"
                fDict[0] = ""
            riskUtilDict[0] = 0
        else:
            downOrCompromisedUtils = downOrCompromisedFunctions.answers
            if debug:
                print "Down or Compromised Utilities:"
                print downOrCompromisedUtils
                #fDict[0] =
            for [f,u] in downOrCompromisedUtils:
                downOrCompUtil += u
            riskUtilDict[0] = downOrCompUtil


        for funcUtilRisk in costsPlusSortedLimited: #Limited to just ones starting from specific compromises
            #u is the questionable utility, not the residual utility
            if debug:
                f = funcUtilRisk[4]
            u = funcUtilRisk[5]
            r = funcUtilRisk[6]
            if r in riskUtilDict:
                #If this is a new worst case scenario for that level of attacker capability
                if u > riskUtilDict[r]:
                    riskUtilDict[r] = u
                    if debug:
                        fDict[r] = f
            #This is the first time seeing this level of risk
            else:
                riskUtilDict[r] = u
                if debug:
                    fDict[r] = f
        if debug:
            print "Worst case scenarios by attacker capability:"
            print riskUtilDict
            print fDict
            riskUtilDictAdjusted = {}

        sum = 0
        worstQuestionableU = downOrCompUtil #To track the worst case so far
        for r in range(0,maxRisk+1):
            if r in riskUtilDict:
                questionableU = riskUtilDict[r]
                if questionableU > worstQuestionableU:
                    worstQuestionableU = questionableU
            if debug:
                riskUtilDictAdjusted[r] = worstQuestionableU

            #Adjust the compromised components here to reflect the particular
            #combination under evaluation


            sum += (maxUtility - worstQuestionableU) * riskDistribution.probabilityOfRisk(r)
        #print("Residual utility: " + str(sum))
        if debug:
            print "Adjusted worst case scenarios:"
            print riskUtilDictAdjusted
            print "Sum: " + str(sum)
            print "Prob: " + str(prob)
        sumOverCombos += sum * prob

        #New code to put back in compromised components for next loop iteration
        for cc in compromisedAllPossible:
            if cc[0] not in compromisedComponents:
                if debug:
                    print "Add back " + str(cc[0])
                pyDatalog.assert_fact("compromised",str(cc[0]))
        #End new code
    if debug:
        print "Residual Utility:"
    #print sumOverCombos
    return sumOverCombos
indirect_manager('Sam', Y)
resultado = Y.data[1]
print("resultado", resultado)

# + parent(bill, 'John Adams')
pyDatalog.assert_fact('parent', 'bill', 'John Adams')
print(pyDatalog.ask('parent(bill,X)'))

# specify what an ancestor is
pyDatalog.load("""
    ancestor(X,Y) <= parent(X,Y)
    ancestor(X,Y) <= parent(X,Z) & ancestor(Z,Y)
""")

# prints a set with one element : the ('bill', 'John Adams') tuple
W = pyDatalog.ask('parent(bill,X)')
print(W.answers[0][0])
#
W = pyDatalog.ask('ancestor(bill,X)')
print('Bill es ancestro de:', W.answers[0][0])

# - parent(bill, 'John Adams')
pyDatalog.retract_fact('parent', 'bill', 'John Adams')

W = pyDatalog.ask('parent(bill,X)')
if (W.__str__() != 'None'):
    print(W.__str__())
else:
    print('No hay resultados')
def cutNetworkConnection(ServiceA, ServiceB,CProvided,IProvided,AProvided):
    pyDatalog.retract_fact("networkConnectsTo",ServiceA,ServiceB,CProvided,IProvided,AProvided)
示例#11
0
def test():

    # test of expressions
    pyDatalog.load("""
        + p(a) # p is a proposition
    """)
    assert pyDatalog.ask('p(a)') == set([('a',)])
    
    pyDatalog.assert_fact('p', 'a', 'b')
    assert pyDatalog.ask('p(a, "b")') == set([('a', 'b')])
    pyDatalog.retract_fact('p', 'a', 'b')
    assert pyDatalog.ask('p(a, "b")') == None
    
    """unary facts                                                            """
    
    @pyDatalog.program()
    def unary(): 
        +z()
        assert ask(z()) == set([()])
        
        + p(a) 
        # check that unary queries work
        assert ask(p(a)) == set([('a',)])
        assert ask(p(X)) == set([('a',)])
        assert ask(p(Y)) == set([('a',)])
        assert ask(p(_X)) == set([('a',)])
        assert ask(p(b)) == None
        assert ask(p(a) & p(b)) == None
        
        + p(b)
        assert ask(p(X), _fast=True) == set([('a',), ('b',)])
        
        + p(b) # facts are unique
        assert ask(p(X)) == set([('a',), ('b',)])
        
        - p(b) # retract a unary fact
        assert ask(p(X)) == set([('a',)])
        
        - p(a)
        assert ask(p(X)) == None
        + p(a)
        
        # strings and integers
        + p('c')
        assert ask(p(c)) == set([('c',)])
        
        + p(1)
        assert ask(p(1)) == set([(1,)])
        
        + n(None)
        assert ask(n(X)) == set([(None,)])
        assert ask(n(None)) == set([(None,)])
        
        # spaces and uppercase in strings
        + farmer('Moshe dayan')
        + farmer('omar')
        assert ask(farmer(X)) == set([('Moshe dayan',), ('omar',)])

    # execute queries in a python program
    moshe_is_a_farmer = pyDatalog.ask("farmer('Moshe dayan')")
    assert moshe_is_a_farmer == set([('Moshe dayan',)])

    """ binary facts                                                         """
    
    @pyDatalog.program()
    def binary(): 
        + q(a, b)
        assert ask(q(a, b)) == set([('a', 'b')])
        assert ask(q(X, b)) == set([('a', 'b')])
        assert ask(q(a, Y)) == set([('a', 'b')])
        assert ask(q(a, c)) == None
        assert ask(q(X, Y)) == set([('a', 'b')])
        
        + q(a,c)
        assert ask(q(a, Y)) == set([('a', 'b'), ('a', 'c')])
        
        - q(a,c)
        assert ask(q(a, Y)) == set([('a', 'b')])
        
        assert ask(q(X, X)) == None 
        +q(a, a)
        assert ask(q(X, X)) == set([('a', 'a')])
        -q(a, a) 
        
    """ (in)equality                                             """

    @pyDatalog.program()
    def equality():
        assert ask(X==1) == set([(1,)]) 
        assert ask(X==Y) == None
        assert ask(X==Y+1) == None
        assert ask((X==1) & (Y==1) & (X==Y)) == set([(1,1)])
        assert ask((X==1) & (Y==2) & (X==Y-1)) == set([(1,2)])
        #assert ask((X==1) & (Y==2) & (X+2==Y+1)) == set([(1,2)])
        assert ask((X==2) & (Y==X/2)) == set([(2,1)])
        assert ask((X==2) & (Y==X//2)) == set([(2,1)])
        
        assert ask((X==1) & (Y==1+X)) == set([(1,2)])
        assert ask((X==1) & (Y==1-X)) == set([(1,0)])
        assert ask((X==1) & (Y==2*X)) == set([(1,2)])
        assert ask((X==2) & (Y==2/X)) == set([(2,1)])
        assert ask((X==2) & (Y==2//X)) == set([(2,1)])
        
    """ Conjunctive queries                                             """

    @pyDatalog.program()
    def conjuctive(): 
        assert ask(q(X, Y) & p(X)) == set([('a', 'b')])

        assert ask(p(X) & p(a)) == set([('a',),('c',),(1,)])
        assert ask(p(X) & p(Y) & (X==Y)) == set([('a', 'a'), ('c', 'c'), (1, 1)])
        assert ask(p(X) & p(Y) & (X==Y) & (Y==a)) == set([('a', 'a')])

        assert ask(q(X, Y)) == set([('a', 'b')])
        assert ask(q(X, Y) & p(X)) == set([('a', 'b')])
    
    @pyDatalog.program()
    def equality2():
        assert ask((X==1) & (X<X+1)) == set([(1,)]) 
        assert ask((X==1) & (Y==X)) == set([(1,1)]) 
        assert ask((X==1) & (Y==X+1)) == set([(1,2)])
        assert ask((X==1) & (Y==X+1) & (X<Y)) == set([(1,2)])
        assert ask((X==1) & (X<1)) == None
        assert ask((X==1) & (X<=1)) == set([(1,)])
        assert ask((X==1) & (X>1)) == None
        assert ask((X==1) & (X>=1)) == set([(1,)])
#       assert ask(X==(1,2)) == set([((1,2), (1,2))])
        assert ask(X in (1,)) == set([(1,)])
        assert ask((X==1) & (X not in (2,))) == set([(1,)])
        assert ask((X==1) & ~(X in (2,))) == set([(1,)])
        assert ask((X==1) & (X not in (1,))) == None
        assert ask((X==1) & ~(X in (1,))) == None

    @pyDatalog.program()
    def equality3():
        # equality (must be between parenthesis):
        s(X) <= (X == a)
        assert ask(s(X)) == set([('a',)])
        s(X) <= (X == 1)
        assert ask(s(X)) == set([(1,), ('a',)])
        
        s(X, Y) <= p(X) & (X == Y)
        assert ask(s(a, a)) == set([('a', 'a')])
        assert ask(s(a, b)) == None
        assert ask(s(X,a)) == set([('a', 'a')])
        assert ask(s(X, Y)) == set([('a', 'a'),('c', 'c'),(1, 1)])

    assert pyDatalog.ask('p(a)') == set([('a',)])

    """ clauses                                                         """
    
    @pyDatalog.program()
    def clauses(): 
    
        p2(X) <= p(X)
        assert ask(p2(a)) == set([('a',)])
        p2(X) <= p(X)
        
        r(X, Y) <= p(X) & p(Y)
        assert ask(r(a, a)) == set([('a', 'a')])
        assert ask(r(a, c)) == set([('a', 'c')])
        r(X, b) <= p(X)
        assert ask(r(a, b)) == set([('a', 'b')])
        
        - (r(X, b) <= p(X))
        assert ask(r(a, b)) == None
        
        # TODO more tests

        # integer variable
        for i in range(10):
            + successor(i+1, i)
        assert ask(successor(2, 1)) == set([(2, 1)])
        
        # built-in
        assert abs(-3)==3
        assert math.sin(3)==math.sin(3)
        
    
    """ in                                                         """
    
    pyDatalog.assert_fact('is_list', (1,2))

    @pyDatalog.program()
    def _in(): 
        assert ((X==1) & (X in (1,2))) == [(1,)]
        _in(X) <= (X in [1,2])
        assert ask(_in(1)) == set([(1,)])
        assert ask(_in(9)) == None
        assert ask(_in(X)) == set([(1,), (2,)])
        
        _in2(X) <= is_list(Y) & (X in Y)
        assert ask(_in2(X)) == set([(1,), (2,)])

        assert ask((Y==(1,2)) & (X==1) & (X in Y)) == set([((1, 2), 1)])
        assert ask((Y==(1,2)) & (X==1) & (X in Y+(3,))) == set([((1, 2), 1)])
                
    """ recursion                                                         """
    
    @pyDatalog.program()
    def recursion(): 
        + even(0)
        even(N) <= successor(N, N1) & odd(N1)
        odd(N) <= ~ even(N)
        assert ask(even(0)) == set([(0,)])
        assert ask(even(X)) == set([(4,), (10,), (6,), (0,), (2,), (8,)])
        assert ask(even(10)) == set([(10,)])
        assert ask(odd(1)) == set([(1,)])
        assert ask(odd(5)) == set([(5,)])
        assert ask(even(5)) == None
    
    """ recursion with expressions                                         """
    # reset the engine
    pyDatalog.clear()
    @pyDatalog.program()
    def recursive_expression(): 
        
        predecessor(X,Y) <= (X==Y-1)
        assert ask(predecessor(X,11)) == set([(10, 11)])
        
        p(X,Z) <= (Y==Z-1) & (X==Y-1)
        assert ask(p(X,11)) == set([(9, 11)])
        
        # odd and even
        + even(0)
        even(N) <= (N > 0) & odd(N-1)
        assert ask(even(0)) == set([(0,)])
        odd(N) <= (N > 0) & ~ even(N)
        assert ask(even(0)) == set([(0,)])
        assert ask(odd(1)) == set([(1,)])
        assert ask(odd(5)) == set([(5,)])
        assert ask(even(5)) == None
        assert ask((X==3) & odd(X+2)) == set([(3,)])
        
    # Factorial
    pyDatalog.clear()
    @pyDatalog.program()
    def factorial(): 
#        (factorial[N] == F) <= (N < 1) & (F == -factorial[-N])
#        + (factorial[1]==1)
#        (factorial[N] == F) <= (N > 1) & (F == N*factorial[N-1])
#        assert ask(factorial[1] == F) == set([(1, 1)])
#        assert ask(factorial[4] == F) == set([(4, 24)])
#        assert ask(factorial[-4] == F) == set([(-4, -24)])
        pass
    
    # Fibonacci
    pyDatalog.clear()
    @pyDatalog.program()
    def fibonacci(): 
        (fibonacci[N] == F) <= (N == 0) & (F==0)
        (fibonacci[N] == F) <= (N == 1) & (F==1)
        (fibonacci[N] == F) <= (N > 1) & (F == fibonacci[N-1]+fibonacci[N-2])
        assert ask(fibonacci[1] == F) == set([(1, 1)])
        assert ask(fibonacci[4] == F) == set([(4, 3)])
        assert ask(fibonacci[18] == F) == set([(18, 2584)])

    # string manipulation
    @pyDatalog.program()
    def _lambda(): 
        split(X, Y,Z) <= (X == Y+'-'+Z)
        assert ask(split(X, 'a', 'b')) == set([('a-b', 'a', 'b')])
        split(X, Y,Z) <= (Y == (lambda X: X.split('-')[0])) & (Z == (lambda X: X.split('-')[1]))
        assert ask(split('a-b', Y, Z)) == set([('a-b', 'a', 'b')])
        assert ask(split(X, 'a', 'b')) == set([('a-b', 'a', 'b')])
        
        (two[X]==Z) <= (Z==X+(lambda X: X))
        assert ask(two['A']==Y) == set([('A','AA')])

    """ negation                                                     """    
    
    @pyDatalog.program()
    def _negation():
        +p(a, b)
        assert ask(~p(X, b)) == None
        assert ask(~p(X, c)) == set([('X', 'c')])

    pyDatalog.load("""
        + even(0)
        even(N) <= (N > 0) & (N1==N-1) & odd(N1)
        odd(N) <= (N2==N+2) & ~ even(N) & (N2>0)
    """)
    assert pyDatalog.ask('~ odd(7)', _fast=True) == None
    assert pyDatalog.ask('~ odd(2)', _fast=True) == set([(2,)])
    assert pyDatalog.ask('odd(3)', _fast=True) == set([(3,)])
    assert pyDatalog.ask('odd(3)'             ) == set([(3,)])
    assert pyDatalog.ask('odd(5)', _fast=True) == set([(5,)])
    assert pyDatalog.ask('odd(5)'            ) == set([(5,)])
    assert pyDatalog.ask('even(5)', _fast=True) == None
    assert pyDatalog.ask('even(5)'            ) == None
    
    """ functions                                                         """
    pyDatalog.clear()
    @pyDatalog.program()
    def function(): 
        + (f[a]==b)
        assert ask(f[X]==Y) == set([('a', 'b')])
        assert ask(f[X]==b) == set([('a', 'b')]) #TODO remove 'b' from result
        assert ask(f[a]==X) == set([('a', 'b')])
        assert ask(f[a]==b) == set([('a', 'b')])
    
        + (f[a]==c)
        assert ask(f[a]==X) == set([('a', 'c')])
        
        + (f[a]==a)
        assert ask(f[f[a]]==X) == set([('a',)])
        assert ask(f[X]==f[a]) == set([('a',)])
        assert ask(f[X]==f[a]+'') == set([('a',)])
        - (f[a]==a)
        assert ask(f[f[a]]==X) == None

        + (f[a]==None)
        assert (ask(f[a]==X)) == set([('a',None)])
        + (f[a]==(1,2))
        assert (ask(f[a]==X)) == set([('a',(1,2))])
        assert (ask(f[X]==(1,2))) == set([('a',(1,2))])

        + (f[a]==c)

        + (f2[a,x]==b)
        assert ask(f2[a,x]==b) == set([('a', 'x', 'b')])
    
        + (f2[a,x]==c)
        assert ask(f2[a,x]==X) == set([('a', 'x', 'c')])
        
        g[X] = f[X]+f[X]
        assert(ask(g[a]==X)) == set([('a', 'cc')])
        
        h(X,Y) <= (f[X]==Y)
        assert (ask(h(X,'c'))) == set([('a', 'c')])
        assert (ask(h(X,Y))) == set([('a', 'c')])
        
    @pyDatalog.program()
    def function_comparison(): 
        assert ask(f[X]==Y) == set([('a', 'c')])
        assert ask(f[a]<'d') == set([('c',)])
        assert ask(f[a]>'a') == set([('c',)])
        assert ask(f[a]>='c') == set([('c',)])
        assert ask(f[a]>'c') == None
        assert ask(f[a]<='c') == set([('c',)])
        assert ask(f[a]>'c') == None
        assert ask(f[a] in ['c',]) == set([('c',)])
        
        assert ask((f[X]=='c') & (f[Y]==f[X])) == set([('a', 'a')])
        assert ask((f[X]=='c') & (f[Y]==f[X]+'')) == set([('a', 'a')])
        assert ask((f[X]=='c') & (f[Y]==(lambda X : 'c'))) == set([('a', 'a')])

        assert ask(f[X]==Y+'') == None
        assert ask((Y=='c') &(f[X]==Y+'')) == set([('c', 'a')])
        assert ask((Y=='c') &(f[X]<=Y+'')) == set([('c', 'a')])
        assert ask((Y=='c') &(f[X]<Y+'')) == None
        assert ask((Y=='c') &(f[X]<'d'+Y+'')) == set([('c', 'a')])
        assert ask((Y==('a','c')) & (f[X] in Y)) == set([(('a', 'c'), 'a')])
        assert ask((Y==('a','c')) & (f[X] in (Y+('z',)))) == set([(('a', 'c'), 'a')])

        assert ask(f[X]==f[X]+'') == set([('a',)])

    @pyDatalog.program()
    def function_negation(): 
        assert not(ask(~(f[a]<'d'))) 
        assert not(ask(~(f[X]<'d'))) 
        assert ask(~(f[a] in('d',)))
        
    """ aggregates                                                         """
    
    pyDatalog.clear()
    @pyDatalog.program()
    def sum(): 
        + p(a, c, 1)
        + p(b, b, 4)
        + p(a, b, 1)

        assert(sum(1,2)) == 3
        (a_sum[X] == sum(Y, key=Z)) <= p(X, Z, Y)
        assert ask(a_sum[X]==Y) == set([('a', 2), ('b', 4)])
        assert ask(a_sum[a]==X) == set([('a', 2)])
        assert ask(a_sum[a]==2) == set([('a', 2)])
        assert ask(a_sum[X]==4) == set([('b', 4)])
        assert ask(a_sum[c]==X) == None
        assert ask((a_sum[X]==2) & (p(X, Z, Y))) == set([('a', 'c', 1), ('a', 'b', 1)])

        (a_sum2[X] == sum(Y, for_each=X)) <= p(X, Z, Y)
        assert ask(a_sum2[a]==X) == set([('a', 1)])

        (a_sum3[X] == sum(Y, key=(X,Z))) <= p(X, Z, Y)
        assert ask(a_sum3[X]==Y) == set([('a', 2), ('b', 4)])
        assert ask(a_sum3[a]==X) == set([('a', 2)])

    @pyDatalog.program()
    def len(): 
        assert(len((1,2))) == 2
        (a_len[X] == len(Z)) <= p(X, Z, Y)
        assert ask(a_len[X]==Y) == set([('a', 2), ('b', 1)])
        assert ask(a_len[a]==X) == set([('a', 2)])
        assert ask(a_len[X]==1) == set([('b', 1)])
        assert ask(a_len[X]==5) == None
        
        (a_lenY[X] == len(Y)) <= p(X, Z, Y)
        assert ask(a_lenY[a]==X) == set([('a', 1)])
        assert ask(a_lenY[c]==X) == None
        
        (a_len2[X,Y] == len(Z)) <= p(X, Y, Z)
        assert ask(a_len2[a,b]==X) == set([('a', 'b', 1)])
        assert ask(a_len2[a,X]==Y) == set([('a', 'b', 1), ('a', 'c', 1)])

        + q(a, c, 1)
        + q(a, b, 2)
        + q(b, b, 4)

    @pyDatalog.program()
    def concat(): 
        (a_concat[X] == concat(Y, key=Z, sep='+')) <= q(X, Y, Z)
        assert ask(a_concat[X]==Y) == set([('b', 'b'), ('a', 'c+b')])
        assert ask(a_concat[a]=='c+b') == set([('a', 'c+b')])
        assert ask(a_concat[a]==X) == set([('a', 'c+b')])
        assert ask(a_concat[X]==b) == set([('b', 'b')])

        (a_concat2[X] == concat(Y, order_by=(Z,), sep='+')) <= q(X, Y, Z)
        assert ask(a_concat2[a]==X) == set([('a', 'c+b')])

        (a_concat3[X] == concat(Y, key=(-Z,), sep='-')) <= q(X, Y, Z)
        assert ask(a_concat3[a]==X) == set([('a', 'b-c')])

    @pyDatalog.program()
    def min(): 
        assert min(1,2) == 1
        (a_min[X] == min(Y, key=Z)) <= q(X, Y, Z)
        assert ask(a_min[X]==Y) == set([('b', 'b'), ('a', 'c')])
        assert ask(a_min[a]=='c') == set([('a', 'c')])
        assert ask(a_min[a]==X) == set([('a', 'c')])
        assert ask(a_min[X]=='b') == set([('b', 'b')])
        
        (a_minD[X] == min(Y, order_by=-Z)) <= q(X, Y, Z)
        assert ask(a_minD[a]==X) == set([('a', 'b')])
        
        (a_min2[X, Y] == min(Z, key=(X,Y))) <= q(X, Y, Z)
        assert ask(a_min2[Y, b]==X) == set([('a', 'b', 2),('b', 'b', 4)])
        assert ask(a_min2[Y, Y]==X) == set([('b', 'b', 4)]), "a_min2"
        
        (a_min3[Y] == min(Z, key=(-X,Z))) <= q(X, Y, Z)
        assert ask(a_min3[b]==Y) == set([('b', 4)]), "a_min3"
        
    @pyDatalog.program()
    def max(): 
        assert max(1,2) == 2
        (a_max[X] == max(Y, key=-Z)) <= q(X, Y, Z)
        assert ask(a_max[a]==X) == set([('a', 'c')])
        
        (a_maxD[X] == max(Y, order_by=Z)) <= q(X, Y, Z)
        assert ask(a_maxD[a]==X) == set([('a', 'b')])

    @pyDatalog.program()
    def rank(): 
        (a_rank1[Z] == rank(for_each=Z, order_by=Z)) <= q(X, Y, Z)
        assert ask(a_rank1[X]==Y) == set([(1, 0), (2, 0), (4, 0)])
        assert ask(a_rank1[X]==0) == set([(1, 0), (2, 0), (4, 0)])
        assert ask(a_rank1[1]==X) == set([(1, 0)])
        assert ask(a_rank1[1]==0) == set([(1, 0)])
        assert ask(a_rank1[1]==1) == None

        # rank
        (a_rank[X,Y] == rank(for_each=(X,Y2), order_by=Z2)) <= q(X, Y, Z) & q(X,Y2,Z2)
        assert ask(a_rank[X,Y]==Z) == set([('a', 'b', 1), ('a', 'c', 0), ('b', 'b', 0)])
        assert ask(a_rank[a,b]==1) == set([('a', 'b', 1)])
        assert ask(a_rank[a,b]==Y) == set([('a', 'b', 1)])
        assert ask(a_rank[a,X]==0) == set([('a', 'c', 0)])
        assert ask(a_rank[a,X]==Y) == set([('a', 'b', 1), ('a', 'c', 0)])
        assert ask(a_rank[X,Y]==1) == set([('a', 'b', 1)])
        assert ask(a_rank[a,y]==Y) == None
        # reversed
        (b_rank[X,Y] == rank(for_each=(X,Y2), order_by=-Z2)) <= q(X, Y, Z) & q(X,Y2,Z2)
        assert ask(b_rank[X,Y]==Z) == set([('a', 'b', 0), ('a', 'c', 1), ('b', 'b', 0)])
        assert ask(b_rank[a,b]==0) == set([('a', 'b', 0)])
        assert ask(b_rank[a,b]==Y) == set([('a', 'b', 0)])
        assert ask(b_rank[a,X]==1) == set([('a', 'c', 1)])
        assert ask(b_rank[a,X]==Y) == set([('a', 'b', 0), ('a', 'c', 1)])
        assert ask(b_rank[X,Y]==0) == set([('a', 'b', 0), ('b', 'b', 0)])
        assert ask(b_rank[a,y]==Y) == None

    @pyDatalog.program()
    def running_sum(): 
        # running_sum
        (a_run_sum[X,Y] == running_sum(Z2, for_each=(X,Y2), order_by=Z2)) <= q(X, Y, Z) & q(X,Y2,Z2)
        assert ask(a_run_sum[X,Y]==Z) == set([('a', 'b', 3), ('a', 'c', 1), ('b', 'b', 4)])
        assert ask(a_run_sum[a,b]==3) == set([('a', 'b', 3)])
        assert ask(a_run_sum[a,b]==Y) == set([('a', 'b', 3)])
        assert ask(a_run_sum[a,X]==1) == set([('a', 'c', 1)])
        assert ask(a_run_sum[a,X]==Y) == set([('a', 'b', 3), ('a', 'c', 1)])
        assert ask(a_run_sum[X,Y]==4) == set([('b', 'b', 4)])
        assert ask(a_run_sum[a,y]==Y) == None

        (b_run_sum[X,Y] == running_sum(Z2, for_each=(X,Y2), order_by=-Z2)) <= q(X, Y, Z) & q(X,Y2,Z2)
        assert ask(b_run_sum[X,Y]==Z) == set([('a', 'b', 2), ('a', 'c', 3), ('b', 'b', 4)])
        assert ask(b_run_sum[a,b]==2) == set([('a', 'b', 2)])
        assert ask(b_run_sum[a,b]==Y) == set([('a', 'b', 2)])
        assert ask(b_run_sum[a,X]==3) == set([('a', 'c', 3)])
        assert ask(b_run_sum[a,X]==Y) == set([('a', 'b', 2), ('a', 'c', 3)])
        assert ask(b_run_sum[X,Y]==4) == set([('b', 'b', 4)])
        assert ask(b_run_sum[a,y]==Y) == None

    """ simple in-line queries                                        """
    X = pyDatalog.Variable()
    assert ((X==1) >= X) == 1
    assert ((X==1) & (X!=2) >= X) == 1
    assert set(X._in((1,2))) == set([(1,),(2,)])
    assert ((X==1) & (X._in ((1,2)))) == [(1,)]

    """ interface with python classes                                        """

    class A(pyDatalog.Mixin):
        def __init__(self, b):
            super(A, self).__init__()
            self.b = b
        def __repr__(self):
            return self.b
        @pyDatalog.program() # indicates that the following method contains pyDatalog clauses
        def _():
            (A.c[X]==N) <= (A.b[X]==N)
            (A.len[X]==len(N)) <= (A.b[X]==N)
        @classmethod
        def _pyD_x1(cls, X):
            if X.is_const() and X.id.b == 'za':
                yield (X.id,)
            else:
                for X in pyDatalog.metaMixin.__refs__[cls]:
                    if X.b == 'za':
                        yield (X,)
            
    a = A('a')
    b = A('b')
    assert a.c == 'a'
    X, Y = pyDatalog.variables(2)
    assert (A.c[X]=='a') == [(a,)]
    assert (A.c[X]=='a')[0] == (a,)
    assert list(X.data) == [a]
    assert X.v() == a
    assert ((A.c[a]==X) >= X) == 'a'
    assert ((A.c[a]==X) & (A.c[a]==X) >= X) == 'a'
    assert ((A.c[a]==X) & (A.c[b]==X) >= X) == None
    (A.c[X]=='b') & (A.b[X]=='a')
    assert list(X.data) == []
    (A.c[X]=='a') & (A.b[X]=='a')
    assert list(X.data) == [a]
    result = (A.c[X]=='a') & (A.b[X]=='a')
    assert result == [(a,)]
    assert (A.c[a] == 'a') == [()]
    assert (A.b[a] == 'a') == [()]
    assert (A.c[a]=='a') & (A.b[a]=='a') == [()]
    assert (A.b[a]=='f') == []
    assert ((A.c[a]=='a') & (A.b[a]=='f')) == []
    
    """ filters on python classes                                        """
    assert (A.b[X]!=Y) == [(a, None), (b, None)]
    assert (A.b[X]!='a') == [(b,)]
    assert (A.b[X]!='z') == [(a,), (b,)]
    assert (A.b[a]!='a') == []
    assert list(A.b[b]!='a') == [()]
    assert ((A.b[b]!='a') & (A.b[b]!='z')) == [()]

    assert (A.b[X]<Y) == [(a, None), (b, None)]
    assert (A.b[X]<'a') == []
    assert (A.b[X]<'z') == [(a,), (b,)]
    assert (A.b[a]<'b') == [()]
    assert (A.b[b]<'a') == []
    assert ((A.b[b]<'z') & (A.b[b]!='z')) == [()]

    assert (A.b[X]<='a') == [(a,)]
    assert (A.b[X]<='z') == [(a,), (b,)]
    assert (A.b[a]<='b') == [()]
    assert (A.b[b]<='a') == []
    assert ((A.b[b]<='z') & (A.b[b]!='z')) == [()]

    assert (A.b[X]>'a') == [(b,)]
    assert (A.b[X]>='a') == [(a,), (b,)]

    assert (A.c[X]<='a') == [(a,)]
    assert (A.c[X]<='a'+'') == [(a,)]

    assert (A.c[X]._in(('a',))) == [(a,)]
    assert (A.c[X]._in(('a',)+('z',))) == [(a,)]
    assert ((Y==('a',)) & (A.c[X]._in(Y))) == [(('a',), a)] # TODO make ' in ' work
    
    assert ((Y==('a',)) & (A.c[X]._in(Y+('z',)))) == [(('a',), a)] # TODO make ' in ' work
    assert (A.c[X]._in(('z',))) == []

    # more complex queries
    assert ((Y=='a') & (A.b[X]!=Y)) == [('a', b)] # order of appearance of the variables !
    
    assert (A.len[X]==Y) == [(b, 1), (a, 1)]
    assert (A.len[a]==Y) == [(1,)]

    """ subclass                                              """

    class Z(A):
        def __init__(self, z):
            super(Z, self).__init__(z+'a')
            self.z = z
        def __repr__(self):
            return self.z
        @pyDatalog.program() # indicates that the following method contains pyDatalog clauses
        def _():
            (Z.w[X]==N) <= (Z.z[X]!=N)
        @classmethod
        def _pyD_query(cls, pred_name, args):
            if pred_name == 'Z.pred':
                if args[0].is_const() and args[0].id.b != 'za':
                    yield (args[0].id,)
                else:
                    for X in pyDatalog.metaMixin.__refs__[cls]:
                        if X.b != 'za':
                            yield (X,)
            else:
                raise AttributeError
    
    z = Z('z')
    assert z.z == 'z'
    assert (Z.z[X]=='z') == [(z,)]
    assert ((Z.z[X]=='z') & (Z.z[X]>'a')) == [(z,)]
    assert list(X.data) == [z]
    try:
        a.z == 'z'
    except Exception as e:
        e_message = e.message if hasattr(e, 'message') else e.args[0]
        if e_message != "Predicate without definition (or error in resolver): A.z[1]==/2":
            print(e_message)
    else:
        assert False
    
    try:
        (Z.z[a] == 'z') == None
    except Exception as e:
        e_message = e.message if hasattr(e, 'message') else e.args[0]
        if e_message != "Object is incompatible with the class that is queried.":
            print(e_message)
    else:
        assert False

    assert (Z.b[X]==Y) == [(z, 'za')]
    assert (Z.c[X]==Y) == [(z, 'za')]
    assert ((Z.c[X]==Y) & (Z.c[X]>'a')) == [(z, 'za')]
    assert (Z.c[X]>'a') == [(z,)]
    assert ((Z.c[X]>'a') & (A.c[X]=='za')) == [(z,)]
    assert (A.c[X]=='za') == [(z,)]
    assert (A.c[z]=='za') == [()]
    assert (z.b) == 'za'
    assert (z.c) == 'za'
    
    w = Z('w')
    w = Z('w') # duplicated to test __refs__[cls]
    assert(Z.x(X)) == [(z,)]
    assert not (~Z.x(z))
    assert ~Z.x(w)
    assert ~ (Z.z[w]=='z')
    assert(Z.pred(X)) == [(w,)] # not duplicated !
    assert(Z.pred(X) & ~ (Z.z[X]>='z')) == [(w,)]
    assert(Z.x(X) & ~(Z.pred(X))) == [(z,)]

    assert (Z.len[X]==Y) == [(w, 1), (z, 1)]
    assert (Z.len[z]==Y) == [(1,)]
    
    # TODO print (A.b[w]==Y)
            
    """ python resolvers                                              """
    
    @pyDatalog.predicate()
    def p(X,Y):
        yield (1,2)
        yield (2,3)
    
    assert pyDatalog.ask('p(X,Y)') == set([(1, 2), (2, 3)])
    assert pyDatalog.ask('p(1,Y)') == set([(1, 2)])
    assert pyDatalog.ask('p(1,2)') == set([(1, 2)])
    
    """ error detection                                              """
    
    @pyDatalog.program()
    def _(): 
        pass
    error = False
    try:
        _()
    except: error = True
    assert error

    def assert_error(code, message='^$'):
        _error = False
        try:
            pyDatalog.load(code)
        except Exception as e:
            e_message = e.message if hasattr(e, 'message') else e.args[0] # python 2 and 3
            if not re.match(message, e_message):
                print(e_message) 
            _error = True
        assert _error
        
    def assert_ask(code, message='^$'):
        _error = False
        try:
            pyDatalog.ask(code)
        except Exception as e: 
            e_message = e.message if hasattr(e, 'message') else e.args[0]
            if not re.match(message, e_message):
                print(e_message) 
            _error = True
        assert _error
        
    assert_error('ask(z(a),True)', 'Too many arguments for ask \!')
    assert_error('ask(z(a))', 'Predicate without definition \(or error in resolver\): z/1')
    assert_error("+ farmer(farmer(moshe))", "Syntax error: Literals cannot have a literal as argument : farmer\[\]")
    assert_error("+ manager[Mary]==John", "Left-hand side of equality must be a symbol or function, not an expression.")
    assert_error("manager[X]==Y <= (X==Y)", "Syntax error: please verify parenthesis around \(in\)equalities")
    assert_error("p(X) <= (Y==2)", "Can't create clause")
    assert_error("p(X) <= X==1 & X==2", "Syntax error: please verify parenthesis around \(in\)equalities")
    assert_error("p(X) <= (manager[X]== min(X))", "Error: argument missing in aggregate")
    assert_error("p(X) <= (manager[X]== max(X, order_by=X))", "Aggregation cannot appear in the body of a clause")
    assert_error("q(min(X, order_by=X)) <= p(X)", "Syntax error: Incorrect use of aggregation\.")
    assert_error("manager[X]== min(X, order_by=X) <= manager(X)", "Syntax error: please verify parenthesis around \(in\)equalities")
    assert_error("(manager[X]== min(X, order_by=X+2)) <= manager(X)", "order_by argument of aggregate must be variable\(s\), not expression\(s\).")
    assert_error("ask(X<1)", 'Error: left hand side of comparison must be bound: =X<1/1')
    assert_error("ask(X<Y)", 'Error: left hand side of comparison must be bound: =X<Y/2')
    assert_error("ask(1<Y)", 'Error: left hand side of comparison must be bound: =Y>1/1')
    assert_error("ask( (A.c[X]==Y) & (Z.c[X]==Y))", "TypeError: First argument of Z.c\[1\]==\('.','.'\) must be a Z, not a A ")
    assert_ask("A.u[X]==Y", "Predicate without definition \(or error in resolver\): A.u\[1\]==/2")
    assert_ask("A.u[X,Y]==Z", "Predicate without definition \(or error in resolver\): A.u\[2\]==/3")
    assert_error('(a_sum[X] == sum(Y, key=Y)) <= p(X, Z, Y)', "Error: Duplicate definition of aggregate function.")
    assert_error('(two(X)==Z) <= (Z==X+(lambda X: X))', 'Syntax error near equality: consider using brackets. two\(X\)')
    assert_error('p(X) <= sum(X, key=X)', 'Invalid body for clause')
    assert_error('ask(- manager[X]==1)', "Left-hand side of equality must be a symbol or function, not an expression.")
    assert_error("p(X) <= (X=={})", "unhashable type: 'dict'")

    """ SQL Alchemy                    """

    from sqlalchemy import create_engine
    from sqlalchemy import Column, Integer, String, ForeignKey
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy.orm import sessionmaker, relationship
    
    engine = create_engine('sqlite:///:memory:', echo=False) # create database in memory
    Session = sessionmaker(bind=engine)
    session = Session()

    Base = declarative_base(cls=pyDatalog.Mixin, metaclass=pyDatalog.sqlMetaMixin)
    Base.session = session
        
    class Employee(Base): # --> Employee inherits from the Base class
        __tablename__ = 'employee'
        
        name = Column(String, primary_key=True)
        manager_name = Column(String, ForeignKey('employee.name'))
        salary = Column(Integer)
        
        def __init__(self, name, manager_name, salary):
            super(Employee, self).__init__()
            self.name = name
            self.manager_name = manager_name # direct manager of the employee, or None
            self.salary = salary # monthly salary of the employee
        def __repr__(self): # specifies how to display the employee
            return "Employee: %s" % self.name
    
        @pyDatalog.program() # --> the following function contains pyDatalog clauses
        def Employee():
            (Employee.manager[X]==Y) <= (Employee.manager_name[X]==Z) & (Z==Employee.name[Y])
            # the salary class of employee X is computed as a function of his/her salary
            # this statement is a logic equality, not an assignment !
            Employee.salary_class[X] = Employee.salary[X]//1000
            
            # all the indirect managers of employee X are derived from his manager, recursively
            Employee.indirect_manager(X,Y) <= (Employee.manager[X]==Y) & (Y != None)
            Employee.indirect_manager(X,Y) <= (Employee.manager[X]==Z) & Employee.indirect_manager(Z,Y) & (Y != None)
            
            # count the number of reports of X
            (Employee.report_count[X] == len(Y)) <= Employee.indirect_manager(Y,X)
            
            Employee.p(X,Y) <= (Y <= Employee.salary[X] + 1)
            

    Base.metadata.create_all(engine) 
    
    John = Employee('John', None, 6800)
    Mary = Employee('Mary', 'John', 6300)
    Sam = Employee('Sam', 'Mary', 5900)
    
    session.add(John)
    session.add(Mary)
    session.add(Sam)
    session.commit()
    
    assert (John.salary_class ==6) 
    
    X = pyDatalog.Variable()
    result = (Employee.salary[X] == 6300) # notice the similarity to a pyDatalog query
    assert result == [(Mary,), ]
    assert (X._value() == [Mary,]) # prints [Employee: Mary]
    assert (X.v() == Mary) # prints Employee:Mary

    result = (Employee.indirect_manager(Mary, X))
    assert result == [(John,), ]
    assert (X.v() == John) # prints [Employee: John]
    
    Mary.salary_class = ((Employee.salary_class[Mary]==X) >= X)
    Mary.salary = 10000
    assert Mary.salary_class != ((Employee.salary_class[Mary]==X) >= X)

    X, Y, N = pyDatalog.variables(3)
    result = (Employee.salary[X]==6800) & (Employee.name[X]==N)
    assert result == [(John,'John'), ]
    assert N.v() == 'John'
    
    result = (Employee.salary[X]==Employee.salary[X])
    assert result == [(John,), (Mary,), (Sam,)]
    
    result = (Employee.p(X,1))
    assert result == [(John,), (Mary,), (Sam,)]
    
    result = (Employee.salary[X]<Employee.salary[X]+1)
    assert result == [(John,), (Mary,), (Sam,)]
    
    result = (Employee.salary[John]==N) & Employee.p(John, N)
    assert result == [(6800,)]
    result = (Employee.salary[X]==6800) & (Employee.salary[X]==N) & Employee.p(X, N) 
    assert result == [(John, 6800)]

    """
示例#12
0
def mitigate(ServiceA):
    pyDatalog.retract_fact(compromised,ServiceA)
示例#13
0
    def __retract_user(self, user, prediction):
        retract_fact('user_id', user.id)
        retract_fact('name', user.id, user.name)
        retract_fact('age', user.id, user.age)
        retract_fact('bio', user.id, user.bio)
        retract_fact('distance', user.id, user.distance)
        retract_fact('distance_unit', user.id, user.distance_unit)
        retract_fact('ping_time', user.id, user.ping_time)
        retract_fact('birth_date', user.id, user.birth_date)

        for friend in user.common_friends:
            retract_fact('common_friend', user.id, friend)

        for like in user.common_likes:
            retract_fact('common_likes', user.id, like)

        retract_fact('prediction', user.id, prediction)
示例#14
0
文件: Bot.py 项目: Nan-Do/TinderBot
 def __retract_user(self, user, prediction):
     retract_fact('user_id', user.id)
     retract_fact('name', user.id, user.name)
     retract_fact('age', user.id, user.age)
     retract_fact('bio', user.id, user.bio)
     retract_fact('distance', user.id, user.distance)
     retract_fact('distance_unit', user.id, user.distance_unit)
     retract_fact('ping_time', user.id, user.ping_time)
     retract_fact('birth_date', user.id, user.birth_date)
     
     for friend in user.common_friends:
         retract_fact('common_friend', user.id, friend)
         
     for like in user.common_likes:
         retract_fact('common_likes', user.id, like)
         
     retract_fact('prediction', user.id, prediction)
示例#15
0
def borrar_sintomas(sintoma):
  for i in sintoma(X):
    pyDatalog.retract_fact(str(sintoma),str(i[0]))    
示例#16
0
def cutNetworkConnection(ServiceA, ServiceB):
    pyDatalog.retract_fact("networkConnectsTo",ServiceA,ServiceB)
示例#17
0
def moveService(ServiceA,HostA,HostB):
    pyDatalog.retract_fact(residesOn,ServiceA,HostA)
    pyDatalog.assert_fact(residesOn,HostB)
示例#18
0
def test():

    # test of expressions
    pyDatalog.load("""
        + p(a) # p is a proposition
    """)
    assert pyDatalog.ask('p(a)') == set([('a', )])

    pyDatalog.assert_fact('p', 'a', 'b')
    assert pyDatalog.ask('p(a, "b")') == set([('a', 'b')])
    pyDatalog.retract_fact('p', 'a', 'b')
    assert pyDatalog.ask('p(a, "b")') == None
    """unary facts                                                            """
    @pyDatalog.program()
    def unary():
        +z()
        assert ask(z()) == set([()])

        +p(a)
        # check that unary queries work
        assert ask(p(a)) == set([('a', )])
        assert ask(p(X)) == set([('a', )])
        assert ask(p(Y)) == set([('a', )])
        assert ask(p(_X)) == set([('a', )])
        assert ask(p(b)) == None
        assert ask(p(a) & p(b)) == None

        +p(b)
        assert ask(p(X), _fast=True) == set([('a', ), ('b', )])

        +p(b)  # facts are unique
        assert ask(p(X)) == set([('a', ), ('b', )])

        -p(b)  # retract a unary fact
        assert ask(p(X)) == set([('a', )])

        -p(a)
        assert ask(p(X)) == None
        +p(a)

        # strings and integers
        +p('c')
        assert ask(p(c)) == set([('c', )])

        +p(1)
        assert ask(p(1)) == set([(1, )])

        +n(None)
        assert ask(n(X)) == set([(None, )])
        assert ask(n(None)) == set([(None, )])

        # spaces and uppercase in strings
        +farmer('Moshe dayan')
        +farmer('omar')
        assert ask(farmer(X)) == set([('Moshe dayan', ), ('omar', )])

    # execute queries in a python program
    moshe_is_a_farmer = pyDatalog.ask("farmer('Moshe dayan')")
    assert moshe_is_a_farmer == set([('Moshe dayan', )])
    """ binary facts                                                         """

    @pyDatalog.program()
    def binary():
        +q(a, b)
        assert ask(q(a, b)) == set([('a', 'b')])
        assert ask(q(X, b)) == set([('a', 'b')])
        assert ask(q(a, Y)) == set([('a', 'b')])
        assert ask(q(a, c)) == None
        assert ask(q(X, Y)) == set([('a', 'b')])

        +q(a, c)
        assert ask(q(a, Y)) == set([('a', 'b'), ('a', 'c')])

        -q(a, c)
        assert ask(q(a, Y)) == set([('a', 'b')])

        assert ask(q(X, X)) == None
        +q(a, a)
        assert ask(q(X, X)) == set([('a', 'a')])
        -q(a, a)

    """ (in)equality                                             """

    @pyDatalog.program()
    def equality():
        assert ask(X == 1) == set([(1, )])
        assert ask(X == Y) == None
        assert ask(X == Y + 1) == None
        assert ask((X == 1) & (Y == 1) & (X == Y)) == set([(1, 1)])
        assert ask((X == 1) & (Y == 2) & (X == Y - 1)) == set([(1, 2)])
        #assert ask((X==1) & (Y==2) & (X+2==Y+1)) == set([(1,2)])
        assert ask((X == 2) & (Y == X / 2)) == set([(2, 1)])
        assert ask((X == 2) & (Y == X // 2)) == set([(2, 1)])

        assert ask((X == 1) & (Y == 1 + X)) == set([(1, 2)])
        assert ask((X == 1) & (Y == 1 - X)) == set([(1, 0)])
        assert ask((X == 1) & (Y == 2 * X)) == set([(1, 2)])
        assert ask((X == 2) & (Y == 2 / X)) == set([(2, 1)])
        assert ask((X == 2) & (Y == 2 // X)) == set([(2, 1)])

    """ Conjunctive queries                                             """

    @pyDatalog.program()
    def conjuctive():
        assert ask(q(X, Y) & p(X)) == set([('a', 'b')])

        assert ask(p(X) & p(a)) == set([('a', ), ('c', ), (1, )])
        assert ask(p(X) & p(Y) & (X == Y)) == set([('a', 'a'), ('c', 'c'),
                                                   (1, 1)])
        assert ask(p(X) & p(Y) & (X == Y) & (Y == a)) == set([('a', 'a')])

        assert ask(q(X, Y)) == set([('a', 'b')])
        assert ask(q(X, Y) & p(X)) == set([('a', 'b')])

    @pyDatalog.program()
    def equality2():
        assert ask((X == 1) & (X < X + 1)) == set([(1, )])
        assert ask((X == 1) & (Y == X)) == set([(1, 1)])
        assert ask((X == 1) & (Y == X + 1)) == set([(1, 2)])
        assert ask((X == 1) & (Y == X + 1) & (X < Y)) == set([(1, 2)])
        assert ask((X == 1) & (X < 1)) == None
        assert ask((X == 1) & (X <= 1)) == set([(1, )])
        assert ask((X == 1) & (X > 1)) == None
        assert ask((X == 1) & (X >= 1)) == set([(1, )])
        #       assert ask(X==(1,2)) == set([((1,2), (1,2))])
        assert ask(X in (1, )) == set([(1, )])
        assert ask((X == 1) & (X not in (2, ))) == set([(1, )])
        assert ask((X == 1) & ~(X in (2, ))) == set([(1, )])
        assert ask((X == 1) & (X not in (1, ))) == None
        assert ask((X == 1) & ~(X in (1, ))) == None

    @pyDatalog.program()
    def equality3():
        # equality (must be between parenthesis):
        s(X) <= (X == a)
        assert ask(s(X)) == set([('a', )])
        s(X) <= (X == 1)
        assert ask(s(X)) == set([(1, ), ('a', )])

        s(X, Y) <= p(X) & (X == Y)
        assert ask(s(a, a)) == set([('a', 'a')])
        assert ask(s(a, b)) == None
        assert ask(s(X, a)) == set([('a', 'a')])
        assert ask(s(X, Y)) == set([('a', 'a'), ('c', 'c'), (1, 1)])

    assert pyDatalog.ask('p(a)') == set([('a', )])
    """ clauses                                                         """

    @pyDatalog.program()
    def clauses():

        p2(X) <= p(X)
        assert ask(p2(a)) == set([('a', )])
        p2(X) <= p(X)

        r(X, Y) <= p(X) & p(Y)
        assert ask(r(a, a)) == set([('a', 'a')])
        assert ask(r(a, c)) == set([('a', 'c')])
        r(X, b) <= p(X)
        assert ask(r(a, b)) == set([('a', 'b')])

        -(r(X, b) <= p(X))
        assert ask(r(a, b)) == None

        # TODO more tests

        # integer variable
        for i in range(10):
            +successor(i + 1, i)
        assert ask(successor(2, 1)) == set([(2, 1)])

        # built-in
        assert abs(-3) == 3
        assert math.sin(3) == math.sin(3)

    """ in                                                         """

    pyDatalog.assert_fact('is_list', (1, 2))

    @pyDatalog.program()
    def _in():
        assert ((X == 1) & (X in (1, 2))) == [(1, )]
        _in(X) <= (X in [1, 2])
        assert ask(_in(1)) == set([(1, )])
        assert ask(_in(9)) == None
        assert ask(_in(X)) == set([(1, ), (2, )])

        _in2(X) <= is_list(Y) & (X in Y)
        assert ask(_in2(X)) == set([(1, ), (2, )])

        assert ask((Y == (1, 2)) & (X == 1) & (X in Y)) == set([((1, 2), 1)])
        assert ask((Y == (1, 2)) & (X == 1) & (X in Y + (3, ))) == set([
            ((1, 2), 1)
        ])

    """ recursion                                                         """

    @pyDatalog.program()
    def recursion():
        +even(0)
        even(N) <= successor(N, N1) & odd(N1)
        odd(N) <= ~even(N)
        assert ask(even(0)) == set([(0, )])
        assert ask(even(X)) == set([(4, ), (10, ), (6, ), (0, ), (2, ), (8, )])
        assert ask(even(10)) == set([(10, )])
        assert ask(odd(1)) == set([(1, )])
        assert ask(odd(5)) == set([(5, )])
        assert ask(even(5)) == None

    """ recursion with expressions                                         """
    # reset the engine
    pyDatalog.clear()

    @pyDatalog.program()
    def recursive_expression():

        predecessor(X, Y) <= (X == Y - 1)
        assert ask(predecessor(X, 11)) == set([(10, 11)])

        p(X, Z) <= (Y == Z - 1) & (X == Y - 1)
        assert ask(p(X, 11)) == set([(9, 11)])

        # odd and even
        +even(0)
        even(N) <= (N > 0) & odd(N - 1)
        assert ask(even(0)) == set([(0, )])
        odd(N) <= (N > 0) & ~even(N)
        assert ask(even(0)) == set([(0, )])
        assert ask(odd(1)) == set([(1, )])
        assert ask(odd(5)) == set([(5, )])
        assert ask(even(5)) == None
        assert ask((X == 3) & odd(X + 2)) == set([(3, )])

    # Factorial
    pyDatalog.clear()

    @pyDatalog.program()
    def factorial():
        #        (factorial[N] == F) <= (N < 1) & (F == -factorial[-N])
        #        + (factorial[1]==1)
        #        (factorial[N] == F) <= (N > 1) & (F == N*factorial[N-1])
        #        assert ask(factorial[1] == F) == set([(1, 1)])
        #        assert ask(factorial[4] == F) == set([(4, 24)])
        #        assert ask(factorial[-4] == F) == set([(-4, -24)])
        pass

    # Fibonacci
    pyDatalog.clear()

    @pyDatalog.program()
    def fibonacci():
        (fibonacci[N] == F) <= (N == 0) & (F == 0)
        (fibonacci[N] == F) <= (N == 1) & (F == 1)
        (fibonacci[N]
         == F) <= (N > 1) & (F == fibonacci[N - 1] + fibonacci[N - 2])
        assert ask(fibonacci[1] == F) == set([(1, 1)])
        assert ask(fibonacci[4] == F) == set([(4, 3)])
        assert ask(fibonacci[18] == F) == set([(18, 2584)])

    # string manipulation
    @pyDatalog.program()
    def _lambda():
        split(X, Y, Z) <= (X == Y + '-' + Z)
        assert ask(split(X, 'a', 'b')) == set([('a-b', 'a', 'b')])
        split(X, Y, Z) <= (Y == (lambda X: X.split('-')[0])) & (Z == (
            lambda X: X.split('-')[1]))
        assert ask(split('a-b', Y, Z)) == set([('a-b', 'a', 'b')])
        assert ask(split(X, 'a', 'b')) == set([('a-b', 'a', 'b')])

        (two[X] == Z) <= (Z == X + (lambda X: X))
        assert ask(two['A'] == Y) == set([('A', 'AA')])

    """ negation                                                     """

    @pyDatalog.program()
    def _negation():
        +p(a, b)
        assert ask(~p(X, b)) == None
        assert ask(~p(X, c)) == set([('X', 'c')])

    pyDatalog.load("""
        + even(0)
        even(N) <= (N > 0) & (N1==N-1) & odd(N1)
        odd(N) <= (N2==N+2) & ~ even(N) & (N2>0)
    """)
    assert pyDatalog.ask('~ odd(7)', _fast=True) == None
    assert pyDatalog.ask('~ odd(2)', _fast=True) == set([(2, )])
    assert pyDatalog.ask('odd(3)', _fast=True) == set([(3, )])
    assert pyDatalog.ask('odd(3)') == set([(3, )])
    assert pyDatalog.ask('odd(5)', _fast=True) == set([(5, )])
    assert pyDatalog.ask('odd(5)') == set([(5, )])
    assert pyDatalog.ask('even(5)', _fast=True) == None
    assert pyDatalog.ask('even(5)') == None
    """ functions                                                         """
    pyDatalog.clear()

    @pyDatalog.program()
    def function():
        +(f[a] == b)
        assert ask(f[X] == Y) == set([('a', 'b')])
        assert ask(f[X] == b) == set([('a', 'b')
                                      ])  #TODO remove 'b' from result
        assert ask(f[a] == X) == set([('a', 'b')])
        assert ask(f[a] == b) == set([('a', 'b')])

        +(f[a] == c)
        assert ask(f[a] == X) == set([('a', 'c')])

        +(f[a] == a)
        assert ask(f[f[a]] == X) == set([('a', )])
        assert ask(f[X] == f[a]) == set([('a', )])
        assert ask(f[X] == f[a] + '') == set([('a', )])
        -(f[a] == a)
        assert ask(f[f[a]] == X) == None

        +(f[a] == None)
        assert (ask(f[a] == X)) == set([('a', None)])
        +(f[a] == (1, 2))
        assert (ask(f[a] == X)) == set([('a', (1, 2))])
        assert (ask(f[X] == (1, 2))) == set([('a', (1, 2))])

        +(f[a] == c)

        +(f2[a, x] == b)
        assert ask(f2[a, x] == b) == set([('a', 'x', 'b')])

        +(f2[a, x] == c)
        assert ask(f2[a, x] == X) == set([('a', 'x', 'c')])

        g[X] = f[X] + f[X]
        assert (ask(g[a] == X)) == set([('a', 'cc')])

        h(X, Y) <= (f[X] == Y)
        assert (ask(h(X, 'c'))) == set([('a', 'c')])
        assert (ask(h(X, Y))) == set([('a', 'c')])

    @pyDatalog.program()
    def function_comparison():
        assert ask(f[X] == Y) == set([('a', 'c')])
        assert ask(f[a] < 'd') == set([('c', )])
        assert ask(f[a] > 'a') == set([('c', )])
        assert ask(f[a] >= 'c') == set([('c', )])
        assert ask(f[a] > 'c') == None
        assert ask(f[a] <= 'c') == set([('c', )])
        assert ask(f[a] > 'c') == None
        assert ask(f[a] in [
            'c',
        ]) == set([('c', )])

        assert ask((f[X] == 'c') & (f[Y] == f[X])) == set([('a', 'a')])
        assert ask((f[X] == 'c') & (f[Y] == f[X] + '')) == set([('a', 'a')])
        assert ask((f[X] == 'c') & (f[Y] == (lambda X: 'c'))) == set([('a',
                                                                       'a')])

        assert ask(f[X] == Y + '') == None
        assert ask((Y == 'c') & (f[X] == Y + '')) == set([('c', 'a')])
        assert ask((Y == 'c') & (f[X] <= Y + '')) == set([('c', 'a')])
        assert ask((Y == 'c') & (f[X] < Y + '')) == None
        assert ask((Y == 'c') & (f[X] < 'd' + Y + '')) == set([('c', 'a')])
        assert ask((Y == ('a', 'c')) & (f[X] in Y)) == set([(('a', 'c'), 'a')])
        assert ask((Y == ('a', 'c')) & (f[X] in (Y + ('z', )))) == set([
            (('a', 'c'), 'a')
        ])

        assert ask(f[X] == f[X] + '') == set([('a', )])

    @pyDatalog.program()
    def function_negation():
        assert not (ask(~(f[a] < 'd')))
        assert not (ask(~(f[X] < 'd')))
        assert ask(~(f[a] in ('d', )))

    """ aggregates                                                         """

    pyDatalog.clear()

    @pyDatalog.program()
    def sum():
        +p(a, c, 1)
        +p(b, b, 4)
        +p(a, b, 1)

        assert (sum(1, 2)) == 3
        (a_sum[X] == sum(Y, key=Z)) <= p(X, Z, Y)
        assert ask(a_sum[X] == Y) == set([('a', 2), ('b', 4)])
        assert ask(a_sum[a] == X) == set([('a', 2)])
        assert ask(a_sum[a] == 2) == set([('a', 2)])
        assert ask(a_sum[X] == 4) == set([('b', 4)])
        assert ask(a_sum[c] == X) == None
        assert ask((a_sum[X] == 2) & (p(X, Z, Y))) == set([('a', 'c', 1),
                                                           ('a', 'b', 1)])

        (a_sum2[X] == sum(Y, for_each=X)) <= p(X, Z, Y)
        assert ask(a_sum2[a] == X) == set([('a', 1)])

        (a_sum3[X] == sum(Y, key=(X, Z))) <= p(X, Z, Y)
        assert ask(a_sum3[X] == Y) == set([('a', 2), ('b', 4)])
        assert ask(a_sum3[a] == X) == set([('a', 2)])

    @pyDatalog.program()
    def len():
        assert (len((1, 2))) == 2
        (a_len[X] == len(Z)) <= p(X, Z, Y)
        assert ask(a_len[X] == Y) == set([('a', 2), ('b', 1)])
        assert ask(a_len[a] == X) == set([('a', 2)])
        assert ask(a_len[X] == 1) == set([('b', 1)])
        assert ask(a_len[X] == 5) == None

        (a_lenY[X] == len(Y)) <= p(X, Z, Y)
        assert ask(a_lenY[a] == X) == set([('a', 1)])
        assert ask(a_lenY[c] == X) == None

        (a_len2[X, Y] == len(Z)) <= p(X, Y, Z)
        assert ask(a_len2[a, b] == X) == set([('a', 'b', 1)])
        assert ask(a_len2[a, X] == Y) == set([('a', 'b', 1), ('a', 'c', 1)])

        +q(a, c, 1)
        +q(a, b, 2)
        +q(b, b, 4)

    @pyDatalog.program()
    def concat():
        (a_concat[X] == concat(Y, key=Z, sep='+')) <= q(X, Y, Z)
        assert ask(a_concat[X] == Y) == set([('b', 'b'), ('a', 'c+b')])
        assert ask(a_concat[a] == 'c+b') == set([('a', 'c+b')])
        assert ask(a_concat[a] == X) == set([('a', 'c+b')])
        assert ask(a_concat[X] == b) == set([('b', 'b')])

        (a_concat2[X] == concat(Y, order_by=(Z, ), sep='+')) <= q(X, Y, Z)
        assert ask(a_concat2[a] == X) == set([('a', 'c+b')])

        (a_concat3[X] == concat(Y, key=(-Z, ), sep='-')) <= q(X, Y, Z)
        assert ask(a_concat3[a] == X) == set([('a', 'b-c')])

    @pyDatalog.program()
    def min():
        assert min(1, 2) == 1
        (a_min[X] == min(Y, key=Z)) <= q(X, Y, Z)
        assert ask(a_min[X] == Y) == set([('b', 'b'), ('a', 'c')])
        assert ask(a_min[a] == 'c') == set([('a', 'c')])
        assert ask(a_min[a] == X) == set([('a', 'c')])
        assert ask(a_min[X] == 'b') == set([('b', 'b')])

        (a_minD[X] == min(Y, order_by=-Z)) <= q(X, Y, Z)
        assert ask(a_minD[a] == X) == set([('a', 'b')])

        (a_min2[X, Y] == min(Z, key=(X, Y))) <= q(X, Y, Z)
        assert ask(a_min2[Y, b] == X) == set([('a', 'b', 2), ('b', 'b', 4)])
        assert ask(a_min2[Y, Y] == X) == set([('b', 'b', 4)]), "a_min2"

        (a_min3[Y] == min(Z, key=(-X, Z))) <= q(X, Y, Z)
        assert ask(a_min3[b] == Y) == set([('b', 4)]), "a_min3"

    @pyDatalog.program()
    def max():
        assert max(1, 2) == 2
        (a_max[X] == max(Y, key=-Z)) <= q(X, Y, Z)
        assert ask(a_max[a] == X) == set([('a', 'c')])

        (a_maxD[X] == max(Y, order_by=Z)) <= q(X, Y, Z)
        assert ask(a_maxD[a] == X) == set([('a', 'b')])

    @pyDatalog.program()
    def rank():
        (a_rank1[Z] == rank(for_each=Z, order_by=Z)) <= q(X, Y, Z)
        assert ask(a_rank1[X] == Y) == set([(1, 0), (2, 0), (4, 0)])
        assert ask(a_rank1[X] == 0) == set([(1, 0), (2, 0), (4, 0)])
        assert ask(a_rank1[1] == X) == set([(1, 0)])
        assert ask(a_rank1[1] == 0) == set([(1, 0)])
        assert ask(a_rank1[1] == 1) == None

        # rank
        (a_rank[X, Y] == rank(for_each=(X, Y2),
                              order_by=Z2)) <= q(X, Y, Z) & q(X, Y2, Z2)
        assert ask(a_rank[X, Y] == Z) == set([('a', 'b', 1), ('a', 'c', 0),
                                              ('b', 'b', 0)])
        assert ask(a_rank[a, b] == 1) == set([('a', 'b', 1)])
        assert ask(a_rank[a, b] == Y) == set([('a', 'b', 1)])
        assert ask(a_rank[a, X] == 0) == set([('a', 'c', 0)])
        assert ask(a_rank[a, X] == Y) == set([('a', 'b', 1), ('a', 'c', 0)])
        assert ask(a_rank[X, Y] == 1) == set([('a', 'b', 1)])
        assert ask(a_rank[a, y] == Y) == None
        # reversed
        (b_rank[X, Y] == rank(for_each=(X, Y2),
                              order_by=-Z2)) <= q(X, Y, Z) & q(X, Y2, Z2)
        assert ask(b_rank[X, Y] == Z) == set([('a', 'b', 0), ('a', 'c', 1),
                                              ('b', 'b', 0)])
        assert ask(b_rank[a, b] == 0) == set([('a', 'b', 0)])
        assert ask(b_rank[a, b] == Y) == set([('a', 'b', 0)])
        assert ask(b_rank[a, X] == 1) == set([('a', 'c', 1)])
        assert ask(b_rank[a, X] == Y) == set([('a', 'b', 0), ('a', 'c', 1)])
        assert ask(b_rank[X, Y] == 0) == set([('a', 'b', 0), ('b', 'b', 0)])
        assert ask(b_rank[a, y] == Y) == None

    @pyDatalog.program()
    def running_sum():
        # running_sum
        (a_run_sum[X, Y] == running_sum(
            Z2, for_each=(X, Y2), order_by=Z2)) <= q(X, Y, Z) & q(X, Y2, Z2)
        assert ask(a_run_sum[X, Y] == Z) == set([('a', 'b', 3), ('a', 'c', 1),
                                                 ('b', 'b', 4)])
        assert ask(a_run_sum[a, b] == 3) == set([('a', 'b', 3)])
        assert ask(a_run_sum[a, b] == Y) == set([('a', 'b', 3)])
        assert ask(a_run_sum[a, X] == 1) == set([('a', 'c', 1)])
        assert ask(a_run_sum[a, X] == Y) == set([('a', 'b', 3), ('a', 'c', 1)])
        assert ask(a_run_sum[X, Y] == 4) == set([('b', 'b', 4)])
        assert ask(a_run_sum[a, y] == Y) == None

        (b_run_sum[X, Y] == running_sum(
            Z2, for_each=(X, Y2), order_by=-Z2)) <= q(X, Y, Z) & q(X, Y2, Z2)
        assert ask(b_run_sum[X, Y] == Z) == set([('a', 'b', 2), ('a', 'c', 3),
                                                 ('b', 'b', 4)])
        assert ask(b_run_sum[a, b] == 2) == set([('a', 'b', 2)])
        assert ask(b_run_sum[a, b] == Y) == set([('a', 'b', 2)])
        assert ask(b_run_sum[a, X] == 3) == set([('a', 'c', 3)])
        assert ask(b_run_sum[a, X] == Y) == set([('a', 'b', 2), ('a', 'c', 3)])
        assert ask(b_run_sum[X, Y] == 4) == set([('b', 'b', 4)])
        assert ask(b_run_sum[a, y] == Y) == None

    """ simple in-line queries                                        """
    X = pyDatalog.Variable()
    assert ((X == 1) >= X) == 1
    assert ((X == 1) & (X != 2) >= X) == 1
    assert set(X._in((1, 2))) == set([(1, ), (2, )])
    assert ((X == 1) & (X._in((1, 2)))) == [(1, )]
    """ interface with python classes                                        """

    class A(pyDatalog.Mixin):
        def __init__(self, b):
            super(A, self).__init__()
            self.b = b

        def __repr__(self):
            return self.b

        @pyDatalog.program(
        )  # indicates that the following method contains pyDatalog clauses
        def _():
            (A.c[X] == N) <= (A.b[X] == N)
            (A.len[X] == len(N)) <= (A.b[X] == N)

        @classmethod
        def _pyD_x1(cls, X):
            if X.is_const() and X.id.b == 'za':
                yield (X.id, )
            else:
                for X in pyDatalog.metaMixin.__refs__[cls]:
                    if X.b == 'za':
                        yield (X, )

    a = A('a')
    b = A('b')
    assert a.c == 'a'
    X, Y = pyDatalog.variables(2)
    assert (A.c[X] == 'a') == [(a, )]
    assert (A.c[X] == 'a')[0] == (a, )
    assert list(X.data) == [a]
    assert X.v() == a
    assert ((A.c[a] == X) >= X) == 'a'
    assert ((A.c[a] == X) & (A.c[a] == X) >= X) == 'a'
    assert ((A.c[a] == X) & (A.c[b] == X) >= X) == None
    (A.c[X] == 'b') & (A.b[X] == 'a')
    assert list(X.data) == []
    (A.c[X] == 'a') & (A.b[X] == 'a')
    assert list(X.data) == [a]
    result = (A.c[X] == 'a') & (A.b[X] == 'a')
    assert result == [(a, )]
    assert (A.c[a] == 'a') == [()]
    assert (A.b[a] == 'a') == [()]
    assert (A.c[a] == 'a') & (A.b[a] == 'a') == [()]
    assert (A.b[a] == 'f') == []
    assert ((A.c[a] == 'a') & (A.b[a] == 'f')) == []
    """ filters on python classes                                        """
    assert (A.b[X] != Y) == [(a, None), (b, None)]
    assert (A.b[X] != 'a') == [(b, )]
    assert (A.b[X] != 'z') == [(a, ), (b, )]
    assert (A.b[a] != 'a') == []
    assert list(A.b[b] != 'a') == [()]
    assert ((A.b[b] != 'a') & (A.b[b] != 'z')) == [()]

    assert (A.b[X] < Y) == [(a, None), (b, None)]
    assert (A.b[X] < 'a') == []
    assert (A.b[X] < 'z') == [(a, ), (b, )]
    assert (A.b[a] < 'b') == [()]
    assert (A.b[b] < 'a') == []
    assert ((A.b[b] < 'z') & (A.b[b] != 'z')) == [()]

    assert (A.b[X] <= 'a') == [(a, )]
    assert (A.b[X] <= 'z') == [(a, ), (b, )]
    assert (A.b[a] <= 'b') == [()]
    assert (A.b[b] <= 'a') == []
    assert ((A.b[b] <= 'z') & (A.b[b] != 'z')) == [()]

    assert (A.b[X] > 'a') == [(b, )]
    assert (A.b[X] >= 'a') == [(a, ), (b, )]

    assert (A.c[X] <= 'a') == [(a, )]
    assert (A.c[X] <= 'a' + '') == [(a, )]

    assert (A.c[X]._in(('a', ))) == [(a, )]
    assert (A.c[X]._in(('a', ) + ('z', ))) == [(a, )]
    assert ((Y == ('a', )) & (A.c[X]._in(Y))) == [(('a', ), a)
                                                  ]  # TODO make ' in ' work

    assert ((Y == ('a', )) & (A.c[X]._in(Y + ('z', )))) == [
        (('a', ), a)
    ]  # TODO make ' in ' work
    assert (A.c[X]._in(('z', ))) == []

    # more complex queries
    assert ((Y == 'a') & (A.b[X] != Y)) == [
        ('a', b)
    ]  # order of appearance of the variables !

    assert (A.len[X] == Y) == [(b, 1), (a, 1)]
    assert (A.len[a] == Y) == [(1, )]
    """ subclass                                              """

    class Z(A):
        def __init__(self, z):
            super(Z, self).__init__(z + 'a')
            self.z = z

        def __repr__(self):
            return self.z

        @pyDatalog.program(
        )  # indicates that the following method contains pyDatalog clauses
        def _():
            (Z.w[X] == N) <= (Z.z[X] != N)

        @classmethod
        def _pyD_query(cls, pred_name, args):
            if pred_name == 'Z.pred':
                if args[0].is_const() and args[0].id.b != 'za':
                    yield (args[0].id, )
                else:
                    for X in pyDatalog.metaMixin.__refs__[cls]:
                        if X.b != 'za':
                            yield (X, )
            else:
                raise AttributeError

    z = Z('z')
    assert z.z == 'z'
    assert (Z.z[X] == 'z') == [(z, )]
    assert ((Z.z[X] == 'z') & (Z.z[X] > 'a')) == [(z, )]
    assert list(X.data) == [z]
    try:
        a.z == 'z'
    except Exception as e:
        e_message = e.message if hasattr(e, 'message') else e.args[0]
        if e_message != "Predicate without definition (or error in resolver): A.z[1]==/2":
            print(e_message)
    else:
        assert False

    try:
        (Z.z[a] == 'z') == None
    except Exception as e:
        e_message = e.message if hasattr(e, 'message') else e.args[0]
        if e_message != "Object is incompatible with the class that is queried.":
            print(e_message)
    else:
        assert False

    assert (Z.b[X] == Y) == [(z, 'za')]
    assert (Z.c[X] == Y) == [(z, 'za')]
    assert ((Z.c[X] == Y) & (Z.c[X] > 'a')) == [(z, 'za')]
    assert (Z.c[X] > 'a') == [(z, )]
    assert ((Z.c[X] > 'a') & (A.c[X] == 'za')) == [(z, )]
    assert (A.c[X] == 'za') == [(z, )]
    assert (A.c[z] == 'za') == [()]
    assert (z.b) == 'za'
    assert (z.c) == 'za'

    w = Z('w')
    w = Z('w')  # duplicated to test __refs__[cls]
    assert (Z.x(X)) == [(z, )]
    assert not (~Z.x(z))
    assert ~Z.x(w)
    assert ~(Z.z[w] == 'z')
    assert (Z.pred(X)) == [(w, )]  # not duplicated !
    assert (Z.pred(X) & ~(Z.z[X] >= 'z')) == [(w, )]
    assert (Z.x(X) & ~(Z.pred(X))) == [(z, )]

    assert (Z.len[X] == Y) == [(w, 1), (z, 1)]
    assert (Z.len[z] == Y) == [(1, )]

    # TODO print (A.b[w]==Y)
    """ python resolvers                                              """

    @pyDatalog.predicate()
    def p(X, Y):
        yield (1, 2)
        yield (2, 3)

    assert pyDatalog.ask('p(X,Y)') == set([(1, 2), (2, 3)])
    assert pyDatalog.ask('p(1,Y)') == set([(1, 2)])
    assert pyDatalog.ask('p(1,2)') == set([(1, 2)])
    """ error detection                                              """

    @pyDatalog.program()
    def _():
        pass

    error = False
    try:
        _()
    except:
        error = True
    assert error

    def assert_error(code, message='^$'):
        _error = False
        try:
            pyDatalog.load(code)
        except Exception as e:
            e_message = e.message if hasattr(
                e, 'message') else e.args[0]  # python 2 and 3
            if not re.match(message, e_message):
                print(e_message)
            _error = True
        assert _error

    def assert_ask(code, message='^$'):
        _error = False
        try:
            pyDatalog.ask(code)
        except Exception as e:
            e_message = e.message if hasattr(e, 'message') else e.args[0]
            if not re.match(message, e_message):
                print(e_message)
            _error = True
        assert _error

    assert_error('ask(z(a),True)', 'Too many arguments for ask \!')
    assert_error('ask(z(a))',
                 'Predicate without definition \(or error in resolver\): z/1')
    assert_error(
        "+ farmer(farmer(moshe))",
        "Syntax error: Literals cannot have a literal as argument : farmer\[\]"
    )
    assert_error(
        "+ manager[Mary]==John",
        "Left-hand side of equality must be a symbol or function, not an expression."
    )
    assert_error(
        "manager[X]==Y <= (X==Y)",
        "Syntax error: please verify parenthesis around \(in\)equalities")
    assert_error("p(X) <= (Y==2)", "Can't create clause")
    assert_error(
        "p(X) <= X==1 & X==2",
        "Syntax error: please verify parenthesis around \(in\)equalities")
    assert_error("p(X) <= (manager[X]== min(X))",
                 "Error: argument missing in aggregate")
    assert_error("p(X) <= (manager[X]== max(X, order_by=X))",
                 "Aggregation cannot appear in the body of a clause")
    assert_error("q(min(X, order_by=X)) <= p(X)",
                 "Syntax error: Incorrect use of aggregation\.")
    assert_error(
        "manager[X]== min(X, order_by=X) <= manager(X)",
        "Syntax error: please verify parenthesis around \(in\)equalities")
    assert_error(
        "(manager[X]== min(X, order_by=X+2)) <= manager(X)",
        "order_by argument of aggregate must be variable\(s\), not expression\(s\)."
    )
    assert_error("ask(X<1)",
                 'Error: left hand side of comparison must be bound: =X<1/1')
    assert_error("ask(X<Y)",
                 'Error: left hand side of comparison must be bound: =X<Y/2')
    assert_error("ask(1<Y)",
                 'Error: left hand side of comparison must be bound: =Y>1/1')
    assert_error(
        "ask( (A.c[X]==Y) & (Z.c[X]==Y))",
        "TypeError: First argument of Z.c\[1\]==\('.','.'\) must be a Z, not a A "
    )
    assert_ask(
        "A.u[X]==Y",
        "Predicate without definition \(or error in resolver\): A.u\[1\]==/2")
    assert_ask(
        "A.u[X,Y]==Z",
        "Predicate without definition \(or error in resolver\): A.u\[2\]==/3")
    assert_error('(a_sum[X] == sum(Y, key=Y)) <= p(X, Z, Y)',
                 "Error: Duplicate definition of aggregate function.")
    assert_error(
        '(two(X)==Z) <= (Z==X+(lambda X: X))',
        'Syntax error near equality: consider using brackets. two\(X\)')
    assert_error('p(X) <= sum(X, key=X)', 'Invalid body for clause')
    assert_error(
        'ask(- manager[X]==1)',
        "Left-hand side of equality must be a symbol or function, not an expression."
    )
    assert_error("p(X) <= (X=={})", "unhashable type: 'dict'")
    """ SQL Alchemy                    """

    from sqlalchemy import create_engine
    from sqlalchemy import Column, Integer, String, ForeignKey
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy.orm import sessionmaker, relationship

    engine = create_engine('sqlite:///:memory:',
                           echo=False)  # create database in memory
    Session = sessionmaker(bind=engine)
    session = Session()

    Base = declarative_base(cls=pyDatalog.Mixin,
                            metaclass=pyDatalog.sqlMetaMixin)
    Base.session = session

    class Employee(Base):  # --> Employee inherits from the Base class
        __tablename__ = 'employee'

        name = Column(String, primary_key=True)
        manager_name = Column(String, ForeignKey('employee.name'))
        salary = Column(Integer)

        def __init__(self, name, manager_name, salary):
            super(Employee, self).__init__()
            self.name = name
            self.manager_name = manager_name  # direct manager of the employee, or None
            self.salary = salary  # monthly salary of the employee

        def __repr__(self):  # specifies how to display the employee
            return "Employee: %s" % self.name

        @pyDatalog.program(
        )  # --> the following function contains pyDatalog clauses
        def Employee():
            (Employee.manager[X]
             == Y) <= (Employee.manager_name[X] == Z) & (Z == Employee.name[Y])
            # the salary class of employee X is computed as a function of his/her salary
            # this statement is a logic equality, not an assignment !
            Employee.salary_class[X] = Employee.salary[X] // 1000

            # all the indirect managers of employee X are derived from his manager, recursively
            Employee.indirect_manager(
                X, Y) <= (Employee.manager[X] == Y) & (Y != None)
            Employee.indirect_manager(
                X, Y) <= (Employee.manager[X]
                          == Z) & Employee.indirect_manager(Z, Y) & (Y != None)

            # count the number of reports of X
            (Employee.report_count[X] == len(Y)) <= Employee.indirect_manager(
                Y, X)

            Employee.p(X, Y) <= (Y <= Employee.salary[X] + 1)

    Base.metadata.create_all(engine)

    John = Employee('John', None, 6800)
    Mary = Employee('Mary', 'John', 6300)
    Sam = Employee('Sam', 'Mary', 5900)

    session.add(John)
    session.add(Mary)
    session.add(Sam)
    session.commit()

    assert (John.salary_class == 6)

    X = pyDatalog.Variable()
    result = (Employee.salary[X] == 6300
              )  # notice the similarity to a pyDatalog query
    assert result == [
        (Mary, ),
    ]
    assert (X._value() == [
        Mary,
    ])  # prints [Employee: Mary]
    assert (X.v() == Mary)  # prints Employee:Mary

    result = (Employee.indirect_manager(Mary, X))
    assert result == [
        (John, ),
    ]
    assert (X.v() == John)  # prints [Employee: John]

    Mary.salary_class = ((Employee.salary_class[Mary] == X) >= X)
    Mary.salary = 10000
    assert Mary.salary_class != ((Employee.salary_class[Mary] == X) >= X)

    X, Y, N = pyDatalog.variables(3)
    result = (Employee.salary[X] == 6800) & (Employee.name[X] == N)
    assert result == [
        (John, 'John'),
    ]
    assert N.v() == 'John'

    result = (Employee.salary[X] == Employee.salary[X])
    assert result == [(John, ), (Mary, ), (Sam, )]

    result = (Employee.p(X, 1))
    assert result == [(John, ), (Mary, ), (Sam, )]

    result = (Employee.salary[X] < Employee.salary[X] + 1)
    assert result == [(John, ), (Mary, ), (Sam, )]

    result = (Employee.salary[John] == N) & Employee.p(John, N)
    assert result == [(6800, )]
    result = (Employee.salary[X] == 6800) & (Employee.salary[X]
                                             == N) & Employee.p(X, N)
    assert result == [(John, 6800)]
    """
示例#19
0
def tryTacticOptions(maxTactics,debug=False):
    #Initialize utilities dictionary for each of the tactic options
    utilities = {}
    #if debug:
    #bestUtility = float(0)
    originalUtil = float(determineResidualUtility())
    bestUtility = originalUtil
    print("Original utility: " + str(originalUtil))
    utilities[""] = originalUtil

    #For progress tracking
    numOptions = 0
    for numTactics in range(1,maxTactics+1):
        numOptions += len(list(itertools.combinations(tacticsIter(),numTactics)))
        #TODO? Put following line back in
        #utilities[None] = float(determineResidualUtility())

    setNumber = 0

    print("Tactic options: " + str(numOptions))
    #TODO Extra double-check to ensure logic is correct here...should be all possible vulnerabilities
    #TODO Also try zero tactics! Maybe current config is the best
    for numTactics in range(1,maxTactics+1):
        #Creator iterator of tactic sets to try
        tacticOptions = itertools.combinations(tacticsIter(),numTactics)
        #if numTactics == 0:
        #    tacticOptions = iter([[]])
        for tacticSet in tacticOptions:
            #Print status
            setNumber += 1
            percentComplete = 100 * setNumber / numOptions
            print(str(percentComplete) + "%...\r"),
            sys.stdout.flush()
            #print "***************************************"
            #print "Tactic Set:"
            #print tacticSet
            #Apply the tactics in the selected tactic set
            for tactic in tacticSet:
                #print tactic
                args = tactic[1:]
                if tactic[0] == "retract":
                    pyDatalog.retract_fact(*args)
                    if bidirectional:
                        if tactic[1] == "networkConnectsTo": #retract the symmetric connection
                            #TODO Did we already do this in the code above? Is it a safe assumption that the reversed connection has the same CIA attributes?
                            #Changed
                            argsRev = [args[0],args[2],args[1],args[3],args[4],args[5]]
                            pyDatalog.retract_fact(*argsRev)
                else:
                    pyDatalog.assert_fact(*args)
            #Determine utility and put in dictionary
            #This weighted value includes cost of tactic: A cost of 1 per tactic
            tsStr = str(sorted(list(tacticSet))) #key
            residualUtil = float(determineResidualUtility() - numTactics) #value
            if debug and residualUtil>=bestUtility:
                print("New optimal: " + str(residualUtil) + str(tacticSet))
                bestUtility = residualUtil
            #TODO The check for multiple tactics may not be necessary in some versions of this code
            if tsStr in utilities:
                #print("MULTIPLE: " + tsStr + ": " + str(residualUtil))
                if residualUtil < utilities[tsStr]:
                    utilities[tsStr] = residualUtil
            else:
                utilities[tsStr] = residualUtil
            #Undo the tactic application
            for tactic in tacticSet:
                args = tactic[1:]
                if tactic[0] == "assert":
                    pyDatalog.retract_fact(*args)
                    #Changed
                    #TODO Is this a safe assumption that the reversed connection has the same CIA attributes?
                    if (tactic[1] == "networkConnectsToWithAttributes") and (bidirectional == True):
                        pyDatalog.retract_fact(args[0],args[2],args[1],args[3],args[4],args[5])
                else:
                    pyDatalog.assert_fact(*args)
    #pprint.pprint(utilities)
    #Find the best performing tactic set in the dictionary created earlier
    bestOptions(utilities)
示例#20
0
 def forgetData(self, estereotipo, nombre):
     pyDatalog.retract_fact(estereotipo, nombre)