OCL Requirement: Invariant for possible range of attribute values - object

So I have the following Object structure:
Class Node
RSSI: int
I need to write a OCL invariant for the requirement that the the RSSI values of all Node objects do not differ by more than 2. I tried to solve it with an iterate expression but since you can't really use variables IN OCL to store current max and min values (or at least im not aware of how to do that) I don't know how to do that. Can anyone help me with that?

You can use allInstances:
context: Node
inv: Node->allInstances()
->forAll(n1, n2| -2 <= n1.RSSI-n2.RSSI and n1.RSSI-n2.RSSI <= 2)

Related

Elegant application of attribute set/get or method to several objects of same type

I'd like to achieve the following without using loops or comprehensions (at least explicitly) as I believe this is more elegant, especially considering the degree of nested attributes I am using.
Say I have an object nested like so: a.b.c.d.e, where e is of type NodeList. NodeList holds a list of Node objects internally. You can use a.b.c.d.e.all() to get this list of all objects.
Node has a member val that I would like to set on all nodes in e.
I want syntax like this to work: a.b.c.d.e.all().val = <val>. Is there a way I can implement the all() method such that this is possible?
Thanks.
I've decided to achieve this by implementing:
a.b.c.d.e.all(lambda x: x.val = <val>) where all applies the lambda to each Node.
I think this solution is quite nice. Not ideal. If you have any other ideas, please let me know. Thanks.

How to limit solution domain in modelica

I have a very simple model in OpenModelica.
model doubleSolution
Real x ;
equation
x^2 -4 = 0;
end doubleSolution;
There are two mathematical solutions for this problem x={-2,+2}.
The Openmodelica Solver will provide just one result. In this case +2.
What if I'm intested in the other solution?
Using proper start values e.g. Real x(Start=-7) might help as a workaround, but I'm not sure if this is always a robust solution. I'd prefer if I could directly limit the solution range e.g. by (x < 0). Are such bundary conditions possible?
As you already noticed using a start value is one option. If that is a robust solution depends on how good the start value is. For this example the Newton-Raphson method is used, which highly depends on a good start value.
You can use max and min to give a variable a range where it is valid.
Check for example 4.8.1 Real Type of the Modelcia Language Specification to see what attributes type Real has.
Together with a good start value this should be robust enough and at least give you a warning if the x becomes bigger then 0.0.
model doubleSolution
Real x(max=0, start=-7);
equation
x^2 -4 = 0;
end doubleSolution;
Another option would be to add an assert to the equations:
assert(value >= min and value <= max , "Variable value out of limit");
For the min and max attribute this assert is added automatically.

Started to learn UML and OCL and when to use Inv, pre, post

So, I started learning OCL about 3 hours ago. I'm very confused about when you can use inv and when you need to use pre and post.
So I was wondering whether you can use inv in a situation like this?
context Service :: cost(d : double)
inv result = workDone.cost -> sum()
this is in answer to a question:
Write an OCL invariant stating that the total cost for a service is the sum of the costs
for all the work done on the car you must assume the existence of a method
Service::cost() and specify the result.
As you can see its asking for the ocl invariant stating, does this mean I have to use inv?
cheers
I don't think an invariant is the right thing to use. When the method first starts in some implementation language, before the sum has its final value, an invariant would be violated. Thus, I think you want a post condition. A post condition tells you what should be true when the method returns.

cql binary protocol and named bound variables in prepared queries

imagine I have a simple CQL table
CREATE TABLE test (
k int PRIMARY KEY,
v1 text,
v2 int,
v3 float
)
There are many cases where one would want to make use of the schema-less essence of Cassandra and only set some of the values and do, for example, a
INSERT into test (k, v1) VALUES (1, 'something');
When writing an application to write to such a CQL table in a Cassandra cluster, the need to do this using prepared statements immediately arises, for performance reasons.
This is handled in different ways by different drivers. Java driver for example has introduced (with the help of a modification in CQL binary protocol), the chance of using named bound variables. Very practical: CASSANDRA-6033
What I am wondering is what is the correct way, from a binary protocol point of view, to provide values only for a subset of bound variables in a prepared query?
Values in fact are provided to a prepared query by building a values list as described in
4.1.4. QUERY
[...]
Values. In that case, a [short] <n> followed by <n> [bytes]
values are provided. Those value are used for bound variables in
the query.
Please note the definition of [bytes]
[bytes] A [int] n, followed by n bytes if n >= 0. If n < 0,
no byte should follow and the value represented is `null`.
From this description I get the following:
"Values" in QUERY offers no ways to provide a value for a specific column. It is just an ordered list of values. I guess the [short] must correspond to the exact number of bound variables in a prepared query?
All values, no matter what types they are, are represented as [bytes]. If that is true, any interpretation of the [bytes] value is left to the server (conversion to int, short, text,...)?
Assuming I got this all right, I wonder if a 'null' [bytes] value can be used to just 'skip' a bound variable and not assign a value for it.
I tried this and patched the cpp driver (which is what I am interested in). Queries get executed but when I perform a SELECT from clqsh, I don't see the 'null' string representation for empty fields, so I wonder if that is a hack that for some reasons is not just crashing or the intended way to do this.
I am sorry but I really don't think I can just download the java driver and see how named bound variables are implemented ! :(
---------- EDIT - SOLVED ----------
My assumptions were right and now support to skip a field in a prepared query has been added to cpp driver (see here ) by using a null [bytes value].
What I am wondering is what is the correct way, from a binary protocol point of view, to provide values only for a subset of bound variables in a prepared query?
You need to prepare a query that only inserts/updates the subset of columns that you're interested in.
"Values" in QUERY offers no ways to provide a value for a specific column. It is just an ordered list of values. I guess the [short] must correspond to the exact number of bound variables in a prepared query?
That's correct. The ordering is determined by the column metadata that Cassandra returns when you prepare a query.
All values, no matter what types they are, are represented as [bytes]. If that is true, any interpretation of the [bytes] value is left to the server (conversion to int, short, text,...)?
That's also correct. The driver will use the returned column metadata to determine how to convert native values (strings, UUIDS, ints, etc) to a binary (bytes) format. Cassandra does the inverse of this operation server-side.
Assuming I got this all right, I wonder if a 'null' [bytes] value can be used to just 'skip' a bound variable and not assign a value for it.
A null column insertion is interpreted as a deletion.
Implementation of what I was trying to achieve has been done (see here ) based on the principle I described.

Template classes with native UML types, and OCL constraints for template classes

I have two questions concerning the same UML class diagram. The first one is about how to model template class with UML native types. The second one is about how to handle template classes in OCL constraints.
Question 1: template class
I would like to use a template class for intervals and represent it using UML standards. Intervals must be usable with integers and floats. The best method I found so far is the following:
The idea here is to have a template class, with parameter T being a superclass of either Integer and Float classes.
The problem I see is that I need to redefine the basic types of UML in order to group them. I would like to know if there is a clean way to define a template class and saying that T is either of type integer or float (being here the primitives of UML).
Question 2: OCL constraints for template classes
The second aspect in my problem is that I want to be able to add OCL constraints to say that my intervals must contain at least 2 elements. The problem is that the rules must not be the same depending on the binding of T in the previous class diagram.
For floats:
context Interval
inv : self.supBound > self.infBound
For integers:
context Interval
inv : (self.infBoundIncluded and self.supBoundIncluded) implies supBound - infBound >= 1
context Interval
inv : (not(self.infBoundIncluded) and self.supBoundIncluded) implies supBound - infBound >= 2
context Interval
inv : (self.infBoundIncluded and not(self.supBoundIncluded)) implies supBound - infBound >= 2
context Interval
inv : (not(self.infBoundIncluded) and not(self.supBoundIncluded)) implies supBound - infBound >= 3
So I need to find a way in OCL to say that some rules only apply when T is bound to Integer, and others when it is bound to Float. I am not an expert in OCL, and I couldn't find any helpful information, so I'm asking for some help.
Thanks in advance,
Bastien.
After more research, I came out with the following solutions:
Question 1
The solution is to use a template class with a generic type (this class won't be instantiable according to UML standards), and to bind it with realization classes. The corresponding UML class diagram is as follows:
Here, we have two usable classes IntegerInterval and RealInterval derived from a general template class Interval, which keeps things simple, in addition to using UML basic types integer and real.
Question 2
Because the separation between integer and real intervals is done at the class level, the OCL differentiation is straightforward. Thus the constraints are as follows:
context IntegerInterval
inv: ...
context RealInterval
inv: ...
Anyway, I'm still open to other suggestions :)

Resources