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()));
}
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;
}
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());
}
Aggregations