def isValid(self, token, wme): reference = self._reference assert isinstance(reference, VariableReference) try: # if token relative index is 0, then the test is an intra-element # test performed in the beta network # this means that the wme where the variable was found first # is the same where the variable was found again if reference.relPatternIndex != 0: nToken = getTokenAnchestor( token, (-1 * reference.relPatternIndex) - 1) # get the exact wme value of the token where variable for used first valueInTokenWme = reference.reference.toValue(nToken.wme) else: valueInTokenWme = reference.reference.toValue(wme) # get the value in current wme there variable must have the same value valueInWme = reference.toValue(wme) # when i've found them all # i can compare them # for eq or neq based on reference.isNegative value eqResult = (valueInTokenWme == valueInWme) return eqResult if reference.isNegative is not True else not eqResult except KeyError: # it's ok. If a catch this exception # means that the wme has not an index at all # so no value can be tested. # This make the test fail return False except Exception, e: # Another type of exception catch # better log this myclips.logger.warning( "Unexpected exception caught in %s: token=%s, wme=%s, exception=%s", self, token, wme, repr(e)) # anyway test failed return False
def isValid(self, token, wme): reference = self._reference assert isinstance(reference, VariableReference) try: # if token relative index is 0, then the test is an intra-element # test performed in the beta network # this means that the wme where the variable was found first # is the same where the variable was found again if reference.relPatternIndex != 0: nToken = getTokenAnchestor(token, (-1 * reference.relPatternIndex) - 1) # get the exact wme value of the token where variable for used first valueInTokenWme = reference.reference.toValue(nToken.wme) else: valueInTokenWme = reference.reference.toValue(wme) # get the value in current wme there variable must have the same value valueInWme = reference.toValue(wme) # when i've found them all # i can compare them # for eq or neq based on reference.isNegative value eqResult = valueInTokenWme == valueInWme return eqResult if reference.isNegative is not True else not eqResult except KeyError: # it's ok. If a catch this exception # means that the wme has not an index at all # so no value can be tested. # This make the test fail return False except Exception, e: # Another type of exception catch # better log this myclips.logger.warning( "Unexpected exception caught in %s: token=%s, wme=%s, exception=%s", self, token, wme, repr(e) ) # anyway test failed return False
def isValid(self, token, wme): """ Evaluate the token and check if constraints are valid @param token: a token @type token: Token @param wme: NONE! Test-CE tests are not performed with a wme from right @type wme: None """ try: # replace all fake variables with their real values # from the token varValues = {} for fakeName, reference in self.references.items(): # if token relative index is 0, then the test is an intra-element # test performed in the beta network # this means that the wme where the variable was found first # is the same where the variable was found again if reference.relPatternIndex != 0: nToken = getTokenAnchestor(token, (-1 * reference.relPatternIndex) - 1) # get the exact wme value of the token where variable for used first valueInTokenWme = reference.reference.toValue(nToken.wme) else: valueInTokenWme = reference.reference.toValue(wme) # get the value in current wme there variable must have the same value varValues[fakeName] = valueInTokenWme # create a new FunctionEnv for function execution # network, modulesManager and RESOURCES are not valid values # because all functions that use knoledge about the network configuration # have to raise exception if called from myclips.functions import FunctionEnv theEnv = FunctionEnv(varValues, None, None, None) # execute the function and get back the result theReturnValue = self.function.funcDefinition.linkedType.__class__.execute( self.function.funcDefinition.linkedType, theEnv, *(self.function.funcArgs) ) if isinstance(theReturnValue, types.Symbol) and theReturnValue.pyEqual("FALSE"): return False else: return True except KeyError: # it's ok. If a catch this exception # means that the wme has not an index at all # so no value can be tested. # This make the test fail return False except Exception, e: # Another type of exception catch # better log this myclips.logger.warning( "Unexpected exception catch in %s: token=%s, wme=%s, exception=%s", self, token, wme, repr(e) ) # import traceback # import sys # traceback.print_stack(file=sys.stderr) # anyway test failed return False
def isValid(self, token, wme): ''' Evaluate the token and check if constraints are valid @param token: a token @type token: Token @param wme: NONE! Test-CE tests are not performed with a wme from right @type wme: None ''' try: # replace all fake variables with their real values # from the token varValues = {} for fakeName, reference in self.references.items(): # if token relative index is 0, then the test is an intra-element # test performed in the beta network # this means that the wme where the variable was found first # is the same where the variable was found again if reference.relPatternIndex != 0: nToken = getTokenAnchestor( token, (-1 * reference.relPatternIndex) - 1) # get the exact wme value of the token where variable for used first valueInTokenWme = reference.reference.toValue(nToken.wme) else: valueInTokenWme = reference.reference.toValue(wme) # get the value in current wme there variable must have the same value varValues[fakeName] = valueInTokenWme # create a new FunctionEnv for function execution # network, modulesManager and RESOURCES are not valid values # because all functions that use knoledge about the network configuration # have to raise exception if called from myclips.functions import FunctionEnv theEnv = FunctionEnv(varValues, None, None, None) # execute the function and get back the result theReturnValue = self.function.funcDefinition.linkedType.__class__.execute( self.function.funcDefinition.linkedType, theEnv, *(self.function.funcArgs)) if isinstance(theReturnValue, types.Symbol) and theReturnValue.pyEqual("FALSE"): return False else: return True except KeyError: # it's ok. If a catch this exception # means that the wme has not an index at all # so no value can be tested. # This make the test fail return False except Exception, e: # Another type of exception catch # better log this myclips.logger.warning( "Unexpected exception catch in %s: token=%s, wme=%s, exception=%s", self, token, wme, repr(e)) #import traceback #import sys #traceback.print_stack(file=sys.stderr) # anyway test failed return False