This repository has been archived by the owner on Oct 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ruleset_test.py
132 lines (116 loc) · 7.21 KB
/
ruleset_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Name: Mason Wilde
# KUID: 2645990
# Course: EECS 690
# Professor: Dr Jerzy Gryzmala-Busse
# Semester: Fall 2017
# Project: AQ Rule Inducer
# File: ruleset_test.py
# Date Modified: 2017-11-19
"""A testing suite for ruleset"""
import unittest
import ruleset
from ruleset import Rule, Condition, Decision, Ruleset
from lers_reader import Lers_Reader
reader = Lers_Reader("datasets/simple.txt")
mock_dataset = reader.read_improved()
class TestRuleset(unittest.TestCase):
def test_get_available_attribute_values(self):
rule = Rule(conditions=[Condition(attribute="Temperature", value="very_high")], decision=Decision(label="Flue", value="no"))
result = ruleset.get_available_attribute_values(mock_dataset, rule)
self.assertEqual(result, {"Temperature":["high", "normal"]})
def test_expand_ruleset_simple(self):
result = ruleset.expand_ruleset(
[Rule(conditions=[],
decision=Decision(label="Flue", value="yes"))],
"Temp", ["high", "normal"])
expected_ruleset = [Rule(conditions=[Condition(attribute="Temp", value="high")],
decision=Decision(label="Flue", value="yes")),
Rule(conditions=[Condition(attribute="Temp", value="normal")],
decision=Decision(label="Flue", value="yes"))]
for i in range(len(expected_ruleset)):
for j in range(len(expected_ruleset[i].conditions)):
self.assertEqual(expected_ruleset[i].conditions[j], result[i].conditions[j])
def test_unnegate_rule(self):
rule = Rule(conditions=[Condition(attribute="Temperature", value="very_high"),
Condition(attribute="Headache", value="no")],
decision=Decision(label="Flue", value="no"))
result = ruleset.unnegate_rule(mock_dataset, rule)
expected_ruleset = [Rule(conditions=[Condition(attribute="Temperature", value="high"), Condition(attribute="Headache", value="yes")],
decision=Decision(label="Flue", value="yes")),
Rule(conditions=[Condition(attribute="Temperature", value="normal"), Condition(attribute="Headache", value="yes")],
decision=Decision(label="Flue", value="yes"))]
for i in range(len(expected_ruleset)):
for j in range(len(expected_ruleset[i].conditions)):
self.assertEqual(expected_ruleset[i].conditions[j], result[i].conditions[j])
def test_covered_cases_negated(self):
rule = Rule(conditions=[Condition(attribute="Temperature", value="normal")],
decision=Decision(label="Flue", value="no"))
self.assertEqual(rule.cases_covered(mock_dataset), [0,1,3,4])
def test_covered_cases_unnegated(self):
rule = Rule(conditions=[Condition(attribute="Temperature", value="normal")],
decision=Decision(label="Flue", value="no"))
self.assertEqual(rule.cases_covered(mock_dataset, negated=False), [2,5,6])
def test_rule_is_consistent_negated(self):
rule = Rule(conditions=[Condition(attribute="Temperature", value="normal")],
decision=Decision(label="Flue", value="no"))
self.assertFalse(rule.is_consistent(mock_dataset))
def test_rule_is_consistent_unnegated(self):
rule = Rule(conditions=[Condition(attribute="Temperature", value="normal")],
decision=Decision(label="Flue", value="no"))
self.assertTrue(rule.is_consistent(mock_dataset, negated=False))
def test_ruleset_is_consistent_negated_false(self):
rule1 = Rule(conditions=[Condition(attribute="Temperature", value="normal")],
decision=Decision(label="Flue", value="yes"))
rule2 = Rule(conditions=[Condition(attribute="Temperature", value="high")],
decision=Decision(label="Flue", value="no"))
rule3 = Rule(conditions=[Condition(attribute="Temperature", value="very_high")],
decision=Decision(label="Flue", value="no"))
ruleset = Ruleset(rules=[rule1,rule2,rule3])
self.assertFalse(ruleset.is_consistent(mock_dataset))
def test_ruleset_is_complete_negated_true(self):
rule1 = Rule(conditions=[Condition(attribute="Temperature", value="normal"),
Condition(attribute="Headache", value="no")],
decision=Decision(label="Flue", value="yes"))
rule2 = Rule(conditions=[Condition(attribute="Temperature", value="high"),
Condition(attribute="Temperature", value="very_high")],
decision=Decision(label="Flue", value="no"))
rule3 = Rule(conditions=[Condition(attribute="Headache", value="yes")],
decision=Decision(label="Flue", value="no"))
ruleset = Ruleset(rules=[rule1,rule2,rule3])
self.assertTrue(ruleset.is_complete(mock_dataset))
def test_ruleset_is_consistent_negated_true(self):
rule1 = Rule(conditions=[Condition(attribute="Temperature", value="normal"),
Condition(attribute="Headache", value="no")],
decision=Decision(label="Flue", value="yes"))
rule2 = Rule(conditions=[Condition(attribute="Temperature", value="high"),
Condition(attribute="Temperature", value="very_high")],
decision=Decision(label="Flue", value="no"))
rule3 = Rule(conditions=[Condition(attribute="Headache", value="yes")],
decision=Decision(label="Flue", value="no"))
ruleset = Ruleset(rules=[rule1,rule2,rule3])
self.assertTrue(ruleset.is_consistent(mock_dataset))
def test_ruleset_is_complete_negated_false(self):
rule1 = Rule(conditions=[Condition(attribute="Temperature", value="normal"),
Condition(attribute="Headache", value="no")],
decision=Decision(label="Flue", value="yes"))
ruleset = Ruleset(rules=[rule1])
self.assertFalse(ruleset.is_complete(mock_dataset))
def test_ruleset_is_complete_unnegated_false(self):
rule1 = Rule(conditions=[Condition(attribute="Temperature", value="normal"),
Condition(attribute="Headache", value="no")],
decision=Decision(label="Flue", value="yes"))
ruleset = Ruleset(rules=[rule1])
self.assertFalse(ruleset.is_complete(mock_dataset, negated=False))
def test_ruleset_is_consistent_unnegated_false(self):
rule1 = Rule(conditions=[Condition(attribute="Temperature", value="normal"),
Condition(attribute="Headache", value="no")],
decision=Decision(label="Flue", value="yes"))
rule2 = Rule(conditions=[Condition(attribute="Temperature", value="high"),
Condition(attribute="Temperature", value="very_high")],
decision=Decision(label="Flue", value="no"))
rule3 = Rule(conditions=[Condition(attribute="Headache", value="yes")],
decision=Decision(label="Flue", value="no"))
ruleset = Ruleset(rules=[rule1,rule2,rule3])
self.assertFalse(ruleset.is_consistent(mock_dataset, negated=False))
if __name__ == '__main__':
unittest.main()