def build_comparisons(self): ''' Generate comparisons from formulas ''' # Generate direct comparisons comp_dict = {} for idx1, formula1 in enumerate(self._formula_list): comp_dict[idx1] = {} for idx2, formula2 in enumerate(self._formula_list): tmp_set = set() if idx1 != idx2: for rule in self._rule_list: # Check if formula1 dominates formula2 if rule.dominates(formula1, formula2): # Screen for intersections att = \ rule.get_preference()\ .get_preference_attribute() if not intersect(formula1[att], formula2[att]): comp = \ build_comparison(formula1, formula2, rule) tmp_set.add(comp) comp_dict[idx1][idx2] = tmp_set self._build_transitive_comparisons(comp_dict)
def split_rule_over_condition_attribute(rule, att, interval): """ Split rule if rule intersects interval over any attribute on conditions """ new_rules_list = [] # Get attribute intervals if not rule.get_condition(): return new_rules_list if att not in rule.get_condition().get_condition_dict().keys(): return new_rules_list rule_int = rule.get_condition().get_condition_dict()[att] # Verify if intervals intersect if (rule_int != interval) and (intersect(rule_int, interval)): # Split intervals new_intervals_list = split_interval(rule_int, interval) # Add new rules with new intervals for new_interval in new_intervals_list: new_rule = rule.copy() new_rule.get_condition().get_condition_dict()[att] = (new_interval) new_rules_list.append(new_rule) return new_rules_list
def is_dict_satisfied_by(condition_dict, record): ''' Check if a record satisfies a condition dictionary ''' for att in condition_dict: interval = condition_dict[att] if att not in record or \ not intersect(interval, record[att]): return False return True
def is_compatible(self, other): ''' Check if a condition is compatible with another condition. A condition is compatible with another condition if their interval overlaps for the same attributes ''' other_cond_dict = other.get_condition_dict() for key in self._condition_dict: if key in other_cond_dict \ and not intersect(self._condition_dict[key], other_cond_dict[key]): return False return True
def is_goal_record(curren_record, goal_record): ''' Check if first record reaches goal record A record reaches a goal if its attributes are inside or equal of correspondent goal attributes Indifferent attributes of goal are ignored ''' for att in curren_record: if att not in goal_record \ or intersect(goal_record[att], curren_record[att]): continue else: return False return True
def _is_record_valid_by_formula(formula, record): ''' Return True if the record satisfies the formula, else return False ''' # For each formula proposition attribute for att in formula: # Take the interval of attribute in the formula interval = formula[att] # Check if attribute does not exists in record or # if record attribute value does not match with correspondent # formula interval if att not in record or \ not intersect(interval, record[att]): return False # Returns true if all attributes are ok return True
def change_record(self, record): ''' Generate a worst record when it is possible, when it is not then return None ''' cond = self._condition pref = self._preference pref_att = pref.get_preference_attribute() best_interval = pref.get_best_interval() if cond is None or cond.is_satisfied_by(record): if pref_att in record and \ intersect(best_interval, record[pref_att]): new_record = record.copy() new_record[pref_att] = pref.get_worst_interval() for att in pref.get_indifferent_set(): if att in new_record: del new_record[att] return new_record return None
def split_rule_preferred(rule, att, interval): """ Split rule if fixed_interval interval overlaps preferred interval """ new_rules_list = [] if rule.get_preference().get_preference_attribute() != att: return new_rules_list # Get preferred interval rule_int = rule.get_preference().get_best_interval() # Check if intervals intersect if (rule_int != interval) and (intersect(rule_int, interval)): # Split intervals new_intervals_list = split_interval(rule_int, interval) # Add new rules with new intervals for new_interval in new_intervals_list: new_rule = rule.copy() new_rule.get_preference().set_best_interval(new_interval) new_rules_list.append(new_rule) return new_rules_list
# Equality operator EQUAL_OP_LIST = ['=', '<>'] # Interval operator INTERV_OP_LIST = ['<', '<='] # Generate equality comparisons for VAL in VAL_LIST: for OP in EQUAL_OP_LIST: INTERV = (VAL, OP, OP, VAL) INTERV_LIST.append(INTERV) # Generate intervals comparisons for VAL1 in VAL_LIST: for OP1 in INTERV_OP_LIST: for OP2 in INTERV_OP_LIST: for VAL2 in VAL_LIST: if VAL1 < VAL2: INTERV = (VAL1, OP1, OP2, VAL2) INTERV_LIST.append(INTERV) # Generate intervals having infinity limits for VAL in VAL_LIST: for OP in INTERV_OP_LIST: INTERV = (VAL, OP, '<=', float('inf')) INTERV_LIST.append(INTERV) INTERV = (float('-inf'), '<=', OP, VAL) INTERV_LIST.append(INTERV) INTERV_LIST.append((float('-inf'), '<=', '<=', float('inf'))) # Print intersection (overlap) for each pair of intervals for INTERV1 in INTERV_LIST: for INTERV2 in INTERV_LIST: print(get_str_predicate('A', INTERV1), get_str_predicate('A', INTERV2), intersect(INTERV1, INTERV2))
def is_worst_satisfied_by(self, record): ''' Check if a record satisfies the worst interval ''' return self._attribute in record and \ intersect(self._worst_interval, record[self._attribute])