diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2017-06-14 14:41:18 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2017-06-14 14:41:18 +0200 |
commit | 6ff3df19226036b8ee1bb559f9d73cab40e8d2a0 (patch) | |
tree | 355a7b0623b58983ba655b868341fe479a22eb3d /searchlib/src/main/java/com | |
parent | b7f9e7ceaef72489d76683537973b639f8895b84 (diff) |
Remove carriage return
Diffstat (limited to 'searchlib/src/main/java/com')
18 files changed, 719 insertions, 719 deletions
diff --git a/searchlib/src/main/java/com/yahoo/searchlib/expression/DivideFunctionNode.java b/searchlib/src/main/java/com/yahoo/searchlib/expression/DivideFunctionNode.java index c7ff00a9e78..0a7b36cfd71 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/expression/DivideFunctionNode.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/expression/DivideFunctionNode.java @@ -20,4 +20,4 @@ public class DivideFunctionNode extends NumericFunctionNode { protected void onArgument(final ResultNode arg, ResultNode result) { ((NumericResultNode)result).divide(arg); } -}
\ No newline at end of file +} diff --git a/searchlib/src/main/java/com/yahoo/searchlib/expression/Int16ResultNode.java b/searchlib/src/main/java/com/yahoo/searchlib/expression/Int16ResultNode.java index 4a636bb8a3f..804b6a52c5b 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/expression/Int16ResultNode.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/expression/Int16ResultNode.java @@ -146,4 +146,4 @@ public class Int16ResultNode extends NumericResultNode { public void set(ResultNode rhs) { value = (short)rhs.getInteger(); } -}
\ No newline at end of file +} diff --git a/searchlib/src/main/java/com/yahoo/searchlib/expression/Int32ResultNode.java b/searchlib/src/main/java/com/yahoo/searchlib/expression/Int32ResultNode.java index 0316ee2d9c3..9a03df015b8 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/expression/Int32ResultNode.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/expression/Int32ResultNode.java @@ -146,4 +146,4 @@ public class Int32ResultNode extends NumericResultNode { public void set(ResultNode rhs) { value = (int)rhs.getInteger(); } -}
\ No newline at end of file +} diff --git a/searchlib/src/main/java/com/yahoo/searchlib/expression/Int8ResultNode.java b/searchlib/src/main/java/com/yahoo/searchlib/expression/Int8ResultNode.java index 0bbc3414c2e..d21473b9be8 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/expression/Int8ResultNode.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/expression/Int8ResultNode.java @@ -146,4 +146,4 @@ public class Int8ResultNode extends NumericResultNode { public void set(ResultNode rhs) { value = (byte)rhs.getInteger(); } -}
\ No newline at end of file +} diff --git a/searchlib/src/main/java/com/yahoo/searchlib/expression/NumElemFunctionNode.java b/searchlib/src/main/java/com/yahoo/searchlib/expression/NumElemFunctionNode.java index 102c598402a..c7897afd514 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/expression/NumElemFunctionNode.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/expression/NumElemFunctionNode.java @@ -47,4 +47,4 @@ public class NumElemFunctionNode extends UnaryFunctionNode { protected boolean equalsUnaryFunction(UnaryFunctionNode obj) { return true; } -}
\ No newline at end of file +} diff --git a/searchlib/src/main/java/com/yahoo/searchlib/mlr/ga/KeyboardChecker.java b/searchlib/src/main/java/com/yahoo/searchlib/mlr/ga/KeyboardChecker.java index 091990ffdc5..6d47b2dfd7a 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/mlr/ga/KeyboardChecker.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/mlr/ga/KeyboardChecker.java @@ -47,4 +47,4 @@ public class KeyboardChecker { } } -}
\ No newline at end of file +} diff --git a/searchlib/src/main/java/com/yahoo/searchlib/ranking/features/fieldmatch/SegmentStartPoint.java b/searchlib/src/main/java/com/yahoo/searchlib/ranking/features/fieldmatch/SegmentStartPoint.java index c7331a0b5b6..15e458dea70 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/ranking/features/fieldmatch/SegmentStartPoint.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/ranking/features/fieldmatch/SegmentStartPoint.java @@ -142,4 +142,4 @@ final class SegmentStartPoint { " (" + (open ? "open" : "closed") + ")"; } -}
\ No newline at end of file +} diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/ExpressionFunction.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/ExpressionFunction.java index 86ac53a1e44..7e5033844a3 100755 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/ExpressionFunction.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/ExpressionFunction.java @@ -1,139 +1,139 @@ // Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.searchlib.rankingexpression;
-
-import com.google.common.collect.ImmutableList;
-import com.yahoo.searchlib.rankingexpression.rule.ExpressionNode;
-import com.yahoo.searchlib.rankingexpression.rule.SerializationContext;
-import com.yahoo.text.Utf8;
-
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
-import java.util.*;
-
-/**
- * <p>A function defined by a ranking expression</p>
- *
- * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
- * @author bratseth
- */
-public class ExpressionFunction {
-
- private final String name;
- private final ImmutableList<String> arguments;
- private final RankingExpression body;
-
- /**
- * <p>Constructs a new function</p>
- *
- * @param name the name of this function
- * @param arguments its argument names
- * @param body the ranking expression that defines this function
- */
- public ExpressionFunction(String name, List<String> arguments, RankingExpression body) {
- this.name = name;
- this.arguments = arguments==null ? ImmutableList.of() : ImmutableList.copyOf(arguments);
- this.body = body;
- }
-
- public String getName() { return name; }
-
- /** Returns an immutable list of the arguments of this */
- public List<String> arguments() { return arguments; }
-
- public RankingExpression getBody() { return body; }
-
- /**
- * <p>Create and return an instance of this function based on the given
- * arguments. If function calls are nested, this call might produce
- * additional scripts.</p>
- *
- * @param context the context used to expand this
- * @param arguments the arguments to instantiate on.
- * @param path the expansion path leading to this.
- * @return the script function instance created.
- */
- public Instance expand(SerializationContext context, List<ExpressionNode> arguments, Deque<String> path) {
- Map<String, String> argumentBindings = new HashMap<>();
- for (int i = 0; i < this.arguments.size() && i < arguments.size(); ++i) {
- argumentBindings.put(this.arguments.get(i), arguments.get(i).toString(context, path, null));
- }
- return new Instance(toSymbol(argumentBindings), body.getRoot().toString(context.createBinding(argumentBindings), path, null));
- }
-
- /**
- * Returns a symbolic string that represents this function with a given
- * list of arguments. The arguments are mangled by hashing the string
- * representation of the argument expressions, so we might need to revisit
- * this if we start seeing collisions.
- *
- * @param argumentBindings the bound arguments to include in the symbolic name.
- * @return the symbolic name for an instance of this function
- */
- private String toSymbol(Map<String, String> argumentBindings) {
- if (argumentBindings.isEmpty()) return name;
-
- StringBuilder ret = new StringBuilder();
- ret.append(name).append("@");
- for (Map.Entry<String,String> argumentBinding : argumentBindings.entrySet()) {
- ret.append(Long.toHexString(symbolCode(argumentBinding.getKey() + "=" + argumentBinding.getValue())));
- ret.append(".");
- }
- if (ret.toString().endsWith("."))
- ret.setLength(ret.length()-1);
- return ret.toString();
- }
-
-
- /**
- * <p>Returns a more unique hash code than what Java's own {@link
- * String#hashCode()} method would produce.</p>
- *
- * @param str The string to hash.
- * @return A 64 bit long hash code.
- */
- private static long symbolCode(String str) {
- try {
- MessageDigest md = java.security.MessageDigest.getInstance("SHA-1");
- byte[] buf = md.digest(Utf8.toBytes(str));
- if (buf.length >= 8) {
- long ret = 0;
- for (int i = 0; i < 8; ++i) {
- ret = (ret << 8) + (buf[i] & 0xff);
- }
- return ret;
- }
- } catch (NoSuchAlgorithmException e) {
- throw new Error("java must always support SHA-1 message digest format", e);
- }
- return str.hashCode();
- }
-
- @Override
- public String toString() {
- return name;
- }
-
- /**
- * An instance of a serialization of this function, using a particular serialization context (by {@link
- * ExpressionFunction#expand})
- */
- public class Instance {
-
- private final String name;
- private final String expressionString;
-
- public Instance(String name, String expressionString) {
- this.name = name;
- this.expressionString = expressionString;
- }
-
- public String getName() {
- return name;
- }
-
- public String getExpressionString() {
- return expressionString;
- }
-
- }
-}
+package com.yahoo.searchlib.rankingexpression; + +import com.google.common.collect.ImmutableList; +import com.yahoo.searchlib.rankingexpression.rule.ExpressionNode; +import com.yahoo.searchlib.rankingexpression.rule.SerializationContext; +import com.yahoo.text.Utf8; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.*; + +/** + * <p>A function defined by a ranking expression</p> + * + * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a> + * @author bratseth + */ +public class ExpressionFunction { + + private final String name; + private final ImmutableList<String> arguments; + private final RankingExpression body; + + /** + * <p>Constructs a new function</p> + * + * @param name the name of this function + * @param arguments its argument names + * @param body the ranking expression that defines this function + */ + public ExpressionFunction(String name, List<String> arguments, RankingExpression body) { + this.name = name; + this.arguments = arguments==null ? ImmutableList.of() : ImmutableList.copyOf(arguments); + this.body = body; + } + + public String getName() { return name; } + + /** Returns an immutable list of the arguments of this */ + public List<String> arguments() { return arguments; } + + public RankingExpression getBody() { return body; } + + /** + * <p>Create and return an instance of this function based on the given + * arguments. If function calls are nested, this call might produce + * additional scripts.</p> + * + * @param context the context used to expand this + * @param arguments the arguments to instantiate on. + * @param path the expansion path leading to this. + * @return the script function instance created. + */ + public Instance expand(SerializationContext context, List<ExpressionNode> arguments, Deque<String> path) { + Map<String, String> argumentBindings = new HashMap<>(); + for (int i = 0; i < this.arguments.size() && i < arguments.size(); ++i) { + argumentBindings.put(this.arguments.get(i), arguments.get(i).toString(context, path, null)); + } + return new Instance(toSymbol(argumentBindings), body.getRoot().toString(context.createBinding(argumentBindings), path, null)); + } + + /** + * Returns a symbolic string that represents this function with a given + * list of arguments. The arguments are mangled by hashing the string + * representation of the argument expressions, so we might need to revisit + * this if we start seeing collisions. + * + * @param argumentBindings the bound arguments to include in the symbolic name. + * @return the symbolic name for an instance of this function + */ + private String toSymbol(Map<String, String> argumentBindings) { + if (argumentBindings.isEmpty()) return name; + + StringBuilder ret = new StringBuilder(); + ret.append(name).append("@"); + for (Map.Entry<String,String> argumentBinding : argumentBindings.entrySet()) { + ret.append(Long.toHexString(symbolCode(argumentBinding.getKey() + "=" + argumentBinding.getValue()))); + ret.append("."); + } + if (ret.toString().endsWith(".")) + ret.setLength(ret.length()-1); + return ret.toString(); + } + + + /** + * <p>Returns a more unique hash code than what Java's own {@link + * String#hashCode()} method would produce.</p> + * + * @param str The string to hash. + * @return A 64 bit long hash code. + */ + private static long symbolCode(String str) { + try { + MessageDigest md = java.security.MessageDigest.getInstance("SHA-1"); + byte[] buf = md.digest(Utf8.toBytes(str)); + if (buf.length >= 8) { + long ret = 0; + for (int i = 0; i < 8; ++i) { + ret = (ret << 8) + (buf[i] & 0xff); + } + return ret; + } + } catch (NoSuchAlgorithmException e) { + throw new Error("java must always support SHA-1 message digest format", e); + } + return str.hashCode(); + } + + @Override + public String toString() { + return name; + } + + /** + * An instance of a serialization of this function, using a particular serialization context (by {@link + * ExpressionFunction#expand}) + */ + public class Instance { + + private final String name; + private final String expressionString; + + public Instance(String name, String expressionString) { + this.name = name; + this.expressionString = expressionString; + } + + public String getName() { + return name; + } + + public String getExpressionString() { + return expressionString; + } + + } +} diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/FeatureList.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/FeatureList.java index 527a908da73..c18a8c0e838 100755 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/FeatureList.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/FeatureList.java @@ -1,140 +1,140 @@ // Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.searchlib.rankingexpression;
-
-import com.google.common.annotations.Beta;
-import com.yahoo.searchlib.rankingexpression.parser.ParseException;
-import com.yahoo.searchlib.rankingexpression.parser.RankingExpressionParser;
-import com.yahoo.searchlib.rankingexpression.parser.TokenMgrError;
-import com.yahoo.searchlib.rankingexpression.rule.ReferenceNode;
-
-import java.io.*;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-
-/**
- * Encapsulates the production rule 'featureList()' int the RankingExpressionParser.
- *
- * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
- */
-@Beta
-public class FeatureList implements Iterable<ReferenceNode> {
-
- private final List<ReferenceNode> features = new ArrayList<>();
-
- /**
- * Creates a new feature list by consuming from a reader object.
- *
- * @param reader The reader that contains the string to parse.
- * @throws ParseException Thrown if the string could not be parsed.
- */
- public FeatureList(Reader reader) throws ParseException {
- features.addAll(parse(reader));
- }
-
- /**
- * Creates a new feature list by parsing a string.
- *
- * @param list The string to parse.
- * @throws ParseException Thrown if the string could not be parsed.
- */
- public FeatureList(String list) throws ParseException {
- features.addAll(parse(new StringReader(list)));
- }
-
- /**
- * Creates a new feature list by reading the content of a file.
- *
- * @param file The file whose content to parse.
- * @throws ParseException Thrown if the string could not be parsed.
- * @throws FileNotFoundException Thrown if the file specified could not be found.
- */
- public FeatureList(File file) throws ParseException, FileNotFoundException {
- features.addAll(parse(new FileReader(file)));
- }
-
- /**
- * Parses the content of a reader object as a list of feature nodes.
- *
- * @param reader A reader object that contains an feature list.
- * @return A list of those features named in the string.
- * @throws ParseException if the string could not be parsed.
- */
- private static List<ReferenceNode> parse(Reader reader) throws ParseException {
- List<ReferenceNode> lst;
- try {
- lst = new RankingExpressionParser(reader).featureList();
- }
- catch (TokenMgrError e) {
- ParseException t = new ParseException();
- throw (ParseException)t.initCause(e);
- }
- List<ReferenceNode> ret = new ArrayList<ReferenceNode>(lst.size());
- for (Object obj : lst) {
- if (!(obj instanceof ReferenceNode)) {
- throw new IllegalStateException("Feature list contains a " + obj.getClass().getName() + ".");
- }
- ret.add((ReferenceNode)obj);
- }
- return ret;
- }
-
- /**
- * Returns the number of features in this list.
- *
- * @return The size.
- */
- public int size() {
- return features.size();
- }
-
- /**
- * Returns the feature at the given index.
- *
- * @param i The index of the feature to return.
- * @return The featuer at the given index.
- */
- public ReferenceNode get(int i) {
- return features.get(i);
- }
-
- @Override
- public int hashCode() {
- int ret = 0;
- for (ReferenceNode node : features) {
- ret += node.hashCode() * 17;
- }
- return ret;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (!(obj instanceof FeatureList)) {
- return false;
- }
- FeatureList lst = (FeatureList)obj;
- if (features.size() != lst.features.size()) {
- return false;
- }
- for (int i = 0; i < features.size(); ++i) {
- if (!features.get(i).equals(lst.features.get(i))) {
- return false;
- }
- }
- return true;
- }
-
- @Override
- public String toString() {
- StringBuilder ret = new StringBuilder();
- for (ReferenceNode node : this) {
- ret.append(node).append(" ");
- }
- return ret.toString();
- }
-
- @Override
- public Iterator<ReferenceNode> iterator() {
- return features.iterator();
- }
-}
+package com.yahoo.searchlib.rankingexpression; + +import com.google.common.annotations.Beta; +import com.yahoo.searchlib.rankingexpression.parser.ParseException; +import com.yahoo.searchlib.rankingexpression.parser.RankingExpressionParser; +import com.yahoo.searchlib.rankingexpression.parser.TokenMgrError; +import com.yahoo.searchlib.rankingexpression.rule.ReferenceNode; + +import java.io.*; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +/** + * Encapsulates the production rule 'featureList()' int the RankingExpressionParser. + * + * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a> + */ +@Beta +public class FeatureList implements Iterable<ReferenceNode> { + + private final List<ReferenceNode> features = new ArrayList<>(); + + /** + * Creates a new feature list by consuming from a reader object. + * + * @param reader The reader that contains the string to parse. + * @throws ParseException Thrown if the string could not be parsed. + */ + public FeatureList(Reader reader) throws ParseException { + features.addAll(parse(reader)); + } + + /** + * Creates a new feature list by parsing a string. + * + * @param list The string to parse. + * @throws ParseException Thrown if the string could not be parsed. + */ + public FeatureList(String list) throws ParseException { + features.addAll(parse(new StringReader(list))); + } + + /** + * Creates a new feature list by reading the content of a file. + * + * @param file The file whose content to parse. + * @throws ParseException Thrown if the string could not be parsed. + * @throws FileNotFoundException Thrown if the file specified could not be found. + */ + public FeatureList(File file) throws ParseException, FileNotFoundException { + features.addAll(parse(new FileReader(file))); + } + + /** + * Parses the content of a reader object as a list of feature nodes. + * + * @param reader A reader object that contains an feature list. + * @return A list of those features named in the string. + * @throws ParseException if the string could not be parsed. + */ + private static List<ReferenceNode> parse(Reader reader) throws ParseException { + List<ReferenceNode> lst; + try { + lst = new RankingExpressionParser(reader).featureList(); + } + catch (TokenMgrError e) { + ParseException t = new ParseException(); + throw (ParseException)t.initCause(e); + } + List<ReferenceNode> ret = new ArrayList<ReferenceNode>(lst.size()); + for (Object obj : lst) { + if (!(obj instanceof ReferenceNode)) { + throw new IllegalStateException("Feature list contains a " + obj.getClass().getName() + "."); + } + ret.add((ReferenceNode)obj); + } + return ret; + } + + /** + * Returns the number of features in this list. + * + * @return The size. + */ + public int size() { + return features.size(); + } + + /** + * Returns the feature at the given index. + * + * @param i The index of the feature to return. + * @return The featuer at the given index. + */ + public ReferenceNode get(int i) { + return features.get(i); + } + + @Override + public int hashCode() { + int ret = 0; + for (ReferenceNode node : features) { + ret += node.hashCode() * 17; + } + return ret; + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof FeatureList)) { + return false; + } + FeatureList lst = (FeatureList)obj; + if (features.size() != lst.features.size()) { + return false; + } + for (int i = 0; i < features.size(); ++i) { + if (!features.get(i).equals(lst.features.get(i))) { + return false; + } + } + return true; + } + + @Override + public String toString() { + StringBuilder ret = new StringBuilder(); + for (ReferenceNode node : this) { + ret.append(node).append(" "); + } + return ret.toString(); + } + + @Override + public Iterator<ReferenceNode> iterator() { + return features.iterator(); + } +} diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/EmbracedNode.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/EmbracedNode.java index 49604f14ca2..fdb71bca2f5 100755 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/EmbracedNode.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/EmbracedNode.java @@ -1,57 +1,57 @@ -// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.searchlib.rankingexpression.rule;
-
-import com.yahoo.searchlib.rankingexpression.evaluation.Context;
-import com.yahoo.searchlib.rankingexpression.evaluation.Value;
-
-import java.util.Collections;
-import java.util.Deque;
-import java.util.List;
-
-/**
- * This class represents another expression enclosed in braces.
- *
- * @author Simon Thoresen
- */
-public final class EmbracedNode extends CompositeNode {
-
- // The node to embrace.
- private final ExpressionNode value;
-
- /**
- * Creates a new expression node that embraces another.
- *
- * @param value The node to embrace.
- */
- public EmbracedNode(ExpressionNode value) {
- this.value=value;
- }
-
- /** Returns the node enclosed by this */
- public ExpressionNode getValue() { return value; }
-
- @Override
- public List<ExpressionNode> children() {
- return Collections.singletonList(value);
- }
-
- @Override
- public String toString(SerializationContext context, Deque<String> path, CompositeNode parent) {
- String expression = value.toString(context, path, this);
- if (value instanceof ReferenceNode) return expression;
- return "(" + expression + ")";
- }
-
- @Override
- public Value evaluate(Context context) {
- return value.evaluate(context);
- }
-
- @Override
- public CompositeNode setChildren(List<ExpressionNode> newChildren) {
- if (newChildren.size() != 1)
- throw new IllegalArgumentException("Expected 1 child but got " + newChildren.size());
- return new EmbracedNode(newChildren.get(0));
- }
-
-}
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.searchlib.rankingexpression.rule; + +import com.yahoo.searchlib.rankingexpression.evaluation.Context; +import com.yahoo.searchlib.rankingexpression.evaluation.Value; + +import java.util.Collections; +import java.util.Deque; +import java.util.List; + +/** + * This class represents another expression enclosed in braces. + * + * @author Simon Thoresen + */ +public final class EmbracedNode extends CompositeNode { + + // The node to embrace. + private final ExpressionNode value; + + /** + * Creates a new expression node that embraces another. + * + * @param value The node to embrace. + */ + public EmbracedNode(ExpressionNode value) { + this.value=value; + } + + /** Returns the node enclosed by this */ + public ExpressionNode getValue() { return value; } + + @Override + public List<ExpressionNode> children() { + return Collections.singletonList(value); + } + + @Override + public String toString(SerializationContext context, Deque<String> path, CompositeNode parent) { + String expression = value.toString(context, path, this); + if (value instanceof ReferenceNode) return expression; + return "(" + expression + ")"; + } + + @Override + public Value evaluate(Context context) { + return value.evaluate(context); + } + + @Override + public CompositeNode setChildren(List<ExpressionNode> newChildren) { + if (newChildren.size() != 1) + throw new IllegalArgumentException("Expected 1 child but got " + newChildren.size()); + return new EmbracedNode(newChildren.get(0)); + } + +} diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/TruthOperator.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/TruthOperator.java index 234825eaa3d..1b74326b168 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/TruthOperator.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/TruthOperator.java @@ -47,4 +47,4 @@ public enum TruthOperator implements Serializable { return x==y; } -}
\ No newline at end of file +} diff --git a/searchlib/src/main/java/com/yahoo/searchlib/treenet/TreeNetConverter.java b/searchlib/src/main/java/com/yahoo/searchlib/treenet/TreeNetConverter.java index c147c3a33b8..628cfc10a48 100755 --- a/searchlib/src/main/java/com/yahoo/searchlib/treenet/TreeNetConverter.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/treenet/TreeNetConverter.java @@ -1,35 +1,35 @@ // Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.searchlib.treenet;
-
-import com.yahoo.searchlib.treenet.parser.TreeNetParser;
-
-import java.io.FileNotFoundException;
-import java.io.FileReader;
-
-/**
- * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
- */
-public class TreeNetConverter {
-
- /**
- * Implements an application main function so that the converter can be used as a command-line tool.
- *
- * @param args List of arguments.
- */
- public static void main(String[] args) {
- if (args.length != 1) {
- System.err.println("Usage: TreeNetConverter <filename>");
- System.exit(1);
- }
- try {
- TreeNetParser parser = new TreeNetParser(new FileReader(args[0]));
- System.out.println(parser.treeNet().toRankingExpression());
- } catch (FileNotFoundException e) {
- System.err.println("Could not find file '" + args[0] + "'.");
- System.exit(1);
- } catch (Exception e) {
- System.err.println("An error occured while parsing the content of file '" + args[0] + "': " + e);
- System.exit(1);
- }
- }
-}
+package com.yahoo.searchlib.treenet; + +import com.yahoo.searchlib.treenet.parser.TreeNetParser; + +import java.io.FileNotFoundException; +import java.io.FileReader; + +/** + * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a> + */ +public class TreeNetConverter { + + /** + * Implements an application main function so that the converter can be used as a command-line tool. + * + * @param args List of arguments. + */ + public static void main(String[] args) { + if (args.length != 1) { + System.err.println("Usage: TreeNetConverter <filename>"); + System.exit(1); + } + try { + TreeNetParser parser = new TreeNetParser(new FileReader(args[0])); + System.out.println(parser.treeNet().toRankingExpression()); + } catch (FileNotFoundException e) { + System.err.println("Could not find file '" + args[0] + "'."); + System.exit(1); + } catch (Exception e) { + System.err.println("An error occured while parsing the content of file '" + args[0] + "': " + e); + System.exit(1); + } + } +} diff --git a/searchlib/src/main/java/com/yahoo/searchlib/treenet/rule/ComparisonCondition.java b/searchlib/src/main/java/com/yahoo/searchlib/treenet/rule/ComparisonCondition.java index 1855a8a5674..6c40406977b 100755 --- a/searchlib/src/main/java/com/yahoo/searchlib/treenet/rule/ComparisonCondition.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/treenet/rule/ComparisonCondition.java @@ -1,39 +1,39 @@ // Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.searchlib.treenet.rule;
-
-import com.yahoo.java7compat.Util;
-
-/**
- * Represents a condition which comparing two values
- *
- * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
- */
-public class ComparisonCondition extends Condition {
-
- private final double rhs;
-
- /**
- * Constructs a new instance of this class.
- *
- * @param lhs The name of the feature to compare to a constant.
- * @param rhs The constant to compare the feature with.
- * @param ift The label to jump to if left < right.
- * @param iff The label to jump to if left >= right;
- */
- public ComparisonCondition(String lhs, double rhs, String ift, String iff) {
- super(lhs, ift, iff);
- this.rhs = rhs;
- }
-
- /**
- * Returns the constant to compare the feature with.
- *
- * @return The constant.
- */
- public double getConstant() { return rhs; }
-
- @Override
- public String conditionToRankingExpression() {
- return "< " + Util.toJava7String(rhs);
- }
-}
+package com.yahoo.searchlib.treenet.rule; + +import com.yahoo.java7compat.Util; + +/** + * Represents a condition which comparing two values + * + * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a> + */ +public class ComparisonCondition extends Condition { + + private final double rhs; + + /** + * Constructs a new instance of this class. + * + * @param lhs The name of the feature to compare to a constant. + * @param rhs The constant to compare the feature with. + * @param ift The label to jump to if left < right. + * @param iff The label to jump to if left >= right; + */ + public ComparisonCondition(String lhs, double rhs, String ift, String iff) { + super(lhs, ift, iff); + this.rhs = rhs; + } + + /** + * Returns the constant to compare the feature with. + * + * @return The constant. + */ + public double getConstant() { return rhs; } + + @Override + public String conditionToRankingExpression() { + return "< " + Util.toJava7String(rhs); + } +} diff --git a/searchlib/src/main/java/com/yahoo/searchlib/treenet/rule/Response.java b/searchlib/src/main/java/com/yahoo/searchlib/treenet/rule/Response.java index 347dd84f419..9838cafcab3 100755 --- a/searchlib/src/main/java/com/yahoo/searchlib/treenet/rule/Response.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/treenet/rule/Response.java @@ -1,45 +1,45 @@ // Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.searchlib.treenet.rule;
-
-/**
- * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
- */
-public class Response extends TreeNode {
-
- // The id of the next tree to run after this.
- private final Double value;
-
- // The value of this response.
- private final String next;
-
- /**
- * Constructs a new response.
- *
- * @param next The id of the next tree to run after this.
- * @param value The value of this response.
- */
- public Response(Double value, String next) {
- super();
- this.value = value;
- this.next = next;
- }
-
- /**
- * Returns the value of this response.
- */
- public Double getValue() {
- return value;
- }
-
- /**
- * Returns the id of the next tree to run after this.
- */
- public String getNext() {
- return next;
- }
-
- @Override
- public String toRankingExpression() {
- return value.toString();
- }
-}
+package com.yahoo.searchlib.treenet.rule; + +/** + * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a> + */ +public class Response extends TreeNode { + + // The id of the next tree to run after this. + private final Double value; + + // The value of this response. + private final String next; + + /** + * Constructs a new response. + * + * @param next The id of the next tree to run after this. + * @param value The value of this response. + */ + public Response(Double value, String next) { + super(); + this.value = value; + this.next = next; + } + + /** + * Returns the value of this response. + */ + public Double getValue() { + return value; + } + + /** + * Returns the id of the next tree to run after this. + */ + public String getNext() { + return next; + } + + @Override + public String toRankingExpression() { + return value.toString(); + } +} diff --git a/searchlib/src/main/java/com/yahoo/searchlib/treenet/rule/SetMembershipCondition.java b/searchlib/src/main/java/com/yahoo/searchlib/treenet/rule/SetMembershipCondition.java index bb47947557c..a992d3b8032 100755 --- a/searchlib/src/main/java/com/yahoo/searchlib/treenet/rule/SetMembershipCondition.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/treenet/rule/SetMembershipCondition.java @@ -1,57 +1,57 @@ -// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.searchlib.treenet.rule;
-
-import com.yahoo.java7compat.Util;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.List;
-
-/**
- * Represents a set membership test on the form <code>feature IN (integer1, integer2 ...)</code>
- *
- * @author bratseth
- * @since 5.1.21
- */
-public class SetMembershipCondition extends Condition {
-
- private final List<Object> setValues;
-
- /**
- * Constructs a new instance of this class.
- *
- * @param testValue the name of the feature to test
- * @param setValues the set of values to compare to
- * @param trueLabel the label to jump to if the value is in the set
- * @param falseLabel the label to jumt to if the value is not in the set
- */
- public SetMembershipCondition(String testValue, List<Object> setValues, String trueLabel, String falseLabel) {
- super(testValue, trueLabel, falseLabel);
- this.setValues = Collections.unmodifiableList(new ArrayList<>(setValues));
- }
-
- /** Returns the unmodifiable set of values to check */
- public List<Object> getSetValues() { return setValues; }
-
- @Override
- protected String conditionToRankingExpression() {
- StringBuilder b = new StringBuilder("in [");
- for (Iterator<Object> i = setValues.iterator(); i.hasNext(); ) {
- Object value = i.next();
- if (value instanceof String)
- b.append("\"").append(value).append("\"");
- else if (value instanceof Integer)
- b.append(value);
- else
- throw new RuntimeException("Excepted a string or integer in a set membership test, not a " +
- value.getClass() + ": " + value);
-
- if (i.hasNext())
- b.append(",");
- }
- b.append("]");
- return b.toString();
- }
-
-}
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.searchlib.treenet.rule; + +import com.yahoo.java7compat.Util; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + +/** + * Represents a set membership test on the form <code>feature IN (integer1, integer2 ...)</code> + * + * @author bratseth + * @since 5.1.21 + */ +public class SetMembershipCondition extends Condition { + + private final List<Object> setValues; + + /** + * Constructs a new instance of this class. + * + * @param testValue the name of the feature to test + * @param setValues the set of values to compare to + * @param trueLabel the label to jump to if the value is in the set + * @param falseLabel the label to jumt to if the value is not in the set + */ + public SetMembershipCondition(String testValue, List<Object> setValues, String trueLabel, String falseLabel) { + super(testValue, trueLabel, falseLabel); + this.setValues = Collections.unmodifiableList(new ArrayList<>(setValues)); + } + + /** Returns the unmodifiable set of values to check */ + public List<Object> getSetValues() { return setValues; } + + @Override + protected String conditionToRankingExpression() { + StringBuilder b = new StringBuilder("in ["); + for (Iterator<Object> i = setValues.iterator(); i.hasNext(); ) { + Object value = i.next(); + if (value instanceof String) + b.append("\"").append(value).append("\""); + else if (value instanceof Integer) + b.append(value); + else + throw new RuntimeException("Excepted a string or integer in a set membership test, not a " + + value.getClass() + ": " + value); + + if (i.hasNext()) + b.append(","); + } + b.append("]"); + return b.toString(); + } + +} diff --git a/searchlib/src/main/java/com/yahoo/searchlib/treenet/rule/Tree.java b/searchlib/src/main/java/com/yahoo/searchlib/treenet/rule/Tree.java index 2a7191baeba..6db319ebc6c 100755 --- a/searchlib/src/main/java/com/yahoo/searchlib/treenet/rule/Tree.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/treenet/rule/Tree.java @@ -1,110 +1,110 @@ // Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.searchlib.treenet.rule;
-
-import java.util.Map;
-
-/**
- * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
- */
-public class Tree {
-
- private final String name;
-
- // The parent tree net of this.
- private TreeNet parent;
-
- // Returns the id of the next tree to run after this.
- private String next;
-
- // The initial response value of this tree, may be null.
- private final Double value;
-
- // The id of the first condition or response to run in this tree.
- private final String begin;
-
- // All named nodes of this tree.
- private final Map<String, TreeNode> nodes;
-
- /**
- * Constructs a new tree.
- *
- * @param name The name of this tree, used for error outputs.
- * @param value The initial response value of this tree, may be null.
- * @param begin The id of the first condition or response to run in this tree.
- * @param nodes All named nodes of this tree.
- */
- public Tree(String name, Double value, String begin, Map<String, TreeNode> nodes) {
- this.name = name;
- this.value = value;
- this.begin = begin;
- this.nodes = nodes;
-
- this.next = null;
- for (TreeNode node : this.nodes.values()) {
- node.setParent(this);
- if (node instanceof Response) {
- String next = ((Response)node).getNext();
- if (this.next == null) {
- this.next = next;
- } else if (!this.next.equals(next)) {
- throw new IllegalStateException("Not all child nodes of tree '" + name + "' agree on the next " +
- "tree to run. Initial name was '" + this.next + "', conflicting " +
- "name is '" + next + "'.");
- }
- }
- }
- }
-
- public String getName() { return name; }
-
- /**
- * Returns the parent tree net of this.
- */
- public TreeNet getParent() { return parent; }
-
- /**
- * Sets the parent tree net of this.
- *
- * @param parent The parent tree net.
- * @return This, to allow chaining.
- */
- public Tree setParent(TreeNet parent) {
- this.parent = parent;
- return this;
- }
-
- /**
- * Returns the id of the next tree to run after this.
- */
- public String getNext() {
- return next;
- }
-
- /**
- * Returns the initial response value of this tree, may be null.
- */
- public Double getValue() {
- return value;
- }
-
- /**
- * Returns the id of the first condition or response to run in this tree.
- */
- public String getBegin() {
- return begin;
- }
-
- /**
- * Returns all named nodes of this tree.
- */
- public Map<String, TreeNode> getNodes() {
- return nodes;
- }
-
- /**
- * Returns a ranking expression equivalent of this tree.
- */
- public String toRankingExpression() {
- return nodes.get(begin).toRankingExpression();
- }
-}
+package com.yahoo.searchlib.treenet.rule; + +import java.util.Map; + +/** + * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a> + */ +public class Tree { + + private final String name; + + // The parent tree net of this. + private TreeNet parent; + + // Returns the id of the next tree to run after this. + private String next; + + // The initial response value of this tree, may be null. + private final Double value; + + // The id of the first condition or response to run in this tree. + private final String begin; + + // All named nodes of this tree. + private final Map<String, TreeNode> nodes; + + /** + * Constructs a new tree. + * + * @param name The name of this tree, used for error outputs. + * @param value The initial response value of this tree, may be null. + * @param begin The id of the first condition or response to run in this tree. + * @param nodes All named nodes of this tree. + */ + public Tree(String name, Double value, String begin, Map<String, TreeNode> nodes) { + this.name = name; + this.value = value; + this.begin = begin; + this.nodes = nodes; + + this.next = null; + for (TreeNode node : this.nodes.values()) { + node.setParent(this); + if (node instanceof Response) { + String next = ((Response)node).getNext(); + if (this.next == null) { + this.next = next; + } else if (!this.next.equals(next)) { + throw new IllegalStateException("Not all child nodes of tree '" + name + "' agree on the next " + + "tree to run. Initial name was '" + this.next + "', conflicting " + + "name is '" + next + "'."); + } + } + } + } + + public String getName() { return name; } + + /** + * Returns the parent tree net of this. + */ + public TreeNet getParent() { return parent; } + + /** + * Sets the parent tree net of this. + * + * @param parent The parent tree net. + * @return This, to allow chaining. + */ + public Tree setParent(TreeNet parent) { + this.parent = parent; + return this; + } + + /** + * Returns the id of the next tree to run after this. + */ + public String getNext() { + return next; + } + + /** + * Returns the initial response value of this tree, may be null. + */ + public Double getValue() { + return value; + } + + /** + * Returns the id of the first condition or response to run in this tree. + */ + public String getBegin() { + return begin; + } + + /** + * Returns all named nodes of this tree. + */ + public Map<String, TreeNode> getNodes() { + return nodes; + } + + /** + * Returns a ranking expression equivalent of this tree. + */ + public String toRankingExpression() { + return nodes.get(begin).toRankingExpression(); + } +} diff --git a/searchlib/src/main/java/com/yahoo/searchlib/treenet/rule/TreeNet.java b/searchlib/src/main/java/com/yahoo/searchlib/treenet/rule/TreeNet.java index 1db13b6c12e..47a2afad4d2 100755 --- a/searchlib/src/main/java/com/yahoo/searchlib/treenet/rule/TreeNet.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/treenet/rule/TreeNet.java @@ -1,63 +1,63 @@ // Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.searchlib.treenet.rule;
-
-import java.util.Map;
-
-/**
- * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
- */
-public class TreeNet {
-
- // The id of the first tree to run in this net.
- private String begin;
-
- // All named trees of this net.
- private final Map<String, Tree> trees;
-
- /**
- * Constructs a new tree net.
- *
- * @param begin The id of the first tree to run in this net.
- * @param trees All named trees of this net.
- */
- public TreeNet(String begin, Map<String, Tree> trees) {
- this.begin = begin;
- this.trees = trees;
- for (Tree tree : this.trees.values()) {
- tree.setParent(this);
- }
- }
-
- /**
- * Returns the id of the first tree to run in this net.
- */
- public String getBegin() {
- return begin;
- }
-
- /**
- * Returns all named trees of this net.
- */
- public Map<String, Tree> getTrees() {
- return trees;
- }
-
- /**
- * Returns a ranking expression equivalent of this net.
- */
- public String toRankingExpression() {
- StringBuilder ret = new StringBuilder();
- String next = begin;
- while (next != null) {
- Tree tree = trees.get(next);
- if (tree.getBegin() != null) {
- if (ret.length() > 0) {
- ret.append(" + \n");
- }
- ret.append(tree.toRankingExpression());
- }
- next = tree.getNext();
- }
- return ret.toString();
- }
-}
+package com.yahoo.searchlib.treenet.rule; + +import java.util.Map; + +/** + * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a> + */ +public class TreeNet { + + // The id of the first tree to run in this net. + private String begin; + + // All named trees of this net. + private final Map<String, Tree> trees; + + /** + * Constructs a new tree net. + * + * @param begin The id of the first tree to run in this net. + * @param trees All named trees of this net. + */ + public TreeNet(String begin, Map<String, Tree> trees) { + this.begin = begin; + this.trees = trees; + for (Tree tree : this.trees.values()) { + tree.setParent(this); + } + } + + /** + * Returns the id of the first tree to run in this net. + */ + public String getBegin() { + return begin; + } + + /** + * Returns all named trees of this net. + */ + public Map<String, Tree> getTrees() { + return trees; + } + + /** + * Returns a ranking expression equivalent of this net. + */ + public String toRankingExpression() { + StringBuilder ret = new StringBuilder(); + String next = begin; + while (next != null) { + Tree tree = trees.get(next); + if (tree.getBegin() != null) { + if (ret.length() > 0) { + ret.append(" + \n"); + } + ret.append(tree.toRankingExpression()); + } + next = tree.getNext(); + } + return ret.toString(); + } +} diff --git a/searchlib/src/main/java/com/yahoo/searchlib/treenet/rule/TreeNode.java b/searchlib/src/main/java/com/yahoo/searchlib/treenet/rule/TreeNode.java index a637adafc73..178d6810193 100755 --- a/searchlib/src/main/java/com/yahoo/searchlib/treenet/rule/TreeNode.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/treenet/rule/TreeNode.java @@ -1,34 +1,34 @@ // Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.searchlib.treenet.rule;
-
-/**
- * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
- */
-public abstract class TreeNode {
-
- // The parent tree of this.
- private Tree parent = null;
-
- /**
- * Returns the parent tree of this.
- */
- public Tree getParent() {
- return parent;
- }
-
- /**
- * Sets the parent tree net of this.
- *
- * @param parent The parent tree net.
- * @return This, to allow chaining.
- */
- public TreeNode setParent(Tree parent) {
- this.parent = parent;
- return this;
- }
-
- /**
- * Returns a ranking expression equivalent of this net.
- */
- public abstract String toRankingExpression();
-}
+package com.yahoo.searchlib.treenet.rule; + +/** + * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a> + */ +public abstract class TreeNode { + + // The parent tree of this. + private Tree parent = null; + + /** + * Returns the parent tree of this. + */ + public Tree getParent() { + return parent; + } + + /** + * Sets the parent tree net of this. + * + * @param parent The parent tree net. + * @return This, to allow chaining. + */ + public TreeNode setParent(Tree parent) { + this.parent = parent; + return this; + } + + /** + * Returns a ranking expression equivalent of this net. + */ + public abstract String toRankingExpression(); +} |