Package org.prolog4j

Class 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.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected Class<S> clazz
      The values that get bound to the default output variable will be converted to this type.
    • 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>>
      C
      collect​(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 a List.
      abstract List<?>[] toLists()
      Collects the values of the variables into a List array.
      Set<S> toSet()
      Collects the values of the primary variable into a Set.
    • Field Detail

      • clazz

        protected Class<S> clazz
        The values that get bound to the default output variable will be converted to this type.
    • Constructor Detail

      • Solution

        public Solution()
    • 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 variable
        clazz - 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 variable
        type - 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, otherwise false
      • 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 variables
        collections - 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 a Set.
        Returns:
        a set containing the values of the variable of interest
      • toList

        public List<S> toList()
        Collects the values of the primary variable into a List.
        Returns:
        a list containing the values of the variable of interest
      • toLists

        public abstract List<?>[] toLists()
        Collects the values of the variables into a List array.
        Returns:
        a list array containing the values of the variables