Пример #1
0
def prioritizeNegatedLast_2(rid, subIDs, cursor):

    posSubs = []
    clockSubs = []
    negSubs = []

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

        cursor.execute("SELECT subgoalName,subgoalPolarity \
                     FROM   Subgoals \
                     WHERE rid=='" + rid + "' AND \
                           sid=='" + subID + "'")
        data = cursor.fetchall()
        data = tools.toAscii_multiList(data)
        name = data[0][0]
        sign = data[0][1]

        if name == "clock":
            clockSubs.append(subID)
        elif not sign == "notin":
            posSubs.append(subID)
        else:
            negSubs.append(subID)

    return posSubs, clockSubs, negSubs
Пример #2
0
def dump_clock(cursor):

    logging.debug("...running dumpers_c4 dump_clock...")

    formattedClockFactsList = []

    #cursor.execute( "SELECT * FROM Clock" )
    cursor.execute("SELECT src, dest, sndTime, delivTime FROM Clock")
    clockFacts = cursor.fetchall()
    clockFacts = tools.toAscii_multiList(clockFacts)

    logging.debug("dump_clock: clockFacts = " + str(clockFacts))

    for c in clockFacts:
        logging.debug("c = " + str(c))

        clockF = 'clock('
        for i in range(0, len(c)):
            currData = c[i]
            if i < 2:
                clockF += '"' + currData + '",'
            else:
                clockF += str(currData)
                if i < len(c) - 1:
                    clockF += ","
        clockF += ");" + "\n"  # all clock facts end with a semicolon
        formattedClockFactsList.append(clockF)

    return formattedClockFactsList
Пример #3
0
    def setAttTypes(self):
        allDataList = self.getAttList()  # ordered by appearance in program
        allAttTypes = self.getTypeList(
            allDataList)  # ordered by appearance in program

        #if self.getName() == "bcast" :
        #  print "allDataList = " + str( allDataList )
        #  print "allAttTypes = " + str( allAttTypes )
        #  tools.dumpAndTerm( self.cursor )

        # set att types for this fact
        # ordering is crucial
        attID = 0
        for attType in allAttTypes:
            self.cursor.execute("UPDATE FactAtt SET attType=='" + attType +
                                "' WHERE fid=='" + self.fid + "' AND attID==" +
                                str(attID))
            attID += 1

        # dump table for debugging
        if DEBUG:
            self.cursor.execute("SELECT * FROM FactAtt")
            res = self.cursor.fetchall()
            res = tools.toAscii_multiList(res)
            print "FACT :"
            print self.display()
            print "CONTENTS OF TABLE FactAtt"
            for r in res:
                print r
Пример #4
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
Пример #5
0
    def getClockTriggerRecordList(self):

        # sanity check
        # all clock records adhere to the same arity-4 schema: src, dest, SndTime, DelivTime
        if not len(self.record) == 4:
            tools.bp(
                __name__,
                inspect.stack()[0][3],
                "FATAL ERROR : clock record does not adhere to clock schema(src,dest,SndTime,DelivTime) : \nself.record = "
                + str(self.record))

        # get clock record data
        src = self.record[0]
        dest = self.record[1]
        sndTime = self.record[2]
        delivTime = self.record[3]

        # get all matching (or partially matching, in the case of '_') clock records
        # optimistic by default
        qSRC = "src=='" + src + "'"
        qDEST = " AND dest=='" + dest + "'"
        qSNDTIME = " AND sndTime==" + sndTime + ""
        qDELIVTIME = " AND delivTime==" + delivTime + ""
        qInclusionBool = " AND simInclude=='True'"

        # erase query components as necessary
        if "_" in src:
            qSRC = ""
        if "_" in dest:
            qDEST = ""
        if "_" in sndTime:
            qSNDTIME = ""
        if "_" in delivTime:
            qDELIVTIME = ""

        # handle empty SRC
        if qSRC == "" and not qDEST == "":
            qDEST = qDEST.replace("AND", "")
        elif qSRC == "" and qDEST == "" and not qSNDTIME == "":
            qSNDTIME = qSNDTIME.replace("AND", "")
        elif qSRC == "" and qDEST == "" and qSNDTIME == "" and not qDELIVTIME == "":
            qDELIVTIME = qDELIVTIME.replace("AND", "")
        elif qSRC == "" and qDEST == "" and qSNDTIME == "" and qDELIVTIME == "" and not qInclusionBool == "":
            qInclusionBool = qInclusionBool.replace("AND", "")

        # set query
        query = "SELECT src,dest,sndTime,delivTime FROM Clock WHERE " + qSRC + qDEST + qSNDTIME + qDELIVTIME + qInclusionBool

        if DEBUG:
            print "query = " + str(query)

        print query

        # execute query
        self.cursor.execute(query)
        triggerRecordList = self.cursor.fetchall()
        triggerRecordList = tools.toAscii_multiList(triggerRecordList)

        return triggerRecordList
Пример #6
0
 def test_toAscii_multiList_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_multiList(testmulti) == None)
Пример #7
0
def dump_crash(cursor):

    if DUMPERS_C4_DEBUG:
        print "...running dumpers_c4 dump_crash..."

    formattedCrashFactsList = []

    cursor.execute("SELECT src, dest, sndTime FROM Crash")
    crashFacts = cursor.fetchall()
    crashFacts = tools.toAscii_multiList(crashFacts)

    if len(crashFacts) < 1:
        #tools.bp( __name__, inspect.stack()[0][3], "FATAL ERROR : crash table is empty." )
        cursor.execute(
            "INSERT INTO Crash (src,dest,sndTime) VALUES ('NULL','NULL','99999999')"
        )
        cursor.execute("SELECT src, dest, sndTime FROM Crash")
        crashFacts = cursor.fetchall()
        crashFacts = tools.toAscii_multiList(crashFacts)
        if DUMPERS_C4_DEBUG:
            print "crashFacts = " + str(crashFacts)

    if DUMPERS_C4_DEBUG:
        print "dump_crash: crashFacts = " + str(crashFacts)

    for c in crashFacts:
        if DUMPERS_C4_DEBUG:
            print "c = " + str(c)

        crashF = 'crash('
        for i in range(0, len(c)):
            currData = c[i]
            if i < 2:
                crashF += '"' + currData + '",'
            else:
                crashF += str(currData)
                if i < len(c) - 1:
                    crashF += ","
        crashF += ");" + "\n"  # all crash facts end with a semicolon
        formattedCrashFactsList.append(crashF)

    return formattedCrashFactsList
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
Пример #9
0
    def getGoalAttMaps(self, oridList):
        ogattMap = []

        # get the ordered list of goal attributes for each orid
        oridAttLists = []
        for orid in oridList:
            self.cursor.execute(
                "SELECT attID,attName FROM GoalAtt WHERE rid=='" + orid + "'")
            attList = self.cursor.fetchall()
            attList = tools.toAscii_multiList(attList)
            attList = [aPair[1] for aPair in attList]

            oridAttLists.append([orid, attList])

        # for each orid:attList, connect atts to vals from seed record
        for oridInfo in oridAttLists:
            thisorid = oridInfo[0]
            thisattList = oridInfo[1]

            # sanity check
            # the number of goal atts in the original rule must match
            # the number of values in the seed record.
            if (len(thisattList) < len(self.record)) or (len(thisattList) >
                                                         len(self.record)):
                tools.bp(
                    __name__,
                    inspect.stack()[0][3],
                    "FATAL ERROR : number of attributes in goal attribute list for "
                    + self.name +
                    " does not match the number of values in the seed record:\nattribute list = "
                    + str(thisattList) + "\nrecord = " + str(self.record))

            # map atts to values from the seed record
            thisAttValList = []
            for i in range(0, len(thisattList)):
                att = thisattList[i]
                val = self.record[i]
                thisAttValList.append([att, val])

            ogattMap.append([thisorid, thisAttValList])

        return ogattMap
Пример #10
0
def rewrite_to_datalog( argDict, factMeta, ruleMeta, cursor ) :

  logging.debug( "  REWRITE : running process..." )

  settings_path = argDict[ "settings" ]
  EOT           = argDict[ "EOT" ]

  for rule in ruleMeta :
    rid = rule.rid
    cursor.execute( "SELECT attID,attName FROM GoalAtt WHERE rid=='" + str( rid ) + "'" )
    goal_atts = cursor.fetchall()
    logging.debug( "  DEDT : len( goal_atts ) = " + str( len( goal_atts )) )
    goal_atts = tools.toAscii_multiList( goal_atts )
    logging.debug( "  DEDT : len( goal_atts ) = " + str( len( goal_atts )) )
    logging.debug( "  DEDT : goal_atts (0) = " + str( goal_atts ) )
    logging.debug( "  DEDT : r = " + dumpers.reconstructRule( rid, cursor ) )

  # ----------------------------------------------------------------------------- #
  # dedalus rewrite

  logging.debug( "  REWRITE : calling dedalus rewrites..." )

  allMeta  = dedalusRewriter.rewriteDedalus( argDict, factMeta, ruleMeta, cursor )
  factMeta = allMeta[0]
  ruleMeta = allMeta[1]

  # be sure to fill in all the type info for the new rule definitions
  #setTypes.setTypes( cursor, argDict, ruleMeta )

  for rule in ruleMeta :
    rid = rule.rid
    cursor.execute( "SELECT attID,attName FROM GoalAtt WHERE rid=='" + str( rid ) + "'" )
    goal_atts = cursor.fetchall()
    goal_atts = tools.toAscii_multiList( goal_atts )
    #logging.debug( "  DEDT : goal_atts (1) = " + str( goal_atts ) )
    logging.debug( "  DEDT : rule.ruleData = " + str( rule.ruleData ) )

  # ----------------------------------------------------------------------------- #
  # other rewrites
  # first get parameters for which rewrites to run.

  # ========== WILDCARD ========== #
  try :
    rewriteWildcards = tools.getConfig( settings_path, "DEFAULT", "WILDCARD_REWRITES", bool )
  except ConfigParser.NoOptionError :
    logging.warning( "WARNING : no 'WILDCARD_REWRITES' defined in 'DEFAULT' section of " + settings_path + \
                     "...running without wildcard rewrites." )
    rewriteWildcards = False
    pass

  # ========== DM ========== #
  try :
    RUN_DM = tools.getConfig( settings_path, "DEFAULT", "DM", bool )
  except ConfigParser.NoOptionError :
    logging.warning( "WARNING : no 'DM' defined in 'DEFAULT' section of " + settings_path + \
                     "...running without dm rewrites" )
    RUN_DM = False
    pass

  # ========== NW_DOM_DEF ========== #
  try :
    NW_DOM_DEF = tools.getConfig( settings_path, "DEFAULT", "NW_DOM_DEF", str )
  except ConfigParser.NoOptionError :
    raise Exception( "no 'NW_DOM_DEF' defined in 'DEFAULT' section of " + settings_path + \
                     ". aborting..." )

  # ========== COMBO ========== #
  try:
    RUN_COMB = tools.getConfig( settings_path, "DEFAULT", "COMB", bool )
  except ConfigParser.NoOptionError :
    logging.info( "WARNING : no 'COMB' defined in 'DEFAULT' section of " + settings_path + \
                  "...running without combo rewrites" )
    RUN_COMB = False
    pass

  # ========== IEDB ========== #
  try :
    RUN_IEDB_REWRITES = tools.getConfig( settings_path, "DEFAULT", "IEDB_REWRITES", bool )
  except ConfigParser.NoOptionError :
    logging.info( "WARNING : no 'IEDB_REWRITES' defined in 'DEFAULT' section of " + settings_path + \
                  "...running without iedb rewrites" )
    RUN_IEDB_REWRITES = False
    pass

  # ----------------------------------------------------------------------------- #
  # do wildcard rewrites
  # always do wildcard rewrites in prep for negative writes.

  if rewriteWildcards or \
     ( NW_DOM_DEF == "sip"         and \
       ( argDict[ "neg_writes" ] == "dm" or \
         argDict[ "neg_writes" ] == "combo" ) ) :

    logging.debug( "  REWRITE : calling wildcard rewrites..." )

    ruleMeta = rewrite_wildcards.rewrite_wildcards( ruleMeta, cursor )

#    for rule in ruleMeta :
#      #logging.debug( "rule.ruleData = " + str( rule.ruleData ) )
#      logging.debug( "  REWRITE : (1) r = " + dumpers.reconstructRule( rule.rid, rule.cursor ) )
#    #sys.exit( "blah2" )
  
#    for rule in ruleMeta :
#      logging.debug( "  DEDT : rule.ruleData (2) = " + str( rule.ruleData ) )
  
    # be sure to fill in all the type info for the new rule definitions
    logging.debug( "  REWRITE : running setTypes after wildcard rewrites." )
    setTypes.setTypes( cursor, argDict, ruleMeta )
  
    update_goal_types( ruleMeta )
  else :
    setTypes.setTypes( cursor, argDict, ruleMeta )

  # ----------------------------------------------------------------------------- #
  # iedb rewrites 

  if RUN_IEDB_REWRITES                 or \
     ( NW_DOM_DEF == "sip"         and \
       ( argDict[ "neg_writes" ] == "dm" or \
         argDict[ "neg_writes" ] == "combo" ) ) :

    logging.debug( "  REWRITE : calling iedb rewrites..." )
    factMeta, ruleMeta = iedb_rewrites.iedb_rewrites( factMeta, \
                                                      ruleMeta, \
                                                      cursor, \
                                                      settings_path )

#    for rule in ruleMeta :
#      rid = rule.rid
#      cursor.execute( "SELECT attID,attName FROM GoalAtt WHERE rid=='" + str( rid ) + "'" )
#      goal_atts = cursor.fetchall()
#      goal_atts = tools.toAscii_multiList( goal_atts )
#      logging.debug( "  DEDT : goal_atts (3) = " + str( goal_atts ) )

    #for rule in ruleMeta :
    #  print c4_translator.get_c4_line( rule.ruleData, "rule" )
    #for fact in factMeta :
    #  print c4_translator.get_c4_line( fact.factData, "fact" )
    #sys.exit( "asdf" )

    # be sure to fill in all the type info for the new rule definitions
    logging.debug( "  REWRITE : running setTypes after iedb rewrites." )
    setTypes.setTypes( cursor, argDict, ruleMeta )

  # ----------------------------------------------------------------------------- #
  # do dm rewrites

  if argDict[ "neg_writes" ] == "dm" :

    logging.debug( "  REWRITE : calling dm rewrites..." )
    factMeta, ruleMeta = dm.dm( factMeta, ruleMeta, cursor, argDict ) # returns new ruleMeta

    logging.debug( "  REWRITE : final dm program lines:" )
    for rule in ruleMeta :
      logging.debug( dumpers.reconstructRule( rule.rid, rule.cursor ) )
    #sys.exit( "blah" )

    # be sure to fill in all the type info for the new rule definitions
    logging.debug( "  REWRITE : running setTypes after dm rewrites." )
    setTypes.setTypes( cursor, argDict, ruleMeta )

  # ----------------------------------------------------------------------------- #
  # do combo rewrites

  if argDict[ "neg_writes" ] == "comb" :

    # collect the results from the original program
    original_prog = c4_translator.c4datalog( argDict, cursor )
    results_array = c4_evaluator.runC4_wrapper( original_prog, argDict )
    parsedResults = tools.getEvalResults_dict_c4( results_array )

    # run the neg rewrite for combinatorial approach
    # returns a new ruleMeta
    logging.debug( "  REWRITE : calling combo rewrites..." )
    ruleMeta, factMeta = combitorialNegRewriter.neg_rewrite( cursor, \
                                                             argDict, \
                                                             settings_path, 
                                                             ruleMeta, \
                                                             factMeta, \
                                                             parsedResults ) 

  if argDict[ "neg_writes" ] == "combo" :

    logging.debug( "  REWRITE : calling combo rewrites..." )
    factMeta, ruleMeta = combo.combo( factMeta, ruleMeta, cursor, argDict )

    #for r in ruleMeta :
    #  print dumpers.reconstructRule( r.rid, r.cursor )
    #sys.exit( "f**k" )

#  for rule in ruleMeta :
#    rid = rule.rid
#    cursor.execute( "SELECT attID,attName FROM GoalAtt WHERE rid=='" + str( rid ) + "'" )
#    goal_atts = cursor.fetchall()
#    goal_atts = tools.toAscii_multiList( goal_atts )
#    logging.debug( "  DEDT : goal_atts (2) = " + str( goal_atts ) )

  # ----------------------------------------------------------------------------- #
  # provenance rewrites

  #logging.debug( "  REWRITE : before prov dump :" )
  #for rule in ruleMeta :
  #  print rule
  #  logging.debug( "  REWRITE : (0) r = " + printRuleWithTypes( rule.rid, cursor ) )
  #sys.exit( "blah2" )

  # add the provenance rules to the existing rule set
  logging.debug( "  REWRITE : calling provenance rewrites..." )
  ruleMeta.extend( provenanceRewriter.rewriteProvenance( ruleMeta, cursor, argDict ) )

  #for rule in ruleMeta :
  #  #logging.debug( "rule.ruleData = " + str( rule.ruleData ) )
  #  logging.debug( "  REWRITE : (1) rid = " + str( rule.rid ) + " : " + dumpers.reconstructRule( rule.rid, rule.cursor ) )
  #sys.exit( "blah2" )

  # be sure to fill in all the type info for the new rule definitions
  logging.debug( "  REWRITE : running setTypes after provenance rewrites." )
  setTypes.setTypes( cursor, argDict, ruleMeta )

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

#  for rule in ruleMeta :
#    logging.debug( "rule.ruleData = " + str( rule.ruleData ) )
#    logging.debug( "  REWRITE : (2) r = " + dumpers.reconstructRule( rule.rid, rule.cursor ) )
#  sys.exit( "blah2" )

  logging.debug( "  REWRITE : ...done." )

  return [ factMeta, ruleMeta ]
Пример #11
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
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
Пример #13
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]
Пример #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
Пример #15
0
def get_program(table_list,
                factMeta,
                ruleMeta,
                sip_ruleData=None,
                sip_type_list=None):

    program_lines = []

    # get defines
    defines = []
    for rule in ruleMeta:
        type_list = [t[1] for t in rule.goal_att_type_list]
        rule_define = "define(" + \
                        rule.relationName + "," + \
                        "{" + ",".join( type_list ) + "});"
        if not rule_define in defines:
            defines.append(rule_define)
    for fact in factMeta:
        type_list = [t[1] for t in fact.dataListWithTypes]
        fact_define = "define(" + \
                      fact.factData[ "relationName" ] + "," + \
                      "{" + ",".join( type_list ) + "});"
        if not fact_define in defines:
            defines.append(fact_define)

    # get sip program line
    if sip_ruleData and sip_type_list:
        sip_line = c4_translator.get_c4_line(sip_ruleData, "rule")
        sip_define = "define(" + \
                          sip_ruleData[ "relationName" ] + "," + \
                          "{" + ",".join( sip_type_list ) + "});"

    # get program lines
    rule_fact_lines = []
    for rule in ruleMeta:
        rule_fact_lines.append(c4_translator.get_c4_line(
            rule.ruleData, "rule"))

    for fact in factMeta:
        rule_fact_lines.append(c4_translator.get_c4_line(
            fact.factData, "fact"))

    # build program
    if sip_ruleData and sip_type_list:
        defines.append(sip_define)
        rule_fact_lines = [sip_line] + rule_fact_lines

    if need_clocks(ruleMeta):
        ruleMeta[0].cursor.execute(
            "SELECT src,dest,sndTime,delivTime FROM Clock")
        clock_facts = ruleMeta[0].cursor.fetchall()
        clock_facts = tools.toAscii_multiList(clock_facts)
        for cf in clock_facts:
            rule_fact_lines.append( 'clock("'  + cf[0] + \
                                         '","' + cf[1] + \
                                         '",'  + str( cf[2] ) + \
                                         ','   + str( cf[3] ) + ');' )
        defines.append("define(clock,{string,string,int,int});")

    program_lines.extend(defines)
    program_lines.extend(rule_fact_lines)

    # get table list
    # sever pointer to original table_list
    if sip_ruleData and sip_type_list:
        sip_table_list = table_list + [sip_ruleData["relationName"]]
        return [program_lines, sip_table_list]
    else:
        return [program_lines, table_list]
Пример #16
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 ]
Пример #17
0
def get_domcomp( target_ruleSet, \
                 table_list, \
                 factMeta, \
                 ruleMeta, \
                 argDict, \
                 rid_to_rule_meta_map ) :

    logging.debug("  GET DOMCOMP : target_ruleSet :")
    for t in target_ruleSet:
        logging.debug("    " + c4_translator.get_c4_line(t.ruleData, "rule"))
    #sys.exit( "shit" )

    cursor = ruleMeta[0].cursor  # it's all the same db cursor
    target_name = target_ruleSet[0].relationName

    program_lines = []
    domcomp_table_list = copy.deepcopy(table_list)

    # get defines
    defines = []
    for rule in ruleMeta:
        type_list = [t[1] for t in rule.goal_att_type_list]
        rule_define = "define(" + \
                        rule.relationName + "," + \
                        "{" + ",".join( type_list ) + "});"
        if not rule_define in defines:
            defines.append(rule_define)
    for fact in factMeta:
        type_list = [t[1] for t in fact.dataListWithTypes]
        fact_define = "define(" + \
                      fact.factData[ "relationName" ] + "," + \
                      "{" + ",".join( type_list ) + "});"
        if not fact_define in defines:
            defines.append(fact_define)

    # check for user-defined not_ constraints
    if user_constriaints_exist(target_name, factMeta):
        pass

    else:

        # otherwise, build adom rules
        adom_rule_meta = dm.buildAdom(factMeta, cursor)
        for arm in adom_rule_meta:
            program_lines.append(
                c4_translator.get_c4_line(arm.ruleData, "rule"))
            if arm.relationName == "adom_string":
                arm_type = "string"
                domcomp_table_list.append("adom_string")
            else:
                arm_type = "int"
                domcomp_table_list.append("adom_int")
            arm_define = "define(" + \
                         arm.ruleData[ "relationName" ] + "," + \
                         "{" + arm_type + "});"
            if not arm_define in defines:
                defines.append(arm_define)

    # get domcomp rule
    if user_constriaints_exist(target_name, factMeta):
        domcomp_ruleData, domcomp_type_list = get_domcomp_rule( target_ruleSet, \
                                                                True, \
                                                                ruleMeta, \
                                                                rid_to_rule_meta_map )
    else:
        domcomp_ruleData, domcomp_type_list = get_domcomp_rule( target_ruleSet, \
                                                                False, \
                                                                ruleMeta, \
                                                                rid_to_rule_meta_map )
        domcomp_define = "define(" + \
                         domcomp_ruleData[ "relationName" ] + "," + \
                         "{" + ",".join( domcomp_type_list ) + "});"
        defines.append(domcomp_define)

    program_lines.append(c4_translator.get_c4_line(domcomp_ruleData, "rule"))
    domcomp_table_list.append("domcomp_not_" + target_name)

    # add all other rules
    rule_fact_lines = []
    for rule in ruleMeta:
        rule_fact_lines.append(c4_translator.get_c4_line(
            rule.ruleData, "rule"))
    for fact in factMeta:
        rule_fact_lines.append(c4_translator.get_c4_line(
            fact.factData, "fact"))

    # deal with clocks.
    if need_clocks(ruleMeta):
        ruleMeta[0].cursor.execute(
            "SELECT src,dest,sndTime,delivTime FROM Clock")
        clock_facts = ruleMeta[0].cursor.fetchall()
        clock_facts = tools.toAscii_multiList(clock_facts)
        for cf in clock_facts:
            rule_fact_lines.append( 'clock("'  + cf[0] + \
                                         '","' + cf[1] + \
                                         '",'  + str( cf[2] ) + \
                                         ','   + str( cf[3] ) + ');' )
        defines.append("define(clock,{string,string,int,int});")

    # run program
    program_lines = defines + program_lines + rule_fact_lines
    parsedResults = run_program_c4([program_lines, domcomp_table_list],
                                   argDict)

    return parsedResults["domcomp_not_" + target_name]
Пример #18
0
def collectUniversalBoundSubgoals(randomDMRID, parentRID, sidInParent, posName,
                                  parentName, univAttData, cursor):

    print
    print "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
    print "BUILDING UNIVERSAL BOUND SUBGOALS"
    print "randomDMRID rule : " + dumpers.reconstructRule(randomDMRID, cursor)
    print "parentRID rule   : " + dumpers.reconstructRule(parentRID, cursor)
    print "sidInParent      : " + sidInParent
    print "posName          : " + posName
    print "parentName       : " + parentName

    universalBoundSubgoals = []

    # ===================================================================== #
    # ===================================================================== #

    # ----------------------------------- #
    # build parent dom subgoal            #
    # ----------------------------------- #
    parentSubgoalName = "dom_" + parentName + "_evalres"

    print "parentSubgoalName : " + parentSubgoalName

    # ---------------------------------------------- #
    # get parent rule attribute list                 #
    # ---------------------------------------------- #
    cursor.execute("SELECT attID,attName,attType FROM GoalAtt WHERE rid=='" +
                   parentRID + "'")
    parentGoalAttList = cursor.fetchall()
    parentGoalAttList = tools.toAscii_multiList(parentGoalAttList)

    # ---------------------------------------------- #
    # get negated subgoal attribute list from parent #
    # ---------------------------------------------- #
    cursor.execute(
        "SELECT attID,attName,attType FROM SubgoalAtt WHERE rid=='" +
        parentRID + "' AND sid=='" + sidInParent + "'")
    subAttDataInParent = cursor.fetchall()
    subAttDataInParent = tools.toAscii_multiList(subAttDataInParent)

    # --------------------------------------------------------------- #
    # map parent goal att indexes to subgoal att indexes or wildcards #
    # --------------------------------------------------------------- #
    parentSubgoalAttIndexMap = {}
    for paratt in parentGoalAttList:
        pattID = paratt[0]
        pattName = paratt[1]
        subIndexes = []
        for subatt in subAttDataInParent:
            sattID = subatt[0]
            sattName = subatt[1]
            if pattName == sattName:
                subIndexes.append(sattID)
        parentSubgoalAttIndexMap[pattID] = subIndexes

    # ------------------------------------------------------------------------------------------------------ #
    # map parent subgoal att vars to the corresponding att vars in the uniform attribute set for the DM rule #
    # ------------------------------------------------------------------------------------------------------ #
    # get uniform attribute list
    cursor.execute("SELECT attID,attName,attType FROM GoalAtt WHERE rid=='" +
                   randomDMRID + "'")
    uniformAttList = cursor.fetchall()
    uniformAttList = tools.toAscii_multiList(uniformAttList)

    # populate map
    subToUniformMap = {}
    for i in range(0, len(subAttDataInParent)):
        subatt = subAttDataInParent[i]
        subAttName = subatt[1]

        subToUniformMap[subAttName] = uniformAttList[i]

    # ---------------------------------------------------------------------------------- #
    # map parent goal att vars to vars from the uniform att list for the negated subgoal #
    # ---------------------------------------------------------------------------------- #
    parentSubgoalAttMap = {}
    for parAttIndex in parentSubgoalAttIndexMap:
        correspondingSubAttIndexList = parentSubgoalAttIndexMap[parAttIndex]

        # this attribute in the parent goal does not appear in the negated subgoal att list.
        # use a wildcard.
        if correspondingSubAttIndexList == []:
            parAttType = parentGoalAttList[parAttIndex][2]
            parentSubgoalAttMap[parAttIndex] = [parAttIndex, "_", parAttType]

        # this attribute in the parent goal appears in the negated subgoal att list.
        # replace with the corresponding uniform attribute variable for the subgoal rewrite.
        else:
            for subAttIndex in correspondingSubAttIndexList:
                parentRuleSubAttVar = subAttDataInParent[subAttIndex][1]
                uniformVar = subToUniformMap[parentRuleSubAttVar]
            parentSubgoalAttMap[
                parAttIndex] = uniformVar  # should be identical across subgoal indexes

    # -------------------- #
    # build parent subgoal #
    # -------------------- #
    # save subgoal contents in a dictionary
    parentSubgoal = {}
    parentSubgoal["sid"] = tools.getID()
    parentSubgoal["subgoalName"] = parentSubgoalName
    parentSubgoal["subgoalTimeArg"] = ""  # default
    parentSubgoal["subgoalAttDict"] = parentSubgoalAttMap
    parentSubgoal["argName"] = ""

    # ============================================================== #
    # ============================================================== #

    # ----------------------------------- #
    # build original dom subgoal          #
    # ----------------------------------- #
    negatedPositiveSubgoalName = "dom_" + posName + "_evalres"

    # ----------------------------------------------- #
    # map parent subgoal att vars to uniform att vars #
    # ----------------------------------------------- #
    # correctness depends upon the schemas of the positive and not_
    # definitions of the targeted negated subgoal to be identical.
    # this can only be guaranteed after the uniformity rewrite.
    negatedPositiveSubgoalAttMap = {}
    for att in subAttDataInParent:
        attID = att[0]
        attName = att[1]
        if attName == "_":
            attType = uniformAttList[attID][2]
            negatedPositiveSubgoalAttMap[attID] = [attID, "_", attType]
        else:
            attName = uniformAttList[attID][1]
            attType = uniformAttList[attID][2]
            negatedPositiveSubgoalAttMap[attID] = [attID, attName, attType]

    # ------------------------------ #
    # build negated positive subgoal #
    # ------------------------------ #
    negatedPositiveSubgoal = {}
    negatedPositiveSubgoal["sid"] = tools.getID()
    negatedPositiveSubgoal["subgoalName"] = negatedPositiveSubgoalName
    negatedPositiveSubgoal["subgoalTimeArg"] = ""  # default
    negatedPositiveSubgoal["subgoalAttDict"] = negatedPositiveSubgoalAttMap
    negatedPositiveSubgoal["argName"] = "notin"  # negated positive

    # ============================================================== #
    # ============================================================== #

    # ----------------------------------- #
    # build original dom subgoal          #
    # ----------------------------------- #
    positivePositiveSubgoalName = "dom_" + posName + "_evalres"

    # ----------------------------------------------- #
    # map parent subgoal att vars to uniform att vars #
    # ----------------------------------------------- #
    # correctness depends upon the schemas of the positive and not_
    # definitions of the targeted negated subgoal to be identical.
    # this can only be guaranteed after the uniformity rewrite.
    #
    # the attributes in the positive domain subgoal for the targetted negated relation
    # are exactly the in the negated domain subgoal relation in this rule
    # minus the attributes incoporated into the parent domain subgoal of this rule.

    parentEvalRes_atts_only = [
        parentSubgoalAttMap[x][1] for x in parentSubgoalAttMap
    ]

    positivePositiveSubgoalAttMap = {}
    for att in univAttData:
        attID = att[0]
        attName = att[1]
        attType = att[2]
        if attName in parentEvalRes_atts_only:
            attName = "_"
        positivePositiveSubgoalAttMap[attID] = [attID, attName, attType]

    # ------------------------------ #
    # build negated positive subgoal #
    # ------------------------------ #
    positivePositiveSubgoal = {}
    positivePositiveSubgoal["sid"] = tools.getID()
    positivePositiveSubgoal["subgoalName"] = positivePositiveSubgoalName
    positivePositiveSubgoal["subgoalTimeArg"] = ""  # default
    positivePositiveSubgoal["subgoalAttDict"] = positivePositiveSubgoalAttMap
    positivePositiveSubgoal["argName"] = ""  # positive positive

    # ============================================================== #
    # ============================================================== #

    universalBoundSubgoals = []

    if not allWildcardAtts(parentSubgoal):
        universalBoundSubgoals.append(parentSubgoal)

    if not allWildcardAtts(negatedPositiveSubgoal):
        universalBoundSubgoals.append(negatedPositiveSubgoal)

    if not allWildcardAtts(positivePositiveSubgoal):
        universalBoundSubgoals.append(positivePositiveSubgoal)

    print "DONE BUILDING UNIVERSAL BOUND SUBGOALS"

    return universalBoundSubgoals
Пример #19
0
    def mergeMaps(self, allIDPairs, ogattMaps):
        pgattMaps = []

        # get list of all prids
        pridList = [aPair[1] for aPair in allIDPairs]

        for prid in pridList:

            # get the goal att list for this provenance rule
            self.cursor.execute(
                "SELECT attID,attName FROM GoalAtt WHERE rid=='" + prid + "'")
            pgattList = self.cursor.fetchall()
            pgattList = tools.toAscii_multiList(pgattList)
            pgattList = [arr[1] for arr in pgattList]

            # get corresponding original rule id
            orid = self.getORID(prid, allIDPairs)

            # ///////////////////////////////////////////////////////// #
            # get map of goal atts for the original rule
            # to seed record values.
            # ------------------------------------------------------------- #
            # Transform into dictionary for convenience
            thisogattMap = self.getOGattMap(orid, ogattMaps)

            ogattDict = {}
            for arr in thisogattMap:
                att = arr[0]
                val = arr[1]

                # sanity check
                if att in ogattDict.keys() and not val == ogattDict[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 ] = " +
                        ogattDict[att])
                else:  # add to dictionary
                    ogattDict[att] = val
            # ------------------------------------------------------------- #

            # map provenance goal atts to values from the seed record based on
            # the attribute mappings in the original rule.
            # unspecified atts are mapped to None.
            # WILDCARDs are mapped to None.
            pattMapping = []
            for patt in pgattList:
                #if patt == "__WILDCARD__" :
                if patt == "_":
                    pattMapping.append([patt, None])
                elif patt in ogattDict.keys():
                    pattMapping.append([patt, ogattDict[patt]])
                else:
                    pattMapping.append([patt, None])

            pgattMaps.append([prid, pattMapping])
            # ///////////////////////////////////////////////////////// #

        if self.name == "bcast":
            print "allIDPairs = " + str(allIDPairs)
            print "ogattMaps  = " + str(ogattMaps)
            print "pgattMaps  = " + str(pgattMaps)
            #tools.bp( __name__, inspect.stack()[0][3], "f*****g with mergeMaps" )

        return pgattMaps
Пример #20
0
    def getAllIDPairs(self):

        # ---------------------------------------------------------- #
        #                      ORIG RULE DATA                        #
        # ---------------------------------------------------------- #
        # get all original rule ids associated with the name
        self.cursor.execute("SELECT rid FROM Rule WHERE goalName='" +
                            self.name + "'")
        origIDs = self.cursor.fetchall()
        origIDs = tools.toAscii_multiList(origIDs)

        # get the complete attList and subgoalList associated with each original rule
        # store as arrays in an array [ rid, [attList], [subgoalList] ]
        origInfo = []
        for orid in origIDs:
            orid = orid[0]  # origIDs is a list of singular lists

            # get attList
            self.cursor.execute(
                "SELECT attID,attName FROM SubgoalAtt WHERE rid='" + orid +
                "'")
            attList = self.cursor.fetchall()
            attList = tools.toAscii_multiList(attList)

            # get subgoalList
            self.cursor.execute(
                "SELECT subgoalName FROM Subgoals WHERE rid='" + orid + "'")
            subgoalList = self.cursor.fetchall()
            subgoalList = tools.toAscii_multiList(subgoalList)

            origInfo.append([orid, attList, subgoalList])

        # ---------------------------------------------------------- #
        #                      PROV RULE DATA                        #
        # ---------------------------------------------------------- #
        # get all provenance rule ids associated with the name
        # first, get all rule id, rule name pairs
        self.cursor.execute("SELECT rid,goalName FROM Rule")
        idNamePairs = self.cursor.fetchall()
        idNamePairs = tools.toAscii_multiList(idNamePairs)

        # next, collect the rids of goalnames starting with self.name+"_prov"
        provIDs = []
        for idName in idNamePairs:
            currID = idName[0]
            currName = idName[1]
            if currName.startswith(self.name + "_prov"):
                provIDs.append(currID)

        # get the complete attList and subgoalList associated with each original rule
        # store as arrays in an array [ rid, [attList], [subgoalList] ]
        provInfo = []
        for prid in provIDs:
            # get attList
            self.cursor.execute(
                "SELECT attID,attName FROM SubgoalAtt WHERE rid='" + prid +
                "'")
            attList = self.cursor.fetchall()
            attList = tools.toAscii_multiList(attList)

            # get subgoalList
            self.cursor.execute(
                "SELECT subgoalName FROM Subgoals WHERE rid='" + prid + "'")
            subgoalList = self.cursor.fetchall()
            subgoalList = tools.toAscii_multiList(subgoalList)

            provInfo.append([prid, attList, subgoalList])

        # ---------------------------------------------------------- #
        #                         MATCH                              #
        # ---------------------------------------------------------- #
        # match original rids with provenance rids by matching
        # attLists and subgoals.
        idPairs = []
        for origIDInfo in origInfo:
            orid = origIDInfo[0]
            oAttList = origIDInfo[1]
            oSubList = origIDInfo[2]

            for provIDInfo in provInfo:
                prid = provIDInfo[0]
                pAttList = provIDInfo[1]
                pSubList = provIDInfo[2]

                if self.checkListEquality(oAttList,
                                          pAttList) and self.checkListEquality(
                                              oSubList, pSubList):
                    idPairs.append([orid, prid])  # save pair

        return idPairs
Пример #21
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
Пример #22
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]
Пример #23
0
def get_pilot_program( domcomp_bindings_tup, \
                       target_ruleSet, \
                       target_domcomp, \
                       factMeta, \
                       ruleMeta ) :

    logging.debug( "  GET PILOT PROGRAM : domcomp_bindings_tup = " + \
                   str( domcomp_bindings_tup ) )

    # collect the unidom facts associated with this test
    test_unidom = []

    # 0. build initial program.
    #    work with a new ruleData element called "sip_bindings",
    #    which is a list of binary lists matching attributes to
    #    sip binding values.

    # [ { 'ruleData'     : { <ruleData> },
    #     'sip_bindings' : [ <data tuple> ],
    #     'sub_colors'   : [ <True | False> ] }, ... ]
    all_pilot_rules = []

    # [ { 'factData' : { <factData> }, ... ]
    all_pilot_facts = []
    for fact in factMeta:
        new_fact = {}
        new_fact["factData"] = fact.factData
        all_pilot_facts.append(new_fact)

    logging.debug("  GET PILOT PROGRAM : orig facts :")
    for tup in all_pilot_facts:
        logging.debug("     " + str(tup))

    for rule in target_ruleSet:
        a_pilot_rule = {}

        if not rule.relationName.startswith( "exidom_" ) and \
           not rule.relationName.startswith( "orig_" ) :

            a_pilot_rule["ruleData"] = copy.deepcopy(rule.ruleData)
            a_pilot_rule["sip_bindings"] = domcomp_bindings_tup
            a_pilot_rule[ "sub_colors" ]   = [ False for i in \
                                               a_pilot_rule[ "ruleData" ][ "subgoalListOfDicts" ] ]
            assert( len( a_pilot_rule[ "ruleData" ][ "subgoalListOfDicts" ] ) == \
                    len( a_pilot_rule[ "sub_colors" ] ) )

            all_pilot_rules.append(a_pilot_rule)

            # spawn any new edbs to support the bindings.
            new_bound_facts = get_new_bound_facts(a_pilot_rule, factMeta,
                                                  ruleMeta)
            logging.debug("  GET PILOT PROGRAM : initial new pilot facts :")
            for tup in new_bound_facts:
                logging.debug("     " + str(tup))
            all_pilot_facts.extend(new_bound_facts)
            test_unidom.extend(new_bound_facts)

    # 1. push down domcomp bindings until no more bound idbs exist.
    COUNTER = 0
    while bound_idbs_still_exist( target_ruleSet[0].relationName, \
                                  all_pilot_rules, \
                                  ruleMeta ) :

        # 1a. get info on a bound idb
        target_bound_idb, pilot_rule, sub_index = get_bound_idb( target_ruleSet[0].relationName, \
                                                                 all_pilot_rules, \
                                                                 ruleMeta )

        # 1b. get the rule corresponding to the bound idb
        new_pilot_rules = get_defn( target_bound_idb[ "subgoalName" ], \
                                    pilot_rule, \
                                    sub_index, \
                                    ruleMeta )
        pilot_rule["sub_colors"][sub_index] = True
        for npr in new_pilot_rules:
            #all_pilot_rules.append( npr )
            if not pilot_rule_already_exists(npr, all_pilot_rules):
                all_pilot_rules.append(npr)
                logging.debug( "  GET NEW BOUND FACTS : adding '" + \
                               c4_translator.get_c4_line( npr[ "ruleData" ], "rule" ) + "'" )
            else:
                logging.debug( "  GET NEW BOUND FACTS : not adding '" + \
                               c4_translator.get_c4_line( npr[ "ruleData" ], "rule" ) + "'" )

        # 1c. push down tuple bindings to target rule edbs and save the new facts
        for npr in new_pilot_rules:
            # spawn any new edbs to support the bindings.
            new_bound_facts = get_new_bound_facts(npr, factMeta, ruleMeta)
            logging.debug("  GET NEW BOUND FACTS : while loop additions :")
            for tup in new_bound_facts:
                logging.debug("     " + str(tup))
            all_pilot_facts.extend(new_bound_facts)
            test_unidom.extend(new_bound_facts)

        if COUNTER == 100:
            for l in all_pilot_rules:
                print c4_translator.get_c4_line(l["ruleData"], "rule")
                print l["sip_bindings"]
                print l["sub_colors"]
            print bound_idbs_still_exist( target_ruleSet[0].relationName, \
                                          all_pilot_rules, \
                                          ruleMeta )
            sys.exit("shit")

        COUNTER += 1

    # 2. convert program into a list of strings
    rule_fact_lines = []
    for r in all_pilot_rules:
        line = c4_translator.get_c4_line(r["ruleData"], "rule")
        if not line in rule_fact_lines:
            rule_fact_lines.append(line)

    #for l in program_lines :
    #  print l
    #sys.exit( "shit" )

    # 3. add the program facts
    for f in all_pilot_facts:
        rule_fact_lines.append(c4_translator.get_c4_line(
            f["factData"], "fact"))

    # 4. derive and all all the defines
    table_list = []
    defines = []
    for pr in all_pilot_rules:
        this_rule = None
        for rule in ruleMeta:
            if rule.relationName == pr["ruleData"]["relationName"]:
                this_rule = rule
                break
        type_list = [t[1] for t in this_rule.goal_att_type_list]
        rule_define = "define(" + \
                        rule.relationName + "," + \
                        "{" + ",".join( type_list ) + "});"
        if not rule_define in defines:
            defines.append(rule_define)
            table_list.append(rule.relationName)
    for fact in factMeta:
        type_list = [t[1] for t in fact.dataListWithTypes]
        fact_define = "define(" + \
                      fact.factData[ "relationName" ] + "," + \
                      "{" + ",".join( type_list ) + "});"
        if not fact_define in defines:
            defines.append(fact_define)
            table_list.append(fact.factData["relationName"])

    # deal with clocks.
    if need_clocks(ruleMeta):
        ruleMeta[0].cursor.execute(
            "SELECT src,dest,sndTime,delivTime FROM Clock")
        clock_facts = ruleMeta[0].cursor.fetchall()
        clock_facts = tools.toAscii_multiList(clock_facts)
        for cf in clock_facts:
            rule_fact_lines.append( 'clock("'  + cf[0] + \
                                         '","' + cf[1] + \
                                         '",'  + str( cf[2] ) + \
                                         ','   + str( cf[3] ) + ');' )
        defines.append("define(clock,{string,string,int,int});")

    program_lines = defines + rule_fact_lines

    #for l in program_lines :
    #  print l
    #print table_list
    #sys.exit( "f**k" )

    return program_lines, table_list
Пример #24
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
Пример #25
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