Package com.yahoo.prelude.semantics
Class RuleBase
- java.lang.Object
-
- com.yahoo.prelude.semantics.RuleBase
-
public class RuleBase extends Object
A set of semantic production rules and named conditions used to analyze and rewrite queries- Author:
- bratseth
-
-
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)
String
analyze(Query query, int traceLevel)
Analyzes a query over this rule baseprotected void
annotatePhrase(PhraseMatcher.Phrase phrase, Query query, int traceLevel)
Iterator<NamedCondition>
conditionIterator()
Returns the named conditions in added orderstatic RuleBase
createFromFile(String ruleFile, String automataFile, com.yahoo.language.Linguistics linguistics)
Creates a rule base from filestatic RuleBase
createFromString(String name, String ruleString, String automataFile, com.yahoo.language.Linguistics linguistics)
Creates a rule base from a stringboolean
equals(Object object)
Returns true if the given object is a rule base having the same name as thisString
getAutomataFile()
Returns the name of the automata file used, or null if noneNamedCondition
getCondition(String name)
Returns a named condition, or null if no condition with that name existsString
getName()
Returns the name of this rule base.PhraseMatcher
getPhraseMatcher()
Thread safely gets the phrase matcher to use in thisString
getSource()
The identifying name of the source of this rule base.int
hashCode()
void
include(RuleBase include)
Include another rule base into this.boolean
includes(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)
ListIterator<ProductionRule>
ruleIterator()
Returns the rules in added orderList<ProductionRule>
rules()
Returns the rules unmodifiablevoid
setAutomataFile(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(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(String source)
Sets the name of the source of this rule base.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 inlinedString
toString()
boolean
usesAutomata()
Returns whether this uses a phrase matcher automata
-
-
-
Constructor Detail
-
RuleBase
public RuleBase(String name)
Creates an empty rule base
-
-
Method Detail
-
createFromFile
public static RuleBase createFromFile(String ruleFile, String automataFile, com.yahoo.language.Linguistics linguistics) throws IOException, ParseException
Creates a rule base from file- Parameters:
ruleFile
- the rule file to read. The name of the file (minus path) becomes the rule base name.automataFile
- the automata file, or null to not use an automata- Throws:
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(String name, String ruleString, String automataFile, com.yahoo.language.Linguistics linguistics) throws 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:
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
-
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(String ruleBaseName)
Returns whether this rule base - directly or through other includes - includes the rule base with the given name
-
setName
public void setName(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 String getName()
Returns the name of this rule base. This is never null.
-
setAutomataFile
public void setAutomataFile(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:
IllegalArgumentException
- if the file is not found
-
getAutomataFile
public 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 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(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(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 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 ListIterator<ProductionRule> ruleIterator()
Returns the rules in added order
-
rules
public List<ProductionRule> rules()
Returns the rules unmodifiable
-
conditionIterator
public Iterator<NamedCondition> conditionIterator()
Returns the named conditions in added order
-
equals
public boolean equals(Object object)
Returns true if the given object is a rule base having the same name as this
-
toContentString
public 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
-
-