Search in sources :

Example 1 with ProductionRule

use of com.yahoo.prelude.semantics.rule.ProductionRule in project vespa by vespa-engine.

the class ConditionTestCase method testNamedConditionReference.

public void testNamedConditionReference() {
    TermCondition term = new TermCondition("foo");
    NamedCondition named = new NamedCondition("cond", term);
    ConditionReference reference = new ConditionReference("cond");
    // To initialize the condition reference...
    ProductionRule rule = new ReplacingProductionRule();
    rule.setCondition(reference);
    rule.setProduction(new ProductionList());
    RuleBase ruleBase = new RuleBase();
    ruleBase.setName("test");
    ruleBase.addCondition(named);
    ruleBase.addRule(rule);
    ruleBase.initialize();
    Query query = new Query("?query=foo");
    assertTrue(query + "  matches " + reference, reference.matches(new Evaluation(query).freshRuleEvaluation()));
}
Also used : NamedCondition(com.yahoo.prelude.semantics.rule.NamedCondition) ConditionReference(com.yahoo.prelude.semantics.rule.ConditionReference) Evaluation(com.yahoo.prelude.semantics.engine.Evaluation) TermCondition(com.yahoo.prelude.semantics.rule.TermCondition) ProductionRule(com.yahoo.prelude.semantics.rule.ProductionRule) ReplacingProductionRule(com.yahoo.prelude.semantics.rule.ReplacingProductionRule) Query(com.yahoo.search.Query) ReplacingProductionRule(com.yahoo.prelude.semantics.rule.ReplacingProductionRule) ProductionList(com.yahoo.prelude.semantics.rule.ProductionList) RuleBase(com.yahoo.prelude.semantics.RuleBase)

Example 2 with ProductionRule

use of com.yahoo.prelude.semantics.rule.ProductionRule in project vespa by vespa-engine.

the class RuleEngine method evaluate.

/**
 * Evaluates a rule base over a query
 *
 * @param query the query to evaluate
 * @param traceLevel the level of tracing to do
 * @return the error caused by analyzing the query, or null if there was no error
 *         If there is an error, this query is destroyed (unusable)
 */
public String evaluate(Query query, int traceLevel) {
    // TODO: This is O(query size*rule base size). We'll eventually need to create indices
    // on rules to look up rule candidates per term to make it O(query size) instead
    // Probably create indices on the first term like Prolog implementations use to
    boolean matchedAnything = false;
    Evaluation evaluation = new Evaluation(query, traceLevel);
    evaluation.setStemming(rules.getStemming());
    evaluation.trace(2, "Evaluating query '" + evaluation.getQuery().getModel().getQueryTree().getRoot() + "':");
    for (ListIterator<ProductionRule> i = rules.ruleIterator(); i.hasNext(); ) {
        evaluation.reset();
        ProductionRule rule = i.next();
        boolean matched = matchRuleAtAllStartPoints(evaluation, rule);
        matchedAnything |= matched;
    }
    if (!matchedAnything)
        return null;
    String error = QueryCanonicalizer.canonicalize(query);
    if (query.getTraceLevel() >= 1)
        query.trace("SemanticSearcher: Rewrote query", true, 1);
    return error;
}
Also used : ProductionRule(com.yahoo.prelude.semantics.rule.ProductionRule)

Example 3 with ProductionRule

use of com.yahoo.prelude.semantics.rule.ProductionRule in project vespa by vespa-engine.

the class ProductionRuleTestCase method testProductionRule.

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());
}
Also used : NamedCondition(com.yahoo.prelude.semantics.rule.NamedCondition) ConditionReference(com.yahoo.prelude.semantics.rule.ConditionReference) Evaluation(com.yahoo.prelude.semantics.engine.Evaluation) RuleEvaluation(com.yahoo.prelude.semantics.engine.RuleEvaluation) TermCondition(com.yahoo.prelude.semantics.rule.TermCondition) ProductionRule(com.yahoo.prelude.semantics.rule.ProductionRule) ReplacingProductionRule(com.yahoo.prelude.semantics.rule.ReplacingProductionRule) Query(com.yahoo.search.Query) ReplacingProductionRule(com.yahoo.prelude.semantics.rule.ReplacingProductionRule) TermProduction(com.yahoo.prelude.semantics.rule.TermProduction) ReferenceTermProduction(com.yahoo.prelude.semantics.rule.ReferenceTermProduction) ReferenceTermProduction(com.yahoo.prelude.semantics.rule.ReferenceTermProduction) ProductionList(com.yahoo.prelude.semantics.rule.ProductionList) RuleBase(com.yahoo.prelude.semantics.RuleBase) RuleEvaluation(com.yahoo.prelude.semantics.engine.RuleEvaluation)

Aggregations

ProductionRule (com.yahoo.prelude.semantics.rule.ProductionRule)3 RuleBase (com.yahoo.prelude.semantics.RuleBase)2 Evaluation (com.yahoo.prelude.semantics.engine.Evaluation)2 ConditionReference (com.yahoo.prelude.semantics.rule.ConditionReference)2 NamedCondition (com.yahoo.prelude.semantics.rule.NamedCondition)2 ProductionList (com.yahoo.prelude.semantics.rule.ProductionList)2 ReplacingProductionRule (com.yahoo.prelude.semantics.rule.ReplacingProductionRule)2 TermCondition (com.yahoo.prelude.semantics.rule.TermCondition)2 Query (com.yahoo.search.Query)2 RuleEvaluation (com.yahoo.prelude.semantics.engine.RuleEvaluation)1 ReferenceTermProduction (com.yahoo.prelude.semantics.rule.ReferenceTermProduction)1 TermProduction (com.yahoo.prelude.semantics.rule.TermProduction)1