Class Projog


  • public final class Projog
    extends java.lang.Object
    Provides an entry point for other Java code to interact with Projog.

    Contains a single instance of KnowledgeBase.

    • Constructor Detail

      • Projog

        public Projog​(ProjogProperties projogProperties,
                      ProjogListener... listeners)
        Constructs a new Projog object with the specified ProjogProperties and ProjogListeners.
    • Method Detail

      • consultFile

        public void consultFile​(java.io.File prologScript)
        Populates this objects KnowledgeBase with clauses read from the specified file.
        Parameters:
        prologScript - source of the prolog syntax defining the clauses to add
        Throws:
        ProjogException - if there is any problem parsing the syntax or adding the new clauses
      • consultReader

        public void consultReader​(java.io.Reader reader)
        Populates this objects KnowledgeBase with clauses read from the specified Reader.
        Parameters:
        reader - source of the prolog syntax defining the clauses to add
        Throws:
        ProjogException - if there is any problem parsing the syntax or adding the new clauses
      • consultResource

        public void consultResource​(java.lang.String resourceName)
        Populates this objects KnowledgeBase with clauses read from the specified resource.

        If prologSourceResourceName refers to an existing file on the file system then that file is used as the source of the prolog syntax else prologSourceResourceName is read from the classpath.

        Parameters:
        resourceName - source of the prolog syntax defining clauses to add to the KnowledgeBase
        Throws:
        ProjogException - if there is any problem parsing the syntax or adding the new clauses to the KnowledgeBase
      • setUserInput

        public void setUserInput​(java.io.InputStream is)
        Reassigns the "standard" input stream.

        By default the "standard" input stream will be System.in.

      • setUserOutput

        public void setUserOutput​(java.io.PrintStream ps)
        Reassigns the "standard" output stream.

        By default the "standard" output stream will be System.out.

      • addPredicateFactory

        public void addPredicateFactory​(PredicateKey key,
                                        PredicateFactory predicateFactory)
        Associates a PredicateFactory with the KnowledgeBase of this Projog.

        This method provides a mechanism for "plugging in" or "injecting" implementations of PredicateFactory at runtime. This mechanism provides an easy way to configure and extend the functionality of Projog - including adding functionality not possible to define in pure Prolog syntax.

        Parameters:
        key - The name and arity to associate the PredicateFactory with.
        predicateFactory - The PredicateFactory to be added.
        Throws:
        ProjogException - if there is already a PredicateFactory associated with the PredicateKey
      • addArithmeticOperator

        public void addArithmeticOperator​(PredicateKey key,
                                          ArithmeticOperator operator)
        Associates a ArithmeticOperator with this KnowledgeBase of this Projog.

        This method provides a mechanism for "plugging in" or "injecting" implementations of ArithmeticOperator at runtime. This mechanism provides an easy way to configure and extend the functionality of Projog - including adding functionality not possible to define in pure Prolog syntax.

        Parameters:
        key - The name and arity to associate the ArithmeticOperator with.
        operator - The instance of ArithmeticOperator to be associated with key.
        Throws:
        ProjogException - if there is already a ArithmeticOperator associated with the PredicateKey
      • executeOnce

        public void executeOnce​(java.lang.String prologQuery)
        Evaluate once the given query.

        The query will only be evaluated once, even if further solutions could of been found on backtracking.

        Parameters:
        prologQuery - prolog syntax representing a query
        Throws:
        ProjogException - if an error occurs parsing prologQuery or no solution can be found for it
        See Also:
        createPlan(String), createStatement(String), executeQuery(String)
      • addListener

        public void addListener​(ProjogListener listener)
        Registers an ProjogListener to receive notifications of events generated during the evaluation of Prolog goals.
        Parameters:
        listener - an listener to be added
      • formatTerm

        public java.lang.String formatTerm​(Term t)
        Returns a string representation of the specified Term.
        Parameters:
        t - the Term to represent as a string
        Returns:
        a string representation of the specified Term
        See Also:
        TermFormatter.formatTerm(Term)
      • getKnowledgeBase

        public KnowledgeBase getKnowledgeBase()
        Returns the KnowledgeBase associated with this object.

        Each Projog object is associated with its own KnowledgeBase. In normal usage it should not be necessary to call this method - as the other methods of Projog provide a more convenient mechanism for updating and querying the "core" inference engine.

        Returns:
        the KnowledgeBase associated with this object.
        See Also:
        KnowledgeBaseUtils
      • printProjogStackTrace

        public void printProjogStackTrace​(java.lang.Throwable exception)
        Prints the all clauses contained in the specified throwable's stack trace to the standard error stream.
      • printProjogStackTrace

        public void printProjogStackTrace​(java.lang.Throwable exception,
                                          java.io.PrintStream out)
        Prints the all clauses contained in the specified throwable's stack trace to the specified print stream.
      • getClauses

        private java.util.List<ClauseModel> getClauses​(java.lang.Throwable e)