Exemplo n.º 1
0
                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)


# Function definition

Printout.DEFINITION = FunctionDefinition(
    "?SYSTEM?",
    "printout",
    Printout(),
    types.NullValue,
    Printout.do, [
        Constraint_MinArgsLength(2),
        Constraint_ArgType(types.Symbol, 0),
    ],
    forward=False)
Exemplo n.º 2
0
                values=[
                    self.semplify(theEnv,
                                  v)  # resolve to BaseParsedTypes if needed
                    for v in arg.values
                ],
                moduleName=theEnv.modulesManager.currentScope.moduleName)

        elif isinstance(arg, types.TemplateRhsPattern):
            # convert it in a new Template Fact
            # the fact value is a dict with (slotName, slotValue) where slotValue
            # need to be resolved

            return TemplateFact(
                templateName=arg.templateName,
                values=dict([(v.slotName, self.semplify(theEnv, v.slotValue))
                             for v in arg.templateSlots]),
                moduleName=theEnv.modulesManager.currentScope.templates.
                getDefinition(arg.templateName).moduleName)

        else:
            raise InvalidArgValueError("Invalid fact format")


Assert.DEFINITION = FunctionDefinition("?SYSTEM?",
                                       "assert",
                                       Assert(), (WME, types.Symbol),
                                       Assert.do, [
                                           Constraint_MinArgsLength(1),
                                       ],
                                       forward=False)
Exemplo n.º 3
0
        args = list(args)
        args.reverse()

        try:
            for theModule in args:
                theModule = self.resolve(
                    theEnv,
                    self.semplify(theEnv, theModule, types.Symbol,
                                  ("ALL", "symbol")))

                if not theEnv.modulesManager.isDefined(theModule):
                    raise InvalidArgValueError("Unable to find defmodule %s" %
                                               theModule)
                else:
                    theEnv.network.agenda.focusStack.append(theModule)

            return types.Symbol("TRUE")
        except IndexError:
            return types.Symbol("FALSE")


Focus.DEFINITION = FunctionDefinition(
    "?SYSTEM?",
    "focus",
    Focus(),
    types.Symbol,
    Focus.do, [Constraint_MinArgsLength(1),
               Constraint_ArgType(types.Symbol)],
    forward=False)
Exemplo n.º 4
0
        Function.__init__(self, *args, **kwargs)

    def do(self, theEnv, *args, **kargs):
        """
        handler of the function
        """

        # comparison is done against the <Symbol:TRUE>
        theValue = types.Symbol("TRUE")

        for theArg in args:
            # resolve the real value
            # before comparison
            if isinstance(theArg, (types.FunctionCall, types.Variable)):
                theArg = self.resolve(theEnv, theArg)
            # types + value comparison
            if theValue == theArg:
                return theValue  # return <Symbol:TRUE>

        return types.Symbol("FALSE")


Or.DEFINITION = FunctionDefinition(
    "?SYSTEM?",
    "or",
    Or(),
    types.Symbol,
    Or.do, [Constraint_MinArgsLength(2),
            Constraint_ArgType(types.Symbol)],
    forward=False)
Exemplo n.º 5
0
        theBegin = theMultifield[0:theIndex - 1]
        theEnd = theMultifield[theIndex - 1:None]

        theInner = []
        for theArg in args:
            theArg = self.semplify(
                theEnv, theArg, (list),
                ("1", "number, lexeme, fact-address or multifield"))
            if isinstance(theArg, list):
                theInner += theArg
            else:
                theInner.append(theArg)

        return theBegin + theInner + theEnd


Insert.DEFINITION = FunctionDefinition(
    "?SYSTEM?",
    "insert$",
    Insert(),
    list,
    Insert.do, [
        Constraint_MinArgsLength(3),
        Constraint_ArgType(list, 0),
        Constraint_ArgType(types.Integer, 1),
        Constraint_ArgType((types.Lexeme, types.Number, WME, list), 2),
        Constraint_ArgType((types.Lexeme, types.Number, WME, list), (3, None),
                           failIfMissing=False)
    ],
    forward=False)
Exemplo n.º 6
0
                  (theBegin, theEnd) if theBegin != theEnd else str(theBegin)),
                 len(theMultifield)))
        else:
            theInner = []
            for theArg in args:
                theArg = self.semplify(
                    theEnv, theArg, (list),
                    ("1", "number, lexeme, fact-address or multifield"))
                if isinstance(theArg, list):
                    theInner += theArg
                else:
                    theInner.append(theArg)

            return theBegin + theInner + theEnd


Replace.DEFINITION = FunctionDefinition(
    "?SYSTEM?",
    "replace$",
    Replace(),
    list,
    Replace.do, [
        Constraint_MinArgsLength(4),
        Constraint_ArgType(list, 0),
        Constraint_ArgType(types.Integer, 1),
        Constraint_ArgType(types.Integer, 2),
        Constraint_ArgType((types.Lexeme, types.Number, WME, list), 3),
        Constraint_ArgType((types.Lexeme, types.Number, WME, list), (4, None),
                           failIfMissing=False)
    ],
    forward=False)
Exemplo n.º 7
0
    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)