-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathCollections.kerml
More file actions
177 lines (153 loc) · 6.13 KB
/
Copy pathCollections.kerml
File metadata and controls
177 lines (153 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
standard library package Collections {
doc
/*
* This package defines a standard set of Collection data types. Unlike sequences of values
* defined directly using multiplicity, these data types allow for the possibility of collections
* as elements of collections.
*/
private import Base::*;
private import ScalarValues::*;
private import SequenceFunctions::size;
private import SequenceFunctions::equals;
private import IntegerFunctions::*;
private import ControlFunctions::*;
abstract datatype Collection {
doc
/*
* A Collection is a DataValue that represents a collection of elements of a given type.
*/
feature elements[0..*] nonunique {
doc
/*
* The contents of the Collection.
*/
}
}
abstract datatype OrderedCollection :> Collection {
doc
/*
* An OrderedCollection is a Collection of which the elements are ordered and not necessarily unique.
*/
feature elements[0..*] ordered nonunique :>> Collection::elements;
}
abstract datatype UniqueCollection :> Collection {
doc
/*
* A UniqueCollection is a Collection of which the elements are unique and not necessarily ordered.
*/
feature elements[0..*] :>> Collection::elements {
doc
/* Note: Redefinition of 'elements' is unique by default. */
}
}
datatype Array :> OrderedCollection {
doc
/*
* An Array is a fixed size, multi-dimensional Collection of which the elements are nonunique and ordered.
* Its dimensions specify how many dimensions the array has, and how many elements there are in each dimension.
* The rank is equal to the number of dimensions. The flattenedSize is equal to the total number of elements
* in the array.
*
* Feature elements is a flattened sequence of all elements of an Array and can be accessed by a tuple of indices.
* The number of indices is equal to rank. The elements are packed according to row-major convention.
*
* The elements of an Array can be assessed by a tuple of indices. The number of indices in such tuple is equal to rank.
* The elements are packed according to the row-major convention.
*
* Note 1. Feature dimensions may be empty, which denotes a zero dimensional array, allowing an Array to collapse to a single element.
* This is useful to allow for specialization of an Array into a type restricted to represent a scalar.
* The flattenedSize of a zero dimensional array is 1.
*
* Note 2: An Array can represent the generalized mathematical concept of an infinite matrix of any rank, i.e. not limited to rank two.
*/
feature dimensions: Positive[0..*] ordered nonunique {
doc
/*
* Defines the N-dimensional shape of the Array.
*/
}
feature rank: Natural[1] = size(dimensions) {
doc
/*
* The number of dimensions.
*/
}
feature flattenedSize: Positive[1] = dimensions->reduce '*' ?? 1 {
doc
/*
* The number of elements in the Array, given as the product of the dimensions,
* or 1 if dimensions is empty.
*/
}
inv { flattenedSize == size(elements) }
}
datatype Bag :> Collection {
doc
/*
* A Bag is a variable-size Collection of which the elements are unordered and nonunique.
*/
}
datatype Set :> UniqueCollection {
doc
/*
* A Set is a variable-size Collection of which the elements are unique and unordered.
*/
}
datatype OrderedSet :> OrderedCollection, UniqueCollection
intersects OrderedCollection, UniqueCollection {
doc
/*
* An OrderedSet is a variable-size Collection of which the elements are unique and ordered.
*/
feature elements[0..*] ordered :>> OrderedCollection::elements, UniqueCollection::elements {
doc
/* Note: Redefinition of elements is unique by default. */
}
}
datatype List :> OrderedCollection {
doc
/*
* A List is a variable-size Collection of which the elements are nonunique and ordered.
*/
}
datatype KeyValuePair {
doc
/*
* A KeyValuePair is a DataValue that represents a pair of a key and an associated val,
* primarily for use in Maps.
*/
feature key: Anything[0..*] ordered nonunique;
feature val: Anything[0..*] ordered nonunique;
}
datatype Map :> Collection {
doc
/*
* Map is a variable-size Collection of which the elements are KeyValuePairs.
* The keys must be unique within the Map. The vals need not be unique.
*/
feature elements: KeyValuePair[0..*] :>> Collection::elements {
doc
/* Note: Redefinition of elements is unique by default.*/
}
inv {
elements->forAll{ in e1 : KeyValuePair;
not elements->exists{ in e2 : KeyValuePair;
e1 != e2 and e1.key->equals(e2.key) }
}
}
}
datatype OrderedMap :> Map, OrderedCollection {
doc
/*
* An OrderedMap is a variable-size Map that maintains an ordering of its elements.
*
* The ordering may be by key of the KeyValuePair elements, or by order of construction,
* or any other method. The essential aspect is that ordering is maintained and guaranteed across
* accesses to the OrderedMap.
*/
feature elements: KeyValuePair[0..*] ordered :>> Map::elements, OrderedCollection::elements {
doc
/* Note: Redefinition of elements is unique by default. */
}
}
}