-
Notifications
You must be signed in to change notification settings - Fork 1
/
Explanation.py
159 lines (138 loc) · 9.4 KB
/
Explanation.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# Purpose: gets an explanation of results for logic-related code (original simulator results)
sensors = {}
effectors = {}
# this reads sensor values from a log file (logActions.txt) to upate sensors dictionary
def getSensorValues():
f = open("logActions.txt", "r")
lines = f.readlines()
f.close()
for l in lines:
if 'setSensorValue' in l:
nameSensor = l.split('(')[1].split(',')[0]
# print(f"nameSensor: {nameSensor}")
valueSensor = l.split(',')[1].split(')')[0]
# print(f"valueSensor: {valueSensor}" )
#hashmap here
sensors[nameSensor] = valueSensor
print(f"sensors: {sensors}")
# reads sensor values form a log file (logActions.txt) and updates the effectors dictionary accordingly
def getEffectorsValue():
f = open("logActions.txt", "r")
# read all the lines in a reversed orders
lines = f.readlines()[::-1]
f.close()
# read all the lines that start with set() and stop when you find the first line that starts with setSensorValue
for l in lines:
if 'setEffector(' in l:
# save the name of the effector and the value
nameEffector = l.split('(')[1].split(',')[0]
valueEffector = l.split(',')[1].split(')')[0]
effectors[nameEffector] = valueEffector
elif 'set(' in l:
effectors['action'] = l.split('(')[1].split(',')[0]
# think this is interacting with prolog code
# generates an "explanation" based off current sensor and effector values and user preferences regarding actions
def getExplanation(prolog):
print(sensors)
print(effectors)
text=""
text = text + "The action selected is "+ effectors['action'] + ".\n"
# decode the light setting
preference = list(prolog.query("preference("+effectors['action']+", light, V, E)"))
if int(sensors['outside_brightness']) >= int(preference[0]['V']):
text = text + "Since the outside light is greater than the desired light, the expert system has turned off the light and opened the windows.\n"
else:
text = text + "Since the outside light is less than the desired light, the expert system has turned on the selected light at the desired value of brightness ("+ str(preference[0]['V']) +" and closed the windows.\n"
#decode the temperature setting
preference_temp = list(prolog.query("preference("+effectors['action']+", temp, V, E)"))
if int(sensors['inside_temperature']) == int(preference_temp[0]['V']):
text = text + "Since the inside temperature is equal to the desired temperature the expert system doesn not performs any changes.\n"
elif int(sensors['inside_temperature']) < int(preference_temp[0]['V']) and int(sensors['outside_temperature']) > int(preference_temp[0]['V']):
text = text + "Since the inside temperature is less then the desidered temperature and the outside temperature is greater the desidered one "
text = text + " the expert system want to open the windows but first check the wind.\n"
preference_wind = list(prolog.query("preference("+effectors['action']+", wind, V, E)"))
if int(sensors['outside_wind']) <= int(preference_wind[0]['V']):
text = text + "Since the wind is less or equal to the desidered "
if int(sensors['outside_rain']) == 0:
if int(effectors['w1']) == 1 and int(effectors['w2'])== 1:
text = text + " and it is not raining, the expert system open w1 and w2."
else:
text = text + " the expert system is not responding.\n"
else:
text = text + " but it is raining, the expert system decide to "
if int(effectors['ac']) != 0:
text = text + " turn on the air conditioner (ar) setting the temperature " + str(preference_temp[0]['V']) + ". "
elif int(effectors['r']) != 0:
text = text + " turn on the radiator (r) setting the temperature " + str(preference_temp[0]['V']) + ".\n"
else:
text = text + "Since the wind is greater to the desidered, the expert system decide to "
if int(effectors['w1']) == 0 and int(effectors['w2']) == 0:
text = text + "close w1 and w2 and "
if int(effectors['ac']) != 0:
text = text + "turn on the air conditioner (ar) setting the temperature " + str(preference_temp[0]['V']) + ". "
elif int(effectors['r']) != 0:
text = text + "turn on the radiator (r) setting the temperature " + str(preference_temp[0]['V']) + ".\n"
else:
text = text + "the expert system is not responding.\n"
elif int(sensors['inside_temperature']) < int(preference_temp[0]['V']) and int(sensors['outside_temperature']) < int(preference_temp[0]['V']):
text = text + "Since both inside and ouside temperature are less then the desidered temperature, "
if int(effectors['w1']) == 0 and int(effectors['w2'])==0:
text = text + " the expert system close w1 and w2 and "
if int(effectors['ac']) != 0:
text = text + "turn on the air conditioner (ar) setting the temperature " + str(preference_temp[0]['V']) + ". "
elif int(effectors['r']) != 0:
text = text + "turn on the radiator (r) setting the temperature " + str(preference_temp[0]['V']) + ".\n"
else:
text = text + "the expert system is not responding.\n"
elif int(sensors['inside_temperature']) > int(preference_temp[0]['V']) and int(sensors['outside_temperature']) > int(preference_temp[0]['V']):
text = text + "Since both inside and ouside temperature are greater then the desidered temperature, "
if int(effectors['w1']) == 0 and int(effectors['w2'])==0 :
text = text + " the expert system close the w1 and w2 and "
if int(effectors['ac']) != 0:
text = text + "turn on the air conditioner (ar) setting the temperature " + str(preference_temp[0]['V']) + ". "
elif int(effectors['r']) != 0:
text = text + "turn on the radiator (r) setting the temperature " + str(preference_temp[0]['V']) + ".\n"
else:
text = text + "the expert system is not responding.\n"
elif int(sensors['inside_temperature']) > int(preference_temp[0]['V']) and int(sensors['outside_temperature']) < int(preference_temp[0]['V']):
text = text + "Since the inside temperature is greater then the desider temperature and the outside temperature is less then the desidered temperature "
text = text + " the expert system want to open the windows but first check the wind.\n"
preference_wind = list(prolog.query("preference("+effectors['action']+", wind, V, E)"))
if int(sensors['outside_wind']) > int(preference_wind[0]['V']):
text = text + "Since the wind is greater to the desidered, the expert system decide to "
if int(effectors['w1']) ==0 and int(effectors['w2']) ==0:
text = text + "close w1 and w2 and "
if int(effectors['ac']) != 0:
text = text + "turn on the air conditioner (ar) setting the temperature " + str(preference_temp[0]['V']) + ". "
elif int(effectors['r']) != 0:
text = text + "turn on the radiator (r) setting the temperature " + str(preference_temp[0]['V']) + ".\n"
else:
text = text + "the expert system is not responding.\n"
else:
text = text + "Since the wind is less or equal to the desidered "
if int(sensors['outside_rain']) == 0:
if int(effectors['w1']) == 1 and int(effectors['w2']) == 1:
text = text + " and it is not raining, the expert system open w1 and w2.\n"
else:
text = text + " the expert system is not responding.\n"
else:
text = text + " but it is raining, the expert system decide to "
if int(effectors['ac']) != 0:
text = text + " turn on the air conditioner (ar) setting the temperature " + str(preference_temp[0]['V']) + ".\n"
elif int(effectors['r']) != 0:
text = text + " turn on the radiator (r) setting the temperature " + str(preference_temp[0]['V']) + ".\n"
# decode the noise setting
preference = list(prolog.query("preference("+effectors['action']+", noise, V, E)"))
preferenceTemp = list(prolog.query("preference("+effectors['action']+", temp, V, E)"))
if int(sensors['outside_noise']) > preference[0]['V']:
text = text + "Since the outside noise is greater than the desired noise, the expert system "
if int(sensors['outside_temperature']) == int(preferenceTemp[0]['V']):
text = text + "has closed the windows.\n"
else:
if int(sensors['outside_temperature']) < int(preferenceTemp[0]['V']):
text = text + "has closed the windows and has turned on the air conditioning because the temperature outside is greater than the temperature inside.\n"
else:
text = text + "has closed the windows and has turned on the radiator because the temperature outside is less than the temperature inside.\n"
else:
text = text + "Since the outside noise is less than the desired noise, the expert system has opened the windows.\n"
return text