Xtext DSL editor referring to the Java classes defined in my model - dsl

I have set of java classes and I want to refer them in the DSL which I am trying to edit using Xtext grammar. e.g. There is a class Employee :
class Employee{
String name;
double salary;
String organisation;
String status;
}
Now in my DSL I wish to use them. Hence when user is trying the name he should get these tree attributes as help. e.g. DSL i will like to trite like this:
employee.salary > 100000 then employee.status='GOLD'
My question is when i am typing the name such as employee than all the three attributes should be available to user as context help.

Related

IntelliJ Live Template for dart named constructor: lists class' fields

I want to generate a custom named constructor in Dart.
I have many dto class to implement and each should provide a named constructor like: ClassName.fromMap().
For example for this class:
class Student {
final String name;
final int age;
}
The generated constructor should be:
Student.fromMap(Map<String, dynamic> map) :
name = map['name'],
age = map['age'];
How can I retrieve the list of the field of my current class as strings? Is that even possibile?
Of course I can have a variable number of fields.
My template looks like:
$CLASS$.fromMap(Map<String, dynamic> map) :
$INITIALIZATION_LIST$
binding $CLASS$ to dartClassName().
Now I'd like to bind $INITIALIZATION_LIST$ to something like:
getClassFieldList().forEach((fieldName) => "$fieldName = map['$fieldName']")
Can I achieve something like that?
There is no way to retrieve a list of Dart class fields using predefined live template functions. You can try developing your own template macro for this. See https://intellij-support.jetbrains.com/hc/en-us/community/posts/206201699-create-a-new-expression-for-a-live-template-for-actionscript for some hints.
Existing live template functions implementations can be found at https://github.com/JetBrains/intellij-community/tree/master/platform/lang-impl/src/com/intellij/codeInsight/template/macro.
You can also try using Structural Search and Replace instead of live template

Cannot instantiate Abstract Type when creating participant or asset in composer-rest-server

My model is as follows
abstract concept Address {
o String street
o String zip
o String city
o String country
}
participant Actor identified by userId {
o String userId
o String firstName
o String name
o Address address
}
When I POST a new Actor in composer rest server, I get the following error
"Cannot instantiate Abstract Type Address in namespace
io.mydomain.myapp",
What am I missing out on here ?
Abstract types are not meant to be instantiated so no wonder that it can't be created.
Read: https://hyperledger.github.io/composer/latest/reference/cto_language
Specifically this quote is of interest:
An optional 'abstract' declaration, to indicate that this type cannot
be created. Abstract resources can be used as a basis for other
classes to extend. Extensions of abstract classes do not inherit the
abstract status. For example, the asset Vehicle defined above should
never be created, as there should be more specific asset classes
defined to extend it.
Consider the Concepts subheading of the docs.
TL;DR: Read the documentation.

UML class diagram dependency or association

I'm not really sure about how to distinguish whether I should define a relationship as dependency or association for certain cases.
For example,
class AttendanceSheet {
Map<String> students;
boolean[] attend;
public void addStudent(Student s)
{
students.add(s.getName(),s.getStudentNumber());
}
public void checkAttendance(String name) { //... }
}
class Student {
private String name;
private int staffNumber;
//more information such as address, age, etc..
Student(String n, int sn)
{
name = n;
studentNumber = sn;
}
public String getName()
{
return name.clone();
}
public String getStudentNumber()
{
return studentNumber;
}
}
For this case, would Student and Association have association or dependency?
This is because I'm not sure whether the association must have the actual reference of the object or it suffice to just have certain information that can reach the object (since student id and number is far more enough to know find out which student object it is directing to).
In your case the <<uses>> is sufficient, because you don't have actual properties of type Student in AttendanceSheet.
As a side note: not using object references and instead just having the studentNumber is - to say the least - an odd design. But I don't know the context.
On the business level those objects are related, but there is no single preferred method of diagramming this relationship.
Please see Section 9.5.4 of UML specification for more details on the topic, especially Figure 9.12
To be specific those two notations are semantically equivalent (I'm ignoring irrelevant details):
In the first one to keep a traceability you can use an explicit Dependency pretty much the way you did.
One can also consider students as a Shared Aggregation, however it might be also considered an overkill. Not necessary, just showing a possibility for an answer completeness.
You may also consider Qulified associations to indicate a reference to the Student is based on their specific properties. This is pretty much closest to your need. Sorry, I don't know how to achieve such notation in my tool, but you can find more details in Figure 11.37 in Section 11.5 of the aforementioned specification.

#Builder groovy AST transformation

I am looking for Groovy AST transformation that would generate builder pattern code inside my class.
I know there are something like #Canonnical or #ToString or #EqualsAndHashCode enhancers that automatically generate useful methods and hoped if there would be #GenerateBuilder. I want to use it something like this:
//Groovy code:
#GenerateBuilder
#CompileStatic
class Person {
String name
int age
Long id
String createdBy
}
//then in Java code:
Person p = Person.newBuilder()
.withName("pawel")
.withAge(19)
.withId(11123)
.withCreatedBy("system")
.build();
There's nothing prior to 2.3 that will do this
But groovy 2.3 has a new #Builder annotation
https://github.com/groovy/groovy-core/blob/master/src/main/groovy/transform/builder/Builder.java

ANTLR4 Tokenizing a Huge Set of Keywords

I want to embed some known identifier names into my grammar e.g. the class names of my project are known and I want to tell the lexer what identifiers are known keywords that actually belongs to the class-name token. But since I have a long list of class names (hundreds of names), I don't want to create a class-name lexer rule by listing all the known class name keywords in the rule, that will make my grammar file too large.
Is it possible to place my keywords into a separate file? One possibility I am thinking about is to place the keywords in a java class that will be subclassed by the generated lexer class. In that case, my lexer's semantic predicate can just call a method in custom lexer superclass to verify if the input token matches my long list of names. And my long list can be placed inside that superclass src code.
However, in the ANTLR4 book it says grammar options 'superClass' for combined grammar only set the parser's superclass. How can I set my lexer's superclass if I still want to use combined grammar. Or is there any other better method to put my long list of keywords into a separate "keyword file".
If you want each keyword to have its own token type, you can do the following:
Add a tokens{} block to the grammar to create tokens for each keyword. This ensures unique token types are created for each of your keywords.
tokens {
Keyword1,
Keyword2,
...
}
Create a separate class MyLanguageKeywords similar to the following:
private static final Map<String, Integer> KEYWORDS =
new HashMap<String, Integer>();
static {
KEYWORDS.put("keyword1", MyLanguageParser.Keyword1);
KEYWORDS.put("keyword2", MyLanguageParser.Keyword2);
...
}
public static int getKeywordOrIdentifierType(String text) {
Integer type = KEYWORDS.get(text);
if (type == null) {
return MyLanguageParser.Identifier;
}
return type;
}
Add an Identifier lexer rule to your grammar that handles keywords and identifiers.
Identifier
: [a-zA-Z_] [a-zA-Z0-9_]*
{_type = MyLanguageKeywords.getKeywordOrIdentifierType(getText());}
;

Resources