Search in sources :

Example 1 with CompositeNode

use of com.yahoo.searchlib.rankingexpression.rule.CompositeNode in project vespa by vespa-engine.

the class SimplifierTestCase method testParenthesisPreservation.

@Test
public void testParenthesisPreservation() throws ParseException {
    Simplifier s = new Simplifier();
    TransformContext c = new TransformContext(Collections.emptyMap());
    CompositeNode transformed = (CompositeNode) s.transform(new RankingExpression("a + (b + c) / 100000000.0"), c).getRoot();
    assertEquals("a + (b + c) / 100000000.0", transformed.toString());
}
Also used : CompositeNode(com.yahoo.searchlib.rankingexpression.rule.CompositeNode) RankingExpression(com.yahoo.searchlib.rankingexpression.RankingExpression) Test(org.junit.Test)

Example 2 with CompositeNode

use of com.yahoo.searchlib.rankingexpression.rule.CompositeNode in project vespa by vespa-engine.

the class GBDTForestOptimizer method findAndOptimize.

/**
 * Recursively descend and optimize gbdt forest nodes.
 *
 * @return the resulting node, which may be the input node if no optimizations were found
 */
private ExpressionNode findAndOptimize(ExpressionNode node) {
    ExpressionNode newNode = optimize(node);
    // 
    if (!(newNode instanceof CompositeNode))
        return newNode;
    CompositeNode newComposite = (CompositeNode) newNode;
    List<ExpressionNode> newChildren = new ArrayList<>();
    for (ExpressionNode child : newComposite.children()) {
        newChildren.add(findAndOptimize(child));
    }
    return newComposite.setChildren(newChildren);
}
Also used : CompositeNode(com.yahoo.searchlib.rankingexpression.rule.CompositeNode) ExpressionNode(com.yahoo.searchlib.rankingexpression.rule.ExpressionNode) ArrayList(java.util.ArrayList)

Example 3 with CompositeNode

use of com.yahoo.searchlib.rankingexpression.rule.CompositeNode in project vespa by vespa-engine.

the class TensorFlowFeatureConverter method reduceBatchDimensionsAtInput.

private ExpressionNode reduceBatchDimensionsAtInput(ExpressionNode node, TensorFlowModel model, TypeContext<Reference> typeContext) {
    if (node instanceof TensorFunctionNode) {
        TensorFunction tensorFunction = ((TensorFunctionNode) node).function();
        if (tensorFunction instanceof Rename) {
            List<ExpressionNode> children = ((TensorFunctionNode) node).children();
            if (children.size() == 1 && children.get(0) instanceof ReferenceNode) {
                ReferenceNode referenceNode = (ReferenceNode) children.get(0);
                if (model.requiredMacros().containsKey(referenceNode.getName())) {
                    return reduceBatchDimensionExpression(tensorFunction, typeContext);
                }
            }
        }
    }
    if (node instanceof ReferenceNode) {
        ReferenceNode referenceNode = (ReferenceNode) node;
        if (model.requiredMacros().containsKey(referenceNode.getName())) {
            return reduceBatchDimensionExpression(TensorFunctionNode.wrapArgument(node), typeContext);
        }
    }
    if (node instanceof CompositeNode) {
        List<ExpressionNode> children = ((CompositeNode) node).children();
        List<ExpressionNode> transformedChildren = new ArrayList<>(children.size());
        for (ExpressionNode child : children) {
            transformedChildren.add(reduceBatchDimensionsAtInput(child, model, typeContext));
        }
        return ((CompositeNode) node).setChildren(transformedChildren);
    }
    return node;
}
Also used : CompositeNode(com.yahoo.searchlib.rankingexpression.rule.CompositeNode) TensorFunctionNode(com.yahoo.searchlib.rankingexpression.rule.TensorFunctionNode) TensorFunction(com.yahoo.tensor.functions.TensorFunction) ReferenceNode(com.yahoo.searchlib.rankingexpression.rule.ReferenceNode) ExpressionNode(com.yahoo.searchlib.rankingexpression.rule.ExpressionNode) ArrayList(java.util.ArrayList) Rename(com.yahoo.tensor.functions.Rename)

Example 4 with CompositeNode

use of com.yahoo.searchlib.rankingexpression.rule.CompositeNode in project vespa by vespa-engine.

the class RankingExpressionTestCase method testIsNan.

@Test
public void testIsNan() throws ParseException {
    String strExpr = "if (isNan(attribute(foo)) == 1.0, 1.0, attribute(foo))";
    RankingExpression expr = new RankingExpression(strExpr);
    CompositeNode root = (CompositeNode) expr.getRoot();
    CompositeNode comparison = (CompositeNode) root.children().get(0);
    ExpressionNode isNan = comparison.children().get(0);
    assertTrue(isNan instanceof FunctionNode);
    assertEquals("isNan(attribute(foo))", isNan.toString());
}
Also used : CompositeNode(com.yahoo.searchlib.rankingexpression.rule.CompositeNode) ExpressionNode(com.yahoo.searchlib.rankingexpression.rule.ExpressionNode) FunctionNode(com.yahoo.searchlib.rankingexpression.rule.FunctionNode) TensorFunctionNode(com.yahoo.searchlib.rankingexpression.rule.TensorFunctionNode) Test(org.junit.Test)

Example 5 with CompositeNode

use of com.yahoo.searchlib.rankingexpression.rule.CompositeNode in project vespa by vespa-engine.

the class AbstractArrayContext method extractVariables.

private void extractVariables(ExpressionNode node, Set<String> variables) {
    if (node instanceof ReferenceNode) {
        ReferenceNode fNode = (ReferenceNode) node;
        if (fNode.getArguments().expressions().size() > 0)
            throw new UnsupportedOperationException("Array lookup is not supported with features having arguments)");
        variables.add(fNode.toString());
    } else if (node instanceof CompositeNode) {
        CompositeNode cNode = (CompositeNode) node;
        for (ExpressionNode child : cNode.children()) extractVariables(child, variables);
    }
}
Also used : CompositeNode(com.yahoo.searchlib.rankingexpression.rule.CompositeNode) ReferenceNode(com.yahoo.searchlib.rankingexpression.rule.ReferenceNode) ExpressionNode(com.yahoo.searchlib.rankingexpression.rule.ExpressionNode)

Aggregations

CompositeNode (com.yahoo.searchlib.rankingexpression.rule.CompositeNode)5 ExpressionNode (com.yahoo.searchlib.rankingexpression.rule.ExpressionNode)4 ReferenceNode (com.yahoo.searchlib.rankingexpression.rule.ReferenceNode)2 TensorFunctionNode (com.yahoo.searchlib.rankingexpression.rule.TensorFunctionNode)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 RankingExpression (com.yahoo.searchlib.rankingexpression.RankingExpression)1 FunctionNode (com.yahoo.searchlib.rankingexpression.rule.FunctionNode)1 Rename (com.yahoo.tensor.functions.Rename)1 TensorFunction (com.yahoo.tensor.functions.TensorFunction)1