def test_least_revision(self): eprint(">> ACEDIA.least_revision(rule, state_1, state_2)") for i in range(self.__nb_unit_test): variables, domains = self.random_system() state_1 = self.random_state(variables, domains) state_2 = self.random_state(variables, domains) # not matching #-------------- rule = self.random_rule(variables, domains) while rule.matches(state_1): rule = self.random_rule(variables, domains) self.assertRaises(ValueError, ACEDIA.least_revision, rule, state_1, state_2) # matching #-------------- rule = self.random_rule(variables, domains) while not rule.matches(state_1): rule = self.random_rule(variables, domains) head_var = rule.get_head_variable() target_val = state_2[rule.get_head_variable()] # Consistent head_value = Continuum() while not head_value.includes(target_val): head_value = Continuum.random( domains[head_var].get_min_value(), domains[head_var].get_max_value()) rule.set_head_value(head_value) self.assertRaises(ValueError, ACEDIA.least_revision, rule, state_1, state_2) # Empty set head rule.set_head_value(Continuum()) LR = ACEDIA.least_revision(rule, state_1, state_2) lg = rule.copy() lg.set_head_value(Continuum(target_val, target_val, True, True)) self.assertTrue(lg in LR) nb_valid_revision = 1 for var, val in rule.get_body(): state_value = state_1[var] # min rev ls = rule.copy() new_val = val.copy() new_val.set_lower_bound(state_value, False) if not new_val.is_empty(): ls.set_condition(var, new_val) self.assertTrue(ls in LR) nb_valid_revision += 1 # max rev ls = rule.copy() new_val = val.copy() new_val.set_upper_bound(state_value, False) if not new_val.is_empty(): ls.set_condition(var, new_val) self.assertTrue(ls in LR) nb_valid_revision += 1 self.assertEqual(len(LR), nb_valid_revision) #eprint(nb_valid_revision) # usual head head_value = Continuum.random(domains[head_var].get_min_value(), domains[head_var].get_max_value()) while head_value.includes(target_val): head_value = Continuum.random( domains[head_var].get_min_value(), domains[head_var].get_max_value()) rule.set_head_value(head_value) LR = ACEDIA.least_revision(rule, state_1, state_2) lg = rule.copy() head_value = lg.get_head_value() if target_val <= head_value.get_min_value(): head_value.set_lower_bound(target_val, True) else: head_value.set_upper_bound(target_val, True) lg.set_head_value(head_value) self.assertTrue(lg in LR) nb_valid_revision = 1 for var, val in rule.get_body(): state_value = state_1[var] # min rev ls = rule.copy() new_val = val.copy() new_val.set_lower_bound(state_value, False) if not new_val.is_empty(): ls.set_condition(var, new_val) self.assertTrue(ls in LR) nb_valid_revision += 1 # max rev ls = rule.copy() new_val = val.copy() new_val.set_upper_bound(state_value, False) if not new_val.is_empty(): ls.set_condition(var, new_val) self.assertTrue(ls in LR) nb_valid_revision += 1 self.assertEqual(len(LR), nb_valid_revision)
def test_intersects(self): eprint(">> Continuum.intersects(self, continuum)") for i in range(self.__nb_unit_test): c = Continuum.random(self.__min_value, self.__max_value) c_ = Continuum() # emptyset self.assertFalse(c.intersects(c_)) self.assertFalse(c_.intersects(c)) self.assertFalse(c_.intersects(c_)) # stricly before c = Continuum.random(self.__min_value, self.__max_value) c_ = Continuum.random(c.get_min_value() - 100, c.get_min_value()) self.assertFalse(c.intersects(c_)) self.assertFalse(c_.intersects(c)) # touching on lower bound c = Continuum.random(self.__min_value, self.__max_value) c_ = Continuum.random(c.get_min_value() - 100, c.get_min_value()) c_.set_upper_bound(c.get_min_value(), True) self.assertEqual(c.intersects(c_), c.min_included()) self.assertEqual(c_.intersects(c), c.min_included()) c_.set_upper_bound(c.get_min_value(), False) self.assertFalse(c.intersects(c_)) self.assertFalse(c_.intersects(c)) # strictly after c = Continuum.random(self.__min_value, self.__max_value) c_ = Continuum.random(c.get_max_value(), c.get_max_value() + 100) self.assertFalse(c.intersects(c_)) self.assertFalse(c_.intersects(c)) # touching on lower bound c = Continuum.random(self.__min_value, self.__max_value) c_ = Continuum.random(c.get_max_value(), c.get_max_value() + 100) c_.set_lower_bound(c.get_max_value(), True) self.assertEqual(c.intersects(c_), c.max_included()) self.assertEqual(c_.intersects(c), c.max_included()) c_.set_lower_bound(c.get_max_value(), False) self.assertFalse(c.intersects(c_)) self.assertFalse(c_.intersects(c)) # same (not empty) c = Continuum.random(self.__min_value, self.__max_value) while c.is_empty(): c = Continuum.random(self.__min_value, self.__max_value) self.assertTrue(c.includes(c)) # smaller c_ = Continuum.random(c.get_min_value(), c.get_max_value()) while c_.get_min_value() == c.get_min_value() and c_.get_max_value( ) == c.get_max_value(): c_ = Continuum.random(c.get_min_value(), c.get_max_value()) self.assertTrue(c.intersects(c_)) self.assertTrue(c_.intersects(c)) # bigger c_ = Continuum.random(c.get_min_value() - 100, c.get_max_value() + 100) while c_.get_min_value() >= c.get_min_value() or c_.get_max_value( ) <= c.get_max_value(): c_ = Continuum.random(c.get_min_value() - 100, c.get_max_value() + 100) #eprint(c.to_string()) #eprint(c_.to_string()) self.assertTrue(c.intersects(c_)) self.assertTrue(c_.intersects(c))
def test_set_lower_bound(self): eprint(">> Continuum.set_lower_bound(self, value, included)") for i in range(self.__nb_unit_test): # Empty set c = Continuum() self.assertRaises(TypeError, c.set_lower_bound, "string", True) self.assertRaises(TypeError, c.set_lower_bound, "string", False) self.assertRaises(TypeError, c.set_lower_bound, 0.5, 10) value = random.uniform(self.__min_value, self.__max_value) # extend with exclusion gives empty set, mistake expected from user # or both min and max will be changed and constructor must be used self.assertRaises(ValueError, c.set_lower_bound, value, False) c.set_lower_bound(value, True) # Empty set to one value interval self.assertEqual(c, Continuum(value, value, True, True)) # Regular continuum # over max value c = Continuum.random(self.__min_value, self.__max_value) value = random.uniform(c.get_max_value(), self.__max_value) while value == c.get_max_value(): value = random.uniform(c.get_max_value(), self.__max_value) self.assertRaises(ValueError, c.set_lower_bound, value, True) self.assertRaises(ValueError, c.set_lower_bound, value, False) # on max value c = Continuum.random(self.__min_value, self.__max_value) c_old = c.copy() value = c.get_max_value() if not c.max_included() or not c.min_included(): c.set_lower_bound(value, False) self.assertEqual(c, Continuum()) # continuum reduced to empty set else: c.set_lower_bound(value, True) self.assertEqual(c.get_min_value(), value) self.assertEqual(c.min_included(), True) self.assertEqual(c.get_min_value(), c.get_max_value()) self.assertEqual(c.get_max_value(), c_old.get_max_value()) self.assertEqual(c.max_included(), c_old.max_included()) # other valid value c = Continuum.random(self.__min_value, self.__max_value) c_old = c.copy() value = random.uniform(self.__min_value, c.get_max_value()) while value == c.get_max_value(): value = random.uniform(self.__min_value, c.get_max_value()) c.set_lower_bound(value, True) self.assertEqual(c.get_min_value(), value) self.assertEqual(c.min_included(), True) c = Continuum.random(self.__min_value, self.__max_value) c_old = c.copy() value = random.uniform(self.__min_value, c.get_max_value()) while value == c.get_max_value(): value = random.uniform(self.__min_value, c.get_max_value()) c.set_lower_bound(value, False) self.assertEqual(c.get_min_value(), value) self.assertEqual(c.min_included(), False)
def test_includes(self): eprint(">> Continuum.includes(self, element)") for i in range(self.__nb_unit_test): # bad argument type c = Continuum.random(self.__min_value, self.__max_value) self.assertRaises(TypeError, c.includes, "test") # float argument #---------------- # empty set includes nothing c = Continuum() value = random.uniform(self.__min_value, self.__max_value) self.assertFalse(c.includes(value)) c = Continuum.random(self.__min_value, self.__max_value) # Before min value = c.get_min_value() while value == c.get_min_value(): value = random.uniform(c.get_min_value() - 100.0, c.get_min_value()) self.assertFalse(c.includes(value)) # on min bound self.assertEqual(c.includes(c.get_min_value()), c.min_included()) # Inside value = c.get_min_value() while value == c.get_min_value() or value == c.get_max_value(): value = random.uniform(c.get_min_value(), c.get_max_value()) self.assertTrue(c.includes(value)) # on max bound self.assertEqual(c.includes(c.get_max_value()), c.max_included()) # after max bound value = c.get_max_value() while value == c.get_max_value(): value = random.uniform(c.get_max_value(), c.get_max_value() + 100.0) self.assertFalse(c.includes(value)) # int argument #-------------- # empty set includes nothing c = Continuum() value = random.randint(int(self.__min_value), int(self.__max_value)) self.assertFalse(c.includes(value)) c = Continuum.random(self.__min_value, self.__max_value) while int(c.get_max_value()) - int(c.get_min_value()) <= 1: min = random.uniform(self.__min_value, self.__max_value) max = random.uniform(min, self.__max_value) c = Continuum.random(min, max) #eprint(c.to_string()) # Before min value = random.randint(int(c.get_min_value() - 100), int(c.get_min_value()) - 1) self.assertFalse(c.includes(value)) # on min bound self.assertEqual(c.includes(c.get_min_value()), c.min_included()) # Inside value = random.randint( int(c.get_min_value()) + 1, int(c.get_max_value()) - 1) #eprint(value) self.assertTrue(c.includes(value)) # on max bound self.assertEqual(c.includes(c.get_max_value()), c.max_included()) # after max bound value = random.randint( int(c.get_max_value()) + 1, int(c.get_max_value() + 100)) self.assertFalse(c.includes(value)) # continuum argument #-------------------- # 0) c is empty set c = Continuum() c_ = Continuum() self.assertTrue(c.includes(c_)) # empty set VS empty set c_ = Continuum.random(self.__min_value, self.__max_value) while c_.is_empty(): c_ = Continuum.random(self.__min_value, self.__max_value) self.assertFalse(c.includes(c_)) # empty set VS non empty # 1) c is non empty c = Continuum.random(self.__min_value, self.__max_value) self.assertTrue(c.includes(Continuum())) # non empty VS empty set self.assertTrue(c.includes(c)) # includes itself # 1.1) Lower bound over c_ = Continuum.random(c.get_min_value(), self.__max_value) while c_.is_empty(): c_ = Continuum.random(c.get_min_value(), self.__max_value) value = c.get_min_value() while value == c.get_min_value(): value = random.uniform(c.get_min_value() - 100, c.get_min_value()) c_.set_lower_bound(value, random.choice([True, False])) self.assertFalse(c.includes(c_)) # 1.2) on min bound c_ = Continuum.random(c.get_min_value(), self.__max_value) while c_.is_empty(): c_ = Continuum.random(c.get_min_value(), self.__max_value) c_.set_lower_bound(c.get_min_value(), random.choice([True, False])) if not c.min_included() and c_.min_included(): # one value over self.assertFalse(c.includes(c_)) # 1.3) upper bound over c_ = Continuum.random(self.__min_value, c.get_max_value()) while c_.is_empty(): c_ = Continuum.random(self.__min_value, c.get_max_value()) value = c.get_max_value() while value == c.get_max_value(): value = random.uniform(c.get_max_value(), c.get_max_value() + 100) c_.set_upper_bound(value, random.choice([True, False])) self.assertFalse(c.includes(c_)) # 1.4) on upper bound c_ = Continuum.random(self.__min_value, c.get_max_value()) while c_.is_empty(): c_ = Continuum.random(self.__min_value, c.get_max_value()) c_.set_upper_bound(c.get_max_value(), random.choice([True, False])) if not c.max_included() and c_.max_included(): # one value over self.assertFalse(c.includes(c_)) # 1.5) inside min = c.get_min_value() while min == c.get_min_value(): min = random.uniform(c.get_min_value(), c.get_max_value()) max = c.get_max_value() while max == c.get_max_value(): max = random.uniform(min, c.get_max_value()) c_ = Continuum(min, max, random.choice([True, False]), random.choice([True, False])) self.assertTrue(c.includes(c_)) self.assertFalse(c_.includes(c))