How to translate a Valloy virtual function code into an Alloy script? - alloy

I am working on a project and found out an article about Valloy they give an example of a code both in Valloy and Alloy but the problem is that the syntax of the Alloy script is not recognised by the Alloy analyser.
Exp :
fun Object::equals(obj: Object) {
this.class = Object_Class => this..Object_equals(obj)
this.class = Dimension_Class => this..Dimension_equals(obj)
this.class = Dimension3D_Class =>this..Dimension3D_equals(obj) }
the analyser can't accept a function with no return type and does not know the ".." !!
can any one help with this ?!!
Thank u in advance !!

That syntax is not standard Alloy. I'm not sure if VAlloy is still maintained or whether you can get it from the authors of that paper.

Related

What is the best way to fix this simple Dart Null error?

I'm following along with an online course and the instructor gave this example of code...but it now throws a Null Safety error...
"The parameter 'namePerson' can't have a value of null beccause of its type..but the implicit default value is null'
void main() {
greet(greeting: 'Hey', namePerson: 'Cindy');
}
void greet({String namePerson, String greeting}){
print("$greeting $namePerson");
}
Now after researching this I found I could fix the error in three ways..by placing a ? after the word String...or by placing the word 'required' before the String...or by giving a default value...eg.
void greet({String namePerson = 'Bob', String greeting = 'Sam'}){
I don't really understand though what we should ideally do in a situation like this?
I watched a Youtube video on null safety errors'..but it is still a bit beyond my current comprehension level. Can someone explain how and why you would solve this error? Thanks for any help or tips!
You should start by asking you "In the context of my function, are my parameters required of optionnal" ?
Clearly, a function to greet someone should have :
a required, so non nullable namePerson: you must have a name to greet someone. And you cannot have a defaultValue (arbitrary "Bob" ? why ? it doesn't really make sense)
an optional greeting but with a default value, so non nullable also : you must say something to him to greet him
So I'd have it like :
void greet({required String namePerson, String greeting = 'Hello'}){
So basically, you should separate the 2 aspects :
should the inner body of my function works with a nullable or non nullable value ?
should my function take a required, optional but non nullable (so with default) value, or a totally optional one (!nullable) ?
Hope that helped
Use null check operator . Your code will run. Use the code like below. Thanks.
void main() {
greet(greeting: 'Hey', namePerson: 'Cindy');
}
void greet({String? namePerson, String? greeting}){
print("$greeting! $namePerson!");
}

Groovy - Set your own property of an Integer

I just started to learn Groovy and wondering if you can set your own property for an integer. For example,
def a = 34.5.plus(34.34)
def b = 5.64.minus(3.43)
def c = 12.64.multiply(33.43)
In the above there are certain methods like plus minus and multiply
What should I do if I want to define some of my own methods for integers like that.
I searched Google but couldn't find much about it.
Sure, you can just add methods to the metaClass of Integer.
Here's an example:
Integer.metaClass.zeds = { -> 'z' * delegate }
assert 3.zeds() == 'zzz'
You can also add methods to a single instance of integer should you wish to, ie:
Integer num = 4
num.metaClass.halved = { -> delegate / 2.0 }
assert num.halved() == 2.0
You can also add methods to classes via Extension Methods a good explanation of which can be found over here
It should be noted (as you originally tagged this question as Java) that obviously, Java code will have no knowledge of these things, as it doesn't know about the metaClass
Use groovy meta programming, this allows you to create dynamic method creation atruntime in the class that you want to place in .
bydefault if a method is not found methodmissing exception throws , this is where groovy allows you add method at runtime for more reference use the below comprehensive link
http://groovy-lang.org/metaprogramming.html
If this answer helps , dont forget to click answered.

How to define function in gremlin that returns the same result as in gremlin shell?

I'm currently using TinkerPop Gremlin (with a Titan backend) to implement a "similar document" algorithm.
The next lines are working perfectly well in gremlin shell :
v = g.v(880068)
m=[:]
v.as('x').out('auto_tag').in('auto_tag').has('status', 1).except('x').groupCount(m).filter{false}
results=[]
m.sort{-it.value}[0..9].each(){key, value -> results.add(key.document_id) }
results
following results are visible :
==>3188749
==>3190640
==>3191407
==>3187753
==>3186634
==>3185534
==>3189883
==>3190108
==>3187088
==>3188890
But when I try to "wrap" the same code in a function, it doesn't work anymore :
v = g.v(880068)
def get_similar_documents(v) {
m=[:]
v.as('x').out('auto_tag').in('auto_tag').has('status', 1).except('x').groupCount(m).filter{false}
results=[]
m.sort{-it.value}[0..9].each(){key, value -> results.add(key.document_id) }
return results
}
get_similar_documents(v)
...nothing is returned
Coming from a Python backend, I assume this is related to variable scope but so far I don't understand how to fix it.
Thanks in advance for any help
Edit : I'm using Bulbs, that's why I'd like to wrap my code in a function (That I could later call from Python)
I think you need to iterate your pipeline when within the get_similar_documents function. Meaning:
v.as('x').out('auto_tag').in('auto_tag').has('status', 1).except('x').groupCount(m).filter{false}.iterate()
It's important to remember that the Gremlin Shell automatically iterates pipelines for you. The shell isn't iterating it within the function so no side-effects are being generated to your m in the groupCount.
You can read more about there here.

Calculate Translation Repetitions

I have looked around the web for the standard formula for calculating repetitions in a document to be translated. I have not found it. For those who don't know what repetitions in translation means, this gives a good description of it.
I first tried something like this
using System;
using System.Collection.Generic;
using System.Text.RegularExpressions;
using System.Linq;
<snip>
Dictionary<string, int> _dict = new Dictionary<string, int>();
int CalculateRepetitions(string plainTextDoc) {
foreach (string item in Regex.Split(plainTextDoc, "\\P{L}+"))
if (_dict.ContainsKey(item))
_dict[item]++;
else
_dict.Add(item, 0);
return _dict.Where((key, value) => value > 0).Count();
}
but that was not close to the sample number from Trados for the same document, and was the wrong definition of repetitions anyway. Does anyone have a good example for calculating translation repetitions? I'm not expecting only C# answers, I'm good with java and c++ answers as well.
The GMX/V standard might be your answer and there seems to be a C# implementation.

Is it possible to define a new operator in Groovy?

Is it possible to define a brand new operator in Groovy? I would like to express a trade where someone buys 200 items for the price of 10 like this:
def trade = 200 # 10
Is this achievable?
Thanks
EDIT: I want to make it clearer that I am interested in defining an operator not adding a method. Cheers.
We always wanted the ability to define an operator through the user in Groovy, but so far we haven't gotten around the problems that come along with that. So the current state is that Groovy does not support custom operators, only the ones that are already in use.
I am not quite sure how you can make this work for the # sign but you could certainly add the operation like this which I actually find more expressive:
Number.metaClass.buyFor { Integer price ->
delegate * price
}
def result = 200.buyFor(10)
println result
Number.metaClass."#" {Integer x -> delegate * x}
assert (2.'#' (2)) == 4
The official documentation has a section on Operator Overloading: https://groovy-lang.org/operators.html#Operator-Overloading
Here is a list from the docs:

Resources