Search in sources :

Example 71 with FunctionInterface

use of com.dexels.navajo.expression.api.FunctionInterface in project navajo by Dexels.

the class StandardFunctionsTest method testXmlUnescape.

@Test
public void testXmlUnescape() {
    FunctionInterface fi = fff.getInstance(cl, "XmlUnescape");
    fi.reset();
    fi.insertStringOperand("<><>");
    String actual = (String) fi.evaluate();
    assertNotNull(actual);
}
Also used : FunctionInterface(com.dexels.navajo.expression.api.FunctionInterface) CapString(com.dexels.navajo.functions.CapString) Test(org.junit.Test)

Example 72 with FunctionInterface

use of com.dexels.navajo.expression.api.FunctionInterface in project navajo by Dexels.

the class FunctionFactoryInterface method getInstance.

// public void clearAdapterNames() {
// adapterConfig.clear();
// }
@SuppressWarnings("unchecked")
public FunctionInterface getInstance(final ClassLoader cl, final String functionName) {
    try {
        FunctionDefinition fd = getDef(functionName);
        if (fd == null) {
            logger.error("Missing function definition: {}", functionName);
            return null;
        }
        Class<FunctionInterface> myClass = (Class<FunctionInterface>) Class.forName(fd.getObject(), true, cl);
        FunctionInterface fi = myClass.getDeclaredConstructor().newInstance();
        fi.setDefinition(fd);
        if (!fi.isInitialized()) {
            fi.setTypes(fd.getInputParams(), fd.getResultParam());
        }
        return fi;
    } catch (Exception e) {
        logger.error("Function: " + functionName + " not found!", e);
        return null;
    }
}
Also used : FunctionInterface(com.dexels.navajo.expression.api.FunctionInterface) FunctionDefinition(com.dexels.navajo.expression.api.FunctionDefinition) InvocationTargetException(java.lang.reflect.InvocationTargetException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) UserException(com.dexels.navajo.script.api.UserException)

Example 73 with FunctionInterface

use of com.dexels.navajo.expression.api.FunctionInterface in project navajo by Dexels.

the class OSGiFunctionFactoryFactory method getFunctionInterface.

public static FunctionInterface getFunctionInterface(final String functionName) {
    if (cache.containsKey(functionName)) {
        return cache.get(functionName).getFunctionInstance();
    }
    FunctionDefinition fd = (FunctionDefinition) getComponent(functionName, "functionName", FunctionDefinition.class);
    if (fd == null) {
        throw NavajoFactory.getInstance().createNavajoException("No such function: " + functionName);
    }
    cache.put(functionName, fd);
    FunctionInterface instance = fd.getFunctionInstance();
    instance.setDefinition(fd);
    return instance;
}
Also used : FunctionInterface(com.dexels.navajo.expression.api.FunctionInterface) FunctionDefinition(com.dexels.navajo.expression.api.FunctionDefinition)

Example 74 with FunctionInterface

use of com.dexels.navajo.expression.api.FunctionInterface in project navajo by Dexels.

the class OsgiFunctionFactory method getInstance.

@Override
public FunctionInterface getInstance(final ClassLoader cl, final String functionName) {
    FunctionDefinition fd = (FunctionDefinition) getComponent(functionName, "functionName", FunctionDefinition.class);
    if (fd == null) {
        logger.debug("OSGi function resolution for function: {} failed, going old school.", functionName);
        return super.getInstance(cl, functionName);
    }
    FunctionInterface osgiResolution = fd.getFunctionInstance();
    if (osgiResolution == null) {
        logger.debug("OSGi function resolution for function: {} failed, going old school.", functionName);
        return super.getInstance(cl, functionName);
    }
    return osgiResolution;
}
Also used : FunctionInterface(com.dexels.navajo.expression.api.FunctionInterface) FunctionDefinition(com.dexels.navajo.expression.api.FunctionDefinition)

Example 75 with FunctionInterface

use of com.dexels.navajo.expression.api.FunctionInterface in project navajo by Dexels.

the class ASTFunctionNode method resolveNormalFunction.

private ContextExpression resolveNormalFunction(List<ContextExpression> l, Map<String, ContextExpression> named, List<String> problems, String expression) {
    FunctionInterface typeCheckInstance = getFunction();
    if (typeCheckInstance == null) {
        throw new NullPointerException("Function: " + functionName + " can not be resolved!");
    }
    try {
        List<String> typeProblems = typeCheckInstance.typeCheck(l, expression);
        if (!typeProblems.isEmpty() && RuntimeConfig.STRICT_TYPECHECK.getValue() != null) {
            problems.addAll(typeProblems);
        }
    } catch (Throwable e2) {
        typechecklogger.error("Typechecker itself failed when parsing: " + expression + " function definition: " + typeCheckInstance + " Error: ", e2);
    }
    boolean isImmutable = typeCheckInstance.isPure() && l.stream().allMatch(e -> e.isLiteral());
    ContextExpression dynamic = new ContextExpression() {

        @Override
        public boolean isLiteral() {
            // TODO also check named params
            return isImmutable;
        }

        @Override
        public Operand apply(Navajo doc, Message parentMsg, Message parentParamMsg, Selection parentSel, MappableTreeNode mapNode, TipiLink tipiLink, Access access, Optional<ImmutableMessage> immutableMessage, Optional<ImmutableMessage> paramMessage) {
            FunctionInterface f = getFunction();
            Map<String, Operand> resolvedNamed = named.entrySet().stream().collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage)));
            f.setInMessage(doc);
            f.setNamedParameter(resolvedNamed);
            f.setCurrentMessage(parentMsg);
            f.setAccess(access);
            f.reset();
            l.stream().map(e -> {
                try {
                    Operand evaluated = e.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage);
                    if (evaluated == null) {
                        logger.warn("Problematic expression returned null object. If you really insist, return an Operand.NULL. Evaluating expression: {}", expression);
                    }
                    return evaluated;
                } catch (TMLExpressionException e1) {
                    throw new TMLExpressionException("Error parsing parameters for function: " + functionName, e1);
                }
            }).forEach(e -> f.insertOperand(e));
            return f.evaluateWithTypeCheckingOperand();
        }

        @Override
        public Optional<String> returnType() {
            return typeCheckInstance.getReturnType();
        }

        @Override
        public String expression() {
            return expression;
        }
    };
    if (isImmutable && CacheSubexpression.getCacheSubExpression()) {
        Optional<String> returnType = dynamic.returnType();
        String immutablExpression = dynamic.expression();
        Operand resolved = dynamic.apply();
        return new ContextExpression() {

            @Override
            public Optional<String> returnType() {
                return returnType;
            }

            @Override
            public boolean isLiteral() {
                return true;
            }

            @Override
            public String expression() {
                return immutablExpression;
            }

            @Override
            public Operand apply(Navajo doc, Message parentMsg, Message parentParamMsg, Selection parentSel, MappableTreeNode mapNode, TipiLink tipiLink, Access access, Optional<ImmutableMessage> immutableMessage, Optional<ImmutableMessage> paramMessage) {
                return resolved;
            }
        };
    } else {
        return dynamic;
    }
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Function(java.util.function.Function) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) Operand(com.dexels.navajo.document.Operand) Selection(com.dexels.navajo.document.Selection) Map(java.util.Map) OSGiFunctionFactoryFactory(com.dexels.navajo.functions.util.OSGiFunctionFactoryFactory) LinkedList(java.util.LinkedList) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) ReactiveParseItem(com.dexels.navajo.parser.compiled.api.ReactiveParseItem) Navajo(com.dexels.navajo.document.Navajo) DispatcherFactory(com.dexels.navajo.server.DispatcherFactory) TipiLink(com.dexels.navajo.expression.api.TipiLink) Logger(org.slf4j.Logger) FunctionInterface(com.dexels.navajo.expression.api.FunctionInterface) AbstractVersion(com.dexels.navajo.version.AbstractVersion) FunctionFactoryFactory(com.dexels.navajo.functions.util.FunctionFactoryFactory) Access(com.dexels.navajo.script.api.Access) RuntimeConfig(com.dexels.navajo.runtime.config.RuntimeConfig) Message(com.dexels.navajo.document.Message) Reactive(com.dexels.navajo.reactive.api.Reactive) Collectors(java.util.stream.Collectors) ContextExpression(com.dexels.navajo.expression.api.ContextExpression) List(java.util.List) FunctionClassification(com.dexels.navajo.expression.api.FunctionClassification) NamedExpression(com.dexels.navajo.parser.NamedExpression) CacheSubexpression(com.dexels.navajo.parser.compiled.api.CacheSubexpression) Optional(java.util.Optional) MappableTreeNode(com.dexels.navajo.script.api.MappableTreeNode) MappableTreeNode(com.dexels.navajo.script.api.MappableTreeNode) FunctionInterface(com.dexels.navajo.expression.api.FunctionInterface) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Message(com.dexels.navajo.document.Message) Optional(java.util.Optional) Selection(com.dexels.navajo.document.Selection) Operand(com.dexels.navajo.document.Operand) ContextExpression(com.dexels.navajo.expression.api.ContextExpression) Access(com.dexels.navajo.script.api.Access) Navajo(com.dexels.navajo.document.Navajo) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) TipiLink(com.dexels.navajo.expression.api.TipiLink)

Aggregations

FunctionInterface (com.dexels.navajo.expression.api.FunctionInterface)161 Test (org.junit.Test)153 Navajo (com.dexels.navajo.document.Navajo)26 FunctionDefinition (com.dexels.navajo.expression.api.FunctionDefinition)20 ArrayList (java.util.ArrayList)16 Date (java.util.Date)16 Operand (com.dexels.navajo.document.Operand)10 GiveLongTestFunction (com.dexels.navajo.expression.compiled.GiveLongTestFunction)10 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)7 CapString (com.dexels.navajo.functions.CapString)7 StringReader (java.io.StringReader)5 ContextExpression (com.dexels.navajo.expression.api.ContextExpression)4 AddTestFunction (com.dexels.navajo.expression.compiled.AddTestFunction)4 Binary (com.dexels.navajo.document.types.Binary)3 CheckInteger (com.dexels.navajo.functions.CheckInteger)3 SimpleDateFormat (java.text.SimpleDateFormat)3 List (java.util.List)3 Message (com.dexels.navajo.document.Message)2 Property (com.dexels.navajo.document.Property)2 ClockTime (com.dexels.navajo.document.types.ClockTime)2