示例#1
0
def rewriteRewritten(cursor):

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

    # grab all existing next rule ids
    rewrittenRuleIDs = getRewrittenRuleIDs(cursor)

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

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

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

    if DEDALUSREWRITER_DEBUG:
        print " ... done asynchronous rewrite ... "
示例#2
0
def equivalent_rule_already_exists(orig_parent_rule, ruleMeta):
    logging.debug( "  EQUIVALENT RULE ALREADY EXISTS : checking:\n     " + \
                   dumpers.reconstructRule( orig_parent_rule.rid, orig_parent_rule.cursor ) )
    for rule in ruleMeta:
        logging.debug( "  EQUIVALENT RULE ALREADY EXISTS : against:\n     " + \
                       dumpers.reconstructRule( rule.rid, rule.cursor ) )
        if rule.relationName == orig_parent_rule.relationName and \
          equivalent_subgoals( orig_parent_rule.relationName, \
                               rule.subgoalListOfDicts, \
                               orig_parent_rule.subgoalListOfDicts ) :
            logging.debug("  EQUIVALENT RULE ALREADY EXISTS : returning True.")
            return True
        logging.debug("  EQUIVALENT RULE ALREADY EXISTS : not a match.")

    logging.debug("  EQUIVALENT RULE ALREADY EXISTS : returning False.")
    return False
示例#3
0
def get_final_fmla(ruleSet):

    logging.debug("  GET FINAL FMLA : running process...")
    logging.debug("  GET FINAL FMLA : ruleSet :")
    for r in ruleSet:
        logging.debug("    " + str(dumpers.reconstructRule(r.rid, r.cursor)))

    # collect the initial set of literals
    literal_id_lists = []
    for rule_index in range(0, len(ruleSet)):
        rule = ruleSet[rule_index]
        this_literal_id_list = []
        for sub_index in range(0, len(rule.subgoalListOfDicts)):
            this_literal = str(rule_index) + "_" + str(sub_index)
            if rule.subgoalListOfDicts[sub_index]["polarity"] == "notin":
                this_literal = "~" + this_literal
            this_literal_id_list.append(this_literal)
        literal_id_lists.append(this_literal_id_list)

    logging.debug("  GET FINAL FMLA : literal_id_lists = " +
                  str(literal_id_lists))

    # based on https://stackoverflow.com/a/798893
    all_clause_combos = list(itertools.product(*literal_id_lists))

    # flip literal polarities
    flipped_literal_id_lists = []
    for clause in all_clause_combos:
        flipped_lits_clause = []
        for lit in clause:
            if "~" in lit:
                lit = lit.replace("~", "")
            else:
                lit = "~" + lit
            flipped_lits_clause.append(lit)
        flipped_literal_id_lists.append(flipped_lits_clause)

    logging.debug( "  GET FINAL FMLA : flipped_literal_id_lists = " + \
                   str( flipped_literal_id_lists ) )

    # build final fmla
    final_fmla = ""
    for i in range(0, len(flipped_literal_id_lists)):
        clause = flipped_literal_id_lists[i]
        this_clause = ""
        for j in range(0, len(clause)):
            lit = clause[j]
            this_clause += lit
            if j < len(clause) - 1:
                this_clause += "&"
        this_clause = "(" + this_clause + ")"
        if i < len(flipped_literal_id_lists) - 1:
            this_clause += "|"
        final_fmla += this_clause

    return final_fmla
示例#4
0
def get_unidom_facts( factMeta, \
                      parent_rule, \
                      target_ruleSet, \
                      ruleMeta, \
                      argDict, \
                      rid_to_rule_meta_map ) :

    logging.debug("  GET UNIDOM FACTS : parent_rule = " +
                  c4_translator.get_c4_line(parent_rule.ruleData, "rule"))
    logging.debug("  GET UNIDOM FACTS : target_ruleSet :")
    for r in target_ruleSet:
        logging.debug("     " + c4_translator.get_c4_line(r.ruleData, "rule"))

    target_name = target_ruleSet[0].relationName

    # 0. get table list
    table_list = []
    for rule in ruleMeta:
        if not rule.relationName in table_list:
            table_list.append(rule.relationName)
    for fact in factMeta:
        if not fact.relationName in table_list:
            table_list.append(fact.relationName)

    # 1. get sip bindings for this parent rule
    #    and generate the domcomp for the target set.

    sip_bindings   = get_sip_bindings( parent_rule, \
                                       target_ruleSet, \
                                       table_list, \
                                       factMeta, \
                                       ruleMeta, \
                                       argDict )

    target_domcomp = get_domcomp( target_ruleSet, \
                                  table_list, \
                                  factMeta, \
                                  ruleMeta, \
                                  argDict, \
                                  rid_to_rule_meta_map )

    logging.debug("  GET UNIDOM FACTS : target_domcomp :")
    for t in target_domcomp:
        logging.debug("     " + str(t))
    #sys.exit( "blah" )

    # 2. test each domcomp fact in a pilot program.
    #    keep only the domcomp facts which make sense.

    COUNTER = 0
    unidom_tups = []
    for domcomp_tup in target_domcomp:

        # sanity check : make sure identical atts have identical bindings
        flag = False
        for rule in target_ruleSet:
            matches = {}
            for i in range(0, len(rule.ruleData["goalAttList"])):
                gatt = rule.ruleData["goalAttList"][i]
                val = domcomp_tup[i]
                if not gatt in matches:
                    matches[gatt] = val
                elif gatt in matches and \
                     val == matches[ gatt ] :
                    pass
                else:
                    logging.debug( "  binding doesn't make sense for rule '" + \
                                   dumpers.reconstructRule( rule.rid, rule.cursor ) + "'" )
                    flag = True
                    break

        if flag:
            continue  # try the next domcomp tuple

        # 2a. build a pilot program using the domcomp tup.
        # observe target_domcomp also contains all the original program results.
        logging.debug("  domcomp_tup = " + str(domcomp_tup))
        pilot_program, pilot_table_list = get_pilot_program( domcomp_tup, \
                                                             target_ruleSet, \
                                                             target_domcomp, \
                                                             factMeta, \
                                                             ruleMeta )

        # 2b. run the pilot program to see if the value makes sense
        #     => yields one of the sip bindings.
        parsedResults = run_program_c4([pilot_program, pilot_table_list],
                                       argDict)

        for tup in sip_bindings:
            if tup in parsedResults[target_ruleSet[0].relationName]:
                if not domcomp_tup in unidom_tups:
                    logging.debug("  GET UNIDOM FACTS : COUNTER = " +
                                  str(COUNTER))
                    logging.debug(
                        "  GET UNIDOM FACTS : adding to unidom facts '" +
                        str(domcomp_tup) + "'")
                    unidom_tups.append(domcomp_tup)

        #if COUNTER == 8 and target_name == "link" :
        if COUNTER == 1081:
            print "len( target_domcomp ) = " + str(len(target_domcomp))
            print "domcomp_tup = " + str(domcomp_tup)
            print "sip bindings :"
            for tup in sip_bindings:
                print tup
            print "++++++"
            print "parsed results for " + target_ruleSet[0].relationName + " :"
            for tup in parsedResults[target_ruleSet[0].relationName]:
                print tup
            print "unidom tups collection :"
            print len(unidom_tups)
            for t in unidom_tups:
                print t
            sys.exit("one of life's grander delusions.")

        COUNTER += 1

    logging.debug("  GET UNIDOM FACTS : unidom_tups :")
    for t in unidom_tups:
        logging.debug("     " + str(t))
    logging.debug("  GET UNIDOM FACTS : COUNTER == " + str(COUNTER))
    #sys.exit( "lmao" )

    # generate facts
    unidom_facts = []
    for tup in unidom_tups:
        ufact = {}
        ufact["relationName"] = "unidom_not_" + target_name
        ufact["factTimeArg"] = ""
        ufact["dataList"] = []
        for d in tup:
            if d.isdigit():
                ufact["dataList"].append(d)
            elif "'" in d or '"' in d:
                ufact["dataList"].append(d)
            else:
                ufact["dataList"].append('"' + d + '"')
        fid = tools.getIDFromCounters("fid")
        new_fact = copy.deepcopy(Fact.Fact(fid, ufact, factMeta[0].cursor))
        new_fact.cursor = factMeta[0].cursor
        unidom_facts.append(new_fact)

    return unidom_facts
示例#5
0
文件: dedt.py 项目: KDahlgren/iapyx
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 ]
示例#6
0
def resolveDoubleNegations(ruleMeta):

    for rule in ruleMeta:

        # ----------------------------------------- #
        # get subgoal info

        subgoalListOfDicts = copy.deepcopy(rule.subgoalListOfDicts)

        new_subgoalListOfDicts = []

        for subgoal in subgoalListOfDicts:

            logging.debug( "  RESOLVE DOUBLE NEGATIONS : subgoalName    = " + \
                           subgoal[ "subgoalName" ] )
            logging.debug( "  RESOLVE DOUBLE NEGATIONS : polarity       = " + \
                           subgoal[ "polarity" ] )
            logging.debug( "  RESOLVE DOUBLE NEGATIONS : subgoalAttList = " + \
                           str( subgoal[ "subgoalAttList" ] ) )
            logging.debug( "  RESOLVE DOUBLE NEGATIONS : subgoalTimeArg = " + \
                           subgoal[ "subgoalTimeArg" ] )

            # ----------------------------------------- #
            # resolve double negatives

            if subgoal["subgoalName"].startswith(
                    "not_") and subgoal["polarity"] == "notin":

                logging.debug(
                    "  RESOLVE DOUBLE NEGATIONS : hit a double neg for rule:")
                logging.debug("     " +
                              dumpers.reconstructRule(rule.rid, rule.cursor))
                logging.debug(
                    "  RESOLVE DOUBLE NEGATIONS : original subgoal = " +
                    str(subgoal))

                # based on https://stackoverflow.com/a/14496084
                double_neg_sub_name = subgoal["subgoalName"]
                double_neg_sub_name = double_neg_sub_name[
                    4:]  # remove the first not
                last_f = double_neg_sub_name.rfind("_f")
                orig_sub_name = double_neg_sub_name[:last_f]

                logging.debug(
                    "  RESOLVE DOUBLE NEGATIONS : double_neg_sub_name = " +
                    str(double_neg_sub_name))
                logging.debug(
                    "  RESOLVE DOUBLE NEGATIONS : orig_sub_name       = " +
                    str(orig_sub_name))

                new_sub_dict = {}
                new_sub_dict["subgoalName"] = orig_sub_name
                new_sub_dict["subgoalAttList"] = subgoal["subgoalAttList"]
                new_sub_dict["polarity"] = ""
                new_sub_dict["subgoalTimeArg"] = subgoal["subgoalTimeArg"]

                logging.debug( "  RESOLVE DOUBLE NEGATIONS : new_sub_dict = " + \
                               str( new_sub_dict ) )
                logging.debug( "  RESOLVE DOUBLE NEGATIONS : adding to new_subgoalListOfDicts : " + \
                               str( new_sub_dict ) + "->1")

                new_subgoalListOfDicts.append(copy.deepcopy(new_sub_dict))

            else:

                logging.debug( "  RESOLVE DOUBLE NEGATIONS : adding to new_subgoalListOfDicts : " + \
                               str( subgoal ) + " -> 3" )

                new_subgoalListOfDicts.append(copy.deepcopy(subgoal))

        # ----------------------------------------- #
        # save new subgoal list

        logging.debug( "  RESOLVE DOUBLE NEGATIONS : new_subgoalListOfDicts = " + \
                       str( new_subgoalListOfDicts ) )

        rule.subgoalListOfDicts = copy.deepcopy(new_subgoalListOfDicts)
        rule.ruleData["subgoalListOfDicts"] = copy.deepcopy(
            new_subgoalListOfDicts)
        rule.saveSubgoals()

        logging.debug("  RESOLVE DOUBLE NEGATIONS : new rule :")
        logging.debug("     " + dumpers.reconstructRule(rule.rid, rule.cursor))

    return ruleMeta
示例#7
0
def get_dom_rules( orig_name, \
                   not_name, \
                   orig_rid, \
                   parent_rid, \
                   rid_to_rule_meta_map, \
                   ruleMeta, \
                   cursor, \
                   argDict ) :

    newRules = []

    # ----------------------------------------- #
    # get parameters

    settings_path = argDict["settings"]

    # ========== POST EOT FILTER ========== #
    try:
        POST_EOT_FILTER = tools.getConfig(settings_path, "DEFAULT",
                                          "POST_EOT_FILTER", bool)
    except ConfigParser.NoOptionError:
        POST_EOT_FILTER = False
        logging.warning( "WARNING : no 'POST_EOT_FILTER' defined in 'DEFAULT' section of " + \
                         "settings.ini ...running with POST_EOT_FILTER=False." )

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

    logging.debug("=====================================================")
    logging.debug("  GET DOM RULES : orig_name   = " + orig_name)
    logging.debug("  GET DOM RULES : not_name    = " + not_name)
    logging.debug("  GET DOM RULES : orig_rid    = " + str(orig_rid))
    logging.debug("  GET DOM RULES : parent_rid  = " + str(parent_rid))
    logging.debug("  GET DOM RULES : parent rule : ")
    logging.debug("     " + dumpers.reconstructRule(parent_rid, cursor))

    # ------------------------------------------ #
    # gather subgoal atts

    negated_subgoal_atts = get_negated_subgoal_atts(orig_name, parent_rid,
                                                    ruleMeta)
    logging.debug("  GET DOM RULES : negated_subgoal_atts = " +
                  str(negated_subgoal_atts))

    # ------------------------------------------ #
    # map parent goal indexes to atts for
    # eval data extraction

    parent_goal_att_to_index_map, parent_goal_att_list = get_goal_att_to_index_map(
        parent_rid, ruleMeta)
    logging.debug("  GET DOM RULES : parent_goal_att_to_index_map = " +
                  str(parent_goal_att_to_index_map))

    # ------------------------------------------ #
    # generate sip domain idbs
    # [ { subgoalName : 'subgoalNameStr',
    #     subgoalAttList : [ data1, ... , dataN ],
    #     polarity : 'notin' OR '',
    #     subgoalTimeArg : <anInteger> }, ... ]

    # ------------------------------------------ #
    # build the universal domain rule

    uni_ruleData = {}

    # get relation name
    uni_ruleData["relationName"] = "unidom_" + not_name

    # check if a rule already exists
    # to prevent duplicates.
    if idb_already_exists(uni_ruleData["relationName"], cursor):
        return newRules

    #get goal atts
    uni_ruleData[ "goalAttList" ] = [ "A" + str(i) \
                                  for i in \
                                  range( 0, len( negated_subgoal_atts[0] ) ) ] # just need one for arity

    # map domain atts to negated subgoal atts
    # eg. [ [ X, Y ], [ Y, Q ] ]
    #  => dom_thing( A0, A1 ) <- ...
    #  => { A0: [ X, Y ], A1: [ Y, Q ] }
    # initialize maps to empty lists.
    uni_dom_atts_to_par_atts_map = { "A" + str( i ) : [] \
                                     for i in range( 0, \
                                     len( uni_ruleData[ "goalAttList" ] ) ) }
    for neg_sub_atts in negated_subgoal_atts:
        for i in range(0, len(neg_sub_atts)):
            sub_att = neg_sub_atts[i]
            uni_dom_atts_to_par_atts_map["A" + str(i)].append(sub_att)
    logging.debug("  GET DOM RULES : uni_dom_atts_to_par_atts_map = " +
                  str(uni_dom_atts_to_par_atts_map))

    logging.debug("  GET DOM RULES : ----------------------------------------")
    logging.debug("  GET DOM RULES : relationName         = " +
                  uni_ruleData["relationName"])
    logging.debug("  GET DOM RULES : goalAttList          = " +
                  str(uni_ruleData["goalAttList"]))
    logging.debug("  GET DOM RULES : negated_subgoal_atts = " +
                  str(negated_subgoal_atts))

    # get goal time arg
    uni_ruleData["goalTimeArg"] = ""

    # get eqn dict
    uni_ruleData["eqnDict"] = {}

    # =================================== #
    # get subgoal list of dicts

    # unidom rules encompass the subset of all tuples in the complenent of the
    # rule targetted for the DM rewrite which help generate data in the parent rule.
    # accordingly, copy over the contents of the parent rule and project the
    # attributes for the targeted subgoal(s).
    # constrain any remaining free goal variables with the actual contents of the
    # positive definition of the targetted rule.

    # 1. copy and edit over the list of parent subgoals

    parent_rule_meta = rid_to_rule_meta_map[parent_rid]
    uni_subgoalListOfDicts = copy.deepcopy(parent_rule_meta.subgoalListOfDicts)

    # replace subgoal references to the orig_ versions of the rule.
    for i in range(0, len(uni_subgoalListOfDicts)):
        if nw_tools.is_idb( uni_subgoalListOfDicts[ i ][ "subgoalName" ], ruleMeta ) and \
           not uni_subgoalListOfDicts[ i ][ "subgoalName" ].startswith( "not_" )     and \
           not uni_subgoalListOfDicts[ i ][ "subgoalName" ].startswith( "orig_" )     and \
           not uni_subgoalListOfDicts[ i ][ "subgoalName" ].startswith( "unidom_" )  and \
           not uni_subgoalListOfDicts[ i ][ "subgoalName" ].startswith( "exidom_" ) :
            uni_subgoalListOfDicts[ i ][ "subgoalName" ] = "orig_" + \
                                                           uni_subgoalListOfDicts[ i ][ "subgoalName" ]

    # replace atts in the parent subgoals with the goal atts
    # for the unidom rule.
    for gatt in uni_dom_atts_to_par_atts_map:
        these_par_atts = uni_dom_atts_to_par_atts_map[gatt]

        # iterate over parent subgoals
        for i in range(0, len(uni_subgoalListOfDicts)):
            sub = uni_subgoalListOfDicts[i]

            # iterate over the parent subgoal atts
            for j in range(0, len(sub["subgoalAttList"])):
                sub_att = sub["subgoalAttList"][j]

                # make the replacement if the parent sub att appears
                # in the atts corresponding to the unidom goal att
                # under consideration.
                if sub_att in these_par_atts:
                    uni_subgoalListOfDicts[i]["subgoalAttList"][j] = gatt

    logging.debug("  GET DOM RULES : subgoalListOfDicts = " +
                  str(uni_subgoalListOfDicts))

    # 2. integrate a reference to the original version of the targetted rule to fill
    #    in any missing attributes.

    all_body_atts = []
    for sub in uni_subgoalListOfDicts:
        if sub["polarity"] == "":
            for satt in sub["subgoalAttList"]:
                if not satt in all_body_atts:
                    all_body_atts.append(satt)

    missing_gatts = []
    for gatt in uni_dom_atts_to_par_atts_map:
        if not gatt in all_body_atts:
            missing_gatts.append(gatt)

#  print "uni_dom_atts_to_par_atts_map = " + str( uni_dom_atts_to_par_atts_map )
#  print "all_body_atts = " + str( all_body_atts )
#  print "missing_gatts = " + str( missing_gatts )
#
#  if uni_ruleData[ "relationName" ] == "unidom_not_node_f23" :
#    sys.exit( "blah" )

    if len(missing_gatts) > 0:
        orig_sub = {}
        orig_sub["subgoalName"] = orig_name
        orig_sub["subgoalTimeArg"] = ""
        orig_sub["polarity"] = ""
        orig_sub["subgoalAttList"] = []
        for i in range(0, len(uni_dom_atts_to_par_atts_map)):
            if "A" + str(i) in missing_gatts:
                orig_sub["subgoalAttList"].append("A" + str(i))
            else:
                orig_sub["subgoalAttList"].append("_")
        uni_subgoalListOfDicts.append(orig_sub)

    uni_ruleData["subgoalListOfDicts"] = uni_subgoalListOfDicts

    # =================================== #
    # save rule

    # replace time arg with constant if the negated subgoal stems from post
    if POST_EOT_FILTER and parent_name == "post":
        uni_ruleData["goalAttList"][-1] = argDict["EOT"]

    uni_rid = tools.getIDFromCounters("rid")
    uni_rule = copy.deepcopy(Rule.Rule(uni_rid, uni_ruleData, cursor))
    uni_rule.cursor = cursor  # need to do this for some reason or else cursor disappears?

    # set the unidom rule types manually
    uni_goal_types = []
    for rule in ruleMeta:
        if rule.rid == orig_rid:
            uni_goal_types = rule.goal_att_type_list
    assert (len(uni_goal_types) > 0)

    uni_rule.goal_att_type_list = uni_goal_types
    uni_rule.manually_set_types()

    # check if a rule already exists
    # to prevent duplicates.
    if not nw_tools.identical_rule_already_exists(uni_rule, ruleMeta):
        newRules.append(uni_rule)
        logging.debug( "  GET DOM RULES : added uni dom rule :\n     " + \
                       dumpers.reconstructRule( uni_rule.rid, uni_rule.cursor ) )
    else:
        logging.debug( "  GET DOM RULES : NOT adding uni dom rule :\n     " + \
                       dumpers.reconstructRule( uni_rule.rid, uni_rule.cursor ) )

#  if uni_rule.relationName == "unidom_not_node_f40" :
#    print orig_name
#    print not_name
#    print dumpers.reconstructRule( parent_rid, uni_rule.cursor )
#    print dumpers.reconstructRule( uni_rule.rid, uni_rule.cursor )
#    sys.exit( "blah" )

# ------------------------------------------ #
# build the existential domain rule

# exidom_ encompasses the set of data from the original version
# of the target rule which contributes to generating data in
# the original version of the target relation.
# accordingly, one exidom_ rule exists per rule in the definition.

# get the list of rules defining the target relation
    target_rules = []
    for rule in ruleMeta:
        if rule.relationName == "orig_" + orig_name:
            target_rules.append(rule)

    for target_rule in target_rules:

        exi_ruleData = {}

        # get relation name
        # need the extra _f to maintain arities.
        exi_ruleData["relationName"] = "exidom_" + not_name + "_f" + str(
            target_rule.rid)

        # grab all existential vars from the original definition for the
        # target relation.
        all_exi_vars = []
        for sub in target_rule.subgoalListOfDicts:
            for satt in sub["subgoalAttList"]:
                if not satt in target_rule.goalAttList and \
                   not satt in all_exi_vars            and \
                   not satt == "_" :
                    all_exi_vars.append(satt)

        # only write an exidom_ rule if existential vars exist.
        if len(all_exi_vars) > 0:

            #get goal atts
            exi_ruleData["goalAttList"] = copy.deepcopy(all_exi_vars)

            # get goal time arg
            exi_ruleData["goalTimeArg"] = ""

            # get eqn dict
            exi_ruleData["eqnDict"] = {}

            # =================================== #
            # get subgoals

            exi_subgoalListOfDicts = copy.deepcopy(
                target_rule.subgoalListOfDicts)
            for i in range(0, len(exi_subgoalListOfDicts)):
                sub = exi_subgoalListOfDicts[i]
                if not sub[ "subgoalName" ].startswith( "orig_" )   and \
                   not sub[ "subgoalName" ] == "clock"              and \
                   not sub[ "subgoalName" ] == "next_clock"         and \
                   not sub[ "subgoalName" ] == "crash"              and \
                   not sub[ "subgoalName" ].startswith( "not_" )    and \
                   not sub[ "subgoalName" ].startswith( "unidom_" ) and \
                   not sub[ "subgoalName" ].startswith( "exidom_" ) and \
                   nw_tools.is_idb( sub[ "subgoalName" ], ruleMeta ) :
                    exi_subgoalListOfDicts[i][
                        "subgoalName"] = "orig_" + sub["subgoalName"]

            exi_ruleData["subgoalListOfDicts"] = exi_subgoalListOfDicts

            # =================================== #
            # save rule

            exi_rid = tools.getIDFromCounters("rid")
            exi_rule = copy.deepcopy(Rule.Rule(exi_rid, exi_ruleData, cursor))
            exi_rule.cursor = cursor  # need to do this for some reason or else cursor disappears?

            # set the unidom rule types manually
            exi_goal_types = []
            for gatt in exi_rule.goalAttList:
                for sub in exi_subgoalListOfDicts:
                    if gatt in sub["subgoalAttList"]:
                        gatt_index = sub["subgoalAttList"].index(gatt)
                        for rule in ruleMeta:
                            if rule.relationName == sub["subgoalName"]:
                                exi_goal_types.append(
                                    rule.goal_att_type_list[gatt_index])
            assert (len(uni_goal_types) > 0)

            exi_rule.goal_att_type_list = exi_goal_types
            exi_rule.manually_set_types()

            # check if a rule already exists
            # to prevent duplicates.
            if not nw_tools.identical_rule_already_exists(exi_rule, cursor):
                newRules.append(exi_rule)
                logging.debug( "  GET DOM RULES : added exi dom rule :\n     " + \
                               dumpers.reconstructRule( exi_rule.rid, exi_rule.cursor ) )
            else:
                logging.debug( "  GET DOM RULES : NOT adding exi dom rule :\n     " + \
                               dumpers.reconstructRule( exi_rule.rid, exi_rule.cursor ) )

    logging.debug("  GET DOM RULES : domain rules:")
    for rule in newRules:
        logging.debug("     " + dumpers.reconstructRule(rule.rid, rule.cursor))

    #if uni_ruleData[ "relationName" ] == "unidom_not_node_f23" :
    #  for rule in newRules :
    #    print dumpers.reconstructRule( rule.rid, rule.cursor )
    #  sys.exit( "blah" )

    return newRules
示例#8
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
示例#9
0
def dm_time_att_replacement(ruleMeta, cursor, argDict):

    logging.debug("  DM TIME ATT REPLACEMENT : running process...")

    replacement_time_att = "MRESERVED"

    target_rule_sets = getRuleMetaSets(ruleMeta, cursor)

    for ruleSet in target_rule_sets:
        logging.debug("..........")
        for rule in ruleSet:
            logging.debug(rule.relationName + ", " + str(rule.goalAttList) +
                          ", " + str(rule.hitUniformityRewrites))
    #sys.exit( "blah" )

    # replace the time att
    for ruleSet in target_rule_sets:
        for rule in ruleSet:

            if rule.hitUniformityRewrites == True:

                target_att = rule.goalAttList[-1]

                # save new goal attribute list
                rule.ruleData["goalAttList"] = rule.goalAttList[:-1] + [
                    replacement_time_att
                ]
                rule.goalAttList = rule.goalAttList[:-1] + [
                    replacement_time_att
                ]
                rule.saveToGoalAtt()

                new_subgoalListOfDicts = []
                for sub in rule.ruleData["subgoalListOfDicts"]:

                    subgoalAttList = sub["subgoalAttList"]
                    new_subgoalAttList = []

                    for satt in subgoalAttList:
                        if satt == target_att:
                            new_subgoalAttList.append(replacement_time_att)
                        else:
                            new_subgoalAttList.append(satt)

                    new_sub_dict = {}
                    new_sub_dict["subgoalName"] = sub["subgoalName"]
                    new_sub_dict["subgoalAttList"] = new_subgoalAttList
                    new_sub_dict["polarity"] = sub["polarity"]
                    new_sub_dict["subgoalTimeArg"] = sub["subgoalTimeArg"]
                    new_subgoalListOfDicts.append(new_sub_dict)

                # save new subgoals
                rule.ruleData["subgoalListOfDicts"] = new_subgoalListOfDicts
                rule.subgoalListOfDicts = new_subgoalListOfDicts
                rule.saveSubgoals()

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

    logging.debug("  DM TIME ATT REPLACEMENT : ...done.")

    return ruleMeta
示例#10
0
def do_combo_sip( factMeta, \
                  ruleMeta, \
                  targetRuleMetaSets, \
                  not_templates, \
                  rid_to_rule_meta_map, \
                  cursor, \
                  argDict ) :

    logging.debug("  DO COMBO SIP : running process...")

    #for r in targetRuleMetaSets :
    #  print r
    #for r in ruleMeta :
    #  print c4_translator.get_c4_line( r.ruleData, "rule" )
    #sys.exit( "asdf" )

    COUNTER = 0
    for rule_info in targetRuleMetaSets:

        logging.debug("  DO COMBO SIP : >COUNTER  = " + str(COUNTER))

        parent_list = rule_info[0]
        ruleSet = rule_info[1]

        # 0. pull the template not_ rules.
        target_name = ruleSet[0].relationName
        #target_not_template = copy.deepcopy( not_templates[ target_name ] )
        if not previously_pulled(target_name, ruleMeta):
            target_not_template = copy.deepcopy(not_templates[target_name])
        else:
            target_not_template = []

        logging.debug("//////////////////////////////////////////////////")
        logging.debug("  DO COMBO SIP : parent_list : " + str(parent_list))
        logging.debug("  DO COMBO SIP : ruleSet:")
        for rule in ruleSet:
            logging.debug("    " +
                          dumpers.reconstructRule(rule.rid, rule.cursor))
        logging.debug("  DO COMBO SIP : target_name : " + target_name)
        logging.debug("  DO COMBO SIP : target_not_template : ")
        for t in target_not_template:
            logging.debug("     " + c4_translator.get_c4_line(t, "rule"))
        logging.debug("//////////////////////////////////////////////////")

        # Augment the unidom rules for this relation using the parent information.
        for curr_parent in parent_list:

            parent_name = curr_parent[0]
            parent_rid = curr_parent[1]
            parent_rule = rid_to_rule_meta_map[parent_rid]

            # don't build unidom references for recursive rules.
            if parent_name == "not_" + ruleSet[0].relationName:
                pass
            elif parent_name == "not_post":
                pass
            else:

                # 2. build and save unidom facts derived using bindings from parent rules.
                unidom_facts = sip.get_unidom_facts( factMeta, \
                                                     parent_rule, \
                                                     ruleSet, \
                                                     ruleMeta, \
                                                     argDict, \
                                                     rid_to_rule_meta_map )
                factMeta.extend(unidom_facts)

                #if parent_list == [['post',2]] :
                #  print COUNTER
                #  sys.exit( "here" )

                # 3. add the parent-specific unidom subgoal to the template not_ rule.
                #if COUNTER == 0 : # why this???
                if True:

                    for ruleData in target_not_template:
                        uni_sub = {}
                        uni_sub["subgoalName"] = unidom_facts[0].relationName
                        uni_sub["subgoalAttList"] = ruleData["goalAttList"]
                        uni_sub["polarity"] = ""
                        uni_sub["subgoalTimeArg"] = ""
                        ruleData["subgoalListOfDicts"].append(uni_sub)

                        # add rule to ruleMeta
                        rid = tools.getIDFromCounters("rid")
                        newRule = copy.deepcopy(
                            Rule.Rule(rid, ruleData, cursor))
                        newRule.cursor = cursor

                        newRule.goal_att_type_list = copy.deepcopy(
                            ruleSet[0].goal_att_type_list)
                        newRule.manually_set_types()

                        logging.debug( "  DO COMBO SIP : adding rule '" + \
                                       c4_translator.get_c4_line( newRule.ruleData, "rule" ) + "'" )
                        ruleMeta.append(newRule)

            # 4. make not_ subgoal replacements in parent rules.
            flag = False
            for i in range(0, len(parent_rule.subgoalListOfDicts)):
                sub = parent_rule.subgoalListOfDicts[i]
                if sub[ "subgoalName" ] == ruleSet[0].relationName and \
                   sub[ "polarity" ] == "notin" :
                    parent_rule.subgoalListOfDicts[i][
                        "subgoalName"] = "not_" + ruleSet[0].relationName
                    parent_rule.subgoalListOfDicts[i]["polarity"] = ""
                    flag = True
            if flag:
                parent_rule.saveSubgoals()

            #if parent_list == [['post', 2]] :
            #  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( "lmao" )

            COUNTER += 1

    # ----------------------------------------- #
    # order recursive rules last

    ruleMeta = nw_tools.sortDMRules(ruleMeta)

    #  if ruleSet[0].relationName == "link" :
    #    for r in ruleMeta :
    #      print dumpers.reconstructRule( r.rid, r.cursor )
    #      #print c4_translator.get_c4_line( r.ruleData, "rule" )
    #    sys.exit( "blah" )

    logging.debug("  DO COMBO SIP : ...done.")
    return ruleMeta
示例#11
0
def rewriteDeductive(cursor):

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                addArg = None

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

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

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

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

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

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

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

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

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

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

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

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

        print "SubgoalAtt"
        cursor.execute("SELECT * FROM SubgoalAtt")
        for s in cursor.fetchall():
            print s
示例#12
0
def rewriteAsynchronous(cursor):

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    return None
示例#13
0
def rewriteInductive(cursor):

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    return None
示例#14
0
def doDeMorgans_sip_idb( factMeta, \
                         ruleMeta, \
                         targetRuleMetaSets, \
                         rid_to_rule_meta_map, \
                         cursor, \
                         argDict ) :

    logging.debug("  DO DEMORGANS SIP IDB : running process...")

    newDMRules = []

    COUNTER = 0
    for rule_info in targetRuleMetaSets:

        parent_list = rule_info[0]
        ruleSet = rule_info[1]

        for rule in ruleSet:
            logging.debug("//////////////////////////////////////////////////")
            logging.debug("  DO DEMORGANS SIP IDB : parent_list: " +
                          str(parent_list))
            logging.debug("  DO DEMORGANS SIP ODB : ruleSet:")
            logging.debug("    " +
                          dumpers.reconstructRule(rule.rid, rule.cursor))
            logging.debug("//////////////////////////////////////////////////")

        # ----------------------------------------- #
        # get an original rule id to reference types

        orig_rid = ruleSet[0].rid

        # ----------------------------------------- #
        # get new rule name

        orig_name = ruleSet[0].relationName

        # ----------------------------------------- #
        # get new rule goal attribute list
        # works only if goal att lists are uniform
        # across all rules per set

        goalAttList = ruleSet[0].ruleData["goalAttList"]

        # ----------------------------------------- #
        # get new rule goal time arg

        goalTimeArg = ""

        # ----------------------------------------- #
        # get current results

        #parsedResults = get_current_results( argDict, cursor )
        #logging.debug( "  DO DEMORGANS SIP IDB : done running eval." )

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

        final_fmla = nw_tools.get_final_fmla(ruleSet)

        # ----------------------------------------- #
        # get the lists of all strings and ints

        #all_strings = get_all_strings( factMeta )
        #all_ints    = get_all_ints( factMeta, cursor )

        # ----------------------------------------- #
        # need a new set of dm rules for every instance of a particular
        # negated subgoal

        for curr_parent in parent_list:

            parent_name = curr_parent[0]
            parent_rid = curr_parent[1]

            logging.debug("  DO DEMORGANS SIP IDB : parent rule:")
            logging.debug("     " +
                          dumpers.reconstructRule(parent_rid, cursor))

            not_name = "not_" + orig_name + "_f" + str(parent_rid)

            # ----------------------------------------- #
            # build the domain subgoal info

            # returns a subgoal dict
            #uni_dom_sub = build_uni( not_name, goalAttList )

            # returns a subgoal dict
            #exi_dom_sub = build_exi( orig_name, not_name, goalAttList, ruleMeta )

            # ----------------------------------------- #
            # get the domain rules

            dom_rules = sip_idb.get_dom_rules( orig_name, \
                                               not_name, \
                                               orig_rid, \
                                               parent_rid, \
                                               rid_to_rule_meta_map, \
                                               ruleMeta, \
                                               cursor, \
                                               argDict )
            ruleMeta.extend(dom_rules)

            # ----------------------------------------- #
            # each clause in the final dnf fmla
            # informs the subgoal list of a new
            # datalog rule

            newDMRules = dnfToDatalog( orig_name, \
                                       not_name, \
                                       goalAttList, \
                                       goalTimeArg, \
                                       final_fmla, \
                                       ruleSet, \
                                       ruleMeta, \
                                       parent_name, \
                                       parent_rid, \
                                       rid_to_rule_meta_map, \
                                       cursor, \
                                       argDict )

            # ----------------------------------------- #
            # add new dm rules to the rule meta

            ruleMeta.extend(newDMRules)

            # ----------------------------------------- #
            # replace instances of the negated subgoal
            # with instances of the positive not_
            # subgoal

            ruleMeta = sip_idb.replaceSubgoalNegations( orig_name, \
                                                        not_name, \
                                                        parent_rid, \
                                                        ruleMeta )

        # ----------------------------------------- #
        # resolve double negations

        ruleMeta = sip_idb.resolveDoubleNegations(ruleMeta)

        # ----------------------------------------- #
        # order recursive rules last

        ruleMeta = nw_tools.sortDMRules(ruleMeta)

        COUNTER += 1

    return ruleMeta
示例#15
0
def dnfToDatalog( orig_name, \
                  not_name, \
                  goalAttList, \
                  goalTimeArg, \
                  final_fmla, \
                  ruleSet, \
                  ruleMeta, \
                  parent_name, \
                  parent_rid, \
                  rid_to_rule_meta_map, \
                  cursor, \
                  argDict ) :

    settings_path = argDict["settings"]

    logging.debug("  DNF TO DATALOG : running process...")
    logging.debug("  DNF TO DATALOG : not_name    = " + not_name)
    logging.debug("  DNF TO DATALOG : goalAttList = " + str(goalAttList))
    logging.debug("  DNF TO DATALOG : goalTimeArg = " + goalTimeArg)
    logging.debug("  DNF TO DATALOG : final_fmla  = " + final_fmla)

    # get goal types
    goal_types = ruleSet[
        0].goal_att_type_list  # just pick any rule in the set.

    logging.debug("  DNF TO DATALOG : ruleSet :")
    for r in ruleSet:
        logging.debug("    " + str(dumpers.reconstructRule(r.rid, r.cursor)))

    # ----------------------------------------- #
    # generate combined equation list
    # collect eqns from all rules and append to
    # all dm rules.

    eqnDict_combined = {}
    for rule in ruleSet:
        for eqn in rule.eqnDict:
            eqnDict_combined[eqn] = rule.eqnDict[eqn]

    # ----------------------------------------- #
    # break positive dnf fmla into a set of
    # conjuncted clauses

    clauseList = final_fmla.replace("(", "")  # valid b/c dnf
    clauseList = clauseList.replace(")", "")  # valid b/c dnf
    clauseList = clauseList.split("|")

    logging.debug("  DNF TO DATALOG : clauseList = " + str(clauseList))

    # ----------------------------------------- #
    # iterate over clause list to create
    # the list of new dm rules
    # create one new dm rule per clause

    newDMRules = []

    for clause in clauseList:

        subgoalListOfDicts = []

        # ----------------------------------------- #
        # get list of subgoal literals

        subgoalLiterals = clause.split("&")
        logging.debug("  DNF TO DATALOG : subgoalLiterals = " +
                      str(subgoalLiterals))

        # ----------------------------------------- #
        # iterate over the subgoal literals
        # observe the first integer represents the
        # index of the parent target rule in the
        # rule meta list of _targetted_ rule objects
        # the second integer represents the index
        # of the associated subgoal in the current
        # targetted rule

        for literal in subgoalLiterals:

            logging.debug("  DNF TO DATALOG : literal = " + literal)

            # ----------------------------------------- #
            # get subgoal polarity

            if "~" in literal:
                polarity = "notin"
                literal = literal.replace("~", "")

            else:
                polarity = ""

            # ----------------------------------------- #
            # grab the rule and subgoal indexes

            literal = literal.split("_")
            ruleIndex = int(literal[0])
            subgoalIndex = int(literal[1])

            # ----------------------------------------- #
            # grab subgoal dict from appropriate rule

            rule = ruleSet[ruleIndex]
            subgoalDict = {}
            sub = rule.subgoalListOfDicts[subgoalIndex]
            for key in sub:
                subgoalDict[key] = sub[key]

            # ----------------------------------------- #
            # if not_ rule contains a negated recusrion
            # instance, replace with the positive head.

            if subgoalDict["subgoalName"] == orig_name:
                subgoalDict["subgoalName"] = not_name
                subgoalDict["polarity"] = ""

            else:

                # ----------------------------------------- #
                # set polarity

                subgoalDict["polarity"] = polarity

            # ----------------------------------------- #
            # save to subgoal list of dicts

            logging.debug( "  DNF TO DATALOG : adding subgoalDict to rule '" + \
                           not_name + "' : " + str( subgoalDict ) )

            subgoalListOfDicts.append(subgoalDict)

        # ----------------------------------------- #
        # add domain subgoals, if applicable

        unidom_rules = []
        exidom_rules = []
        for r in ruleMeta:
            if r.relationName.endswith( not_name ) and \
               r.relationName.startswith( "unidom_" ) :
                unidom_rules.append(r)
            elif sip_idb.is_an_exidom(not_name, r, orig_name, ruleMeta):
                exidom_rules.append(r)

        try:
            assert (len(unidom_rules) == 1)
        except AssertionError:
            raise AssertionError(
                "no unidom_ rule. all DM rules have a unidom_. aborting...")

        unidom_sub = {}
        unidom_sub["subgoalName"] = unidom_rules[0].relationName
        unidom_sub["subgoalTimeArg"] = ""
        unidom_sub["polarity"] = ""
        unidom_sub["subgoalAttList"] = goalAttList
        subgoalListOfDicts.append(unidom_sub)

        if len(exidom_rules) > 0:
            for esub in exidom_rules:
                exidom_sub = {}
                exidom_sub["subgoalName"] = esub.relationName
                exidom_sub["subgoalTimeArg"] = ""
                exidom_sub["polarity"] = ""
                exidom_sub["subgoalAttList"] = esub.goalAttList
                subgoalListOfDicts.append(exidom_sub)

        # ----------------------------------------- #
        # add existential domain subgoal,
        # if applicable

#    prevRules = []
#    for currRule in existentialVarsRules :
#
#      if currRule.relationName in prevRules :
#        pass
#
#      else :
#        prevRules.append( currRule.relationName )
#
#        existentialVarSubgoal_dict = {}
#        existentialVarSubgoal_dict[ "subgoalName" ]    = currRule.ruleData[ "relationName" ]
#        existentialVarSubgoal_dict[ "subgoalAttList" ] = currRule.ruleData[ "goalAttList" ]
#        existentialVarSubgoal_dict[ "polarity" ]       = ""
#        existentialVarSubgoal_dict[ "subgoalTimeArg" ] = ""
#
#        subgoalListOfDicts.append( existentialVarSubgoal_dict )

# ----------------------------------------- #
# build ruleData for new rule and save

        ruleData = {}
        ruleData["relationName"] = not_name
        ruleData["goalAttList"] = goalAttList
        ruleData["goalTimeArg"] = goalTimeArg
        ruleData["subgoalListOfDicts"] = subgoalListOfDicts
        ruleData["eqnDict"] = eqnDict_combined

        # ----------------------------------------- #
        # add negation of original version
        # for good measure.

        orig_neg = {}
        orig_neg["subgoalName"] = "orig_" + orig_name
        orig_neg["subgoalTimeArg"] = ""
        orig_neg["polarity"] = "notin"
        orig_neg["subgoalAttList"] = ruleData["goalAttList"]
        ruleData["subgoalListOfDicts"].append(orig_neg)

        # ----------------------------------------- #
        # save rule

        rid = tools.getIDFromCounters("rid")
        newRule = copy.deepcopy(Rule.Rule(rid, ruleData, cursor))
        newRule.cursor = cursor  # need to do this for some reason or else cursor disappears?
        newRule.goal_att_type_list = goal_types

        # maintain a list of not_ rules previously derived in the
        # lineage of this rule.
        parent_rule = rid_to_rule_meta_map[parent_rid]
        newRule.lineage_not_names = parent_rule.lineage_not_names
        if parent_name.startswith("not_"):
            newRule.lineage_not_names.append(parent_name)

        newRule.manually_set_types()
        newDMRules.append(newRule)

#    if len( newRule.lineage_not_names ) > 0 :
#      print newRule.lineage_not_names
#      print dumpers.reconstructRule( parent_rid, cursor )
#      print dumpers.reconstructRule( newRule.rid, newRule.cursor )
#      sys.exit( "blah" )

    logging.debug("  DNF TO DATALOG : newDMRules :")
    for newRule in newDMRules:
        logging.debug(
            "    " + str(dumpers.reconstructRule(newRule.rid, newRule.cursor)))

    return newDMRules
示例#16
0
def get_sip_bindings(parent_rule, target_ruleSet, table_list, factMeta,
                     ruleMeta, argDict):

    logging.debug("  GET SIP BINDINGS : parent_rule :")
    logging.debug("     " +
                  c4_translator.get_c4_line(parent_rule.ruleData, "rule"))
    logging.debug("  GET SIP BINDINGS : target_ruleSet :")
    for t in target_ruleSet:
        logging.debug("     " + c4_translator.get_c4_line(t.ruleData, "rule"))
    logging.debug("  GET SIP BINDINGS : ruleMeta :")
    for r in ruleMeta:
        logging.debug("     " + c4_translator.get_c4_line(r.ruleData, "rule"))

    target_name = target_ruleSet[0].relationName

    # generate sip rule
    orig_rel_name = target_ruleSet[0].relationName
    sip_ruleData = {}
    sip_ruleData["relationName"] = "sip_not_" + orig_rel_name
    sip_ruleData["goalTimeArg"] = ""
    sip_ruleData["subgoalListOfDicts"] = copy.deepcopy(
        parent_rule.subgoalListOfDicts)
    sip_ruleData["eqnDict"] = copy.deepcopy(parent_rule.eqnDict)

    lists_of_att_bindings = []
    for sub in parent_rule.subgoalListOfDicts:
        #print "sub = " + str( sub )
        if sub[ "subgoalName" ] == orig_rel_name and \
           sub[ "polarity" ] == "notin" :
            lists_of_att_bindings.append(sub["subgoalAttList"])

    # cannot support the appearance of the same negated subgoal
    # more than once in the same rule.
    try:
        assert (len(lists_of_att_bindings) == 1)
    except AssertionError:
        if len(lists_of_att_bindings) > 1:
            raise AssertionError( "iapyx does not support rules negating the same subgoal twice. " + \
                                  "see rule :\n     " + \
                                  dumpers.reconstructRule( parent_rule.rid, parent_rule.cursor ) )
        else:
            raise AssertionError( "no rule bindings for relation '" + \
                                  orig_rel_name + \
                                  "' in rule :\n     " + \
                                  dumpers.reconstructRule( parent_rule.rid, parent_rule.cursor ) )

    # replace wildcards with some actual value.
    # since the value doesn't matter, just pick one.
    if "_" in lists_of_att_bindings[0]:
        program = get_program(table_list, factMeta, ruleMeta)
        parsedResults = run_program_c4(program, argDict)

        logging.debug("before:")
        logging.debug(lists_of_att_bindings[0])

        for i in range(0, len(lists_of_att_bindings[0])):
            satt = lists_of_att_bindings[0][i]
            if satt == "_":
                pick_something = parsedResults[orig_rel_name][0][i]
                if pick_something.isdigit():
                    lists_of_att_bindings[0][i] = pick_something
                elif "'" in pick_something or '"' in pick_something:
                    lists_of_att_bindings[0][i] = pick_something
                else:
                    lists_of_att_bindings[0][i] = '"' + pick_something + '"'

        logging.debug("after:")
        logging.debug(lists_of_att_bindings[0])
        #sys.exit( "fuckingshit" )

    sip_ruleData["goalAttList"] = lists_of_att_bindings[0]

    #sip_type_list = [ t[1] for t in parent_rule.goal_att_type_list ]
    sip_type_list = [t[1] for t in target_ruleSet[0].goal_att_type_list]

    if target_name == "link" and \
       lists_of_att_bindings[0] == [ "S", "Z3", "Y", "_", "NRESERVED" ] :
        print c4_translator.get_c4_line(parent_rule.ruleData, "rule")
        for l in target_ruleSet:
            print c4_translator.get_c4_line(l.ruleData, "rule")
        print sip_ruleData
        print sip_type_list
        print c4_translator.get_c4_line(sip_ruleData, "rule")
        sys.exit("asdf")

    # build and run program
    program = get_program(table_list, factMeta, ruleMeta, sip_ruleData,
                          sip_type_list)
    parsedResults = run_program_c4(program, argDict)

    return parsedResults[sip_ruleData["relationName"]]
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
示例#18
0
def combo(factMeta, ruleMeta, cursor, argDict):

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

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

    settings_path = argDict["settings"]

    # ----------------------------------------- #
    # get parameters

    # ========== NW DOM DEF ========== #
    try:
        NW_DOM_DEF = tools.getConfig(settings_path, "DEFAULT", "NW_DOM_DEF",
                                     str)
        if NW_DOM_DEF == "sip":
            pass
        else:
            raise ValueError( "unrecognized NW_DOM_DEF option '" + NW_DOM_DEF + \
                              "' for combo NW rewrites. aborting..." )
    except ConfigParser.NoOptionError:
        raise ValueError( "no 'NW_DOM_DEF' defined in 'DEFAULT' section of " + settings_path + \
                          ". aborting..." )

    # ----------------------------------------- #
    # replace unused variables with wildcards

    if NW_DOM_DEF == "sip":
        ruleMeta = nw_tools.replace_unused_vars(ruleMeta, cursor)

    # ----------------------------------------- #
    # rewrite rules with fixed data
    # in the head

    ruleMeta, factMeta = nw_tools.fixed_data_head_rewrites(
        ruleMeta, factMeta, argDict)

    # ----------------------------------------- #
    # rewrite rules with aggregate functions
    # in the head

    ruleMeta = nw_tools.aggRewrites(ruleMeta, argDict)

    # ----------------------------------------- #
    # enforce a uniform goal attribute lists

    ruleMeta = nw_tools.setUniformAttList(ruleMeta, cursor)

    logging.debug("  COMBO : len( ruleMeta ) after setUniformAttList = " +
                  str(len(ruleMeta)))

    # ----------------------------------------- #
    # enforce unique existential attributes
    # per rule

    ruleMeta = nw_tools.setUniqueExistentialVars(ruleMeta)

    # ----------------------------------------- #
    # replace time att references

    ruleMeta = dm_time_att_replacement.dm_time_att_replacement(
        ruleMeta, cursor, argDict)

    # ----------------------------------------- #
    # append rids to all rel names and
    # generate cps of the original rules
    # (do not reference these in final programs)

    if NW_DOM_DEF == "sip":

        # future optimization : do this lazily:
        ruleMeta.extend(nw_tools.generate_orig_cps(ruleMeta))

    # ----------------------------------------- #
    # append rids to all rel names and
    # generate cps of the original rules
    # (do not reference these in final programs)

    if NW_DOM_DEF == "sip":

        # future optimization : do this lazily:
        not_templates, ruleMeta = get_not_templates_combo(factMeta, ruleMeta)

        #for r in ruleMeta :
        #  print str( r.rid ) + " : " + dumpers.reconstructRule( r.rid, r.cursor )
        #sys.exit( "blee" )

    # ----------------------------------------- #
    # generate a map of all rids to corresponding
    # rule meta object pointers.

    if NW_DOM_DEF == "sip":
        rid_to_rule_meta_map = nw_tools.generate_rid_to_rule_meta_map(ruleMeta)

    # ----------------------------------------- #
    # build all de morgan's rules

    COUNTER = 0
    while nw_tools.stillContainsNegatedIDBs(ruleMeta, cursor):

        logging.debug("  COMBO : COUNTER = " + str(COUNTER))
        if COUNTER == 3:
            print "////////////"
            for r in ruleMeta:
                print dumpers.reconstructRule(r.rid, r.cursor)
            sys.exit("wtf?")

        # ----------------------------------------- #
        # check if any rules include negated idb
        # subgoals

        targetRuleMetaSets = nw_tools.getRuleMetaSetsForRulesCorrespondingToNegatedSubgoals( ruleMeta, \
                                                                                             cursor )

        #if COUNTER == 2 :
        #  print targetRuleMetaSets[0][0]
        #  for r in targetRuleMetaSets[0][1] :
        #    print c4_translator.get_c4_line( r.ruleData, "rule" )
        #  print "////////////"
        #  for r in ruleMeta :
        #    print dumpers.reconstructRule( r.rid, r.cursor )
        #  sys.exit( "asdf" )

        # ----------------------------------------- #
        # break execution if no rules contain negated IDBs.
        # should not hit this b/c of loop condition.

        if len(targetRuleMetaSets) < 1:
            return []

        # ----------------------------------------- #
        # create the de morgan rewrite rules.
        # incorporates domcomp and existential
        # domain subgoals.

        if NW_DOM_DEF == "sip":
            ruleMeta = do_combo_sip( factMeta, \
                                     ruleMeta, \
                                     targetRuleMetaSets, \
                                     not_templates, \
                                     rid_to_rule_meta_map, \
                                     cursor, \
                                     argDict )
        else:
            raise ValueError( "unrecognized NW_DOM_DEF option '" + NW_DOM_DEF + \
                              "'. aborting..." )

        #for r in ruleMeta :
        #  print str( r.rid ) + " : " + dumpers.reconstructRule( r.rid, r.cursor )
        #sys.exit( "blast" )

        # ----------------------------------------- #
        # update rid to rule meta map

        rid_to_rule_meta_map = nw_tools.generate_rid_to_rule_meta_map(ruleMeta)

        #if COUNTER == 2 :
        #  for r in ruleMeta :
        #    print str( r.rid ) + " : " + dumpers.reconstructRule( r.rid, r.cursor )
        #  sys.exit( "blahasdf" )

        # increment loop counter
        COUNTER += 1

    # ----------------------------------------- #
    # replace unused variables with wildcards

    if NW_DOM_DEF == "sip":
        ruleMeta = nw_tools.replace_unused_vars(ruleMeta, cursor)

    # ----------------------------------------- #
    # filter out unused not_ rules

    if NW_DOM_DEF == "sip":
        ruleMeta = nw_tools.delete_unused_not_rules(ruleMeta, cursor)

    for r in ruleMeta:
        print str(r.rid) + " : " + dumpers.reconstructRule(r.rid, r.cursor)
    sys.exit("blahasdf")

    logging.debug("  COMBO : ...done.")
    return factMeta, ruleMeta
示例#19
0
文件: domain.py 项目: KDahlgren/iapyx
def insertDomainFactWithoutPar(cursor, rule, ruleMeta, factMeta,
                               parsedResults):

    logging.debug("  INSERT DOMAIN FACT WITHOUT PAR : rule = " + str(rule))

    # print "not found parent dom"
    parRules, childRule, childVars = collectDomainRuleInfo(
        cursor, rule, ruleMeta)
    childRule = childRule[0]
    newRules = []
    newFacts = []

    logging.debug("------------")
    logging.debug("  INSERT DOMAIN FACT WITHOUT PAR : parRules :")
    for i in parRules:
        logging.debug("     " + dumpers.reconstructRule(i.rid, i.cursor))

    logging.debug("  INSERT DOMAIN FACT WITHOUT PAR : childRule :")
    logging.debug("     " +
                  dumpers.reconstructRule(childRule.rid, childRule.cursor))
    logging.debug("------------")

    for parRule in parRules:
        for subgoal in parRule.subgoalListOfDicts:

            if subgoal['subgoalName'] == childRule.relationName:

                # we found the matching subgoal
                # iterate over the attributes in the subgoal and get the
                # range of values for the attributes, given the eval results.
                for attIndex in range(0, len(childRule.goalAttList)):
                    att = subgoal['subgoalAttList'][attIndex]
                    logging.debug("  INSERT DOMAIN FACT WITHOUT PAR : att = " +
                                  att)

                    if isConstant(att):
                        # the attribute is constant in the parent goal therefore add a fact for it
                        newFact = createDomFact(
                            cursor, "dom_not_" + rule[0] + "_" + str(attIndex),
                            [att])
                        newFacts.append(newFact)
                        continue

                    found = False
                    for parAttIndex in range(0, len(parRule.goalAttList)):
                        parAtt = parRule.goalAttList[parAttIndex]
                        if parAtt == att:
                            found = True

                            logging.debug(
                                "  INSERT DOMAIN FACT WITHOUT PAR : ...is a goal att. "
                            )
                            logging.debug( "  INSERT DOMAIN FACT WITHOUT PAR : parent results for '" + \
                                           rule[1] + "'" )
                            for j in parsedResults[rule[1]]:
                                logging.debug("       " + str(j))

                            # found this attribute in the parent head.
                            # therefore can define based off this value in the parents slot
                            if len(parsedResults[rule[1]]) == 0:

                                # nothing exists in the parents domain.
                                # Going to base off of whole active domain.
                                if rule[0] in parsedResults.keys():

                                    # in this case we can define the domain based off
                                    # its previously fired not domain
                                    newFacts = newFacts + createFactsBasedOffParsedResults( cursor, \
                                                                                            childVars, \
                                                                                            childRule, \
                                                                                            attIndex, \
                                                                                            parsedResults )
                                    continue

                                dom_name = 'dom_str'
                                if childVars[childRule.
                                             goalAttList[attIndex]] == "int":
                                    dom_name = 'dom_int'

                                logging.debug( "  INSERT DOMAIN FACT WITHOUT PAR : dom_name = " + \
                                               dom_name )

                                ruleData = createDomRule(
                                    rule[0], attIndex, childRule.goalTimeArg,
                                    att)
                                goalDict = createSubgoalDict(
                                    dom_name, [att], '', childRule.goalTimeArg)

                                ruleData['subgoalListOfDicts'].append(goalDict)
                                domrid = tools.getIDFromCounters("rid")
                                newRule = Rule.Rule(domrid, ruleData, cursor)
                                newRules.append(newRule)

                            else:
                                dom_name = "dom_not_" + rule[0] + "_" + str(
                                    attIndex)
                                logging.debug( "  INSERT DOMAIN FACT WITHOUT PAR : dom_name = " + \
                                               dom_name )

                                usedVals = {}
                                logging.debug( "  INSERT DOMAIN FACT WITHOUT PAR : usedVals = " + \
                                               str( usedVals ) )
                                for val in parsedResults[rule[1]]:
                                    # add in a rule into the parsed results
                                    data = val[parAttIndex]
                                    if data in usedVals.keys():
                                        continue
                                    usedVals[data] = True
                                    newFact = createDomFact( cursor, \
                                                             dom_name, \
                                                             [data] )
                                    newFacts.append(newFact)
                                break

                    if not found:
                        logging.debug(
                            "  INSERT DOMAIN FACT WITHOUT PAR : ...is NOT a goal att."
                        )
                        if rule[0] in parsedResults.keys():
                            newFacts = newFacts + createFactsBasedOffParsedResults( cursor, \
                                                                                    childVars, \
                                                                                    childRule, \
                                                                                    attIndex, \
                                                                                    parsedResults )
                            continue

                        # we have not found it therefore must go off the active domain.
                        att = childRule.goalAttList[attIndex]
                        dom_name = 'dom_int'
                        val = att
                        if childVars[att] == 'string':
                            dom_name = 'dom_str'

                        ruleData = createDomRule(rule[0], attIndex,
                                                 childRule.goalTimeArg, val)

                        # add in the adom
                        dom_name = "dom_str"
                        if childVars[att].lower() == 'int':
                            dom_name = "dom_int"
                        goalDict = createSubgoalDict(dom_name, [val], '',
                                                     childRule.goalTimeArg)
                        ruleData['subgoalListOfDicts'].append(goalDict)

                        domrid = tools.getIDFromCounters("rid")
                        newRule = Rule.Rule(domrid, ruleData, cursor)
                        newRules.append(newRule)

    ruleMeta = ruleMeta + newRules
    factMeta = factMeta + newFacts
    return ruleMeta, factMeta
示例#20
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