Esempio n. 1
0
def updateFactSchemas(fids, cursor):

    for fid in fids:

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

        # get max attID
        cursor.execute("SELECT max(attID) FROM FactAtt WHERE fid=='" + fid +
                       "'")
        maxID = cursor.fetchone()
        maxID = maxID[0]

        # update fact schema
        attID = maxID + 1
        thisType = "int"

        #DEBUGGING
        cursor.execute("SELECT name FROM Fact WHERE fid=='" + fid + "'")
        name = cursor.fetchone()
        name = tools.toAscii_str(name)
        #if name == "bcast" :
        #  print "1CHECK THIS BCAST INSERT!!!"
        #  print "INSERT INTO FactAtt VALUES ('" + fid + "','" + str(attID) + "','" + timeArg + "','" + thisType + "')"

        cursor.execute("INSERT INTO FactAtt VALUES ('" + fid + "','" +
                       str(attID) + "','" + timeArg + "','" + thisType + "')")
Esempio n. 2
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
Esempio n. 3
0
def domainRewrites(parentRID, sidInParent, posName, posNameRIDs, newDMRIDList,
                   COUNTER, cursor):

    print "&&& posName : " + posName

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

    # ---------------------------------------------- #
    # buid new domain rule name                      #
    # ---------------------------------------------- #
    domainRuleName = "dom_not_" + posName + "_from_" + parentName

    # ---------------------------------------------- #
    # add new rule for the domain subgoal            #
    # ---------------------------------------------- #
    newRuleMeta = addDomainRules(parentRID, sidInParent, parentName, posName,
                                 domainRuleName, posNameRIDs, newDMRIDList,
                                 cursor)

    # ---------------------------------------------- #
    # add domain idb subgoal to each new DM rule     #
    # ---------------------------------------------- #
    addSubgoalsToRules(domainRuleName, newDMRIDList, cursor)

    return newRuleMeta
Esempio n. 4
0
def prioritizeNegatedLast(rid, subIDs, cursor):

    posSubs = []
    negSubs = []

    # check if subgoal is negated
    # branch on result.
    for subID in subIDs:

        cursor.execute("SELECT subgoalPolarity FROM Subgoals WHERE rid=='" +
                       rid + "' AND sid=='" + subID + "'")
        sign = cursor.fetchone()

        # positive subgoals may have no argName data
        # all instances of negative subgoals WILL have an argName
        if sign:
            sign = tools.toAscii_str(sign)
        else:
            sign = ""

        if not sign == "notin":
            posSubs.append(subID)
        else:
            negSubs.append(subID)

    return posSubs + negSubs
Esempio n. 5
0
def addSubgoalsToRules(domainRuleName, newDMRIDList, cursor):

    # -------------------------------------------- #
    # get goal att list for the dom_not_whatever
    # rule previously written in addDomainRules
    # -------------------------------------------- #

    # get rid for the domain rule
    cursor.execute("SELECT rid FROM Rule WHERE goalName=='" + domainRuleName +
                   "'")
    rid = cursor.fetchone()
    if not rid or rid == "":
        tools.bp(
            __name__,
            inspect.stack()[0][3],
            "FATAL ERROR : writing domain subgoals, but no '" +
            domainRuleName + "' rule exists. aborting...")
    else:
        rid = tools.toAscii_str(rid)

    # get the goal attribute list for the domain rule
    cursor.execute("SELECT attID,attName,attType FROM GoalAtt WHERE rid=='" +
                   rid + "'")
    goalAttList = cursor.fetchall()
    goalAttList = tools.toAscii_multiList(goalAttList)

    for rid in newDMRIDList:

        # ----------------------------------- #
        # generate sid                        #
        # ----------------------------------- #
        sid = tools.getID()

        # ----------------------------------- #
        # fixed subgoal time arg to nothing   #
        # ----------------------------------- #
        subgoalTimeArg = ""

        # ----------------------------------- #
        # insert subgoal metadata             #
        # ----------------------------------- #
        cursor.execute("INSERT INTO Subgoals VALUES ('" + rid + "','" + sid +
                       "','" + domainRuleName + "','" + subgoalTimeArg + "')")

        # ----------------------------------- #
        # insert subgoal att data             #
        # ----------------------------------- #

        attID = 0
        for att in goalAttList:

            attName = att[1]
            attType = att[2]

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

            attID += 1
def 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
Esempio n. 7
0
    def getAllTriggerRecords(self, pgattMaps):

        if self.name == "bcast":
            print "pgattMaps : " + str(pgattMaps)

        if self.name == "not_missing_log_from_post":
            print "<><>pgattMaps = " + str(pgattMaps)

        pTrigRecs = []

        for attMap in pgattMaps:
            prid = attMap[0]
            mapping = attMap[1]

            # get full relation name
            self.cursor.execute("SELECT goalName FROM Rule WHERE rid=='" +
                                prid + "'")
            pname = self.cursor.fetchone(
            )  # goalName should be unique per provenance rule
            pname = tools.toAscii_str(pname)

            # get full results table
            resultsTable = self.results[pname]

            # get list of valid records which agree with the provenance rule
            # goal attribute mapping
            # correctness relies upon ordered nature of the mappings.
            validRecList = []
            for rec in resultsTable:

                if self.name == "not_missing_log_from_post":
                    print "rec = " + str(rec)
                    print "mapping = " + str(mapping)

                if self.checkAgreement(mapping, rec):
                    validRecList.append(rec)

            if self.name == "not_missing_log_from_post":
                print "pname = " + pname
                print "validRecList = " + str(validRecList)

            pTrigRecs.append([prid, mapping, validRecList])

        return pTrigRecs
Esempio n. 8
0
def prioritizeDoms(rid, subIDs, cursor):

    domSubs = []
    nonDomSubs = []

    # check if subgoal is a domain subgoal
    # branch on result.
    for subID in subIDs:

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

        if "dom_" in subgoalName[0:4]:
            domSubs.append(subID)
        else:
            nonDomSubs.append(subID)

    return domSubs + nonDomSubs
Esempio n. 9
0
    def getSubgoalInfo(self, fullProvMap):
        # ------------------------------------------------------------- #
        # Transform the provAttMap into a dictionary for convenience.
        fullProvDict = {}
        for arr in fullProvMap:
            att = arr[0]
            val = arr[1]

            # sanity check
            if att in fullProvDict.keys() and not val == fullProvDict[att]:
                tools.bp(
                    __name__,
                    inspect.stack()[0][3],
                    "FATAL ERROR : multiple data values exist for the same attribute.\nattribute = "
                    + att + "\nval = " + val + " and ogattDict[ att ] = " +
                    fullProvDict[att])
            else:  # add to dictionary
                fullProvDict[att] = val
        # ------------------------------------------------------------- #

        subgoalInfo = []

        # get the list of subgoals
        self.cursor.execute(
            "SELECT sid,subgoalName FROM Subgoals WHERE rid=='" + self.prid +
            "'")
        subIDNameList = self.cursor.fetchall()
        subIDNameList = tools.toAscii_multiList(subIDNameList)

        # for each sid, grab the subgoal attribute list and the isNeg
        for idNamePair in subIDNameList:
            sid = idNamePair[0]
            subname = idNamePair[1]

            # get the attribute list for this subgoal
            self.cursor.execute(
                "SELECT attID,attName FROM SubgoalAtt WHERE rid=='" +
                self.prid + "' AND sid=='" + sid + "'")
            attList = self.cursor.fetchall()
            attList = tools.toAscii_multiList(attList)
            attList = [idNamePair[1] for idNamePair in attList]

            # get the isNeg information
            self.cursor.execute(
                "SELECT argName FROM SubgoalAddArgs WHERE rid=='" + self.prid +
                "' AND sid=='" + sid + "'")
            isNeg = self.cursor.fetchone()
            if isNeg:
                isNeg = tools.toAscii_str(isNeg)
            else:
                isNeg = ""

            # ///////////////////////////////////////////////////////// #
            # Map each attribute to a value based on the mappings from
            # the full provAttMap.
            thisSubAttMap = []
            for att in attList:
                #if att == "__WILDCARD__" :
                if att == "_":
                    thisSubAttMap.append([att, None])
                elif att in fullProvDict.keys():
                    thisSubAttMap.append([att, fullProvDict[att]])
                else:
                    thisSubAttMap.append([att, None])

            subgoalInfo.append([subname, isNeg, thisSubAttMap])
            # ///////////////////////////////////////////////////////// #

        return subgoalInfo
Esempio n. 10
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
Esempio n. 11
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 ]
Esempio n. 12
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]
Esempio n. 13
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
Esempio n. 14
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
Esempio n. 15
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
Esempio n. 16
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
Esempio n. 17
0
 def test_toAscii_str_tools(self):
     unitestfile = testPath + "/testfiles/unicodetest.txt"
     with open(unitestfile, "r") as f:
         line = [f.readlines()[0][6:]]
         line = tools.toAscii_str(line)
         self.assertFalse(line.decode('utf-8') == None)
Esempio n. 18
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]
def rewriteRule(rid, sid, attData, cursor):

    print "#################################################################"
    print " ... running REWRITE RULE from rewriteSubgoalsWithWildcards ... #"
    print "#################################################################"

    print "BEFORE : attData : " + str(attData)

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

    # ------------------------------------------- #
    # branch on idb subgoals.
    # if negated subgoal with wildcard is IDB, then ensure all attTypes are defined
    # by pulling attTypes from the IDB goal attribute list.

    if not tools.isFact_only(subgoalName, cursor):
        # get rule id for the idb
        cursor.execute("SELECT rid FROM Rule WHERE goalName=='" + subgoalName +
                       "'")
        idbrid = cursor.fetchone()
        idbrid = tools.toAscii_str(idbrid)

        # get goal schema
        cursor.execute(
            "SELECT attID,attName,attType FROM GoalAtt WHERE rid=='" + idbrid +
            "'")
        idbAttData = cursor.fetchall()
        idbAttData = tools.toAscii_multiList(idbAttData)

        print "idbAttData : " + str(idbAttData)

        # fix original subgoal att list types
        tmp = []
        for i in range(0, len(idbAttData)):
            idb_att = idbAttData[i]
            orig_att = attData[i]

            attID = orig_att[0]
            attName = orig_att[1]
            attType = idb_att[2]

            tmp.append([attID, attName, attType])
        attData = tmp

    # ------------------------------------------- #
    # generate new subgoal name
    newSubgoalName = subgoalName + "_" + tools.getID_4() + "_wildcardrewrite"

    # ------------------------------------------- #
    # generate new subgoal att list
    attNameList = []
    newSubgoalAttList = []
    attID = 0
    for att in attData:
        attName = att[1]
        attType = att[2]

        if not attName == "_" and attName not in attNameList:
            newSubgoalAttList.append([attID, attName, attType])
            attNameList.append(attName)
            attID += 1

    if True:
        print ">> ORIG RULE <<"
        print dumpers.reconstructRule(rid, cursor)
        print ">>>         <<<"
        print "subgoalName       = " + subgoalName
        print "newSubgoalName    = " + newSubgoalName
        print "attData           = " + str(attData)
        print "newSubgoalAttList = " + str(newSubgoalAttList)

    # ------------------------------------------- #
    # update subgoal name
    cursor.execute("UPDATE Subgoals SET subgoalName=='" + newSubgoalName +
                   "' WHERE rid=='" + rid + "' AND sid=='" + sid + "'")

    # ------------------------------------------- #
    # delete old subgoal att data
    arity = len(attData)
    for attID in range(0, arity):
        cursor.execute("DELETE FROM SubgoalAtt WHERE rid=='" + rid +
                       "' AND sid=='" + sid + "' AND attID=='" + str(attID) +
                       "'")

    # ------------------------------------------- #
    # input new subgoal att data
    for att in newSubgoalAttList:
        attID = att[0]
        attName = att[1]
        attType = att[2]

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

    # ------------------------------------------- #
    # add new rule

    print "-------------------------------------"
    print "subgoalName       : " + subgoalName
    print "newSubgoalName    : " + newSubgoalName
    print "attData           : " + str(attData)
    print "newSubgoalAttList : " + str(newSubgoalAttList)
    #tools.bp( __name__, inspect.stack()[0][3], "asdf" )

    newRule = addNewRule(subgoalName, newSubgoalName, attData,
                         newSubgoalAttList, cursor)

    return newRule
Esempio n. 20
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
Esempio n. 21
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
Esempio n. 22
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