Example #1
0
def concate_neg_rules(cursor, neg_rules):

  ''' Loops through all the negated rules and concatinates them together, performs the and '''

  logging.debug( "  CONCATE NEG RULES : neg_rules = " + str( neg_rules ) )

  # This is legacy code, changing the fomualtion of this to support a form of multi level aggregation
  if len( neg_rules ) <= 1:
    logging.debug( "  CONCATE NEG RULES : returning None," + str( neg_rules[0] ) )
    return None, neg_rules[0]

  rules = []

  # create the head rule that we will add e ach rule too.
  headRuleData = createRuleData( neg_rules[0][0].relationName, '', neg_rules[0][0].goalAttList )

  # match each rule with every other rule
  for i in range( 0, len( neg_rules ) ) :

    # first rename each rule to be 'not_{relationName}_{i}'
    for j in range(0, len(neg_rules[i])):
      new_neg_rule                          = copy.deepcopy( neg_rules[ i ][ j ] )
      new_neg_rule.relationName             = new_neg_rule.relationName + "_" + str(i)
      new_neg_rule.ruleData['relationName'] = new_neg_rule.relationName
      rules.append(new_neg_rule)

    headRuleData = createSubgoal( headRuleData, headRuleData['goalAttList'], new_neg_rule.relationName )

  head_rid = tools.getIDFromCounters( "rid" )
  newRule  = Rule.Rule( head_rid, headRuleData, cursor )

  logging.debug( "  CONCATE NEG RULES : newRule.ruleData = " + str( newRule.ruleData ) )
  return newRule, rules
Example #2
0
def createDomFact(cursor, name, data):
    # print "createDomFact", name
    for i in range(0, len(data)):
        if not is_int(data[i]):
            if not data[i].startswith('"'):
                data[i] = '"' + data[i] + '"'
    factData = {}
    factData['relationName'] = name
    factData['dataList'] = data
    factData["factTimeArg"] = ''
    fid = tools.getIDFromCounters("fid")
    return Fact.Fact(fid, factData, cursor)
Example #3
0
def addNegFact(cursor, ruleMeta, factMeta, old_name):

  '''
    If there are any facts associated with the rule, 
    sets up a negated rule that covers all things within the 
    domain that are notin the rule and are at the same timestamp. 
    This covers base facts for recursive rules.
  '''

  for fact in factMeta:

    if fact.relationName == old_name:
      atts = []

      for attIndex in range(0, len(fact.dataListWithTypes)-1):
        atts.append("Att"+str(attIndex))

      atts.append(fact.dataListWithTypes[len(fact.dataListWithTypes)-1][0])
      rule = createRuleData("not_"+old_name,"", atts)

      for attIndex in range (0,len(atts)):
        rule = createSubgoal( rule, \
                             [ "Att" + str( attIndex ) ], \
                             "dom_not_" + old_name + "_" + str( attIndex ) )

      rule    = createSubgoal( rule, \
                               atts, \
                               old_name, \
                               notin = "notin" )
      rid     = tools.getIDFromCounters( "rid" )
      newRule = Rule.Rule( rid, rule, cursor )

      ruleMeta.append(newRule)

      return ruleMeta, factMeta

  return ruleMeta, factMeta
Example #4
0
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
Example #5
0
def negateRule( cursor, \
                rule, \
                ruleMeta, \
                factMeta,  \
                parsedResults, \
                neg_clocks = True) :

  ''' 
    Takes in a list of rules that all correspond to the same rule,
    performs a form of demorgans in which each negated version of the rule
    is performed and then they are anded together
  '''

  logging.debug( "  COMBO REWRITE: Negating " + rule[0] + " from "+ rule[1] )

  # Find the parent rule
  parRule = None
  for r in ruleMeta:
    if r.relationName == rule[0]:
      parRule = r
      break

  if parRule == None :
    logging.warning( "  COMBO REWRITE: parent rule " + rule[0] + " not found." )
    return ruleMeta, factMeta

  # insert domain information
  ruleMeta, factMeta = domain.insertDomainFact( cursor, \
                                                rule, \
                                                ruleMeta, \
                                                factMeta, \
                                                parsedResults )

  # Find the rule to be negated
  rule = rule[0]
  ruleData = []
  for r in ruleMeta:
    if r.relationName == rule:
      ruleData.append(r)
  
  if len( ruleData ) == 0:
    logging.warning( "  COMBO REWRITE: rule " + rule + " not found." )
    return ruleMeta, factMeta

  old_rid = ruleData[0].rid
  old_name = ruleData[0].relationName
  new_name = 'not_' + ruleData[0].relationName

  negated_pieces = []
  for piece in ruleData:
    neg_piece = copy.deepcopy( piece )

    # remove clock from things to be negated if neg_clocks is false.
    clk = None
    if not neg_clocks:
      for subgoal in neg_piece.subgoalListOfDicts:
        if subgoal['subgoalName'] == 'clock':
          clk = subgoal
          neg_piece.subgoalListOfDicts.remove(clk)

    neg_piece.relationName = new_name
    neg_piece.ruleData['relationName'] = new_name
    numGoals = len(neg_piece.subgoalListOfDicts)
    numRules = pow(2, numGoals)
    rules = []
    for i in range (1, numRules):
      for j in range (0, numRules):
        power = pow(2, j)
        if i%power == 0:
          neg_piece = flip_negation( cursor, neg_piece, j, parsedResults )
      piece = copy.deepcopy( neg_piece )

      # add back in clocks
      if clk:
        piece.subgoalListOfDicts.append( clk )

      rules.append( copy.deepcopy( piece ) )
    negated_pieces.append( rules )

  #headRule, negated_pieces = concate_neg_rules( cursor, negated_pieces )
  headRule       = None
  negated_pieces = negated_pieces[0]

  if headRule != None:
    headRule = goalData.saveRule( headRule )
    ruleMeta.append( headRule )

  for negRule in negated_pieces:

    # append the domain information
    negRule = domain.concateDomain( cursor, negRule, old_rid, new_name )
    logging.debug( "  COMBO REWRITE : negRule.ruleData = " + str( negRule.ruleData ) )

    #insert new rule
    neg_rid        = tools.getIDFromCounters( "rid" )
    negRule.cursor = cursor
    negRule.rid    = neg_rid
    negRule        = goalData.saveRule( negRule )
    ruleMeta.append( negRule )

  # if there are facts associated with it, 
  # adds in a rule to cover things at the time stamp that dont exist.
  ruleMeta, factMeta = addNegFact( cursor, ruleMeta, factMeta, old_name )

  # flips the notin version of the rules to not_
  ruleMeta = replaceAllNotins( cursor, old_name, new_name, ruleMeta )

  logging.debug("  COMBO REWRITE: Negate of " + old_name + " complete...")
  return ruleMeta, factMeta
Example #6
0
def add_exidom_subs(not_template_ruleData, factMeta, ruleMeta, cursor):

    exidom_ruleData_list = []
    exi_var_to_rel_index_map_list = []
    for key in not_template_ruleData:

        orig_ruleMeta_list = get_orig_ruleMeta_list(key, ruleMeta)
        if len(orig_ruleMeta_list) < 1:
            logging.debug("skipping dom rules for " + key)
            continue

        # build exidom_ rules
        lists_of_exi_vars = []
        for orule in orig_ruleMeta_list:
            exi_vars = []
            for sub in orule.subgoalListOfDicts:
                for var in sub["subgoalAttList"]:
                    if not var in orule.goalAttList and \
                       not var in exi_vars         and \
                       not var == "_"              and \
                       not "'" in var              and \
                       not '"' in var              and \
                       not var.isdigit() :
                        exi_vars.append(var)

            if len(exi_vars) > 0:

                lists_of_exi_vars.append([exi_vars, orule.rid])

                # catch all possible values for existential atts
                patternMap = nw_tools.getPatternMap(
                    len(orule.subgoalListOfDicts))
                for row in patternMap:

                    #print "len( exidom_ruleData_list ) = " + str( len( exidom_ruleData_list ) )
                    #print "row = " + str( row )

                    # one exidom rule per pattern
                    exidom_ruleData = {}
                    exidom_ruleData[ "relationName" ]       = "exidom_" + \
                                                              "not_" + key + \
                                                              "_f" + str( orule.rid )
                    exidom_ruleData["goalTimeArg"] = ""
                    exidom_ruleData["goalAttList"] = exi_vars

                    # fails if exi_var parameterized with equations.
                    exidom_ruleData["eqnDict"] = {}

                    exidom_ruleData["subgoalListOfDicts"] = []

                    # maintain a map of exi_vars to appearances in subgoals
                    exi_var_to_rel_index_map = {}
                    for var in exi_vars:
                        exi_var_to_rel_index_map[var] = []

                    for i in range(0, len(row)):
                        bit = row[i]
                        newSubgoal = orule.subgoalListOfDicts[i]

                        # edit the polarity
                        if bit == "0":
                            newSubgoal["polarity"] = ""
                        else:
                            newSubgoal["polarity"] = "notin"

                        # remove references to global vars
                        for i in range(0, len(newSubgoal["subgoalAttList"])):
                            var = newSubgoal["subgoalAttList"][i]
                            if not var in exi_vars:
                                newSubgoal["subgoalAttList"][i] = "_"
                            else:
                                newSubgoal["subgoalAttList"][i] = var
                                exi_var_to_rel_index_map[var].append(
                                    [newSubgoal["subgoalName"], i])
                        #logging.debug( "adding newSubgoal = " + str( newSubgoal ) )
                        exidom_ruleData["subgoalListOfDicts"].append(
                            newSubgoal)

                    exi_var_to_rel_index_map_list.append(
                        copy.deepcopy(exi_var_to_rel_index_map))
                    logging.debug( "  ADD EXIDOM SUBS : adding rule data for " + \
                                   c4_translator.get_c4_line( exidom_ruleData, "rule" ) )
                    exidom_ruleData_list.append(copy.deepcopy(exidom_ruleData))

                    #print "checking contents exidom_ruleData_list:"
                    #for e in exidom_ruleData_list :
                    #  print c4_translator.get_c4_line( e, "rule" )

        logging.debug("  ADD EXIDOM SUBS : exidom_ruleData_list:")
        for r in exidom_ruleData_list:
            logging.debug("     " + c4_translator.get_c4_line(r, "rule"))
        #sys.exit( "shit" )

        # define subgoals
        exidom_subgoals = []
        for ev_list_info in lists_of_exi_vars:
            ev_list = ev_list_info[0]
            rid = ev_list_info[1]
            exidom = {}
            exidom["subgoalName"] = "exidom_" + "not_" + key + "_f" + str(rid)
            exidom["subgoalTimeArg"] = ""
            exidom["polarity"] = ""
            exidom["subgoalAttList"] = ev_list
            exidom_subgoals.append(exidom)

        # add exidom_ subgoals, if applicable
        if len(exidom_subgoals) > 0:
            for ntr_ruleData in not_template_ruleData[key]:
                for esub in exidom_subgoals:
                    if is_applicable(esub, ntr_ruleData):
                        ntr_ruleData["subgoalListOfDicts"].append(esub)
                        logging.debug("  ADD DOM SUBS : added sub '" +
                                      str(sub) + "'")

    # define exidom rules, if applicable
    exidom_rules = []
    for i in range(0, len(exidom_ruleData_list)):

        exidom_ruleData = exidom_ruleData_list[i]
        this_exi_var_to_rel_index_map = exi_var_to_rel_index_map_list[i]

        #print c4_translator.get_c4_line( exidom_ruleData, "rule" )

        if nw_tools.isSafe( exidom_ruleData ) and \
           not nw_tools.identical_rule_already_exists( exidom_ruleData, ruleMeta ) :

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

            # set the exidom rule types manually
            exi_goal_types = []
            for i in range(0, len(exidom_ruleData["goalAttList"])):
                gatt = exidom_ruleData["goalAttList"][i]

                # assume the types make sense
                representative_rel_index = this_exi_var_to_rel_index_map[gatt][
                    0]

                # check rules for type data
                this_type = None
                for rule in ruleMeta:
                    if rule.relationName == representative_rel_index[0]:
                        index = representative_rel_index[1]
                        this_type = [i, rule.goal_att_type_list[index][1]]
                        break

                if not this_type:
                    for fact in factMeta:
                        if fact.relationName == representative_rel_index[0]:
                            index = representative_rel_index[1]
                            this_type = [i, fact.dataListWithTypes[index][1]]

                assert (this_type != None)
                exi_goal_types.append(this_type)

            assert (len(exi_goal_types) > 0)

            exi_rule.goal_att_type_list = exi_goal_types
            exi_rule.manually_set_types()
            exidom_rules.append(exi_rule)

    ruleMeta.extend(exidom_rules)

    #for r in exidom_rules :
    #  print "rid = " + str( r.rid ) + " : " + c4_translator.get_c4_line( r.ruleData, "rule" )
    #sys.exit( "asdf" )

    return not_template_ruleData, ruleMeta
Example #7
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
Example #8
0
def regProv(regRule, nameAppend, cursor, argDict):

    try:
        USING_MOLLY = tools.getConfig(argDict["settings"], "DEFAULT",
                                      "USING_MOLLY", bool)
    except ConfigParser.NoOptionError:
        logging.warning(
            "WARNING : no 'USING_MOLLY' defined in 'DEFAULT' section of settings.ini ...assume running without molly."
        )
        USING_MOLLY = False

    logging.debug("  REG PROV : running regProv...")
    logging.debug("  REG PROV : regRule              = " + str(regRule))
    logging.debug("  REG PROV : regRule.relationName = " +
                  regRule.relationName)

    # ------------------------------------------------------ #
    # generate a random ID for the new provenance rule

    rid = tools.getIDFromCounters("rid")

    # ------------------------------------------------------ #
    # initialize the prov rule to old version of
    # meta rule

    new_provmeta_ruleData = copy.deepcopy(regRule.ruleData)

    # ------------------------------------------------------ #
    # the provenance rule name ends with "_prov" appended
    # with a unique number

    new_provmeta_ruleData[
        "relationName"] = new_provmeta_ruleData["relationName"] + nameAppend

    # ------------------------------------------------------ #
    # the goal att list consists of all subgoal atts

    provGoalAttList = []

    # grab all goal atts
    goalAttList = new_provmeta_ruleData["goalAttList"]

    # save to provenance rule goal attribute list
    provGoalAttList.extend(goalAttList)

    # extract and save the time argument as the last element in the attribute list
    #  provGoalAttList_last = provGoalAttList[-1]
    #  provGoalAttList      = provGoalAttList[:-1]

    # ------------------------------------------------------------------ #
    # grab all subgoal atts

    subgoalListOfDicts = new_provmeta_ruleData["subgoalListOfDicts"]
    for subgoal in subgoalListOfDicts:
        subgoalAttList = subgoal["subgoalAttList"]
        for att in subgoalAttList:

            logging.debug("  REG PROV : att in subgoalAttList = " + att)

            # don't duplicate atts in the prov head
            if not att in provGoalAttList:

                logging.debug("  REG PROV : att not in " +
                              str(provGoalAttList))

                # do not add wildcards and fixed integer inputs
                if not att == "_" and not att.isdigit():

                    logging.debug("  REG PROV : att not '_' and not isdigit")

                    # do not add fixed string inputs
                    # do not add unused variables (huh? why? messes up not_rule arities)
                    #if not isFixedString( att ) and not isUnused( subgoalListOfDicts, new_provmeta_ruleData[ "eqnDict" ], att ) :
                    if not isFixedString(att):
                        provGoalAttList.append(att)

    # ------------------------------------------------------------------ #
    # add the time argument last

#  if not provGoalAttList_last in provGoalAttList :
#    provGoalAttList.append( provGoalAttList_last )

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

    logging.debug("  REG PROV : new_provmeta_ruleData['relationName'] = " +
                  new_provmeta_ruleData["relationName"])
    logging.debug("  REG PROV : provGoalAttList                       = " +
                  str(provGoalAttList))

    # sort goal atts to ensure NRESERVED, NRESERVED+1, and MRESERVED are rightmost
    if USING_MOLLY:
        provGoalAttList = sortGoalAttList(provGoalAttList)

    logging.debug("  REG PROV : provGoalAttList (after sorting)       = " +
                  str(provGoalAttList))

    # save to rule data
    new_provmeta_ruleData["goalAttList"] = provGoalAttList

    # ------------------------------------------------------ #
    # preserve adjustments by instantiating the new meta rule
    # as a Rule

    provRule = Rule.Rule(rid, new_provmeta_ruleData, cursor)
    provRule.orig_rule_ptr = regRule
    provRule.rule_type = regRule.rule_type

    logging.debug("  REG PROV : regRule                  = " + str(regRule))
    logging.debug("  REG PROV : regRule.orig_goalAttList = " +
                  str(regRule.orig_goalAttList))
    logging.debug("  REG PROV : provRule.orig_rule_ptr   = " +
                  str(provRule.orig_rule_ptr))
    logging.debug("  REG PROV : provRule.orig_rule_ptr.orig_goalAttList = " +
                  str(provRule.orig_rule_ptr.orig_goalAttList))
    logging.debug("  REG PROV : returning prov rule id " + str(rid) +
                  " provRule.ruleData = " + str(provRule.ruleData))
    logging.debug("  REG PROV : provRule.relationName       = " +
                  provRule.relationName)
    logging.debug("  REG PROV : provRule.goalAttList        = " +
                  str(provRule.goalAttList))
    logging.debug("  REG PROV : provRule.goalTimeArg        = " +
                  provRule.goalTimeArg)
    logging.debug("  REG PROV : provRule.subgoalListOfDicts = " +
                  str(provRule.subgoalListOfDicts))
    logging.debug("  REG PROV : provRule.eqnDict            = " +
                  str(provRule.eqnDict))
    logging.debug("  REG PROV : provRule.orig_rule_ptr      = " +
                  str(provRule.orig_rule_ptr))
    logging.debug("  REG PROV : provRule.orig_rule_ptr.goalAttList = " +
                  str(provRule.orig_rule_ptr.goalAttList))

    #if provRule.relationName == "not_missing_log_prov8" :
    #  sys.exit( "blah" )

    # ------------------------------------------------------ #
    # replace original time goal atts

    #  if tools.getConfig( argDict[ "settings" ], "DEFAULT", "DM", bool ) :
    #    provRule = replaceTimeAtts( provRule )
    #
    #  logging.debug( "  REG PROV : returning prov rule id " + str( rid ) + " provRule.ruleData = " + str( provRule.ruleData ) )
    #  logging.debug( "  REG PROV : provRule.relationName       = " + provRule.relationName )
    #  logging.debug( "  REG PROV : provRule.goalAttList        = " + str( provRule.goalAttList ) )
    #  logging.debug( "  REG PROV : provRule.goalTimeArg        = " + provRule.goalTimeArg )
    #  logging.debug( "  REG PROV : provRule.subgoalListOfDicts = " + str( provRule.subgoalListOfDicts ) )
    #  logging.debug( "  REG PROV : provRule.eqnDict            = " + str( provRule.eqnDict ) )

    return provRule
Example #9
0
def get_dom_facts( orig_name, \
                   not_name, \
                   orig_rid, \
                   parent_name, \
                   parent_rid, \
                   parsedResults, \
                   ruleMeta, \
                   all_strings, \
                   all_ints, \
                   cursor, \
                   argDict ) :

    logging.debug("  GET DOM FACTS : not_name = " + not_name)

    newFacts = []

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

    negated_subgoal_atts = get_negated_subgoal_atts(orig_name, parent_rid,
                                                    ruleMeta)

    logging.debug("  GET DOM FACTS : negated_subgoal_atts = " +
                  str(negated_subgoal_atts))

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

    parent_goal_att_to_index_map = get_goal_att_to_index_map(
        parent_rid, ruleMeta)

    logging.debug("  GET DOM FACTS : parent_goal_att_to_index_map = " +
                  str(parent_goal_att_to_index_map))

    # ------------------------------------------ #
    # generate sip edbs based upon parent eval
    # results.

    logging.debug( "  GET DOM FACTS : parsedResults['" + parent_name + \
                   "'] = " + str( parsedResults[ parent_name ] ) )

    try:
        assert (len(parsedResults[parent_name]) > 0)
    except AssertionError:
        raise AssertionError( "parsedResults['" + parent_name + \
                     "'] = " + str( parsedResults[ parent_name ] ) )

    for tup in parsedResults[parent_name]:
        data_list = []

        for att_list in negated_subgoal_atts:
            a_data_list = []
            contains_list = False

            for index in range(0, len(att_list)):
                att = att_list[index]

                if att in parent_goal_att_to_index_map:
                    this_data = tup[parent_goal_att_to_index_map[att]]

                    # need to make this a list for iterttools
                    if this_data.isdigit():
                        a_data_list.append(
                            [tup[parent_goal_att_to_index_map[att]]])
                    else:
                        a_data_list.append([
                            '"' + tup[parent_goal_att_to_index_map[att]] + '"'
                        ])

                else:
                    att_type = get_goal_att_type(index, orig_rid, ruleMeta)
                    if att_type == "string":
                        a_data_list.append(all_strings)
                        contains_list = True

                    elif att_type == "int":
                        a_data_list.append(all_ints)
                        contains_list = True

                    else:
                        raise ValueError(
                            "  GET DOM FACTS : unrecognized att_type '" +
                            att_type + "'")

            data_lists = get_all_data_lists(a_data_list)

        # ------------------------------------------ #
        # create a new edb fact for every data tuple

        for data_tup in data_lists:

            # ------------------------------------------ #
            # generate random ID for fact

            fid = tools.getIDFromCounters("fid")

            # ------------------------------------------ #
            # also add in the type info, since
            # it's easily accessible.
            # observe this only works b/c currently
            # supporting only ints and strings.

            dataListWithTypes = []
            for data in data_tup:
                if data.isdigit():
                    dataListWithTypes.append([data, "int"])
                else:
                    dataListWithTypes.append([data, "string"])

            # ------------------------------------------ #
            # define factData

            factData = {}
            factData["relationName"] = "dom_" + not_name
            factData["dataList"] = data_tup
            factData["factTimeArg"] = ""

            # ------------------------------------------ #
            # save fact data in persistent DB using IR

            newFact = Fact.Fact(fid, factData, cursor)
            newFact.dataListWithTypes = dataListWithTypes

            logging.debug( "  GET DOM FACTS : adding fact:\n     " + \
                           dumpers.reconstructFact( newFact.fid, newFact.cursor ) )

            newFacts.append(newFact)

    #if not_name == "not_node_from_not_log_from_missing_log" :
    #if not_name == "not_log_from_missing_log" :
    #  sys.exit( "blah" )

    return newFacts
Example #10
0
def add_dom_subs(not_template_ruleMeta, ruleMeta, cursor):

    dom_subs = []

    for key in not_template_ruleMeta:

        orig_ruleMeta_list = get_orig_ruleMeta_list(key, ruleMeta)
        if len(orig_ruleMeta_list) < 1:
            print "skipping dom rules for " + key
            continue

        for a_not_rule in not_template_ruleMeta[key]:

            # only add subgoals to a rule once.
            if "unidom_" + a_not_rule.relationName in \
               [ sub[ "subgoalName" ] for sub in a_not_rule.subgoalListOfDicts ] :
                continue

            # build unidom_
            unidom = {}
            unidom["subgoalName"] = "unidom_" + a_not_rule.relationName
            unidom[
                "subgoalAttList"] = a_not_rule.goalAttList  #this only works b/c uniformit rewrites
            unidom["subgoalTimeArg"] = ""
            unidom["polarity"] = ""

            # add unidom_
            for ntr in not_template_ruleMeta[key]:
                ntr.ruleData["subgoalListOfDicts"].append(unidom)
                logging.debug("  ADD DOM SUBS : adding sub '" + str(unidom) +
                              "'")
                ntr.saveSubgoals()

            # build exidom_
            # and exidom_ rules
            lists_of_exi_vars = []
            exidom_rules = []
            for rule in orig_ruleMeta_list:
                exi_vars = []
                for sub in rule.subgoalListOfDicts:
                    for var in sub["subgoalAttList"]:
                        if not var in rule.goalAttList and \
                           not var in exi_vars         and \
                           not var == "_"              and \
                           not "'" in var              and \
                           not '"' in var              and \
                           not var.isdigit() :
                            exi_vars.append(var)

                if len(exi_vars) > 0:
                    lists_of_exi_vars.append([exi_vars, rule.rid])
                    exidom_ruleData = {}
                    exidom_ruleData[ "relationName" ]       = "exidom_" + \
                                                              a_not_rule.relationName + \
                                                              "_f" + str( rule.rid )
                    exidom_ruleData["goalTimeArg"] = ""
                    exidom_ruleData["goalAttList"] = exi_vars
                    exidom_ruleData["eqnDict"] = {}
                    exidom_ruleData["subgoalListOfDicts"] = []

                    for sub in rule.subgoalListOfDicts:
                        new_sub = copy.deepcopy(sub)
                        for i in range(0, len(sub["subgoalAttList"])):
                            var = sub["subgoalAttList"][i]
                            if not var in exi_vars:
                                new_sub["subgoalAttList"][i] = "_"
                            else:
                                new_sub["subgoalAttList"][i] = var
                        exidom_ruleData["subgoalListOfDicts"].append(new_sub)

                    # do save
                    exi_rid = tools.getIDFromCounters("rid")
                    exi_rule = copy.deepcopy(
                        Rule.Rule(exi_rid, exidom_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 = rule.goal_att_type_list
                    assert (len(exi_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, ruleMeta):
                        ruleMeta.append(exi_rule)

            exidom_subgoals = []
            for ev_list_info in lists_of_exi_vars:
                ev_list = ev_list_info[0]
                rid = ev_list_info[1]
                exidom = {}
                exidom[
                    "subgoalName"] = "exidom_" + a_not_rule.relationName + "_f" + str(
                        rid)
                exidom["subgoalTimeArg"] = ""
                exidom["polarity"] = ""
                exidom["subgoalAttList"] = ev_list
                exidom_subgoals.append(exidom)

            # add exidoms_, if applicable
            if len(exidom_subgoals) > 0:
                for ntr in not_template_ruleMeta[key]:
                    flag = False
                    for esub in exidom_subgoals:
                        if is_applicable(esub, ntr):
                            ntr.ruleData["subgoalListOfDicts"].append(esub)
                            logging.debug("  ADD DOM SUBS : adding sub '" +
                                          str(sub) + "'")
                            flag = True
                    if flag:
                        ntr.saveSubgoals()

        #if key == "log" :
        #  for i in orig_ruleMeta_list :
        #    print dumpers.reconstructRule( i.rid, i.cursor )
        #  for i in not_template_ruleMeta[ key ] :
        #    print dumpers.reconstructRule( i.rid, i.cursor )
        #  sys.exit( "blah" )

    return not_template_ruleMeta, exidom_rules
Example #11
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
Example #12
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
Example #13
0
def rewrite_wildcards(ruleMeta, cursor):

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

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

    for rule in ruleMeta:

        logging.debug("  REWRITE WILDCARDS : rule.relationName = " +
                      rule.relationName)

        if "_nowilds" in rule.relationName and not rule.relationName.startswith(
                "not_"):
            logging.debug("  REWRITE WILDCARDS : skipping wildcard rewrites.")

        else:

            # ------------------------------------------- #
            # examine all subgoals per rule

            itID = 0
            nowilds_rules = []
            subgoalListOfDicts = []
            for subgoal in rule.ruleData["subgoalListOfDicts"]:

                logging.debug("  REWRITE WILDCARDS : subgoal = " +
                              str(subgoal))

                # ------------------------------------------- #
                # examine all negated subgoals with wildcards

                if subgoal["polarity"] == "notin" and "_" in subgoal[
                        "subgoalAttList"]:
                    logging.debug(
                        "  REWRITE WILDCARDS : hit a notin with wilds")

                    # ------------------------------------------- #
                    # generate new subgoal name

                    new_subgoalName = subgoal["subgoalName"] + "_nowilds" + str(
                        rule.rid) + "_" + str(itID)
                    itID += 1

                    logging.debug("  REWRITE WILDCARDS : new_subgoalName = " +
                                  new_subgoalName)

                    # ------------------------------------------- #
                    # generate new subgoal att list

                    new_subgoalAttList = []
                    for att in subgoal["subgoalAttList"]:
                        if not att == "_":
                            new_subgoalAttList.append(att)

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

                    new_subgoal = {}
                    new_subgoal["subgoalName"] = new_subgoalName
                    new_subgoal["subgoalAttList"] = new_subgoalAttList
                    new_subgoal["polarity"] = subgoal["polarity"]
                    new_subgoal["subgoalTimeArg"] = subgoal["subgoalTimeArg"]

                    subgoalListOfDicts.append(new_subgoal)

                    # ------------------------------------------- #
                    # build new nowilds rule

                    nowilds_ruleData = {}
                    nowilds_ruleData["relationName"] = new_subgoalName
                    nowilds_ruleData["goalAttList"] = new_subgoalAttList
                    nowilds_ruleData["goalTimeArg"] = ""
                    nowilds_ruleData["eqnDict"] = {}

                    nowilds_subgoal = copy.deepcopy(subgoal)
                    nowilds_subgoal["polarity"] = ""
                    nowilds_ruleData["subgoalListOfDicts"] = [nowilds_subgoal]

                    rid = tools.getIDFromCounters("rid")
                    ruleMeta.append(Rule.Rule(rid, nowilds_ruleData, cursor))
                    logging.debug(
                        "  REWRITE WILDCARDS : added rule with ruleData = " +
                        str(nowilds_ruleData))

                else:
                    logging.debug(
                        "  REWRITE WILDCARDS : hit positive subgoal.")
                    subgoalListOfDicts.append(subgoal)

                # ------------------------------------------- #
                # save original rule with any new subgoal data

                rule.ruleData["subgoalListOfDicts"] = subgoalListOfDicts
                rule.subgoalListOfDicts = subgoalListOfDicts
                rule.saveSubgoals()

    logging.debug("  REWRITE WILDCARDS : ...done.")
    return ruleMeta
Example #14
0
def iedb_rewrites(factMeta, ruleMeta, cursor, settings_path):

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

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

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

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

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

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

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

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

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

    for rel_name in rel_name_to_fact_obj_map:

        fact_obj_list = rel_name_to_fact_obj_map[rel_name]

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

        for fact_obj in fact_obj_list:

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

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

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

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

    for rel_name in rel_name_to_fact_obj_map:

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

        if len(rel_name_to_fact_obj_map[rel_name]) > 0:

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

            # build the goal attribute list
            new_goalAttList = []

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

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

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

            new_ruleData["goalAttList"] = new_goalAttList

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

            new_ruleData["subgoalListOfDicts"] = [new_subgoal_dict]

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

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

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

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

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

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

    logging.debug("  IEDB REWRITES : ...done.")
    return factMeta, ruleMeta
Example #15
0
def aggProv(aggRule, provid, cursor, argDict):

    logging.debug("  AGG PROV : running aggProv...")

    try:
        USING_MOLLY = tools.getConfig(argDict["settings"], "DEFAULT",
                                      "USING_MOLLY", bool)
    except ConfigParser.NoOptionError:
        logging.warning(
            "WARNING : no 'USING_MOLLY' defined in 'DEFAULT' section of settings.ini ...assume running without molly."
        )
        USING_MOLLY = False

    orig_aggRule_goalAttList = aggRule.ruleData["goalAttList"]

    logging.debug("  AGG PROV : orig_aggRule_goalAttList = " +
                  str(orig_aggRule_goalAttList))

    # ------------------------------------------------------ #
    #                 BUILD THE BINDINGS RULE                #
    # ------------------------------------------------------ #

    # ------------------------------------------------------ #
    # generate a random ID for the new provenance rule

    bindings_rid = tools.getIDFromCounters("rid")

    # ------------------------------------------------------ #
    # initialize the prov rule to old version of
    # meta rule

    bindingsmeta_ruleData = {}
    for key in aggRule.ruleData:
        val = aggRule.ruleData[key]
        bindingsmeta_ruleData[key] = val

    logging.debug("  AGG PROV : bindingsmeta_ruleData = " +
                  str(bindingsmeta_ruleData))

    # ------------------------------------------------------ #
    # the provenance rule name ends with "_prov" appended
    # with a unique number

    # NOTE!!!! LDFI paper says "_bindings", but molly implementation actually uses "_vars" append. >~<

    #bindingsmeta_ruleData[ "relationName" ] = bindingsmeta_ruleData[ "relationName" ] + "_bindings" + str( provid )
    bindingsmeta_ruleData[
        "relationName"] = bindingsmeta_ruleData["relationName"] + "_vars"

    # ------------------------------------------------------ #
    # the goal att list consists of all subgoal atts

    bindings_goalAttList = []

    # grab all goal atts
    old_bindings_goalAttList = bindingsmeta_ruleData["goalAttList"]
    bindings_goalAttList = getAllGoalAtts_noAggs(old_bindings_goalAttList)

    # extract and save the time argument as the last element in the attribute list
    bindings_goalAttList_last = bindings_goalAttList[-1]
    bindings_goalAttList = bindings_goalAttList[:-1]

    # grab all subgoal atts
    subgoalListOfDicts = bindingsmeta_ruleData["subgoalListOfDicts"]

    logging.debug("  AGG PROV : subgoalListOfDicts = " +
                  str(subgoalListOfDicts))

    for subgoal in subgoalListOfDicts:
        subgoalAttList = subgoal["subgoalAttList"]
        for att in subgoalAttList:

            # don't duplicate atts in the prov head
            if not att in bindings_goalAttList:

                # do not add wildcards and fixed integer inputs
                if not att == "_" and not att.isdigit():

                    # do not add fixed string inputs
                    if not isFixedString(att):
                        bindings_goalAttList.append(att)

    # add the time argument last
    if not bindings_goalAttList_last in bindings_goalAttList:
        bindings_goalAttList.append(bindings_goalAttList_last)

    # save to rule data
    if USING_MOLLY:
        bindings_goalAttList = sortGoalAttList(bindings_goalAttList)
    bindingsmeta_ruleData["goalAttList"] = bindings_goalAttList

    # ------------------------------------------------------ #
    # preserve adjustments by instantiating the new meta rule
    # as a Rule

    bindings_rule = Rule.Rule(bindings_rid, bindingsmeta_ruleData, cursor)
    bindings_rule.rule_type = aggRule.rule_type

    # ------------------------------------------------------ #
    #              BUILD THE AGG PROVENANCE RULE             #
    # ------------------------------------------------------ #

    # ------------------------------------------------------ #
    # generate a random ID for the new provenance rule

    aggprovmeta_rid = tools.getIDFromCounters("rid")

    # ------------------------------------------------------ #
    # initialize rule data

    aggprovmeta_ruleData = {}

    # ------------------------------------------------------ #
    # the provenance rule name ends with "_bindings" appended
    # with a unique number

    aggprovmeta_ruleData["relationName"] = aggRule.ruleData[
        "relationName"] + "_prov" + str(provid)

    # ------------------------------------------------------ #
    # the goal att list consists of all subgoal atts

    if USING_MOLLY:
        orig_aggRule_goalAttList = sortGoalAttList(orig_aggRule_goalAttList)

    aggprovmeta_ruleData["goalAttList"] = orig_aggRule_goalAttList

    # ------------------------------------------------------ #
    # define goal time arg as empty

    aggprovmeta_ruleData["goalTimeArg"] = ""

    # ------------------------------------------------------ #
    # define subgoal list of dicts
    # agg prov rules only have one subgoal in the head of
    # the previously defined bindings rule

    subgoalListOfDicts = []
    bindings_subgoal = {}
    bindings_subgoal["subgoalName"] = bindingsmeta_ruleData["relationName"]

    # replace all existential vars in the subgoal att list with wildcards
    allGoalAtts = getAllGoalAtts_noAggs(aggprovmeta_ruleData["goalAttList"])
    allSubgoalAtts = bindingsmeta_ruleData["goalAttList"]

    subgoalAttList = []
    for att in allSubgoalAtts:
        if not att in allGoalAtts:
            subgoalAttList.append("_")
        else:
            subgoalAttList.append(att)

    bindings_subgoal["subgoalAttList"] = subgoalAttList
    bindings_subgoal["polarity"] = ""
    bindings_subgoal["subgoalTimeArg"] = ""

    subgoalListOfDicts.append(bindings_subgoal)
    aggprovmeta_ruleData["subgoalListOfDicts"] = subgoalListOfDicts

    # ------------------------------------------------------ #
    # define eqnDict as empty

    aggprovmeta_ruleData["eqnDict"] = {}

    # ------------------------------------------------------ #
    # preserve adjustments by instantiating the new meta rule
    # as a Rule

    aggprovmeta_rule = Rule.Rule(aggprovmeta_rid, aggprovmeta_ruleData, cursor)
    aggprovmeta_rule.rule_type = aggRule.rule_type

    # ------------------------------------------------------ #
    #               REWRITE ORIGINAL AGG RULE                #
    # ------------------------------------------------------ #

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

    # update rule meta with the new bindings subgoal
    aggRule.ruleData["subgoalListOfDicts"] = aggprovmeta_rule.ruleData[
        "subgoalListOfDicts"]
    aggRule.subgoalListOfDicts = aggRule.ruleData["subgoalListOfDicts"]

    # save new subgoal data
    aggRule.saveSubgoals()

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

    # update rule meta with the new empty eqn dict
    aggRule.ruleData["eqnDict"] = aggprovmeta_rule.ruleData["eqnDict"]
    aggRule.eqnDict = aggRule.ruleData["eqnDict"]

    # save new subgoal data
    aggRule.saveEquations()

    return [bindings_rule, aggprovmeta_rule]
Example #16
0
def dedToIR( filename, cursor, settings_path ) :

  parsedLines = []
  parsedLines = dedalusParser.parseDedalus( filename, settings_path ) # program exits here if file cannot be opened.

  logging.debug( "  DED TO IR : parsedLines = " + str( parsedLines ) )

  # collect fact and rule metadata for future dumping.
  # these are lists of object pointers.
  factMeta = []
  ruleMeta = []

  # ----------------------------------------------- #
  #                     FACTS                       #
  # ----------------------------------------------- #
  # process facts first to establish 
  # all fact data types.

  # iterate over parsed lines
  for line in parsedLines :

    if line[0] == "fact" :

      logging.debug( ">> PROCESSING FACT : " + str( line ) )

      # get data for fact
      factData = line[1]

      # generate random ID for fact
      #fid = tools.getID()
      fid = tools.getIDFromCounters( "fid" )

      logging.debug( "  DED TO IR : fid = " + str( fid ) )

      # save fact data in persistent DB using IR
      newFact = Fact.Fact( fid, factData, cursor )

      # save fact metadata (aka object)
      factMeta.append( newFact )

  # ----------------------------------------------- #
  #                     RULES                       #
  # ----------------------------------------------- #
  # process rules after consuming all facts

  # iterate over parsed lines
  for line in parsedLines :

    if line[0] == "rule" : # save rules

      logging.debug( ">> PROCESSING RULE : " + str( line ) )

      # get data for rule
      ruleData = line[1]

      # generate a random ID for the rule
      #rid = tools.getID()
      rid = tools.getIDFromCounters( "rid" )

      logging.debug( "  DED TO IR : rid = " + str( rid ) )

      # save rule goal info
      newRule = Rule.Rule( rid, ruleData, cursor )

      # save rule metadata (aka object)
      ruleMeta.append( newRule )

  return [ factMeta, ruleMeta ]
Example #17
0
def insertDomainFactOffParDomain(cursor, rule, checkName, ruleMeta, factMeta):

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

    parRule, childRule, childVars = collectDomainRuleInfo(
        cursor, rule, ruleMeta)
    parRule = parRule[0]
    childRule = childRule[0]
    newRules = []
    newFacts = []

    # in this case  we want to base it on the previous iteration of the goal.
    for subgoal in parRule.subgoalListOfDicts:
        if subgoal['subgoalName'] == rule[0]:
            for attIndex in range(0, len(subgoal['subgoalAttList'])):
                att = childRule.goalAttList[attIndex]
                ruleData = createDomRule(rule[0], attIndex,
                                         childRule.goalTimeArg, att)

                # add in the adom
                dom_name = "dom_str"
                if childVars[childRule.goalAttList[attIndex]].lower() == 'int':
                    dom_name = "dom_int"

                goalDict = createSubgoalDict(dom_name, [att], '',
                                             childRule.goalTimeArg)
                ruleData['subgoalListOfDicts'].append(goalDict)
                done = False
                for parAttIndex in range(0, len(parRule.goalAttList)):
                    parAtt = parRule.goalAttList[parAttIndex]
                    subgoalAtt = subgoal['subgoalAttList'][attIndex]
                    if parAtt == subgoalAtt:
                        # in here we define the new rule based on this parent index.
                        goalDict = createSubgoalDict(
                            'dom_' + str(checkName) + '_' + str(parAttIndex),
                            [att], '', childRule.goalTimeArg)
                        ruleData['subgoalListOfDicts'].append(goalDict)
                        domrid = tools.getIDFromCounters("rid")
                        newRule = Rule.Rule(domrid, ruleData, cursor)
                        newRules.append(newRule)
                        done = True
                        break

                if done:
                    done = False
                    continue
                # if we get here we need to some sideways information passing
                # with magic set ish stuff.
                # add in the originial rule!
                goalAttList = []
                for i in range(0, len(childRule.goalAttList)):
                    if i == attIndex:
                        goalAttList.append(att)
                    else:
                        goalAttList.append('_')
                goalDict = createSubgoalDict( childRule.relationName, \
                                              goalAttList, \
                                              'notin', \
                                              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
Example #18
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