def do(self, funcEnv, resourceId, *args, **kargs): """ Retract function handler implementation """ # convert <TYPE:value> to python value resourceId = Function.resolve( self, funcEnv, self.semplify(funcEnv, resourceId, types.Symbol, ("1", "symbol"))) if resourceId != "nil": try: resource = funcEnv.RESOURCES[resourceId] except KeyError: raise InvalidArgValueError( "Resource with logical name %s cannot be found" % str(resourceId)) else: # for fragment in args: # # # revolve variables and function calls # fragment = self.resolve(funcEnv, self.semplify(funcEnv, fragment)) # # resource.write(str(fragment)) resource.write("".join([ str(self.resolve(funcEnv, self.semplify(funcEnv, x))) for x in args ])) return types.NullValue()
def do(self, theEnv, *args, **kargs): """ function handler implementation @see: http://www.comp.rgu.ac.uk/staff/smc/teaching/clips/vol1/vol1-13.2.html#Heading418 """ if len(args): args = dict([(x, True) for x in self.resolve(theEnv, self.semplify(theEnv, args))]) if args.has_key("all") or args.has_key("network"): try: theEnv.network.settings.delSetting("_funcs.Watch.network").uninstall() except: pass if args.has_key("all") or args.has_key("facts"): try: theEnv.network.settings.delSetting("_funcs.Watch.facts").uninstall() except: pass if args.has_key("all") or args.has_key("rules"): try: theEnv.network.settings.delSetting("_funcs.Watch.rules").uninstall() except: pass if args.has_key("all") or args.has_key("activations"): try: theEnv.network.settings.delSetting("_funcs.Watch.activations").uninstall() except: pass if args.has_key("all") or args.has_key("focus"): try: theEnv.network.settings.delSetting("_funcs.Watch.focus").uninstall() except: pass if args.has_key("all") or args.has_key("actions"): try: theEnv.network.settings.delSetting("_funcs.Watch.actions").uninstall() except: pass if args.has_key("all") or args.has_key("strategy"): try: theEnv.network.settings.delSetting("_funcs.Watch.strategy").uninstall() except: pass if args.has_key("all") or args.has_key("statistics"): try: theEnv.network.settings.delSetting("_funcs.Watch.statistics").uninstall() except: pass return types.NullValue()
def do(self, theEnv, *args, **kargs): """ function handler implementation """ theEnv.network.clear() return types.NullValue()
def do(self, theEnv, theResets=None, *args, **kargs): """ function handler implementation """ theEnv.network.reset() return types.NullValue()
def do(self, theEnv, theCondition, theThen, *args, **kargs): """ handler of the IfThenElse function: if-then-else conditional structure @see: http://www.comp.rgu.ac.uk/staff/smc/teaching/clips/vol1/vol1-12.6.html#Heading280 """ returnValue = types.NullValue() if not theThen.pyEqual("then"): raise InvalidArgValueError("Check appropriate syntax for if function: if expects second argument to be the `then` keyword") # try to split the then-actions from the else-actions thenActions = [] elseActions = [] workingOn = thenActions for action in args: if isinstance(action, types.Symbol) and action.pyEqual("else"): workingOn = elseActions else: workingOn.append(action) # if no else is found, not problem: elseActions is empty and all actions are in thenActions # time to evaluate the condition if isinstance(theCondition, types.FunctionCall): theCondition = self.resolve(theEnv, theCondition) # CLIPS documentation: # then-actions are executed if the result of theCondition is not <Symbol:FALSE> # (even <NullValue> is a true-like value) # else-actions are executed only if return of theCondition is exactly <Symbol:FALSE> if isinstance(theCondition, types.Symbol) and theCondition.pyEqual("FALSE"): # execute ELSE theActions = elseActions else: # execute THEN theActions = thenActions for action in theActions: # function are executed if isinstance(action, types.FunctionCall): returnValue = self.resolve(theEnv, action) else: # types work as return values returnValue = action # CLIPS documentation: # the if-then-else return value is always the value of the last execute action return returnValue
def do(self, theEnv, theWme, *args, **kargs): """ function handler implementation """ theWme = self.resolveWme(theEnv, self.semplify(theEnv, theWme, (WME, types.Integer), ("1", "fact-address or i"))) import myclips.debug as debug debug.show_wme_details(theEnv.RESOURCES['wtrace'], theWme, explodeToken=True, explodeAMem=True) return types.NullValue()
def do(self, theEnv, theLevel, *args, **kargs): """ function handler implementation """ theLevel = self.resolve(theEnv, self.semplify(theEnv, theLevel, types.Integer, ("1", "integer"))) myclips.logger.setLevel(theLevel) return types.NullValue()
def do(self, theEnv, theModule, *args, **kargs): """ function handler implementation """ theModule = self.resolve( theEnv, self.semplify(theEnv, theModule, types.Symbol, ("1", "symbol"))) theEnv.RESOURCES['wtrace'].write( str(theEnv.modulesManager.getScope(theModule)) + "\n") return types.NullValue()
def do(self, theEnv, theRuns=None, *args, **kargs): """ function handler implementation """ if theRuns is not None: theRuns = self.resolve( theEnv, self.semplify(theEnv, theRuns, types.Integer, ("1", "integer"))) theEnv.network.run(theRuns) return types.NullValue()
def do(self, theEnv, theModule=None, *args, **kargs): """ function handler implementation """ if theModule is not None: theModule = self.resolve( theEnv, self.semplify(theEnv, theModule, types.Symbol, ("1", "symbol"))) theModules = [ theModule ] if theModule != '*' else theEnv.modulesManager.getModulesNames() actCount = 0 theStdout = theEnv.RESOURCES['wdisplay'] if len(theModules) > 1: for theModule in theModules: theStdout.write("%s:\n" % theModule) acts = theEnv.network.agenda.activations(theModule) actCount += len(acts) for (salience, pnode, token) in acts: theStdout.write("\t%-6d %s: %s\n" % (salience, pnode.mainRuleName, token)) else: acts = theEnv.network.agenda.activations(theModule) #acts.reverse() actCount += len(acts) for (salience, pnode, token) in acts: theStdout.write("%-6d %s: %s\n" % (salience, pnode.mainRuleName, token)) if actCount: theStdout.write("For a total of %d activations.\n" % actCount) return types.NullValue()
def do(self, theEnv, *args, **kargs): """ function handler implementation @see: http://www.comp.rgu.ac.uk/staff/smc/teaching/clips/vol1/vol1-13.2.html#Heading417 """ if len(args): args = dict([ (x, True) for x in self.resolve(theEnv, self.semplify(theEnv, args)) ]) if args.has_key("all") or args.has_key("network"): try: theEnv.network.settings.getSetting("_funcs.Watch.network") except KeyError: theEnv.network.settings.setSetting( "_funcs.Watch.network", NetworkBuildPrinter( theEnv.RESOURCES['wtrace']).install( theEnv.network.eventsManager)) if args.has_key("all") or args.has_key("facts"): try: theEnv.network.settings.getSetting("_funcs.Watch.facts") except KeyError: theEnv.network.settings.setSetting( "_funcs.Watch.facts", FactsWatcher(theEnv.RESOURCES['wtrace']).install( theEnv.network.eventsManager)) if args.has_key("all") or args.has_key("rules"): try: theEnv.network.settings.getSetting("_funcs.Watch.rules") except KeyError: theEnv.network.settings.setSetting( "_funcs.Watch.rules", RulesWatcher(theEnv.RESOURCES['wtrace']).install( theEnv.network.eventsManager)) if args.has_key("all") or args.has_key("activations"): try: theEnv.network.settings.getSetting( "_funcs.Watch.activations") except KeyError: theEnv.network.settings.setSetting( "_funcs.Watch.activations", ActivationsWatcher(theEnv.RESOURCES['wtrace']).install( theEnv.network.eventsManager)) if args.has_key("all") or args.has_key("focus"): try: theEnv.network.settings.getSetting("_funcs.Watch.focus") except KeyError: theEnv.network.settings.setSetting( "_funcs.Watch.focus", FocusWatcher(theEnv.RESOURCES['wtrace']).install( theEnv.network.eventsManager)) if args.has_key("all") or args.has_key("actions"): try: theEnv.network.settings.getSetting("_funcs.Watch.actions") except KeyError: theEnv.network.settings.setSetting( "_funcs.Watch.actions", ActionsWatcher(theEnv.RESOURCES['wtrace']).install( theEnv.network.eventsManager)) if args.has_key("all") or args.has_key("strategy"): try: theEnv.network.settings.getSetting("_funcs.Watch.strategy") except KeyError: theEnv.network.settings.setSetting( "_funcs.Watch.strategy", StrategyWatcher(theEnv.RESOURCES['wtrace']).install( theEnv.network.eventsManager)) if args.has_key("all") or args.has_key("statistics"): try: theEnv.network.settings.getSetting( "_funcs.Watch.statistics") except KeyError: theEnv.network.settings.setSetting( "_funcs.Watch.statistics", StatisticsWatcher(theEnv.RESOURCES['wtrace'], theEnv.network).install( theEnv.network.eventsManager)) return types.NullValue()
def do(self, theEnv, *args, **kargs): """ function handler implementation """ argsLen = len(args) if argsLen == 4: # 0 = theModule, 1 = theStart, 2 = theEnd, 3 = theMax theModules = self.resolve(theEnv, self.semplify(theEnv, args[0], types.Symbol, ("1", "symbol or * (in a 4 args configuration)"))) theStart = self.resolve(theEnv, self.semplify(theEnv, args[1], types.Integer, ("2", "integer (in a 4 args configuration)"))) theEnd = self.resolve(theEnv, self.semplify(theEnv, args[2], types.Integer, ("3", "integer (in a 4 args configuration)"))) theMax = self.resolve(theEnv, self.semplify(theEnv, args[3], types.Integer, ("4", "integer (in a 4 args configuration)"))) elif argsLen == 3: # 0 = theModule, 1 = theStart, 2 = theEnd # or # 0 = theStart, 1 = theEnd, 2 = theMax theFirst = self.semplify(theEnv, args[0], (types.Integer, types.Symbol), ("1", "integer, symbol or * (in a 3 args configuration)")) if isinstance(theFirst, types.Symbol): # first case theModules = self.resolve(theEnv, theFirst) theStart = self.resolve(theEnv, self.semplify(theEnv, args[1], types.Integer, ("2", "integer (in a 3 args configuration)"))) theEnd = self.resolve(theEnv, self.semplify(theEnv, args[2], types.Integer, ("3", "integer (in a 3 args configuration)"))) theMax = None else: theModules = theEnv.modulesManager.currentScope.moduleName theStart = self.resolve(theEnv, theFirst) theEnd = self.resolve(theEnv, self.semplify(theEnv, args[1], types.Integer, ("2", "integer (in a 3 args configuration)"))) theMax = self.resolve(theEnv, self.semplify(theEnv, args[2], types.Integer, ("3", "integer (in a 3 args configuration)"))) elif argsLen == 2: # 0 = theModule, 1 = theStart # or # 0 = theStart, 1 = theEnd theFirst = self.semplify(theEnv, args[0], (types.Integer, types.Symbol), ("1", "integer, symbol or * (in a 2 args configuration)")) if isinstance(theFirst, types.Symbol): # first case theModules = self.resolve(theEnv, theFirst) theStart = self.resolve(theEnv, self.semplify(theEnv, args[1], types.Integer, ("2", "integer (in a 2 args configuration)"))) theEnd = sys.maxint theMax = None else: theModules = theEnv.modulesManager.currentScope.moduleName theStart = self.resolve(theEnv, theFirst) theEnd = self.resolve(theEnv, self.semplify(theEnv, args[1], types.Integer, ("2", "integer (in a 2 args configuration)"))) theMax = None elif argsLen == 1: # 0 = theModule # or # 0 = theStart theFirst = self.semplify(theEnv, args[0], (types.Integer, types.Symbol), ("1", "integer, symbol or * (in a 1 args configuration)")) if isinstance(theFirst, types.Symbol): # first case theModules = self.resolve(theEnv, theFirst) theStart = 0 theEnd = sys.maxint theMax = None else: theModules = theEnv.modulesManager.currentScope.moduleName theStart = self.resolve(theEnv, theFirst) theEnd = sys.maxint theMax = None else: # only show the current modules fact, without filters about fact-id theModules = theEnv.modulesManager.currentScope.moduleName theStart = 0 theEnd = sys.maxint theMax = None #normalize theModules theModules = [] if theModules == "*" else [theModules] theFacts = [] if len(theModules) > 0: for theModule in theModules: theFacts += theEnv.network.factsForScope(theModule) else: theFacts = theEnv.network.facts # filter and cut the wme list to display theFacts = [wme for wme in theFacts if wme.factId >= theStart and wme.factId <= theEnd][:theMax] if len(theFacts): theStdout = theEnv.RESOURCES['wdisplay'] for wme in theFacts: theStdout.write("f-%-5d %s\n"%(wme.factId, wme.fact)) theStdout.write("For a total of %d facts.\n"%len(theFacts)) return types.NullValue()
raise InvalidArgValueError(e.message) else: # RuleNotFoundError could be raised... but it will flow outside of the network # and it's ok! thePNodes = [] for thePNode in _thePNodes: from myclips.rete.nodes.PNode import PNode assert isinstance(thePNode, PNode) thePNodes += [thePNode] + thePNode.getLinkedPNodes() import myclips.debug as debug debug.draw_network_fragment(thePNodes) return types.NullValue() DrawCircuit.DEFINITION = FunctionDefinition( "?SYSTEM?", "draw-circuit", DrawCircuit(), types.NullValue, DrawCircuit.do, [ Constraint_MinArgsLength(1), Constraint_ArgType(types.Symbol), ], forward=False)