Class Projog
- java.lang.Object
-
- org.projog.api.Projog
-
public final class Projog extends java.lang.ObjectProvides an entry point for other Java code to interact with Projog.Contains a single instance of
KnowledgeBase.
-
-
Field Summary
Fields Modifier and Type Field Description private KnowledgeBasekb
-
Constructor Summary
Constructors Constructor Description Projog(ProjogListener... listeners)Projog(ProjogProperties projogProperties, ProjogListener... listeners)Constructs a newProjogobject with the specifiedProjogPropertiesandProjogListeners.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddArithmeticOperator(PredicateKey key, ArithmeticOperator operator)voidaddListener(ProjogListener listener)Registers anProjogListenerto receive notifications of events generated during the evaluation of Prolog goals.voidaddPredicateFactory(PredicateKey key, PredicateFactory predicateFactory)voidconsultFile(java.io.File prologScript)Populates this objectsKnowledgeBasewith clauses read from the specified file.voidconsultReader(java.io.Reader reader)Populates this objectsKnowledgeBasewith clauses read from the specifiedReader.voidconsultResource(java.lang.String resourceName)Populates this objectsKnowledgeBasewith clauses read from the specified resource.QueryPlancreatePlan(java.lang.String prologQuery)Creates aQueryPlanfor querying the Projog environment.QueryStatementcreateStatement(java.lang.String prologQuery)Creates aQueryStatementfor querying the Projog environment.voidexecuteOnce(java.lang.String prologQuery)Evaluate once the given query.QueryResultexecuteQuery(java.lang.String prologQuery)Creates aQueryResultfor querying the Projog environment.java.lang.StringformatTerm(Term t)Returns a string representation of the specifiedTerm.private java.util.List<ClauseModel>getClauses(java.lang.Throwable e)KnowledgeBasegetKnowledgeBase()Returns theKnowledgeBaseassociated with this object.ProjogStackTraceElement[]getStackTrace(java.lang.Throwable exception)Provides programmatic access to the stack trace information printed byprintProjogStackTrace(Throwable).voidprintProjogStackTrace(java.lang.Throwable exception)Prints the all clauses contained in the specified throwable's stack trace to the standard error stream.voidprintProjogStackTrace(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.voidsetUserInput(java.io.InputStream is)Reassigns the "standard" input stream.voidsetUserOutput(java.io.PrintStream ps)Reassigns the "standard" output stream.
-
-
-
Field Detail
-
kb
private final KnowledgeBase kb
-
-
Constructor Detail
-
Projog
public Projog(ProjogListener... listeners)
-
Projog
public Projog(ProjogProperties projogProperties, ProjogListener... listeners)
Constructs a newProjogobject with the specifiedProjogPropertiesandProjogListeners.
-
-
Method Detail
-
consultFile
public void consultFile(java.io.File prologScript)
Populates this objectsKnowledgeBasewith 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 objectsKnowledgeBasewith clauses read from the specifiedReader.- 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 objectsKnowledgeBasewith clauses read from the specified resource.If
prologSourceResourceNamerefers to an existing file on the file system then that file is used as the source of the prolog syntax elseprologSourceResourceNameis 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 aPredicateFactorywith theKnowledgeBaseof thisProjog.This method provides a mechanism for "plugging in" or "injecting" implementations of
PredicateFactoryat 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 thePredicateFactorywith.predicateFactory- ThePredicateFactoryto be added.- Throws:
ProjogException- if there is already aPredicateFactoryassociated with thePredicateKey
-
addArithmeticOperator
public void addArithmeticOperator(PredicateKey key, ArithmeticOperator operator)
Associates aArithmeticOperatorwith thisKnowledgeBaseof thisProjog.This method provides a mechanism for "plugging in" or "injecting" implementations of
ArithmeticOperatorat 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 theArithmeticOperatorwith.operator- The instance ofArithmeticOperatorto be associated withkey.- Throws:
ProjogException- if there is already aArithmeticOperatorassociated with thePredicateKey
-
createPlan
public QueryPlan createPlan(java.lang.String prologQuery)
Creates aQueryPlanfor querying the Projog environment.The newly created object represents the query parsed from the specified syntax. A single
QueryPlancan be used to create multipleQueryStatementobjects.- Parameters:
prologQuery- prolog syntax representing a query- Returns:
- representation of the query parsed from the specified syntax
- Throws:
ProjogException- if an error occurs parsingprologQuery- See Also:
createStatement(String),executeQuery(String),executeOnce(String)
-
createStatement
public QueryStatement createStatement(java.lang.String prologQuery)
Creates aQueryStatementfor 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 callingQueryStatement.executeQuery(). The resultingQueryResultcan 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 parsingprologQuery- See Also:
createPlan(String),executeQuery(String),executeOnce(String)
-
executeQuery
public QueryResult executeQuery(java.lang.String prologQuery)
Creates aQueryResultfor querying the Projog environment.The newly created object represents the query parsed from the specified syntax. The
QueryResult.next()andQueryResult.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 parsingprologQuery- See Also:
createPlan(String),createStatement(String),executeOnce(String)
-
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 parsingprologQueryor no solution can be found for it- See Also:
createPlan(String),createStatement(String),executeQuery(String)
-
addListener
public void addListener(ProjogListener listener)
Registers anProjogListenerto 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 specifiedTerm.- Parameters:
t- theTermto represent as a string- Returns:
- a string representation of the specified
Term - See Also:
TermFormatter.formatTerm(Term)
-
getKnowledgeBase
public KnowledgeBase getKnowledgeBase()
Returns theKnowledgeBaseassociated with this object.Each
Projogobject is associated with its ownKnowledgeBase. In normal usage it should not be necessary to call this method - as the other methods ofProjogprovide a more convenient mechanism for updating and querying the "core" inference engine.- Returns:
- the
KnowledgeBaseassociated 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.
-
getStackTrace
public ProjogStackTraceElement[] getStackTrace(java.lang.Throwable exception)
Provides programmatic access to the stack trace information printed byprintProjogStackTrace(Throwable).
-
getClauses
private java.util.List<ClauseModel> getClauses(java.lang.Throwable e)
-
-