# # Exercise 10: Expert Systems

# Install packages incase you cannot import
import unittest

# ## Implement backward chaining

class Rule():
    def __init__(self, name: any, lhs: dict[any, set], rhs: tuple[any,any]):
        self.name = name
        self.lhs = lhs
        self.rhs = rhs

    def __str__(self):
        return self.name

    def __repr__(self) -> str:
        return self.name

def value_valid(ont: dict, var: any, val: any) -> bool:
    # Your code here
    pass

def rule_status(rule: Rule, wm: dict) -> any:
    # Your code here
    pass

def apply_rule(rule: Rule, wm: dict):
    # Your code here
    pass

def find_rules(rules: list[Rule], goal: any) -> list[Rule]:
    # Your code here
    pass

def backward_chain(ont: dict, rules: list[Rule], goal: any, known_data: dict) -> any:
    # Your code here
    pass
            

# Examples of rule and ontology

r1_lhs = {'shape': {'elongated'}, 'color': {'green', 'yellow'}}
r1_rhs = ('fruit', 'banana')
r1 = Rule('r1', r1_lhs, r1_rhs)
print(r1)
print(f"Left-hand-side rule 1: {r1.lhs}")
print(f"Right-hand-side rule 1: {r1.rhs}")

ont = {
    'shape': {'elongated', 'circular', 'rounded'}, 
    'color': {'green', 'yellow', 'brown-yellow', 'orange', 'red', 'blue'},
    'diameter': {'>10', '<10'},
    'fruit_type': {'vine', 'tree'},
    'no_seeds': {'1', '>1'},
    'seed_type': {'bony', 'multiple'},
    'surface': {'smooth', 'course'},
    'fruit': {'banana', 'watermelon', 'melon', 'cantaloupe', 'apricot', 'orange', 'cherry', 'peach', 'apple', 'plum'}
}
print(f"Ontology: {ont}")

known_data = {'fruit_type': 'tree', 'shape': 'circular', 'diameter': '<10', 'color': 'green', 'no_seeds': '>1'}
print(f"Known data: {known_data}")


# Test cases (test only proper execution of the method. Does not test for complexity).
class TestBackchaining(unittest.TestCase):

    def build_ont(self):
        ont = {
            'shape': {'elongated', 'circular', 'rounded'}, 
            'color': {'green', 'yellow', 'brown-yellow', 'orange', 'red', 'blue'},
            'diameter': {'>10', '<10'},
            'fruit_type': {'vine', 'tree'},
            'no_seeds': {'1', '>1'},
            'seed_type': {'bony', 'multiple'},
            'surface': {'smooth', 'course'},
            'fruit': {'banana', 'watermelon', 'melon', 'cantaloupe', 'apricot', 'orange', 'cherry', 'peach', 'apple', 'plum'}
            } 
        return ont

    def build_rules(self):
        rules = []
        r1_lhs = {'shape': {'elongated'}, 'color': {'green', 'yellow'}}
        r1_rhs = ('fruit', 'banana')
        r1 = Rule('r1', r1_lhs, r1_rhs)
        rules.append(r1)
        r2_lhs = {'shape': {'circular', 'rounded'}, 'diameter': {'>10'}}
        r2_rhs = ('fruit_type', 'vine')
        r2 = Rule('r2', r2_lhs, r2_rhs)
        rules.append(r2)
        r3_lhs = {'shape': {'circular'}, 'diameter': {'<10'}}
        r3_rhs = ('fruit_type', 'tree')
        r3 = Rule('r3', r3_lhs, r3_rhs)
        rules.append(r3)
        r4_lhs = {'no_seeds': {'1'}}
        r4_rhs = ('seed_type', 'bony')
        r4 = Rule('r4', r4_lhs, r4_rhs)
        rules.append(r4)
        r5_lhs = {'no_seeds': {'>1'}}
        r5_rhs = ('seed_type', 'multiple')
        r5 = Rule('r5', r5_lhs, r5_rhs)
        rules.append(r5)
        r6_lhs = {'fruit_type': {'vine'}, 'color': {'green'}}
        r6_rhs = ('fruit', 'watermelon')
        r6 = Rule('r6', r6_lhs, r6_rhs)
        rules.append(r6)
        r7_lhs = {'fruit_type': {'vine'}, 'color': {'yellow'}, 'surface': {'smooth'}}
        r7_rhs = ('fruit', 'melon')
        r7 = Rule('r7', r7_lhs, r7_rhs)
        rules.append(r7)
        r8_lhs = {'fruit_type': {'vine'}, 'color': {'brown-yellow'}, 'surface': {'course'}}
        r8_rhs = ('fruit', 'cantaloupe')
        r8 = Rule('r8', r8_lhs, r8_rhs)
        rules.append(r8)
        r9_lhs = {'fruit_type': {'tree'}, 'color': {'orange'}, 'seed_type': {'bony'}}
        r9_rhs = ('fruit', 'apricot')
        r9 = Rule('r9', r9_lhs, r9_rhs)
        rules.append(r9)
        r10_lhs = {'fruit_type': {'tree'}, 'color': {'orange'}, 'seed_type': {'multiple'}}
        r10_rhs = ('fruit', 'orange')
        r10 = Rule('r10', r10_lhs, r10_rhs)
        rules.append(r10)
        r11_lhs = {'fruit_type': {'tree'}, 'color': {'red'}, 'seed_type': {'bony'}}
        r11_rhs = ('fruit', 'cherry')
        r11 = Rule('r11', r11_lhs, r11_rhs)
        rules.append(r11)
        r12_lhs = {'fruit_type': {'tree'}, 'color': {'orange'}, 'seed_type': {'bony'}}
        r12_rhs = ('fruit', 'peach')
        r12 = Rule('r12', r12_lhs, r12_rhs)
        rules.append(r12)
        r13_lhs = {'fruit_type': {'tree'}, 'color': {'yellow', 'green'}, 'seed_type': {'multiple'}}
        r13_rhs = ('fruit', 'apple')
        r13 = Rule('r13', r13_lhs, r13_rhs)
        rules.append(r13)
        r14_lhs = {'fruit_type': {'tree'}, 'color': {'yellow', 'green'}, 'seed_type': {'multiple'}}
        r14_rhs = ('fruit', 'apple')
        r14 = Rule('r14', r14_lhs, r14_rhs)
        rules.append(r14)
        return rules 

    def testBackchaining1(self):
        ont = self.build_ont()
        rules = self.build_rules()[::-1]
        known_data = {'fruit_type': 'tree', 'shape': 'circular', 'diameter': '<10', 'color': 'green', 'no_seeds': '>1'}
        self.assertEqual('apple', backward_chain(ont, rules, 'fruit', known_data))#
    
    def testBackchaining1(self):
        ont = self.build_ont()
        rules = self.build_rules()
        known_data = {'fruit_type': 'tree', 'shape': 'circular', 'diameter': '<10', 'color': 'red', 'no_seeds': '1'}
        self.assertEqual('cherry', backward_chain(ont, rules, 'fruit', known_data))


if __name__ == "main":
    # Run the test cases
    unittest.main(argv=[''], verbosity=2, exit=False)


