summaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/prelude/semantics/test/ProductionRuleTestCase.java
blob: 595a2b4c75cd7debd90b6dda85718fc7182c7eca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.semantics.test;

import com.yahoo.search.Query;
import com.yahoo.prelude.semantics.RuleBase;
import com.yahoo.prelude.semantics.engine.Evaluation;
import com.yahoo.prelude.semantics.engine.RuleEvaluation;
import com.yahoo.prelude.semantics.rule.ConditionReference;
import com.yahoo.prelude.semantics.rule.NamedCondition;
import com.yahoo.prelude.semantics.rule.ProductionList;
import com.yahoo.prelude.semantics.rule.ProductionRule;
import com.yahoo.prelude.semantics.rule.ReferenceTermProduction;
import com.yahoo.prelude.semantics.rule.ReplacingProductionRule;
import com.yahoo.prelude.semantics.rule.TermCondition;
import com.yahoo.prelude.semantics.rule.TermProduction;

/**
 * @author <a href="mailto:bratseth@yahoo-inc.com">Jon S Bratseth</a>
 */
public class ProductionRuleTestCase extends junit.framework.TestCase {

    public ProductionRuleTestCase(String name) {
        super(name);
    }

    public void testProductionRule() {
        TermCondition term=new TermCondition("sony");
        NamedCondition named=new NamedCondition("brand",term);
        ConditionReference reference=new ConditionReference("brand");

        TermProduction termProduction =new ReferenceTermProduction("brand","brand");
        ProductionList productionList =new ProductionList();
        productionList.addProduction(termProduction);

        ProductionRule rule=new ReplacingProductionRule();
        rule.setCondition(reference);
        rule.setProduction(productionList);

        // To initialize the condition reference...
        RuleBase ruleBase=new RuleBase();
        ruleBase.setName("test");
        ruleBase.addCondition(named);
        ruleBase.addRule(rule);
        ruleBase.initialize();

        assertTrue("Brand is referenced",rule.matchReferences().contains("brand"));

        Query query=new Query("?query=sony");
        RuleEvaluation e=new Evaluation(query).freshRuleEvaluation();
        assertTrue(rule.matches(e));
        rule.produce(e);
        assertEquals("brand:sony", query.getModel().getQueryTree().getRoot().toString());
    }

}