Search in sources :

Example 1 with ExpressionFunction

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

the class ReferenceNode method toString.

@Override
public String toString(SerializationContext context, Deque<String> path, CompositeNode parent) {
    // A reference to a macro argument?
    if (reference.isIdentifier() && context.getBinding(getName()) != null) {
        // a bound identifier: replace by the value it is bound to
        return context.getBinding(getName());
    }
    // A reference to a function?
    ExpressionFunction function = context.getFunction(getName());
    if (function != null && function.arguments().size() == getArguments().size() && getOutput() == null) {
        // a function reference: replace by the referenced function wrapped in rankingExpression
        if (path == null)
            path = new ArrayDeque<>();
        String myPath = getName() + getArguments().expressions();
        if (path.contains(myPath))
            throw new IllegalStateException("Cycle in ranking expression function: " + path);
        path.addLast(myPath);
        ExpressionFunction.Instance instance = function.expand(context, getArguments().expressions(), path);
        path.removeLast();
        context.addFunctionSerialization(RankingExpression.propertyName(instance.getName()), instance.getExpressionString());
        return "rankingExpression(" + instance.getName() + ")";
    }
    // Not resolved in this context: output as-is
    return reference.toString(context, path, parent);
}
Also used : ExpressionFunction(com.yahoo.searchlib.rankingexpression.ExpressionFunction) ArrayDeque(java.util.ArrayDeque)

Example 2 with ExpressionFunction

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

the class MapEvaluationTypeContext method getType.

@Override
public TensorType getType(Reference reference) {
    // A reference to a macro argument?
    Optional<String> binding = boundIdentifier(reference);
    if (binding.isPresent()) {
        try {
            // than their string values requires deeper changes
            return new RankingExpression(binding.get()).type(this);
        } catch (ParseException e) {
            throw new IllegalArgumentException(e);
        }
    }
    // A reference to an attribute, query or constant feature?
    if (FeatureNames.isSimpleFeature(reference)) {
        // The argument may be a local identifier bound to the actual value
        String argument = reference.simpleArgument().get();
        reference = Reference.simple(reference.name(), bindings.getOrDefault(argument, argument));
        return featureTypes.getOrDefault(reference, defaultTypeOf(reference));
    }
    // A reference to a function?
    Optional<ExpressionFunction> function = functionInvocation(reference);
    if (function.isPresent()) {
        return function.get().getBody().type(this.withBindings(bind(function.get().arguments(), reference.arguments())));
    }
    // A reference to a feature which returns a tensor?
    Optional<TensorType> featureTensorType = tensorFeatureType(reference);
    if (featureTensorType.isPresent()) {
        return featureTensorType.get();
    }
    // all match features
    return TensorType.empty;
}
Also used : RankingExpression(com.yahoo.searchlib.rankingexpression.RankingExpression) ExpressionFunction(com.yahoo.searchlib.rankingexpression.ExpressionFunction) ParseException(com.yahoo.searchlib.rankingexpression.parser.ParseException) TensorType(com.yahoo.tensor.TensorType)

Aggregations

ExpressionFunction (com.yahoo.searchlib.rankingexpression.ExpressionFunction)2 RankingExpression (com.yahoo.searchlib.rankingexpression.RankingExpression)1 ParseException (com.yahoo.searchlib.rankingexpression.parser.ParseException)1 TensorType (com.yahoo.tensor.TensorType)1 ArrayDeque (java.util.ArrayDeque)1