Package com.yahoo.prelude.semantics
Class RuleBase
- java.lang.Object
-
- com.yahoo.prelude.semantics.RuleBase
-
public class RuleBase extends java.lang.Object
A set of semantic production rules and named conditions used to analyze and rewrite queries- Author:
- bratseth
-
-
Field Summary
Fields Modifier and Type Field Description protected java.lang.String
automataFileName
The name of the automata file used, or null if none
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addCondition(NamedCondition namedCondition)
Adds a named condition which can be referenced by rulesvoid
addRule(ProductionRule productionRule)
java.lang.String
analyze(Query query, int traceLevel)
Analyzes a query over this rule baseprotected void
annotatePhrase(PhraseMatcher.Phrase phrase, Query query, int traceLevel)
java.util.Iterator<NamedCondition>
conditionIterator()
Returns the named conditions in added orderstatic RuleBase
createFromFile(java.lang.String ruleFile, java.lang.String automataFile)
Creates a rule base from a filestatic RuleBase
createFromString(java.lang.String name, java.lang.String ruleString, java.lang.String automataFile)
Creates a rule base from a stringboolean
equals(java.lang.Object object)
Returns true if the given object is a rule base having the same name as thisjava.lang.String
getAutomataFile()
Returns the name of the automata file used, or null if noneNamedCondition
getCondition(java.lang.String name)
Returns a named condition, or null if no condition with that name existsjava.lang.String
getName()
Returns the name of this rule base.PhraseMatcher
getPhraseMatcher()
Thread safely gets the phrase matcher to use in thisjava.lang.String
getSource()
The identifying name of the source of this rule base.boolean
getStemming()
Returns whether stemmed matches are allowed.int
hashCode()
void
include(RuleBase include)
Include another rule base into this.boolean
includes(java.lang.String ruleBaseName)
Returns whether this rule base - directly or through other includes - includes the rule base with the given namevoid
initialize()
Call this when all rules are added, before any rule evaluation starts.boolean
isDefault()
Returns whether this base is default, the semantics of default is left to the applicationprotected void
matchAutomata(Query query, int traceLevel)
java.util.ListIterator<ProductionRule>
ruleIterator()
Returns the rules in added orderjava.util.List<ProductionRule>
rules()
Returns the rules unmodifiablevoid
setAutomataFile(java.lang.String automataFile)
Sets the name of the automata file to use as a source of condition matches.void
setDefault(boolean isDefault)
Sets whether this base is default, the semantics of default is left to the applicationvoid
setName(java.lang.String name)
Sets the name of this rule base.void
setPhraseMatcher(PhraseMatcher matcher)
Thread safely sets the phrase matcher to use in this, or null to not use a phrase matchervoid
setSource(java.lang.String source)
Sets the name of the source of this rule base.void
setStemming(boolean stemming)
Set to true to enable stemmed matches.java.lang.String
toContentString()
Returns a string containing all the rules and conditions of this rule base in the form they will be evaluated, with all included rule bases inlinedjava.lang.String
toString()
boolean
usesAutomata()
Returns whether this uses a phrase matcher automata
-
-
-
Method Detail
-
createFromFile
public static RuleBase createFromFile(java.lang.String ruleFile, java.lang.String automataFile) throws java.io.IOException, ParseException
Creates a rule base from a file- Parameters:
ruleFile
- the rule file to read. The name of the file (minus path) becomes the rule base nameautomataFile
- the automata file, or null to not use an automata- Throws:
java.io.IOException
- if there is a problem reading one of the filesParseException
- if the rule file can not be parsed correctlyRuleBaseException
- if the rule file contains inconsistencies
-
createFromString
public static RuleBase createFromString(java.lang.String name, java.lang.String ruleString, java.lang.String automataFile) throws java.io.IOException, ParseException
Creates a rule base from a string- Parameters:
name
- the name of the rule baseruleString
- the rule string to readautomataFile
- the automata file, or null to not use an automata- Throws:
java.io.IOException
- if there is a problem reading the automata fileParseException
- if the rule file can not be parsed correctlyRuleBaseException
- if the rule file contains inconsistencies
-
setStemming
public void setStemming(boolean stemming)
Set to true to enable stemmed matches. True by default
-
getStemming
public boolean getStemming()
Returns whether stemmed matches are allowed. True by default
-
include
public void include(RuleBase include)
Include another rule base into this. This transfers ownership of the given rule base - it can not be subsequently used for any purpose (including accessing).
Each rule base will only be included by the first include directive enountered for that rule base.
-
addCondition
public void addCondition(NamedCondition namedCondition)
Adds a named condition which can be referenced by rules
-
includes
public boolean includes(java.lang.String ruleBaseName)
Returns whether this rule base - directly or through other includes - includes the rule base with the given name
-
setName
public void setName(java.lang.String name)
Sets the name of this rule base. If this rule base is given to a searcher, it must be removed before the name change, and then re-added
-
getName
public java.lang.String getName()
Returns the name of this rule base. This is never null.
-
setAutomataFile
public void setAutomataFile(java.lang.String automataFile)
Sets the name of the automata file to use as a source of condition matches. To reload the automata, call this again. This can be done safely at any point by any thread while this rule base is in use.- Throws:
java.lang.IllegalArgumentException
- if the file is not found
-
getAutomataFile
public java.lang.String getAutomataFile()
Returns the name of the automata file used, or null if none
-
setDefault
public void setDefault(boolean isDefault)
Sets whether this base is default, the semantics of default is left to the application
-
isDefault
public boolean isDefault()
Returns whether this base is default, the semantics of default is left to the application
-
setPhraseMatcher
public void setPhraseMatcher(PhraseMatcher matcher)
Thread safely sets the phrase matcher to use in this, or null to not use a phrase matcher
-
getPhraseMatcher
public PhraseMatcher getPhraseMatcher()
Thread safely gets the phrase matcher to use in this
-
getSource
public java.lang.String getSource()
The identifying name of the source of this rule base. The absolute file name if this came from a file.
-
setSource
public void setSource(java.lang.String source)
Sets the name of the source of this rule base. If this came from a file, the source must be set to the absolute file name of the rule base
-
usesAutomata
public boolean usesAutomata()
Returns whether this uses a phrase matcher automata
-
addRule
public void addRule(ProductionRule productionRule)
-
getCondition
public NamedCondition getCondition(java.lang.String name)
Returns a named condition, or null if no condition with that name exists
-
initialize
public void initialize()
Call this when all rules are added, before any rule evaluation starts.- Throws:
RuleBaseException
- if there is an inconsistency in the rule base.
-
analyze
public java.lang.String analyze(Query query, int traceLevel)
Analyzes a query over this rule base- Parameters:
query
- the query to analyzetraceLevel
- the level of tracing to add to the query- Returns:
- the error caused by analyzing the query, or null if there was no error If there is an error, this query is destroyed (unusable)
-
matchAutomata
protected void matchAutomata(Query query, int traceLevel)
-
annotatePhrase
protected void annotatePhrase(PhraseMatcher.Phrase phrase, Query query, int traceLevel)
-
ruleIterator
public java.util.ListIterator<ProductionRule> ruleIterator()
Returns the rules in added order
-
rules
public java.util.List<ProductionRule> rules()
Returns the rules unmodifiable
-
conditionIterator
public java.util.Iterator<NamedCondition> conditionIterator()
Returns the named conditions in added order
-
equals
public boolean equals(java.lang.Object object)
Returns true if the given object is a rule base having the same name as this- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
toContentString
public java.lang.String toContentString()
Returns a string containing all the rules and conditions of this rule base in the form they will be evaluated, with all included rule bases inlined
-
-