def addNewRule(origSubgoalName, newSubgoalName, origSubgoalAttList, newSubgoalAttList, cursor): print "////////////////////////////////////////////" print "origSubgoalName : " + origSubgoalName print "newSubgoalName : " + newSubgoalName print "origSubgoalAttList : " + str(origSubgoalAttList) print "newSubgoalAttList : " + str(newSubgoalAttList) # ------------------------------------------- # # generate new rule id newRID = tools.getID() # ------------------------------------------- # # save goal data goalTimeArg = "" rewrittenFlag = True # insert into Rule cursor.execute( "INSERT INTO Rule (rid, goalName, goalTimeArg, rewritten) VALUES ('" + newRID + "','" + newSubgoalName + "','" + goalTimeArg + "','" + str(rewrittenFlag) + "')") # insert into GoalAtt for att in newSubgoalAttList: attID = att[0] attName = att[1] attType = att[2] cursor.execute("INSERT INTO GoalAtt VALUES ('" + newRID + "','" + str(attID) + "','" + attName + "','" + attType + "')") # ------------------------------------------- # # generate new subgoal id subgoalID = tools.getID() subgoalTimeArg = "" # insert into Subgoals cursor.execute("INSERT INTO Subgoals VALUES ('" + newRID + "','" + subgoalID + "','" + origSubgoalName + "','" + subgoalTimeArg + "')") # insert into SubgoalAtt for att in origSubgoalAttList: attID = att[0] attName = att[1] attType = att[2] cursor.execute("INSERT INTO SubgoalAtt VALUES ('" + newRID + "','" + subgoalID + "','" + str(attID) + "','" + attName + "','" + attType + "')") # ------------------------------------------- # # generate meta for new rule newRule = Rule.Rule(newRID, cursor) return newRule
def addSubgoalsToRules(domainRuleName, newDMRIDList, cursor): # -------------------------------------------- # # get goal att list for the dom_not_whatever # rule previously written in addDomainRules # -------------------------------------------- # # get rid for the domain rule cursor.execute("SELECT rid FROM Rule WHERE goalName=='" + domainRuleName + "'") rid = cursor.fetchone() if not rid or rid == "": tools.bp( __name__, inspect.stack()[0][3], "FATAL ERROR : writing domain subgoals, but no '" + domainRuleName + "' rule exists. aborting...") else: rid = tools.toAscii_str(rid) # get the goal attribute list for the domain rule cursor.execute("SELECT attID,attName,attType FROM GoalAtt WHERE rid=='" + rid + "'") goalAttList = cursor.fetchall() goalAttList = tools.toAscii_multiList(goalAttList) for rid in newDMRIDList: # ----------------------------------- # # generate sid # # ----------------------------------- # sid = tools.getID() # ----------------------------------- # # fixed subgoal time arg to nothing # # ----------------------------------- # subgoalTimeArg = "" # ----------------------------------- # # insert subgoal metadata # # ----------------------------------- # cursor.execute("INSERT INTO Subgoals VALUES ('" + rid + "','" + sid + "','" + domainRuleName + "','" + subgoalTimeArg + "')") # ----------------------------------- # # insert subgoal att data # # ----------------------------------- # attID = 0 for att in goalAttList: attName = att[1] attType = att[2] cursor.execute("INSERT INTO SubgoalAtt VALUES ('" + rid + "','" + sid + "','" + str(attID) + "','" + attName + "','" + attType + "')") attID += 1
def addDomainRules(parentRID, sidInParent, parentName, posName, domainRuleName, posNameRIDs, newDMRIDList, cursor): # ---------------------------------------------- # # generate a new rid for this rule. # # ---------------------------------------------- # rid = tools.getID() # ---------------------------------------------- # # get goal name # # ---------------------------------------------- # goalName = domainRuleName # ---------------------------------------------- # # build and save all goal data # # ---------------------------------------------- # randomDMRID = newDMRIDList[0] univAttData = setGoal(rid, goalName, randomDMRID, newDMRIDList, cursor) # -------------------------------------------------- # # collect all universal subgoals for the domain rule # # -------------------------------------------------- # # return a array univSubgoals = collectUniversalBoundSubgoals(randomDMRID, parentRID, sidInParent, posName, parentName, univAttData, cursor) # ---------------------------------------------------- # # collect all existential subgoals for the domain rule # # ---------------------------------------------------- # # return a array exisSubgoals = collectExistentialBoundSubgoals(univSubgoals, posNameRIDs, cursor) #if goalName.startswith( "dom_not_node_from_not_log_from_missing_log" ) : # for sub in exisSubgoals : # print sub # tools.bp( __name__, inspect.stack()[0][3], "shit" ) # ---------------------------------------------------- # # save all subgoals # # ---------------------------------------------------- # saveAllSubgoals(rid, univSubgoals, exisSubgoals, cursor) # ---------------------------------------------- # # generate new rule meta objects to pass # # to provenance rewriter. # # ---------------------------------------------- # return Rule.Rule(rid, cursor)
def doDeMorgans( parentRID, ruleRIDs, cursor ) : print "===========================================" print "... running DO DEMORGANS from deMorgans ..." print "===========================================" # ----------------------------------------------------------- # # get data for naming stuff # ----------------------------------------------------------- # # parent name cursor.execute( "SELECT goalName FROM Rule WHERE rid='" + parentRID + "'" ) parentName = cursor.fetchall() parentName = tools.toAscii_str( parentName[0] ) # this IDB name cursor.execute( "SELECT goalName FROM Rule WHERE rid='" + ruleRIDs[0] + "'" ) thisIDBName = cursor.fetchall() thisIDBName = tools.toAscii_str( thisIDBName[0] ) # ----------------------------------------------------------- # # combine all rules for each rule name into a single # sympy formula string # ----------------------------------------------------------- # # associate each subgoal per rule for this IDB definition with an identifier # and map to [ rid, [ sign, sid ] ] arrays predicateToID_map = {} # map rids,sid pairs to predicate identifiers (essentially the reverse of the above mapping) ridSidToPredicate_map = {} # ////////////////////////////////////////// # # get all rule data for rid in ruleRIDs : # get list of all sids for this rid # filter out sids for domain subgoals cursor.execute( "SELECT sid FROM Subgoals WHERE rid=='" + rid + "' AND NOT subgoalName LIKE 'dom_%'" ) sidList = cursor.fetchall() sidList = tools.toAscii_list( sidList ) # map sids to sign for this rule signMap = {} for sid in sidList : cursor.execute( "SELECT argName FROM SubgoalAddArgs WHERE rid='" + rid + "' AND sid='" + sid + "'" ) sign = cursor.fetchone() if sign : sign = tools.toAscii_str( sign ) signMap[ sid ] = sign for sid in sidList : sign = signMap[ sid ] # get a random identifier string predID = tools.getID() # map predIDs to rids and sids predicateToID_map[ predID ] = [ rid, [ sign, sid ] ] # map rids and sids to predIDs if sign == "notin" : key = rid + ",_NEG_" + sid else : key = rid + "," + sid ridSidToPredicate_map[ key ] = predID # ----------------------------------------------------------- # # simplify DNF simplified_negFmla = getDNFFmla_v1( ridSidToPredicate_map ) #simplified_negFmla = getDNFFmla_v2( ridSidToPredicate_map ) # ----------------------------------------------------------- # # save a new rule to IR db per disjunct newDMRIDList = setNewRules( parentName, thisIDBName, simplified_negFmla, predicateToID_map, cursor ) # ----------------------------------------------------------- # return newDMRIDList
def setNewRules( parentName, ruleName, simplified_negFmla, predicateToID_map, cursor ) : if NEGATIVEWRITES_DEBUG : print " ... running set new rules ..." #print "simplified_negFmla = " + str( simplified_negFmla ) #tools.dumpAndTerm( cursor ) # -------------------------------------------------------------------- # # initialize local data collection structures # # -------------------------------------------------------------------- # newName = "not_" + ruleName + "_from_" + parentName newGoalAtts = [] # populate with a uniform set of variables and propogate the set among all DM rules. newRIDs = [] # list of the rids for the new DM rules. goalAttMapper = {} # maintain a consistent set of goal attribute strings across all DM rules. # -------------------------------------------------------------------- # # populate goal attribute mapper with uniform attribute strings. # get an rid for this rule cursor.execute( "SELECT rid FROM Rule WHERE goalName='" + ruleName + "'" ) ridList = cursor.fetchall() ridList = tools.toAscii_list( ridList ) pickedRID = ridList[0] # get original attribute list. This is authoratative b/c # setting uniform variable scheme prior to applying DeMorgan's rewrites. cursor.execute( "SELECT attID,attName,attType FROM GoalAtt WHERE rid='" + pickedRID + "'" ) origAttList = cursor.fetchall() origAttList = tools.toAscii_multiList( origAttList ) # fill goalAttMapper with attID : uniform string for att in origAttList : attID = att[0] attName = att[1] goalAttMapper[ attID ] = attName # -------------------------------------------------------------------- # # get all clauses for this rule # # -------------------------------------------------------------------- # negated_simplified_fmla_str = str( simplified_negFmla ) negated_simplified_fmla_str = negated_simplified_fmla_str.translate( None, string.whitespace) negated_simplified_fmla_str = negated_simplified_fmla_str.replace( "(", "" ) negated_simplified_fmla_str = negated_simplified_fmla_str.replace( ")", "" ) clauses = negated_simplified_fmla_str.split( "|" ) # -------------------------------------------------------------------- # # get predicate (subgoal) ID mapping for this rule # # predicateToID_map[ predID ] = [ rid, [ sign, sid ] ] # # -------------------------------------------------------------------- # predMap = predicateToID_map # -------------------------------------------------------------------- # # spawn one new not_ rule per clause # # -------------------------------------------------------------------- # for c in clauses : # get an id for the new rule newRID = tools.getID() newRIDs.append( newRID ) # grab the list of literals in this clause literalList = c.split( "&" ) # iterate over literal list to construct the string representation for literal in literalList : # remove negation from string and record a notin for this subgoal. if "~" in literal : predicate = literal.replace( "~", "" ) addArg = "notin" # no negation means do nothing. else : predicate = literal addArg = None # grab the parent rid, sign, and sid for this subgoal # represented by this literal in the Boolean formula. predData = predMap[ predicate ] rid = predData[0] sign = predData[1][0] sid = predData[1][1] # grab info regarding the original rule. origRule = Rule.Rule( rid, cursor ) origRule_typeMap = origRule.getAllAttTypes() origRule_goalAttList = origRule.getGoalAttList() # -------------------------------------------- # # get subgoal info # # -------------------------------------------- # # get name and time arg for this subgoal from the original rule. cursor.execute( "SELECT subgoalName,subgoalTimeArg FROM Subgoals WHERE rid='" + rid + "' AND sid='" + sid + "'" ) data = cursor.fetchone() data = tools.toAscii_list( data ) subgoalName = data[0] try : subgoalTimeArg = data[1] except IndexError : subgoalTimeArg = "" #print "here" #tools.dumpAndTerm( cursor ) # get subgoal attribute list cursor.execute( "SELECT attID,attName,attType FROM SubgoalAtt WHERE rid='" + rid + "' AND sid='" + sid + "'" ) subgoalAtts = cursor.fetchall() subgoalAtts = tools.toAscii_multiList( subgoalAtts ) # -------------------------------------------- # # save subgoal with the rid of the new rule # # -------------------------------------------- # # create new sid newSID = tools.getID() # save subgoal name and time arg cursor.execute( "INSERT INTO Subgoals VALUES ('" + newRID + "','" + newSID + "','" + subgoalName.lower() + "','" + subgoalTimeArg + "')" ) # save subgoal attributes for att in subgoalAtts : attID = att[0] attName = att[1] attType = att[2] goalAttNames = [ x[0] for x in newGoalAtts ] # ----------------------------------------------------- # # check if atts appear in goal atts # # if so, get the corresponding attID from goal att list # # ----------------------------------------------------- # goalAttID = None if attName in origRule_goalAttList : goalAttID = origRule_goalAttList.index( attName ) # ----------------------------------------------------- # if attName == "_" : # insert cursor.execute( "INSERT INTO SubgoalAtt VALUES ('" + newRID + "','" + newSID + "','" + str( attID ) + "','" + attName + "','" + attType + "')" ) continue if not attType == "UNDEFINEDTYPE" and not attName in goalAttNames : newGoalAtts.append( [ goalAttID, attName, attType ] ) elif not attName in goalAttNames : if not attType == "UNDEFINEDTYPE" : newGoalAtts.append( [ goalAttID, attName, attType ] ) else : print "origRule_typeMap : " + str( origRule_typeMap ) attType = origRule_typeMap[ attName ] newGoalAtts.append( [ goalAttID, attName, attType ] ) # replace with uniform goal att str, if applicable if not goalAttID == None : attName = goalAttMapper[ goalAttID ] # insert cursor.execute( "INSERT INTO SubgoalAtt VALUES ('" + newRID + "','" + newSID + "','" + str( attID ) + "','" + attName + "','" + attType + "')" ) #print "completed insert" #print "--------------------" #print "c = " + str( c ) #print "origRule : " + dumpers.reconstructRule( rid, cursor ) #print "subgoalName = " + subgoalName #print "subgoalTimeArg = " + subgoalTimeArg #print "subgoalAtts = " + str( subgoalAtts ) #print "origRule_goalAttList = " + str( origRule_goalAttList ) #print "att = " + str( att ) #print "goalAttID = " + str( goalAttID ) #tools.bp( __name__, inspect.stack()[0][3], "breakhere." ) # save subgoal additional args if addArg : cursor.execute("INSERT INTO SubgoalAddArgs VALUES ('" + newRID + "','" + newSID + "','" + str( addArg ) + "')") else : cursor.execute("INSERT INTO SubgoalAddArgs VALUES ('" + newRID + "','" + newSID + "','')") # -------------------------------------------- # # save new goal data for newRID in newRIDs : # save new goal name and rewritten status timeArg = "" rewrittenFlag = True cursor.execute( "INSERT INTO Rule (rid, goalName, goalTimeArg, rewritten) VALUES ('" + newRID + "','" + newName + "','" + timeArg + "','" + str(rewrittenFlag) + "')" ) # save new goal attributes #prevInserts = [] #for attData in newGoalAtts : # goalAttID = attData[0] # attName = attData[1] # attType = attData[2] # if not attName == "_" and not goalAttID == None and not goalAttID in prevInserts : # cursor.execute( "INSERT INTO GoalAtt VALUES ('" + newRID + "','" + str(goalAttID) + "','" + goalAttMapper[goalAttID ] + "','" + attType + "')" ) # prevInserts.append( goalAttID ) for attData in origAttList : goalAttID = attData[0] attName = attData[1] attType = attData[2] cursor.execute( "INSERT INTO GoalAtt VALUES ('" + newRID + "','" + str(goalAttID) + "','" + attName + "','" + attType + "')" ) # --------------------------------------------------------------------- # return newRIDs
def aggProv(aggRule, nameAppend, cursor): # create bindings rule (see LDFI paper section 4.1.2) bindingsRule = regProv(aggRule, nameAppend, cursor) # generate random ID rid = tools.getID() # initialize firings rule firingsRule = Rule.Rule(rid, cursor) # goal info goalName = aggRule.getGoalName() + "_prov" + str( rid) # allows tables for duplicate names goalAttList = aggRule.getGoalAttList() goalTimeArg = "" rewrittenFlag = 0 # new rules have not yet been rewritten # check for bugs if PROVENANCEREWRITE_DEBUG: print "aggProv: goalName = " + goalName print "aggProv: goalAttList = " + str(goalAttList) # subgoal list info subgoalName = bindingsRule.getGoalName() subgoalAttList_init = bindingsRule.getGoalAttList() # check for bugs if PROVENANCEREWRITE_DEBUG: print "aggProv: subgoalName = " + subgoalName print "aggProv: subgoalAttList_init = " + str(subgoalAttList_init) aggAtts = [] for att in goalAttList: containsAgg = False for op in aggOps: if op in att: containsAgg = True if containsAgg: att = att.split("<") att = att[1].replace(">", "") aggAtts.append(att) subgoalAttList_final = [] for att in subgoalAttList_init: for a in aggAtts: if a == att: subgoalAttList_final.append("_") else: subgoalAttList_final.append(att) # check for bugs if PROVENANCEREWRITE_DEBUG: print "aggProv: subgoalAttList_final = " + str(subgoalAttList_final) sid = tools.getID() subgoalTimeArg = "" subgoalAddArgs = "" # save rule firingsRule.setGoalInfo(goalName, goalTimeArg, rewrittenFlag) firingsRule.setGoalAttList(goalAttList) firingsRule.setSingleSubgoalInfo(sid, subgoalName, subgoalTimeArg) firingsRule.setSingleSubgoalAttList(sid, subgoalAttList_final) firingsRule.setSingleSubgoalAddArgs(sid, subgoalAddArgs) # ----------------------------------------------------------- # # set goal attribute types for all rules firingsRule.setAttTypes()
def regProv(regRule, nameAppend, cursor): if PROVENANCEREWRITE_DEBUG: print " ... running regProv ..." #sys.exit( "BREAKPOINT: regRule = " + str(regRule.getSubgoalListStr()) ) # -------------------------------------------------- # # parse rule parsedRule = dedalusParser.parse(regRule.display()) #sys.exit( "BREAKPOINT: regRule.display() = " + str( regRule.display() ) + "\nparsedRule = " + str(parsedRule) ) # -------------------------------------------------- # # generate random ID for new rule rid = tools.getID() # -------------------------------------------------- # # initialize new rule firingsRule = Rule.Rule(rid, cursor) # -------------------------------------------------- # # get goal info goalName = regRule.getGoalName() + nameAppend + str(rid) goalTimeArg = "" rewrittenFlag = 1 # new log rules are not rewritten # check for bugs if PROVENANCEREWRITE_DEBUG: print "regProv: goalName = " + goalName # -------------------------------------------------- # # get subgoal array subgoalArray = extractors.extractSubgoalList(parsedRule[1]) # ................................... # #if goalName.startswith( "pre_prov" ) : # tools.bp( __name__, inspect.stack()[0][3], "subgoalArray = " + str(subgoalArray) ) # ................................... # # check for bugs if PROVENANCEREWRITE_DEBUG: print "regProv: subgoalArray = " + str(subgoalArray) # -------------------------------------------------- # # collect goal args while inserting subgoals firstSubgoalAtts = [] goalAttList = [] # -------------------------------------------------- # # populate with original goal atts first goalAttList = regRule.getGoalAttList() if PROVENANCEREWRITE_DEBUG: print ">>>>>>>> goalAttList init = " + str(goalAttList) # -------------------------------------------------- # # then populate with remaining subgoal atts for subgoal in subgoalArray: # generate random ID for subgoal sid = tools.getID() # -------------------------------------------------- # # extract info subgoalName = extractors.extractSubgoalName(subgoal) subgoalAttList = extractors.extractAttList(subgoal) # returns list subgoalTimeArg = "" subgoalAddArgs = extractors.extractAdditionalArgs( subgoal) # returns list # check for bugs if PROVENANCEREWRITE_DEBUG: print "regProv: subgoal = " + str(subgoal) print "regProv: subgoalName = " + subgoalName print "regProv: subgoalAttList = " + str(subgoalAttList) print "regProv: subgoalAddArgs = " + str(subgoalAddArgs) # -------------------------------------------------- # # catch first subgoal att if len(subgoalAddArgs) == 0: firstSubgoalAtts.append(subgoalAttList[0]) # -------------------------------------------------- # # populate goal attribute list for att in subgoalAttList: if (not att in goalAttList) and (not att.isdigit()) and ( not att == "_"): # exclude numbers from goal atts goalAttList.append(att) # -------------------------------------------------- # # make sure time attribute appears as rightmost attribute if not goalAttList == None: for att in goalAttList: # make sure time attribute(s) appear in the rightmost columns if "SndTime" in att: goalAttList = [x for x in goalAttList if x != att] goalAttList.append(att) # -------------------------------------------------- # # save firings subgoal firingsRule.setSingleSubgoalInfo(sid, subgoalName, subgoalTimeArg) firingsRule.setSingleSubgoalAttList(sid, subgoalAttList) firingsRule.setSingleSubgoalAddArgs(sid, subgoalAddArgs) if PROVENANCEREWRITE_DEBUG: print ">>>>>>>> goalAttList final = " + str(goalAttList) # -------------------------------------------------- # # get eqn array eqnArray = regRule.getEquationListArray() # save firings rule equations for eqn in eqnArray: # generate random ID eid = tools.getID() # save eqn firingsRule.setSingleEqn(eid, eqn) # -------------------------------------------------- # # save firings rule goal firingsRule.setGoalInfo(goalName, goalTimeArg, rewrittenFlag) firingsRule.setGoalAttList(goalAttList) # ----------------------------------------------------------- # # set goal attribute types for all rules firingsRule.setAttTypes() return firingsRule
def test_getID_tools(self): outputResult = 16 self.assertEqual(len(tools.getID()), outputResult) self.assertTrue(tools.getID().isalpha()) self.assertTrue(tools.getID().islower())
def addDomainEDBs(original_prog, cursor): # --------------------------------------------------- # # run original program and grab results dictionary # # --------------------------------------------------- # results_array = c4_evaluator.runC4_wrapper(original_prog) parsedResults = tools.getEvalResults_dict_c4(results_array) # --------------------------------------------------- # # save results as edb facts in IR db # # --------------------------------------------------- # for relationName in parsedResults: print ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;" print "relationName : " + relationName # build fact name newEDBName = "dom_" + relationName + "_evalres" print "newEDBName : " + newEDBName # ---------------------------------------------------------- # # no evaluation data exists for this relation if parsedResults[relationName] == []: # set fact info fid = tools.getID() timeArg += 1 print ">>> INSERT INTO Fact VALUES ('" + fid + "','" + newEDBName + "','" + str( timeArg) + "')" cursor.execute("INSERT INTO Fact VALUES ('" + fid + "','" + newEDBName + "','" + str(timeArg) + "')") # get fact schema # observe a relation defined in a dedalust program can only be empty in this context if it is an IDB # because c4 define statements are derived from relations defined in the dedalus program only. cursor.execute("SELECT rid FROM Rule WHERE goalName=='" + relationName + "'") ridList = cursor.fetchall() ridList = tools.toAscii_list(ridList) pickedRID = ridList[0] cursor.execute( "SELECT attID,attName,attType FROM GoalAtt WHERE rid=='" + pickedRID + "'") attData = cursor.fetchall() attData = tools.toAscii_multiList(attData) # set fact data fattID = 0 for att in attData: attID = att[0] attName = att[1] attType = att[2] if attType == "string": d = '"___DEFAULTSTR___"' else: d = 9999999999 print ">>> INSERT INTO FactAtt (" + fid + "," + str( fattID) + "," + str(d) + "," + attType + ")" cursor.execute("INSERT INTO FactAtt VALUES ('" + fid + "','" + str(fattID) + "','" + str(d) + "','" + str(attType) + "')") fattID += 1 #tools.bp( __name__, inspect.stack()[0][3], "blah" ) # ---------------------------------------------------------- # # evaluation data exists for this relation else: for dataTup in parsedResults[relationName]: # build fact id fid = tools.getID() # default time arg to 1. domain edb facts are true starting at time 1. timeArg = 1 # set fact info print ">>> INSERT INTO Fact VALUES ('" + fid + "','" + newEDBName + "','" + str( timeArg) + "')" cursor.execute("INSERT INTO Fact VALUES ('" + fid + "','" + newEDBName + "','" + str(timeArg) + "')") fattID = 0 for d in dataTup: # get data type for d if d.isdigit(): attType = "int" else: attType = "string" # modify string data with quotes if attType == "string": d = '"' + d + '"' # set fact data print ">>> INSERT INTO FactAtt (" + fid + "," + str( fattID) + "," + str(d) + "," + attType + ")" cursor.execute("INSERT INTO FactAtt VALUES ('" + fid + "','" + str(fattID) + "','" + str(d) + "','" + str(attType) + "')") fattID += 1
def collectExistentialBoundSubgoals(universalBoundSubgoals, posNameRIDs, cursor): print print "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" print "BUILDING EXISTENTIAL BOUND SUBGOALS" print "universalBoundSubgoals : " for sub in universalBoundSubgoals: print sub print "posNameRIDs :" for r in posNameRIDs: print dumpers.reconstructRule(r, cursor) existentialBoundSubgoals = [] # -------------------------- # # extract all universal vars # # -------------------------- # univars = [] for subgoalDict in universalBoundSubgoals: subgoalAttDict = subgoalDict["subgoalAttDict"] for subindex in subgoalAttDict: attData = subgoalAttDict[subindex] attVar = attData[1] if not attVar in univars and not attVar == "_": univars.append(attVar) # ---------------------------------------------------------------------------------------- # # collect all subgoals from pos rules with all universal variables replaced with wildcards # # ---------------------------------------------------------------------------------------- # for posrid in posNameRIDs: print "<><> posNameRIDs rule : " + dumpers.reconstructRule( posrid, cursor) # get all sids cursor.execute("SELECT sid FROM Subgoals WHERE rid=='" + posrid + "'") sidList = cursor.fetchall() sidList = tools.toAscii_list(sidList) for possid in sidList: # ---------------- # # get subgoal name # # ---------------- # cursor.execute("SELECT subgoalName FROM Subgoals WHERE rid=='" + posrid + "' AND sid=='" + possid + "'") exisSubgoalName = cursor.fetchone() exisSubgoalName = tools.toAscii_str(exisSubgoalName) # ------------------------- # # get subgoal time argument # # ------------------------- # cursor.execute("SELECT subgoalTimeArg FROM Subgoals WHERE rid=='" + posrid + "' AND sid=='" + possid + "'") exisSubgoalTimeArg = cursor.fetchone() exisSubgoalTimearg = tools.toAscii_str(exisSubgoalTimeArg) if type(exisSubgoalTimeArg) is tuple: exisSubgoalTimeArg = exisSubgoalTimeArg[0] # ------------------- # # get subgoal att map # # ------------------- # # ====================================== # # CASE : subgoal is an EDB # # ====================================== # if tools.isFact_only(exisSubgoalName, cursor): if exisSubgoalName == "clock": # just need to get the data for one clock fact cursor.execute( "SELECT src,dest,sndTime,delivTime FROM Clock") fact = cursor.fetchall() fact = tools.toAscii_multiList(fact) fact = fact[0] factData = {} attID = 0 for d in fact: attName = d if type(d) is int: attType = "int" elif d.isdigit(): attType = "int" else: attType = "string" factData[attID] = [attID, attName, attType] attID += 1 #tools.bp( __name__, inspect.stack()[0][3], "factData : " + str( factData ) ) else: # get an fid for this edb cursor.execute("SELECT fid FROM Fact WHERE name=='" + exisSubgoalName + "'") thisSubgoalEDBFID = cursor.fetchone() thisSubgoalEDBFID = tools.toAscii_str(thisSubgoalEDBFID) # get fact data cursor.execute( "SELECT attID,attName,attType FROM FactAtt WHERE fid=='" + thisSubgoalEDBFID + "'") factData = cursor.fetchall() factData = tools.toAscii_multiList(factData) # get subgoal attribute data cursor.execute( "SELECT attID,attName,attType FROM SubgoalAtt WHERE rid=='" + posrid + "' AND sid=='" + possid + "'") subAttData = cursor.fetchall() subAttData = tools.toAscii_multiList(subAttData) # map subgoal attributes to att types from the corresponding fact schema attData = {} for i in range(0, len(factData)): fact = factData[i] subatt = subAttData[i] attID = fact[0] attName = subatt[1] attType = fact[2] attData[attID] = [attID, attName, attType] # ====================================== # # CASE : suboal is an IDB # # ====================================== # else: # get original name orig_name = exisSubgoalName.split("_from") orig_name = orig_name[0] # remove stuff from _from and after if orig_name.startswith("not_"): orig_name = orig_name[4:] print "orig_name = " + str(orig_name) # get an rid for this idb #cursor.execute( "SELECT rid FROM Rule WHERE goalName=='" + exisSubgoalName + "'" ) cursor.execute("SELECT rid FROM Rule WHERE goalName=='" + orig_name + "'") thisSubgoalIDBRID = cursor.fetchone() thisSubgoalIDBRID = tools.toAscii_str(thisSubgoalIDBRID) # get att data from IDB definition cursor.execute( "SELECT attID,attName,attType FROM GoalAtt WHERE rid=='" + thisSubgoalIDBRID + "'") orig_subgoalAttList = cursor.fetchall() orig_subgoalAttList = tools.toAscii_multiList( orig_subgoalAttList) # get att data from subgoal from positive rule cursor.execute( "SELECT attID,attName,attType FROM SubgoalAtt WHERE rid=='" + posrid + "' AND sid=='" + possid + "'") subgoal_subgoalAttList = cursor.fetchall() subgoal_subgoalAttList = tools.toAscii_multiList( subgoal_subgoalAttList) attData = {} for i in range(0, len(orig_subgoalAttList)): orig_att = orig_subgoalAttList[i] sub_att = subgoal_subgoalAttList[i] attID = orig_att[0] attName = sub_att[1] attType = orig_att[2] attData[attID] = [attID, attName, attType] print "attData : " + str(attData) exisSubgoalAttMap = {} for attID in attData: att = attData[attID] attID = att[0] attName = att[1] attType = att[2] if attName in univars and not attName == "_": attName = "_" exisSubgoalAttMap[attID] = [attID, attName, attType] # ------------------- # # get subgoal add arg # # ------------------- # cursor.execute("SELECT argName FROM SubgoalAddArgs WHERE rid=='" + posrid + "' AND sid=='" + possid + "'") exisAddArg = cursor.fetchone() if exisAddArg: exisAddArg = tools.toAscii_str(exisAddArg) else: exisAddArg = "" # ------------------------------ # # build negated positive subgoal # # ------------------------------ # exisSubgoal = {} exisSubgoal["sid"] = tools.getID() exisSubgoal["subgoalName"] = exisSubgoalName exisSubgoal["subgoalTimeArg"] = exisSubgoalTimeArg exisSubgoal["subgoalAttDict"] = exisSubgoalAttMap exisSubgoal["argName"] = exisAddArg existentialBoundSubgoals.append(exisSubgoal) print "DONE BUILDING EXISTENTIAL BOUND SUBGOALS" return existentialBoundSubgoals
def collectUniversalBoundSubgoals(randomDMRID, parentRID, sidInParent, posName, parentName, univAttData, cursor): print print "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" print "BUILDING UNIVERSAL BOUND SUBGOALS" print "randomDMRID rule : " + dumpers.reconstructRule(randomDMRID, cursor) print "parentRID rule : " + dumpers.reconstructRule(parentRID, cursor) print "sidInParent : " + sidInParent print "posName : " + posName print "parentName : " + parentName universalBoundSubgoals = [] # ===================================================================== # # ===================================================================== # # ----------------------------------- # # build parent dom subgoal # # ----------------------------------- # parentSubgoalName = "dom_" + parentName + "_evalres" print "parentSubgoalName : " + parentSubgoalName # ---------------------------------------------- # # get parent rule attribute list # # ---------------------------------------------- # cursor.execute("SELECT attID,attName,attType FROM GoalAtt WHERE rid=='" + parentRID + "'") parentGoalAttList = cursor.fetchall() parentGoalAttList = tools.toAscii_multiList(parentGoalAttList) # ---------------------------------------------- # # get negated subgoal attribute list from parent # # ---------------------------------------------- # cursor.execute( "SELECT attID,attName,attType FROM SubgoalAtt WHERE rid=='" + parentRID + "' AND sid=='" + sidInParent + "'") subAttDataInParent = cursor.fetchall() subAttDataInParent = tools.toAscii_multiList(subAttDataInParent) # --------------------------------------------------------------- # # map parent goal att indexes to subgoal att indexes or wildcards # # --------------------------------------------------------------- # parentSubgoalAttIndexMap = {} for paratt in parentGoalAttList: pattID = paratt[0] pattName = paratt[1] subIndexes = [] for subatt in subAttDataInParent: sattID = subatt[0] sattName = subatt[1] if pattName == sattName: subIndexes.append(sattID) parentSubgoalAttIndexMap[pattID] = subIndexes # ------------------------------------------------------------------------------------------------------ # # map parent subgoal att vars to the corresponding att vars in the uniform attribute set for the DM rule # # ------------------------------------------------------------------------------------------------------ # # get uniform attribute list cursor.execute("SELECT attID,attName,attType FROM GoalAtt WHERE rid=='" + randomDMRID + "'") uniformAttList = cursor.fetchall() uniformAttList = tools.toAscii_multiList(uniformAttList) # populate map subToUniformMap = {} for i in range(0, len(subAttDataInParent)): subatt = subAttDataInParent[i] subAttName = subatt[1] subToUniformMap[subAttName] = uniformAttList[i] # ---------------------------------------------------------------------------------- # # map parent goal att vars to vars from the uniform att list for the negated subgoal # # ---------------------------------------------------------------------------------- # parentSubgoalAttMap = {} for parAttIndex in parentSubgoalAttIndexMap: correspondingSubAttIndexList = parentSubgoalAttIndexMap[parAttIndex] # this attribute in the parent goal does not appear in the negated subgoal att list. # use a wildcard. if correspondingSubAttIndexList == []: parAttType = parentGoalAttList[parAttIndex][2] parentSubgoalAttMap[parAttIndex] = [parAttIndex, "_", parAttType] # this attribute in the parent goal appears in the negated subgoal att list. # replace with the corresponding uniform attribute variable for the subgoal rewrite. else: for subAttIndex in correspondingSubAttIndexList: parentRuleSubAttVar = subAttDataInParent[subAttIndex][1] uniformVar = subToUniformMap[parentRuleSubAttVar] parentSubgoalAttMap[ parAttIndex] = uniformVar # should be identical across subgoal indexes # -------------------- # # build parent subgoal # # -------------------- # # save subgoal contents in a dictionary parentSubgoal = {} parentSubgoal["sid"] = tools.getID() parentSubgoal["subgoalName"] = parentSubgoalName parentSubgoal["subgoalTimeArg"] = "" # default parentSubgoal["subgoalAttDict"] = parentSubgoalAttMap parentSubgoal["argName"] = "" # ============================================================== # # ============================================================== # # ----------------------------------- # # build original dom subgoal # # ----------------------------------- # negatedPositiveSubgoalName = "dom_" + posName + "_evalres" # ----------------------------------------------- # # map parent subgoal att vars to uniform att vars # # ----------------------------------------------- # # correctness depends upon the schemas of the positive and not_ # definitions of the targeted negated subgoal to be identical. # this can only be guaranteed after the uniformity rewrite. negatedPositiveSubgoalAttMap = {} for att in subAttDataInParent: attID = att[0] attName = att[1] if attName == "_": attType = uniformAttList[attID][2] negatedPositiveSubgoalAttMap[attID] = [attID, "_", attType] else: attName = uniformAttList[attID][1] attType = uniformAttList[attID][2] negatedPositiveSubgoalAttMap[attID] = [attID, attName, attType] # ------------------------------ # # build negated positive subgoal # # ------------------------------ # negatedPositiveSubgoal = {} negatedPositiveSubgoal["sid"] = tools.getID() negatedPositiveSubgoal["subgoalName"] = negatedPositiveSubgoalName negatedPositiveSubgoal["subgoalTimeArg"] = "" # default negatedPositiveSubgoal["subgoalAttDict"] = negatedPositiveSubgoalAttMap negatedPositiveSubgoal["argName"] = "notin" # negated positive # ============================================================== # # ============================================================== # # ----------------------------------- # # build original dom subgoal # # ----------------------------------- # positivePositiveSubgoalName = "dom_" + posName + "_evalres" # ----------------------------------------------- # # map parent subgoal att vars to uniform att vars # # ----------------------------------------------- # # correctness depends upon the schemas of the positive and not_ # definitions of the targeted negated subgoal to be identical. # this can only be guaranteed after the uniformity rewrite. # # the attributes in the positive domain subgoal for the targetted negated relation # are exactly the in the negated domain subgoal relation in this rule # minus the attributes incoporated into the parent domain subgoal of this rule. parentEvalRes_atts_only = [ parentSubgoalAttMap[x][1] for x in parentSubgoalAttMap ] positivePositiveSubgoalAttMap = {} for att in univAttData: attID = att[0] attName = att[1] attType = att[2] if attName in parentEvalRes_atts_only: attName = "_" positivePositiveSubgoalAttMap[attID] = [attID, attName, attType] # ------------------------------ # # build negated positive subgoal # # ------------------------------ # positivePositiveSubgoal = {} positivePositiveSubgoal["sid"] = tools.getID() positivePositiveSubgoal["subgoalName"] = positivePositiveSubgoalName positivePositiveSubgoal["subgoalTimeArg"] = "" # default positivePositiveSubgoal["subgoalAttDict"] = positivePositiveSubgoalAttMap positivePositiveSubgoal["argName"] = "" # positive positive # ============================================================== # # ============================================================== # universalBoundSubgoals = [] if not allWildcardAtts(parentSubgoal): universalBoundSubgoals.append(parentSubgoal) if not allWildcardAtts(negatedPositiveSubgoal): universalBoundSubgoals.append(negatedPositiveSubgoal) if not allWildcardAtts(positivePositiveSubgoal): universalBoundSubgoals.append(positivePositiveSubgoal) print "DONE BUILDING UNIVERSAL BOUND SUBGOALS" return universalBoundSubgoals