public class RuleBase extends Object
Modifier and Type | Class and Description |
---|---|
private static class |
RuleBase.IncludeDirective
A placeholder for an included rule base until it is inlined
|
Modifier and Type | Field and Description |
---|---|
private RuleEngine |
analyzer
The analyzer used to do evaluations over this rule base
|
protected String |
automataFileName
The name of the automata file used, or null if none
|
private Set<String> |
includedNames
The names of the rule bases included indirectly or directly in this
Ordered by first to last included
|
private boolean |
isDefault
True if this rule base is default.
|
private String |
name
The globally identifying name of this rule base
|
private Map<String,NamedCondition> |
namedConditions |
private static PhraseMatcher |
nullPhraseMatcher |
private PhraseMatcher |
phraseMatcher
The matcher using an automata to match terms and phrases prior to matching rules
or the null matcher if no matcher is used.
|
private List<ProductionRule> |
productionRules |
private String |
source
The name of the source of this rules
|
private boolean |
stemming
Should we allow stemmed matches?
|
private boolean |
usesAutomata
True if this uses an automata, even if an automata is not present right now.
|
Constructor and Description |
---|
RuleBase()
Creates an empty rule base.
|
RuleBase(String name)
Creates an empty rule base
|
Modifier and Type | Method and Description |
---|---|
void |
addCondition(NamedCondition namedCondition)
Adds a named condition which can be referenced by rules
|
void |
addRule(ProductionRule productionRule) |
String |
analyze(Query query,
int traceLevel)
Analyzes a query over this rule base
|
protected void |
annotatePhrase(PhraseMatcher.Phrase phrase,
Query query,
int traceLevel) |
Iterator<NamedCondition> |
conditionIterator()
Returns the named conditions in added order
|
static RuleBase |
createFromFile(String ruleFile,
String automataFile)
Creates a rule base from a file
|
static RuleBase |
createFromString(String name,
String ruleString,
String automataFile)
Creates a rule base from a string
|
boolean |
equals(Object object)
Returns true if the given object is a rule base having the same name as this
|
private Condition |
findIncludedCondition(String name) |
String |
getAutomataFile()
Returns the name of the automata file used, or null if none
|
NamedCondition |
getCondition(String name)
Returns a named condition, or null if no condition with that name exists
|
String |
getName()
Returns the name of this rule base.
|
PhraseMatcher |
getPhraseMatcher()
Thread safely gets the phrase matcher to use in this
|
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(String ruleBaseName)
Returns whether this rule base - directly or through other includes - includes
the rule base with the given name
|
void |
initialize()
Call this when all rules are added, before any rule evaluation starts.
|
private void |
inlineIn(RuleBase receiver,
ListIterator<ProductionRule> receiverRules,
Set<RuleBase> included)
Recursively include this and everything it includes into the given rule base.
|
private void |
inlineIncluded()
Rules are order based - they are included recursively depth first
|
boolean |
isDefault()
Returns whether this base is default, the semantics of default is left to the application
|
private void |
makeReferences() |
protected void |
matchAutomata(Query query,
int traceLevel) |
private void |
resolveSuper(Condition condition,
Condition superCondition) |
ListIterator<ProductionRule> |
ruleIterator()
Returns the rules in added order
|
List<ProductionRule> |
rules()
Returns the rules unmodifiable
|
void |
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 application
|
void |
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 matcher
|
void |
setSource(String source)
Sets the name of the source of this rule base.
|
void |
setStemming(boolean stemming)
Set to true to enable stemmed matches.
|
(package private) void |
setUsesAutomata(boolean usesAutomata)
Set to truew if this uses an automata, even if an automata is not present right now.
|
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
|
String |
toString() |
boolean |
usesAutomata()
Returns whether this uses a phrase matcher automata
|
private String name
private String source
protected String automataFileName
private boolean isDefault
private List<ProductionRule> productionRules
private Map<String,NamedCondition> namedConditions
private RuleEngine analyzer
private static final PhraseMatcher nullPhraseMatcher
private PhraseMatcher phraseMatcher
private Set<String> includedNames
private boolean usesAutomata
private boolean stemming
public RuleBase()
public RuleBase(String name)
public static RuleBase createFromFile(String ruleFile, String automataFile) throws IOException, com.yahoo.prelude.semantics.parser.ParseException
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 automataIOException
- if there is a problem reading one of the filescom.yahoo.prelude.semantics.parser.ParseException
- if the rule file can not be parsed correctlyRuleBaseException
- if the rule file contains inconsistenciespublic static RuleBase createFromString(String name, String ruleString, String automataFile) throws IOException, com.yahoo.prelude.semantics.parser.ParseException
name
- the name of the rule baseruleString
- the rule string to readautomataFile
- the automata file, or null to not use an automataIOException
- if there is a problem reading the automata filecom.yahoo.prelude.semantics.parser.ParseException
- if the rule file can not be parsed correctlyRuleBaseException
- if the rule file contains inconsistenciespublic void setStemming(boolean stemming)
public boolean getStemming()
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.
private void inlineIncluded()
private void inlineIn(RuleBase receiver, ListIterator<ProductionRule> receiverRules, Set<RuleBase> included)
public void addCondition(NamedCondition namedCondition)
public boolean includes(String ruleBaseName)
public void setName(String name)
public String getName()
public void setAutomataFile(String automataFile)
IllegalArgumentException
- if the file is not foundpublic String getAutomataFile()
public void setDefault(boolean isDefault)
public boolean isDefault()
public void setPhraseMatcher(PhraseMatcher matcher)
public PhraseMatcher getPhraseMatcher()
public String getSource()
public void setSource(String source)
public boolean usesAutomata()
void setUsesAutomata(boolean usesAutomata)
public void addRule(ProductionRule productionRule)
public NamedCondition getCondition(String name)
public void initialize()
RuleBaseException
- if there is an inconsistency in the rule base.public String analyze(Query query, int traceLevel)
query
- the query to analyzetraceLevel
- the level of tracing to add to the queryprotected void matchAutomata(Query query, int traceLevel)
protected void annotatePhrase(PhraseMatcher.Phrase phrase, Query query, int traceLevel)
private void makeReferences()
public ListIterator<ProductionRule> ruleIterator()
public List<ProductionRule> rules()
public Iterator<NamedCondition> conditionIterator()
public boolean equals(Object object)
public String toContentString()
Copyright © 2018. All rights reserved.