/
BBCLIPS.py
232 lines (182 loc) · 7.68 KB
/
BBCLIPS.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# -*- coding: utf-8 -*-
'''
@author: arcra
'''
import time, threading, os
import Tkinter as tk
import argparse
import clipsFunctions
from clipsFunctions import clips, _clipsLock
import pyrobotics.BB as BB
from pyrobotics.messages import Command, Response
import GUI
from BBFunctions import assertQueue, ResponseReceived, CreateSharedVar, WriteSharedVar, SubscribeToSharedVar, RunCommand
defaultTimeout = 2000
defaultAttempts = 1
_sleepingLock = threading.Lock()
_sleeping = False
def setCmdTimer(t, cmd, cmdId):
t = threading.Thread(target=cmdTimerThread, args = (t, cmd, cmdId))
t.daemon = True
t.start()
return True
def cmdTimerThread(t, cmd, cmdId):
time.sleep(t/1000.0)
assertQueue.append('(BB_timer "{0}" {1})'.format(cmd, cmdId))
#clipsFunctions.Assert('(BB_timer "{0}" {1})'.format(cmd, cmdId))
def setTimer(t, sym):
t = threading.Thread(target=timerThread, args = (t, sym))
t.daemon = True
t.start()
return True
def timerThread(t, sym):
time.sleep(t/1000.0)
assertQueue.append('(BB_timer {0})'.format(sym))
#clipsFunctions.Assert('(BB_timer {0})'.format(sym))
def SendCommand(cmdName, params):
cmd = Command(cmdName, params)
BB.Send(cmd)
return cmd._id
def SendResponse(cmdName, cmd_id, result, response):
result = str(result).lower() not in ['false', '0']
r = Response(cmdName, result, response)
r._id = cmd_id
BB.Send(r)
def stop():
GUI._pausedLock.acquire()
GUI.gui.paused = True
GUI._pausedLock.release()
return True
def sleep(ms, sym):
t = threading.Thread(target=sleepingTimerThread, args = (ms, sym))
t.daemon = True
t.start()
return True
def sleepingTimerThread(t, sym):
_sleepingLock.acquire()
_sleeping = True
_sleepingLock.release()
time.sleep(t/1000)
_sleepingLock.acquire()
_sleeping = False
_sleepingLock.release()
def Initialize(params):
clips.Memory.Conserve = True
clips.Memory.EnvironmentErrorsEnabled = True
clips.SetExternalTraceback(True)
clips.DebugConfig.FactsWatched = params.watchfacts
clips.DebugConfig.GlobalsWatched = params.watchglobals
clips.DebugConfig.FunctionsWatched = params.watchfunctions
clips.DebugConfig.RulesWatched = params.watchrules
clips.RegisterPythonFunction(SendCommand)
clips.RegisterPythonFunction(SendResponse)
clips.RegisterPythonFunction(setCmdTimer)
clips.RegisterPythonFunction(setTimer)
clips.RegisterPythonFunction(CreateSharedVar)
clips.RegisterPythonFunction(WriteSharedVar)
clips.RegisterPythonFunction(SubscribeToSharedVar)
clips.RegisterPythonFunction(sleep)
clips.RegisterPythonFunction(stop)
clips.BuildGlobal('defaultTimeout', defaultTimeout)
clips.BuildGlobal('defaultAttempts', defaultAttempts)
filePath = os.path.dirname(os.path.abspath(__file__))
clips.BatchStar(os.path.join(filePath, 'CLIPS', 'utils.clp'))
clips.BatchStar(os.path.join(filePath, 'CLIPS', 'BB_interface.clp'))
clipsFunctions.PrintOutput()
GUI.use_gui = not params.nogui
if GUI.use_gui:
GUI.gui = GUI.clipsGUI()
else:
GUI.debug = params.debug
if params.file:
GUI.load_file(params.file)
BB.Initialize(params.port, functionMap = {'*':(RunCommand, True)}, asyncHandler = ResponseReceived)
print 'Waiting for BlackBoard to connect...'
BB.Start()
print 'BlackBoard connected!'
BB.SetReady()
print 'READY!'
def main():
parser = argparse.ArgumentParser(description="Runs an instance of BBCLIPS. (CLIPS interpreter embedded in python with BB communication.)")
parser.add_argument('-p', '--port', default = '2001', type=int, help='States the port number that this instance module should use.')
parser.add_argument('--nogui', default=False, action='store_const', const=True, help='Runs the program without the GUI.')
parser.add_argument('--debug', default=False, action='store_const', const=True, help='Show a CLIPS prompt as in an interactive CLIPS session.')
parser.add_argument('-n', '--steps', default=1, action='store', type=int, help='Number of steps to run when pressing enter on a debug session.')
parser.add_argument('-f', '--file', help='Specifies the file that should be loaded (mainly for nogui usage).')
watch_group = parser.add_argument_group('Watch options', 'Set the watch flags of the clips interpreter.')
watch_group.add_argument('--watchfunctions', '--wfunctions', '--wfunc', default=False, action='store_const', const=True, help='Enables the watch functions flag of the clips interpreter.')
watch_group.add_argument('--watchglobals', '--wglobals', '--wg', default=False, action='store_const', const=True, help='Enables the watch globals flag of the clips interpreter.')
watch_group.add_argument('--watchfacts', '--wfacts', '--wf', default=False, action='store_const', const=True, help='Enables the watch facts flag of the clips interpreter.')
watch_group.add_argument('--watchrules', '--wrules', '--wr', default=False, action='store_const', const=True, help='Enables the watch rules flag of the clips interpreter.')
log_group = parser.add_argument_group('Log options', 'Set the log level of the BBCLIPS module.')
log_group.add_argument('--log', default='ERROR', choices=['INFO', 'WARNING', 'ERROR'], help='Default is ERROR.')
args = parser.parse_args()
Initialize(args)
if args.nogui:
if args.debug:
s = raw_input('[CLIPS]>')
while s != '(exit)':
if s == '(facts)':
clips.PrintFacts()
elif s == '(rules)':
clips.PrintRules()
elif s == '(agenda)':
clips.PrintAgenda()
elif s == '':
assertEnqueuedFacts()
clipsFunctions.PrintOutput()
clipsFunctions.Run(args.steps)
clipsFunctions.PrintOutput()
else:
try:
_clipsLock.acquire()
#clips.SendCommand(s, True)
clips.Eval(s)
clipsFunctions.PrintOutput()
_clipsLock.release()
except:
print 'ERROR: Clips could not run the command.'
clipsFunctions.PrintOutput()
_clipsLock.release()
s = raw_input('[CLIPS]>')
else:
mainLoop()
else:
loop_thread = threading.Thread(target=mainLoop)
loop_thread.daemon = True
loop_thread.start()
tk.mainloop()
def assertEnqueuedFacts():
_clipsLock.acquire()
while True:
try:
f = assertQueue.popleft()
except:
break
asserted = False
while not asserted:
try:
clips.Assert(f)
asserted = True
except:
#print 'Fact: ' + str(f) + ' could not be asserted, trying again...'
pass
if not asserted:
time.sleep(50)
_clipsLock.release()
def mainLoop():
while True:
assertEnqueuedFacts()
_sleepingLock.acquire()
sleeping = _sleeping
_sleepingLock.release()
GUI._pausedLock.acquire()
paused = GUI.gui.paused
GUI._pausedLock.release()
if sleeping or paused or (GUI.use_gui and GUI.gui.runTimes):
clipsFunctions.PrintOutput()
continue
clipsFunctions.Run(2)
clipsFunctions.PrintOutput()
if __name__ == "__main__":
main()