DLV
Class Model

java.lang.Object
  extended byDLV.Model

public class Model
extends java.lang.Object

This class represents a Model of a logic program (output of DLV).
The class Model allows to handle a collection of Predicate objects.
Two interfaces are provided. The first is java.util.Enumeration like the latter is java.sql.ResultSet like.

            Ex. 1 - Enumeration like methods
    
               ...                                   // dlv call
    
               Model m=dlv.getModel();               // gets a Model object
    
               ...
    
               while(m.hasMoreModels())              //tests if m has Predicates
               {
                 Predicate p=m.nextPredicate();   //gets next Predicate
                 ...
               }
    
               ...
    
            Ex. 2 - ResultSet like methods
    
               ...                                   // dlv call
    
               Model m=dlv.getModel();               // gets a Model object
    
               ...
    
               while(m.next())                       //tests if m has Predicates
               {
                 Predicate p=m.getPredicate();   //gets next Predicate
                 ...
               }
    
               ...
            

The static constant No_Model allows to represent a program which hasn't models.
If you want to test if a Model instance m is "no nodel" you can call m.isNoModel() or test m==Model.NO_Model.

Version:
3.0
Author:
Francesco Ricca
See Also:
DlvHandler, Predicate

Field Summary
static Model NO_MODEL
          DLV programs may have many models or no model at all.
 
Method Summary
 boolean absolute(int position)
          Moves the cursor to the given Predicate number in this Model object.
 void afterLast()
          Moves the cursor to the end of this Model object, just after the last Predicate.
 void appendTo(java.io.PrintWriter out)
          This method allows to save this Model in the file handled by "out".
 void beforeFirst()
          Moves the cursor to the front of this Model object, just before the first Predicate.
 boolean first()
          This method moves the cursor to the first Predicate in this Model object.
 Predicate firstPredicate()
          This method returns the first predicate and set the cursor at the beginning of this Model.
 int getCost()
          Returns the cost of this model if the program hasn't weak constraints.
 int[][] getCostWithPriority()
          Returns the cost of this model if the program has some weak constraints, null otherwise.
 int getCursor()
          This method returns the position of current predicate.
 Predicate getPredicate()
          Returns the Predicate currently pointed by the cursor.
 Predicate getPredicate(int i)
          Returns the i-th predicate.
 Predicate getPredicate(java.lang.String name)
          Returns, if exists, a Predicate object of name "name", null otherwise.
 java.lang.String[] getPredicateNames()
          Returns an array which contains the predicate names contained in this model.
 java.util.Enumeration getPredicates()
          Returns an Enumeration of predicates contained in this model.
 boolean hasMorePredicates()
          This method returns true if there are more predicates.
 boolean isBest()
          Returns true if and only if this is a best model.
 boolean isEmpty()
          Returns true if and only if this is an empty model.
 boolean isFirst()
          Indicates whether the cursor is on the first Predicate of this Model object.
 boolean isLast()
          Indicates whether the cursor is on the last Predicate of this Model object.
 boolean isNoModel()
          Returns true if and only if this object represents the lack of models (no model were computed).
 boolean last()
          This method moves the cursor to the last Predicate in this Model object.
 Predicate lastPredicate()
          This method returns the last predicate and set the cursor at the end of this Model.
 boolean next()
          Moves the cursor down one position from its current.
 Predicate nextPredicate()
          This method returns the next predicate.
 boolean previous()
          Moves the cursor to the previous Predicate in this Model object.
 Predicate previousPredicate()
          This method returns the previous Predicate.
 boolean relative(int i)
          Moves the cursor a relative number of positions, either positive or negative.
 void saveTo(java.lang.String file)
          This method allows to save this Model in the file "file".
 int size()
          Returns the size of this model (The number of predicates it contains).
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NO_MODEL

public static final Model NO_MODEL
DLV programs may have many models or no model at all. This static variable is useful to represent the second case.

Method Detail

isEmpty

public boolean isEmpty()
Returns true if and only if this is an empty model.

Returns:
true if and only if this is an empty model.
Throws:
IsNoModelException - if this object represents no model.

isBest

public boolean isBest()
Returns true if and only if this is a best model.

Returns:
true if and only if this is the best model.
Throws:
IsNoModelException - if this object represents no model.

isNoModel

public boolean isNoModel()
Returns true if and only if this object represents the lack of models (no model were computed). You can also test if "this" is no model comparing with Model.NO_Model.

Returns:
true if and only if this object represents the lack of model (no model).

getCost

public int getCost()
Returns the cost of this model if the program hasn't weak constraints.

Returns:
The cost of this model if the program hasn't weak constraints.
Throws:
IsNoModelException - if this object represents no model.

getCostWithPriority

public int[][] getCostWithPriority()
Returns the cost of this model if the program has some weak constraints, null otherwise.

Returns:
The cost of this model if the program has some weak constraints, null otherwise.
Throws:
IsNoModelException - if this object represents no model.

getPredicates

public java.util.Enumeration getPredicates()
Returns an Enumeration of predicates contained in this model.
Enumeration implementation is not synchronized, if you use threads take care of this fact

Returns:
An Enumeration of predicates contained in this model.
Throws:
IsNoModelException - if this object represents no model.

getPredicateNames

public java.lang.String[] getPredicateNames()
Returns an array which contains the predicate names contained in this model.

Returns:
An array which contains the predicate names contained in this model.
Throws:
IsNoModelException - if this object represents no model.

getPredicate

public Predicate getPredicate(java.lang.String name)
Returns, if exists, a Predicate object of name "name", null otherwise.

Parameters:
name - A String containing a predicate name.
Returns:
If exists, the selected predicate.
Throws:
IsNoModelException - if this object represents no model.

size

public int size()
Returns the size of this model (The number of predicates it contains).

Returns:
The size of this predicate.
Throws:
IsNoModelException - if this object represents no model.

getPredicate

public Predicate getPredicate()
Returns the Predicate currently pointed by the cursor.

Returns:
The Predicate currently pointed by the cursor.
Throws:
NoSuchPredicateException - if you try to get a predicate which doesn't exist.

getPredicate

public Predicate getPredicate(int i)
Returns the i-th predicate.

Returns:
The i-th predicate.
Throws:
NoSuchPredicateException - if you try to get a predicate which doesn't exist.

hasMorePredicates

public boolean hasMorePredicates()
This method returns true if there are more predicates. This method is useful to iterate inside a Model object.

Returns:
true if there are more predicates false otherwise.

nextPredicate

public Predicate nextPredicate()
This method returns the next predicate. It moves the cursor a position down and then returns the predicate pointed by it. This method is useful to iterate inside a Model object.

Returns:
The next predicate.
Throws:
NoSuchPredicateException - if you try to get a predicate which doesn't exist.

previousPredicate

public Predicate previousPredicate()
This method returns the previous Predicate. It moves the cursor a position up and then returns the predicate pointed by it. This method is useful to iterate inside a Model object.

Returns:
The previous predicate.
Throws:
NoSuchFactException - if you try to get a fact which doesn't exist.

firstPredicate

public Predicate firstPredicate()
This method returns the first predicate and set the cursor at the beginning of this Model. This method is useful to iterate inside a Model object.

Returns:
The first Predicate of this Model.
Throws:
NoSuchPredicateException - if you try to get a predicate which doesn't exist.

lastPredicate

public Predicate lastPredicate()
This method returns the last predicate and set the cursor at the end of this Model. (After calling this method you can call nextPredicate() an other time without throwing a NoSuchPredicateException) This method is useful to iterate inside a Model object.

Returns:
The last Predicate of this Model.
Throws:
NoSuchPredicateException - if you try to get a predicate which doesn't exist.

getCursor

public int getCursor()
This method returns the position of current predicate. If it returns -1 the cursor is before the first predicate.

Returns:
The position of current predicate.

first

public boolean first()
This method moves the cursor to the first Predicate in this Model object.

Returns:
true if the cursor is on a valid Predicate; false if there are no Predicates in the Model

last

public boolean last()
This method moves the cursor to the last Predicate in this Model object.

Returns:
true if the cursor is on a valid Predicate; false if there are no Predicates in the Model

beforeFirst

public void beforeFirst()
Moves the cursor to the front of this Model object, just before the first Predicate. This method has no effect if the Model contains no Predicates.


afterLast

public void afterLast()
Moves the cursor to the end of this Model object, just after the last Predicate. This method has no effect if the Model contains no Predicates.


isFirst

public boolean isFirst()
Indicates whether the cursor is on the first Predicate of this Model object.

Returns:
true if the cursor is on the first Predicate; false otherwise

isLast

public boolean isLast()
Indicates whether the cursor is on the last Predicate of this Model object.

Returns:
true if the cursor is on the last Predicate; false otherwise

relative

public boolean relative(int i)
Moves the cursor a relative number of positions, either positive or negative. Attempting to move beyond the first/last Predicate in the Model positions the cursor before/after the the first/last Predicate. Calling relative(0) is valid, but does not change the cursor position.
Note: Calling the method relative(1) is different from calling the method next() because it makes sense to call next() when there is no current Predicate, for example, when the cursor is before the first Predicate or after the last Predicate of the Model.

Returns:
true if the cursor is on a Predicate; false otherwise

previous

public boolean previous()
Moves the cursor to the previous Predicate in this Model object.

Returns:
true if the cursor is on a valid position; false if it is off the Model

next

public boolean next()
Moves the cursor down one position from its current. A Model cursor is initially positioned before the first Predicate; the first call to the method next makes the first Predicate the current Predicate; the second call makes the second Predicate the current Predicate, and so on.

Returns:
true if the new current position is valid; false if there are no more Predicates

absolute

public boolean absolute(int position)
Moves the cursor to the given Predicate number in this Model object.

WARNING! If the position number is positive, the cursor moves to the given position with respect to the beginning of the Model. The first position is 1, the second is 2, and so on.

If the given position number is negative, the cursor moves to an absolute position with respect to the end of the Model. For example, calling the method absolute(-1) positions the cursor on the last position; calling the method absolute(-2) moves the cursor to the next-to-last position, and so on.

An attempt to position the cursor beyond the first/last in the Model leaves the cursor before the first or after the last position.

Note: Calling absolute(1) is the same as calling first(). Calling absolute(-1) is the same as calling last().

Parameters:
position - the position which the cursor should move to. A positive number indicates the position counting from the beginning of the Model; a negative number indicates the position counting from the end of the Model
Returns:
true if the cursor is on the Model; false otherwise

saveTo

public void saveTo(java.lang.String file)
            throws java.io.IOException
This method allows to save this Model in the file "file". The file "file" is overwritten by this method if it already exsists, otherwise a new file is created. The file created by this method can be reused as input of DLV.

Parameters:
file - a String which contains a full file name.
Throws:
java.io.IOException - if an error occurs during file creation.

appendTo

public void appendTo(java.io.PrintWriter out)
              throws java.io.IOException
This method allows to save this Model in the file handled by "out". This Model is appended if "out" already exsists. Be sure that "out" is open before calling this method. This method doesn't close "out". The file modified by this method could be reused as input of DLV.

Throws:
java.io.IOException - if an error occurs processing "out".

toString

public java.lang.String toString()