Class StaticUserDefinedPredicateFactory
- java.lang.Object
-
- org.projog.core.predicate.udp.StaticUserDefinedPredicateFactory
-
- All Implemented Interfaces:
PredicateFactory,PreprocessablePredicateFactory,UserDefinedPredicateFactory
public class StaticUserDefinedPredicateFactory extends java.lang.Object implements UserDefinedPredicateFactory, PreprocessablePredicateFactory
Maintains a record of the clauses that represents a "static" user defined predicate.A "static" user defined predicate is one that can not have clauses added or removed after it is first defined.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classStaticUserDefinedPredicateFactory.ActionIteratorprivate static classStaticUserDefinedPredicateFactory.ImplicationsIteratorprivate classStaticUserDefinedPredicateFactory.IndexablePredicateFactoryprivate classStaticUserDefinedPredicateFactory.LinkedHashMapPredicateFactoryprivate classStaticUserDefinedPredicateFactory.NotIndexablePredicateFactoryprivate classStaticUserDefinedPredicateFactory.SingleIndexPredicateFactory
-
Field Summary
Fields Modifier and Type Field Description private PredicateFactorycompiledPredicateFactoryprivate java.util.List<ClauseModel>implicationsprivate KnowledgeBasekbprivate java.lang.Objectlockprivate PredicateKeypredicateKeyprivate intsetCompiledPredicateFactoryInvocationCtrprivate SpyPoints.SpyPointspyPoint
-
Constructor Summary
Constructors Constructor Description StaticUserDefinedPredicateFactory(KnowledgeBase kb, PredicateKey predicateKey)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddFirst(ClauseModel clauseModel)Not supported.voidaddLast(ClauseModel clauseModel)Adds new clause to list of clauses for this predicate.voidcompile()private PredicateFactorycreateInterpretedPredicateFactoryFromClauseActions(Clauses clauses, java.util.List<ClauseModel> clauseModels)private PredicateFactorycreateInterpretedPredicateFactoryFromClauses(Clauses clauses)private PredicatecreatePredicate(Term[] args, ClauseAction[] clauses)private PredicateFactorycreateSingleClausePredicateFactory(ClauseAction clause)PredicateFactorygetActualPredicateFactory()ClauseModelgetClauseModel(int index)Returns the clause at the specified position in this predicate's list of clauses.private java.util.List<ClauseModel>getCopyOfImplications()java.util.Iterator<ClauseModel>getImplications()Returns an iterator over the clauses of this user defined predicate.PredicategetPredicate(Term[] args)Returns aPredicateto be used in the evaluation of a goal.PredicateKeygetPredicateKey()Returns the key for the predicate this object representsprivate static booleanisClausesRetryable(ClauseAction[] clauses)Returns true if clauses could return more than one result.private booleanisCyclic()Returntrueif this predicate calls a predicate that in turns calls this predicate.booleanisDynamic()Returnstrueis this predicate is dynamic.booleanisRetryable()Should instances of this implementation be re-evaluated when backtracking?private static java.util.List<ClauseAction>optimisePredicateFactory(KnowledgeBase kb, ClauseAction[] data, Term arg)PredicateFactorypreprocess(Term arg)private voidsetCompiledPredicateFactory()-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.projog.core.predicate.PredicateFactory
isAlwaysCutOnBacktrack
-
-
-
-
Field Detail
-
lock
private final java.lang.Object lock
-
predicateKey
private final PredicateKey predicateKey
-
kb
private final KnowledgeBase kb
-
spyPoint
private final SpyPoints.SpyPoint spyPoint
-
implications
private final java.util.List<ClauseModel> implications
-
compiledPredicateFactory
private PredicateFactory compiledPredicateFactory
-
setCompiledPredicateFactoryInvocationCtr
private int setCompiledPredicateFactoryInvocationCtr
-
-
Constructor Detail
-
StaticUserDefinedPredicateFactory
public StaticUserDefinedPredicateFactory(KnowledgeBase kb, PredicateKey predicateKey)
-
-
Method Detail
-
addFirst
public void addFirst(ClauseModel clauseModel)
Not supported.It is not possible to add a clause to the beginning of a static user defined predicate.
- Specified by:
addFirstin interfaceUserDefinedPredicateFactory- Parameters:
clauseModel- the clause to add to the beginning of the predicate- Throws:
ProjogException
-
addLast
public void addLast(ClauseModel clauseModel)
Adds new clause to list of clauses for this predicate.Note: it is not possible to add clauses to a static user defined predicate once it has been compiled.
- Specified by:
addLastin interfaceUserDefinedPredicateFactory- Parameters:
clauseModel- the clause to add to the end of the predicate- Throws:
java.lang.IllegalStateException- if the predicate has already been compiled.
-
compile
public void compile()
-
setCompiledPredicateFactory
private void setCompiledPredicateFactory()
-
getCopyOfImplications
private java.util.List<ClauseModel> getCopyOfImplications()
-
isCyclic
private boolean isCyclic()
Returntrueif this predicate calls a predicate that in turns calls this predicate.For example, in the following script both
aandbare cyclic, butcis not.a(Z) :- b(Z). b(Z) :- a(Z). c(Z) :- b(Z).
-
createInterpretedPredicateFactoryFromClauseActions
private PredicateFactory createInterpretedPredicateFactoryFromClauseActions(Clauses clauses, java.util.List<ClauseModel> clauseModels)
-
createInterpretedPredicateFactoryFromClauses
private PredicateFactory createInterpretedPredicateFactoryFromClauses(Clauses clauses)
-
createSingleClausePredicateFactory
private PredicateFactory createSingleClausePredicateFactory(ClauseAction clause)
-
createPredicate
private Predicate createPredicate(Term[] args, ClauseAction[] clauses)
-
isClausesRetryable
private static boolean isClausesRetryable(ClauseAction[] clauses)
Returns true if clauses could return more than one result.Returns false if use of cut means once a result has been found a subsequent attempt at backtracking will immediately fail. e.g.:
p(X) :- var(X), !. p(1) :- !. p(7). % OK for last rule not to contain a cut, as long as it is not retryable.
-
getPredicate
public Predicate getPredicate(Term[] args)
Description copied from interface:PredicateFactoryReturns aPredicateto be used in the evaluation of a goal.- Specified by:
getPredicatein interfacePredicateFactory- Parameters:
args- the arguments to use in the evaluation of the goal- Returns:
- Predicate to be used in the evaluation of the goal
- See Also:
Predicate.evaluate()
-
getPredicateKey
public PredicateKey getPredicateKey()
Description copied from interface:UserDefinedPredicateFactoryReturns the key for the predicate this object represents- Specified by:
getPredicateKeyin interfaceUserDefinedPredicateFactory- Returns:
- the key for the predicate this object represents
-
getActualPredicateFactory
public PredicateFactory getActualPredicateFactory()
-
getImplications
public java.util.Iterator<ClauseModel> getImplications()
Returns an iterator over the clauses of this user defined predicate.The iterator returned will have the following characteristics which prevent the underlying structure of the user defined predicate being altered:
- Calls to
Iterator.next()return a new copy of theClauseModel. - Calls to
Iterator.remove()cause aUnsupportedOperationException
- Specified by:
getImplicationsin interfaceUserDefinedPredicateFactory- Returns:
- an iterator over the clauses in the predicate in proper sequence.
- Calls to
-
isDynamic
public boolean isDynamic()
Description copied from interface:UserDefinedPredicateFactoryReturnstrueis this predicate is dynamic.A "dynamic" predicate is a user defined predicate that can have clauses added or removed after is first defined.
- Specified by:
isDynamicin interfaceUserDefinedPredicateFactory- Returns:
trueis this predicate is dynamic
-
getClauseModel
public ClauseModel getClauseModel(int index)
Description copied from interface:UserDefinedPredicateFactoryReturns the clause at the specified position in this predicate's list of clauses.- Specified by:
getClauseModelin interfaceUserDefinedPredicateFactory- Parameters:
index- index of the clause to return- Returns:
- the clause at the specified position in this predicate's list of clauses or
nullif out of bounds
-
isRetryable
public boolean isRetryable()
Description copied from interface:PredicateFactoryShould instances of this implementation be re-evaluated when backtracking?Some goals (e.g.
X is 1) are only meant to be evaluated once (the statement is either true or false) while others (e.g.repeat(3)) are meant to be evaluated multiple times. For instances ofPredicatethat are designed to possibly havePredicate.evaluate()called on them multiple times for the same individual query this method should returntrue. For instances ofPredicatethat are designed to only be evaluated once per individual query this method should returnfalse.- Specified by:
isRetryablein interfacePredicateFactory- Returns:
trueif an attempt should be made to re-evaluate instances of implementing classes when backtracking,falseotherwise
-
preprocess
public PredicateFactory preprocess(Term arg)
- Specified by:
preprocessin interfacePreprocessablePredicateFactory
-
optimisePredicateFactory
private static java.util.List<ClauseAction> optimisePredicateFactory(KnowledgeBase kb, ClauseAction[] data, Term arg)
-
-