how to hide something that i don't want to look in Xtext - xtext

I am using Sirius and Xtext to do a graphic-text two-way synchronization work, probably after I use Sirius to draw a graphic to generate Xtext text, and modifying the text will modify the graphic synchronously.
When I draw with Sirius, the model will store a lot of necessary information, as shown below
[enter image description here][1]
I will get the XML likes this :
<siriusModel:ElementFactory>
<Elements> type="A" deviveName="A" logicalEntity="AbstractA"
<Elements> type="B" deviceName="B" logicalEntity="AbstractB"
</Elements>
</Elements>
</siriusModel:ElementFactory>
My xtext grammer is like this:
ElementFactory returns ElementFactory:
{ElementFactory}
'ElementFactory'
'{'
Elements+=Element*
'}';
Element returns Element:
{Element}
'{'
'[''type''='type=STRING']'
'[''deviceName''='deviceName=STRING']'
'[''logicalEntity''='logicalEntity=STRING']'
'}';
And my DSL looks like this:
ElementFactory{
Element{
[type="A"]
[deviceName="A"]
[logicalEntity="AbstractA"]
}
Element{
[type="B"]
[deviceName="B"]
[logicalEntity="AbstractB"]
}
}
but i don't want to show the [deviceName="A"] and [logicalEntity="AbstractA"] , I only want to this:
ElementFactory{
Element{
[type="A"]
}
Element{
[type="B"]
}
}
is there any way i can do it? Any help i would appreciate! thanks!
[1]: https://i.stack.imgur.com/qh5Qy.png

Your problem is that you want your textual concrete syntax (Xtext grammar) to not represent your whole semantic model. As far as I know this is not really possible for Xtext: in general the whole file is the whole model.
In Sirius you can more easily choose to not represent some of the semantic informations and its OK.
I would advise you to devise a "real, complete" abstract syntax for your language, then also create an Xtext-facing abstract syntax to use for the textual parts, and synchronize between the "real" model and the "made for text" one. I believe Eclipse OCL works like this as well with several metamodels with transformations in-between in order to both accomodate the Xtext technology and the OCL standard/API it is supposed to conform to.

Related

Writing Xtext Grammar for method calls like java

Hi I have already written grammar like below for one of my requirement which uses xtext editor.
Feature:
dclass=[ecore::EClass] (".")? (feature=[ecore::EStructuralFeature])?;
Basically i'm crossreferencing my emf model, so that i can call methods on my Objects, Which supports functionality like below- If I have Employee class with methods getSalary() And Salary as class with method getBasic()
Presently below call is supported
Employee.salary
But what i want is since the above code returns Salary i want to call further methods on that
Employee.salary.amount
or Employee.getSalary().getAmount()
How can i achieve that? Any help?
I just changed the grammar as below and then through scope provider i achieved the content assist.
Feature:
{Feature} dclass=[ecore::EClass];
DotExpression:
(Feature) ({DotExpression.ref=current} "."tail=[ecore::EStructuralFeature])*;
Use DotExpression to get the java like behaviour

How to implement the generation gap pattern for xText?

Currently I'm modifying a project that uses xText to parse a custom DSL. I want to add functionality to the generated classes but unfortunately I failed implementing the generation gap pattern. I used this article as a basis:
http://heikobehrens.net/2009/04/23/generation-gap-pattern/
My problem is that we're using a lot of Fragments to customize the org.eclipse.xtext.generator.Generator. It seems I cannot reuse those fragments for org.eclipse.xpand2.Generator.
So in conclusion:
how can I implement the generation gap pattern for the xtext generator
OR how can I use Fragments with the xpand2-Generator
OR is there a third solution that allows me to use fragments and implement the generation gap pattern?
After researching the matter, I'm thoroughly confused.
The generation gap pattern described in the article will work with almost any general purpose code generation framework. Xtext is no exception to this.
Besides that, Xtext offers another nifty solution to fill the generation gap. This is Xbase. But Xbase forces you to tightly integrate with java, so this is not always an alternative.
Consider following steps adding generation gap pattern to the existing Xtext project:
Locate generated file with the gap (with the code fragment you want to write by hand). Let it be e.g. MyClass.
Alter generator so that
the generated file get renamed to the AbstractMyClass.
the abstract keyword get added to the AbstractMyClass class definition.
the gap get moved to a single method.
an abstract method get generated for the gap.
the abstract method get called from the generated code.
Add 'MyClass extends AbstractMyClass' by hand and implement the abstract gap-method
If you have concrete problems with some Xtend2 code, post questions here or in Xtend forum.

Injecting code into generated xText classes

A rule in xText called "Component" will typically generate a class "Component" in the src-gen folder.
I would like to add additional methods to these classes without them being overridden every time I make minor changes to the DSL. What's the proper way to inject my own code into these classes and is there a way to make all classes extend my own root class instead of the default EObject?
Thanks in advance.
You basically have two choices:
You can use a IXtext2EcorePostProcessor to modify the EMF-model which Xtext inferred from your grammar. The actual code generation is done by EMF, so you have to fiddle your code through that bottleneck. The details are described in a blog of Christian Dietrich. This approach is only suitable for small modifications.
You can use the "generation gap pattern" (a.k. "implementation gap pattern") which allows you the write classes which derive from the generated model classes. Here you can add anything you want. The details are described in a blog of Heiko Behrens. This approach is better suited for large scale modifcations by inheritance.
You may of course mix the two approaches...

Binding nested complex objects with KSOAP2 Android

I'm trying to bind a XML response from a WS to a set of POJO's classes using ksoap2-android.
A lot of examples on the internet treat very simple responses like this one.
In my case however, I have a lot of custom classes and they keep a reference of each other, sometimes even inside an arraylist.
Here's what i'm trying to do, i would like to bind this kind of response :
<Car number="35">
<Engine>
[...]
</Engine>
<Passenger id="1">
[...]
</Passenger>
<Passenger id="2">
[...]
</Passenger>
</Car>
To this kind of class :
public class Car {
private int number;
private Engine engine;
private ArrayList<Passenger> passengers;
}
With, if possible, only modifying POJOs files (the Simple XML annotations system is very elegant, too bad there's no equivalent to this in ksoap).
I looked into the KVMSerializable interface, but when I try to override the getPropertyInfo() method, I have no idea of what I should return in the PropertyInfo.type and how will ksoap handle ArrayLists.
I got a huge headache right now, please help me
Please go to the below URL and read it.
http://www.c-sharpcorner.com/UploadFile/88b6e5/how-to-call-web-service-in-android-using-soap/
Thanks
Ashok Parmar
Traction Software Co.
You should read the document from ksoap 2, they have many useful examples in there.
For getting an array of complex type, you can check here
And I have used this approach to parse a complex object.
If all of them not work, you have to map field by field from soap object to your pojo.
Updated:
Thanks a lot for the 3rd link, it's very useful and I'm now able to
map custom objects. However, in the wiki page you provided, the author
is parsing an array of custom classes, wrapped in a parent element. Is
there a way to do this with inline lists like in my example ?
I have never tried it before, but I think you can combine my answer and the wiki.
First, you can try an example from wiki to implement your passengers list (extends Vector). Then you can use my approach to create a complex object with arraylist inside. The important thing is you must register your object with the response from web service. Something like this:
public class PassengerVector extends Vector<Passenger> implements KvmSerializable {
}
envelope.addMapping(NAMESPACE, "Car", Car.class);
envelope.addMapping(NAMESPACE, "Passenger", PassengerVector.class);
But I'm not sure this way can work. For a very complex object like your example, I recommend you should get data field by field by its name, as like the wiki

How does an interpreter use a DSL?

I'm using an interpreter for my domain specific language rather than a compiler (despite the performance). I'm struggling to understand some of the concepts though:
Suppose I have a DSL (in XML style) for a game so that developers can make building objects easily:
<building>
<name> hotel </name>
<capacity> 10 </capacity>
</building>
The DSL script is parsed, then what happens?
Does it execute an existing method for creating a new building? As I understand it does not simply transform the DSL into a lower level language (as this would then need to be compiled).
Could someone please describe what an interpreter would do with the resulting parsed tree?
Thank you for your help.
Much depends on your specific application details. For example, are name and capacity required? I'm going to give a fairly generic answer, which might be a bit overkill.
Assumptions:
All nested properties are optional
There are many nested properties, possibly of varying depth
This invites 2 ideas: structuring your interpreter as a recursive descent parser and using some sort of builder for your objects. In your specific example, you'd have a BuildingBuilder that looks something like (in Java):
public class BuildingBuilder {
public BuildingBuilder() { ... }
public BuildingBuilder setName(String name) { ... return this; }
public BuildingBuilder setCapacity(int capacity) { ... return this; }
...
public Building build() { ... }
}
Now, when your parser encounters a building element, use the BuildingBuilder to build a building. Then add that object to whatever context the DSL applies to (city.addBuilding(building)).
Note that if the name and capacity are exhaustive and are always required, you can just create a building by passing the two parameters directly. You can also construct the building and set the properties directly as encountered instead of using the builder (the Builder Pattern is nice when you have many properties and said properties are both immutable and optional).
If this is in a non-object-oriented context, you'll end up implementing some sort of buildBuilding function that takes the current context and the inner xml of the building element. Effectively, you are building a recursive descent parser by hand with an xml library providing the actual parsing of individual elements.
However you implement it, you will probably appreciate having a direct semantic mapping between xml elements in your DSL and methods/objects in your interpreter.

Resources