blob: b320d10b43d9ce4f3de2b42c0379c8f081396d67 (
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.semantics.rule;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import com.yahoo.prelude.semantics.RuleBase;
import com.yahoo.prelude.semantics.engine.RuleEvaluation;
/**
* A query rewriting rule.
*
* @author bratseth
*/
public abstract class ProductionRule {
/** What must be true for this rule to be true */
private Condition condition;
/** What is produced when this rule is true */
private ProductionList production = new ProductionList();
/** The set of match name Strings which the production part of this rule references */
private final Set<String> matchReferences = new java.util.LinkedHashSet<>();
/** Sets what must be true for this rule to be true */
public void setCondition(Condition condition) { this.condition = condition; }
public Condition getCondition() { return condition; }
/** Sets what is produced when this rule is true */
public void setProduction(ProductionList production) { this.production = production; }
public ProductionList getProduction() { return production; }
/** Returns whether this rule matches the given query */
public boolean matches(RuleEvaluation e) {
e.setMatchReferences(matchReferences);
return condition.matches(e);
}
/**
* Returns the set of context names the production of this rule references
*
* @return an unmodifiable Set of condition context name Strings
*/
public Set<String> matchReferences() {
return Collections.unmodifiableSet(matchReferences);
}
public void makeReferences(RuleBase rules) {
condition.makeReferences(rules);
production.addMatchReferences(matchReferences);
}
/** Carries out the production of this rule */
public void produce(RuleEvaluation e) {
production.produce(e);
}
/**
* Returns the canonical string representation of this rule.
* This string representation can always be reparsed to produce an
* identical rule to this one.
*/
@Override
public String toString() {
return condition.toString() + " " + getSymbol() + " " + production.toString();
}
/**
* Returns the symbol of this production rule.
* All rules are on the form <code>condition symbol production</code>.
*/
protected abstract String getSymbol();
/**
* Returns true if it is known that this rule matches its own output.
* If it does, it will only be evaluated once, to avoid infinite loops.
* This default implementation returns false;
*/
public boolean isLoop() {
// TODO: There are many more possible loops, we should probably detect a few more obvious ones
if (conditionIsEllipsAndOtherNameSpacesOnly(getCondition())) return true;
if (producesItself()) return true;
return false;
}
private boolean conditionIsEllipsAndOtherNameSpacesOnly(Condition condition) {
if (condition instanceof EllipsisCondition) return true;
if (! (condition instanceof CompositeCondition)) return false;
for (Iterator<Condition> i = ((CompositeCondition)condition).conditionIterator(); i.hasNext(); ) {
Condition child = i.next();
if (child.getNameSpace() == null && conditionIsEllipsAndOtherNameSpacesOnly(child))
return true;
}
return false;
}
private boolean producesItself() {
return production.productionList()
.stream()
.anyMatch(p -> (p instanceof ReferenceTermProduction) && ((ReferenceTermProduction)p).producesAll());
}
}
|