def buscar(): print( 'Opciones para buscar personas\n1: Buscar a todos\n2: Buscar por nombre\n3: Volver\n' ) sel = None while sel not in ("1", "2", "3"): sel = input('Seleccione una opción:\n') if (sel == '1'): print(pyDatalog.ask("is_person(X)")) elif (sel == '2'): nombre = input("Escribe un nombre para consultar:\n") W = pyDatalog.ask("is_person('" + nombre + "')") if (str(W) == 'None'): op = None while op not in ("s", "n", "S", "N"): op = input( "No existe esta persona, ¿Desea agregarla?(S/N)") if (op == 's' or op == 'S'): agregar(nombre) elif (op == "n" or op == "N"): break else: print('Solo puede agregar S o N') else: print(W.answers) elif (sel == '3'): break else: print("Ingrese una opción valida")
def assertPatient(self, name, age, sick): Logic(self.first) #metodo que agrega un paciente a la base de conocimiento pyDatalog.assert_fact('age', name, age) pyDatalog.assert_fact('sick_of', name, sick) tq2 = "sick_of(" + name + ",Y)" tq3 = "require_limits_temperature(" + name + ",A,B)" tq4 = "require_temperature(" + name + ",A)" tq5 = "is_in_stage(" + name + ",A)" q2 = pyDatalog.ask(tq2) q3 = pyDatalog.ask(tq3) q4 = pyDatalog.ask(tq4) q5 = pyDatalog.ask(tq5) db = self.con.conexion() coleccion = db['sensor_temp'] query = {"activo": "t"} data = { "$set": { "act0_status": "f", "act1_status": "f", "enfermedad": q2.answers[0][0], "temp_min": int(q3.answers[0][1]), "temp_max": int(q3.answers[0][0]), "temp_pref": int(q4.answers[0][0]), "stage": q5.answers[0][0] } } coleccion.update_one(query, data) print("mongodb updated") return 'saved'
def addRecipe(name, ingredients, instructions): isAlreadyDefined = pyDatalog.ask("hasID('" + name + "', 'recipe', Z)") if (not isAlreadyDefined == None): print("This recipe is already defined") return isAlreadyDefined.answers[0][0] # insert recipe in database database = connectToDatabase() mydb = database[0] mycursor = database[1] sqlRezepte = "INSERT INTO `Rezepte` (`Name`, `Anleitung`) VALUES (%s, %s)" valRezepte = [(name, instructions)] mycursor.executemany(sqlRezepte, valRezepte) mydb.commit() recipeNumber = mycursor.lastrowid id = "recipe" + str(recipeNumber) # add recipe to datalog pyDatalog.assert_fact('hasID', name, "recipe", id) pyDatalog.assert_fact('recipeInstructions', name, instructions) # insert ingredients in database for i in ingredients: if isValidId(i[0]): if "ingredient" in i[0]: sqlEnthaeltLebensmittel = "INSERT INTO `enthaeltLebensmittel` (`RezeptId`, `LebensmittelId`, `Menge`) VALUES (%s, %s, %s)" valEnthaeltLebensmittel = [(recipeNumber, int(i[0][10:11]), i[1])] mycursor.executemany(sqlEnthaeltLebensmittel, valEnthaeltLebensmittel) mydb.commit() if "recipe" in i[0]: sqlEnthaeltrezept = "INSERT INTO `enthaeltRezept` (`RezeptId`, `KomponentenRezeptId`, `Menge`) VALUES (%s, %s, %s)" valEnthaeltrezept = [(recipeNumber, int(i[0][6:7]), i[1])] mycursor.executemany(sqlEnthaeltrezept, valEnthaeltrezept) mydb.commit() # add ingredient to datalog ingredient = pyDatalog.ask(("hasID(X, Y, '" + i[0] + "')")) pyDatalog.assert_fact('containsIngredient', name, ingredient.answers[0][0], i[1]) else: print(i[0] + " is no valid id") # add weight to datalog ingredients = pyDatalog.ask("containsIngredient('" + name + "', Y, Z)") weight = 0 if not ingredients == None: for i in ingredients.answers: weight += i[1] pyDatalog.assert_fact('weightPerServing', name, weight) pyDatalog.assert_fact('hasCaloriesPer100g', name, getCaloriesPer100g(name)) if mydb.is_connected(): mycursor.close() mydb.close() return id
def getRecipeDetails(recipeID): recipeInstructions = pyDatalog.ask("hasID(X, 'recipe', '" + recipeID + "') & recipeInstructions(X, Y)") recipeIngredients = pyDatalog.ask("containsIngredient('" + recipeInstructions.answers[0][0] + "', Y, Z)") recipeIngredientsReturnValue = [] if not recipeIngredients == None: recipeIngredientsReturnValue = recipeIngredients.answers return recipeInstructions.answers + recipeIngredientsReturnValue
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
def test(): query = "parent(X, Y)" answers = pyDatalog.ask(query).answers for ans in answers: print(ans) print() print() query = 'decendent(X, "Fernanda del Carpio")' answers = sorted(pyDatalog.ask(query).answers) expected_answers = ("Aureliano (II)", ) print(answers) pass
def getCaloriesPer100g(recipeName): calorieSum = 0 calories = pyDatalog.ask("baseIngredientWithCalories('" + recipeName + "', X, Y)") if not calories == None: for c in calories.answers: calorieSum += c[1] return calorieSum * 100 / pyDatalog.ask("weightPerServing('" + recipeName + "', X)").answers[0][0] else: return 0
def readPatient(self, name): Logic(self.first) #metodo que consulta los datos del paciente actualmente presente en la base de conocimiento returnvalue = "" try: tq1 = "age(" + name + ",Y)" tq2 = "sick_of(" + name + ",Y)" q1 = pyDatalog.ask(tq1) q2 = pyDatalog.ask(tq2) returnvalue = "{'name':'" + name + "','age':'" + q1.answers[0][ 0] + "','sick':'" + q2.answers[0][0] + "'}" except: returnvalue = "NOPATIENT" return returnvalue
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
def addIngredient(name, calorie, glutenFree, lactoseFree, vegan, vegetarian): isAlreadyDefined = pyDatalog.ask("hasID('" + name + "', 'ingredient', Z)") if (not isAlreadyDefined == None): print("This ingredient is already defined") return isAlreadyDefined.answers[0][0] # insert ingredient in database database = connectToDatabase() mydb = database[0] mycursor = database[1] sqlZutat = "INSERT INTO `Lebensmittel` (`Name`, `KalorienPro100g`, `Fleisch`, `Tierprodukt`, `Gluten`, `Krebstiere`, `Eier`, `Fisch`, `Erdnuesse`, `Sojabohnen`, `Milch`, `Schalenfruechte`, `Sellerie`, `Sesamsamen`, `Schwefeldioxid`, `Lupinien`, `Weichtiere`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" valZutat = [(name, calorie, 0 if vegetarian else 1, 0 if vegan else 1, 0 if glutenFree else 1, 0, 0, 0, 0, 0, 0 if lactoseFree else 1, 0, 0, 0, 0, 0, 0)] mycursor.executemany(sqlZutat, valZutat) mydb.commit() ingredientNumber = mycursor.lastrowid id = "ingredient" + str(ingredientNumber) # add ingredient to datalog pyDatalog.assert_fact('hasID', name, "ingredient", id) pyDatalog.assert_fact('hasCaloriesPer100g', name, calorie) if not glutenFree: pyDatalog.assert_fact('containsGluten', name) if not lactoseFree: pyDatalog.assert_fact('containsLactose', name) if not vegetarian: pyDatalog.assert_fact('containsMeat', name) if (not vegetarian) | (not vegan): pyDatalog.assert_fact('containsAnimalProduct', name) pyDatalog.assert_fact('weightPerServing', name, 1)
def queen(thread_name): n = int(random.random() * 8) + 1 # 1 to 8 Logic() queens(X0) <= (X0._in(range(n))) queens(X0,X1) <= queens(X0) & next_queen(X0,X1) queens(X0,X1,X2) <= queens(X0,X1) & next_queen(X0,X1,X2) queens(X0,X1,X2,X3) <= queens(X0,X1,X2) & next_queen(X0,X1,X2,X3) queens(X0,X1,X2,X3,X4) <= queens(X0,X1,X2,X3) & next_queen(X0,X1,X2,X3,X4) queens(X0,X1,X2,X3,X4,X5) <= queens(X0,X1,X2,X3,X4) & next_queen(X0,X1,X2,X3,X4,X5) queens(X0,X1,X2,X3,X4,X5,X6) <= queens(X0,X1,X2,X3,X4,X5) & next_queen(X0,X1,X2,X3,X4,X5,X6) queens(X0,X1,X2,X3,X4,X5,X6,X7) <= queens(X0,X1,X2,X3,X4,X5,X6) & next_queen(X0,X1,X2,X3,X4,X5,X6,X7) next_queen(X0,X1) <= queens(X1) & ok(X0,1,X1) next_queen(X0,X1,X2) <= next_queen(X1,X2) & ok(X0,2,X2) next_queen(X0,X1,X2,X3) <= next_queen(X1,X2,X3) & ok(X0,3,X3) next_queen(X0,X1,X2,X3,X4) <= next_queen(X1,X2,X3,X4) & ok(X0,4,X4) next_queen(X0,X1,X2,X3,X4,X5) <= next_queen(X1,X2,X3,X4,X5) & ok(X0,5,X5) next_queen(X0,X1,X2,X3,X4,X5,X6) <= next_queen(X1,X2,X3,X4,X5,X6) & ok(X0,6,X6) next_queen(X0,X1,X2,X3,X4,X5,X6,X7) <= next_queen(X1,X2,X3,X4,X5,X6,X7) & ok(X0,7,X7) query = pyDatalog.ask("queens(%s)" % (",".join("X%s" % i for i in range(n)))) answers = query.answers if query else [] result = "OK" if len(answers) == [1,0,0,2,10,4,40,92][n-1] else "* not OK ! *" print("%s : n = %d %s " % (thread_name, n, result))
def leer_perfil(self, datos): str_experto = datos[0] str_medico = "" if(datos[1] == "si" and datos[2]=="si" and datos[3] == "si"): str_medico = "si" elif(datos[1] == "si" and datos[2]=="no" and datos[3] == "si"): str_medico = "si" else: str_medico = "no" Logic(self.conocimiento) try: pyDatalog.assert_fact("resp_experto",str_experto) pyDatalog.assert_fact("resp_medico",str_medico) q = "tipo_usuario(X,Y)" rq_3 = pyDatalog.ask(q) arr_resp = [ rq_3.answers[0][0], rq_3.answers[0][1] ] return arr_resp except: print("error")
def __listarPalabrasComunesIdiomas(_idiomaA, _idiomaB): # Listar todas las palabras comunes entre dos idiomas consulta = ask('palabrasComunes(' + _idiomaA + ', ' + _idiomaB + ', R1)') if (consulta): return consulta.answers else: return "No hubo coincidencia."
def answerQuestion(peer_name, net, user_password): policy_filename = 'entities/' + peer_name + '/datalog_policy.data' data_filename = 'entities/' + peer_name + '/data.data' queryString = net.recv(); queryList = queryString.split(network_protocol.SEPARATOR) print(queryList) trusted_peer_dict = getTrustedPeers(peer_name) for other_peer, trusted_with in trusted_peer_dict.items(): data = getDataFromTrustedPeer(peer_name, other_peer, user_password) pruned_data = prune(data, trusted_with) add_facts(pruned_data) # Add our own data if(fileIO.fileExists(data_filename)): add_facts(fileIO.readFile(data_filename)) pyDatalog.load(fileIO.readFile(policy_filename)) print("Asking question: " + queryList[0]) query_result = pyDatalog.ask(queryList[0]); print('Query result:',query_result) if(query_result != None): net.send(queryList[1]) else: net.send(queryList[2])
def __palabrasEnUnIdiomaOriginadasPorPalabra(_palabra, _idioma): #Obtener el conjunto de todas las palabras en un idioma originadas por una palabra específica consulta = ask('hijosIdioma(' + _palabra + ',' + _idioma + ', R)') if (consulta): return consulta.answers else: return "No se obtuvo coincidencias"
def __idiomasRelacionadosPalabra(_palabra): # Listar los idiomas relacionados con una palabra consulta = ask('_soloIdiomas(' + _palabra + ', R)') if (consulta): return consulta.answers else: return "No hay idiomas relacionados"
def __numeroPalabrasComunesIdiomas(_idiomaA, _idiomaB): # Contar todas las palabras comunes entre dos idiomas consulta = ask('contarPalabrasComunes('+_idiomaA+', '+_idiomaB+', R1)') if (consulta): return consulta.answers[0][0][0] else: return 0
def _(): # the function name is ignored pyDatalog.load(mc) #pyDatalog.load(""" #+ (factorial[1]==1) #(factorial[N] == F) <= (N > 1) & (F == N*factorial[N-1]) #""") print(pyDatalog.ask('factorial[4]==F'))
def materialize(facts, clauses, parser): logger.info('Asserting facts ..') for f in facts: # Each fact is asserted using the index of the subject, predicate and object for avoiding syntax issues s_idx = parser.entity_to_index[f.argument_names[0]] p_idx = parser.predicate_to_index[f.predicate_name] o_idx = parser.entity_to_index[f.argument_names[1]] # Asserting p(S, P, O) pyDatalog.assert_fact('p', s_idx, p_idx, o_idx) rules_str = '\n'.join( [clause_to_str(clause, parser) for clause in clauses]) pyDatalog.load(rules_str) # Asking for all P(s, p, o) triples which hold true in the Knowledge Graph logger.info('Querying triples ..') _ans = pyDatalog.ask('p(S, P, O)') index_to_predicate = { idx: p for p, idx in parser.predicate_to_index.items() } index_to_entity = {idx: e for e, idx in parser.entity_to_index.items()} # Generating a list of inferred facts by replacing each entity and predicate index with their corresponding symbols inferred_facts = [ Fact(index_to_predicate[p], [index_to_entity[s], index_to_entity[o]]) for (s, p, o) in sorted(_ans.answers) ] return inferred_facts
def __esHija(_idiomaHijo, _hijo, _idiomaPadre, _padre): # Determinar si una palabra es hij@ de otra if (ask('esHijo(' + _idiomaHijo + ',' + _hijo + ',' + _idiomaPadre + ',' + _padre + ', R)')): return "Sí es hija" else: return "No es hija"
def __sonPrimas(_idiomaA, _nombreA, _idiomaB, _nombreB): # Determinar si dos palabras son prim@s if (ask('sonPrimas(' + _idiomaA + ',' + _nombreA + ',' + _idiomaB + ',' + _nombreB + ')')): return "Son primas" else: return "No son primas"
def connectionsAnswer(): connectionsAnswer = pyDatalog.ask('networkConnectsTo(ServiceA,ServiceB,C*K,IOK,AOK)') if connectionsAnswer == None: connections = [] else: connections = connectionsAnswer.answers return connections
def check_logic(thread_name, n): query = pyDatalog.ask("queens(%s)" % (",".join("X%s" % i for i in range(n)))) answers = query.answers if query else [] result = "OK" if len(answers) == [1, 0, 0, 2, 10, 4, 40, 92 ][n - 1] else answers print("%s : n = %d %s " % (thread_name, n, result))
def __esTia(_idiomaTio, _tio, _idiomaSobrino, _sobrino): # Determinar si una palabra es tí@ if (ask('esTio(' + _idiomaTio + ',' + _tio + ',' + _idiomaSobrino + ',' + _sobrino + ', R)')): return "Sí es tía" else: return "No es tía"
def __listarIdiomasAportaronOtro(_idioma): # LIstar todos los idiomas que aportaron a otro. Similar al anterior pero debe incluir porcentaje para todos los idiomas consulta = ask('contribucionXidioma(' + _idioma + ', R1, R2)') if (consulta): return consulta.answers else: return "Ninguno"
def __gradoPrimas(_nombreA, _nombreB): # Determina si son primas y en qué grado consulta = ask('gradoPrimos(' + _nombreA + ',' + _nombreB + ', R)') if (consulta): return "Son primas", consulta.answers[0][0] else: return "No son primas", 0
def __idiomaMasAporto(_idioma): # Idioma que más aportó a otro (e.g. latín a español). Medir basado en porcentaje consulta = ask('__mayorContribucion(' + _idioma + ', R1)') if (consulta): return consulta.answers[0][0] else: return "Ninguno"
def designOfExperiment(possibleCompromises,riskDict,debug=True): query = "networkConnectsTo(SourceService,TargetService,C*K,IOK,AOK)" components = pyDatalog.ask(query).answers getService0 = itemgetter(0) getService1 = itemgetter(1) componentsList = list(map(getService0,iter(components))) componentsList2 = list(map(getService1,iter(components))) componentsList.extend(componentsList2) componentsList = list(set(componentsList)) #pprint.pprint(componentsList) compromisedComponents = list(map(getService0,iter(possibleCompromises))) possibleCompromisesDict = dict(possibleCompromises) ledger = [] #pprint.pprint(compromisedComponents) #print(possibleCompromisesDict.get("internet")) #sumComponentUtilCompromised = 0 #sumComponentUtilUncompromised = 0 for component in componentsList: newPossibleCompromises = possibleCompromisesDict newPossibleCompromises[component] = 1.0 componentUtilCompromised = 1.0 #For code development only #componentUtilCompromised = determineResidualUtility(newPossibleCompromises,riskDict,True) newPossibleCompromises = possibleCompromisesDict if component in compromisedComponents: del newPossibleCompromises[component] componentUtilUncompromised = 0.0 #For code development only #componentUtilUncompromised = determineResidualUtility(newPossibleCompromises,riskDict,True) ledger.append([component,componentUtilCompromised,componentUtilUncompromised]) pprint.pprint(ledger)
def __sonHermanos(_idiomaA, _nombreA, _idiomaB, _nombreB): # Determinar si dos palabras son heman@s if (ask('sonHermanos(' + _idiomaA + ',' + _nombreA + ',' + _idiomaB + ',' + _nombreB + ', R)')): return "Son hermanas" else: return "No son hermanas"
def executeQuery(query, console): result = pyDatalog.ask(query) console.print("|--------------------Query---------------------\n|", query) console.print("|--------------------Result--------------------\n|", result) if result == None: return False return True
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))
def __idiomasRelacionadosPalabra(_palabra): # Listar los idiomas relacionados con una palabra consulta = ask('_soloIdiomas(' + _palabra + ', R)') if (consulta): words = [] for r in consulta.answers: words.append(r[0]) return words else: return "No hay idiomas relacionados"
def findall2(S,L): """ Hackish PROLOG findall emulation. use cases: findall(C,V) -> Given a query String in S, ask the LogKB and resolve to a list of results in L. """ if (S.is_const() and not L.is_const()): result = pyDatalog.ask(S.id).answers result.sort() yield (S,tuple(result)) else: raise Exception("unhandled case in findall: {0},{1}".format(S.is_const(),L.is_const()))
def analize_user(self, user, prediction): actions = [] self.__assert_user(user, prediction) if ask('accept("{}")'.format(str(user.id))) != None: actions.append('Accept') else: actions.append('Reject') #if ask('log("{}")'.format(str(user.id))) != None: # actions.append('Log') return actions
def ask_predicate(self, predicate): """ Queries PyDataLog for the given predicate by constructing a query fitting the pyDataLog Syntax. :param predicate: The predicate to be quried for :return: The Value of the given predicate if it exists in the Database, None otherwise. """ query = predicate.name + "(" query += ','.join([str(a) if not isinstance(a, Symbol) else "\'" + str(a) + "\'" \ for a in predicate.args]) query += ", X)" answer = pyDatalog.ask(query) if answer is None: return None return self.transform_answer(answer.answers)
def ask(self, query_symbols, logical_query, coeff_expr=None): """ Builds a pyDataLog program from the logical_query and loads it. Then executes the query for the query_symbols. :param query_symbols: The symbols to be queried. :type query_symbols: list(SubSymbol) :param logical_query: :type: :return: """ helper_len = 0 tmp = None if not query_symbols: return None if coeff_expr is None: helper_len = len(query_symbols) helper_predicate = 'helper(' + ','.join([str(v) for v in query_symbols]) + ')' tmp = helper_predicate + " <= " + self.transform_query(logical_query) else: helper_len = len(query_symbols) + 1 syms = OrderedSet(query_symbols) syms.add('COEFF_EXPR') helper_predicate = 'helper(' + ','.join([str(v) for v in syms]) + ')' index_query = self.transform_query(logical_query) coeff_query = "(COEFF_EXPR == " + str(coeff_expr) + ")" if index_query is None: tmp = helper_predicate + " <= " + coeff_query else: tmp = helper_predicate + " <= " + " & ".join([index_query, coeff_query]) log.debug("pyDatalog query: " + tmp) pyDatalog.load(tmp) answer = pyDatalog.ask(helper_predicate) pyEngine.Pred.reset_clauses(pyEngine.Pred("helper", helper_len)) if answer is None: return [] return self.transform_answer(answer.answers)
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)] """
def check_logic(thread_name, n): query = pyDatalog.ask("queens(%s)" % (",".join("X%s" % i for i in range(n)))) answers = query.answers if query else [] result = "OK" if len(answers) == [1,0,0,2,10,4,40,92][n-1] else answers print("%s : n = %d %s " % (thread_name, n, result))
Employee.indirect_manager(Mary, X) print(X) # prints [John] # Who are the employees of John with a salary below 6000 ? result = (Employee.salary[X] < 6000) & Employee.indirect_manager(X, John) print(result) # prints [(Sam,)] print(X) # prints [Sam] print((Employee.salary_class[X] == 5) & Employee.indirect_manager(X, John) >= X) # Sam # verify that the manager of Mary is John assert Employee.manager[Mary]==John # who is his own indirect manager ? Employee.indirect_manager(X, X) print(X) # prints [] # who has 2 reports ? Employee.report_count[X] == 2 print(X) # prints [John] # what is the total salary of the employees of John ? # note : it is better to place aggregation clauses in the class definition pyDatalog.load("(Employee.budget[X] == sum(N, for_each=Y)) <= (Employee.indirect_manager(Y, X)) & (Employee.salary[Y]==N)") Employee.budget[John]==X print(X) # prints [12200] # who has the lowest salary ? pyDatalog.load("(lowest[1] == min(X, order_by=N)) <= (Employee.salary[X]==N)") # must use ask() because inline queries cannot use unprefixed literals print(pyDatalog.ask("lowest[1]==X")) # prints set([(1, 'Sam')])