Class 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
    • Constructor Summary

      Constructors 
      Constructor Description
      RuleBase()
      Creates an empty rule base.
      RuleBase​(java.lang.String name)
      Creates an empty rule base
    • 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 rules
      void addRule​(ProductionRule productionRule)  
      java.lang.String analyze​(Query query, int traceLevel)
      Analyzes a query over this rule base
      protected void annotatePhrase​(PhraseMatcher.Phrase phrase, Query query, int traceLevel)  
      java.util.Iterator<NamedCondition> conditionIterator()
      Returns the named conditions in added order
      static RuleBase createFromFile​(java.lang.String ruleFile, java.lang.String automataFile)
      Creates a rule base from a file
      static RuleBase createFromString​(java.lang.String name, java.lang.String ruleString, java.lang.String automataFile)
      Creates a rule base from a string
      boolean equals​(java.lang.Object object)
      Returns true if the given object is a rule base having the same name as this
      java.lang.String getAutomataFile()
      Returns the name of the automata file used, or null if none
      NamedCondition getCondition​(java.lang.String name)
      Returns a named condition, or null if no condition with that name exists
      java.lang.String getName()
      Returns the name of this rule base.
      PhraseMatcher getPhraseMatcher()
      Thread safely gets the phrase matcher to use in this
      java.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 name
      void 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 application
      protected void matchAutomata​(Query query, int traceLevel)  
      java.util.ListIterator<ProductionRule> ruleIterator()
      Returns the rules in added order
      java.util.List<ProductionRule> rules()
      Returns the rules unmodifiable
      void 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 application
      void 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 matcher
      void 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 inlined
      java.lang.String toString()  
      boolean usesAutomata()
      Returns whether this uses a phrase matcher automata
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • automataFileName

        protected java.lang.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​(java.lang.String name)
        Creates an empty rule base
    • 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 name
        automataFile - the automata file, or null to not use an automata
        Throws:
        java.io.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​(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 base
        ruleString - the rule string to read
        automataFile - the automata file, or null to not use an automata
        Throws:
        java.io.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​(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
      • 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 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)
      • 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 class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.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