if theBegin - 1 >= len(theMultifield) or theEnd > len( theMultifield): raise IndexError() del theMultifield[theBegin - 1:theEnd] else: # remove a single item from the multifield del theMultifield[theBegin - 1] except IndexError: # invalid field! raise InvalidArgValueError( "Multifield index %s out of range 1..%d in function delete$" % (("range %d..%d" % (theBegin, theEnd) if theBegin != theEnd else str(theBegin)), len(theMultifield))) else: # no error, return the modified multifield return theMultifield Delete.DEFINITION = FunctionDefinition( "?SYSTEM?", "delete$", Delete(), list, Delete.do, [ Constraint_ExactArgsLength(3), Constraint_ArgType(list, 0), Constraint_ArgType(types.Integer, 1), Constraint_ArgType(types.Integer, 2) ], forward=False)
class FactIndex(Function): ''' The factindex function returns the fact- index (an integer) of a fact-address. WARNING: MyClips's version of fact-address is a WME instance @see http://www.comp.rgu.ac.uk/staff/smc/teaching/clips/vol1/vol1-12.9.html#Heading306 ''' def __init__(self, *args, **kwargs): Function.__init__(self, *args, **kwargs) def do(self, theEnv, theWme, *args, **kargs): """ Function handler implementation @see http://www.comp.rgu.ac.uk/staff/smc/teaching/clips/vol1/vol1-12.9.html#Heading306 """ theWme = self.semplify(theEnv, theWme, WME, ("1", "fact-address")) return types.Integer(theWme.factId) FactIndex.DEFINITION = FunctionDefinition( "?SYSTEM?", "fact-index", FactIndex(), (WME, types.Integer), FactIndex.do, [Constraint_ExactArgsLength(1), Constraint_ArgType(WME, 0)], forward=False)
''' Created on 05/ago/2012 @author: Francesco Capozzo ''' from myclips.functions.predicate._TypeTesting import _TypeTesting import myclips.parser.Types as types from myclips.FunctionsManager import Constraint_ExactArgsLength, FunctionDefinition class Integerp(_TypeTesting): ''' The integerp function returns the symbol TRUE if its argument is a Integer, otherwise it returns the symbol FALSE. @see: http://www.comp.rgu.ac.uk/staff/smc/teaching/clips/vol1/vol1-12.1.html#Heading188 ''' pass Integerp.DEFINITION = FunctionDefinition("?SYSTEM?", "integerp", Integerp(types.Integer), types.Symbol, Integerp.do, [Constraint_ExactArgsLength(1)], forward=False)
def do(self, theEnv, theFirst, theSecond, *args, **kargs): """ handler of the function @see: http://www.comp.rgu.ac.uk/staff/smc/teaching/clips/vol1/vol1-12.5.html#Heading277 """ theFirst = self.semplify(theEnv, theSecond, types.Number, ("2", "integer or float")) theSecond = self.semplify(theEnv, theSecond, types.Number, ("2", "integer or float")) theReturnClass = types.Integer if isinstance( theFirst, types.Integer) and isinstance( theSecond, types.Integer) else types.Float theMod = self.resolve(theEnv, theFirst) % self.resolve( theEnv, theSecond) return theReturnClass(theMod) Modulus.DEFINITION = FunctionDefinition( "?SYSTEM?", "mod", Modulus(), types.Number, Modulus.do, [Constraint_ExactArgsLength(2), Constraint_ArgType(types.Number)], forward=False)
class Halt(Function): ''' The halt function may be used on the RHS of a rule to prevent further rule firing. It is called without arguments. After halt is called, control is returned from the run command. The agenda is left intact, and execution may be continued with a run command. This function has no return value. @see http://www.comp.rgu.ac.uk/staff/smc/teaching/clips/vol1/vol1-13.7.html#Heading451 ''' def __init__(self, *args, **kwargs): Function.__init__(self, *args, **kwargs) def do(self, theEnv, *args, **kargs): """ function handler implementation @see http://www.comp.rgu.ac.uk/staff/smc/teaching/clips/vol1/vol1-13.7.html#Heading451 """ raise HaltException Halt.DEFINITION = FunctionDefinition( "?SYSTEM?", "halt", Halt(), (types.Lexeme, types.Symbol, types.String, types.Number, types.Integer, types.Float, list, types.NullValue, WME), Halt.do, [Constraint_ExactArgsLength(0)], forward=False)
def __init__(self, functionName, modulesManager, params=None, actions=None, comment=None): functionName = HasScope.cleanName(functionName.evaluate( ) if isinstance(functionName, BaseParsedType) else functionName) ParsedType.__init__(self, functionName) HasScope.__init__(self, modulesManager) self.functionName = functionName self.comment = comment.evaluate().strip('"') if isinstance( comment, BaseParsedType) else comment self.params = [] if params is None else params self.actions = [] if actions is None else actions self.functionDefinition = None # lets create the function definition if self.scope.functions.has(functionName): # has function return True # is exists a definition # and it is not a forward declaration # system functions aren't always forwards # valutate systemFunction only for # custom error if self.scope.functions.hasSystemFunction(functionName): raise TypeInstanceCreationError( "Deffunctions are not allowed to replace external functions: %s" % functionName) else: raise TypeInstanceCreationError( "Cannot define deffunction %s because of an import/export conflict" % functionName) # generate constraints from params constraints = [] # args length minParams = len( [x for x in self.params if isinstance(x, SingleFieldVariable)]) hasMax = not isinstance(self.params[-1], MultiFieldVariable) if hasMax: constraints.append(Constraint_ExactArgsLength(minParams)) else: constraints.append(Constraint_MinArgsLength(minParams)) from myclips.functions.UserFunction import UserFunction functionHandler = UserFunction(self.params, self.actions) fDef = FunctionDefinition( self.scope.moduleName, functionName, linkedType=functionHandler, returnTypes=(self.actions[-1].funcDefinition.returnTypes if len(self.actions) > 0 and isinstance(self.actions[-1], FunctionCall) else self.actions[-1].__class__ if isinstance( self.actions[-1], BaseParsedType) else (Lexeme, Number, list, WME)), handler=functionHandler.do, constraints=constraints, forward=True) functionHandler.DEFINITION = fDef self.scope.functions.addDefinition(fDef)