SetInstance

public class SetInstance extends Instance<Set> implements java.util.Set<Object>

A SetInstance object stores the information regarding a specific instance of a set. The instances can be accessed through the function Set.get of the parent entity.

The members of the set can be strings, double numbers or tuples.

Data can be assigned using Set.setValues or using AMPL.setData and a DataFrame object.

All the accessors in this class throw a IllegalStateException if the instance has been deleted in the underlying AMPL interpreter.

Fields

value

LinkedHashSet<Object> value

Constructors

SetInstance

SetInstance(Set map, Object key, int initialCapacity)

Construct a new set

Parameters:
  • entity – Map
  • key – Key

Methods

add

public boolean add(Object e)

Not supported.

Throws:

addAll

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

Not supported.

Throws:

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)

Returns:A copy of the elements in this set

asStrings

String[] asStrings()

Returns all the members as strings

clear

public void clear()

Not supported.

Throws:

contains

public boolean contains(Object o)

Check contents of the set for members. The object to be found can be a String, a double, a Tuple or an array. In this last case, each element in the array is considered as elements of a Tuple to be found.

i.e. the following are equivalent:

contains(new Tuple(1, "abc"));

and

contains(new Object[]{1, "abc});
Parameters:
  • o – Object to be found (double, String, Tuple or Object[] with the elements of a tuple)
Returns:

True if the object is present.

containsAll

public boolean containsAll(Collection<?> 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.

Example: considering the AMPL set declared as

set s := {(1, 'abc'), ('def', 2)};

the following is true:

List<Object> l = new LinkedList<Object>();
list.add(new Tuple("def", 2));
list.add(new Object[] { 1, "abc" });
ampl.getSet("s").get().containsAll(list);
Parameters:
  • c – A Collection of objects to be found. See above for the semantics of the objects.
Returns:

True if all the specified objects are present

copyFrom

void copyFrom(Instance<?> from)

getValues

public DataFrame getValues()

Get all the members of this set in a DataFrame

isEmpty

public boolean isEmpty()

Return true if the set is empty.

iterator

public Iterator<Object> iterator()

Get the iterator for this set.

remove

public boolean remove(Object o)

Not supported.

Throws:

removeAll

public boolean removeAll(Collection<?> c)

Not supported.

Throws:

retainAll

public boolean retainAll(Collection<?> c)

Not supported.

Throws:

setValues

public void setValues(Object... objects)

Assign values to the this set. The values can be strings, doubles, tuples or arrays of objects (which will be converted into tuples).

For example, to assign values to a set of tuples:

ampl.eval("set s dimen 2;");
Set s = ampl.getSet("s");
Object[] data = new Object[] { new Tuple(1, "abc"), new Tuple(2, "def") };
s.setValues(data);

or alternatively, using arrays of objects:

ampl.eval("set s dimen 2;");
Set s = ampl.getSet("s");
Object[] data = new Object[] {new Object[]{1, "abc"}, new Object{2, "def"}};
s.setValues(data);

An alternative way to assign values to a set of tuples of size n is to use a flattened array, in which every n elements are considered a Tuple. So, an equivalent formulation of the example above is:

ampl.eval("set s dimen 2;");
Set s = ampl.getSet("s");
Object[] data = new Object[] { 1, "abc", 2, "def" };
s.setValues(data);
Parameters:
  • objects – Values to be assigned to the set

setValues

public void setValues(DataFrame data)

size

public int size()

Get the size

Returns:Cardinality of this set

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.

Parameters:
  • a – An array of the desired type

toDouble

double toDouble(Object o)

toString

public String toString()

Converts the set to string, showing its name, its index value and its members.