Home » Modeling » TMF (Xtext) » [Xtext] Xtext newbie
[Xtext] Xtext newbie [message #38873] |
Tue, 07 April 2009 07:29  |
Eclipse User |
|
|
|
Hello
Actually, I am interested in the use of TMF Xtext, but I don't know
whether it can solve my problem.
Actually, I have a text file with a specific structure and I created a
metamodel of this file. What I exactly want is to automatically inject the
content of my text file in a model instantiation (with .ecore or .xmi
extension) of the metamodel. I don't want to generate code or text but a
model which is an instantiation of a metamodel.
I thought that can do this with TCS but I think it is not useful anymore.
I tried an example with TMF Xtext and it generated many packages which I
tried to understand.
It is ok but I don't really know how to start with this.
Could you please tell whether this is feasible with TMF Xtext. If so,
could you give an idea how to start with this.
Thank you so much for your interest.
Best regards,
Hayfa.
|
|
|
Re: [Xtext] Xtext newbie [message #38902 is a reply to message #38873] |
Tue, 07 April 2009 08:18   |
Eclipse User |
|
|
|
Hi Hayfa,
Hayfa schrieb:
> Actually, I am interested in the use of TMF Xtext, but I don't know
> whether it can solve my problem.
Let's see :-)
> Actually, I have a text file with a specific structure and I created a
> metamodel of this file. What I exactly want is to automatically inject
> the content of my text file in a model instantiation (with .ecore or
> .xmi extension) of the metamodel.
Sounds like what we refer to as "parsing".
Yes, that should be possible with Xtext.
> I don't want to generate code or text
> but a model which is an instantiation of a metamodel.
Sure you don't need to.
> I thought that can do this with TCS but I think it is not useful anymore.
Generally speaking it should also be possible to do this with TCS.
> I tried an example with TMF Xtext and it generated many packages which I
> tried to understand.
Do you mean Java packages?
It's not neccessary to understand the generated code in every detail, in
order to work with it.
> Could you please tell whether this is feasible with TMF Xtext. If so,
> could you give an idea how to start with this.
It's not clear to me what exactly the problem is. Do you need assistance
writing the grammar? Or is there something specific which does not work?
Maybe you want to provide a snippet of your text file you want to load?
Given that you already have an example running, you now should change
the content of the *.xtext file to reflect the structure of your text file.
Note, that if you don't need to write the ecore model manually Xtext can
derive it from the grammar. In most cases this sufficient and way more
convenient.
But if you want to reuse an existing ecore model, you need to change the
generate foo "..."
to something like this
import "classpath:/mypack/model.ecore"
This is described here :
http://wiki.eclipse.org/Xtext/Documentation#Importing_existi ng_Meta_Models
Regards,
Sven
|
|
|
Re: [Xtext] Xtext newbie [message #38951 is a reply to message #38902] |
Tue, 07 April 2009 09:45   |
Eclipse User |
|
|
|
Hi Sven,
Here is an examlpe of what I want to generate.
1)- Suppose that I have a metamodel of called "AddressBook". This
metamodel should for example contain the classes AddressBook and contacts.
Each contact has its own informations: first name, last name, phone
number,....
2)-On the other hand, I have a text file containing those infomations:
AddressBook {contact ('John', 'Smith', 'MyCompany_A',
'john.smith@nomail.com', '0001')
contact'Peter', 'Robinson', 'MyCompany_B', 'peter.robinson@nomail.com',
'0012')
contact ('Brenda', 'Smith', 'MyCompany_A', 'brenda.smith@nomail.com',
'0002')
}
3- Now I would like to generate automatically a sample model ".ecore" (for
instance "MyAddressBook")that conforms to the metamodel "AddressBook" and
contains the informations about the listed contacts (John, Peter and
Brenda)
I Tried to resolve this by TCS by I had some technical problems.
So my question is : Is it possible to generate my sample model with xText.
If so , how this??
Thank you again Sven
Best regards,
Hayfa.
|
|
|
Re: [Xtext] Xtext newbie [message #39001 is a reply to message #38951] |
Tue, 07 April 2009 10:17   |
Eclipse User |
|
|
|
Hi Hayfa,
you could use two rules like
AddressBook:
'AddressBook' '{'
(contacts+=Contact)*
'}';
Contact:
'contact' '('
lastName=STRING ','
firstName=STRING ','
companyName=STRING ','
email=STRING ','
someNumber=STRING
')';
as a starting point. It derives a metamodel that contains two EClasses
Addressbook and Contact with the needed features. The generated parser
and editor will be able to work with you provided input (apart from a
missing '(' before 'Peter').
If you want to reuse your existing metamodel instead have a look at the
reference documenation http://wiki.eclipse.org/Xtext/Documentation
It describes how you can import existing metamodels and make return
types of rules explicit.
Regards,
Heiko
> Hi Sven,
>
> Here is an examlpe of what I want to generate.
>
> 1)- Suppose that I have a metamodel of called "AddressBook". This
> metamodel should for example contain the classes AddressBook and
> contacts. Each contact has its own informations: first name, last name,
> phone number,....
>
>
> 2)-On the other hand, I have a text file containing those infomations:
>
> AddressBook {contact ('John', 'Smith', 'MyCompany_A',
> 'john.smith@nomail.com', '0001')
> contact'Peter', 'Robinson', 'MyCompany_B', 'peter.robinson@nomail.com',
> '0012')
> contact ('Brenda', 'Smith', 'MyCompany_A', 'brenda.smith@nomail.com',
> '0002')
> }
>
> 3- Now I would like to generate automatically a sample model ".ecore"
> (for instance "MyAddressBook")that conforms to the metamodel
> "AddressBook" and contains the informations about the listed contacts
> (John, Peter and Brenda)
>
>
> I Tried to resolve this by TCS by I had some technical problems.
>
> So my question is : Is it possible to generate my sample model with
> xText. If so , how this??
>
> Thank you again Sven
> Best regards,
>
> Hayfa.
>
|
|
| |
Re: [Xtext] Xtext newbie [message #39106 is a reply to message #39079] |
Tue, 07 April 2009 13:15   |
Eclipse User |
|
|
|
Hey Hayfa,
once you have generated everything from your grammar with the
Generate<DSL>.mwe you can use Xtext to load your text files into an
EObject graph that is an instance of the generated or imported ePackage.
The workflow you are mentioning below does exactly this in the line
> 1047 [main] INFO e.core.container.CompositeComponent - MweReader:
> loading file from classpath:/model/MyModel.pv
Have a look at the file <DSL>Generator.mwe and you will see that it
tries to load a specific textfile by calling a generic component MWEReader.
Xtext itself allows you to produce different artifacts from a given
grammar. One addresses loading models as described before. Another is
deriving a suitable metamodel. Yet another is and editor with content
assist, outline and syntax highlighting.
If you have all this and would like to concentrate on code generation
with the modeling workflow engine (mwe) and xpand (this is what the
Xtext wizard did produce for you to get you started) I would rather
point you to these tools. As a first step make yourself familiar with
Xpand to learn more about its features.
I first draft of your template could be
«DEFINE main FOR AddressBook»
«FOREACH contacts AS contact»
«contact.firstName» «contact.lastName»
«ENDFOREACH»
«ENDDEFINE»
The reference documentation of the oAW Xpand should give you more
information:
http://www.openarchitectureware.org/pub/documentation/4.3.1/ html/contents/core_reference.html
Regards,
Heiko
> Hi Heiko,
>
> I tried tried the example of the AddressBook but I have a problem when I
> try to run the Generator.mwe file in the workflow directory. I get this
> exception:
>
>
>
> ***********************************************
> 0 [main] INFO eclipse.emf.mwe.core.WorkflowRunner -
> ------------------------------------------------------------ --------------------------
>
> 16 [main] INFO eclipse.emf.mwe.core.WorkflowRunner - EMF Modeling
> Workflow Engine 0.7.0, Build v200903161748
> 16 [main] INFO eclipse.emf.mwe.core.WorkflowRunner - (c) 2005-2009
> openarchitectureware.org and contributors
> 16 [main] INFO eclipse.emf.mwe.core.WorkflowRunner -
> ------------------------------------------------------------ --------------------------
>
> 16 [main] INFO eclipse.emf.mwe.core.WorkflowRunner - running
> workflow: workflow/PvGenerator.mwe
> 16 [main] INFO eclipse.emf.mwe.core.WorkflowRunner - 391 [main]
> INFO lipse.emf.mwe.utils.StandaloneSetup - Registering platform uri
> 'C:\Documents and Settings\nakourih\workspace2'
> 1047 [main] INFO e.core.container.CompositeComponent -
> DirectoryCleaner: cleaning directory 'src-gen'
> 1047 [main] INFO ipse.emf.mwe.utils.DirectoryCleaner - Cleaning
> C:\Documents and
> Settings\nakourih\workspace2\org.xtext.example.pv.generator\ src-gen
> 1047 [main] INFO e.core.container.CompositeComponent - MweReader:
> loading file from classpath:/model/MyModel.pv
> 1453 [main] INFO e.core.container.CompositeComponent - Generator:
> generating 'templates::Template::main FOR model' => []
> 1469 [main] ERROR org.eclipse.xpand2.Generator - Error in
> Component of type org.eclipse.xpand2.Generator: EvaluationException
> : No Definition 'templates::Template::main for pv::AddressBook' found!
> [23,42] on line 1 'EXPAND templates::Template::main FOR model'
>
> 1469 [main] ERROR eclipse.emf.mwe.core.WorkflowRunner - Workflow
> interrupted. Reason: No Definition 'templates::Template::main for
> pv::AddressBook' found!
> 1469 [main] ERROR eclipse.emf.mwe.core.WorkflowRunner - [ERROR]: No
> Definition 'templates::Template::main for pv::AddressBook'
> found!(Element: EXPAND templates::Template::main FOR model; Reported by:
> Generator: generating 'templates::Template::main FOR model' => [])
>
> ************************************************************
>
> Is that because of the "Template.xpt" content? I haven't changed
> anything in it.
>
> On the other hand, even if it works, it will generate a text file. Is it
> possible to generate an ".ecore" or ".xmi file" that corresponds to my
> metamodel?
>
> I am sorry but I still have some confusions regrading the possible kind
> of inputs that xtext can generate foe me.
>
> Thank you,
>
> Best regards,
>
> Hayfa.
>
>
>
>
>
|
|
| |
Re: [Xtext] Xtext newbie [message #39199 is a reply to message #39137] |
Tue, 07 April 2009 16:35  |
Eclipse User |
|
|
|
Hey Hayfa,
for the grammar definition in Xtext you can use any URI that refers to
an existing EPackage, e.g.
import "classpath:/org/xtext/example/MyExistingMM.ecore" as mm
AddressBook returns mm::AddressBook :
'AddressBook' '{'
(contacts+=Contact)*
'}';
when generating code Xpand has to know your metamodel. This oAW forum
thread might be useful for you
http://www.openarchitectureware.org/forum/viewtopic.php?show topic=9122
As you will see, it not only deals with existing metamodels it discusses
addressbooks, too ;)
This might lead to something like
<bean class="org.eclipse.emf.mwe.utils.StandaloneSetup" platformUri="..">
<registerEcoreFile
value=" platform:/resource/org.xtext.example.mydsl/src/org/xtext/exa mple/MyExistingMM.ecore "/>
</bean>
And don't forget to change the imports of your Xpand templates.
Good luck,
Heiko
> Heiko,
>
> Thank you, it works now.
> Now when I want to import my existing ecore metamodel, I have seen in
> the documentation that fisrt of all, we have to export the model code of
> the ecore existing metamodel as a plugin, but I cannot do this with the
> export option of eclipse.
> Actuallay I created my ecore model starting from a KM3 file (by
> injecting the KM3 to ecore metamodel)
> Sorry for asking so many questions, but I have to run this as soon as
> possible :)
>
> Thank you
> Hayfa.
>
|
|
|
Goto Forum:
Current Time: Thu May 29 06:45:53 EDT 2025
Powered by FUDForum. Page generated in 0.04257 seconds
|