Search in sources :

Example 31 with Access

use of com.dexels.navajo.script.api.Access in project navajo by Dexels.

the class ASTAndNode method interpretToLambda.

@Override
public ContextExpression interpretToLambda(List<String> problems, String expression, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
    ContextExpression expA = jjtGetChild(0).interpretToLambda(problems, expression, functionClassifier, mapResolver);
    ContextExpression expB = jjtGetChild(1).interpretToLambda(problems, expression, functionClassifier, mapResolver);
    Optional<String> expressionA = expA.returnType();
    checkOrAdd("In AND expression the first expression is not a boolean but a " + expressionA.orElse("<unknown>"), problems, expB.returnType(), Property.BOOLEAN_PROPERTY);
    Optional<String> expressionB = expB.returnType();
    checkOrAdd("In AND expression the second expression is not a boolean but a " + expressionB.orElse("<unknown>"), problems, expB.returnType(), Property.BOOLEAN_PROPERTY);
    return new ContextExpression() {

        @Override
        public Operand apply(Navajo doc, Message parentMsg, Message parentParamMsg, Selection parentSel, MappableTreeNode mapNode, TipiLink tipiLink, Access access, Optional<ImmutableMessage> immutableMessage, Optional<ImmutableMessage> paramMessage) {
            Operand a = expA.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage);
            if (a == null) {
                return Operand.ofBoolean(Boolean.FALSE);
            }
            Boolean ba = (Boolean) a.value;
            if (!(ba.booleanValue())) {
                return Operand.ofBoolean(Boolean.FALSE);
            }
            Operand b = expB.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage);
            if (b == null) {
                return Operand.ofBoolean(Boolean.FALSE);
            }
            Boolean value = (Boolean) b.value;
            return Operand.ofBoolean(value != null ? value : Boolean.FALSE);
        }

        @Override
        public boolean isLiteral() {
            return expA.isLiteral() && expB.isLiteral();
        }

        @Override
        public Optional<String> returnType() {
            return Optional.of(Property.BOOLEAN_PROPERTY);
        }

        @Override
        public String expression() {
            return expression;
        }
    };
}
Also used : MappableTreeNode(com.dexels.navajo.script.api.MappableTreeNode) TipiLink(com.dexels.navajo.expression.api.TipiLink) Message(com.dexels.navajo.document.Message) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) 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)

Example 32 with Access

use of com.dexels.navajo.script.api.Access 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)

Example 33 with Access

use of com.dexels.navajo.script.api.Access in project navajo by Dexels.

the class DomainObjectMapperTest method testStoreWithNonExistingAttributesCheck.

@Test
public void testStoreWithNonExistingAttributesCheck() throws Exception {
    // The other way around, with an input Navajo with property that does
    // have an associated attribute.
    Navajo doc = NavajoFactory.getInstance().createNavajo();
    Message m = NavajoFactory.getInstance().createMessage(doc, "MyMessage");
    doc.addMessage(m);
    Property p1 = NavajoFactory.getInstance().createProperty(doc, "Id", "string", "hello", 0, "", "in");
    Property p2 = NavajoFactory.getInstance().createProperty(doc, "NonExisting", "string", "hello", 0, "", "in");
    m.addProperty(p1);
    m.addProperty(p2);
    Access a = new Access();
    a.setInDoc(doc);
    DomainObjectMapper dom2 = new DomainObjectMapper();
    dom2.setCurrentMessageName("MyMessage");
    dom2.load(a);
    dom2.setObjectName("com.dexels.navajo.mapping.bean.Relation");
    dom2.setIgnoreNonExistingAttributes(true);
    boolean exception = false;
    try {
        dom2.store();
    } catch (Exception e) {
        exception = true;
    }
    // DO NOT Expect exception because Navajo contains property NonExisting
    // that does not exist in domain object and
    // setIgnoreNonExistingAttributes is set.
    assertFalse(exception);
    Object o = dom2.getMyObject();
    assertNotNull(o);
    assertEquals(Relation.class, o.getClass());
    assertEquals("hello", ((Relation) o).getId());
}
Also used : Message(com.dexels.navajo.document.Message) Access(com.dexels.navajo.script.api.Access) Navajo(com.dexels.navajo.document.Navajo) Property(com.dexels.navajo.document.Property) Test(org.junit.Test)

Example 34 with Access

use of com.dexels.navajo.script.api.Access in project navajo by Dexels.

the class DomainObjectMapperTest method testStore.

@Test
public void testStore() throws Exception {
    // With null domain object and null Access object.
    DomainObjectMapper dom2 = new DomainObjectMapper(null);
    dom2.store();
    // With null domain object and an Access object.
    dom2 = new DomainObjectMapper(null);
    dom2.load(new Access());
    boolean exception = false;
    try {
        dom2.store();
    } catch (Exception e) {
        exception = true;
    }
    assertTrue(exception);
    // With some domain object and an Access object
    dom2 = new DomainObjectMapper(new Relation());
    dom2.load(new Access());
    exception = false;
    try {
        dom2.store();
    } catch (Exception e) {
        exception = true;
    }
    assertTrue(exception);
    // With some domain object and an Access object and a valid Navajo.
    dom2 = new DomainObjectMapper(new Relation());
    Navajo doc = NavajoFactory.getInstance().createNavajo();
    Message m = NavajoFactory.getInstance().createMessage(doc, "MyMessage");
    doc.addMessage(m);
    Access a = new Access();
    a.setOutputDoc(doc);
    a.setCurrentOutMessage(m);
    dom2.load(a);
    dom2.store();
    assertNotNull(doc.getProperty("/MyMessage/Id"));
    // The other way around, with an input Navajo.
    doc = NavajoFactory.getInstance().createNavajo();
    m = NavajoFactory.getInstance().createMessage(doc, "MyMessage");
    doc.addMessage(m);
    Property p = NavajoFactory.getInstance().createProperty(doc, "Id", "string", "hello", 0, "", "in");
    m.addProperty(p);
    a = new Access();
    a.setInDoc(doc);
    dom2 = new DomainObjectMapper();
    dom2.setCurrentMessageName("MyMessage");
    dom2.load(a);
    dom2.setObjectName("com.dexels.navajo.mapping.bean.Relation");
    dom2.store();
    Object o = dom2.getMyObject();
    assertNotNull(o);
    assertEquals(Relation.class, o.getClass());
    assertEquals("hello", ((Relation) o).getId());
    // The other way around, with an input Navajo with property that does
    // have an associated attribute.
    doc = NavajoFactory.getInstance().createNavajo();
    m = NavajoFactory.getInstance().createMessage(doc, "MyMessage");
    doc.addMessage(m);
    Property p1 = NavajoFactory.getInstance().createProperty(doc, "Id", "string", "hello", 0, "", "in");
    Property p2 = NavajoFactory.getInstance().createProperty(doc, "NonExisting", "string", "hello", 0, "", "in");
    m.addProperty(p1);
    m.addProperty(p2);
    a = new Access();
    a.setInDoc(doc);
    dom2 = new DomainObjectMapper();
    dom2.setCurrentMessageName("MyMessage");
    dom2.load(a);
    dom2.setObjectName("com.dexels.navajo.mapping.bean.Relation");
    exception = false;
    try {
        dom2.store();
    } catch (Exception e) {
        exception = true;
    }
    // Expect exception because Navajo contains property NonExisting that
    // does not exist in domain object.
    assertTrue(exception);
// o = dom2.getMyObject();
// assertNotNull(o);
// assertEquals(Relation.class, o.getClass());
// assertEquals("hello", ((Relation) o).getId());
}
Also used : Message(com.dexels.navajo.document.Message) Access(com.dexels.navajo.script.api.Access) Navajo(com.dexels.navajo.document.Navajo) Property(com.dexels.navajo.document.Property) Test(org.junit.Test)

Example 35 with Access

use of com.dexels.navajo.script.api.Access in project navajo by Dexels.

the class AdminMap method getUsers.

public AccessMap[] getUsers() {
    Set<Access> all = new HashSet<>(com.dexels.navajo.server.DispatcherFactory.getInstance().getAccessSet());
    Iterator<Access> iter = all.iterator();
    List<AccessMap> d = new ArrayList<>();
    while (iter.hasNext()) {
        Access a = iter.next();
        d.add(new AccessMap(a));
    }
    AccessMap[] ams = new AccessMap[d.size()];
    return d.toArray(ams);
}
Also used : Access(com.dexels.navajo.script.api.Access) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Aggregations

Access (com.dexels.navajo.script.api.Access)45 Navajo (com.dexels.navajo.document.Navajo)29 Message (com.dexels.navajo.document.Message)27 Test (org.junit.Test)18 Property (com.dexels.navajo.document.Property)15 Selection (com.dexels.navajo.document.Selection)14 ImmutableMessage (com.dexels.immutable.api.ImmutableMessage)12 ContextExpression (com.dexels.navajo.expression.api.ContextExpression)12 TipiLink (com.dexels.navajo.expression.api.TipiLink)12 MappableTreeNode (com.dexels.navajo.script.api.MappableTreeNode)12 Optional (java.util.Optional)12 Operand (com.dexels.navajo.document.Operand)10 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)10 ArrayList (java.util.ArrayList)8 UserException (com.dexels.navajo.script.api.UserException)6 ResultMessage (com.dexels.navajo.adapter.messagemap.ResultMessage)5 Binary (com.dexels.navajo.document.types.Binary)4 HttpResource (com.dexels.navajo.resource.http.HttpResource)4 HttpResourceFactory (com.dexels.navajo.resource.http.HttpResourceFactory)4 Dispatcher (com.dexels.navajo.server.Dispatcher)4