def rule_match_and(): greater_rule = Rule(RuleType.GREATER, 3) less_rule = Rule(RuleType.LESS, 10) and_rule = Rule(RuleType.AND, [greater_rule, less_rule]) # between 3 & 10 test("Rule AND 1", and_rule.match(7), True) test("Rule AND 2", and_rule.match(0), False) test("Rule AND 3", and_rule.match(15), False)
def rule_match_xor(): contain_rule = Rule(RuleType.CONTAIN, 'h') xor_rule = Rule(RuleType.XOR, [contain_rule, Rule(RuleType.CONTAIN, 'o')]) test("Rule XOR 1", xor_rule.match("world"), True) test("Rule XOR 2", xor_rule.match("help"), True) test("Rule XOR 3", xor_rule.match("hello"), False) test("Rule XOR 4", xor_rule.match("animal"), False)
def rule_eq(): rule1 = Rule(RuleType.GREATER, 5) rule2 = Rule(RuleType.GREATER, 5) rule3 = Rule(RuleType.GREATER, 3) rule4 = Rule(RuleType.LESS, 5) test("Rule Equality", rule1 == rule2, True) test("Rule Inequality 1", rule1 == rule3, False) test("Rule Inequality 2", rule1 == rule4, False)
def property_eq(): rule = Rule(RuleType.GREATER, 5) prop1 = Property("count", rule) prop2 = Property("count", rule) prop3 = Property("name", rule) prop4 = Property("count", Rule(RuleType.LESS, 10)) test("Property Equality", prop1 == prop2, True) test("Property Inequality 1", prop1 == prop3, False) test("Property Inequality 2", prop1 == prop4, False)
def webster_eq(): # define rules to use greater_rule = Rule(RuleType.GREATER, 3) less_rule = Rule(RuleType.LESS, 10) and_rule = Rule(RuleType.AND, [greater_rule, less_rule]) contain_rule = Rule(RuleType.CONTAIN, 'h') xor_rule = Rule(RuleType.XOR, [contain_rule, Rule(RuleType.CONTAIN, 'o')]) # define properties to use name_prop = Property("Name", xor_rule) valu_prop = Property("Value", and_rule) # define a definition for testing definition = Definition("TestyDef", [name_prop, valu_prop]) # define a thing attrs = {"Name": "Hello", "Value": 3} thing = Thing("TestyThingy", attributes=attrs, definition="TestyDef") # defines Webster webster1 = Webster(brain=[thing], dictionary=[definition]) webster2 = Webster(brain=[thing], dictionary=[definition]) webster3 = Webster(dictionary=[definition]) webster4 = Webster(brain=[thing]) test("Webster Equality", webster1 == webster2, True) test("Webster Inequality 1", webster1 == webster3, False) test("Webster Inequality 2", webster1 == webster4, False)
def init_webster(): # define rules to use greater_rule = Rule(RuleType.GREATER, 3) less_rule = Rule(RuleType.LESS, 10) and_rule = Rule(RuleType.AND, [greater_rule, less_rule]) contain_rule = Rule(RuleType.CONTAIN, 'h') xor_rule = Rule(RuleType.XOR, [contain_rule, Rule(RuleType.CONTAIN, 'o')]) # define properties to use name_prop = Property("Name", xor_rule) valu_prop = Property("Value", and_rule) # define a definition for testing definition = Definition("TestyDef", [name_prop, valu_prop]) # define a thing attrs = {"Name": "Hello", "Value": 3} thing = Thing("TestyThingy", attributes=attrs, definition="TestyDef") # defines Webster webster = Webster(brain=[thing], dictionary=[definition]) # tests for the correct values dict_test = webster.dictionary["TestyDef"] test("Webster Construction Dictionary", dict_test, definition) test("Webster Construction Brain", webster.brain["TestyThingy"], thing)
def webster_has_def(): # define rules to use greater_rule = Rule(RuleType.GREATER, 3) less_rule = Rule(RuleType.LESS, 10) and_rule = Rule(RuleType.AND, [greater_rule, less_rule]) contain_rule = Rule(RuleType.CONTAIN, 'h') xor_rule = Rule(RuleType.XOR, [contain_rule, Rule(RuleType.CONTAIN, 'o')]) # define properties to use name_prop = Property("Name", xor_rule) valu_prop = Property("Value", and_rule) # define a definition for testing definition = Definition("TestyDef", [name_prop, valu_prop]) # define a thing attrs1 = {"Name": "Hello", "Value": 5} attrs2 = {"Name": "hello", "Value": 1} thing1 = Thing("Thing 1", attributes=attrs1, definition="TestyDef") thing2 = Thing("Thing 2", attributes=attrs2) thing3 = Thing("Thing Red", attributes=attrs2, definition="TestyDef") thing4 = Thing("Thing Blue", attributes=attrs1) brain = [thing1, thing2, thing3, thing4] # runs test webster = Webster(brain=brain, dictionary=[definition]) result1 = webster.has_definition("Thing 1", "TestyDef") result2 = webster.has_definition("Thing 2", "TestyDef") result3 = webster.has_definition("Thing Red", "TestyDef") result4 = webster.has_definition("Thing Blue", "TestyDef") test("Webster Has Definition 1", result1, True) test("Webster Has Definition 2", result2, False) test("Webster Has Definition 3", result3, True) test("Webster Has Definition 4", result4, True)
def webster_set_rule(): # define rules to use greater_rule = Rule(RuleType.GREATER, 3) less_rule = Rule(RuleType.LESS, 10) and_rule = Rule(RuleType.AND, [greater_rule, less_rule]) contain_rule = Rule(RuleType.CONTAIN, 'h') xor_rule = Rule(RuleType.XOR, [contain_rule, Rule(RuleType.CONTAIN, 'o')]) new_rule = Rule(RuleType.IS, "Hello") # define properties to use name_prop = Property("Name", xor_rule) valu_prop = Property("Value", and_rule) # run test webster = Webster() webster.add_definition("TestyDef", [name_prop, valu_prop]) webster.set_property_rule("TestyDef", "Name", new_rule) test("Webster Set Rule", webster.get_rule("TestyDef", "Name"), new_rule)
def webster_get_prop(): # define rules to use greater_rule = Rule(RuleType.GREATER, 3) less_rule = Rule(RuleType.LESS, 10) and_rule = Rule(RuleType.AND, [greater_rule, less_rule]) contain_rule = Rule(RuleType.CONTAIN, 'h') xor_rule = Rule(RuleType.XOR, [contain_rule, Rule(RuleType.CONTAIN, 'o')]) # define properties to use name_prop = Property("Name", xor_rule) valu_prop = Property("Value", and_rule) # run test webster = Webster() webster.add_definition("TestyDef", [name_prop, valu_prop]) result = webster.get_property("TestyDef", "Name") test("Webster Get Property", result, name_prop)
def webster_has_prop(): # define rules to use greater_rule = Rule(RuleType.GREATER, 3) less_rule = Rule(RuleType.LESS, 10) and_rule = Rule(RuleType.AND, [greater_rule, less_rule]) contain_rule = Rule(RuleType.CONTAIN, 'h') xor_rule = Rule(RuleType.XOR, [contain_rule, Rule(RuleType.CONTAIN, 'o')]) # define properties to use name_prop = Property("Name", xor_rule) valu_prop = Property("Value", and_rule) # run test webster = Webster() webster.add_definition("TestyDef", [name_prop, valu_prop]) result1 = webster.has_property("TestyDef", "Name") result2 = webster.has_property("TestyDef", "Count") test("Webster Has Property 1", result1, True) test("Webster Has Property 2", result2, False)
def definition_eq(): # define rules to use greater_rule = Rule(RuleType.GREATER, 3) less_rule = Rule(RuleType.LESS, 10) and_rule = Rule(RuleType.AND, [greater_rule, less_rule]) contain_rule = Rule(RuleType.CONTAIN, 'h') xor_rule = Rule(RuleType.XOR, [contain_rule, Rule(RuleType.CONTAIN, 'o')]) # define properties to use name_prop = Property("Name", xor_rule) valu_prop = Property("Value", and_rule) # run the test def1 = Definition("TestyDef", [name_prop, valu_prop]) def2 = Definition("TestyDef", [name_prop, valu_prop]) def3 = Definition("TestDef", [name_prop, valu_prop]) def4 = Definition("TestyDef", [name_prop]) test("Definition Equality", def1 == def2, True) test("Definition Inequality 1", def1 == def3, False) test("Definition Inequality 2", def1 == def4, False)
def init_definition(): # define rules to use greater_rule = Rule(RuleType.GREATER, 3) less_rule = Rule(RuleType.LESS, 10) and_rule = Rule(RuleType.AND, [greater_rule, less_rule]) contain_rule = Rule(RuleType.CONTAIN, 'h') xor_rule = Rule(RuleType.XOR, [contain_rule, Rule(RuleType.CONTAIN, 'o')]) # define properties to use name_prop = Property("Name", xor_rule) valu_prop = Property("Value", and_rule) # define a definition for testing definition = Definition("TestyDef", [name_prop, valu_prop]) # the expected value of definition.prop exp_prop = {"Name": name_prop, "Value": valu_prop} test("Definition Construction Name", definition.name, "TestyDef") test("Definition Construction Properties", definition.props, exp_prop)
def webster_get_definition(): # define rules to use greater_rule = Rule(RuleType.GREATER, 3) less_rule = Rule(RuleType.LESS, 10) and_rule = Rule(RuleType.AND, [greater_rule, less_rule]) contain_rule = Rule(RuleType.CONTAIN, 'h') xor_rule = Rule(RuleType.XOR, [contain_rule, Rule(RuleType.CONTAIN, 'o')]) # define properties to use name_prop = Property("Name", xor_rule) valu_prop = Property("Value", and_rule) # define a definition for testing definition = Definition("TestyDef", [name_prop, valu_prop]) # defines Webster webster = Webster(dictionary=[definition]) # tests to get the definition test_result = webster.get_definition("TestyDef") test("Webster Get Definition", test_result, definition)
def definition_match(): # define rules to use greater_rule = Rule(RuleType.GREATER, 3) less_rule = Rule(RuleType.LESS, 10) and_rule = Rule(RuleType.AND, [greater_rule, less_rule]) contain_rule = Rule(RuleType.CONTAIN, 'h') xor_rule = Rule(RuleType.XOR, [contain_rule, Rule(RuleType.CONTAIN, 'o')]) # define properties to use name_prop = Property("Name", xor_rule) valu_prop = Property("Value", and_rule) # define a definition for testing definition = Definition("TestyDef", [name_prop, valu_prop]) # define a thing for testing thing = Thing("TestyThingy", attributes={"Name": "Hello", "Value": 4}) test("Definition Match 1", definition.match(thing), True) thing = Thing("TestyThingy", attributes={"Name": "hello", "Value": 4}) test("Definition Match 2", definition.match(thing), False) thing = Thing("TestyThingy", attributes={"Name": "Hello", "Value": 3}) test("Definition Match 3", definition.match(thing), False)
def property_match(): rule = Rule(RuleType.GREATER, 5) prop = Property("count", rule) test("Property Match 1", prop.match(7), True) test("Property Match 1", prop.match(5), False)
def rule_match_contain(): contain_rule = Rule(RuleType.CONTAIN, 'h') # contains letter h test("Rule CONTAIN 1", contain_rule.match("hello"), True) test("Rule CONTAIN 2", contain_rule.match("world"), False)
def rule_match_or(): greater_rule = Rule(RuleType.GREATER, 3) or_rule = Rule(RuleType.OR, [Rule(RuleType.IS, 3), greater_rule]) # >= 3 test("Rule OR 1", or_rule.match(3), True) test("Rule OR 2", or_rule.match(5), True) test("Rule OR 3", or_rule.match(-3), False)
def rule_match_is(): test("Rule IS 1", Rule(RuleType.IS, "Hello").match("Hello"), True) test("Rule IS 2", Rule(RuleType.IS, "Hello").match("World"), False)
def rule_match_not(): not_rule = Rule(RuleType.NOT, Rule(RuleType.IS, 6)) # not 6 test("Rule NOT 1", not_rule.match(3), True) test("Rule NOT 2", not_rule.match(6), False)
def init_property(): rule = Rule(RuleType.GREATER, 5) prop = Property("count", rule) test("Property Construction Name", prop.name, "count") test("Property Construction Rule", prop.rule, rule)
def rule_match_less(): less_rule = Rule(RuleType.LESS, 10) # less than 10 test("Rule LESS 1", less_rule.match(1), True) test("Rule LESS 2", less_rule.match(12), False)
def init_rule(): rule = Rule(RuleType.GREATER, 5) test("Rule Construction Type", rule.rule_type, RuleType.GREATER) test("Rule Construction Value", rule.value, 5)
def rule_match_greater(): greater_rule = Rule(RuleType.GREATER, 3) # greater than 3 test("Rule GREATER 1", greater_rule.match(5), True) test("Rule GREATER 2", Rule(RuleType.GREATER, 3).match(1), False)