Package org.prolog4j
Class Solution<S>
- java.lang.Object
-
- org.prolog4j.Solution<S>
-
- Type Parameters:
S
- The type of the values of the variable that is of special interest
- All Implemented Interfaces:
Iterable<S>
- Direct Known Subclasses:
SWIPrologSolution
,TuPrologSolution
public abstract class Solution<S> extends Object implements Iterable<S>
Represents the solutions of a query.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description <C extends Collection<? super S>>
Ccollect(C collection)
Collects the values of the primary variable into the given collection.void
collect(String[] variables, Collection<?>[] collections)
Collects the values of the specified variables into the given collections.abstract void
collect(Collection<?>... collections)
Collects the values of the variables into the given collections.protected abstract boolean
fetch()
Fetches the next solution if there is one.S
get()
Returns the value of the variable last occurring in the goal bound to by the first solution of the goal.<A> A
get(Class<A> type)
Returns the value of the variable last occurring in the goal bound to by the first solution of the goal.abstract <A> A
get(String variable)
Returns the value of the given variable bound to by the first solution of the goal.abstract <A> A
get(String variable, Class<A> type)
Returns the value of the given variable bound by the first solution.abstract boolean
isSuccess()
Returns whether there exists a solution or not.SolutionIterator<S>
iterator()
<A> Solution<A>
on(String variable)
Changes the state of this solution object so that it supports traversing the solutions according to another variable.<A> Solution<A>
on(String variable, Class<A> clazz)
Changes the state of this solution object so that it supports traversing the solutions according to another variable.List<S>
toList()
Collects the values of the primary variable into aList
.abstract List<?>[]
toLists()
Collects the values of the variables into aList
array.Set<S>
toSet()
Collects the values of the primary variable into aSet
.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Method Detail
-
isSuccess
public abstract boolean isSuccess()
Returns whether there exists a solution or not. Does not depend on the state of the traversal, only one solution should exist.- Returns:
- true if the goal can be satisfied, otherwise false
-
on
public <A> Solution<A> on(String variable)
Changes the state of this solution object so that it supports traversing the solutions according to another variable.- Type Parameters:
A
- the type of the values of the variable that is of special interest- Parameters:
variable
- the name of the variable- Returns:
- the same object
-
on
public <A> Solution<A> on(String variable, Class<A> clazz)
Changes the state of this solution object so that it supports traversing the solutions according to another variable. The returned object will convert the traversed elements to the specified type.- Type Parameters:
A
- the type of the values of the variable that is of special interest- Parameters:
variable
- the name of the variableclazz
- the type to which the elements have to be converted- Returns:
- the same object
-
get
public S get()
Returns the value of the variable last occurring in the goal bound to by the first solution of the goal.- Returns:
- the value of the last variable occurring in the goal
-
get
public <A> A get(Class<A> type)
Returns the value of the variable last occurring in the goal bound to by the first solution of the goal. The required type of the value can be specified explicitly. This is useful when not the default type is desired.- Type Parameters:
A
- the type of the value- Parameters:
type
- the type which the value should be converted to- Returns:
- the value bound to the variable
-
get
public abstract <A> A get(String variable)
Returns the value of the given variable bound to by the first solution of the goal.- Type Parameters:
A
- the type of the value- Parameters:
variable
- the name of the variable- Returns:
- the value bound to the variable
-
get
public abstract <A> A get(String variable, Class<A> type)
Returns the value of the given variable bound by the first solution. The required type of the value can be specified explicitly. This is useful when not the default type is desired.- Type Parameters:
A
- the type of the value- Parameters:
variable
- the name of the variabletype
- the type which the value should be converted to- Returns:
- the value bound to the variable
-
fetch
protected abstract boolean fetch()
Fetches the next solution if there is one.- Returns:
true
if there was another solution, otherwisefalse
-
iterator
public SolutionIterator<S> iterator()
-
collect
public final <C extends Collection<? super S>> C collect(C collection)
Collects the values of the primary variable into the given collection. Returns its parameter.- Type Parameters:
C
- the type of the collection- Parameters:
collection
- the collection which will store the solutions- Returns:
- collection
-
collect
public final void collect(String[] variables, Collection<?>[] collections)
Collects the values of the specified variables into the given collections. The size of the arrays is expected to be the same.- Parameters:
variables
- the name of the output variablescollections
- the collections where to store the solutions
-
collect
public abstract void collect(Collection<?>... collections)
Collects the values of the variables into the given collections.- Parameters:
collections
- the collections which will store the solutions
-
toSet
public Set<S> toSet()
Collects the values of the primary variable into aSet
.- Returns:
- a set containing the values of the variable of interest
-
toList
public List<S> toList()
Collects the values of the primary variable into aList
.- Returns:
- a list containing the values of the variable of interest
-
-