Exemple #1
0
def _analyzeTerm(atomLocation, aTerm, variables, inPatternVariables):
    '''
    Analyze a types.Term and create a list of pattern tests and join tests
    to describe the term
    
    @param atomLocation: the location of the term inside the LHS
    @param aTerm: a types.Term element
    @param variables: a dict of varName => VarLocation(s) in previous patterns
    @param inPatternVariables: a list of VarLocations in the current pattern
    '''
    

    alphaTests = []
    joinTests = []
    
    isNegative = True if isinstance(aTerm, types.NegativeTerm) else False
    # dewrap: aTerm from a Term object to Term's content 
    aTerm = aTerm.term
    
    if isinstance(aTerm, (types.MultiFieldVariable, types.SingleFieldVariable)):
        varLocation = VariableLocation.fromAtomLocation(aTerm.evaluate(), atomLocation)
        # check the main location of the variable
        # but if it's the same of this location, ignore the cc
        mainReference = getVar(aTerm.evaluate(), variables, inPatternVariables)
        if mainReference is not False:
            # create a reference to this variable
            varReference = VariableReference()
            varLocation.toVarReference(varReference)
            varReference.reference = mainReference
            varReference.isNegative = isNegative
            # calcolate the relPatternIndex
            if varLocation.patternIndex is not None:
                if mainReference.patternIndex is not None:
                    varReference.relPatternIndex = mainReference.patternIndex - varLocation.patternIndex
                else:
                    # TODO verificare!!!!
                    varReference.relPatternIndex =  0 - varLocation.patternIndex
            else:
                varReference.relPatternIndex = 0
            
            # make a new join test
            joinTests.append(VariableBindingTest(varReference))
            
        else:
            # unknown variable!
            inPatternVariables.append(varLocation)
        
    elif isinstance(aTerm, types.BaseParsedType):
        # a pattern test is required, but created by _makeAlphaNetwork. so ignore
        # remove patternIndex location from the atomLocation
        # because alpha tests are always on the wme from the right
        atomLocationCopy = copy(atomLocation)
        atomLocationCopy.patternIndex = None
        aAlphaTest = ConstantValueAtIndexTest(atomLocationCopy, aTerm)
        alphaTests.append(aAlphaTest if not isNegative else NegativeAlphaTest(aAlphaTest))
    
    elif isinstance(aTerm, types.FunctionCall):
        # well... this is a special case. This must be converted in a
        # (test (function-call))
        _newFunc, _fakeVar = analyzeFunction(aTerm, atomLocation.patternIndex, variables, inPatternVariables)
        joinTests.append(DynamicFunctionTest(_newFunc, _fakeVar))
        
    # unnamed multifield and single field are ignored
        
    return (alphaTests, joinTests)