Messages to sets (spreading)
Access to the i element
J sets do not currently allow instructions such as "E[i]=V;" or "V=E[i];" to be executed. The specific services below must be used:
Object:getItemSet (inObject[] pSet, in int pIndex, out Object pElt)
Returns in "pElt", the "pIndex" rank element of the "pSet" set. The first element is at the 0 index. "pSet" and "pElt" types can be more precise (String[] and String, for example).
Object:setItemSet (inout Object[] pSet, in int pIndex, in Object pElt)
Replaces the "pIndex" rank element by "pElt" in "pSet". The set must have an element at the "pIndex" index. The first element is at the 0 index. "pSet" and "pElt" types can be more precise (String[] and String, for example).
Object:sort(in boolean pAscendingSort, in String pMetaAttributeName)
Sorts a set in ascending or descending order, according to the value of pAscendingSort. The sort criterion is provided by the name of a meta-attribute for metamodel objects, and by the value of the objects which make up the set for the basic classes (int, float, boolean, String, enumerate). For the latter, pMetaAttributeName is ignored and becomes optional.
Note: To directly sort modeling elements, the "sortSemanticAssociation" service is used. For more information, please see the "J Libraries User Guide".
boolean Object:contains (in Object
pElement)
Indicates whether pElement belongs to the set in question. If the set contains model elements, a reference to pElement is searched for. If a basic class is concerned (int, float, boolean, String, enumerate), an identical value is searched for.
Object:remove (in Object pElement)
Removes all references of pElement in the set for metamodel objects, and removes all basic classes (int, float, boolean, String, enumerate) of the same value for a set of basic classes.
Note: To remove a modeling element from a relationship, the "erase" service is used. For more information, please see the "J Libraries User Guide".
Object:clear
Empties the set in question.
The "Size" message
The "size" or "length" operator is used to find out the number of elements in the current set.
Example:
Here, we are
displaying a text according to the number of methods of the current class:
if (PartOperation.size() != 0)
{
StdOut.write("The current class is composed of ",
PartOperation.size(), " operations ");
else
StdOut.write("The current class has no operation ");
}
The "addElement" message
The "addElement"
message is used to insert elements into a set. When a set is being scanned (diffusion, anonymous method, …), elements are scanned in their initial order
of insertion. The example below shows another way to create the E
set of public operations of a class.
PartOperation.<select(Visibility == Public)
{
E.addElement (this);
}
The "Add" message
The "add"
operator is used to cumulate two sets by
concatenating the operating set with the receiver of the message:
E1.add(E2); // the elements of E2 are added to E1.
The "Retract" message
When the "retract" message is applied to a set, the last element of this
set is withdrawn from it.
E1.retract();//the last element is retracted from the set
The "Select" message
The "select"
operator is used to carry out a selection amongst
the occurrences of a set, according to the boolean expression supplied in the
parameter. The result is a sub-set of the initial set. On this
result, it is possible to apply either an anonymous method, or a new "select"
operator, or to carry out a message diffusion.
When the
boolean expression is evaluated, the current object is the set's current
element to which "select" is applied.
"select" filters the set before passing the resulting set to the rest of the expression; the filter boolean expression must not contain any term modified in the part of expression that follows the "select".
The "Select" message: Example
You will
find displayed here three different techniques for
displaying a class's public operations.
//1: Anonymous method
PartOperation.<select(Visibility == Public)
{
display ();
}
//2: Message diffusion (method "display")
PartOperation.<select(Visibility == Public).<display ();
//3: Passing by an intermediary variable
Operation [] E;
E = PartOperation.<select(Visibility == Public);
E.<display();
The "While" message
The "while"
operator is used to scan a set, while a certain condition is fulfilled. It scans all the occurrences until the stop scanning condition occurs.
When the
boolean expression is evaluated, the current object is the set's current
element to which "while" is applied.
The boolean expression is evaluated for each element. If it equals "true", the part of expression that follows "while" is evaluated. It is, therefore, possible to change a term of the boolean expression in the part that follows the "while".
The "While" message: Example
In the example below, the attributes of a class
are displayed until an attribute named "Size" is found or
until all the attributes have been scanned.
PartAttribute.<while(Name != "Size")
{
StdOut.write(Name);
}
Another way
of writing it shows the modification of the boolean expression term in the
anonymous method that follows.
boolean found;
PartAttribute.<while(found)
{
found = Name == "Size";
if (not(found) )
{
StdOut.write(Name);
}
}
Generalization of while and select
The "while"
and "select" messages are not only applied to sets, but may
also be used with simple objects.
So the following
example:
Operation: printIfPublic()
{
if (Visibility == Public)
{
printMethod();
}
}
can be
written with a select (or a while):
Operation: printIfPublic()
{
this.select(Visibility == Public).<printMethod();
}