Search in sources :

Example 1 with IfNode

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

the class EvaluationTestCase method testEvaluation.

@Test
public void testEvaluation() {
    EvaluationTester tester = new EvaluationTester();
    tester.assertEvaluates(0.5, "0.5");
    tester.assertEvaluates(-0.5, "-0.5");
    tester.assertEvaluates(0.5, "one_half");
    tester.assertEvaluates(-0.5, "-one_half");
    tester.assertEvaluates(0, "nonexisting");
    tester.assertEvaluates(0.75, "0.5 + 0.25");
    tester.assertEvaluates(0.75, "one_half + a_quarter");
    tester.assertEvaluates(1.25, "0.5 - 0.25 + one");
    tester.assertEvaluates(9.0, "3 ^ 2");
    // String
    tester.assertEvaluates(1, "if(\"a\"==\"a\",1,0)");
    // Precedence
    tester.assertEvaluates(26, "2*3+4*5");
    tester.assertEvaluates(1, "2/6+4/6");
    tester.assertEvaluates(2 * 3 * 4 + 3 * 4 * 5 - 4 * 200 / 10, "2*3*4+3*4*5-4*200/10");
    tester.assertEvaluates(3, "1 + 10 % 6 / 2");
    tester.assertEvaluates(10.0, "3 ^ 2 + 1");
    tester.assertEvaluates(18.0, "2 * 3 ^ 2");
    // Conditionals
    tester.assertEvaluates(2 * (3 * 4 + 3) * (4 * 5 - 4 * 200) / 10, "2*(3*4+3)*(4*5-4*200)/10");
    tester.assertEvaluates(0.5, "if( 2<3, one_half, one_quarter)");
    tester.assertEvaluates(0.25, "if( 2>3, one_half, a_quarter)");
    tester.assertEvaluates(0.5, "if( 1==1, one_half, a_quarter)");
    tester.assertEvaluates(0.5, "if( 1<=1, one_half, a_quarter)");
    tester.assertEvaluates(0.5, "if( 1<=1.1, one_half, a_quarter)");
    tester.assertEvaluates(0.25, "if( 1>=1.1, one_half, a_quarter)");
    tester.assertEvaluates(0.5, "if( 0.33333333333333333333~=1/3, one_half, a_quarter)");
    tester.assertEvaluates(0.25, "if( 0.33333333333333333333~=1/35, one_half, a_quarter)");
    tester.assertEvaluates(5.5, "if(one_half in [one_quarter,one_half],  one_half+5,log(one_quarter) * one_quarter)");
    tester.assertEvaluates(0.5, "if( 1 in [1,2 , 3], one_half, a_quarter)");
    tester.assertEvaluates(0.25, "if( 1 in [  2,3,4], one_half, a_quarter)");
    tester.assertEvaluates(0.5, "if( \"foo\" in [\"foo\",\"bar\"], one_half, a_quarter)");
    tester.assertEvaluates(0.5, "if( foo in [\"foo\",\"bar\"], one_half, a_quarter)");
    tester.assertEvaluates(0.5, "if( \"foo\" in [foo,\"bar\"], one_half, a_quarter)");
    tester.assertEvaluates(0.5, "if( foo in [foo,\"bar\"], one_half, a_quarter)");
    tester.assertEvaluates(0.25, "if( \"foo\" in [\"baz\",\"boz\"], one_half, a_quarter)");
    tester.assertEvaluates(0.5, "if( one in [0, 1, 2], one_half, a_quarter)");
    tester.assertEvaluates(0.25, "if( one in [2], one_half, a_quarter)");
    tester.assertEvaluates(2.5, "if(1.0, 2.5, 3.5)");
    tester.assertEvaluates(3.5, "if(0.0, 2.5, 3.5)");
    tester.assertEvaluates(2.5, "if(1.0-1.1, 2.5, 3.5)");
    tester.assertEvaluates(3.5, "if(1.0-1.0, 2.5, 3.5)");
    // Conditionals with branch probabilities
    RankingExpression e = tester.assertEvaluates(3.5, "if(1.0-1.0, 2.5, 3.5, 0.3)");
    assertEquals(0.3d, (double) ((IfNode) e.getRoot()).getTrueProbability(), tolerance);
    // Conditionals as expressions
    tester.assertEvaluates(new BooleanValue(true), "2<3");
    tester.assertEvaluates(new BooleanValue(false), "2>3");
    tester.assertEvaluates(new BooleanValue(false), "if (3>2, 2>3, 5.0)");
    // The result of 2>3 is converted to 0, which is <1
    tester.assertEvaluates(new BooleanValue(true), "2>3<1");
    tester.assertEvaluates(2.5, "if(2>3<1, 2.5, 3.5)");
    tester.assertEvaluates(2.5, "if(1+1>3<1+0, 2.5, 3.5)");
    // Functions
    tester.assertEvaluates(0, "sin(0)");
    tester.assertEvaluates(1, "cos(0)");
    tester.assertEvaluates(8, "pow(4/2,min(cos(0)*3,5))");
    // Random feature (which is also a tensor function) (We expect to be able to parse it and look up a zero)
    tester.assertEvaluates(0, "random(1)");
    tester.assertEvaluates(0, "random(foo)");
    // Combined
    tester.assertEvaluates(1.25, "5*if(1>=1.1, one_half, if(min(1,2)<max(1,2),if (\"foo\" in [\"foo\",\"bar\"],a_quarter,3000), 0.57345347))");
}
Also used : RankingExpression(com.yahoo.searchlib.rankingexpression.RankingExpression) IfNode(com.yahoo.searchlib.rankingexpression.rule.IfNode) Test(org.junit.Test)

Aggregations

RankingExpression (com.yahoo.searchlib.rankingexpression.RankingExpression)1 IfNode (com.yahoo.searchlib.rankingexpression.rule.IfNode)1 Test (org.junit.Test)1