Hi everyone,
I'm trying to use MARTE profile in a standalone java application.
First of all, I register all needed packages and factories into the registries as follows:
ResourceSet set = UMLResourcesUtil.init(new ResourceSetImpl());
set.getPackageRegistry().put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
set.getPackageRegistry().put(StandardPackage.eNS_URI, StandardPackage.eINSTANCE);
set.getPackageRegistry().put(MARTEPackage.eNS_URI, MARTEPackage.eINSTANCE);
set.getPackageRegistry().put(GQAMPackage.eNS_URI, GQAMPackage.eINSTANCE);
set.getResourceFactoryRegistry().getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION,UMLResource.Factory.INSTANCE);
By analyzing the registry, I'm sure that it contains all stereotypes, thus I cannot understand where is the problem.
Then, I load the UML model as follows:
UMLResource umlResource = (UMLResource) getResourceSet().getResource(manager.string2FileUri(sourcePath.toString()), true);
return (Model) EcoreUtil.getObjectByType(umlResource.getContents(), UMLPackage.Literals.MODEL);
Thus, I use OCL programmatically to retrieve objects with a specific MARTE stereotype from the model, here a query example:
String query = "Node.allInstances()->select(node : Node | node.getAppliedStereotypes()->exists(s | s.name = 'GaExecHost'))->size()"
//Query is the OCL query and contextualElement is the model Root (i.e., a Model)
public Object evaluateOCL(String query, Model contextualElement) throws ParserException {
// create an OCL instance for Ecore
OCL<?, EClassifier, ?, ?, ?, ?, ?, ?, ?, Constraint, EClass, EObject> ocl;
ocl = OCL.newInstance(EcoreEnvironmentFactory.INSTANCE);
// create an OCL helper object
OCLHelper<EClassifier, ?, ?, Constraint> helper = ocl.createOCLHelper();
helper.setContext(UMLPackage.Literals.MODEL);
// helper.setInstanceContext(UMLPackage.eINSTANCE.getClass_());
OCLExpression<EClassifier> oclExpression = helper.createQuery(query);
Query<EClassifier, EClass, EObject> oclQuery = ocl.createQuery(oclExpression);
//this should return the number of Nodes stereotyped with GaExecHost (it should be 8, but it always returns 0
return oclQuery.evaluate(contextualElement);
}
I have also installed the OCL plugin in Eclipse to try the query that works well. Hence, I think that the problem is related to the profile registration.
My configuration is:
-- I have created the model with MagicDraw.
-- I'm using Eclipse for RCP and RAP Developers Oxygen 4.7.0
-- Java 8
-- I build the project with Maven
-- I don't know if it is relevant, but I'm using JUnit 4.
I would like to share the project with you, but it is a big project.
Please find attached the UML model.
Any suggestion?