Set

public class Set extends Entity<SetInstance> implements java.lang.Iterable<Object>

Represents an AMPL set. In case of not indexed sets, this class exposes functionalities of a Java Set for accessing its elements. The members of the set can be strings, double numbers or tuples. Moreover, the inherited Set.getValues can be used to get all the members in DataFrame class.

All these methods throw an UnsupportedOperationException if called for an indexed set.

In case of indexed sets, use the method Set.get to obtain the objects of type SetInstance, from which to access all properties (see section Access to instances and values for further information on how to access set instances).

Data can be assigned to the set using the methods Set.setValues, or using AMPL.setData and an object of class DataFrame.

Fields

arity

int arity

Constructors

Set

Set()

Set

Set(AMPL ampl, Engine engine, String name)

Methods

arity

public int arity()

The dimension of s, or number of components in each member of this set

asDoubles

double[] asDoubles()

Returns all the members as doubles. It tries to convert strings to doubles and throws an exception if any of the members is not convertible.

Validity of conversions from member type and runtime content:

  • (double) 1.1 -> YES
  • (double) 1 -> YES
  • (string) “1.1” -> YES
  • (string) “1” -> YES
  • (string) “a” -> NO
Throws:

asIntegers

int[] asIntegers()

Returns all the members as integers. It tries to convert strings and double to integers and throws an exception if any of the members is not convertible.

Validity of conversions from member type and runtime content:

  • (double) 1.1 -> NO
  • (double) 1 -> YES
  • (string) “1.1” -> NO
  • (string) “1” -> YES
  • (string) “a” -> NO
Throws:

asSet

java.util.Set<Object> asSet()

Get a copy of the elements in this set as a java Set of Objects (Strings, double numbers or Tuple in case of multidimensional sets)

See java:ref:SetInstance.containsAll.

Valid only for not indexed sets.

Returns:The copy of the set

asStrings

String[] asStrings()

Returns all the members as strings

contains

public boolean contains(Object o)

Check contents.

See SetInstance.contains.

Valid only for not indexed sets.

Parameters:
  • o – Object to be searched
Returns:

True if the object is present

containsAll

public boolean containsAll(Collection<? extends Object> c)

Check if the sets contains all the elements in the collection c. Each element in the collection can be a double, a String, a Tuple or an array. In case of arrays, each array is considered as a Tuple.

See SetInstance.containsAll.

Valid only for not indexed sets.

Parameters:
  • c – The collection containing the elements to find.

get

public SetInstance get()

Get the set instance (for not indexed sets)

Throws:

get

public final SetInstance get(Object... key)

Get the set instance corresponding to the specified key. See Access to instances and values for further information on how the key can be specified.

Throws:
Returns:

The instance corresponding to the specified key. Null if the specified key is not present.

get

public final SetInstance get(Tuple key)

Get the set instance corresponding to the specified key (passed as a Tuple). See Access to instances and values for further information on how the key can be specified.

Parameters:
  • key – The indexing tuple of the instance to be found
Throws:
Returns:

The instance corresponding to the specified key. Null if the specified key is not present.

getValues

public DataFrame getValues()

Returns a DataFrame containing all the members of this set. The DataFrame will have a number of index columns equal to the arity of this set and a number of rows equal to the number of members.

isEmpty

public boolean isEmpty()

Return true if the set is empty.

Valid only for not indexed sets.

iterator

public Iterator<Object> iterator()

If the set is not indexed, get the iterator for this set, allowing easy access to its elements, as in:

ampl.eval("set A := {'a', 'b', 3};"); for (Object o :
ampl.getSet("A")) { // ... }
Throws:

refreshInstances

void refreshInstances()

setDeclaration

void setDeclaration(String declaration)

setValues

public void setValues(DataFrame data)

setValues

public void setValues(Object... objects)

Assign values to the this set.

See SetInstance.setValues for the usage instructions.

size

public int size()

Get the size of the set (number of elements) (valid only for not indexed sets)

Throws:

toArray

public Object[] toArray()

Convert this set to an array.

Valid only for not indexed sets.

toArray

public <T> T[] toArray(T[] a)

Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array. If the set fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this set.

If this set fits in the specified array with room to spare (i.e., the array has more elements than this set), the element in the array immediately following the end of the set is set to null. This is useful in determining the length of this set only if the caller knows that this set does not contain any null elements.

Valid only for not indexed sets.

Parameters:
  • a – An array of the desired type

toString

public String toString()

String representation of the set. Equivalent to the AMPL statement

show setName;