|
Re: New to OCL - Need some Info [message #52954 is a reply to message #52923] |
Fri, 14 March 2008 13:55 |
Eclipse User |
|
|
|
Originally posted by: cdamus.ca.ibm.com
Hi, Shiva,
A question that I have for you is, how are clients of your base class
expected to access the value that it holds in a uniform way? Will they
somehow know which kind of value to expect? Or do they just have to
retrieve the value and make do with whatever it turns out to be?
The UML metamodel does something like what you suggest in the definition of
values of its primitive types. The ValueSpecification class has the
following operations:
- booleanValue() : Boolean
- integerValue() : Integer
- stringValue() : String
- unlimitedValue() : UnlimitedNatural
- isNull() : Boolean
which return the value of the appropriate type if it can be computed.
Specializations of the ValueSpecification define writable properties and
redefine these operations to return appropriate values. For example,
LiteralInteger
- value : Integer
- integerValue() : Integer -- redefined to return the 'value'
- stringValue() : Integer -- redefined to return the 'value' as a string
- unlimitedValue() : UnlimitedNatural -- redefined to return the 'value'
-- if it is positive
This kind of approach doesn't require OCL.
If you want your base type to be able to write the value, then OCL can help
with that. You could define pre-conditions on a mutator operation that
assert that the value can be stored in the context object. For example,
extending the UML ValueSpecification, we might define some setter
operations and read-only attributes with initial values:
ValueSpecification
setValue(b : Boolean)
setValue(i : Integer)
setValue(s : String)
setValue(u : UnlimitedNatural)
isBooleanValued : Boolean {readOnly} = false
isIntegerValued : Boolean {readOnly} = false
isStringValued : Boolean {readOnly} = false
isUnlimitedValued : Boolean {readOnly} = false
and some pre-condition constraints on the setter operations
context ValueSpecification::setValue(b : Boolean) :
pre: self.isBooleanValued
context ValueSpecification::setValue(i : Integer) :
pre: self.isIntegerValued
context ValueSpecification::setValue(s : String) :
pre: self.isStringValued
context ValueSpecification::setValue(u : UnlimitedNatural) :
pre: self.isUnlimitedValued
and in the literal specifications:
context LiteralBoolean::isBooleanValued : Boolean
init: true
context LiteralInteger::isIntegerValued : Boolean
init: true
context LiteralString::isStringValued : Boolean
init: true
context LiteralUnlimitedNatural::isUnlimitedValued : Boolean
init: true
HTH,
Christian
Shiva Selvadurai wrote:
> I have a model in which a class can hold a value which can be of String or
> Int or Float. I initially designed such a way that I have a class and
> derived it with three different class which holds corresponding values
> (i.e) one to hold String value so on.
>
> Now I don't need the derived class instead I need the base class itself to
> handle such a way that it can take in only values of these type. Is this
> possible using OCL and is there any way to handle the same.
>
> Please suggest.
>
> -Shiva.S
|
|
|
Powered by
FUDForum. Page generated in 0.02817 seconds