Home » Modeling » TMF (Xtext) » [Xtext] Model elements extra initialization
| |
Re: [Xtext] Model elements extra initialization [message #29585 is a reply to message #29530] |
Wed, 18 February 2009 11:22 |
Lazar Codrut-Lucian Messages: 91 Registered: July 2009 |
Member |
|
|
Hello,
I am using UML as a metamodel:
---mydsl.xtxt---
preventMMGeneration
importMetamodel "http://www.eclipse.org/uml2/2.1.0/UML" as uml;
....
---
I want to be able to change some attributes at initialization.
For instance, I have several rules for uml::Operation, one being:
---
ValidateOperationRule[uml::Operation]:
"validate" name=ID
;
---
I want to be able to set some properties in the uml::Operation to the
default values I need for a "ValidateOperation".
Also, I need to apply a stereotype to this uml::Operation.
What are the recommended steps needed for importing the UML profile and
applying the stereotypes?
Thank you,
Lucian
Sebastian Zarnekow wrote:
> Hi Lucian,
>
> sorry if I miss the point, but I think you'ld like to build a textual
> DSL on top of an existing UML metamodel? So you should be able to import
> your metamodel in your grammar and initialize instances and attributes
> via parser rules. This should be pretty straightforward. If you want to
> set some additional values (defaults, mandatory attributes in your
> metamodel that you do not want to represent in your textual syntax) you
> can use the linking hook in your resource.
>
> Please have a look at org.eclipse.xtext.crossref.internal.Linker.
> Although it is currently in an internal package, you should be able to
> create your own implementation that extends the default Linker (I'll
> file a bugzilla to move this useful baseclass to crossref.impl). The
> implementation provides several hooks to set (and unset) default values
> etc. See XtextLinker for extensive usage of the different template methods.
>
> Most likely we'll come up with another service interface that makes it
> more straighforward to enhance the model before and after the linker did
> his job.
>
> If my guess was wrong and you want to achieve something else please
> elaborate a bit more on it :-)
>
> Regards,
> Sebastian
>
>> I want to make a DSL that maps to UML elements.
>> I need to apply stereotypes and initialize certain attributes of the UML
>> elements I use.
>> Can this be done? What is the proper place to do this initialization?
>>
>> Thank you,
>> Lucian
|
|
|
Re: [Xtext] Model elements extra initialization [message #30146 is a reply to message #29585] |
Wed, 18 February 2009 13:12 |
Sebastian Zarnekow Messages: 3118 Registered: July 2009 |
Senior Member |
|
|
Hi Lucian,
the grammar should look like this snippet:
language my.namespace.UmlLanguage
import "http://www.eclipse.org/uml2/2.1.0/UML" as uml
ValidateOperationRule returns uml::Operation:
"validate" name=ID;
Pretty much the same as you suggested.
However, to initialize any default value, you have to subclass
org.eclipse.xtext.crossref.internal.Linker and override
* Linker.setDefaultValueImpl(EObject obj, EReference ref,
IDiagnosticProducer producer), and
* Linker.beforeEnsureIsLinked(EObject obj, EReference ref,
IDiagnosticProducer producer)
The latter is called before the linker made any attempt do establish a
crossreference for the given obj and feature. The first is a well
defined hook to initialize any default values for references. If you
want to init some EAttributes, I advise to use the more coarse-grained
method
* Linker.ensureLinked(EObject obj, IDiagnosticProducer producer).
For your short example, the code might look like similar to the following:
public class UMLLinker extends Linker {
@Inject
private UMLLanguageGrammarAccess grammarAccess;
@Override
public void ensureLinked(EObject obj, IDiagnosticProducer producer) {
super.ensureLinked(obj, producer);
if (obj instanceof Operation &&
isValidateOperationRule(
NodeUtil.getNodeAdapter(obj).getGrammarElement()) {
Operation op = obj;
// set default attributes
}
}
boolean isValidateOperationRule(EObject grammarElement) {
return grammarAccess.prValidateOperationRule().getRule() ==
grammarElement || // might even be a RuleCall with call.getRule() == ..
}
@Override
protected void setDefaultValueImpl(EObject obj, EReference ref,
IDiagnosticProducer producer) {
if (ref == <OperationStereoptype>) {
obj.setStereotype(createStereotype(..));
}
}
}
Don't forget to register your Linker with bindILinker() in your runtime
module.
The pseudo code is only a shot in the dark, but the pointer to existing
methods may help you to init your model as you like.
Regards,
Sebastian
Lazar Codrut-Lucian schrieb:
> Hello,
>
> I am using UML as a metamodel:
> ---mydsl.xtxt---
> preventMMGeneration
> importMetamodel "http://www.eclipse.org/uml2/2.1.0/UML" as uml;
> ...
> ---
>
> I want to be able to change some attributes at initialization.
> For instance, I have several rules for uml::Operation, one being:
> ---
> ValidateOperationRule[uml::Operation]:
> "validate" name=ID
> ;
> ---
> I want to be able to set some properties in the uml::Operation to the
> default values I need for a "ValidateOperation".
> Also, I need to apply a stereotype to this uml::Operation.
> What are the recommended steps needed for importing the UML profile and
> applying the stereotypes?
>
> Thank you,
> Lucian
>
> Sebastian Zarnekow wrote:
>> Hi Lucian,
>>
>> sorry if I miss the point, but I think you'ld like to build a textual
>> DSL on top of an existing UML metamodel? So you should be able to import
>> your metamodel in your grammar and initialize instances and attributes
>> via parser rules. This should be pretty straightforward. If you want to
>> set some additional values (defaults, mandatory attributes in your
>> metamodel that you do not want to represent in your textual syntax) you
>> can use the linking hook in your resource.
>>
>> Please have a look at org.eclipse.xtext.crossref.internal.Linker.
>> Although it is currently in an internal package, you should be able to
>> create your own implementation that extends the default Linker (I'll
>> file a bugzilla to move this useful baseclass to crossref.impl). The
>> implementation provides several hooks to set (and unset) default values
>> etc. See XtextLinker for extensive usage of the different template methods.
>>
>> Most likely we'll come up with another service interface that makes it
>> more straighforward to enhance the model before and after the linker did
>> his job.
>>
>> If my guess was wrong and you want to achieve something else please
>> elaborate a bit more on it :-)
>>
>> Regards,
>> Sebastian
>>
>>> I want to make a DSL that maps to UML elements.
>>> I need to apply stereotypes and initialize certain attributes of the UML
>>> elements I use.
>>> Can this be done? What is the proper place to do this initialization?
>>>
>>> Thank you,
>>> Lucian
|
|
|
Re: [Xtext] Model elements extra initialization [message #30463 is a reply to message #29585] |
Thu, 19 February 2009 08:59 |
Sebastian Zarnekow Messages: 3118 Registered: July 2009 |
Senior Member |
|
|
Hi Lucian,
I didn't get that your are refering to and still using
openArchitectureWare Xtext. So my answer won't help you...
Please post your oAW related questions in the forum at
www.openarchitectureware.org.
Regards,
Sebastian
> Hello,
>
> I am using UML as a metamodel:
> ---mydsl.xtxt---
> preventMMGeneration
> importMetamodel "http://www.eclipse.org/uml2/2.1.0/UML" as uml;
> ...
> ---
>
> I want to be able to change some attributes at initialization.
> For instance, I have several rules for uml::Operation, one being:
> ---
> ValidateOperationRule[uml::Operation]:
> "validate" name=ID
> ;
> ---
> I want to be able to set some properties in the uml::Operation to the
> default values I need for a "ValidateOperation".
> Also, I need to apply a stereotype to this uml::Operation.
> What are the recommended steps needed for importing the UML profile and
> applying the stereotypes?
>
> Thank you,
> Lucian
>
> Sebastian Zarnekow wrote:
>> Hi Lucian,
>>
>> sorry if I miss the point, but I think you'ld like to build a textual
>> DSL on top of an existing UML metamodel? So you should be able to import
>> your metamodel in your grammar and initialize instances and attributes
>> via parser rules. This should be pretty straightforward. If you want to
>> set some additional values (defaults, mandatory attributes in your
>> metamodel that you do not want to represent in your textual syntax) you
>> can use the linking hook in your resource.
>>
>> Please have a look at org.eclipse.xtext.crossref.internal.Linker.
>> Although it is currently in an internal package, you should be able to
>> create your own implementation that extends the default Linker (I'll
>> file a bugzilla to move this useful baseclass to crossref.impl). The
>> implementation provides several hooks to set (and unset) default values
>> etc. See XtextLinker for extensive usage of the different template methods.
>>
>> Most likely we'll come up with another service interface that makes it
>> more straighforward to enhance the model before and after the linker did
>> his job.
>>
>> If my guess was wrong and you want to achieve something else please
>> elaborate a bit more on it :-)
>>
>> Regards,
>> Sebastian
>>
>>> I want to make a DSL that maps to UML elements.
>>> I need to apply stereotypes and initialize certain attributes of the UML
>>> elements I use.
>>> Can this be done? What is the proper place to do this initialization?
>>>
>>> Thank you,
>>> Lucian
|
|
|
Re: [Xtext] Model elements extra initialization [message #30601 is a reply to message #30463] |
Fri, 20 February 2009 14:42 |
Lazar Codrut-Lucian Messages: 91 Registered: July 2009 |
Member |
|
|
I did not know I was using the other Xtext.
I'll try to install TMF Xtext and then I will try your suggestions here.
Thanks,
Lucian
Sebastian Zarnekow wrote:
> Hi Lucian,
>
> I didn't get that your are refering to and still using
> openArchitectureWare Xtext. So my answer won't help you...
>
> Please post your oAW related questions in the forum at
> www.openarchitectureware.org.
>
> Regards,
> Sebastian
>
>> Hello,
>>
>> I am using UML as a metamodel:
>> ---mydsl.xtxt---
>> preventMMGeneration
>> importMetamodel "http://www.eclipse.org/uml2/2.1.0/UML" as uml;
>> ...
>> ---
>>
>> I want to be able to change some attributes at initialization.
>> For instance, I have several rules for uml::Operation, one being:
>> ---
>> ValidateOperationRule[uml::Operation]:
>> "validate" name=ID
>> ;
>> ---
>> I want to be able to set some properties in the uml::Operation to the
>> default values I need for a "ValidateOperation".
>> Also, I need to apply a stereotype to this uml::Operation.
>> What are the recommended steps needed for importing the UML profile and
>> applying the stereotypes?
>>
>> Thank you,
>> Lucian
>>
>> Sebastian Zarnekow wrote:
>>> Hi Lucian,
>>>
>>> sorry if I miss the point, but I think you'ld like to build a textual
>>> DSL on top of an existing UML metamodel? So you should be able to import
>>> your metamodel in your grammar and initialize instances and attributes
>>> via parser rules. This should be pretty straightforward. If you want to
>>> set some additional values (defaults, mandatory attributes in your
>>> metamodel that you do not want to represent in your textual syntax) you
>>> can use the linking hook in your resource.
>>>
>>> Please have a look at org.eclipse.xtext.crossref.internal.Linker.
>>> Although it is currently in an internal package, you should be able to
>>> create your own implementation that extends the default Linker (I'll
>>> file a bugzilla to move this useful baseclass to crossref.impl). The
>>> implementation provides several hooks to set (and unset) default values
>>> etc. See XtextLinker for extensive usage of the different template
>>> methods.
>>>
>>> Most likely we'll come up with another service interface that makes it
>>> more straighforward to enhance the model before and after the linker did
>>> his job.
>>>
>>> If my guess was wrong and you want to achieve something else please
>>> elaborate a bit more on it :-)
>>>
>>> Regards,
>>> Sebastian
>>>
>>>> I want to make a DSL that maps to UML elements.
>>>> I need to apply stereotypes and initialize certain attributes of the
>>>> UML
>>>> elements I use.
>>>> Can this be done? What is the proper place to do this initialization?
>>>>
>>>> Thank you,
>>>> Lucian
|
|
|
Goto Forum:
Current Time: Thu Apr 25 21:33:09 GMT 2024
Powered by FUDForum. Page generated in 0.02566 seconds
|