Class Projog

java.lang.Object
org.projog.api.Projog

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

Contains a single instance of KnowledgeBase.

Class diagram
  • Constructor Details

  • Method Details

    • consultFile

      public void consultFile(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(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(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(InputStream is)
      Reassigns the "standard" input stream.

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

    • setUserOutput

      public void setUserOutput(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
    • createPlan

      public QueryPlan createPlan(String prologQuery)
      Creates a QueryPlan for querying the Projog environment.

      The newly created object represents the query parsed from the specified syntax. A single QueryPlan can be used to create multiple QueryStatement objects.

      Parameters:
      prologQuery - prolog syntax representing a query
      Returns:
      representation of the query parsed from the specified syntax
      Throws:
      ProjogException - if an error occurs parsing prologQuery
      See Also:
    • createStatement

      public QueryStatement createStatement(String prologQuery)
      Creates a QueryStatement for querying the Projog environment.

      The newly created object represents the query parsed from the specified syntax. Before the query is executed, values can be assigned to variables in the query by using QueryStatement.setTerm(String, Term). The query can be executed by calling QueryStatement.executeQuery(). The resulting QueryResult can be used to access the result.

      Note: If you do not intend to assign terms to variables then executeQuery(String) can be called instead.

      Parameters:
      prologQuery - prolog syntax representing a query
      Returns:
      representation of the query parsed from the specified syntax
      Throws:
      ProjogException - if an error occurs parsing prologQuery
      See Also:
    • executeQuery

      public QueryResult executeQuery(String prologQuery)
      Creates a QueryResult for querying the Projog environment.

      The newly created object represents the query parsed from the specified syntax. The QueryResult.next() and QueryResult.getTerm(String) methods can be used to evaluate the query and access values unified to the variables of the query.

      Parameters:
      prologQuery - prolog syntax representing a query
      Returns:
      representation of the query parsed from the specified syntax
      Throws:
      ProjogException - if an error occurs parsing prologQuery
      See Also:
    • executeOnce

      public void executeOnce(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:
    • 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 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:
    • 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:
    • printProjogStackTrace

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

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

      public ProjogStackTraceElement[] getStackTrace(Throwable exception)
      Provides programmatic access to the stack trace information printed by printProjogStackTrace(Throwable).