Adding Random
Operations to OCL
Antonio Vallecillo,
Martin Gogolla
SEPTEMBER 19, 2017
Random in OCL
• Uncertainty is an essential element of
many of the systems we have to model
• Values of properties representing measures
• Decisions and branches
• Number of elements of populations
• Expected durations, costs, tolerance,…
• Random numbers and probability distributions can be of help here
• They permit combining definite knowledge at one level with uncertain values
• Expectations and assumptions that remain uncertain or imprecise at high-
level, can be made precise and realized by stating the corresponding
percentages or the probability distributions that parameters or values follow
2
Random in OCL
• Current OCL specification does not provide support for random
numbers or probability distributions
• Indeterminism only provided by “any()” operation on OCL Collections
11.9.1 Collection
any()
Returns any element in the source collection for which body evaluates to true.
Returns invalid if any body evaluates to invalid for any element, otherwise if
there are one or more elements for which body is true, an indeterminate
choice of one of them is returned, otherwise the result is invalid.
source->any(iterator | body) =
source->select(iterator | body)->asSequence()->first()
3
Indeterminism <> random
• This is the common behaviour of OCL “any()”
4
OCL random operations
context Real::rand() : Real
post undetermined:
if self > 0.0 then ( 0.0 <= result ) and ( result < self )
else if self < 0.0 then ( result <= 0.0 ) and ( self < result )
else result = self endif endif
post statisticallyRandom:
-- A sequence contains no recognizable patterns or regularities
-- This property deserves its own discussion :-)
context Integer::srnd() : Integer
post seedGeneration:
-- if self > 0 then self is the new seed
-- else a new seed is automatically generated (time, etc.)
-- in both cases it returns the previous seed
5
Some examples
6
Probability distributions
7
Some examples
8
Some examples
9
Generating random samples
10
Generating random samples
11
Implementation (1/2)
<operation source="Real" name="rand" returnType="Real">
<body>
<![CDATA[
$self * Random.rand
]]>
</body>
</operation>
<operation source="Integer" name="srand“ returnType="Integer">
<body>
<![CDATA[
if $self > 0 return Random.srand($self) % 1000000
else return Random.srand() % 10000000
end
]]>
</body>
</operation> 12
Implementation (2/2)
• Employing USE
extension mechanism
• Ruby implementation
of newly defined
operations
13
14
Conclusions
• Random numbers and Probability distributions permit dealing with
uncertainty at different levels
• Values of parameters and attributes
• Number of objects and links
• Size of test samples and values
• Beyond the basic indeterminism currently provided by OCL.
• Implementation available at
https://www.dropbox.com/s/2j9tgejbj507id0/oclextensions.zip?dl=0
15
Thanks!
Adding Random
Operations to OCL
Antonio Vallecillo,
Martin Gogolla
SEPTEMBER 19, 2017

Adding Random Operations to OCL

  • 1.
    Adding Random Operations toOCL Antonio Vallecillo, Martin Gogolla SEPTEMBER 19, 2017
  • 2.
    Random in OCL •Uncertainty is an essential element of many of the systems we have to model • Values of properties representing measures • Decisions and branches • Number of elements of populations • Expected durations, costs, tolerance,… • Random numbers and probability distributions can be of help here • They permit combining definite knowledge at one level with uncertain values • Expectations and assumptions that remain uncertain or imprecise at high- level, can be made precise and realized by stating the corresponding percentages or the probability distributions that parameters or values follow 2
  • 3.
    Random in OCL •Current OCL specification does not provide support for random numbers or probability distributions • Indeterminism only provided by “any()” operation on OCL Collections 11.9.1 Collection any() Returns any element in the source collection for which body evaluates to true. Returns invalid if any body evaluates to invalid for any element, otherwise if there are one or more elements for which body is true, an indeterminate choice of one of them is returned, otherwise the result is invalid. source->any(iterator | body) = source->select(iterator | body)->asSequence()->first() 3
  • 4.
    Indeterminism <> random •This is the common behaviour of OCL “any()” 4
  • 5.
    OCL random operations contextReal::rand() : Real post undetermined: if self > 0.0 then ( 0.0 <= result ) and ( result < self ) else if self < 0.0 then ( result <= 0.0 ) and ( self < result ) else result = self endif endif post statisticallyRandom: -- A sequence contains no recognizable patterns or regularities -- This property deserves its own discussion :-) context Integer::srnd() : Integer post seedGeneration: -- if self > 0 then self is the new seed -- else a new seed is automatically generated (time, etc.) -- in both cases it returns the previous seed 5
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
    Implementation (1/2) <operation source="Real"name="rand" returnType="Real"> <body> <![CDATA[ $self * Random.rand ]]> </body> </operation> <operation source="Integer" name="srand“ returnType="Integer"> <body> <![CDATA[ if $self > 0 return Random.srand($self) % 1000000 else return Random.srand() % 10000000 end ]]> </body> </operation> 12
  • 13.
    Implementation (2/2) • EmployingUSE extension mechanism • Ruby implementation of newly defined operations 13
  • 14.
  • 15.
    Conclusions • Random numbersand Probability distributions permit dealing with uncertainty at different levels • Values of parameters and attributes • Number of objects and links • Size of test samples and values • Beyond the basic indeterminism currently provided by OCL. • Implementation available at https://www.dropbox.com/s/2j9tgejbj507id0/oclextensions.zip?dl=0 15
  • 16.
  • 17.
    Adding Random Operations toOCL Antonio Vallecillo, Martin Gogolla SEPTEMBER 19, 2017