def execute(self, theToken): # resolve variables from the token resolved = {} linearToken = theToken.linearize() for theVar, theLocation in self._variables.items(): try: assert isinstance(theLocation, VariableLocation) resolved[theVar] = theLocation.toValue(linearToken[theLocation.patternIndex]) except: import myclips myclips.logger.debug("%s: unresolvable variable %s: %s:%s %s", self.completeRuleName(), theVar, theLocation.patternIndex, theLocation, linearToken ) # prepare the FunctionEnv object theEnv = FunctionEnv(resolved, self._network, self._network.modulesManager, self._network.resources) # execute all rhs passing FunctionEnv # theEnv could be modified from the function call. # This THE way to share memory between functions for action in self._rhs: assert isinstance(action, types.FunctionCall) # get the function definition linked to the FunctionCall #funcDefinition = action.funcDefinition #assert isinstance(funcDefinition, FunctionDefinition) # expand the args #funcDefinition.linkedType.__class__.execute(funcDefinition.linkedType, theEnv, *(action.funcArgs)) Function.doExecute(action, theEnv)
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, 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 resolve(self, funcEnv, arg): """ Override Function.resolve to manage the <Symbol:crlf> conversion to NEWLINE and to remove quotes in types.String values """ if isinstance(arg, types.Symbol) and arg.pyEqual("crlf"): return "\n" else: return Function.resolve(self, funcEnv, arg)
def __init__(self, *args, **kwargs): Function.__init__(self, *args, **kwargs)
except Exception, e: print >> theEnv.RESOURCES['werror'], theEnv.network.getParser( ).ExceptionPPrint(e, aString) os.chdir(oldcwd) return types.Symbol('FALSE') else: cString = "" for p in parsed: if isinstance(p, types.DefRuleConstruct): theEnv.network.addRule(p) elif isinstance(p, types.DefFactsConstruct): theEnv.network.addDeffacts(p) elif isinstance(p, types.FunctionCall): theResult = Function.doExecute(p, theEnv) if not isinstance(theResult, types.NullValue): print >> theEnv.RESOURCES['wtrace'], str(theResult) os.chdir(oldcwd) return types.Symbol('TRUE') Batch.DEFINITION = FunctionDefinition("?SYSTEM?", "batch", Batch(), types.Symbol, Batch.do, [ Constraint_ExactArgsLength(1), Constraint_ArgType(
def __init__(self, cmpType=None, *args, **kwargs): Function.__init__(self, *args, **kwargs) self._cmpType = cmpType if cmpType is not None else types.BaseParsedType
# add the new rule to the network self._network.addRule(parsed) elif isinstance(parsed, types.DefFactsConstruct): # add the deffacts self._network.addDeffacts(parsed) elif isinstance(parsed, types.FunctionCall): # execute the function assert isinstance(parsed, types.FunctionCall) # prepare the FunctionEnv object theEnv = FunctionEnv({}, self._network, self._network.modulesManager, self._network.resources) #funcDefinition = parsed.funcDefinition #theResult = funcDefinition.linkedType.__class__.execute(funcDefinition.linkedType, theEnv, *(parsed.funcArgs)) # replace old function bootstrap method with a faster one theResult = Function.doExecute(parsed, theEnv) if not isinstance(theResult, types.NullValue): return theResult elif isinstance(parsed, types.GlobalVariable): # resolve the global value return self._network.modulesManager.currentScope.globalsvars.getDefinition(parsed.evaluate()).linkedType.runningValue elif isinstance(parsed, types.BaseParsedType): return parsed
parsed = theEnv.network.getParser().parse(aString, extended=True) except Exception, e: print >> theEnv.RESOURCES['werror'], theEnv.network.getParser().ExceptionPPrint(e, aString) os.chdir(oldcwd) return types.Symbol('FALSE') else: cString = "" for p in parsed: if isinstance(p, types.DefRuleConstruct): theEnv.network.addRule(p) elif isinstance(p, types.DefFactsConstruct): theEnv.network.addDeffacts(p) elif isinstance(p, types.FunctionCall): theResult = Function.doExecute(p, theEnv) if not isinstance(theResult, types.NullValue): print >> theEnv.RESOURCES['wtrace'], str(theResult) os.chdir(oldcwd) return types.Symbol('TRUE') Batch.DEFINITION = FunctionDefinition("?SYSTEM?", "batch", Batch(), types.Symbol, Batch.do , [ Constraint_ExactArgsLength(1), Constraint_ArgType((types.Symbol, types.String), 0) ],forward=False)
def __init__(self, theParams, theActions, *args, **kwargs): Function.__init__(self, *args, **kwargs) self._actions = theActions if isinstance(theActions, list) else [] self._params = theParams if isinstance(theParams, list) else []
elif isinstance(parsed, types.DefFactsConstruct): # add the deffacts self._network.addDeffacts(parsed) elif isinstance(parsed, types.FunctionCall): # execute the function assert isinstance(parsed, types.FunctionCall) # prepare the FunctionEnv object theEnv = FunctionEnv({}, self._network, self._network.modulesManager, self._network.resources) #funcDefinition = parsed.funcDefinition #theResult = funcDefinition.linkedType.__class__.execute(funcDefinition.linkedType, theEnv, *(parsed.funcArgs)) # replace old function bootstrap method with a faster one theResult = Function.doExecute(parsed, theEnv) if not isinstance(theResult, types.NullValue): return theResult elif isinstance(parsed, types.GlobalVariable): # resolve the global value return self._network.modulesManager.currentScope.globalsvars.getDefinition( parsed.evaluate()).linkedType.runningValue elif isinstance(parsed, types.BaseParsedType): return parsed