If you are using EMF standalone, i.e. you are not running it in an Eclipse-based environment, and you want to use OCL some additional steps have to be performed besides registering your resource factory.
This description is for the MDT OCL project which can be found and installed from the Juno update site under Modeling > OCL Examples and Editors.
Note: See the update on Eclipse Mars.
For the sake of completeness this is the code necessary to register a resource factory to be able to load your model files:
// Register the XMI resource factory for the any extension Resource.Factory.Registry registry = Resource.Factory.Registry.INSTANCE; Map<String, Object> map = registry.getExtensionToFactoryMap(); map.put("*", new XMIResourceFactoryImpl()); // or specifically state your file extension // initialize your package YourPackage.eINSTANCE.eClass();
If you are using your own resource factory, e.g. to force generation of unique IDs when serializing your model, replace XMIResourceFactoryImpl with your own.
To use OCL additionally use the following:
// Register Pivot globally (resourceSet == null) // Alternatively register it just for your resource set (see Javadoc). org.eclipse.ocl.examples.pivot.OCL.initialize(null); String oclDelegateURI = OCLDelegateDomain.OCL_DELEGATE_URI_PIVOT; EOperation.Internal.InvocationDelegate.Factory.Registry.INSTANCE.put(oclDelegateURI, new OCLInvocationDelegateFactory.Global()); EStructuralFeature.Internal.SettingDelegate.Factory.Registry.INSTANCE.put(oclDelegateURI, new OCLSettingDelegateFactory.Global()); EValidator.ValidationDelegate.Registry.INSTANCE.put(oclDelegateURI, new OCLValidationDelegateFactory.Global()); OCLinEcoreStandaloneSetup.doSetup(); // Install the OCL standard library. OCLstdlib.install();
This requires dependencies to org.eclipse.ocl.examples.xtext.oclinecore and com.google.log4j (as David pointed out in the comments, thanks!). EMF itself requires org.eclipse.emf.ecore and org.eclipse.emf.ecore.xmi.
Since Eclipse Luna OCLInEcore does not re-export UML anymore. If you encounter a “
java.lang.NoClassDefFoundError: org/eclipse/uml2/uml/Type” exception during runtime when calling
OCLinEcoreStandaloneSetup.doSetup(), you need to add a dependency to org.eclipse.uml2.uml.resources as well.
Update 07.12.2012: Added missing call to install the OCL standard library and dependencies.
Update 28.06.2014: Added missing UML dependency for Eclipse Luna.
Update 18.08.2015: Updated for Eclipe Mars (in separate post).