简体   繁体   中英

Problem integrating OCL types in Xtext grammar

I'm working on a grammar (Xtext project) where I want to reuse OCL types. Usually you refer in Xtext to existing types with an import statement, eg

import "http://www.eclipse.org/emf/2002/Ecore" as ecore

There is already an example Xtext project (CompleteOCL) which integrates OCL types in a grammar. Unfortunately the project refers in its import statements to local plugins, eg

import "platform:/resource/org.eclipse.ocl.examples.xtext.base/model/BaseCST.ecore" as base

So there are no dependencies defined in the Manifest.MF file. If I want to reuse OCL types in my grammar I have to write for example

import "http://www.eclipse.org/ocl/3.0.0/BaseCST" as base

I've added the org.eclipse.ocl.examples.xtext.base dependency and can write rules in my grammar which refer to (OCL) BaseCST types. If I try to generate the Xtext artifcats I get the following error:

     ... 3 more
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.eclipse.emf.mwe2.language.factory.SettingProviderImpl$1$1.setValue(SettingProviderImpl.java:54)
    ... 36 more
Caused by: java.lang.IllegalStateException: Problem parsing 'classpath:/org/xtext/example/mydsl/MyDsl.xtext':[XtextLinkingDiagnostic: null:5 Couldn't resolve reference to EPackage 'http://www.eclipse.org/ocl/3.0.0/BaseCST'.]
    at org.eclipse.xtext.generator.LanguageConfig.setUri(LanguageConfig.java:112)
    ... 41 more

So probably I'missing something? The problem is quite easy to reproduce. Create a new Xtext project, add the dependency, edit the grammar and add the last import statement ("import "http://...") and try to generate the Xtext artifacts. Any ideas are welcome!

Thanks in advance!
Michael

EDIT: In order to use a grammar you have to refer in your MWE2 workflow (see section 3.2.2.2 of the the Xtext documentation (version 1.0.1)) to the corresponding genmodel file of the ecore model, for example

fragment = org.eclipse.xtext.generator.ecore.EcoreGeneratorFragement {
  referencedGenModels = "platform:/plugins/org.eclipse.ocl.examples.xtext.base/model/BaseCST.genmodel
} 

Nevertheless it's still not working for me.

Is the CompleteOCL project part of the Eclipse workspace that contains your Xtext project? If so you could try to reference the Ecore file using the platform URI you mentioned:

import "platform:/resource/org.eclipse.ocl.examples.xtext.base/model/BaseCST.ecore" as base

instead of

import "http://www.eclipse.org/ocl/3.0.0/BaseCST" as base

If the CompleteOCL project's bundles are part of your Eclipse target platform (eg, part of your Eclipse installation), then the

Couldn't resolve reference to EPackage 'http://www.eclipse.org/ocl/3.0.0/BaseCST'

error imho indicates that the bundle providing this EPackage is not activated.

Your best shot should be to install that grammar within your eclipse. Import your CompleteOCL project. Then right click on it>export>deployable plug-ins and fragments>check CompleteOCL project>and click the radio button install into host>Click finish and restart Eclipse.

Now your should be able to make your import as import "http://www.eclipse.org/ocl/3.0.0/BaseCST" as base

Regards,

Xavier

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM