Exemple #1
0
def factDump(cursor):

    logging.debug("  FACT DUMP : running process...")

    facts = []

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

    # iterate over fact ids
    newFact = []
    for i in factIDs:
        cursor.execute("SELECT name FROM Fact WHERE fid == '" + str(i) +
                       "'")  # get fact name
        factName = cursor.fetchone()
        factName = tools.toAscii_str(factName)

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

        logging.debug("  FACT DUMP : factList = " + str(factList))

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

        # convert fact info to formatted string
        newFact.append(factName + "(")
        for j in range(0, len(factList)):

            if j < (len(factList) - 1):
                newFact.append(factList[j] + ",")
            else:
                newFact.append(factList[j] + ")")

        if not factTimeArg == "":
            newFact.append("@" + factTimeArg)

        newFact.append(";")  # end all facts with a semicolon
        facts.append(newFact)
        newFact = []

    returnFacts = []
    for f in facts:
        returnFacts.append(''.join(f))

    logging.debug("  FACT DUMP : returning returnFacts = " + str(returnFacts))
    return returnFacts
Exemple #2
0
def factDump( cursor ) :

  print "********************\nProgram Facts :"

  facts = []

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

  # iterate over fact ids
  newFact = []
  for i in factIDs :
    cursor.execute( "SELECT name FROM Fact WHERE fid == '" + str(i) + "'" ) # get fact name
    factName    = cursor.fetchone()
    factName    = tools.toAscii_str( factName )

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

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

    # convert fact info to pretty string
    newFact.append( factName + "(" )
    for j in range(0,len(factList)) :

      # this should only appear if facts are rewritten in the 
      # dedalus rewrite phase.
      if factTimeArg and j==len(factList)-2 :
        continue

      if j < (len(factList) - 1) :
        newFact.append( factList[j] + "," )
      else :
        newFact.append( factList[j] + ")" )
    if not factTimeArg == "" :
      newFact.append( "@" + factTimeArg )

    newFact.append( " ;" ) # end all facts with a semicolon
    facts.append( newFact )
    newFact = []

  # print facts
  for f in facts :
    print ''.join(f)
Exemple #3
0
def eqnDump(cursor):

    logging.debug("  EQN DUMP : running process...")

    eqns = {}

    # get all eqn ids
    cursor.execute("SELECT eid FROM Equation")
    eqnIDs = cursor.fetchall()
    eqnIDs = tools.toAscii_list(eqnIDs)

    # iterate over eqn ids
    for i in eqnIDs:

        # get eqn string
        cursor.execute("SELECT eqn FROM Equation WHERE eid == '" + str(i) +
                       "'")
        eqnStr = cursor.fetchone()
        eqnStr = tools.toAscii_str(eqnStr)

        # get list of variables in the equation
        cursor.execute("SELECT var FROM EquationVars WHERE eid == '" + str(i) +
                       "'")  # list of fact atts
        varList = cursor.fetchall()
        varList = tools.toAscii_list(varList)

        eqns[eqnStr] = varList

    logging.debug("  EQN DUMP : eqns = " + str(eqns))

    return eqns
Exemple #4
0
def isNOTRule(cursor, rid):

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

    if "not_" in goalName:
        return True
    else:
        return False
Exemple #5
0
def singleRuleAttDump(rid, cursor):

    currRuleAttData = {}

    # ================================================= #
    # grab rule name for clarity of explanation

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

    currRuleAttData["goalName"] = goalName

    # ================================================= #
    # grab all goal atts and att types

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

    goalAttTypeMappingArrays = []
    for i in range(0, len(goalAttData)):
        attID = goalAttData[i][0]
        attName = goalAttData[i][1]
        attType = goalAttData[i][2]
        goalAttTypeMappingArrays.append([attID, attName, attType])

    currRuleAttData["goalAttData"] = goalAttTypeMappingArrays

    # ================================================= #
    # grab all subgoal atts and att types
    # [ [ subgoal1, [ [ data1, type1 ], ..., [ dataM, typeM ] ] ],
    #    ...,
    #   [ subgoalK, [ [ data1, type1 ], ..., [ dataN, typeN ] ] ] ]

    # grab all subgoal ids
    cursor.execute("SELECT sid FROM Subgoals WHERE rid=='" + rid + "'")
    sidList = cursor.fetchall()
    sidList = tools.toAscii_list(sidList)

    subgoalAttDataMaps = []
    for sid in sidList:

        # grab the subgoal name for clarity of explanation
        cursor.execute("SELECT subgoalName FROM Subgoals WHERE rid=='" + rid +
                       "' AND sid=='" + sid + "'")
        subgoalName = cursor.fetchone()
        subgoalName = tools.toAscii_str(subgoalName)

        # grab subgoal att list and types
        cursor.execute(
            "SELECT attID,attName,attType FROM SubgoalAtt WHERE rid=='" + rid +
            "' AND sid=='" + sid + "'")
        subgoalAttData = cursor.fetchall()
        subgoalAttData = tools.toAscii_multiList(subgoalAttData)

        # grab attribute maps per subgoal and store in arrays
        subgoalAttTypeMappingArrays = []
        for i in range(0, len(subgoalAttData)):
            attID = subgoalAttData[i][0]
            attName = subgoalAttData[i][1]
            attType = subgoalAttData[i][2]
            subgoalAttTypeMappingArrays.append([attID, attName, attType])

        # save to subgoalAttData
        subgoalAttDataMaps.append(
            [sid, subgoalName, subgoalAttTypeMappingArrays])

    currRuleAttData["subgoalAttData"] = subgoalAttDataMaps

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

    return currRuleAttData
Exemple #6
0
def reconstructRule(rid, cursor):

    #logging.debug( "... running reconstructRule ..." )

    rule = ""

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

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

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

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

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

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

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

        s = subIDs[k]

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

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

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

            # get subgoal time arg
            cursor.execute(
                "SELECT subgoalTimeArg FROM Subgoals WHERE rid == '" +
                str(rid) + "' AND sid == '" + s +
                "'")  # get list of sids for this rule
            subTimeArg = cursor.fetchone()  # assume only one additional arg
            subTimeArg = tools.toAscii_str(subTimeArg)

            # get subgoal additional args
            subAddArg = None
            cursor.execute(
                "SELECT subgoalPolarity FROM Subgoals WHERE rid == '" +
                str(rid) + "' AND sid == '" + s +
                "'")  # get list of sids for this rule
            subAddArg = cursor.fetchone()  # assume only one additional arg
            if not subAddArg == "":
                subAddArg = tools.toAscii_str(subAddArg)
                subAddArg += " "
                newSubgoal += subAddArg

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

            # add in all attributes
            for j in range(0, len(subAtts)):
                if j < (len(subAtts) - 1):
                    newSubgoal += subAtts[j] + ","
                else:
                    newSubgoal += subAtts[j] + ")"

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

        rule += newSubgoal

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

    cursor.execute("SELECT eid,eqn FROM Equation WHERE rid=='" + str(rid) +
                   "'")  # get list of eids for this rule
    eqnList = cursor.fetchall()
    eqnList = tools.toAscii_multiList(eqnList)

    for e in eqnList:
        eid = e[0]
        eqn = e[1]
        rule += "," + str(eqn)

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

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

    return rule
Exemple #7
0
def ruleDump(cursor):

    logging.debug("  RULE DUMP : running process...")

    rules = []

    # --------------------------------------------------------------- #
    #                           GOALS                                 #

    # get all rule ids
    cursor.execute("SELECT rid FROM Rule")
    ruleIDs = cursor.fetchall()
    ruleIDs = tools.toAscii_list(ruleIDs)

    # iterate over rule ids
    newRule = []
    for i in ruleIDs:
        cursor.execute("SELECT goalName FROM Rule WHERE rid == '" + i +
                       "'")  # get goal name
        goalName = cursor.fetchone()
        goalName = tools.toAscii_str(goalName)

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

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

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

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

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

        # iterate over subgoal ids
        for k in range(0, len(subIDs)):
            s = subIDs[k]

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

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

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

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

                # --------------------------------------- #
                # get subgoal additional args
                cursor.execute(
                    "SELECT subgoalPolarity FROM Subgoals WHERE rid == '" + i +
                    "' AND sid == '" + s + "'")
                subAddArg = cursor.fetchone()
                subAddArg = tools.toAscii_str(subAddArg)
                if subAddArg == "notin":
                    subAddArg += " "
                    newRule.append(" " + subAddArg)

                # all subgoals have a name and open paren
                newRule.append(subgoalName + "(")

                # add in all attributes
                for j in range(0, len(subAtts)):
                    if j < (len(subAtts) - 1):
                        newRule.append(subAtts[j] + ",")
                    else:
                        newRule.append(subAtts[j] + ")")

                # conclude with time arg, if applicable
                if not subTimeArg == "":
                    newRule.append("@" + subTimeArg)

                # cap with a comma, if applicable
                if k < len(subIDs) - 1:
                    newRule.append(",")

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

        cursor.execute("SELECT eid,eqn FROM Equation WHERE rid=='" + i +
                       "'")  # get list of eids for this rule
        eqnList = cursor.fetchall()
        eqnList = tools.toAscii_multiList(eqnList)

        for e in eqnList:
            eid = e[0]
            eqn = e[1]
            newRule.append("," + str(eqn))

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

        newRule.append(";")  # end all rules with a semicolon
        rules.append(newRule)
        newRule = []

    logging.debug("  RULE DUMP : rules = " + str(rules))

    returnRules = []
    for r in rules:
        returnRules.append(''.join(r))

    return returnRules
Exemple #8
0
def addClockSubgoal_async(rid, firstSubgoalAtts, secondAtt, timeAtt_snd,
                          timeAtt_deliv, cursor):
    baseAtt = firstSubgoalAtts[0]

    if CLOCKTOOLS_DEBUG:
        print "CLOCKTOOLS_DEBUG: For rule : " + str(
            dumpers.reconstructRule(rid, cursor) +
            "\n    firstSubgoalAtts = " + str(firstSubgoalAtts))

    for c in firstSubgoalAtts:
        if not c == baseAtt:
            if isNotSubgoal(cursor, rid, c) or isDMSubgoal(
                    cursor, rid, c) or isNOTRule(cursor, rid):
                pass
            else:
                sys.exit(
                    "Syntax error:\n   Offending rule:\n      " +
                    dumpers.reconstructRule(rid, cursor) +
                    "\n   The first attribute of all positive subgoals in async rules must be identical. Semantically, the first attribute is expected to represent the message sender.\n    First att list for positive subgoals: "
                    + str(firstSubgoalAtts))

    # get first att in first subgoal, assume specifies 'sender' node
    firstAtt = baseAtt

    # get first att of goal, assume specifies 'reciever' node
    # while we're here, collect the first attribute of this goal
    cursor.execute("SELECT attName FROM GoalAtt WHERE GoalAtt.rid == '" + rid +
                   "' AND GoalAtt.attID == '" + str(0) + "'")
    firstGoalAtt = cursor.fetchone()
    firstGoalAtt = tools.toAscii_str(firstGoalAtt)

    # add clock subgoal
    # clock(Node1, Node2, SndTime)
    subgoalName = "clock"
    subgoalAttList = [firstAtt, secondAtt, timeAtt_snd, timeAtt_deliv]
    subgoalTimeArg = ""
    subgoalAddArgs = [""]

    # generate random ID for subgoal
    sid = tools.getID()

    # save name and time arg
    cursor.execute("INSERT INTO Subgoals VALUES ('" + rid + "','" + sid +
                   "','" + subgoalName + "','" + subgoalTimeArg + "')")

    # save subgoal attributes
    cursor.execute(
        '''SELECT MAX(attID) FROM GoalAtt WHERE GoalAtt.rid == "''' + rid +
        '''"''')
    rawMaxID = cursor.fetchone()
    #newAttID = int(rawMaxID[0]) + 1
    newAttID = 0
    for attName in subgoalAttList:
        if CLOCKTOOLS_DEBUG:
            print rid, sid, subgoalName, subgoalTimeArg, str(newAttID), attName
        #cursor.execute("INSERT INTO SubgoalAtt VALUES ('" + rid + "','" + sid + "','" + str(newAttID) + "','" + attName + "','UNDEFINEDTYPE')")
        if newAttID < 2:
            cursor.execute("INSERT INTO SubgoalAtt VALUES ('" + rid + "','" +
                           sid + "','" + str(newAttID) + "','" + attName +
                           "','string')")
        else:
            cursor.execute("INSERT INTO SubgoalAtt VALUES ('" + rid + "','" +
                           sid + "','" + str(newAttID) + "','" + attName +
                           "','int')")
        newAttID += 1

    # save subgoal additional args
    for addArg in subgoalAddArgs:
        cursor.execute("INSERT INTO SubgoalAddArgs VALUES ('" + rid + "','" +
                       sid + "','" + addArg + "')")

    # reset variables for next async rule
    firstSubgoalAtts = []
    firstGoalAtt = ""