예제 #1
0
def pjcintersection(rls1, w1, rls2, w2, ds, wsc, cac, F, fd):
    mrls = []

    for i in range(len(rls1)):
        for j in range(len(rls2)):

            newantec = []
            noint = 0
            for f in F:
                ai = rls1[i].searchantec(f)
                aj = rls2[j].searchantec(f)

                newc, outcome = getintersection(ai, aj, f, fd[f])
                if outcome == 0:
                    noint = 1

                newantec.extend(newc)

            newrl = None
            wij = getweight(w1[i], w2[j], wsc)
            cij = assignclass(w1[i], w2[j], rls1[i].cons, rls2[j].cons, cac)

            if noint == 1:
                newrl = Rule([], cij)
                noint = 0
            else:
                newrl = Rule(newantec, cij)

            entry = TableEntryPJC(rls1[i], w1[i], rls2[j], w2[j], newrl, wij)
            mrls.append(entry)

    return mrls

    self
예제 #2
0
def topDownInduction(tree, s, split=False, LabelDict=None):
    """
    Rule Generation
    """
    global INVALIDS, DELETES, EXCEPTIONS

    logging.info("string: {}".format(s))
    logging.info("tree: {}".format(tree))
    
    rules = set()
    treeBuffer = [(tree,'S!')]
    try:
        while treeBuffer:
        
            r = Rule()
            node, label = treeBuffer.pop()
            
            arguments = ['X'] * len(node.childNodes) if not split else [LabelDict[child.name] for child in node.childNodes]
            
            for idx,child in enumerate(node.childNodes):
                treeBuffer.append((child,arguments[idx]))
            #
            # create rule label
            #
            r.label = getLabel(label, node.idx, arguments)
            #
            # create string representation
            #
            logging.debug("node.interval:".format(node.interval))
            interval = node.interval
            for child in node.childNodes:
                interval = interval.without(child.interval)
            logging.debug("Interval:".format(interval))
            r.s = getStringRule(interval, s)
            #print r.s
            #
            # create meaning representation
            #
            r.t = getMeaningRule(node.name,len(node.childNodes))
            
            if len(node.childNodes) != interval.flatten().count(-1):
                logging.debug("Invalid number of arguments: child({}) interval({})".format(
                    len(node.childNodes),
                    interval.flatten().count(-1))
                )
                INVALIDS += 1
                return set()
            if r.s in ("?1", "*(?1,?2)"):
                logging.debug("Deleting homorphism: {}".format(r.s))
                DELETES += 1
                return set()
            
            rules.add(r)

    except Exception as e:
        logging.error(e)
        EXCEPTIONS += 1
        return set()

    return rules
예제 #3
0
    def insert_rule(self, row):
        """This function inserts new rule into rule map and if a rule already exists then updates the
        existing rule"""
        IP = Range(row[3])

        if '-' in row[2]:
            # Port is ranged
            port_range = row[2].split("-")
            start = int(port_range[0])
            end = int(port_range[1])

            for port in range(start, end + 1):
                if int(port) in self.rule_map:
                    existing_rule = self.rule_map[int(port)]
                    existing_rule.extend_permission(Rule(row[0], row[1], IP))

                self.rule_map[int(port)] = Rule(row[0], row[1], IP)
        else:
            # Port is fixed
            port = row[2]
            if int(port) in self.rule_map:
                existing_rule = self.rule_map[int(port)]
                existing_rule.extend_permission(Rule(row[0], row[1], IP))

            self.rule_map[int(port)] = Rule(row[0], row[1], IP)
def read_rules():
	#file=open("rules_new.txt","r")
	file=open("rules.txt","r")
	rules=file.readlines()
	file.close()
	rules_objects=[]
	for each in rules:
		each=each.replace("\n","")
		each=each.replace(" ","")
		if (each != ""):
			rule_object=Rule(each)
			rules_objects.append(rule_object)

	choice = int(input("Enter Part No.:"))
	if choice == 1:
		input_rule=input("Enter Term: ")
		input_rule_object=Rule(input_rule)
		result_rule=evaluate(rules_objects,input_rule_object)
		print ("Evaluated Solution:",result_rule.string)
	elif choice == 2:
		input_rule=input("Enter Equation to be solved: ")
		input_rule_object=Rule(input_rule)
		part_two_type_check(input_rule_object)
		if input_rule_object.function_type=='->':
			part_two_main(rules_objects,input_rule_object)
		else:
			print("Wrong Equation. Exiting.")
	else :
		print("Wrong Choice. Exiting.")
예제 #5
0
    def __init__(self, refresh, vsrv_num, rule_num, vsrv_nam):
        CTK.Container.__init__(self)

        rule = Rule('vserver!%s!rule!%s!match' % (vsrv_num, rule_num))
        rule_nam = rule.GetName()
        self += CTK.RawHTML('<h2><a href="/vserver/%s">%s</a> &rarr; %s</h2>' %
                            (vsrv_num, CTK.escape_html(vsrv_nam), rule_nam))
예제 #6
0
 def newObject(self,e=''):
     """
     adds into the current ruleChain (starting a new Rule)
     """
     rule = Rule(e)
     rule.minZoom=float(self.scalepair[0])
     rule.maxZoom=float(self.scalepair[1])
     self.ruleChains.append(rule)
예제 #7
0
 def newObject(self, e=''):
     """
     adds into the current ruleChain (starting a new Rule)
     """
     rule = Rule(e)
     rule.minZoom = float(self.scalepair[0])
     rule.maxZoom = float(self.scalepair[1])
     self.ruleChains.append(rule)
예제 #8
0
def getdtrules(dt):
    ID_IDX = 0
    IF1_IDX = 3
    ID1_IDX = 6
    IF2_IDX = 8
    ID2_IDX = 11
    CLASS_IDX = 2
    LAB_IDX = 4
    class_str = "class"
    delim = " "
    stdc = {}

    for i in dt:
        lv1tkns = i.strip('\n').split(" ")
        id = 0
        try:
            id = int(lv1tkns[ID_IDX])
        except ValueError:
            continue

        if lv1tkns[CLASS_IDX] == class_str:
            stdc[id] = lv1tkns[LAB_IDX]
        else:
            stdc[id] = lv1tkns[IF1_IDX] + delim + lv1tkns[ID1_IDX] + delim + lv1tkns[IF2_IDX] + delim + lv1tkns[ID2_IDX]


    vislst = []
    rules = []

    ids = list(stdc.keys())
    ids.sort()

    exp1, exp2 = getexprs(stdc[ids[0]])  
    vislst.extend([[Rule([exp1[0]], ""), exp1[1]],
                   [Rule([exp2[0]], ""), exp2[1]]])  

    curr = None 
    while len(vislst) > 0:
        if curr is None:
            curr = vislst[0]  
            vislst.remove(curr)  

        exp1, exp2 = getexprs(stdc[curr[1]])  
        if exp1 is None:
            curr[0].cons = stdc[curr[1]] 
            rules.append(curr[0])
            curr = None
        else:
            newant = list(curr[0].antec) 
            newant.append(exp2[0])
            newrl = Rule(newant, "")
            curr[1] = exp1[1]
            curr[0].antec.append(exp1[0]) 
            vislst.append(
                [newrl, exp2[1]]) 

    return rules
예제 #9
0
    def write_rules(self,req):
        '''Get the posted rules and write them to the temporary session table.'''
        # read post values and save them. Intended as an Ajax call.

        form_vars = req.POST
            #print "Failed to open the rules file."
            # rule_file = open('/var/tmp/rules_list.dat','wb')

        rules = []
        try:
            try:
                rule_file = open('/var/tmp/rules_list.dat','rb')
            except:
                pass
            rules = pickle.load(rule_file)
            rule_file.close()
        except:
            rules = []
            #return "Failed to read the rules."

        try:
            command = form_vars['command']
            if command == 'clear':
                try:
                    rule_file = open('/var/tmp/rules_list.dat','wb')
                    rules = []
                    pickle.dump(rules,rule_file)
                    rule_file.close()
                except:
                    pass
                # self.session.cachestore['temp_rules'] = rules
                # self.session.save()
                return 'success'
        except KeyError:
            pass



        try:
            r = Rule()
            r.action = form_vars['action']
            r.content = 'children:' + form_vars['content']
            r.theme = 'children:' + form_vars['theme']
        except KeyError:
            return "failed"


        rules.append(r)
        try:
            rule_file = open('/var/tmp/rules_list.dat','wb')
            pickle.dump(rules,rule_file)
        except:
            return "Failed to Write rules"

        # self.session.cachestore['temp_rules'] = rules
        # self.session.save()
        return 'success'
        def __init__ (self, refresh, refresh_header, vsrv, rule, apply):
            CTK.Container.__init__ (self)
            pre = 'vserver!%s!rule!%s!match' %(vsrv, rule)

            rule = Rule (pre)
            rule.bind ('changed',        refresh.JS_to_refresh() + refresh_header.JS_to_refresh())
            rule.bind ('submit_success', refresh.JS_to_refresh() + refresh_header.JS_to_refresh())

            self += CTK.RawHTML ("<h2>%s</h2>" % (_('Matching Rule')))
            self += CTK.Indenter (rule)
예제 #11
0
 def newRule(self):
     name, ok = QInputDialog.getText(None, "Create new rule",
                                     "Enter new rule name:")
     if name is not None and ok:
         self.rule = Rule()
         self.rule.setName(name)
         self.ruleSet.addRule(self.rule)
     self.updateRuleList()
     self.changeCurrentRule(len(self.ruleSet.ruleList) - 1)
     self.ruleListWidget.setCurrentRow(len(self.ruleSet.ruleList) - 1)
예제 #12
0
def create_rules():
    rules = {}
    rules[0] = Rule('center', 41, 1.1, 3, 8, 'min',
                    ["Obiekt ", " znajduje się na środku pola", ". "],
                    ["Object ", " is on the center of field", ". "])
    rules[1] = Rule('top left corner', 42, 1, 1.1, 6, 'min',
                    ["Obiekt ", " znajduje się w lewym-górnym narożniku pola", ". "],
                    ["Object ", " is on the left-top corner of field", ". "])
    rules[2] = Rule('top right corner', 43, 1.1, 5, 6, 'min',
                    ["Obiekt ", " znajduje się w prawym-górnym narożniku pola", ". "],
                    ["Object ", " is on the left-top corner of field", ". "])
    rules[3] = Rule('bottom right corner', 44, 1.1, 5, 10, 'min',
                    ["Obiekt ", " w prawym-dolnym narożniku pola", ". "],
                    ["Object ", " is on the right-bottom corner of field", ". "])
    rules[4] = Rule('bottom left corner', 45, 1, 1, 10, 'min',
                    ["Obiekt ", " znajduje się w lewym-dolnym narożniku pola", ". "],
                    ["Object ", " is on the left-bottom corner of field", ". "])
    rules[5] = Rule('top left part', 46, 1, 2, 7, 'min',
                    ["Obiekt ", " znajduje się w lewej-górnej części pola", ". "],
                    ["Object ", " is on the left-top part of field", ". "])
    rules[6] = Rule('top right part', 47, 1, 4, 7, 'min',
                    ["Obiekt ", " znajduje się w prawej-górnej części pola", ". "],
                    ["Object ", " is on the right-top part of field", ". "])
    rules[7] = Rule('bottom right part', 48, 1, 4, 9, 'min',
                    ["Obiekt ", " znajduje się w prawej-dolnej części pola", ". "],
                    ["Object ", " is on the right-bottom part of field", ". "])
    rules[8] = Rule('bottom left part', 49, 1, 2, 9, 'min',
                    ["Obiekt ", " znajduje się w lewej-dolnej części pola", ". "],
                    ["Object ", " is on the left-bottom part of field", ". "])
    return rules
예제 #13
0
 def event_setup(self, squadron, count):
     r = Rule('onwing')
     r.subject = 'instructor'
     r.qual = Qual('onwing')
     r.qual.objectType = 'student'
     for i in range(1, count + 1):
         e = Event(i)
         e.flightHours = 1.0
         e.instructionalHours = 0.0
         e.rules.append(r)
         if i-1 in squadron.syllabus:
             e.precedingEvents.add(squadron.syllabus[i-1])
             squadron.syllabus[i-1].followingEvents.add(e)
         squadron.syllabus[i] = e
예제 #14
0
    def generate_negative_rules(self, train, confident_value_pass, zone_confident_pass):

        class_value_arr = self.get_class_value_array(train)
        self.prepare_data_rows(train)
        for i in range(0, len(self.rule_base_array)):
            rule_negative = Rule(self.data_base)
            rule_negative.antecedent = self.rule_base_array[i].antecedent
            positive_rule_class_value = self.rule_base_array[i].get_class()
            print("the positive rule class value is " + str(positive_rule_class_value) + " ,the i is :" + str(i))
            # rule_negative.setClass(positive_rule_class_value)

            for j in range(0, len(class_value_arr)):
                class_type = int(class_value_arr[j])
                if positive_rule_class_value != class_type:  # need to get another class value for negative rule

                    rule_negative.setClass(class_type)  # change the class type in the rule
                    rule_negative.calculate_confident_support(self.data_row_array)
                    print("Negative rule's  confident value is :" + str(rule_negative.confident_value))

                    if rule_negative.confident_value > confident_value_pass and rule_negative.zone_confident > zone_confident_pass:
                        rule_negative.weight = rule_negative.confident_value
                        if not (self.duplicated_negative_rule(rule_negative)):

                            for k in range(0, len(rule_negative.antecedent)):
                                print("antecedent L_ " + str(rule_negative.antecedent[j]))
                            # print("Negative rule's class value " + str(rule_negative.get_class()))
                            # print(" Negative rule's weight, confident_vale  " + str(rule_negative.weight))
                            # print(" Negative rule's zone confident value   " + str(rule_negative.zone_confident))
                            # print("Negative rule's positive_rule_class_value" + str(positive_rule_class_value))
                            # print("Negative rule's class_type" + str(class_type))
                            self.negative_rule_base_array.append(rule_negative)
예제 #15
0
        def __init__ (self, refresh, refresh_header, vsrv, rule, apply):
            CTK.Container.__init__ (self)
            pre = 'vserver!%s!rule!%s!match' %(vsrv, rule)

            rule = Rule (pre)
            rule.bind ('changed',        refresh.JS_to_refresh() + refresh_header.JS_to_refresh())
            rule.bind ('submit_success', refresh.JS_to_refresh() + refresh_header.JS_to_refresh())

            self += CTK.RawHTML ("<h2>%s</h2>" % (_('Matching Rule')))
            self += CTK.Indenter (rule)

            # Trigger the 'update_rule_list' so the Rule list sibling
            # widget is updated.
            #
            rule.bind ('submit_success', rule.JS_to_trigger('update_rule_list'))
예제 #16
0
    def add_itemset(self, itemset_pass):
        item = None
        antecedent_array = [0 for x in range(self.n_variables)]
        for i in range(0, self.n_variables):
            antecedent_array[i] = -1
        for i in range(0, itemset_pass.size()):
            item = itemset_pass.get(i)
            antecedent_array[item.get_variable()] = item.get_label()

        rule = Rule(self.data_base)
        rule.assign_antecedente(antecedent_array)
        rule.set_consequent(itemset_pass.get_class())
        rule.set_confidence(itemset_pass.get_support_class() / itemset_pass.get_support())
        rule.set_support(itemset_pass.get_support_class())
        self.rule_base_array.append(rule)
예제 #17
0
    def create_three_step_right_org(self):

        this_rules = []
        for direction in self.right_code:
            for obj1 in self.obj_code:
                for obj2 in self.obj_code:
                    for obj3 in self.obj_code:
                        for obj4 in self.obj_code:
                            if obj1 != obj2 and obj1 != obj3 and obj1 != obj4 and obj2 != obj3 and obj2 != obj4 and \
                                    obj3 != obj4:
                                rule_str = ""
                                rule_str += self.impl_code[0]  # ->
                                rule_str += self.con_code[0]  # AND
                                rule_str += self.right_code[3]  # lefts
                                rule_str += obj1
                                rule_str += obj2
                                rule_str += self.con_code[0]  # AND
                                rule_str += self.right_code[3]  # lefts
                                rule_str += obj3
                                rule_str += obj4
                                rule_str += self.right_code[3]  # lefts
                                rule_str += obj2
                                rule_str += obj3
                                rule_str += direction
                                rule_str += obj1
                                rule_str += obj3
                                this_rules.append(Rule(rule_str, False))
        return this_rules
예제 #18
0
 def newObject(self,e=''):
   """
   adds into the current ruleChain (starting a new Rule)
   """
   self.ruleChains[self.rcpos].append(Rule(e))
   self.ruleChains[self.rcpos][-1].minZoom=float(self.scalepair[0])
   self.ruleChains[self.rcpos][-1].maxZoom=float(self.scalepair[1])
예제 #19
0
파일: AgentPool.py 프로젝트: croatis/SATLO
 def __init__(self, identifier, actionSet, minIndividualRunsPerGen,
              trafficLightsAssigned):
     self.id = identifier  # AgentPool name
     self.actionSet = actionSet  # A list of action names that can be applied by assigned TL's of the pool
     self.addDoNothingAction()  # Add "do nothing" action to action set
     self.trafficLightsAssigned = [
     ]  # List of traffic lights using Agent Pool
     self.setTrafficLightsAssigned(trafficLightsAssigned)
     self.individuals = []
     self.userDefinedRuleSet = [
         Rule(-1, ["emergencyVehicleApproachingVertical"], -1, self),
         Rule(-1, ["emergencyVehicleApproachingHorizontal"], -1, self),
         Rule(-1, ["maxGreenPhaseTimeReached"], -1, self),
         Rule(-1, ["maxYellowPhaseTimeReached"], -1, self)
     ]
     self.minIndividualRunsPerGen = minIndividualRunsPerGen
예제 #20
0
 def create_one_sided_rules_left(self):
     """
     creates rules like: a L b AND b L c -> a L c
     :return:
     """
     this_rules = []
     for direction in self.left_code:
         for obj1 in self.obj_code:
             for obj2 in self.obj_code:
                 for obj3 in self.obj_code:
                     if obj1 != obj2 and obj2 != obj3 and obj1 != obj3:
                         rule_str = ""
                         rule_str += self.impl_code[0]  # ->
                         rule_str += self.con_code[0]        # AND
                         rule_str += self.left_code[3]  # lefts
                         rule_str += obj1
                         rule_str += obj2
                         rule_str += self.left_code[3]  # lefts
                         rule_str += obj2
                         rule_str += obj3
                         rule_str += direction
                         rule_str += obj1
                         rule_str += obj3
                         this_rules.append(Rule(rule_str, False))
     return this_rules
예제 #21
0
    def goalToRule(path):
        with open(path) as f:
            jsonFile = json.load(f)
        lhs = Graph("lhs")
        if "objects" in jsonFile["graph"]:
            for objects in jsonFile["graph"]["objects"]:
                lhs.addVertex(objects["id"], objects["type"])
        if "relations" in jsonFile["graph"]:
            for relations in jsonFile["graph"]["relations"]:
                lhs.addEdge(relations["source"], relations["target"], relations["type"])

        rhs = Graph("rhs")

        nacs = []
        cnt = 0
        if "nacs" in jsonFile:
            for nac in jsonFile["nacs"]:
                str = "nac {}".format(cnt)
                cnt += 1
                g = Graph(str)
                if "objects" in nac:
                    for objects in nac["objects"]:
                        g.addVertex(objects["id"], objects["type"])
                if "relations" in nac:
                    for relations in nac["relations"]:
                        g.addEdge(relations["source"], relations["target"], relations["type"])
                nacs.append(g)

        return Rule("goal", lhs, rhs, nacs)
예제 #22
0
 def create_basic_rules_front_back(self):
     """
     creates rules like: a F b <-> b B a
     :return:
     """
     this_rules = []
     for direction in range(0, 4):
         for each1 in self.objects:
             for each2 in self.objects:
                 if each1 != each2:
                     # start making front/back rules
                     parent = Node("<->")
                     lchild = Node(self.front[direction], None, None, parent)
                     rchild = Node(self.back[direction], None, None, parent)
                     rrchild = Node(each1, None, None, rchild)
                     rlchild = Node(each2, None, None, rchild)
                     rchild.right = rrchild
                     rchild.left = rlchild
                     lrchild = Node(each2, None, None, lchild)
                     llchild = Node(each1, None, None, lchild)
                     lchild.left = llchild
                     lchild.right = lrchild
                     parent.left = lchild
                     parent.right = rchild
                     rule = Rule(parent)
                     this_rules.append(rule)
     return this_rules
예제 #23
0
def predict_test():
    earley = Earley('a')
    earley.rules = [Rule('S#', 'S'), Rule('S', 'a')]
    earley.situations_dict[0] = set()
    earley.situations_dict[0].add(Situation('S#', 'S', 0, 0))
    earley.predict(0)

    is_added = False
    to_add = Situation('S', 'a', 0, 0)
    for sit in earley.situations_dict[0]:
        if (sit.input == to_add.input and sit.output == to_add.output
                and sit.point == to_add.point and sit.ind == to_add.ind):
            is_added = True

    assert is_added is True
    print('Predict test passed\n')
예제 #24
0
def write_in_rule(rules_string_arr):
    rule = Rule()
    scope = 0
    skip_idx = 0
    #    print('ARR: ', rules_string_arr)
    if len(rules_string_arr[-1]) < 1 or rules_string_arr[-1][0] not in letters:
        print('Expression must and with letter!')
        return None
    for i in rules_string_arr:
        if skip_idx > 0:
            skip_idx -= 1
            continue
        if i in letters:
            rule.facts.append(i)
        elif i in operations:
            rule.operations.append(i)
        else:
            tmp_rule, skip_idx = new_group_str(rules_string_arr)
            if tmp_rule is None or skip_idx is None:
                return None
            scope += 1
            if tmp_rule is None:
                return None
            rule.facts.append(tmp_rule)
    return rule
예제 #25
0
 def detect_relationship_rules(self, relationship, rule_count_limit=1):
     '''
     @return a list of Rule objects, no longer than rule_count_limit
     '''
     extract_objects = lambda figure: FigureObjectSet(figure.objects)
     figure_sequence = map(extract_objects, relationship)
     return [Rule(figure_sequence)]
def createRandomRule(agentPool, ruleType):
    conditions = []  # Conditions for a rule

    # RS rule
    if ruleType == 0:
        # Set conditions of rules as a random amount of random predicates
        for i in range(randint(1, maxRulePredicates)):
            newCond = PredicateSet.getRandomPredicate()
            if checkValidCond(newCond, conditions):
                conditions.append(newCond)

        # RSint rule
    elif ruleType == 1:
        # Set conditions of rules as a random amount of random predicates
        for i in range(randint(1, maxRulePredicates)):
            newCond = agentPool.getRandomRSintPredicate()
            if checkValidCond(newCond, conditions):
                conditions.append(newCond)

        # Get index of possible action. SUMO changes phases on indexes
    action = randrange(
        0, len(agentPool.getActionSet())
    )  # Set rule action to a random action from ActionSet pertaining to Agent Pool being serviced
    #print("The action set is:", agentPool.getActionSet())
    rule = Rule(ruleType, conditions, action, agentPool)

    return rule
예제 #27
0
 def create_three_step_left_org(self):
     """
     creates rules like: a L b AND b L c AND c L d -> a L d
     :return:
     """
     this_rules = []
     for direction in self.left_code:
         for obj1 in self.obj_code:
             for obj2 in self.obj_code:
                 for obj3 in self.obj_code:
                     for obj4 in self.obj_code:
                         if obj1 != obj2 and obj1 != obj3 and obj1 != obj4 and obj2 != obj3 and obj2 != obj4 and \
                                 obj3 != obj4:
                             rule_str = ""
                             rule_str += self.impl_code[0]  # ->
                             rule_str += self.con_code[0]  # AND
                             rule_str += self.left_code[3]  # lefts
                             rule_str += obj1
                             rule_str += obj2
                             rule_str += self.con_code[0]  # AND
                             rule_str += self.left_code[3]  # lefts
                             rule_str += obj3
                             rule_str += obj4
                             rule_str += self.left_code[3]  # lefts
                             rule_str += obj2
                             rule_str += obj3
                             rule_str += direction
                             rule_str += obj1
                             rule_str += obj3
                             this_rules.append(Rule(rule_str, False))
     return this_rules
예제 #28
0
def test_misshapen_indices_3():
    indices = [
        ((1, -2), (10, 20)),
        ((3, 4, 5), (-30, 40)),
        ((6, -7, 8, 9), (60, 70)),
    ]
    with pytest.raises(ValueError):
        Rule(indices, GAME_OF_LIFE_RULESET)
예제 #29
0
 def rule(self, p):
     # Just do LOD=1 for now
     newRule = Rule(p.left, p.right, 1)
     if newRule.label in self.ruleFromLabel:
         self.ruleFromLabel[newRule.label].append(newRule)
     else:
         self.ruleFromLabel[newRule.label] = [newRule]
     return newRule
예제 #30
0
 def __get_rules(self, lines: List[str], facts: List[Fact]) -> List[Rule]:
     ans = []
     for line in lines:
         line = line.strip()
         if not line:
             continue
         ans.append(Rule.init_from_str(line, facts))
     return ans
예제 #31
0
 def __specialize_complexes(self, stars):
     new_star = []
     if stars is self.selectors:
         for selector in self.selectors:
             rule = Rule.Rule()
             rule.add_condition(selector[0], selector[1])
             new_star.append(rule)
         return new_star
     else:
         for selector in self.selectors:
             for star in stars:
                 star_copy = Rule.Rule()
                 star_copy.add_existing_conditions(star.conditions)
                 star_copy.add_condition(selector[0], selector[1])
                 if star_copy.check_if_conditions_unique():
                     new_star.append(star_copy)
     return new_star
예제 #32
0
def arr_to_rule(arr):
    rule = Rule()
    for i in arr:
        if i in letters:
            rule.facts.append(i)
        else:
            rule.operations.append(i)
    return rule
예제 #33
0
파일: Engine.py 프로젝트: leticia16/infeng
 def load_rules(self, rules):
     print('Loading rules...')
     for i in rules:
         rule = Rule(i)
         self.knowledge_base.append(rule)
         # associa uma um valor (A) às regras nas quais ele
         # aparece no lado direito de uma regra
         self.values_table[rule.right].add_rule(rule)
예제 #34
0
 def test_rule_txt_with_three_rules(self):
     txt_rule = "L1>=20\n" \
                "L2=3\n" \
                "W<=90".splitlines()
     exp_result = [("L1", ">=", 20),
                   ("L2", "=", 3),
                   ("W", "<=", 90)]
     self.assertEqual(exp_result, Rule.parse(txt_rule))
예제 #35
0
 def setup(self, filename):
     #read txt file
     #construct attr
     grammar_file = open(filename)
     line_count = 0
     for line in grammar_file:
         line = line.strip('\n')
         if (line_count == 0):
             self.start = line.split(' ')[0]
             self.nonterminal = set(line.split(' '))
         elif (line_count == 1):
             self.terminal = set(line.split(' '))
         else:
             rule = Rule()
             rule.setRule(line.split(' '))
             self.rule.append(rule)
         line_count += 1
예제 #36
0
    def searchForBestAntecedent(self,example,clas):
            ruleInstance=Rule( )
            ruleInstance.setTwoParameters(self.n_variables, self.compatibilityType)
            print("In searchForBestAntecedent ,self.n_variables is :" + str(self.n_variables))
            ruleInstance.setClass(clas)
            print("In searchForBestAntecedent ,self.n_labels is :" + str(self.n_labels))
            for i in range( 0,self.n_variables):
                max = 0.0
                etq = -1
                per= None
                for j in range( 0, self.n_labels) :
                    print("Inside the second loop of searchForBestAntecedent......")
                    per = self.dataBase.membershipFunction(i, j, example[i])
                    if (per > max) :
                        max = per
                        etq = j
                if (max == 0.0) :
                    print("There was an Error while searching for the antecedent of the rule")
                    print("Example: ")
                    for j in range(0,self.n_variables):
                        print(example[j] + "\t")

                    print("Variable " + str(i))
                    exit(1)

                ruleInstance.antecedent[i] = self.dataBase.clone(i, etq)
            return ruleInstance
예제 #37
0
 def newRule(self):
     name, ok = QInputDialog.getText(None, "Create new rule", "Enter new rule name:")
     if name is not None and ok:
         self.rule = Rule()
         self.rule.setName(name)
         self.ruleSet.addRule(self.rule)
     self.updateRuleList()
     self.changeCurrentRule(len(self.ruleSet.ruleList) - 1)
     self.ruleListWidget.setCurrentRow(len(self.ruleSet.ruleList) - 1)
예제 #38
0
    def test_W_03(self):
        txt_rule = "L1>0\n" \
                   "L1<10\n" \
                   "L2>0\n" \
                   "L2<10\n" \
                   "W==90".splitlines()
        rules = Rule.parse(txt_rule)

        Equation.validate_rules_for_w(rules)
예제 #39
0
    def get_ruleset(self):
        ruleset = []

        pair_inclusion = []
        for bw in self.based_words:
            for ic in self.inclusion:
                pair = [bw, ic]
                pair_inclusion.append(pair)

        #Combine each pair in inclusion_list with exclusion_list
        for pair in pair_inclusion:
            for ew in self.exclusion:
                rule = Rule()
                rule.inc_keywords = pair
                rule.exc_keywords.append(ew)

                ruleset.append(rule)

        return ruleset
예제 #40
0
    def test_W_01(self):
        txt_rule = "L1>0\n" \
                   "L1<10\n" \
                   "L2>0\n" \
                   "L2<10\n" \
                   "W<=90".splitlines()
        rules = Rule.parse(txt_rule)

        exceptions = (RuleException)
        with self.assertRaises(exceptions):
            Equation.validate_rules_for_w(rules)
예제 #41
0
파일: RuleManager.py 프로젝트: theNailz/drw
    def getRule(self, rid):
        dbRule = self.db.read("SELECT rid, name, enabled FROM rule r WHERE r.rid = :rid", { "rid": int(rid) })

        if dbRule == None:
            return False

        dbRule = dbRule.fetchone()

        if not dbRule:
            return False

        rule = Rule(dbRule['rid'], dbRule['name'], dbRule['enabled'])

        dbResConditions = self.db.read("SELECT c.cid, c.rule_rid, c.device_did, c.device_value_vid, c.operator, c.value, d.name FROM condition c LEFT JOIN device d ON d.did = c.device_did WHERE rule_rid = :rid", {"rid": rule.rid})

        for (cid, rule_rid, device_did, device_value_vid, operator, value, device_name) in dbResConditions.fetchall():
            rule.addCondition({
                "cid": cid,
                "rule_rid": rule_rid,
                "device_did": device_did,
                "device_name": device_name,
                "device_value_vid": device_value_vid,
                "operator": operator,
                "value": value
            })

        dbResActions = self.db.read("SELECT a.aid, a.rule_rid, a.device_did, a.device_action_aid, a.value, d.name FROM action a LEFT JOIN device d ON d.did = a.device_did WHERE a.rule_rid = :rid", {"rid": rule.rid})

        for (aid, rule_rid, device_did, device_action_aid, value, device_name) in dbResActions.fetchall():
            rule.addAction({
                "aid": aid,
                "rule_rid": rule_rid,
                "device_did": device_did,
                "device_name": device_name,
                "device_action_aid": device_action_aid,
                "value": value
            })

        return rule
예제 #42
0
	def load(self, file, info = False):
		'''
			Loads employee data
			@param file: a file pointer open in "rb" mode
			@params info: T/F whether to print additional info while loading.
			@return True if load was successful, False if any error occured
		'''
		try:
			self.name = pickle.load(file)
			print("  Loading data for %s..."%self.name)
			self.priority = pickle.load(file)
			if info: print("    Read priority: %d"%self.priority)
			self.curShifts = pickle.load(file)
			if info: print("    Read number of shifts: %d"%self.curShifts)
			self.shiftsPerWeek = pickle.load(file)
			if info: print("    Read shifts per week: %s"%self.shiftsPerWeek)
			numRules = pickle.load(file)
			if info: print("    Read number of rules: %d"%numRules)

			print("    %d rules to load"%numRules)
			self.rules = []
			num = 0
			for i in range(0,numRules):
				r = Rule()
				if not r.load(file, num, info):
					print("\nError (%s): Loaded invalid rule, aborting..."%self.name)
					return False
				self.setRule(r)
				num += 1

		except Exception as e:
			print("\nError: %s while loading %s"%(str(e), self.name))
			return False

		if info: print("  Finished loading data for %s"%self.name)
		return True
예제 #43
0
    def get_Rule(self, slist, bookmark, weight):
        def logged_match(m, file, variables):
            common.debug("Testing %s against %s rule (%s)" % (file, self.match_token[0], " ".join(slist)))
            return m(file, variables)

        r = Rule()
        match = self.get_match_function(slist, {"bookmark": bookmark, "weight": weight})
        if match is not None:
            r.bookmark = bookmark
            r.weight = weight
            r.text = " ".join(slist)
            r.match_func = lambda file, variables: logged_match(match, file, variables)
            r.match_token = self.match_token[0]
            return r
예제 #44
0
 def __init__(self,items,func,msg='%s'):
   Rule.__init__(self, "action(%s,%s)" % (items, func), items, msg)
   self.func = func
예제 #45
0
 def test_rule_txt_with_two_rules(self):
     txt_rule = "L1>=20\n" \
                "L2=3".splitlines()
     exp_result = [("L1", ">=", 20),
                   ("L2", "=", 3)]
     self.assertEqual(exp_result, Rule.parse(txt_rule))
예제 #46
0
 def test_rule_txt_as_none(self):
     exceptions = (RuleException)
     with self.assertRaises(exceptions):
         Rule.parse(None)
예제 #47
0
 def test_rule_txt_with_missing_value(self):
     exceptions = (RuleException)
     with self.assertRaises(exceptions):
         txt_rule = "L1>=".splitlines()
         Rule.parse(txt_rule)
예제 #48
0
 def test_rule_txt_with_invalid_value_02(self):
     exceptions = (RuleException)
     with self.assertRaises(exceptions):
         txt_rule = "L1>=10.6".splitlines()
         Rule.parse(txt_rule)
예제 #49
0
 def test_rule_txt_with_invalid_operand_02(self):
     exceptions = (RuleException)
     with self.assertRaises(exceptions):
         txt_rule = "L1<>20".splitlines()
         Rule.parse(txt_rule)
    def __get_next_rule__(self, tokens, instances):

        best_pattern = []
        best_match_count = -1

        best_score = float('-inf')
        un_matched_instances = []

        _, ys = zip(*instances)

        improved = True
        while improved:
            improved = False
            current_best_pattern = best_pattern[:]
            insertion_indices = range(len(current_best_pattern) + 1)

            for token in tokens:
                for insertion_index in insertion_indices:
                    new_pattern = best_pattern[:insertion_index] + [token] + best_pattern[insertion_index:]

                    new_rule = Rule(new_pattern)
                    un_matched = []
                    predictions = []
                    match_count = 0
                    for x, y in instances:
                        if new_rule.matches(x):
                            match_count += 1
                            predictions.append(self.positive_label)
                        else:
                            predictions.append(0)
                            un_matched.append((x, y))

                    score = self.rule_score_fn(ys, predictions)
                    if score >= best_score and match_count >= self.min_positive_rules_covered:

                        # If tied, always prefer ones that match more instances
                        if score == best_score and match_count <= best_match_count:
                            continue

                        current_best_pattern = new_pattern
                        best_match_count = match_count

                        best_score = score
                        improved = True
                        un_matched_instances = un_matched
                    pass # End for
                pass # End for
            best_pattern = current_best_pattern
            pass

        if len(best_pattern) == 0:
            return None, None, None

        best_rule = Rule(best_pattern)

        print "\tNew rule added: {0}\n\t\tRule Score: {1}\n\t\tMatches: {2}".format(best_rule, best_score,
                                                                                    best_match_count)

        """ Compute remaining tokens """
        un_matched_positives, un_matched_negatives = self.__partition_by_class__(un_matched_instances)
        positive_doc_freq = compute_document_frequency(un_matched_positives)
        remaining_tokens = self.__above_doc_freq__(positive_doc_freq, self.min_positive_rules_covered)

        return best_rule, un_matched_instances, remaining_tokens
예제 #51
0
 def test_rule_txt_with_invalid_element_02(self):
     exceptions = (RuleException)
     with self.assertRaises(exceptions):
         txt_rule = "12>=20".splitlines()
         Rule.parse(txt_rule)
예제 #52
0
def bottomUpInduction(tree, s, split=False, LabelDict=None):
    """
    Rule Generation
    """
    global INVALIDS, DELETES, EXCEPTIONS

    logging.info("string: {}".format(s))
    logging.info("tree: {}".format(tree))
    
    rules = set()
    treeBuffer = [(tree,tree.interval,'S!')]

    try:
        while treeBuffer:
            r = Rule()
            node, nodeInterval, label = treeBuffer.pop()
            arguments = ['X'] * len(node.childNodes) if not split else [LabelDict[child.name] for child in node.childNodes]
            if len(node.alignment) == 1:
                alignedWord = node.alignment[0]
                tmpInterval = Interval(nodeInterval.first(), alignedWord)
            else:
                minWord, maxWord = min(node.alignment), max(node.alignment)
                tmpInterval = Interval(minWord-1, maxWord)
            
            childsInterval = nodeInterval.without(tmpInterval)
            interval = tmpInterval
            
            if node.childNodes:
                splitIdx = node.childNodes[0].interval.last()
                tmpInterval = Interval(childsInterval.first(),splitIdx+1)
                
                treeBuffer.append((node.childNodes[0],tmpInterval,arguments[0]))
                for idx,child in enumerate(node.childNodes[1:]):
                    oldSplit = splitIdx
                    splitIdx = child.interval.last()
                    tmpInterval = Interval(oldSplit+1,splitIdx+1)
                    treeBuffer.append((child,tmpInterval,arguments[idx+1]))
            
            for _ in range(len(node.childNodes)): interval.addPlaceholder()
            
            #
            # create rule label
            #
            r.label = getLabel(label, node.idx, arguments)
            #
            # create string representation
            #
            r.s = getStringRule(interval, s)
            #
            # create meaning representation
            #
            r.t = getMeaningRule(node.name,len(node.childNodes))
            
            if len(node.childNodes) != interval.flatten().count(-1):
                logging.debug("Invalid number of arguments: child({}) interval({})".format(
                    len(node.childNodes),
                    interval.flatten().count(-1))
                )
                INVALIDS += 1
                return set()
            if r.s in ("?1", "*(?1,?2)"):
                logging.debug("Deleting homorphism: {}".format(r.s))
                DELETES += 1
                return set()
            
            rules.add(r)

    except Exception as e:
        logging.error(e)
        EXCEPTIONS += 1
        return set()

    return rules
예제 #53
0
class RuleEditor(QMainWindow):
    ###########################################################################
    def __init__(self, parent=None):
        super(RuleEditor, self).__init__(parent)

        self.currentElementFile = None
        self.currentRuleFile = None
        self.sceneElementList = []
        self.connections = []
        self.elementSet = ElementSet()
        self.rule = Rule()
        self.ruleSet = RuleSet()

        # Frames
        self.gBox1 = QFrame()
        self.gBox1.setMaximumSize(QSize(250, 600))

        self.scene = ChainScene(-200, -200, 400, 400)
        self.scene.itemInserted.connect(self.itemInserted)

        # Graphics view
        self.graphicsView = QGraphicsView(self.scene)
        self.graphicsView.setMinimumSize(QSize(800, 600))

        #Toolbox
        self.createToolBox()
        self.loadToolBox()

        # Left menu
        self.leftLayout = QVBoxLayout(self.gBox1)
        self.leftInnerLayout = QHBoxLayout()
        self.leftInnerLayout2 = QHBoxLayout()

        self.label1 = QLabel("Elements file:")
        self.fileName = QLabel("--")
        self.chooseFile = QPushButton("Choose file")
        self.loadFile = QPushButton("Load")
        self.ruleListWidget = QListWidget()
        self.addRule = QPushButton("Add rule")
        self.deleteRule = QPushButton("Delete")

        self.leftLayout.addWidget(self.label1)
        self.leftLayout.addWidget(self.fileName)
        self.leftInnerLayout.addWidget(self.chooseFile)
        self.leftInnerLayout.addWidget(self.loadFile)
        self.leftLayout.addLayout(self.leftInnerLayout)
        self.leftLayout.addWidget(self.ruleListWidget)
        self.leftInnerLayout2.addWidget(self.addRule)
        self.leftInnerLayout2.addWidget(self.deleteRule)
        self.leftLayout.addLayout(self.leftInnerLayout2)
        self.leftLayout.addWidget(self.toolBox)

        self.chooseFile.clicked.connect(self.chooseElementsFile)
        self.loadFile.clicked.connect(self.loadElementsFile)
        self.addRule.clicked.connect(self.newRule)
        self.deleteRule.clicked.connect(self.removeRule)
        self.ruleListWidget.currentRowChanged[int].connect(self.changeCurrentRule)


        # Layout
        self.centralWidget = QWidget()
        self.centralLayout = QHBoxLayout()
        self.centralLayout.addWidget(self.gBox1)
        self.centralLayout.addWidget(self.graphicsView)
        self.centralWidget.setLayout(self.centralLayout)

        self.setCentralWidget(self.centralWidget)

        self.resize(1200, 600)

        self.setWindowTitle("WordChain : Rule editor")

        # ToolBars
        self.fileToolBar = self.addToolBar("File")

        self.saveAction = QAction(QIcon(':/images/save.png'), "Save to &File", self,
                                  shortcut="Ctrl+S", triggered=self.saveToFile)
        self.openAction = QAction(QIcon(':/images/open.png'), "Open &File", self,
                                  shortcut="Ctrl+O", triggered=self.loadFromFile)
        self.fileToolBar.addAction(self.saveAction)
        self.fileToolBar.addAction(self.openAction)

        self.pointerToolbar = self.addToolBar("Pointer type")

        pointerButton = QToolButton()
        pointerButton.setCheckable(True)
        pointerButton.setChecked(True)
        pointerButton.setIcon(QIcon(":/images/arrow.png"))
        lineButton = QToolButton()
        lineButton.setCheckable(True)
        lineButton.setIcon(QIcon(":/images/line.png"))

        self.pointerTypeGroup = QButtonGroup()
        self.pointerTypeGroup.addButton(pointerButton, ChainScene.MoveItem)
        self.pointerTypeGroup.addButton(lineButton, ChainScene.InsertLine)

        self.pointerTypeGroup.buttonClicked[int].connect(self.pointerGroupClicked)

        self.pointerToolbar.addWidget(pointerButton)
        self.pointerToolbar.addWidget(lineButton)

        self.testToolbar = self.addToolBar("Model testing")
        testButton = QPushButton()
        testButton.setIcon(QIcon(":/images/play.png"))
        testButton.setText("Test rule")
        testButton.clicked.connect(self.testRule)
        self.testToolbar.addWidget(testButton)

    ###########################################################################
    def createToolBox(self):
        self.toolBox = QToolBox()
        self.toolBox.setMinimumSize(QSize(250, 600))
        self.buttonGroup = QButtonGroup()
        self.buttonGroup.setExclusive(False)
        self.buttonGroup.buttonClicked[int].connect(self.buttonGroupClicked)

        self.toolBoxLayout = QGridLayout()
        self.toolBoxLayout.setRowStretch(4, 10)
        self.toolBoxLayout.setColumnStretch(2, 10)

        self.toolBoxWidget = QWidget()
        self.toolBoxWidget.setLayout(self.toolBoxLayout)
        self.toolBox.addItem(self.toolBoxWidget, "Grammar elements")

    ###########################################################################
    def buttonGroupClicked(self, i):
        if i == len(self.elementSet.elementList):
            pass
        else:
            el = self.elementSet.elementList[i]
            self.scene.setToInert(el)
            self.scene.setMode(ChainScene.InsertItem)

            buttons = self.buttonGroup.buttons()
            for button in buttons:
                if self.buttonGroup.button(i) != button:
                    button.setChecked(False)

    ###########################################################################
    def itemInserted(self, item):
        self.pointerTypeGroup.button(ChainScene.MoveItem).setChecked(True)
        self.scene.setMode(self.pointerTypeGroup.checkedId())

        buttons = self.buttonGroup.buttons()
        for button in buttons:
            button.setChecked(False)
        self.rule.fromElementList(self.scene.items())

    ###########################################################################
    def pointerGroupClicked(self, i):
        self.scene.setMode(self.pointerTypeGroup.checkedId())

    ###########################################################################
    def layoutToolBox(self):
        self.clearToolBox()
        self.loadToolBox()

    ###########################################################################
    def loadToolBox(self):
        rowNum = 0
        colNum = 0
        num = 0
        for elem in self.elementSet.elementList:
            self.toolBoxLayout.addWidget(self.createCellWidget(elem.elementName, elem.image(), num), rowNum, colNum)
            colNum += 1
            num += 1
            if colNum > 1:
                colNum = 0
                rowNum += 1

    ###########################################################################
    def clearToolBox(self):
        cnt = self.toolBoxLayout.count()

        while cnt > 0:
            itm = self.toolBoxLayout.itemAt(cnt - 1)
            if itm is not None:
                wdg = itm.widget()
                self.toolBoxLayout.removeWidget(wdg)
                wdg.hide()
                wdg.deleteLater()
                cnt -= 1

    ###########################################################################
    def createCellWidget(self, text, elementImage, num):
        icon = QIcon(elementImage)
        button = QToolButton()
        button.setIcon(icon)
        button.setIconSize(QSize(50, 50))
        button.setCheckable(True)
        self.buttonGroup.addButton(button, num)

        layout = QGridLayout()
        layout.addWidget(button, 0, 0, Qt.AlignHCenter)
        layout.addWidget(QLabel(text), 1, 0, Qt.AlignHCenter)

        widget = QWidget()
        widget.setLayout(layout)
        return widget

    ###########################################################################
    def saveToFile(self):
        self.rule.fromElementList(self.scene.items())
        if self.currentRuleFile is not None:
            saveFile = QSaveFile(self.currentRuleFile)
            saveFile.open(QIODevice.WriteOnly)
            data = self.ruleSet.toBytes()
            saveFile.writeData(data)
            saveFile.commit()
        else:
            self.saveAs()
        self.setWindowTitle("WordChain : Rule editor : " + self.currentRuleFile)

    ###########################################################################
    def saveAs(self):
        ret = QFileDialog.getSaveFileName(filter='*.rl')
        self.currentRuleFile = ret[0]
        self.saveToFile()
        self.setWindowTitle("WordChain : Rule editor : " + self.currentRuleFile)

    ###########################################################################
    def loadFromFile(self):
        ret = QFileDialog.getOpenFileName(filter='*.rl')
        self.currentRuleFile = ret[0]
        file = open(self.currentRuleFile, 'r')

        st = '\n'.join(file.readlines())

        self.ruleSet.fromString(st)

        if len(self.scene.items()) > 0:
            for item in self.scene.items():
                self.scene.removeItem(item)

        self.currentElementFile = self.ruleSet.elementsFileName
        self.fileName.setText(os.path.basename(self.currentElementFile))
        self.loadElementsFile()
        self.scene.setMode(ChainScene.MoveItem)
        self.updateRuleList()
        self.changeCurrentRule(0)

        self.setWindowTitle("WordChain : Rule editor : " + self.currentRuleFile)

    ###########################################################################
    def showWindow(self):
        self.show()
        self.updateRuleList()

    def updateRuleList(self):
        self.ruleListWidget.clear()
        rowCount = 0
        for rule in self.ruleSet.ruleList:
            self.ruleListWidget.insertItem(rowCount, rule.name)
            rowCount += 1

    def newRule(self):
        name, ok = QInputDialog.getText(None, "Create new rule", "Enter new rule name:")
        if name is not None and ok:
            self.rule = Rule()
            self.rule.setName(name)
            self.ruleSet.addRule(self.rule)
        self.updateRuleList()
        self.changeCurrentRule(len(self.ruleSet.ruleList) - 1)
        self.ruleListWidget.setCurrentRow(len(self.ruleSet.ruleList) - 1)

    def changeCurrentRule(self, idx):
        self.rule.fromElementList(self.scene.items())
        self.rule = self.ruleSet.getRule(idx)
        self.loadRuleToScene()

    def removeRule(self):
        self.ruleSet.deleteRule(self.ruleListWidget.currentRow())
        self.updateRuleList()

    def loadRuleToScene(self):
        self.scene.clear()
        for ruleElem in self.rule.elements:
            for el in self.elementSet.elementList:
                if el.elementName == ruleElem['elementName']:
                    item = MetaElement(el.leftConnectorType,
                                       el.rightConnectorType,
                                       el.elementName,
                                       el.color)
                    item.addProperties(el.properties)
                    self.scene.addItem(item)
                    item.setPos(QPoint(ruleElem['x'], ruleElem['y']))

        for ruleConn in self.rule.connections:
            el1 = self.scene.items()[ruleConn['p1']]
            el2 = self.scene.items()[ruleConn['p2']]
            name = ruleConn.get('name', '')
            conn = self.scene.addConnection(el2, el1, name)
            conn.p1Properties = ruleConn['p1Properties']
            conn.p2Properties = ruleConn['p2Properties']
            self.scene.addItem(conn)

    ###########################################################################
    def chooseElementsFile(self):
        ret = QFileDialog.getOpenFileName(filter='*.elms')
        self.currentElementFile = ret[0]
        self.ruleSet.setElementsFileName(self.currentElementFile)
        self.fileName.setText(os.path.basename(self.currentElementFile))

    ###########################################################################
    def loadElementsFile(self):
        if self.currentElementFile is not None:
            file = open(self.currentElementFile, 'r')

            st = '\n'.join(file.readlines())

            self.elementSet.fromString(st)
            file.close()
            self.layoutToolBox()

    ###########################################################################
    def updateElementListView(self):
        self.elemListWidget.clear()

        for item in self.elementSet.elementList:
            self.elemListWidget.addItem(item.elementName)

    ###########################################################################
    def testRule(self):
        self.rule.fromElementList(self.scene.items())
        print(self.ruleSet.toString())
예제 #54
0
    def __init__(self, parent=None):
        super(RuleEditor, self).__init__(parent)

        self.currentElementFile = None
        self.currentRuleFile = None
        self.sceneElementList = []
        self.connections = []
        self.elementSet = ElementSet()
        self.rule = Rule()
        self.ruleSet = RuleSet()

        # Frames
        self.gBox1 = QFrame()
        self.gBox1.setMaximumSize(QSize(250, 600))

        self.scene = ChainScene(-200, -200, 400, 400)
        self.scene.itemInserted.connect(self.itemInserted)

        # Graphics view
        self.graphicsView = QGraphicsView(self.scene)
        self.graphicsView.setMinimumSize(QSize(800, 600))

        #Toolbox
        self.createToolBox()
        self.loadToolBox()

        # Left menu
        self.leftLayout = QVBoxLayout(self.gBox1)
        self.leftInnerLayout = QHBoxLayout()
        self.leftInnerLayout2 = QHBoxLayout()

        self.label1 = QLabel("Elements file:")
        self.fileName = QLabel("--")
        self.chooseFile = QPushButton("Choose file")
        self.loadFile = QPushButton("Load")
        self.ruleListWidget = QListWidget()
        self.addRule = QPushButton("Add rule")
        self.deleteRule = QPushButton("Delete")

        self.leftLayout.addWidget(self.label1)
        self.leftLayout.addWidget(self.fileName)
        self.leftInnerLayout.addWidget(self.chooseFile)
        self.leftInnerLayout.addWidget(self.loadFile)
        self.leftLayout.addLayout(self.leftInnerLayout)
        self.leftLayout.addWidget(self.ruleListWidget)
        self.leftInnerLayout2.addWidget(self.addRule)
        self.leftInnerLayout2.addWidget(self.deleteRule)
        self.leftLayout.addLayout(self.leftInnerLayout2)
        self.leftLayout.addWidget(self.toolBox)

        self.chooseFile.clicked.connect(self.chooseElementsFile)
        self.loadFile.clicked.connect(self.loadElementsFile)
        self.addRule.clicked.connect(self.newRule)
        self.deleteRule.clicked.connect(self.removeRule)
        self.ruleListWidget.currentRowChanged[int].connect(self.changeCurrentRule)


        # Layout
        self.centralWidget = QWidget()
        self.centralLayout = QHBoxLayout()
        self.centralLayout.addWidget(self.gBox1)
        self.centralLayout.addWidget(self.graphicsView)
        self.centralWidget.setLayout(self.centralLayout)

        self.setCentralWidget(self.centralWidget)

        self.resize(1200, 600)

        self.setWindowTitle("WordChain : Rule editor")

        # ToolBars
        self.fileToolBar = self.addToolBar("File")

        self.saveAction = QAction(QIcon(':/images/save.png'), "Save to &File", self,
                                  shortcut="Ctrl+S", triggered=self.saveToFile)
        self.openAction = QAction(QIcon(':/images/open.png'), "Open &File", self,
                                  shortcut="Ctrl+O", triggered=self.loadFromFile)
        self.fileToolBar.addAction(self.saveAction)
        self.fileToolBar.addAction(self.openAction)

        self.pointerToolbar = self.addToolBar("Pointer type")

        pointerButton = QToolButton()
        pointerButton.setCheckable(True)
        pointerButton.setChecked(True)
        pointerButton.setIcon(QIcon(":/images/arrow.png"))
        lineButton = QToolButton()
        lineButton.setCheckable(True)
        lineButton.setIcon(QIcon(":/images/line.png"))

        self.pointerTypeGroup = QButtonGroup()
        self.pointerTypeGroup.addButton(pointerButton, ChainScene.MoveItem)
        self.pointerTypeGroup.addButton(lineButton, ChainScene.InsertLine)

        self.pointerTypeGroup.buttonClicked[int].connect(self.pointerGroupClicked)

        self.pointerToolbar.addWidget(pointerButton)
        self.pointerToolbar.addWidget(lineButton)

        self.testToolbar = self.addToolBar("Model testing")
        testButton = QPushButton()
        testButton.setIcon(QIcon(":/images/play.png"))
        testButton.setText("Test rule")
        testButton.clicked.connect(self.testRule)
        self.testToolbar.addWidget(testButton)
예제 #55
0
 def test_rule_txt_with_one_rule_and_spaces(self):
     txt_rule = "L1 >= 20".splitlines()
     exp_result = [("L1", ">=", 20)]
     self.assertEqual(exp_result, Rule.parse(txt_rule))
예제 #56
0
 def test_rule_txt_with_missing_element(self):
     exceptions = (RuleException)
     with self.assertRaises(exceptions):
         txt_rule = " >=20".splitlines()
         Rule.parse(txt_rule)
예제 #57
0
	def _extractRules_(self, wordRulesFlag, extensiveRulesFlag, phraseRulesFlag, s2t):
		"""
		Return a list of rules extracted from this bead.

		:type wordRulesFlag: bool
		:param wordRulesFlag: True means to add word alignments that are not in the subtree alignments into the ruleList,
							False means no;

		"""
		ruleList = []
		if s2t: glueRuleList = []
		# add in rules with non-terminal Xs
		for key in self.subtreeAlignmentDic:
			if len(self.subtreeAlignmentDic[key]) > 2:		# prune all rules with more than 2 Xs
				continue
			for subaList in util.allCombinations(self.subtreeAlignmentDic[key]):
				#if self.verbose: print >> debug_log, key, ':', subaList
				ruleType, tmpRule = self._extract_(key, subaList, s2t)
				if s2t and ruleType == 'glueRule':
					glueRuleList.append(tmpRule)
				elif ruleType == 'regularRule':
					ruleList.append(tmpRule)

		# add in phrase pairs as rules
		# add in rules with no non-terminal Xs, i.e. rules that are phrase pairs (only phrase pairs that satisfy the tree structures)
		squareList = [suba for lis in self.subtreeAlignment for suba in lis]
		# if phraseRulesFlag, add in all phrase pairs, including the ones that don't satisfy the tree structures
		if phraseRulesFlag:
			squareListWOtags = [suba[:4] for suba in squareList]  # make sure no duplicated squares are added
			tmp = extractMinPhrasePairs(self.wordAlignment)
			squareList += [suba for suba in tmp if suba[:4] not in squareListWOtags]
		#pdb.set_trace()
		for square in squareList: 
			if s2t: lhsSrc, lhsTgt = 'X', square[5]
			else: lhsSrc, lhsTgt = 'X', 'X'
			rhsSrc = range(square[0], square[2])
			rhsTgt = range(square[1], square[3])
			align = []
			if self.legalRule(rhsSrc, rhsTgt):
				tmpRule = Rule(lhsSrc, lhsTgt, rhsSrc, rhsTgt, align, self.wordAlignment, self.srcSnt, self.tgtSnt, square)
				#if self.verbose: print >> debug_log, tmpRule, '\t\t',
				ruleList.append(tmpRule)

		# if not wordRulesFlag, only add in rules that are word alignments (i.e. word pairs) but not corresponding subtree alignments
		#if self.verbose:
			#print >> debug_log, "Bead got wordRulesFlag:", str(wordRulesFlag)
		if not wordRulesFlag and not phraseRulesFlag:
			#if self.verbose: print >> debug_log, "wordRules are:"
			lhsSrc, lhsTgt = 'X', 'X'
			rhsSrc, rhsTgt, align = [], [], []   # here align is for the alignment of Xs, not word alignment, so keep empty 
			for i in xrange(len(self.wordAlignment)):
				for j in xrange(len(self.wordAlignment[0])):
					if self.wordAlignment[i][j]:
						if sum(self.wordAlignment[i]) == 1 and sum([row[j] for row in self.wordAlignment]) == 1:
							rhsSrc, rhsTgt = [i], [j]
							#if self.verbose: print >> debug_log, i, j, self.srcSnt[i].encode('utf-8'), self.tgtSnt[j].encode('utf-8')
							if self.legalRule(rhsSrc, rhsTgt):
								#if self.verbose: print >> debug_log, "legal"
								tmpRule = Rule(lhsSrc, lhsTgt, rhsSrc, rhsTgt, align, self.wordAlignment, self.srcSnt, self.tgtSnt, (i, j, i + 1, j + 1))
								ruleList.append(tmpRule)
						break

		# if extensiveRulesFlag, add in extensive rules which include:
		#	- a rule with the determiner ("a" or "the") removed, e.g. given an existing rule "... ||| a peace agreement [X] ||| ...", 
		#			another rule "... ||| peace agreement [X] ||| ..." is added, iff. "a" is not aligned to any foreign words,
		#			word/X alignments and occurences are kept the same;
		if extensiveRulesFlag:
			tmpRuleList = []
			for rule in ruleList:
				if len(rule.rhsTgt) > 1 and rule.rhsTgt[0] in ["a", "the"]:
						#and 0 not in [align[1] for align in rule.alignment]:
					tmpRule = Rule(None, None, None, None, None, None, None, None, None)
					tmpRule.lhsSrc, tmpRule.lhsTgt, tmpRule.rhsSrc, tmpRule.rhsTgt = rule.lhsSrc, rule.lhsTgt, rule.rhsSrc, rule.rhsTgt[1:]
					#tmpRule.alignment = [(align[0], align[1] - 1) for align in rule.alignment]
					tmpRule.alignment = [(align[0], align[1] - 1) for align in rule.alignment if align[1] > 0]
					#print tmpRule.alignment
					tmpRule.square = rule.square[0], rule.square[1] + 1, rule.square[2], rule.square[3]
					tmpRuleList.append(tmpRule)
			ruleList.extend(tmpRuleList)

		if s2t: return ruleList, glueRuleList
		else: return ruleList, None
예제 #58
0
 def __init__(self,items,msg='%s'):
   Rule.__init__(self, '<disp:%s>', [items])
   self.msg = msg
예제 #59
0
 def test_rule_txt_as_empty_string(self):
     exceptions = (RuleException)
     self.assertEqual([], Rule.parse(""))
예제 #60
0
 def __init__(self,name):
   Rule.__init__(self, name, [name])