Class RuleBase


  • public class RuleBase
    extends Object
    A set of semantic production rules and named conditions used to analyze and rewrite queries
    Author:
    bratseth
    • Field Detail

      • automataFileName

        protected String automataFileName
        The name of the automata file used, or null if none
    • Constructor Detail

      • RuleBase

        public RuleBase()
        Creates an empty rule base. TODO: Disallow
      • RuleBase

        public RuleBase​(String name)
        Creates an empty rule base
    • Method Detail

      • createFromFile

        public static RuleBase createFromFile​(String ruleFile,
                                              String automataFile)
                                       throws 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 name
        automataFile - the automata file, or null to not use an automata
        Throws:
        IOException - if there is a problem reading one of the files
        ParseException - if the rule file can not be parsed correctly
        RuleBaseException - if the rule file contains inconsistencies
      • createFromString

        public static RuleBase createFromString​(String name,
                                                String ruleString,
                                                String automataFile)
                                         throws IOException,
                                                ParseException
        Creates a rule base from a string
        Parameters:
        name - the name of the rule base
        ruleString - the rule string to read
        automataFile - the automata file, or null to not use an automata
        Throws:
        IOException - if there is a problem reading the automata file
        ParseException - if the rule file can not be parsed correctly
        RuleBaseException - 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​(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
      • 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 analyze
        traceLevel - 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)
      • 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
        Overrides:
        equals in class Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • 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