From 53f9c05823beb0fe9689b7d5dc110889d77c2aba Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Sat, 17 Feb 2018 16:12:17 +0100 Subject: Cleanup --- .../searchlib/rankingexpression/Reference.java | 37 ++++++++++++++++------ .../rankingexpression/rule/ReferenceNode.java | 21 +++--------- 2 files changed, 31 insertions(+), 27 deletions(-) (limited to 'searchlib') diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/Reference.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/Reference.java index 05562f9c933..6277721e8f5 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/Reference.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/Reference.java @@ -2,10 +2,13 @@ package com.yahoo.searchlib.rankingexpression; import com.yahoo.searchlib.rankingexpression.rule.Arguments; +import com.yahoo.searchlib.rankingexpression.rule.CompositeNode; import com.yahoo.searchlib.rankingexpression.rule.ExpressionNode; import com.yahoo.searchlib.rankingexpression.rule.ReferenceNode; +import com.yahoo.searchlib.rankingexpression.rule.SerializationContext; import com.yahoo.tensor.evaluation.TypeContext; +import java.util.Deque; import java.util.Objects; import java.util.Optional; import java.util.stream.Collectors; @@ -20,7 +23,9 @@ public class Reference extends TypeContext.Name { private final String name; private final Arguments arguments; - /** The output, or null if none */ + /** + * The output, or null if none + */ private final String output; public Reference(String name, Arguments arguments, String output) { @@ -33,10 +38,14 @@ public class Reference extends TypeContext.Name { } public String name() { return name; } + public Arguments arguments() { return arguments; } + public String output() { return output; } - /** Creates a reference to a simple feature consisting of a name and a single argument */ + /** + * Creates a reference to a simple feature consisting of a name and a single argument + */ public static Reference simple(String name, String argumentValue) { return new Reference(name, new Arguments(new ReferenceNode(argumentValue)), @@ -62,7 +71,9 @@ public class Reference extends TypeContext.Name { return Optional.of(simple(featureName, argument)); } - /** Returns whether this is a simple identifier - no arguments or output */ + /** + * Returns whether this is a simple identifier - no arguments or output + */ public boolean isIdentifier() { return this.arguments.expressions().size() == 0 && output == null; } @@ -78,11 +89,11 @@ public class Reference extends TypeContext.Name { @Override public boolean equals(Object o) { if (o == this) return true; - if ( ! (o instanceof Reference)) return false; - Reference other = (Reference)o; - if ( ! Objects.equals(other.name, this.name)) return false; - if ( ! Objects.equals(other.arguments, this.arguments)) return false; - if ( ! Objects.equals(other.output, this.output)) return false; + if (!(o instanceof Reference)) return false; + Reference other = (Reference) o; + if (!Objects.equals(other.name, this.name)) return false; + if (!Objects.equals(other.arguments, this.arguments)) return false; + if (!Objects.equals(other.output, this.output)) return false; return true; } @@ -93,10 +104,16 @@ public class Reference extends TypeContext.Name { @Override public String toString() { + return toString(new SerializationContext(), null, null); + } + + public String toString(SerializationContext context, Deque path, CompositeNode parent) { StringBuilder b = new StringBuilder(name); if (arguments != null && arguments.expressions().size() > 0) - b.append("(").append(arguments.expressions().stream().map(ExpressionNode::toString).collect(Collectors.joining(","))).append(")"); - if (output !=null) + b.append("(").append(arguments.expressions().stream() + .map(node -> node.toString(context, path, parent)) + .collect(Collectors.joining(","))).append(")"); + if (output != null) b.append(".").append(output); return b.toString(); } diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/ReferenceNode.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/ReferenceNode.java index 5605b8b8963..ca6d8aa7104 100755 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/ReferenceNode.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/ReferenceNode.java @@ -64,9 +64,6 @@ public final class ReferenceNode extends CompositeNode { @Override public String toString(SerializationContext context, Deque path, CompositeNode parent) { - if (path == null) - path = new ArrayDeque<>(); - if (reference.isIdentifier() && context.getBinding(getName()) != null) { // a bound identifier: replace by the value it is bound to return context.getBinding(getName()); @@ -75,6 +72,8 @@ public final class ReferenceNode extends CompositeNode { 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); @@ -85,19 +84,8 @@ public final class ReferenceNode extends CompositeNode { return "rankingExpression(" + instance.getName() + ")"; } - StringBuilder ret = new StringBuilder(getName()); - if (getArguments().size() > 0) { - ret.append("("); - for (int i = 0; i < getArguments().size(); ++i) { - ret.append(getArguments().expressions().get(i).toString(context, path, this)); - if (i < getArguments().size() - 1) { - ret.append(","); - } - } - ret.append(")"); - } - ret.append(getOutput() != null ? "." + getOutput() : ""); - return ret.toString(); + // not resolved in this context: output as-is + return reference.toString(context, path, parent); } /** Returns the reference of this node */ @@ -114,7 +102,6 @@ public final class ReferenceNode extends CompositeNode { @Override public Value evaluate(Context context) { // TODO: Context should accept a Reference instead. - if (reference.isIdentifier()) return context.get(reference.name()); return context.get(getName(), getArguments(), getOutput()); -- cgit v1.2.3