« Back to University of Duisburg-Essen

Handling arrays in AdoScript

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Handling arrays in AdoScript
Answer
6/20/14 2:08 PM
Hello!
I have some questions regarding the array data structure in AdoScript. The Administration Manual doesn't give that much information about creating and working with arrays. Somehow I got to see some AdoScript someone uploaded in the forum so I got to know how to create them:
1SETL aArray: (set(aGenericArray, array(0)), aGenericArray)
The ADOxx Administration doesn't even mention this possibilty. Is there another PDF-File I missed which contains more information about coding with AdoScript/Leo/AQL/etc. so that I get a basic understanding and how to work e. g. with arrays?

Further questions would be how to work with arrays, especially with multidimensional arrays, which I need. I tried something like this:

1SETL aArray: (set(aGenericArray, array(2,2)), aGenericArray)

And with

1CC "AdoScript" INFOBOX(aArray)
I get following output: {{(undefined(, (undefined)}, {(undefined(, (undefined)}}. But how to access the array now and fill it with data? I know how
1SET dummy: aappend(aArray, "Some Text")
works, which basically just adds a new one dimensional element at the end (BTW: do I always have to create a dummyVar to use this command and why?) and the outcome would be like: {{(undefined(, (undefined)}, {(undefined(, (undefined)}, "Some Text"}.
So, to make it more clear what I am struggling with:
  1. How can I properly initialize arrays?
  2. What's the best way to work on a multi-dimensional array structure (how to fill the multimensional fields with some value, etc...)
  3. Regarding to question 2: Given a 2x2 array already filled with data and I intend to fill it with a new entry with expected outcome 3x2. How would I do that?
    1arrayBefore: {{"1", "1.1"},{"2", "2.2"}}
    is supposed to look like:
    1arrayAfter: {{"1", "1.1"},{"2", "2.1"},{"3", "3.1"}}
  4. Is there a way to remove an array field, so that "arrayAfter" would have "arrayBefore"'s structure again (I mean really removed, not just set back to "" or 0 if we are speaking about integer)
Maybe you can think of other possible difficulties I could get in contact with. I have a PHP and Java background and that didn't help me here when working with AdoScript arrays.

I hope I was able to explain in a proper way what I am struggeling with and I hope anyone can give me some advice here.

Best wishes,
David

RE: Handling arrays in AdoScript
Answer
6/20/14 3:22 PM as a reply to David Becher.
Just a short update: I figured out by now how to get access to the array. My mistake was that I kept trying to use a notation like
1myArray[n][m]: "some value"
However,
1myArray[n,m]: "some value"
worked like a charm!

I found out how to add a new multi-dimensional element as well:
1SET dummyVar: (aappend(myArray, array(1,2)))

Remains the question how to properly delete an item, when even possible in AdoScript. I merely managed to do it that way:
1SET myArray[3]: ""
or
1SET myArray[3]: (array(0))

Though, both variants are not desireable, I am trying to find a way to completely remove an element.

Best regards,
David

PS: Still, are there some documentations for coding with AdoScript/Leo apart from the Administration Manual? I'd be glad to read them! :-)

RE: Handling arrays in AdoScript
Answer
6/26/14 11:17 AM as a reply to David Becher.
Dear David,

thank you for your message and apologies for not coming back to you earlier. We are currently working on an update of the documentation pages to also included language construct definitions. In the meantime, an in relation to your post, please find below a detailed description of all array functionality in ADOxx. I will keep you posted as soon as the update is released.
The documentation below provides also generic examples in AdoScript to support you in applying the concepts.

Overview Arrays
Arrays are part of the LEO language. So they actually may not just be used in AdoScript, but also in any other LEO based language. However, there are also some AdoScript specific elements concerning the handling of arrays, e.g. the SET command for arrays.
LEO arrays are implemented as array lists, i.e. the size of an array can be changed dynamically, elements can be appended.
Each element of an array may be any LEO value, i.e. the elements of one array need not all be of one same type.

Defining array variables
New arrays variables are defined like other variables using the SET/SETG/SETL commands. Within expressions, array values are written with curly braces {}, like in the followiing:

 1SET a: ({0, 2, 3, 6}) # array of four integer values
 2CC "AdoScript" INFOBOX (STR a)
 3
 4SET ei: ("nen")
 5SET b: {"ei", ei}) # string constant "ei" and value of variable ei
 6CC "AdoScript" INFOBOX (STR b)
 7
 8# different data types in array
 9SET ei: (1)
10SET b: ({"ei", ei}) # string constant "ei" and value of variable ei
11CC "AdoScript" INFOBOX (STR b)

Note that it is not correct to ommit the parenthesises (round braces) here. Without them, the content inside the curly braces would be interpreted as a new LEO program (block), known for instance from the notation of IF branches.
Another way to create a new array is using the array() function. It is used when the values of the array elements shall be assigned later. The function creates an array of a given size (number of elements) and with undefined element values. 
1SET c: (array(10)) # new array with 10 elements
2CC "AdoScript" INFOBOX (STR c) # all elements are undefined

Getting the array size
The size, or length, of an array is the array's number of elements. For a given array, its size can be evaluated with the LEN operator or the array function .length:
1SET c: (array(10)) # new array with 10 elements
2SET n: (LEN c) # the value of n becomes 10
3CC "AdoScript" INFOBOX (STR n)
4
5SET c: (array(10)) # new array with 10 elements
6SET m: (c.length) # the value of n becomes 10
7CC "AdoScript" INFOBOX (STR m)
c has to be of type array. The result is number of elements of a.

Accessing arrays/Subscription to an array
Array access is written with square brackets []. Inside the brackets the index of the accessed array element is specified. The index of the first element of any array is 0. The index of the last element of an array with n elements is n-1. It is always an error to try to access an element with an index less than 0 or greater or equal than the number of elements:

1SET a: ({0, 2, 3, 6}) # array of four integer values
2SET n1: (a[1]) # the value of n1 becomes 2
3SET n2: (a[1] + a[2]) # the value of n2 becomes 5
4SET n3: (a[4]) # error since a.length is 4
5CC "AdoScript" INFOBOX (STR n1 + " " + STR n2) # before the message, the error message of n3 is shown

Subscription to an array using SUB
a SUB i #same as a
a has to be of type array, i of type integer. The result is the i-th element of a. The indexing of array elements begins with 0, as stated above.

HINT: Array elements, and so the result, may be of any type. The type of two different elements of one array needs not to be the same. Use type() if you are not sure about the element type.


For multi-dimensional arrays indices of different dimensions can be written comma-separated inside one bracket pair. a [i, j] means the same as a .

Assigning values to array elements
Single elements of array variables can be modified using the AdoScript SET command. The index is specified in enclosing brackets:
 1# set all elements of array c to 0:
 2SET c: (array(10)) # new array with 10 elements
 3FOR i from:0 to:9 {
 4SET c:0
 5}
 6CC "AdoScript" INFOBOX (STR c)
 7
 8# increase value of a[1]
 9SET a: ({0, 2, 3, 6}) # array of four integer values
10CC "AdoScript" INFOBOX ("Before: " + STR a)
11SET a[1]: (a[1] + 1) # increase a[1] by 1, new value is 3
12CC "AdoScript" INFOBOX ("After: " + STR a)

Replacing values in an array
The areplace() function (areplace ( a, i1 , ..., in , val ) n >= 1) can also be used to change the value of an array element. As this is a LEO function, it can be used in any expression.
1SET a: ({0, 2, 3, 6}) # array of four integer values
2CC "AdoScript" INFOBOX ("Before: " + STR a)
3SET dummy: (areplace(a, 1, 263)) # same effect as SET a[1]:263, or as an Exporession set( a [ i1 , ..., in ], val ).
4CC "AdoScript" INFOBOX ("After: " + STR a)
The return value of areplace() is the new value of the replaced element.

Appending values to an array
The aappend() function (aappend ( a, i1 , ..., in , val ) n >= 0) also belongs to the LEO syntax and may not just be used in AdoScript. aappend() adds a new value at the end of an array:
1SET a:{7, 14, 21}
2CC "AdoScript" INFOBOX ("Before: " + STR a)
3SET dummy: (aappend(a, 28)) # a becomes {7, 14, 21, 28}
4CC "AdoScript" INFOBOX ("After: " + STR a)
The return value of aappend() is the appended value.

Deleting values in an array
aerase ( a, i1 , ..., in ) n >= 1
Erases a single array element. a must be the name of an array variable. The array must have at least n dimensions. i1 to in must be of type integer.
Return value is the value of element which has been erased.
1SET a:{7, 14, 21}
2CC "AdoScript" INFOBOX ("Before: " + STR a)
3SET dummy: (aerase(a, 2)) # a becomes {7, 21, 28}
4CC "AdoScript" INFOBOX ("After: " + STR a)

Sorting arrays
a.sort ( [ lambdaExpr ] )
Sorts the elements of an array. With the optional lambda expression a comparison operator can be specified. The labmda expression must have two parameters and returns true if the first value shall be inserted before the second.
The default lamda expression is
1lambda(x, y, x < y)
which leads to an ascending sorting of the array elements.
1SET a:{7, 14, 21}
2CC "AdoScript" INFOBOX ("Before: " + STR a)
3SET dummy: (a.sort(lambda(x, y, x > y)))
4CC "AdoScript" INFOBOX ("After: " + STR a)

Check if an array is empty
a.empty
Returns 1 (true) if the array a is empty, and 0 (false) otherwise.
1SET a:{7, 14, 21}
2SET b: (array(0)) # initializes an empty array
3CC "AdoScript" INFOBOX ("A empty?: " + STR a.empty)
4CC "AdoScript" INFOBOX ("B empty?: " + STR b.empty)

Check if value in an array
val IN a
Returns 1 (true) if val (value of any type) is contained in array a, and 0 (false) otherwise.
1SET a:{7, 14, 21}
2CC "AdoScript" INFOBOX ("7 included?: " + STR (7 IN a))
3CC "AdoScript" INFOBOX ("18 included?: " + STR (18 IN a))





RE: Handling arrays in AdoScript
Answer
6/26/14 11:20 AM as a reply to David Becher.
Multi-dimensional arrays
Multi-dimensional arrays can be constructed by putting arrays into arrays. The following is an array with three elements, where each element is again an array, consisting of string values:
1SET ducks: ({{"Donald", "Dagobert"},
2{"Tick", "Trick", "Track"},
3{"Daisy"}})
4CC "AdoScript" INFOBOX (STR ducks)

Note that, as each array element may be anything, different subarrays of one array may be of different size.
The array() function can be called with multiple parameters, each for the size of one dimension:
1SET a43: (array(4, 3)) # defining a new 4x3 array
2CC "AdoScript" INFOBOX (STR a43)

Within expressions you may write h[i, 1] or h[1]. However, an array element to be modified using SET may not be written using multiple bracket pairs:
 1
 2SET a43: (array(4, 3)) # defining a new 4x3 array
 3FOR i from:0 to:3 {
 4SET a43[i,0]: (i) # ok
 5# SET a43[1]: (i) # error!
 6SET a43[i,1]: (i + 8) # ok
 7SET a43[i,2]: (i + 8) # ok
 8}
 9CC "AdoScript" INFOBOX (STR a43)

# The access on an element in a nested array is also written with brackets, where indices for dimensions are comma-separated:
1FOR i from:0 to:4 {
2SET h[i, 0]:0
3SET h[i, 1]: (i)
4SET h[i, 2]: (STR h[i, 1])
5}
6[i][/i]
areplace() and ohter functions can also be used for modifying multi-dimensional arrays:
1SET dummy: (areplace(a43, i, 1, -i)) # same effect as SET a43[i, 1]: (-i)

RE: Handling arrays in AdoScript
Answer
7/2/14 7:07 AM as a reply to Wilfrid Utz.
Hello,

thank you for your detailed reply, I really appreciate it.
In the meantime I was able to write a AdoScript which is - at its desirable end state - able to parse the graph of a model and checks whether its syntiactical correct or not. Therefore a given set of constraints should be implemented.

For the current implementation I used and rewrote a script of yours parsing the graph and saving it in an array. The first constraint I implemented: "Every path in the graph has to start with a concept 'StartEvent' and has to end in a concept 'StopEvent'."

For this purpose the array-using version of the script does his job quite well. However, I think it will be getting more complex depending on the model. For example: according to whether specific concepts/classes are used it is possible to build a cyclic graph. In its current version the script would be stuck in an endless loop.

I think a more desireable structure would be to parse the paths actually as a real graph.

I am thinking of like an object oriented structure I learned to know in one of my programming lectures (though we were programming in Java). Each node should have an attribute which contains references to its succeeding nodes and so on. On this data structure I could process all kinds of known graph algorithms. And to solve the endless loop problem maybe I could add an attribute of type boolean 'boolVisited' which skips the current node when it has already been visited.

So my question is as follows: are there any data stuctures or language concepts I could use for my purposes described above? My problem is that I cannot think of a way to use arrays for this because it would be difficult to reference to a certain child node within the array.

Attached you can see my current script which should not be really interesing for you because it just adapted your script. Though still just using arrays, not a real graph structure.

Best regards,
David
Attachments: evaluateMEMOOrgMLModel.asc (6.0k)

RE: Handling arrays in AdoScript
Answer
7/2/14 11:20 AM as a reply to David Becher.
Hello David,

that really looks very interesting and promising. I will have a look at the script and update you shortly on my thoughts - I had in mind that the actual graph algorithm does not need to be in the model structure but could be part of an AdoScript variable.

With respect to model checks, you could consider platform functionality on class cardinalities (which is a different approach), and/or the way the simulation algorithm/path analysis validates the abstract metamodel. 


Best regards,
Wilfrid

RE: Handling arrays in AdoScript
Answer
7/2/14 12:46 PM as a reply to Wilfrid Utz.
Hello Wilfrid,

thank you for your effort!

I hope you can give me some advise for processing a thorough syntactical evaluation of constraints. Like already mentioned, building a graph representation known like in informatics would allow for applying all kinds of graph algorithms. And I don't know whether arrays would meet those requierements. Unfortunately from the Administration Manual I can't say whether LEO allows for creating own 'object oriented'-like data structures to store the graph's information.

Anyway, I am eager to hear what do you think or what could be the best way to check whether constraints are being kept. We already implemented extensive cardinalities for each concept. However, we have a couple of pages full of constraints that have not been implemented yet.

Best regards,
David