Skip to content

Commit

Permalink
Simplify: Use lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
bratseth committed Sep 27, 2022
1 parent 373eac0 commit 6b97667
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,33 @@ void testFunctionInliningPreserveArithmeticOrdering() throws ParseException {
child.getFirstPhaseRanking().getRoot().toString());
}

@Test
void testInlinedComparison() throws ParseException {
RankProfileRegistry rankProfileRegistry = new RankProfileRegistry();
ApplicationBuilder builder = new ApplicationBuilder(rankProfileRegistry);
builder.addSchema("search test {\n" +
" document test { \n" +
" }\n" +
" \n" +
" rank-profile parent {\n" +
"function foo() {\n" +
" expression: 3 * bar\n" +
"}\n" +
"\n" +
"function inline bar() {\n" +
" expression: query(test) > 2.0\n" +
"}\n" +
"}\n" +
"}\n");
builder.build(true);
Schema s = builder.getSchema();

RankProfile parent = rankProfileRegistry.get(s, "parent").compile(new QueryProfileRegistry(), new ImportedMlModels());
assertEquals("3 * ( query(test) > 2.0 )",
parent.getFunctions().get("foo").function().getBody().getRoot().toString());

}

@Test
void testConstants() throws ParseException {
RankProfileRegistry rankProfileRegistry = new RankProfileRegistry();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.yahoo.searchlib.rankingexpression.evaluation.Value;

import java.util.List;
import java.util.function.BiFunction;

/**
* A mathematical operator
Expand All @@ -12,48 +13,53 @@
*/
public enum ArithmeticOperator {

OR(0, "||") { public Value evaluate(Value x, Value y) {
return x.or(y);
}},
AND(1, "&&") { public Value evaluate(Value x, Value y) {
return x.and(y);
}},
PLUS(2, "+") { public Value evaluate(Value x, Value y) {
return x.add(y);
}},
MINUS(3, "-") { public Value evaluate(Value x, Value y) {
return x.subtract(y);
}},
MULTIPLY(4, "*") { public Value evaluate(Value x, Value y) {
return x.multiply(y);
}},
DIVIDE(5, "/") { public Value evaluate(Value x, Value y) {
return x.divide(y);
}},
MODULO(6, "%") { public Value evaluate(Value x, Value y) {
return x.modulo(y);
}},
POWER(7, "^") { public Value evaluate(Value x, Value y) {
return x.power(y);
}};
/*
struct Sub : OperatorHelper<Sub> { Sub() : Helper("-", 101, LEFT) {}};
struct Mul : OperatorHelper<Mul> { Mul() : Helper("*", 102, LEFT) {}};
struct Div : OperatorHelper<Div> { Div() : Helper("/", 102, LEFT) {}};
struct Mod : OperatorHelper<Mod> { Mod() : Helper("%", 102, LEFT) {}};
struct Pow : OperatorHelper<Pow> { Pow() : Helper("^", 103, RIGHT) {}};
struct Equal : OperatorHelper<Equal> { Equal() : Helper("==", 10, LEFT) {}};
struct NotEqual : OperatorHelper<NotEqual> { NotEqual() : Helper("!=", 10, LEFT) {}};
struct Approx : OperatorHelper<Approx> { Approx() : Helper("~=", 10, LEFT) {}};
struct Less : OperatorHelper<Less> { Less() : Helper("<", 10, LEFT) {}};
struct LessEqual : OperatorHelper<LessEqual> { LessEqual() : Helper("<=", 10, LEFT) {}};
struct Greater : OperatorHelper<Greater> { Greater() : Helper(">", 10, LEFT) {}};
struct GreaterEqual : OperatorHelper<GreaterEqual> { GreaterEqual() : Helper(">=", 10, LEFT) {}};
struct And : OperatorHelper<And> { And() : Helper("&&", 2, LEFT) {}};
struct Or : OperatorHelper<Or> { Or() : Helper("||", 1, LEFT) {}};
*/

OR(0, "||", (x, y) -> x.or(y)),
AND(1, "&&", (x, y) -> x.and(y)),
PLUS(2, "+", (x, y) -> x.add(y)),
MINUS(3, "-", (x, y) -> x.subtract(y)),
MULTIPLY(4, "*", (x, y) -> x.multiply(y)),
DIVIDE(5, "/", (x, y) -> x.divide(y)),
MODULO(6, "%", (x, y) -> x.modulo(y)),
POWER(7, "^", (x, y) -> x.power(y));

/** A list of all the operators in this in order of decreasing precedence */
public static final List<ArithmeticOperator> operatorsByPrecedence = List.of(POWER, MODULO, DIVIDE, MULTIPLY, MINUS, PLUS, AND, OR);

private final int precedence;
private final String image;
private final BiFunction<Value, Value, Value> function;

private ArithmeticOperator(int precedence, String image) {
ArithmeticOperator(int precedence, String image, BiFunction<Value, Value, Value> function) {
this.precedence = precedence;
this.image = image;
this.function = function;
}

/** Returns true if this operator has precedence over the given operator */
public boolean hasPrecedenceOver(ArithmeticOperator op) {
return precedence > op.precedence;
}

public abstract Value evaluate(Value x, Value y);
public final Value evaluate(Value x, Value y) {
return function.apply(x, y);
}

@Override
public String toString() {
Expand Down

0 comments on commit 6b97667

Please sign in to comment.