Count the occurrences of a boolean operation - uml

I have to make an OCL rule saying that two parameters cannot be equal. I cannot use the not() so I have to show that something like param1 = param2 is empty.
I tried to use isEmpty() and size() but as it's boolean, these operators don't work
self.ab1.ab2
->forAll(x | x.b1.b2
->forAll(port | (self.param1 = port.param2)->isEmpty())
)
I think I have to use some kind of count() operator that needs to be equal to 0 to show that param1 and param2 are different but I don't know how to use it.
Thank you for your help

For your direct question, use of <> and so self.param1 <> port.param2 would seem appropriate. However doing a doubly nested forAll starting in some context seems unhelpful. If you cannot move the 'constraint' to a better context perhaps you can at least be clearer by:
self.ab1.ab2.b1.b2.param2->asSet()->excludes(self.param2)

Related

Haskell define multiple functions using tuples

I've just come across some Haskell code that looks something like this:
(functionOne, functionTwo)
| someCondition = (10, "Ten")
| otherwise = (20, "Twenty")
From the way the code is used I think I understand the intent of this code i.e. it is just a more concise way of writing this:
functionOne
| someCondition = 10
| otherwise = 20
functionTwo
| someCondition = "Ten"
| otherwise = "Twenty"
However, I can't recall ever seeing functions written this way before and have no idea what this technique is called so can't search for any additional information about this.
So my questions are:
Is my understanding of what is going on here correct?
Does this technique have a name?
These aren't functions, just variable bindings. You correctly understand how it works. It doesn't have any particular name, because it's just another application of pattern matching. Anytime you could declare a variable, you can declare a more complex pattern in that same position.

Groovy compareTo for CustomClass and numbers/strings

I am building DSL and try to define a custom class CustomClass that you can use in expressions like
def result = customInstance >= 100 ? 'a' : 'b'
if (customInstance == 'hello') {...}
Groovy doesn't call == when your class defines equals and implements Comparable (defines compareTo) at the same time.
Instead Groovy calls compareToWithEqualityCheck which has a branching logic. And unless your custom DSL class is assignable from String or Number your custom compareTo won't be called for the example above.
You can't extend CustomClass with String.
I feel like I am missing something. Hope you can help me figure out how to implement a simple case like I showed above.
Here is a short answer first: You could extend GString for the CustomClass. Then its compareTo method will be called in both cases - when you check for equality and when you actually compare.
Edit: Considering the following cases, it will work for 1 and 2, but not for 3.
customInstance >= 100 // case 1
customInstance == 'hallo' // case 2
customInstance == 10 // case 3
Now I will explain what I understand from the implementation in Groovy's ScriptBytecodeAdapter and DefaultTypeTransformation.
For the == operator, in case Comparable is implemented (and there is no simple identity), it tries to use the interface method compareTo, hence the same logic that is used for other comparison operators. Only if Comparable is not implemented it tries to determine equality based on some smart type adjustments and as an ultima ratio falls back to calling the equals method. This happens in DefaultTypeTransformation.compareEqual#L603-L608
For all other comparison operators such as >=, Groovy delegates to the compareToWithEqualityCheck method. Now this method is called with the equalityCheckOnly flag set to false, while it is set to true for the first case when it the invocation originates from the == operator. Again there is some Groovy smartness happening based on the type of the left side if it is Number, Character, or String. If none applies it ends up calling the compareTo method in DefaultTypeTransformation.compareToWithEqualityCheck#L584-L586.
Now, this happens only if
!equalityCheckOnly || left.getClass().isAssignableFrom(right.getClass())
|| (right.getClass() != Object.class && right.getClass().isAssignableFrom(left.getClass())) //GROOVY-4046
|| (left instanceof GString && right instanceof String)
There are some restrictions for the case of equalityCheckOnly, hence when we come from the == operator. While I can not explain all of those I believe these are to prevent exceptions to be thrown under specific circumstances, such as the issue mentioned in the comment.
For brevity I omitted above that there are also cases that are handled upfront in the ScriptBytecodeAdapter and delegated to equals right away, if left and right hand side are both of the same type and one of Integer, Double or Long.

Prolog - List into String

im done of searching here for a solution but nothing so i decided to ask.
So, i have a query that returns in a list something like this:
List = [6-"read",3-"magazines",3-"music",1-"sport"].
And i need to perform a transformation so do i can get the list like this:
List = [read,magazines,music,sport].
or
List = ["read","magazines","music","sport"].
For that i think i should pass first the to a string to take out the numbers and the '-'. But im struggling with that.
Hope someone can help me! Thanks
This looks like homework, so I will not give you the full implementation. What you are looking for is pattern matching in a rule head:
fst_pair(X, pair(X,Y)).
The - operator is just an infix function symbol such that you could write
fst(X, X-Y).
Using this kind of pattern matching it should be easy to write a recursive predicate over the list. It must have a base case for the empty list and a step case for a head followed by the tail of the list:
fsts_list([], []).
fsts_list([ ... | TailFirsts ], [... | Pairs] ) :- % replace ... by some terms
% possibly insert some predicates here, depending on what you do above
fst_lists(TailFirsts, Pairs).
Happy solving!

Multiple methods call in a method (OCaml)

I would like to call more than 3 methods in the same method. Let's say that I want to set three different variables of an object with three different "set" method.
What is the best way to do it?
I tried with "and" but it doesn't seem to work.
In OCaml, ";" is the sequence operator. Expression "a; b" evaluates expression a, discards its result (which should be unit), evaluates b, then returns the result of b.
In OCaml, a function call or a method call is nothing but an expression.
So, you can write something like this:
method my_method =
my_object#set_x 10;
my_object#set_y 50;
my_object#set_z 30

How to add only non-null item to a list in Groovy

I want to add non null items to a List. So I do this:
List<Foo> foos = []
Foo foo = makeFoo()
if (foo)
foos << foo
But is there a way to do it in a single operation (without using findAll after the creation of the list). Like:
foos.addNonNull(makeFoo())
Another alternative is to use a short circuit expression:
foo && foos << foo
The foo variable must evaluate to true for the second part to be evaluated. This is a common practice in some other languages but I'd hesitate to use it widely in groovy due to readability issues and conventions.
No, you'd need to use an if, or write your own addNonNull method (which just uses an if)
Also:
if( foo ) {
probably isn't enough, as this will skip empty strings, or 0 if it returns integers
You'd need
if( foo != null ) {
The answer is YES! we can get rid of assigning a variable
Foo foo = makeFoo()//we can ditch this
The answer is NO we can't get rid of the condition. BUT we can make it more compact.
Here's how
List<Foo> foos = []
foos += (makeFoo()?:[]);
The trick is groovy's "+" operator which works differently based on what is to the left and what is to the right of the "+". It just so happens that if what is on the left is a list and what is on the right is an empty list, nothing gets added to the list on the left.
Pros are it is quick to type and compact.
Cons are it is not instantly obvious what is happening to most people
AND we replaced the variable assignment with an extra operation. Groovy is
going to try to do something to List foos no matter what, it just so happens that in the second case the result of that operation gives us a desired result.

Resources