summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@gmail.com>2021-04-05 09:49:55 +0200
committerJon Bratseth <bratseth@gmail.com>2021-04-05 09:49:55 +0200
commite4060156feb3bd441ea9554621e11a7244962968 (patch)
tree6a2501e4c25e6a70e98efaf73adddc979f55744c /container-search/src/main/java/com/yahoo/prelude
parent55766b25eadd4c9527040fd1a8b3458fc526e713 (diff)
Cleanup
Diffstat (limited to 'container-search/src/main/java/com/yahoo/prelude')
-rw-r--r--container-search/src/main/java/com/yahoo/prelude/semantics/RuleBase.java101
-rw-r--r--container-search/src/main/java/com/yahoo/prelude/semantics/engine/RuleEvaluation.java93
-rw-r--r--container-search/src/main/java/com/yahoo/prelude/semantics/rule/ProductionList.java24
3 files changed, 107 insertions, 111 deletions
diff --git a/container-search/src/main/java/com/yahoo/prelude/semantics/RuleBase.java b/container-search/src/main/java/com/yahoo/prelude/semantics/RuleBase.java
index ccc9c1d2f8f..feab8faf898 100644
--- a/container-search/src/main/java/com/yahoo/prelude/semantics/RuleBase.java
+++ b/container-search/src/main/java/com/yahoo/prelude/semantics/RuleBase.java
@@ -26,43 +26,43 @@ public class RuleBase {
private String source;
/** The name of the automata file used, or null if none */
- protected String automataFileName=null;
+ protected String automataFileName = null;
/**
* True if this rule base is default.
* The semantics of default is left to the surrounding framework
*/
- private boolean isDefault=false;
+ private boolean isDefault = false;
- private List<ProductionRule> productionRules=new java.util.ArrayList<>();
+ private final List<ProductionRule> productionRules = new java.util.ArrayList<>();
- private Map<String, NamedCondition> namedConditions=new java.util.LinkedHashMap<>();
+ private Map<String, NamedCondition> namedConditions = new java.util.LinkedHashMap<>();
/** The analyzer used to do evaluations over this rule base */
- private RuleEngine analyzer=new RuleEngine(this);
+ private final RuleEngine analyzer = new RuleEngine(this);
- private static final PhraseMatcher nullPhraseMatcher=PhraseMatcher.getNullMatcher();
+ private static final PhraseMatcher nullPhraseMatcher = PhraseMatcher.getNullMatcher();
/**
* The matcher using an automata to match terms and phrases prior to matching rules
* or the null matcher if no matcher is used.
*/
- private PhraseMatcher phraseMatcher=nullPhraseMatcher;
+ private PhraseMatcher phraseMatcher = nullPhraseMatcher;
/**
* The names of the rule bases included indirectly or directly in this
* Ordered by first to last included
*/
- private Set<String> includedNames=new java.util.LinkedHashSet<>();
+ private final Set<String> includedNames = new java.util.LinkedHashSet<>();
/**
* True if this uses an automata, even if an automata is not present right now. Useful to validate without
* having automatas available
*/
- private boolean usesAutomata=false;
+ private boolean usesAutomata = false;
/** Should we allow stemmed matches? */
- private boolean stemming=true;
+ private boolean stemming = true;
/** Creates an empty rule base. TODO: Disallow */
public RuleBase() {
@@ -82,8 +82,8 @@ public class RuleBase {
* @throws ParseException if the rule file can not be parsed correctly
* @throws RuleBaseException if the rule file contains inconsistencies
*/
- public static RuleBase createFromFile(String ruleFile,String automataFile) throws java.io.IOException, ParseException {
- return new RuleImporter().importFile(ruleFile,automataFile);
+ public static RuleBase createFromFile(String ruleFile, String automataFile) throws java.io.IOException, ParseException {
+ return new RuleImporter().importFile(ruleFile, automataFile);
}
/**
@@ -96,14 +96,14 @@ public class RuleBase {
* @throws com.yahoo.prelude.semantics.parser.ParseException if the rule file can not be parsed correctly
* @throws com.yahoo.prelude.semantics.RuleBaseException if the rule file contains inconsistencies
*/
- public static RuleBase createFromString(String name,String ruleString,String automataFile) throws java.io.IOException, ParseException {
- RuleBase base=new RuleImporter().importString(ruleString,automataFile,new RuleBase());
+ public static RuleBase createFromString(String name, String ruleString, String automataFile) throws java.io.IOException, ParseException {
+ RuleBase base = new RuleImporter().importString(ruleString, automataFile, new RuleBase());
base.setName(name);
return base;
}
/** Set to true to enable stemmed matches. True by default */
- public void setStemming(boolean stemming) { this.stemming=stemming; }
+ public void setStemming(boolean stemming) { this.stemming = stemming; }
/** Returns whether stemmed matches are allowed. True by default */
public boolean getStemming() { return stemming; }
@@ -125,19 +125,19 @@ public class RuleBase {
/** Rules are order based - they are included recursively depth first */
private void inlineIncluded() {
// Re-add our own conditions last to - added later overrides
- Map<String, NamedCondition> thisConditions=namedConditions;
- namedConditions=new LinkedHashMap<>();
+ Map<String, NamedCondition> thisConditions = namedConditions;
+ namedConditions = new LinkedHashMap<>();
- Set<RuleBase> included=new HashSet<>();
+ Set<RuleBase> included = new HashSet<>();
included.add(this);
- for (ListIterator<ProductionRule> i=productionRules.listIterator(); i.hasNext(); ) {
- ProductionRule rule=i.next();
+ for (ListIterator<ProductionRule> i = productionRules.listIterator(); i.hasNext(); ) {
+ ProductionRule rule = i.next();
if ( ! (rule instanceof IncludeDirective) ) continue;
i.remove();
- RuleBase toInclude=((IncludeDirective)rule).getIncludedBase();
+ RuleBase toInclude = ((IncludeDirective)rule).getIncludedBase();
if ( ! included.contains(toInclude))
- toInclude.inlineIn(this,i,included);
+ toInclude.inlineIn(this, i, included);
}
namedConditions.putAll(thisConditions);
@@ -147,14 +147,14 @@ public class RuleBase {
* Recursively include this and everything it includes into the given rule base.
* Skips bases already included in this.
*/
- private void inlineIn(RuleBase receiver,ListIterator<ProductionRule> receiverRules,Set<RuleBase> included) {
+ private void inlineIn(RuleBase receiver, ListIterator<ProductionRule> receiverRules, Set<RuleBase> included) {
if (included.contains(this)) return;
included.add(this);
- for (Iterator<ProductionRule> i=productionRules.iterator(); i.hasNext(); ) {
- ProductionRule rule=i.next();
+ for (Iterator<ProductionRule> i = productionRules.iterator(); i.hasNext(); ) {
+ ProductionRule rule = i.next();
if (rule instanceof IncludeDirective)
- ((IncludeDirective)rule).getIncludedBase().inlineIn(receiver,receiverRules,included);
+ ((IncludeDirective)rule).getIncludedBase().inlineIn(receiver, receiverRules, included);
else
receiverRules.add(rule);
}
@@ -164,11 +164,11 @@ public class RuleBase {
/** Adds a named condition which can be referenced by rules */
public void addCondition(NamedCondition namedCondition) {
- namedConditions.put(namedCondition.getName(),namedCondition);
+ namedConditions.put(namedCondition.getName(), namedCondition);
- Condition condition=namedCondition.getCondition();
- Condition superCondition=findIncludedCondition(namedCondition.getName());
- resolveSuper(condition,superCondition);
+ Condition condition = namedCondition.getCondition();
+ Condition superCondition = findIncludedCondition(namedCondition.getName());
+ resolveSuper(condition, superCondition);
}
private void resolveSuper(Condition condition,Condition superCondition) {
@@ -176,24 +176,22 @@ public class RuleBase {
((SuperCondition)condition).setCondition(superCondition);
}
else if (condition instanceof CompositeCondition) {
- for (Iterator<Condition> i=((CompositeCondition)condition).conditionIterator(); i.hasNext(); ) {
- Condition subCondition=i.next();
- resolveSuper(subCondition,superCondition);
+ for (Iterator<Condition> i = ((CompositeCondition)condition).conditionIterator(); i.hasNext(); ) {
+ Condition subCondition = i.next();
+ resolveSuper(subCondition, superCondition);
}
}
}
private Condition findIncludedCondition(String name) {
- for (Iterator<ProductionRule> i=productionRules.iterator(); i.hasNext(); ) {
- ProductionRule rule=i.next();
+ for (Iterator<ProductionRule> i = productionRules.iterator(); i.hasNext(); ) {
+ ProductionRule rule = i.next();
if ( ! (rule instanceof IncludeDirective) ) continue;
- RuleBase included=((IncludeDirective)rule).getIncludedBase();
- NamedCondition condition=included.getCondition(name);
- if (condition!=null) return condition.getCondition();
+ RuleBase included = ((IncludeDirective)rule).getIncludedBase();
+ NamedCondition condition = included.getCondition(name);
+ if (condition != null) return condition.getCondition();
included.findIncludedCondition(name);
- // FIXME: dead code commented out
- // if (condition!=null) return condition.getCondition();
}
return null;
}
@@ -212,8 +210,8 @@ public class RuleBase {
* change, and then re-added
*/
public void setName(String name) {
- Validator.ensureNotNull("Rule base name",name);
- this.name=name;
+ Validator.ensureNotNull("Rule base name", name);
+ this.name = name;
}
/** Returns the name of this rule base. This is never null. */
@@ -230,27 +228,27 @@ public class RuleBase {
if ( ! new File(automataFile).exists())
throw new IllegalArgumentException("Automata file '" + automataFile + "' " +
"included in " + this + " not found");
- phraseMatcher=new PhraseMatcher(automataFile);
+ phraseMatcher = new PhraseMatcher(automataFile);
phraseMatcher.setIgnorePluralForm(true);
phraseMatcher.setMatchAll(true);
phraseMatcher.setMatchPhraseItems(true);
phraseMatcher.setMatchSingleItems(true);
setPhraseMatcher(phraseMatcher);
- this.automataFileName=automataFile;
+ this.automataFileName = automataFile;
}
/** Returns the name of the automata file used, or null if none */
public String getAutomataFile() { return automataFileName; }
/** Sets whether this base is default, the semantics of default is left to the application */
- public void setDefault(boolean isDefault) { this.isDefault=isDefault; }
+ public void setDefault(boolean isDefault) { this.isDefault = isDefault; }
/** Returns whether this base is default, the semantics of default is left to the application */
public boolean isDefault() { return isDefault; }
/** Thread safely sets the phrase matcher to use in this, or null to not use a phrase matcher */
public synchronized void setPhraseMatcher(PhraseMatcher matcher) {
- if (matcher==null)
+ if (matcher == null)
this.phraseMatcher = nullPhraseMatcher;
else
this.phraseMatcher = matcher;
@@ -282,7 +280,7 @@ public class RuleBase {
* Set to truew if this uses an automata, even if an automata is not present right now.
* Useful to validate without having automatas available
*/
- void setUsesAutomata(boolean usesAutomata) { this.usesAutomata=usesAutomata; }
+ void setUsesAutomata(boolean usesAutomata) { this.usesAutomata = usesAutomata; }
// Note that included rules are added though a list iterator, not this */
public void addRule(ProductionRule productionRule) {
@@ -399,13 +397,13 @@ public class RuleBase {
public String toContentString() {
StringBuilder b = new StringBuilder();
for (Iterator<ProductionRule> i = productionRules.iterator(); i.hasNext(); ) {
- b.append(i.next().toString());
+ b.append(i.next());
b.append("\n");
}
b.append("\n");
b.append("\n");
for (Iterator<NamedCondition> i = namedConditions.values().iterator(); i.hasNext(); ) {
- b.append(i.next().toString());
+ b.append(i.next());
b.append("\n");
}
return b.toString();
@@ -414,10 +412,10 @@ public class RuleBase {
/** A placeholder for an included rule base until it is inlined */
private static class IncludeDirective extends ProductionRule {
- private RuleBase includedBase;
+ private final RuleBase includedBase;
public IncludeDirective(RuleBase ruleBase) {
- this.includedBase=ruleBase;
+ this.includedBase = ruleBase;
}
public RuleBase getIncludedBase() { return includedBase; }
@@ -425,7 +423,6 @@ public class RuleBase {
/** Not used */
public String getSymbol() { return ""; }
-
}
}
diff --git a/container-search/src/main/java/com/yahoo/prelude/semantics/engine/RuleEvaluation.java b/container-search/src/main/java/com/yahoo/prelude/semantics/engine/RuleEvaluation.java
index 29a781b1e68..09fcb6a424f 100644
--- a/container-search/src/main/java/com/yahoo/prelude/semantics/engine/RuleEvaluation.java
+++ b/container-search/src/main/java/com/yahoo/prelude/semantics/engine/RuleEvaluation.java
@@ -10,7 +10,7 @@ import com.yahoo.prelude.semantics.rule.ProductionRule;
import java.util.*;
/**
- * A particular evalutation of a particular rule.
+ * A particular evaluation of a particular rule.
*
* @author bratseth
*/
@@ -23,7 +23,7 @@ public class RuleEvaluation {
// Remember that whenever state is added to this class, you
// must consider whether/how to make that state backtrackable
- // by savinginformation in choicepoint.state
+ // by saving information in choicepoint.state
/** The items to match in this evaluation */
private List<FlattenedItem> items;
@@ -41,12 +41,12 @@ public class RuleEvaluation {
private String currentContext;
/** A list of referencedMatches */
- private List<ReferencedMatches> referencedMatchesList = new java.util.ArrayList<>();
+ private final List<ReferencedMatches> referencedMatchesList = new java.util.ArrayList<>();
- private List<Match> nonreferencedMatches = new java.util.ArrayList<>();
+ private final List<Match> nonreferencedMatches = new java.util.ArrayList<>();
/** The evaluation owning this */
- private Evaluation evaluation;
+ private final Evaluation evaluation;
/** The choice points saved in this evaluation */
private Stack<Choicepoint> choicepoints = null;
@@ -82,7 +82,7 @@ public class RuleEvaluation {
choicepoints.clear();
}
- public void setMatchReferences(Set<String> matchReferences) { this.matchReferences=matchReferences; }
+ public void setMatchReferences(Set<String> matchReferences) { this.matchReferences = matchReferences; }
/**
* <p>Calculates an id which is unique for each match (the totality of the matched terms)
@@ -96,23 +96,23 @@ public class RuleEvaluation {
* we add other kinds of conditions.</p>
*/
int calculateMatchDigest(ProductionRule rule) {
- int matchDigest=rule.hashCode();
- int matchCounter=1;
- for (Iterator<ReferencedMatches> i=referencedMatchesList.iterator(); i.hasNext(); ) {
- ReferencedMatches matches=i.next();
- int termCounter=0;
- for (Iterator<Match> j=matches.matchIterator(); j.hasNext(); ) {
- Match match=j.next();
- matchDigest=7*matchDigest*matchCounter+
- 71*termCounter+
- match.hashCode();
+ int matchDigest = rule.hashCode();
+ int matchCounter = 1;
+ for (Iterator<ReferencedMatches> i = referencedMatchesList.iterator(); i.hasNext(); ) {
+ ReferencedMatches matches = i.next();
+ int termCounter = 0;
+ for (Iterator<Match> j = matches.matchIterator(); j.hasNext(); ) {
+ Match match = j.next();
+ matchDigest = 7 * matchDigest * matchCounter+
+ 71 * termCounter +
+ match.hashCode();
termCounter++;
}
matchCounter++;
}
- for (Iterator<Match> i=nonreferencedMatches.iterator(); i.hasNext(); ) {
- Match match=i.next();
- matchDigest=7*matchDigest*matchCounter+match.hashCode();
+ for (Iterator<Match> i = nonreferencedMatches.iterator(); i.hasNext(); ) {
+ Match match = i.next();
+ matchDigest = 7 * matchDigest * matchCounter + match.hashCode();
matchCounter++;
}
return matchDigest;
@@ -139,7 +139,7 @@ public class RuleEvaluation {
/** Sets the current position */
public void setPosition(int position) {
- this.position=position;
+ this.position = position;
}
/** Returns the total number of items to match in this evaluation */
@@ -151,21 +151,21 @@ public class RuleEvaluation {
public Object getValue() { return value; }
/** Sets the last value returned by a condition in this evaluatiino, or null */
- public void setValue(Object value) { this.value=value; }
+ public void setValue(Object value) { this.value = value; }
/** Returns whether we are evaluating inside a condition which inverts the truth value */
public boolean isInNegation() { return inNegation; }
/** sets whether we are evaluating inside a condition which inverts the truth value */
- public void setInNegation(boolean inNegation) { this.inNegation=inNegation; }
+ public void setInNegation(boolean inNegation) { this.inNegation = inNegation; }
/** Returns the current position into the terms this evaluates over */
public int getPosition() { return position; }
/** Sets a new current label and returns the previous one */
public String setCurrentLabel(String currentLabel) {
- String oldLabel=currentLabel;
- this.currentLabel=currentLabel;
+ String oldLabel = currentLabel;
+ this.currentLabel = currentLabel;
return oldLabel;
}
@@ -179,8 +179,8 @@ public class RuleEvaluation {
public FlattenedItem next() {
position++;
- if (position>=items.size()) {
- position=items.size();
+ if (position >= items.size()) {
+ position = items.size();
return null;
}
@@ -189,17 +189,16 @@ public class RuleEvaluation {
// TODO: Simplistic yet. Nedd to support context nesting etc.
public void entering(String context) {
- if (context==null) return;
- if (matchReferences!=null && matchReferences.contains(context))
- currentContext=context;
-
+ if (context == null) return;
+ if (matchReferences != null && matchReferences.contains(context))
+ currentContext = context;
}
public void leaving(String context) {
- if (context==null) return;
- if (currentContext==null) return;
+ if (context == null) return;
+ if (currentContext == null) return;
if (currentContext.equals(context))
- currentContext=null;
+ currentContext = null;
}
/**
@@ -269,7 +268,7 @@ public class RuleEvaluation {
* @param termType the kind of item to index, this decides the resulting structure
*/
public void insertItem(Item item, CompositeItem parent, int index, TermType termType) {
- evaluation.insertItem(item,parent,index,termType);
+ evaluation.insertItem(item, parent, index, termType);
}
/** Returns a read-only view of the items of this */
@@ -282,7 +281,7 @@ public class RuleEvaluation {
}
public void trace(int level,String string) {
- evaluation.trace(level,string);
+ evaluation.trace(level, string);
}
public int getTraceLevel() {
@@ -304,24 +303,24 @@ public class RuleEvaluation {
* @param create true to create this choicepoint if it is missing
* @return the choicepoint, or null if not present, and create is false
*/
- public Choicepoint getChoicepoint(Condition condition,boolean create) {
- if (choicepoints==null) {
- if (!create) return null;
- choicepoints=new java.util.Stack<>();
+ public Choicepoint getChoicepoint(Condition condition, boolean create) {
+ if (choicepoints == null) {
+ if ( ! create) return null;
+ choicepoints = new java.util.Stack<>();
}
Choicepoint choicepoint=lookupChoicepoint(condition);
- if (choicepoint==null) {
- if (!create) return null;
- choicepoint=new Choicepoint(this,condition);
+ if (choicepoint == null) {
+ if ( ! create) return null;
+ choicepoint = new Choicepoint(this, condition);
choicepoints.push(choicepoint);
}
return choicepoint;
}
private Choicepoint lookupChoicepoint(Condition condition) {
- for (Iterator<Choicepoint> i=choicepoints.iterator(); i.hasNext(); ) {
- Choicepoint choicepoint=i.next();
- if (condition==choicepoint.getCondition())
+ for (Iterator<Choicepoint> i = choicepoints.iterator(); i.hasNext(); ) {
+ Choicepoint choicepoint = i.next();
+ if (condition == choicepoint.getCondition())
return choicepoint;
}
return null;
@@ -337,8 +336,8 @@ public class RuleEvaluation {
/** Remove all the terms recognized by this match */
public void removeMatches(ReferencedMatches matches) {
- for (Iterator<Match> i=matches.matchIterator(); i.hasNext(); ) {
- Match match=i.next();
+ for (Iterator<Match> i = matches.matchIterator(); i.hasNext(); ) {
+ Match match = i.next();
removeItemByIdentity(match.getItem());
}
}
diff --git a/container-search/src/main/java/com/yahoo/prelude/semantics/rule/ProductionList.java b/container-search/src/main/java/com/yahoo/prelude/semantics/rule/ProductionList.java
index 5b973228b1f..49d15710cd7 100644
--- a/container-search/src/main/java/com/yahoo/prelude/semantics/rule/ProductionList.java
+++ b/container-search/src/main/java/com/yahoo/prelude/semantics/rule/ProductionList.java
@@ -15,10 +15,10 @@ import com.yahoo.prelude.semantics.engine.RuleEvaluation;
*/
public class ProductionList {
- private List<Production> productions =new java.util.ArrayList<>();
+ private final List<Production> productions = new java.util.ArrayList<>();
/** True to replace by the production, false to add it */
- private boolean replacing=true;
+ private boolean replacing = true;
public void addProduction(Production term) {
term.setReplacing(replacing);
@@ -28,12 +28,12 @@ public class ProductionList {
/** True to replace, false to add, default true */
void setReplacing(boolean replacing) {
- for (Iterator<Production> i=productions.iterator(); i.hasNext(); ) {
- Production production=i.next();
+ for (Iterator<Production> i = productions.iterator(); i.hasNext(); ) {
+ Production production = i.next();
production.setReplacing(replacing);
}
- this.replacing=replacing;
+ this.replacing = replacing;
}
/** Returns an unmodifiable view of the productions in this */
@@ -42,22 +42,22 @@ public class ProductionList {
public int getTermCount() { return productions.size(); }
void addMatchReferences(Set<String> matchReferences) {
- for (Iterator<Production> i=productions.iterator(); i.hasNext(); ) {
- Production term=i.next();
+ for (Iterator<Production> i = productions.iterator(); i.hasNext(); ) {
+ Production term = i.next();
term.addMatchReferences(matchReferences);
}
}
public void produce(RuleEvaluation e) {
- for (int i=0; i<productions.size(); i++) {
- productions.get(i).produce(e,i);
+ for (int i = 0; i < productions.size(); i++) {
+ productions.get(i).produce(e, i);
}
}
public String toString() {
- StringBuilder buffer=new StringBuilder();
- for (Iterator<Production> i=productions.iterator(); i.hasNext(); ) {
- buffer.append(i.next().toString());
+ StringBuilder buffer = new StringBuilder();
+ for (Iterator<Production> i = productions.iterator(); i.hasNext(); ) {
+ buffer.append(i.next());
if (i.hasNext())
buffer.append(" ");
}