EMF入门开发者:Eclipse插件多EMF模型开发技术咨询
Hey there! Let's break down your questions step by step since you're just getting started with EMF—totally get the learning curve here.
1. 创建可在插件中可见的Ecore模型
Getting your Ecore model recognized by your Eclipse plugin involves a few key steps:
- First, design your Ecore model: Use Eclipse's built-in Ecore Model Editor to create your
.ecorefile. Define all your EClasses, EAttributes, references, and set the correct namespace URI (this is critical for later registration). - Integrate the model into your plugin project:
- Place your
.ecorefile in a dedicated folder likemodel/inside your plugin project to keep things organized. - Open your plugin's
MANIFEST.MF, go to the Runtime tab, and add the folder containing your.ecorefile to theBundle-Classpath(e.g., add.if themodel/folder is at the root, or explicitly addmodel/). - Register the model so EMF can find it at runtime: You have two solid options here:
- Generate model code with GenModel: Right-click your
.ecorefile →New > Other... > EMF Generator Modelto create a.genmodelfile. Then right-click the root node of the.genmodel→Generate Model Code. This generates Java classes (including a package implementation and factory) that automatically register your model when the plugin starts. Just make sure these generated classes are included in your plugin's build path. - Manual registration in the plugin activator: If you prefer not to generate code, add registration logic to your plugin's
Activatorclass'sstart()method:
Replace@Override public void start(BundleContext context) throws Exception { super.start(context); // Load the Ecore model from your plugin ResourceSet resourceSet = new ResourceSetImpl(); URI modelUri = URI.createPlatformPluginURI("/your.plugin.id/model/your-model.ecore", true); Resource resource = resourceSet.getResource(modelUri, true); // Get the root EPackage from the model EPackage modelPackage = (EPackage) resource.getContents().get(0); // Register it in the global EPackage registry EPackage.Registry.INSTANCE.put(modelPackage.getNsURI(), modelPackage); }your.plugin.idandyour-model.ecorewith your actual plugin ID and model file path.
- Generate model code with GenModel: Right-click your
- Place your
2. 在插件运行时创建Ecore模型的EMF实例
Once your model is properly registered, creating instances is straightforward—here's how to do it in both Java and Xtend:
Java Implementation
- If you generated model code: Use the auto-generated factory class directly:
// Replace with your actual factory and EClass names MyModelFactory modelFactory = MyModelFactory.eINSTANCE; Person newPerson = modelFactory.createPerson(); newPerson.setName("Johan"); - If you registered manually: You'll need to fetch the EClass first, then create the instance:
// Fetch your registered EPackage using its namespace URI EPackage modelPackage = EPackage.Registry.INSTANCE.getEPackage("http://your-namespace-uri.com/mymodel"); // Get the EClass you want to instantiate EClass personClass = (EClass) modelPackage.getEClassifier("Person"); // Create the instance EObject newPerson = EcoreUtil.create(personClass); // Set attributes using EMF's generic API newPerson.eSet(modelPackage.getEStructuralFeature("Person", "name"), "Johan");
Xtend Implementation
Xtend's syntax makes this even cleaner:
- With generated model code:
val modelFactory = MyModelFactory::eINSTANCE val newPerson = modelFactory.createPerson newPerson.name = "Johan" - With manual registration:
val modelPackage = EPackage.Registry.INSTANCE.getEPackage("http://your-namespace-uri.com/mymodel") val personClass = modelPackage.getEClassifier("Person") as EClass val newPerson = EcoreUtil::create(personClass) newPerson.eSet(modelPackage.getEStructuralFeature("Person", "name"), "Johan")
A quick tip: If you run into issues where the model isn't found at runtime, double-check your MANIFEST.MF classpath entries, ensure the namespace URI in your .ecore matches what you're using for registration, and verify that your activator's start method is actually executing.
内容的提问来源于stack exchange,提问作者Johan
相关产品推荐
相关产品推荐

