Exemple #1
0
    def typeMatch(self, cursor, programData):
        # grab all rules type information
        cursor.execute("SELECT rid FROM Rule")
        rids = cursor.fetchall()
        rids = tools.toAscii_list(rids)
        ruleTypes = {}
        for rid in rids:
            r = dumpers.singleRuleAttDump(rid, cursor)
            ruleTypes[r['goalName']] = r

        # grab all facts
        cursor.execute("SELECT fid FROM Fact")
        fids = cursor.fetchall()
        fids = tools.toAscii_list(fids)
        factTypes = {}
        for fid in fids:
            # working out the fact match still
            continue

        # loop through and check matches between goals and subgoals
        for rid, rule in ruleTypes.iteritems():
            for subgoal in rule['subgoalAttData']:
                if subgoal[1] not in ruleTypes.keys():
                    # working out the fact matching still
                    continue
                matchingGoal = ruleTypes[subgoal[1]]
                for i in range(0, len(subgoal[2])):
                    attr = subgoal[2][i]
                    matchingAttr = matchingGoal['goalAttData'][i]
                    self.assertEqual(attr[2], matchingAttr[2])
def rewriteNegativeSubgoalsWithWildcards(cursor):

    newRuleMeta = []

    # ------------------------------------------- #
    # get all rids
    cursor.execute("SELECT rid FROM RUlE")
    rids = cursor.fetchall()
    rids = tools.toAscii_list(rids)

    for rid in rids:
        cursor.execute("SELECT sid FROM Subgoals WHERE rid=='" + rid + "'")
        sids = cursor.fetchall()
        sids = tools.toAscii_list(sids)

        # ------------------------------------------- #
        # examine all subgoals per rule
        for sid in sids:

            cursor.execute("SELECT argName FROM SubgoalAddArgs WHERE rid=='" +
                           rid + "' AND sid=='" + sid + "'")
            sign = cursor.fetchone()
            if sign:
                sign = tools.toAscii_str(sign)
            else:
                sign = ""

            if sign == "notin":
                cursor.execute(
                    "SELECT attID,attName,attType FROM SubgoalAtt WHERE rid=='"
                    + rid + "' AND sid=='" + sid + "'")
                attData = cursor.fetchall()
                attData = tools.toAscii_multiList(attData)

                # ------------------------------------------- #
                # examine all atts per subgoal per rule
                for att in attData:
                    attID = att[0]
                    attName = att[1]
                    attType = att[2]

                    if attName == "_":
                        newRule = rewriteRule(rid, sid, attData, cursor)
                        newRuleMeta.append(newRule)
                        break

            else:
                continue

    return newRuleMeta
Exemple #3
0
def dumpSingleFact_c4(fid, cursor):
    fact = ""

    cursor.execute("SELECT name FROM Fact WHERE fid == '" + fid +
                   "'")  # get fact name
    factName = cursor.fetchone()
    factName = tools.toAscii_str(factName)

    # get list of attribs in fact
    factList = cursor.execute("SELECT data FROM FactData WHERE fid == '" +
                              fid + "'")  # list of fact atts
    factList = cursor.fetchall()
    factList = tools.toAscii_list(factList)

    # get fact time arg
    #factTimeArg = ""
    #cursor.execute( "SELECT timeArg FROM Fact WHERE fid == '" + fid + "'" )
    #factTimeArg = cursor.fetchone()
    #factTimeArg = tools.toAscii_str( factTimeArg )

    # convert fact info to pretty string
    fact += factName + "("
    for j in range(0, len(factList)):
        if j < (len(factList) - 1):
            fact += factList[j] + ","
        else:
            fact += factList[j]
    #if not factTimeArg == "" :
    #  fact += "," + factTimeArg

    fact += ");" + "\n"  # end all facts with a semicolon

    return fact
Exemple #4
0
def dumpIR(cursor, db_dump_save_path):

    # get facts
    cursor.execute("SELECT fid FROM Fact")
    fid_all = cursor.fetchall()
    fid_all = tools.toAscii_list(fid_all)

    full_facts = []
    for fid in fid_all:
        full_facts.append(dumpSingleFact_c4(fid, cursor))

    # get rules
    cursor.execute("SELECT rid FROM Rule")
    rid_all = cursor.fetchall()
    rid_all = tools.toAscii_list(rid_all)

    full_rules = []
    for rid in rid_all:
        full_rules.append(dumpSingleRule_c4(rid, cursor))

    # get clock
    full_clock = dump_clock(cursor)

    if db_dump_save_path:
        if DUMPERS_C4_DEBUG:
            print "...DUMPING IR..."
            print full_facts
            print full_rules
            print full_clock

        # save to file
        fo = open(db_dump_save_path, "w")
        for fact in full_facts:  # write facts
            fo.write(fact)
        for rule in full_rules:  # write rules
            fo.write(rule)
        for clock in full_clock:  # write clock facts
            fo.write(clock)
        fo.close()

        print "IR DUMP SAVED TO : " + db_dump_save_path

    else:
        print "...DUMPING IR..."
        print full_facts
        print full_rules
        print full_clock
Exemple #5
0
def getGoallAtts(cursor, rid):
    ''' collects goalAtts '''
    cursor.execute('''SELECT ga.attName,
      ga.attType
    FROM GoalAtt ga
    WHERE ga.rid = "''' + rid + '"')
    goalAtt = cursor.fetchall()
    return tools.toAscii_list(goalAtt)
Exemple #6
0
def getDeductiveRuleIDs(cursor):
    # deductive rules are not next or async
    cursor.execute(
        "SELECT rid FROM Rule WHERE (NOT goalTimeArg == 'next') AND (NOT goalTimeArg == 'async') AND (NOT goalTimeArg == 'rewritten')"
    )
    ridList = cursor.fetchall()
    ridList = tools.toAscii_list(ridList)
    return ridList
Exemple #7
0
def getSubGoalAttList(cursor, rid, sid):
    ''' Collects infomration to create the subgoalAttList '''
    cursor.execute('''
    SELECT sa.attName
    FROM SubgoalAtt sa
    WHERE sa.rid = "''' + rid + '" AND sid = "' + str(sid) + '"')
    subGoalAtts = cursor.fetchall()
    return tools.toAscii_list(subGoalAtts)
Exemple #8
0
 def getName(self):
     self.cursor.execute("SELECT name FROM Fact WHERE fid = '" + self.fid +
                         "'")
     nameList = self.cursor.fetchall()
     nameList = tools.toAscii_list(nameList)
     if not nameList == None:
         if len(nameList) == 1:
             return nameList[0]
         else:
             sys.exit("ERROR: Fact possesses more than 1 name: " + nameList)
Exemple #9
0
 def test_toAscii_list_tools(self):
     unitestfile = testPath + "/testfiles/unicodetest.txt"
     with open(unitestfile, "r") as f:
         line = f.readlines()[0][6:]
         x = 0
         testmulti = []
         while x != 5:
             testmulti.append(line)
             x = x + 1
         self.assertFalse(tools.toAscii_list(testmulti) == None)
Exemple #10
0
 def getTimeArg(self):
     self.cursor.execute("SELECT timeArg FROM Fact WHERE fid = '" +
                         self.fid + "'")
     timeArgList = self.cursor.fetchall()
     timeArgList = tools.toAscii_list(timeArgList)
     if not timeArgList == None:
         if len(timeArgList) == 1:
             return timeArgList[0]
         else:
             sys.exit("ERROR: Fact possesses more than 1 timeArg: " +
                      timeArgList)
Exemple #11
0
def printRuleWithTypes( rid, cursor ) :

  logging.debug( "  PRINT RULE WITH TYPES : running process..." )

  printLine = ""

  # ----------------------------------------- #
  # get relation name

  cursor.execute( "SELECT goalName FROM Rule WHERE rid=='" + str( rid ) + "'" )
  goalName = cursor.fetchone()
  goalName = tools.toAscii_str( goalName )

  # ----------------------------------------- #
  # get goal att names and types

  cursor.execute( "SELECT attID,attName,attType FROM GoalAtt WHERE rid=='" + str( rid ) + "'" )
  gattData = cursor.fetchall()
  gattData = tools.toAscii_multiList( gattData )

  # build head for printing
  goal = goalName + "("
  for gatt in gattData :
    gattName = gatt[1]
    gattType = gatt[2]
    goal += "[" + gattName + "," + gattType + "]"
  goal += ")"

  printLine += goal + " :- "

  # ----------------------------------------- #
  # get all subgoal ids

  cursor.execute( "SELECT sid FROM Subgoals WHERE rid=='" + str( rid ) + "'" )
  sidList = cursor.fetchall()
  sidList = tools.toAscii_list( sidList )

  # ----------------------------------------- #
  # iterate over sids

  for sid in sidList :

    # ----------------------------------------- #
    # get subgoal name

    cursor.execute( "SELECT subgoalName FROM Subgoals WHERE rid=='" + str( rid ) + "' AND sid=='" + str( sid ) + "'" )
    subgoalName = cursor.fetchone()
    subgoalName = tools.toAscii_str( subgoalName )

    # ----------------------------------------- #
    # get subgoal att names and types

    cursor.execute( "SELECT attID,attName,attType FROM SubgoalAtt WHERE rid=='" + str( rid ) + "' AND sid=='" + str( sid ) + "'" )
    sattData = cursor.fetchall()
    sattData = tools.toAscii_multiList( sattData )

    # ----------------------------------------- #
    # build subgoal for printing

    subgoal = subgoalName + "("
    for satt in sattData :
      sattName = satt[1]
      sattType = satt[2]
      subgoal += "[" + sattName + "," + sattType + "]"
    subgoal += ")"

    printLine += subgoal + ","

  return printLine[:-1]
Exemple #12
0
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
Exemple #13
0
def dumpSingleRule_c4(rid, cursor):

    rule = ""

    # -------------------------------------------------------------- #
    #                           GOAL                                 #

    # get goal name
    cursor.execute("SELECT goalName FROM Rule WHERE rid == '" + rid +
                   "'")  # get goal name
    goalName = cursor.fetchone()
    goalName = tools.toAscii_str(goalName)

    # get list of attribs in goal
    goalList = cursor.execute("SELECT attName FROM GoalAtt WHERE rid == '" +
                              rid + "'")  # list of goal atts
    goalList = cursor.fetchall()
    goalList = tools.toAscii_list(goalList)

    # get goal time arg
    goalTimeArg = ""
    cursor.execute("SELECT goalTimeArg FROM Rule WHERE rid == '" + rid + "'")
    goalTimeArg = cursor.fetchone()
    goalTimeArg = tools.toAscii_str(goalTimeArg)

    # convert goal info to pretty string
    rule += goalName + "("
    for j in range(0, len(goalList)):
        if j < (len(goalList) - 1):
            rule += goalList[j] + ","
        else:
            rule += goalList[j] + ")"
    if not goalTimeArg == "":
        #rule += "@" + goalTimeArg + " :- "
        sys.exit("ERROR: leftover timeArg in goal: " + rule + "@" +
                 goalTimeArg)
    else:
        rule += " :- "

    # --------------------------------------------------------------- #
    #                         SUBGOALS                                #

    # get list of sids for the subgoals of this rule
    cursor.execute("SELECT sid FROM Subgoals WHERE rid == '" + str(rid) +
                   "'")  # get list of sids for this rule
    subIDs = cursor.fetchall()
    subIDs = tools.toAscii_list(subIDs)

    # prioritize dom subgoals first.
    subIDs = prioritizeDoms(rid, subIDs, cursor)

    # prioritize negated subgoals last.
    subIDs = prioritizeNegatedLast(rid, subIDs, cursor)

    subTimeArg = None
    # iterate over subgoal ids
    for k in range(0, len(subIDs)):
        newSubgoal = ""

        s = subIDs[k]

        # get subgoal name
        cursor.execute("SELECT subgoalName FROM Subgoals WHERE rid == '" +
                       str(rid) + "' AND sid == '" + str(s) + "'")
        subgoalName = cursor.fetchone()

        if not subgoalName == None:
            subgoalName = tools.toAscii_str(subgoalName)

            if DUMPERS_C4_DEBUG:
                print "subgoalName = " + subgoalName

            # get subgoal attribute list
            subAtts = cursor.execute(
                "SELECT attName FROM SubgoalAtt WHERE rid == '" + rid +
                "' AND sid == '" + s + "'")
            subAtts = cursor.fetchall()
            subAtts = tools.toAscii_list(subAtts)

            # get subgoal time arg
            cursor.execute(
                "SELECT subgoalTimeArg FROM Subgoals WHERE rid == '" + rid +
                "' AND sid == '" + s + "'")
            subTimeArg = cursor.fetchone()  # assume only one time arg
            subTimeArg = tools.toAscii_str(subTimeArg)

            #if goalName == "pre" and subgoalName == "bcast" :
            #  print "............................................"
            #  print dumpers.reconstructRule( rid, cursor )
            #  print "subgoalName = " + subgoalName
            #  print "subAtts     = " + str( subAtts )
            #  print "subTimeArg  = " + str( subTimeArg )
            #  tools.bp( __name__, inspect.stack()[0][3], "stuff" )

            # get subgoal additional args
            cursor.execute(
                "SELECT argName FROM SubgoalAddArgs WHERE rid == '" + rid +
                "' AND sid == '" + s + "'")
            subAddArg = cursor.fetchone()  # assume only one additional arg
            if not subAddArg == None:
                subAddArg = tools.toAscii_str(subAddArg)
                subAddArg += " "
                newSubgoal += subAddArg

            # all subgoals have a name and open paren
            newSubgoal += subgoalName + "("

            # add in all attributes
            for j in range(0, len(subAtts)):

                currAtt = subAtts[j]

                # replace SndTime in subgoal with subTimeArg, if applicable
                if not subTimeArg == "" and "SndTime" in currAtt:
                    currAtt = str(subTimeArg)

                if j < (len(subAtts) - 1):
                    newSubgoal += currAtt + ","
                else:
                    newSubgoal += currAtt + ")"

            # cap with a comma, if applicable
            if k < len(subIDs) - 1:
                newSubgoal += ","

        rule += newSubgoal

    # --------------------------------------------------------------- #
    #                         EQUATIONS                               #

    # get list of sids for the subgoals of this rule
    cursor.execute(
        "SELECT eid FROM Equation")  # get list of eids for this rule
    eqnIDs = cursor.fetchall()
    eqnIDs = tools.toAscii_list(eqnIDs)

    for e in range(0, len(eqnIDs)):
        currEqnID = eqnIDs[e]

        # get associated equation
        if not currEqnID == None:
            cursor.execute("SELECT eqn FROM Equation WHERE rid == '" + rid +
                           "' AND eid == '" + str(currEqnID) + "'")
            eqn = cursor.fetchone()
            if not eqn == None:
                eqn = tools.toAscii_str(eqn)

                # convert eqn info to pretty string
                rule += "," + str(eqn)

    # add SndTime eqn (only works for one subgoal time annotation)
    #if not subTimeArg == None and not subTimeArg == "" :
    #  rule += ",SndTime==" + str( subTimeArg )

    # --------------------------------------------------------------- #

    rule += " ;" + "\n"  # end all rules with a semicolon

    # .................................. #
    #if goalName == "pre" :
    #  tools.bp( __name__, inspect.stack()[0][3], "rule = " + rule )
    # .................................. #

    return rule
Exemple #14
0
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
Exemple #15
0
 def getAttList(self):
     self.cursor.execute("SELECT attName,attID FROM FactAtt WHERE fid = '" +
                         self.fid + "'")
     attList = self.cursor.fetchall()
     attList = tools.toAscii_list(attList)
     return attList
Exemple #16
0
def setGoal(rid, goalName, randomDMRID, newDMRIDList, cursor):

    # --------------------------------- #
    # set defaults                      #
    # --------------------------------- #
    goalTimeArg = ""
    rewritten = True

    # --------------------------------- #
    # save goal info                    #
    # --------------------------------- #
    cursor.execute("INSERT INTO Rule VALUES ('" + rid + "','" + goalName +
                   "','" + goalTimeArg + "','" + str(rewritten) + "')")

    # --------------------------------- #
    # collect all universal atts        #
    # --------------------------------- #
    # the set of existential attributes in the domain rule is exactly the
    # set of goal attributes for the not_ DM rules.
    cursor.execute("SELECT attID,attName,attType FROM GoalAtt WHERE rid=='" +
                   randomDMRID + "'")
    univAttData = cursor.fetchall()
    univAttData = tools.toAscii_multiList(univAttData)

    # ----------------------------------- #
    # collect all existential atts        #
    # ----------------------------------- #
    # iterate over all new not_ rules and collect all variables per rule
    # not appearing in the list of goal attributes.

    goalAttList = [x[1] for x in univAttData]
    exisAttList = [
    ]  # collect previously appended existential att names here to avoid duplicates
    exisAttData = []
    for dmrid in newDMRIDList:

        allSubgoalAtts = []

        # get all sids for this rule
        cursor.execute("SELECT sid FROM Subgoals WHERE rid=='" + dmrid + "'")
        sidList = cursor.fetchall()
        sidList = tools.toAscii_list(sidList)

        for sid in sidList:
            # get all subgoal att data
            cursor.execute(
                "SELECT attID,attName,attType FROM SubgoalAtt WHERE rid=='" +
                dmrid + "' AND sid=='" + sid + "'")
            subgoalAttData = cursor.fetchall()
            subgoalAttData = tools.toAscii_multiList(subgoalAttData)

            for att in subgoalAttData:
                # grab all existential atts
                attName = att[1]

                # existential atts do not exist in the goal attribute list
                # do not duplicate existential atts in the collection structure
                # wildcards are not existential atts
                if not attName in goalAttList and not attName in exisAttList and not attName == "_":
                    exisAttData.append(att)
                    exisAttList.append(attName)

    # ----------------------------------- #
    # save goal attributes                #
    # ----------------------------------- #
    allAttData = univAttData + exisAttData
    for att in allAttData:
        attID = att[0]
        attName = att[1]
        attType = att[2]

        print "INSERT INTO GoalAtt VALUES ('" + rid + "','" + str(
            attID) + "','" + attName + "','" + attType + "')"
        cursor.execute("INSERT INTO GoalAtt VALUES ('" + rid + "','" +
                       str(attID) + "','" + attName + "','" + attType + "')")

    return univAttData
Exemple #17
0
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
Exemple #18
0
def c4datalog( cursor ) :

  goalName         = None
  provGoalNameOrig = None

  tableListStr   = "" # collect all table names delmited by a single comma only.
  tableListArray = []

  # ----------------------------------------------------------- #
  # create goal defines

  # get all rids
  cursor.execute( "SELECT rid FROM Rule" )
  ridList = cursor.fetchall()
  ridList = tools.toAscii_list( ridList )

  definesNames = []
  definesList  = []
  # ////////////////////////////////////////////////////////// #
  # populate defines list for rules
  for rid in ridList :
    newDefine   = ""

    # get goal name
    cursor.execute( "SELECT goalName FROM Rule WHERE rid = '" + rid + "'" )
    goalName = cursor.fetchone()
    goalName = tools.toAscii_str( goalName )

    # if it's a prov rule, get the original goal name
    provGoalNameOrig = None
    if "_prov" in goalName :
      provGoalNameOrig = goalName.split( "_prov" )
      provGoalNameOrig = provGoalNameOrig[0]

    # populate table information collection structures
    tableListStr += goalName + ","
    tableListArray.append( goalName )

    # ////////////////////////////////////////////////////////// #
    # populate defines list for rule goals
    if C4_TRANSLATOR_DEBUG :
      print "In c4datalog: definesList = " + str(definesList)

    if not existingDefine( goalName, definesNames ) : # prevent duplicates

      # get goal attribute list
      cursor.execute( "SELECT attID,attType From GoalAtt WHERE rid = '" + rid + "'" )
      goalAttList = cursor.fetchall()
      goalAttList = tools.toAscii_multiList( goalAttList )

      if C4_TRANSLATOR_DEBUG_1 :
        print "* goalName = " + goalName + ", goalAttList " + str( goalAttList )

      # populate type list for rule
      typeList = []
      for k in range(0,len(goalAttList)) :
        att     = goalAttList[ k ]
        attID   = att[0]
        attType = att[1]

        typeList.append( attType )

      # populate new c4 define statement
      newDefine = ""
      newDefine += "define("
      newDefine += goalName
      newDefine += ",{"

      for i in range(0,len(typeList)) :
        newDefine += typeList[i]
        if i < len(typeList) - 1 :
          newDefine += ","
        else :
          newDefine += "});" + "\n"

      # save new c4 define statement
      if not newDefine in definesList :
        definesNames.append( goalName )
        definesList.append( newDefine )
    # ////////////////////////////////////////////////////////// #

  # ----------------------------------------------------------- #
  # create fact defines

  # get all fact ids
  cursor.execute( "SELECT fid FROM Fact" )
  fidList = cursor.fetchall()
  fidList = tools.toAscii_list( fidList )

  for fid in fidList :

    # get goal name
    cursor.execute( "SELECT name FROM Fact WHERE fid = '" + fid + "'" )
    factName = cursor.fetchone()
    factName = tools.toAscii_str( factName )

    if C4_TRANSLATOR_DEBUG :
      print "**> factName = " + factName

    if C4_TRANSLATOR_DEBUG :
      print "In c4datalog: definesList = " + str(definesList)
    if not existingDefine( factName, definesNames ) : # prevent duplicates

      # populate table string
      tableListStr += factName + ","
      tableListArray.append( factName )

      # get goal attribute list
      cursor.execute( "SELECT attID,attType From FactAtt WHERE fid = '" + fid + "'" )
      factAttList = cursor.fetchall()
      factAttList = tools.toAscii_multiList( factAttList )

      if C4_TRANSLATOR_DEBUG_1 :
        print "* factName = " + factName + ", factAttList " + str( factAttList )

      # populate type list for fact
      typeList = []
      for k in range(0,len(factAttList)) :
        att     = factAttList[ k ]
        attID   = att[0]
        attType = att[1]

        typeList.append( attType )

      # check for time argument
      #cursor.execute( "SELECT timeArg FROM Fact WHERE fid='" + fid + "'" )
      #timeArg = cursor.fetchone()
      #timeArg = tools.toAscii_str( timeArg )

      #if timeArg :
      #  typeList.append( "int" )

      # populate new c4 define statement
      newDefine = ""
      newDefine += "define("
      newDefine += factName
      newDefine += ",{"

      for i in range(0,len(typeList)) :
        newDefine += typeList[i]
        if i < len(typeList) - 1 :
          newDefine += ","
        else :
          newDefine += "});" + "\n"

      # save new c4 define statement
      if not newDefine in definesList :
        definesNames.append( factName )
        definesList.append( newDefine )
  # ////////////////////////////////////////////////////////// #

  # ----------------------------------------------------------- #
  # add clock define

  definesList.append( "define(clock,{string,string,int,int});\n" )
  tableListStr += "clock,"
  tableListArray.append( "clock" )

  # ----------------------------------------------------------- #
  # add not_clock define

  #definesList.append( "define(not_clock,{string,string,int,int});\n" )
  #tableListStr += "not_clock,"
  #tableListArray.append( "not_clock" )

  # ----------------------------------------------------------- #
  # add facts

  cursor.execute( "SELECT fid FROM Fact" )
  fidList = cursor.fetchall()
  fidList = tools.toAscii_list( fidList )

  factList = []
  for fid in fidList :
    newFact = dumpers_c4.dumpSingleFact_c4( fid, cursor )
    factList.append( newFact )


  # ----------------------------------------------------------- #
  # add clock facts

  clockFactList = dumpers_c4.dump_clock( cursor )

  if C4_TRANSLATOR_DEBUG :
    print "c4_translator: clockFactList = " + str( clockFactList )

  # ----------------------------------------------------------- #
  # add rules

  cursor.execute( "SELECT rid FROM Rule" )
  ridList = cursor.fetchall()
  ridList = tools.toAscii_list( ridList )

  ruleList = []
  for rid in ridList :
    # verify data type compatibility for rules with equations
    verificationResults = tools.checkDataTypes( rid, cursor ) # returns array

    yesCompatible = verificationResults[0]
    offensiveEqn  = verificationResults[1]
    lhsType       = verificationResults[2]
    rhsType       = verificationResults[3]

    if yesCompatible :
      newRule = dumpers_c4.dumpSingleRule_c4( rid, cursor )
      ruleList.append( newRule )

    else : # data types are incompatible
      # throw error and abort
      tools.bp( __name__, inspect.stack()[0][3], "FATAL ERROR: DATA TYPE INCOMPATABILITY\nAttempting to evaluate an equation in which variables possess incomparable types.\nERROR in line: " + dumpers_c4.dumpSingleRule_c4( rid, cursor )+ "\nERROR in eqn: " + offensiveEqn + "\nlhs is of type " + lhsType + " and rhs is of type " + rhsType )


  # ----------------------------------------------------------- #
  # save table list

  if C4_TRANSLATOR_DEBUG :
    print "*******************************************"
    print "table list str :"
    print tableListStr
    print "table list array :"
    print tableListArray

  # ----------------------------------------------------------- #
  # collect program statements

  if C4_TRANSLATOR_DEBUG :
    print "*******************************************"
    print "definesList :"
    print definesList
    print "*******************************************"
    print "factList :"
    print factList
    print "*******************************************"
    print "ruleList :"
    print ruleList

  listOfStatementLists = [ definesList, factList, ruleList, clockFactList ]
  program              = tools.combineLines( listOfStatementLists )

  # break down into list of individual statements
  allProgramLines = []
  for group in listOfStatementLists :
    for statement in group :
      allProgramLines.append( statement.rstrip() )

  # remove duplicates
  tableListArray = set( tableListArray )
  tableListArray = list( tableListArray )

  return [ allProgramLines, tableListArray ]
Exemple #19
0
def getAllFactIDs(cursor):
    cursor.execute("SELECT fid FROM Fact")
    fids = cursor.fetchall()
    fids = tools.toAscii_list(fids)
    return fids
Exemple #20
0
def getAsynchronousRuleIDs(cursor):
    cursor.execute("SELECT rid FROM Rule WHERE goalTimeArg == 'async'")
    ridList = cursor.fetchall()
    ridList = tools.toAscii_list(ridList)
    return ridList
Exemple #21
0
def getInductiveRuleIDs(cursor):
    cursor.execute("SELECT rid FROM Rule WHERE goalTimeArg == 'next'")
    ridList = cursor.fetchall()
    ridList = tools.toAscii_list(ridList)
    return ridList
Exemple #22
0
def rewriteAsynchronous(cursor):

    if DEDALUSREWRITER_DEBUG:
        print " ... running asynchronous rewrite ... "

    # grab all existing next rule ids
    asynchronousRuleIDs = getAsynchronousRuleIDs(cursor)

    # check for bugs
    if DEDALUSREWRITER_DUMPS_DEBUG:
        print "asynchronousRuleIDs = " + str(asynchronousRuleIDs)
        for r in asynchronousRuleIDs:
            print "<><><><><><><><><><><><><><><>"
            print "    DUMPING ASYNC RULES   "
            print dumpers.reconstructRule(r, cursor)
            print "<><><><><><><><><><><><><><><>"

    # add attribute 'DelivTime' to head
    for rid in asynchronousRuleIDs:

        #print "*******************************"
        #print "old rule : " + str( dumpers.reconstructRule(rid, cursor) )

        if not tools.checkIfRewrittenAlready(rid, cursor):
            # set rule as rewritten
            cursor.execute("UPDATE Rule SET rewritten='" + rewrittenFlag +
                           "' WHERE rid=='" + rid + "'")

            cursor.execute(
                '''SELECT MAX(attID) FROM GoalAtt WHERE GoalAtt.rid == "''' +
                rid + '''"''')
            rawMaxID = cursor.fetchone()
            newAttID = int(rawMaxID[0] + 1)
            cursor.execute("INSERT INTO GoalAtt VALUES ('" + rid + "','" +
                           str(newAttID) + "','" + timeAtt_deliv + "','int')")

    # add attribute 'SndTime' to all subgoals and add clock subgoal
    for rid in asynchronousRuleIDs:
        sids = getSubgoalIDs(cursor, rid)  # get all subgoal ids
        sids = tools.toAscii_list(sids)

        firstSubgoalAtts = []
        for s in sids:
            cursor.execute(
                '''SELECT MAX(attID) FROM SubgoalAtt WHERE SubgoalAtt.sid == "'''
                + s + '''"''')
            rawMaxID = cursor.fetchone()
            newAttID = int(rawMaxID[0] + 1)
            cursor.execute("INSERT INTO SubgoalAtt VALUES ('" + rid + "','" +
                           s + "'," + str(newAttID) + ",'" + timeAtt_snd +
                           "','int')")

            # while we're here, collect the first attribute of this subgoal
            cursor.execute(
                "SELECT attName FROM SubgoalAtt WHERE SubgoalAtt.sid == '" +
                s + "' AND SubgoalAtt.attID == '" + str(0) + "'")
            firstAtt = cursor.fetchone()
            if firstAtt:
                firstAtt = tools.toAscii_str(firstAtt)
                firstSubgoalAtts.append(firstAtt)

        # add clock subgoal
        cursor.execute("SELECT attName FROM GoalAtt WHERE GoalAtt.rid == '" +
                       rid + "' AND GoalAtt.attID == '" + str(0) + "'")
        secondAtt = cursor.fetchone()
        secondAtt = tools.toAscii_str(secondAtt)
        clockTools.addClockSubgoal_async(rid, firstSubgoalAtts, secondAtt,
                                         timeAtt_snd, timeAtt_deliv, cursor)

    # remove time arg from rule goals
    for rid in asynchronousRuleIDs:
        cursor.execute("UPDATE Rule SET goalTimeArg='' WHERE rid='" + rid +
                       "'")

        #print "new rule : " + str( dumpers.reconstructRule(rid, cursor) )
        #print "-------------------------------"
        #tools.bp( __name__, inspect.stack()[0][3], "breakhere" )

    # check for bugs
    if DEDALUSREWRITER_DUMPS_DEBUG:
        print "Dump all rules from async : "
        dumpers.ruleDump(cursor)
        #dumpers.factDump( cursor )

    if DEDALUSREWRITER_DEBUG:
        print " ... done asynchronous rewrite ... "

    return None
Exemple #23
0
def rewriteInductive(cursor):

    if DEDALUSREWRITER_DEBUG:
        print " ... running inductive rewrite ... "

    # grab all existing next rule ids
    inductiveRuleIDs = getInductiveRuleIDs(cursor)

    # check for bugs
    if DEDALUSREWRITER_DUMPS_DEBUG:
        print "inductiveRuleIDs = " + str(inductiveRuleIDs)
        print "<><><><><><><><><><><><><><><>"
        print "    DUMPING INDUCTIVE RULES   "
        for r in inductiveRuleIDs:
            print dumpers.reconstructRule(r, cursor)
        print "<><><><><><><><><><><><><><><>"

    # add attribute 'SndTime+1' to head
    for rid in inductiveRuleIDs:

        if not tools.checkIfRewrittenAlready(rid, cursor):
            # set rule as rewritten
            cursor.execute("UPDATE Rule SET rewritten='" + rewrittenFlag +
                           "' WHERE rid=='" + rid + "'")

            # grab maximum attribute id
            cursor.execute("SELECT MAX(attID) FROM GoalAtt WHERE rid == '" +
                           rid + "'")
            rawMaxID = cursor.fetchone()
            rawMaxID = rawMaxID[0]  # extract from tuple

            # .................................. #
            #cursor.execute( "SELECT goalName FROM Rule WHERE rid=='" + rid + "'" )
            #goalName = cursor.fetchone()
            #goalName = tools.toAscii_str( goalName )
            #print "___ goalName = " + str(goalName)
            #if goalName == "clients" :
            #  print "rawMaxID = " + str( rawMaxID )
            #  sys.exit()
            # .................................. #

            if rawMaxID or (rawMaxID == 0):
                newAttID = int(rawMaxID + 1)  # the att id for SndTime

                # check if add arg is a specific time
                cursor.execute("SELECT goalTimeArg FROM Rule WHERE rid == '" +
                               rid + "'")
                timeArg = cursor.fetchone()
                timeArg = tools.toAscii_str(timeArg)

                # add attribute 'SndTime+1' to head
                cursor.execute("INSERT INTO GoalAtt VALUES ('" + rid + "','" +
                               str(newAttID) + "','" + timeAtt_snd + "+1" +
                               "','int')")
            else:
                tools.bp(
                    __name__,
                    inspect.stack()[0][3],
                    "FATAL ERROR : current rule goal has no attributes:\n" +
                    dumpers.reconstructRule(rid, cursor))

            if DEDALUSREWRITER_DEBUG:
                print "inductive: rawMaxID    = " + str(rawMaxID)
                print "inductive: rawMaxID[0] = " + str(rawMaxID[0])

    #   add attribute 'SndTime' to all subgoals
    for rid in inductiveRuleIDs:
        sids = getSubgoalIDs(cursor, rid)  # get all subgoal ids
        sids = tools.toAscii_list(sids)

        firstSubgoalAtts = []
        for s in sids:
            cursor.execute(
                '''SELECT MAX(attID) FROM SubgoalAtt WHERE SubgoalAtt.sid == "'''
                + s + '''"''')
            rawMaxID = cursor.fetchone()
            newAttID = int(rawMaxID[0] + 1)
            cursor.execute("INSERT INTO SubgoalAtt VALUES ('" + rid + "','" +
                           s + "'," + str(newAttID) + ",'" + timeAtt_snd +
                           "','int')")

            # while we're here, collect the first attribute of this subgoal
            cursor.execute(
                "SELECT argName FROM SubgoalAddArgs WHERE SubgoalAddArgs.rid == '"
                + rid + "' AND SubgoalAddArgs.sid == '" + s + "'")
            addArg = cursor.fetchone()
            if not addArg == None:
                addArg = tools.toAscii_str(addArg)

            cursor.execute(
                "SELECT attName FROM SubgoalAtt WHERE SubgoalAtt.sid == '" +
                s + "' AND SubgoalAtt.attID == '" + str(0) + "'")
            firstAtt = cursor.fetchone()
            if (not firstAtt == None) and (not addArg == "notin"):
                firstAtt = tools.toAscii_str(firstAtt)
                firstSubgoalAtts.append(firstAtt)
            else:
                if DEDALUSREWRITER_DEBUG:
                    print "firstAtt = " + str(firstAtt)

        # add clock subgoal
        clockTools.addClockSubgoal_inductive(rid, firstSubgoalAtts,
                                             timeAtt_snd, timeAtt_deliv,
                                             cursor)

    # remove time arg from rule goals
    for rid in inductiveRuleIDs:
        cursor.execute("UPDATE Rule SET goalTimeArg='' WHERE rid='" + rid +
                       "'")

    # check for bugs
    if DEDALUSREWRITER_DUMPS_DEBUG:
        print "Dump all rules from inductive : "
        dumpers.ruleDump(cursor)

    if DEDALUSREWRITER_DEBUG:
        print "... done rewriteInductive ..."

    return None
Exemple #24
0
def dumpSingleRule_c4(rid, cursor):

    rule = ""

    # -------------------------------------------------------------- #
    #                           GOAL                                 #

    # get goal name
    cursor.execute("SELECT goalName FROM Rule WHERE rid == '" + rid +
                   "'")  # get goal name
    goalName = cursor.fetchone()
    goalName = tools.toAscii_str(goalName)

    # get list of attribs in goal
    goalList = cursor.execute("SELECT attName FROM GoalAtt WHERE rid == '" +
                              rid + "'")  # list of goal atts
    goalList = cursor.fetchall()
    goalList = tools.toAscii_list(goalList)

    # get goal time arg
    goalTimeArg = ""
    cursor.execute("SELECT goalTimeArg FROM Rule WHERE rid == '" + rid + "'")
    goalTimeArg = cursor.fetchone()
    goalTimeArg = tools.toAscii_str(goalTimeArg)

    # convert goal info to pretty string
    rule += goalName + "("
    for j in range(0, len(goalList)):
        if j < (len(goalList) - 1):
            rule += goalList[j] + ","
        else:
            rule += goalList[j] + ")"
    #KD
    if not goalTimeArg == "":
        rule += "@" + goalTimeArg + " :- "
        #sys.exit( "ERROR: leftover timeArg in goal: " + rule + "@" + goalTimeArg )
    else:
        rule += " :- "

    # --------------------------------------------------------------- #
    #                         SUBGOALS                                #

    # get list of sids for the subgoals of this rule
    cursor.execute("SELECT sid FROM Subgoals WHERE rid == '" + str(rid) +
                   "'")  # get list of sids for this rule
    subIDs = cursor.fetchall()
    subIDs = tools.toAscii_list(subIDs)

    # prioritize dom subgoals first.
    subIDs = prioritizeDoms(rid, subIDs, cursor)

    # prioritize negated subgoals last.
    #subIDs = prioritizeNegatedLast( rid, subIDs, cursor )
    subIDs, clockSubIDs, negSubIDs = prioritizeNegatedLast_2(
        rid, subIDs, cursor)

    subTimeArg = None
    # iterate over subgoal ids
    for k in range(0, len(subIDs)):
        newSubgoal = ""

        s = subIDs[k]

        # get subgoal name
        cursor.execute("SELECT subgoalName FROM Subgoals WHERE rid == '" +
                       str(rid) + "' AND sid == '" + str(s) + "'")
        subgoalName = cursor.fetchone()

        if not subgoalName == None:
            subgoalName = tools.toAscii_str(subgoalName)

            logging.debug("subgoalName = " + subgoalName)

            # get subgoal attribute list
            subAtts = cursor.execute(
                "SELECT attName FROM SubgoalAtt WHERE rid == '" + rid +
                "' AND sid == '" + s + "'")
            subAtts = cursor.fetchall()
            subAtts = tools.toAscii_list(subAtts)

            # get subgoal time arg
            cursor.execute(
                "SELECT subgoalTimeArg FROM Subgoals WHERE rid == '" + rid +
                "' AND sid == '" + s + "'")
            subTimeArg = cursor.fetchone()  # assume only one time arg
            subTimeArg = tools.toAscii_str(subTimeArg)

            # get subgoal polarity
            cursor.execute(
                "SELECT subgoalPolarity FROM Subgoals WHERE rid == '" + rid +
                "' AND sid == '" + s + "'")
            subPolarity = cursor.fetchone()  # assume only one additional arg
            subPolarity = tools.toAscii_str(subPolarity)
            if subPolarity == "notin":
                subPolarity += " "
            newSubgoal += subPolarity

            # all subgoals have a name and open paren
            newSubgoal += subgoalName + "("

            # add in all attributes
            for j in range(0, len(subAtts)):

                currAtt = subAtts[j]

                # replace SndTime in subgoal with subTimeArg, if applicable
                if not subTimeArg == "" and "SndTime" in currAtt:
                    currAtt = str(subTimeArg)

                if j < (len(subAtts) - 1):
                    newSubgoal += currAtt + ","
                else:
                    newSubgoal += currAtt + ")"

            # cap with a comma, if applicable
            if k < len(subIDs) - 1:
                newSubgoal += ","

        rule += newSubgoal

    # cap with comma
    if len(subIDs) > 0:
        rule += ","

    # --------------------------------------------------------------- #
    #                         EQUATIONS                               #

    # get list of sids for the subgoals of this rule
    cursor.execute(
        "SELECT eid FROM Equation")  # get list of eids for this rule
    eqnIDs = cursor.fetchall()
    eqnIDs = tools.toAscii_list(eqnIDs)

    #  if   len( subIDs )      > 0 and \
    #     ( len( eqnIDs )      > 0 or \
    #       len( clockSubIDs ) > 0 or \
    #       len( negSubIDs )   > 0 ):
    #    rule += ","

    for e in range(0, len(eqnIDs)):
        currEqnID = eqnIDs[e]

        # get associated equation
        if not currEqnID == None:
            cursor.execute("SELECT eqn FROM Equation WHERE rid == '" + rid +
                           "' AND eid == '" + str(currEqnID) + "'")
            eqn = cursor.fetchone()
            if not eqn == None:
                eqn = tools.toAscii_str(eqn)

                # convert eqn info to pretty string
                rule += "," + str(eqn)

#  if len( eqnIDs ) > 0 and len( clockSubIDs ) > 0 :
#    rule += ","

# add SndTime eqn (only works for one subgoal time annotation)
#if not subTimeArg == None and not subTimeArg == "" :
#  rule += ",SndTime==" + str( subTimeArg )

# cap with comma
    if len(eqnIDs) > 0:
        rule += ","

    # --------------------------------------------------------------- #

    if len(clockSubIDs) > 0:

        subTimeArg = None
        # iterate over subgoal ids
        for k in range(0, len(clockSubIDs)):
            newSubgoal = ""

            s = clockSubIDs[k]

            # get subgoal name
            cursor.execute("SELECT subgoalName \
                       FROM   Subgoals \
                       WHERE rid == '" + str(rid) + "' AND \
                             sid == '" + str(s) + "'")
            subgoalName = cursor.fetchone()

            if not subgoalName == None:
                subgoalName = tools.toAscii_str(subgoalName)

                logging.debug("subgoalName = " + subgoalName)

                # get subgoal attribute list
                subAtts = cursor.execute("SELECT attName \
                                   FROM   SubgoalAtt \
                                   WHERE rid == '" + rid + "' AND \
                                         sid == '" + s + "'")
                subAtts = cursor.fetchall()
                subAtts = tools.toAscii_list(subAtts)

                # get subgoal time arg
                cursor.execute("SELECT subgoalTimeArg \
                         FROM   Subgoals \
                         WHERE rid == '" + rid + "' AND \
                               sid == '" + s + "'")
                subTimeArg = cursor.fetchone()  # assume only one time arg
                subTimeArg = tools.toAscii_str(subTimeArg)

                # get subgoal polarity
                cursor.execute("SELECT subgoalPolarity \
                         FROM   Subgoals \
                         WHERE rid == '" + rid + "' AND \
                               sid == '" + s + "'")
                subPolarity = cursor.fetchone(
                )  # assume only one additional arg
                subPolarity = tools.toAscii_str(subPolarity)
                subPolarity += " "
                newSubgoal += subPolarity

                # all subgoals have a name and open paren
                newSubgoal += subgoalName + "("

                # add in all attributes
                for j in range(0, len(subAtts)):

                    currAtt = subAtts[j]

                    # replace SndTime in subgoal with subTimeArg, if applicable
                    if not subTimeArg == "" and "SndTime" in currAtt:
                        currAtt = str(subTimeArg)

                    if j < (len(subAtts) - 1):
                        newSubgoal += currAtt + ","
                    else:
                        newSubgoal += currAtt + ")"

                # cap with a comma, if applicable
                if k < len(clockSubIDs) - 1:
                    newSubgoal += ","

            rule += newSubgoal

    #if len( clockSubIDs ) > 0 and len( negSubIDs ) > 0 :
    #  rule += ","

    # cap with comma
    if len(clockSubIDs) > 0:
        rule += ","

    # --------------------------------------------------------------- #

    if len(negSubIDs) > 0:

        subTimeArg = None
        # iterate over subgoal ids
        for k in range(0, len(negSubIDs)):
            newSubgoal = ""

            s = negSubIDs[k]

            # get subgoal name
            cursor.execute("SELECT subgoalName \
                       FROM   Subgoals \
                       WHERE rid == '" + str(rid) + "' AND \
                             sid == '" + str(s) + "'")
            subgoalName = cursor.fetchone()

            if not subgoalName == None:
                subgoalName = tools.toAscii_str(subgoalName)

                logging.debug("subgoalName = " + subgoalName)

                # get subgoal attribute list
                subAtts = cursor.execute("SELECT attName \
                                   FROM   SubgoalAtt \
                                   WHERE rid == '" + rid + "' AND \
                                         sid == '" + s + "'")
                subAtts = cursor.fetchall()
                subAtts = tools.toAscii_list(subAtts)

                # get subgoal time arg
                cursor.execute("SELECT subgoalTimeArg \
                         FROM   Subgoals \
                         WHERE rid == '" + rid + "' AND \
                               sid == '" + s + "'")
                subTimeArg = cursor.fetchone()  # assume only one time arg
                subTimeArg = tools.toAscii_str(subTimeArg)

                # get subgoal polarity
                cursor.execute("SELECT subgoalPolarity \
                         FROM   Subgoals \
                         WHERE rid == '" + rid + "' AND \
                               sid == '" + s + "'")
                subPolarity = cursor.fetchone(
                )  # assume only one additional arg
                subPolarity = tools.toAscii_str(subPolarity)
                subPolarity += " "
                newSubgoal += subPolarity

                # all subgoals have a name and open paren
                newSubgoal += subgoalName + "("

                # add in all attributes
                for j in range(0, len(subAtts)):

                    currAtt = subAtts[j]

                    # replace SndTime in subgoal with subTimeArg, if applicable
                    if not subTimeArg == "" and "SndTime" in currAtt:
                        currAtt = str(subTimeArg)

                    if j < (len(subAtts) - 1):
                        newSubgoal += currAtt + ","
                    else:
                        newSubgoal += currAtt + ")"

                # cap with a comma, if applicable
                if k < len(negSubIDs) - 1:
                    newSubgoal += ","

            rule += newSubgoal

    # --------------------------------------------------------------- #

    if rule.endswith(","):
        rule = rule[:-1]

    rule += " ;" + "\n"  # end all rules with a semicolon
    rule = rule.replace(" ,", "")
    rule = rule.replace(",,", ",")
    rule = rule.translate(None, string.whitespace)
    rule = rule.replace(",;", ";")
    rule = rule.replace("notin", "notin ")

    logging.debug("  >>> rule = " + rule)

    return rule
Exemple #25
0
def iedb_rewrites(factMeta, ruleMeta, cursor, settings_path):

    logging.debug("  IEDB REWRITES : running process...")

    # ----------------------------------------- #
    # generate a dictionary mapping
    # relation names to lists of fact ids.

    cursor.execute("SELECT goalName FROM Rule")
    idb_rel_list = cursor.fetchall()
    idb_rel_list = tools.toAscii_list(idb_rel_list)

    logging.debug("  IEDB REWRITES : idb_rel_list = " + str(idb_rel_list))

    # initialize dictionary
    rel_name_to_fact_obj_map = {}
    for rel_name in idb_rel_list:
        rel_name_to_fact_obj_map[rel_name] = []

    logging.debug("  IEDB REWRITES : rel_name_to_fact_obj_map = " +
                  str(rel_name_to_fact_obj_map))

    # collect fids
    for f in factMeta:
        if f.relationName in idb_rel_list:
            rel_name_to_fact_obj_map[f.relationName].append(f)

    logging.debug("  IEDB REWRITES : rel_name_to_fact_obj_map = " +
                  str(rel_name_to_fact_obj_map))

    # ----------------------------------------- #
    # append "_edb" to all fact names

    for rel_name in rel_name_to_fact_obj_map:

        fact_obj_list = rel_name_to_fact_obj_map[rel_name]

        logging.debug("  IEDB REWRITES : rel_name      = " + rel_name)
        logging.debug("  IEDB REWRITES : fact_obj_list = " +
                      str(fact_obj_list))

        for fact_obj in fact_obj_list:

            logging.debug("  IEDB REWRITES : fact_obj.cursor   = " +
                          str(fact_obj.cursor))
            logging.debug("  IEDB REWRITES : fact_obj.factData = " +
                          str(fact_obj.factData))

            fact_obj.relationName += "_edb"
            fact_obj.saveFactInfo()
            fact_obj.factData[
                "relationName"] += "_edb"  # reflect the change in the factData

            logging.debug("  IEDB REWRITES : after:")
            logging.debug("  IEDB REWRITES : fact_obj.factData = " +
                          str(fact_obj.factData))
            cursor.execute("SELECT name FROM Fact WHERE fid=='" +
                           str(fact_obj.fid) + "'")
            name = cursor.fetchone()
            name = tools.toAscii_str(name)
            logging.debug("  IEBD REWRITES : name = " + str(name))
            #sys.exit( "foo" )

    # ----------------------------------------- #
    # add a new rule with the original goal
    # predicated on the new _edb goal name

    for rel_name in rel_name_to_fact_obj_map:

        logging.debug("  IEDB REWRITES : rel_name = " + rel_name)
        logging.debug( "  IEDB REWRITEs : len( rel_name_to_fact_obj_map[ " + rel_name + " ] ) = " + \
                          str( len( rel_name_to_fact_obj_map[ rel_name ] ) ) )

        if len(rel_name_to_fact_obj_map[rel_name]) > 0:

            new_ruleData = {}
            new_ruleData["relationName"] = rel_name
            new_ruleData["goalTimeArg"] = ""
            new_ruleData["eqnDict"] = {}

            # build the goal attribute list
            new_goalAttList = []

            representative_factData = rel_name_to_fact_obj_map[rel_name][
                0].factData  # just pick one
            representative_dataList = representative_factData["dataList"]

            logging.debug("  representative_factData = " +
                          str(representative_factData))
            logging.debug("  representative_dataList = " +
                          str(representative_dataList))

            arity = len(representative_dataList)
            for i in range(0, arity):
                new_goalAttList.append("A" + str(i))

            new_ruleData["goalAttList"] = new_goalAttList

            # build the subgoal
            new_subgoal_dict = {}
            new_subgoal_dict["subgoalName"] = rel_name + "_edb"
            new_subgoal_dict["subgoalAttList"] = new_goalAttList
            new_subgoal_dict["polarity"] = ""
            new_subgoal_dict["subgoalTimeArg"] = ""

            new_ruleData["subgoalListOfDicts"] = [new_subgoal_dict]

            # generate a new rid
            rid = tools.getIDFromCounters("rid")

            logging.debug("  IEDB REWRITES : new_ruleData = " +
                          str(new_ruleData))

            # save the new rule
            new_rule = copy.deepcopy(Rule.Rule(rid, new_ruleData, cursor))
            new_rule.cursor = cursor  # need to do this again for some reason???
            ruleMeta.append(new_rule)

            # set the types!
            #setTypes.setTypes( cursor, argDict )

        logging.debug("  IEDB REWRITES : done on rel_name = " + rel_name)

    # ----------------------------------------- #

    logging.debug("  IEDB REWRITES : ...done.")
    return factMeta, ruleMeta
Exemple #26
0
def getRewrittenRuleIDs(cursor):
    cursor.execute("SELECT rid FROM Rule WHERE goalTimeArg == 'rewritten'")
    ridList = cursor.fetchall()
    ridList = tools.toAscii_list(ridList)
    return ridList
Exemple #27
0
def c4datalog(argDict, cursor):

    logging.debug("  C4 DATALOG : running process...")

    goalName = None
    provGoalNameOrig = None

    tableListStr = ""  # collect all table names delmited by a single comma only.
    tableListArray = []

    # ----------------------------------------------------------- #
    # create goal defines

    # get all rids
    cursor.execute("SELECT rid FROM Rule")
    ridList = cursor.fetchall()
    ridList = tools.toAscii_list(ridList)

    definesNames = []
    definesList = []
    # ////////////////////////////////////////////////////////// #
    # populate defines list for rules
    for rid in ridList:
        newDefine = ""

        # get goal name
        cursor.execute("SELECT goalName FROM Rule WHERE rid = '" + rid + "'")
        goalName = cursor.fetchone()
        goalName = tools.toAscii_str(goalName)

        # if it's a prov rule, get the original goal name
        provGoalNameOrig = None
        if "_prov" in goalName:
            provGoalNameOrig = goalName.split("_prov")
            provGoalNameOrig = provGoalNameOrig[0]

        # populate table information collection structures
        tableListStr += goalName + ","
        tableListArray.append(goalName)

        # ////////////////////////////////////////////////////////// #
        # populate defines list for rule goals
        logging.debug("In c4datalog: definesList = " + str(definesList))

        if not existingDefine(goalName, definesNames):  # prevent duplicates

            # get goal attribute list
            cursor.execute("SELECT attID,attType From GoalAtt WHERE rid = '" +
                           rid + "'")
            goalAttList = cursor.fetchall()
            goalAttList = tools.toAscii_multiList(goalAttList)

            logging.debug("* goalName = " + goalName + ", goalAttList " +
                          str(goalAttList))

            # populate type list for rule
            typeList = []
            for k in range(0, len(goalAttList)):
                att = goalAttList[k]
                attID = att[0]
                attType = att[1]

                typeList.append(attType)

            # populate new c4 define statement
            newDefine = ""
            newDefine += "define("
            newDefine += goalName
            newDefine += ",{"

            for i in range(0, len(typeList)):
                newDefine += typeList[i]
                if i < len(typeList) - 1:
                    newDefine += ","
                else:
                    newDefine += "});" + "\n"

            # save new c4 define statement
            if not newDefine in definesList:
                definesNames.append(goalName)
                definesList.append(newDefine)
        # ////////////////////////////////////////////////////////// #

    # ----------------------------------------------------------- #
    # create fact defines

    # get all fact ids
    cursor.execute("SELECT fid FROM Fact")
    fidList = cursor.fetchall()
    fidList = tools.toAscii_list(fidList)

    for fid in fidList:

        # get goal name
        cursor.execute("SELECT name FROM Fact WHERE fid = '" + fid + "'")
        factName = cursor.fetchone()
        factName = tools.toAscii_str(factName)

        logging.debug("**> factName = " + factName)

        logging.debug("In c4datalog: definesList = " + str(definesList))

        if not existingDefine(factName, definesNames):  # prevent duplicates

            # populate table string
            tableListStr += factName + ","
            tableListArray.append(factName)

            # get goal attribute list
            cursor.execute(
                "SELECT dataID,dataType From FactData WHERE fid = '" + fid +
                "'")
            factAttList = cursor.fetchall()
            factAttList = tools.toAscii_multiList(factAttList)

            logging.debug("* factName = " + factName + ", factAttList " +
                          str(factAttList))

            # populate type list for fact
            typeList = []
            for k in range(0, len(factAttList)):
                att = factAttList[k]
                attID = att[0]
                attType = att[1]

                typeList.append(attType)

            # check for time argument
            #cursor.execute( "SELECT timeArg FROM Fact WHERE fid='" + fid + "'" )
            #timeArg = cursor.fetchone()
            #timeArg = tools.toAscii_str( timeArg )

            #if timeArg :
            #  typeList.append( "int" )

            # populate new c4 define statement
            newDefine = ""
            newDefine += "define("
            newDefine += factName
            newDefine += ",{"

            for i in range(0, len(typeList)):
                newDefine += typeList[i]
                if i < len(typeList) - 1:
                    newDefine += ","
                else:
                    newDefine += "});" + "\n"

            # save new c4 define statement
            if not newDefine in definesList:
                definesNames.append(factName)
                definesList.append(newDefine)
    # ////////////////////////////////////////////////////////// #

    # ----------------------------------------------------------- #
    # add clock define

    definesList.append("define(clock,{string,string,int,int});\n")
    tableListStr += "clock,"
    tableListArray.append("clock")

    # ----------------------------------------------------------- #
    # add not_clock define

    #definesList.append( "define(not_clock,{string,string,int,int});\n" )
    #tableListStr += "not_clock,"
    #tableListArray.append( "not_clock" )

    # ----------------------------------------------------------- #
    # add crash define

    definesList.append("define(crash,{string,string,int,int});\n")
    tableListStr += "crash,"
    tableListArray.append("crash")

    # ----------------------------------------------------------- #
    # add facts

    cursor.execute("SELECT fid FROM Fact")
    fidList = cursor.fetchall()
    fidList = tools.toAscii_list(fidList)

    factList = []
    for fid in fidList:
        newFact = dumpers_c4.dumpSingleFact_c4(fid, cursor)
        factList.append(newFact)

    # ----------------------------------------------------------- #
    # add clock facts

    clockFactList = dumpers_c4.dump_clock(cursor)

    logging.debug("c4_translator: clockFactList = " + str(clockFactList))

    # ----------------------------------------------------------- #
    # add crash facts

    crashFactList = dumpers_c4.dump_crash(cursor)
    #crashFactList = []

    #print crashFactList
    #tools.bp( __name__, inspect.stack()[0][3], "blah" )

    #logging.debug( "c4_translator: crashFactList = " + str( crashFactList ) )

    # ----------------------------------------------------------- #
    # add rules

    cursor.execute("SELECT rid FROM Rule")
    ridList = cursor.fetchall()
    ridList = tools.toAscii_list(ridList)

    ruleList = []
    for rid in ridList:

        # verify data type compatibility for rules with equations
        #verificationResults = tools.checkDataTypes( rid, cursor ) # returns array

        #yesCompatible = verificationResults[0]
        #offensiveEqn  = verificationResults[1]
        #lhsType       = verificationResults[2]
        #rhsType       = verificationResults[3]

        #if yesCompatible :
        if True:
            newRule = dumpers_c4.dumpSingleRule_c4(rid, cursor)
            ruleList.append(newRule)

        else:  # data types are incompatible
            # throw error and abort
            tools.bp(
                __name__,
                inspect.stack()[0][3],
                "FATAL ERROR: DATA TYPE INCOMPATABILITY\nAttempting to evaluate an equation in which variables possess incomparable types.\nERROR in line: "
                + dumpers_c4.dumpSingleRule_c4(rid, cursor) +
                "\nERROR in eqn: " + offensiveEqn + "\nlhs is of type " +
                lhsType + " and rhs is of type " + rhsType)

    # ------------------------------------------------------ #
    # grab the next rule handling method

    try:
        NEXT_RULE_HANDLING = tools.getConfig( argDict[ "settings" ], \
                                              "DEFAULT", \
                                              "NEXT_RULE_HANDLING", \
                                              str )

    except ConfigParser.NoOptionError:
        logging.info(
            "WARNING : no 'NEXT_RULE_HANLDING' defined in 'DEFAULT' section of settings file."
        )
        tools.bp( __name__, inspect.stack()[0][3], \
                 "FATAL ERROR : NEXT_RULE_HANDLING parameter not specified in DEFAULT section of settings file. use 'USE_AGGS', 'SYNC_ASSUMPTION', or 'USE_NEXT_CLOCK' only." )

    # sanity check next rule handling value
    if NEXT_RULE_HANDLING == "USE_AGGS" or \
       NEXT_RULE_HANDLING == "SYNC_ASSUMPTION" or \
       NEXT_RULE_HANDLING == "USE_NEXT_CLOCK" :
        pass
    else:
        tools.bp( __name__, inspect.stack()[0][3], \
                  "FATAL ERROR : unrecognized NEXT_RULE_HANDLING value '" + NEXT_RULE_HANDLING + "'. use 'USE_AGGS', 'SYNC_ASSUMPTION', or 'USE_NEXT_CLOCK' only." )

    # ----------------------------------------------------------- #
    # add next_clock, if necessary

    if NEXT_RULE_HANDLING == "USE_NEXT_CLOCK":

        # ------------------------------------------------------ #
        # add define

        definesList.append("define(next_clock,{string,string,int,int});\n")
        tableListStr += "next_clock,"
        tableListArray.append("next_clock")

        # ------------------------------------------------------ #
        # add next_clock facts for all synchronous facts appearing clock

        next_clock_factList = []
        for cfact in clockFactList:
            if isSynchronous(cfact):
                next_clock_fact = "next_" + cfact
                next_clock_factList.append(next_clock_fact)

    # ----------------------------------------------------------- #
    # save table list

    logging.debug("*******************************************")
    logging.debug("table list str :\n" + str(tableListStr))
    logging.debug("table list array :\n" + str(tableListArray))

    # ----------------------------------------------------------- #
    # collect program statements

    logging.debug("*******************************************")
    logging.debug("definesList :\n" + str(definesList))
    logging.debug("*******************************************")
    logging.debug("factList :\n" + str(factList))
    logging.debug("*******************************************")
    logging.debug("ruleList :\n" + str(ruleList))

    # NOTE: listOfStatementLists controls the ordering of statements
    #       in the final c4 program.
    if NEXT_RULE_HANDLING == "USE_NEXT_CLOCK":
        listOfStatementLists = [ definesList, \
                                 ruleList, \
                                 factList, \
                                 crashFactList, \
                                 next_clock_factList, \
                                 clockFactList ]
    else:
        #listOfStatementLists = [ definesList, \
        #                         factList, \
        #                         ruleList, \
        #                         clockFactList ]
        listOfStatementLists = [ definesList, \
                                 ruleList, \
                                 factList, \
                                 crashFactList, \
                                 clockFactList ]

    program = tools.combineLines(listOfStatementLists)

    # break down into list of individual statements
    allProgramLines = []
    for group in listOfStatementLists:
        for statement in group:
            allProgramLines.append(statement.rstrip())

    # remove duplicates
    tableListArray = set(tableListArray)
    tableListArray = list(tableListArray)

    logging.debug("  C4 DATALOG : ...done.")
    return [allProgramLines, tableListArray]
Exemple #28
0
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
Exemple #29
0
def rewriteDeductive(cursor):

    if DEDALUSREWRITER_DEBUG:
        print " ... running deductive rewrite ..."

    # grab all existing non-next and non-async rule ids
    deductiveRuleIDs = getDeductiveRuleIDs(cursor)

    # add attribute 'SndTime' to head as the new rightmost attribute
    for rid in deductiveRuleIDs:

        # ......................................... #
        cursor.execute("SELECT goalName FROM Rule WHERE rid =='" + rid + "'")
        goalName = cursor.fetchone()
        goalName = tools.toAscii_str(goalName)
        # ......................................... #

        #print "*******************************"
        #print "old rule : " + str( dumpers.reconstructRule(rid, cursor) )

        if not tools.checkIfRewrittenAlready(rid, cursor):
            # set rule as rewritten
            cursor.execute("UPDATE Rule SET rewritten='" + rewrittenFlag +
                           "' WHERE rid=='" + rid + "'")

            # grab maximum attribute id
            cursor.execute("SELECT MAX(attID) FROM GoalAtt WHERE rid == '" +
                           rid + "'")
            rawMaxID = cursor.fetchone()
            rawMaxID = rawMaxID[0]  # extract from tuple

            if rawMaxID or (rawMaxID == 0):
                newAttID = int(rawMaxID + 1)  # the att id for SndTime

                # check if add arg is a specific time
                cursor.execute("SELECT goalTimeArg FROM Rule WHERE rid == '" +
                               rid + "'")
                timeArg = cursor.fetchone()
                timeArg = tools.toAscii_str(timeArg)

                if timeArg.isdigit():
                    cursor.execute("INSERT INTO GoalAtt VALUES ('" + rid +
                                   "','" + str(newAttID) + "','" + timeArg +
                                   "','int')")
                else:
                    cursor.execute("INSERT INTO GoalAtt VALUES ('" + rid +
                                   "','" + str(newAttID) + "','" +
                                   timeAtt_snd + "','int')")

            else:
                tools.bp(
                    __name__,
                    inspect.stack()[0][3],
                    "FATAL ERROR : current rule goal has no attributes:\n" +
                    dumpers.reconstructRule(rid, cursor))

            # add attribute 'Time' to all subgoals
            sids = getSubgoalIDs(cursor, rid)  # get all subgoal ids
            sids = tools.toAscii_list(sids)

            firstSubgoalAtts = []
            # iterate over rule subgoals
            for s in sids:

                # ......................................... #
                cursor.execute(
                    "SELECT subgoalName FROM Subgoals WHERE rid=='" + rid +
                    "' AND sid=='" + s + "'")
                subgoalName = cursor.fetchone()
                subgoalName = tools.toAscii_str(subgoalName)
                # ......................................... #

                addArg = None

                # get time arg for subgoal
                cursor.execute(
                    "SELECT subgoalTimeArg FROM Subgoals WHERE rid = '" + rid +
                    "' AND sid = '" + s + "'")
                timeArg = cursor.fetchone()
                timeArg = tools.toAscii_str(timeArg)

                # add Time as a subgoal attribute
                cursor.execute(
                    '''SELECT MAX(attID) FROM SubgoalAtt WHERE SubgoalAtt.sid == "'''
                    + s + '''"''')
                rawMaxID = cursor.fetchone()  # int type
                newAttID = int(rawMaxID[0] + 1)
                cursor.execute("INSERT INTO SubgoalAtt VALUES ('" + rid +
                               "','" + s + "'," + str(newAttID) + ",'" +
                               timeAtt_snd + "','int')")

                # replace subgoal time attribute with numeric time arg
                if timeArg.isdigit():
                    cursor.execute(
                        "SELECT attName FROM SubgoalAtt WHERE sid = '" + s +
                        "'")
                    satts = cursor.fetchall()
                    satts = tools.toAscii_list(satts)

                    # ......................................... #
                    #if goalName == "pre" :
                    #  if subgoalName == "bcast" :
                    #    print " timeArg = " + str(timeArg)
                    #    print " satts   = " + str(satts)
                    #    tools.bp( __name__, inspect.stack()[0][3], "___ASDFASLKDHFWER" )
                    # ......................................... #

                    for i in range(0, len(satts)):
                        if satts[i] == timeAtt_snd:
                            cursor.execute("UPDATE SubgoalAtt SET attName='" +
                                           timeArg + "' WHERE rid = '" + rid +
                                           "' AND sid = '" + s +
                                           "' AND attID = '" + str(i) + "'")

                # collect the additional argument (aka notin for c4)
                cursor.execute(
                    "SELECT argName FROM SubgoalAddArgs WHERE SubgoalAddArgs.rid == '"
                    + rid + "' AND SubgoalAddArgs.sid == '" + s + "'")
                addArg = cursor.fetchone()
                if addArg:
                    addArg = tools.toAscii_str(addArg)

                # while we're here, collect the first attribute of this subgoal
                cursor.execute(
                    "SELECT attName FROM SubgoalAtt WHERE SubgoalAtt.sid == '"
                    + s + "' AND SubgoalAtt.attID == '" + str(0) + "'")
                firstAtt = cursor.fetchone()
                if (not firstAtt == None) and (
                        not addArg
                        == "notin"):  # need branch on notin for safety
                    firstAtt = tools.toAscii_str(firstAtt)
                    firstSubgoalAtts.append(firstAtt)
                else:
                    if DEDALUSREWRITER_DEBUG:
                        print "firstAtt = " + str(firstAtt)

            # sanity checking branch
            if len(firstSubgoalAtts) > 0:
                pass
                # add clock subgoal
                #clockTools.addClockSubgoal_deductive( rid, firstSubgoalAtts, timeAtt_snd, timeAtt_deliv, cursor )
            else:
                print dumpers.reconstructRule(rid, cursor)
                tools.bp(
                    __name__,
                    inspect.stack()[0][3],
                    "We've got major probs, mate. The subgoals of this rule have no attributes.\nfirstSubgoalAtts = "
                    + str(firstSubgoalAtts) + "\nMake sure the rule is safe.")

        #tools.bp( __name__, inspect.stack()[0][3], "new rule : " + str(dumpers.reconstructRule(rid, cursor)) )

    # check for bugs
    if DEDALUSREWRITER_DEBUG:
        print "Rule :"
        cursor.execute('''SELECT * FROM Rule''')
        for i in cursor.fetchall():
            print i

        print "GoalAtt"
        cursor.execute("SELECT * FROM GoalAtt")
        for s in cursor.fetchall():
            print s

        print "Subgoals"
        cursor.execute("SELECT * FROM Subgoals")
        for s in cursor.fetchall():
            print s

        print "SubgoalAtt"
        cursor.execute("SELECT * FROM SubgoalAtt")
        for s in cursor.fetchall():
            print s