Home » Modeling » Papyrus » Using SysML and custom profile programmatically
Using SysML and custom profile programmatically [message #1701530] |
Mon, 13 July 2015 21:28 |
Víctor López Messages: 22 Registered: June 2014 |
Junior Member |
|
|
Hi all,
I would like to create a Papyrus model programatically in a a plugin to generate some UML elements and the apply some profiles to them.
I already know how to create the model:
ModelSet modelSet = new ModelSet();
URI diURI = URI.createPlatformResourceURI("MyProject/MyModel.di", true);
ModelsReader reader = new ModelsReader(); //Standard ModelsReader for Di + UML + Notation
reader.readModel(modelSet);
try {
modelSet.loadModels(diURI);
} catch (ModelMultiException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
modelSet.createModels(diURI); //Use an EMF URI instead of an Eclipse IFile
ServicesRegistry registry;
try {
registry = new ExtensionServicesRegistry(org.eclipse.papyrus.infra.core.Activator.PLUGIN_ID);
registry.add(ModelSet.class, Integer.MAX_VALUE, modelSet);
registry.startRegistry();
} catch (ServiceException ex) {
log("Error in registry.", ex);
}
IModelCreationCommand creationCommand = new CreateUMLModelCommand();
creationCommand.createModel(modelSet);
try {
modelSet.save(new NullProgressMonitor());
} catch (IOException e) {
e.printStackTrace();
}
This is working and I can create classes, etc.
My problem is that I don't how to load the SysML and the custom profile to apply them to the model I created.
After searching the forums I tried this code to register SysML profile, but I always get an error related to 'SysmlPackage.eINSTANCE'. It looks like it is not available. So I guess something must be done before registering the profile.
public void registerSysmlProfile() {
initPackageRegistry(EPackage.Registry.INSTANCE);
initURIConverterURIMap(URIConverter.URI_MAP);
UMLPlugin.getEPackageNsURIToProfileLocationMap().put(SysmlPackage.eNS_URI, URI.createURI(SysmlResource.SYSML_PROFILE_URI));
}
private void initURIConverterURIMap(final Map<URI, URI> uriMap) {
final URI baseUri = URI.createURI(SysmlResource.SYSML_PROFILE_URI);
uriMap.put(URI.createURI(SysmlResource.PROFILES_PATHMAP), baseUri.appendSegment("model").appendSegment(""));
uriMap.put(URI.createURI(SysmlResource.LIBRARIES_PATHMAP), baseUri.appendSegment("libraries").appendSegment(""));
}
private void initPackageRegistry(final EPackage.Registry packageRegistry) {
packageRegistry.put(org.eclipse.papyrus.sysml.SysmlPackage.eNS_URI, SysmlPackage.eINSTANCE);
packageRegistry.put(BlocksPackage.eNS_URI, BlocksPackage.eINSTANCE);
packageRegistry.put(RequirementsPackage.eNS_URI, RequirementsPackage.eINSTANCE);
packageRegistry.put(ActivitiesPackage.eNS_URI, ActivitiesPackage.eINSTANCE);
packageRegistry.put(AllocationsPackage.eNS_URI, AllocationsPackage.eINSTANCE);
packageRegistry.put(ConstraintsPackage.eNS_URI, ConstraintsPackage.eINSTANCE);
packageRegistry.put(InteractionsPackage.eNS_URI, InteractionsPackage.eINSTANCE);
packageRegistry.put(ModelelementsPackage.eNS_URI, ModelelementsPackage.eINSTANCE);
packageRegistry.put(PortandflowsPackage.eNS_URI, PortandflowsPackage.eINSTANCE);
packageRegistry.put(StatemachinesPackage.eNS_URI, StatemachinesPackage.eINSTANCE);
packageRegistry.put(UsecasesPackage.eNS_URI, UsecasesPackage.eINSTANCE);
}
Finally, to apply the profile I was trying this code:
try {
registerSysmlProfile();
URI sysml_URI = URI.createURI(SysmlResource.SYSML_PROFILE_URI);
Profile sysml = (Profile) PackageUtil.loadPackage(URI.createURI(SysmlResource.SYSML_PROFILE_URI), model.eResource().getResourceSet());
if (sysml != null) {
log("sysml is not null", null);
PackageUtil.applyProfile(((org.eclipse.uml2.uml.Package) model), sysml, true);
}
} catch (Exception e) {
e.printStackTrace();
log("Can't apply SysML profile.", e);
}
One thing I notice is that probably I should register the profiles in the registry of the resources of my model, rather than in the general registry. I tried that also, but got the same error about 'SysmlPackage.eINSTANCE'.
Could please someone tell me how to load SysML profile and how to load a custom profile?
Thanks in advance,
Victor
|
|
|
Re: Using SysML and custom profile programmatically [message #1701551 is a reply to message #1701530] |
Tue, 14 July 2015 06:34 |
Thanh Liem Phan Messages: 28 Registered: January 2015 |
Junior Member |
|
|
Hi Victor,
I suppose you want to create SysML model in standalone mode, which means you have to register manually the SysML package. So you have to position exactly the path of your SysML package (SYSML_JAR_PATH in the source code below).
Here is my source code to register SysML package. Hope this can help you.
public static String SYSML_JAR_PATH =
"jar:file:/C:/Users/tp242786/MyPostdoc-INES/TOOLS/eclipse_mars/plugins/org.eclipse.papyrus.sysml_1.1.0.201504170509.jar!/";
public void loadProfiles() {
umlStandardProfile = (Profile) load(URI.createURI(UMLResource.STANDARD_PROFILE_URI));
registerSysmlProfile();
sysmlProfile = (Profile) load(URI.createURI(SysmlResource.SYSML_PROFILE_URI));
sysmlBlocksProfile = (Profile) sysmlProfile.getNestedPackage("Blocks");
sysmlPortAndFlowsProfile = (Profile) sysmlProfile.getNestedPackage("PortAndFlows");
sysmlModelElementsProfile = (Profile) sysmlProfile.getNestedPackage("ModelElements");
}
public void registerSysmlProfile() {
initSysmlPackageRegistry(RESOURCE_SET.getPackageRegistry());
initSysmlURIConverterURIMap(RESOURCE_SET.getURIConverter().getURIMap());
initSysmlURI2ProfileMap();
}
private static void initSysmlURIConverterURIMap( final Map<URI, URI> uriMap) {
URI sysmlBaseUri=URI.createURI(SYSML_JAR_PATH);
URI sysmlProfileUri = URI.createURI(sysmlBaseUri+"model/SysML.profile.uml");
uriMap.put(URI.createURI(SysmlResource.SYSML_PROFILE_URI), sysmlProfileUri);
// platform:/plugin/org.eclipse.papyrus.sysml/libraries
uriMap.put(URI.createURI(SysmlResource.LIBRARIES_PATHMAP), sysmlBaseUri.appendSegment("libraries").appendSegment(""));
// platform:/plugin/org.eclipse.papyrus.sysml/model
uriMap.put(URI.createURI(SysmlResource.PROFILES_PATHMAP), sysmlBaseUri.appendSegment("model").appendSegment(""));
}
private static void initSysmlURI2ProfileMap() {
UMLPlugin.getEPackageNsURIToProfileLocationMap().put(SysmlPackage.eNS_URI,
URI.createURI(SysmlResource.SYSML_PROFILE_URI+"#_TZ_nULU5EduiKqCzJMWbGw"));
UMLPlugin.getEPackageNsURIToProfileLocationMap().put(ModelelementsPackage.eNS_URI,
URI.createURI(SysmlResource.SYSML_PROFILE_URI+"#_Gx8MgLX7EduFmqQsrNB9lw"));
UMLPlugin.getEPackageNsURIToProfileLocationMap().put(BlocksPackage.eNS_URI,
URI.createURI(SysmlResource.SYSML_PROFILE_URI+"#_fSw28LX7EduFmqQsrNB9lw"));
UMLPlugin.getEPackageNsURIToProfileLocationMap().put(PortandflowsPackage.eNS_URI,
URI.createURI(SysmlResource.SYSML_PROFILE_URI+"#_rpx28LX7EduFmqQsrNB9lw"));
UMLPlugin.getEPackageNsURIToProfileLocationMap().put(ConstraintsPackage.eNS_URI,
URI.createURI(SysmlResource.SYSML_PROFILE_URI+"#_5WYJ0LX7EduFmqQsrNB9lw"));
UMLPlugin.getEPackageNsURIToProfileLocationMap().put(ActivitiesPackage.eNS_URI,
URI.createURI(SysmlResource.SYSML_PROFILE_URI+"#_C2zXMLX8EduFmqQsrNB9lw"));
UMLPlugin.getEPackageNsURIToProfileLocationMap().put(AllocationsPackage.eNS_URI,
URI.createURI(SysmlResource.SYSML_PROFILE_URI+"#_NxdG4LX8EduFmqQsrNB9lw"));
UMLPlugin.getEPackageNsURIToProfileLocationMap().put(RequirementsPackage.eNS_URI,
URI.createURI(SysmlResource.SYSML_PROFILE_URI+"#_OOJC4LX8EduFmqQsrNB9lw"));
UMLPlugin.getEPackageNsURIToProfileLocationMap().put(InteractionsPackage.eNS_URI,
URI.createURI(SysmlResource.SYSML_PROFILE_URI+"#_meOioLX8EduFmqQsrNB9lw"));
UMLPlugin.getEPackageNsURIToProfileLocationMap().put(StatemachinesPackage.eNS_URI,
URI.createURI(SysmlResource.SYSML_PROFILE_URI+"#_nAF5kLX8EduFmqQsrNB9lw"));
UMLPlugin.getEPackageNsURIToProfileLocationMap().put(UsecasesPackage.eNS_URI,
URI.createURI(SysmlResource.SYSML_PROFILE_URI+"#_neZmMLX8EduFmqQsrNB9lw"));
}
/*
* Sysml Ecore mapping
*/
private static void initSysmlPackageRegistry(final EPackage.Registry packageRegistry) {
packageRegistry.put(SysmlPackage.eNS_URI, SysmlPackage.eINSTANCE);
packageRegistry.put(BlocksPackage.eNS_URI, BlocksPackage.eINSTANCE);
packageRegistry.put(RequirementsPackage.eNS_URI, RequirementsPackage.eINSTANCE);
packageRegistry.put(ActivitiesPackage.eNS_URI, ActivitiesPackage.eINSTANCE);
packageRegistry.put(AllocationsPackage.eNS_URI, AllocationsPackage.eINSTANCE);
packageRegistry.put(ConstraintsPackage.eNS_URI, ConstraintsPackage.eINSTANCE);
packageRegistry.put(InteractionsPackage.eNS_URI, InteractionsPackage.eINSTANCE);
packageRegistry.put(ModelelementsPackage.eNS_URI, ModelelementsPackage.eINSTANCE);
packageRegistry.put(PortandflowsPackage.eNS_URI, PortandflowsPackage.eINSTANCE);
packageRegistry.put(StatemachinesPackage.eNS_URI, StatemachinesPackage.eINSTANCE);
packageRegistry.put(UsecasesPackage.eNS_URI, UsecasesPackage.eINSTANCE);
}
Regards.
|
|
|
Re: Using SysML and custom profile programmatically [message #1701613 is a reply to message #1701530] |
Tue, 14 July 2015 12:58 |
|
Hi, Victor,
If this code is in an Eclipse plug-in, as you describe, then you don't
need any of the stand-alone setup configuration code. All of the
registering profiles, initializing URI converter maps, and package
registry stuff is unnecessary.
What error do you get that is "related to SysmlPackage.eINSTANCE"? If
it's a compilation error complaining that the type is not resolved,
then it's simply a matter of adding the org.eclipse.papyrus.sysml
bundle (or sysml14, depending) to your dependencies in your plug-in's
MANIFEST.MF.
HTH,
Christian
On 2015-07-13 21:28:27 +0000, Víctor López said:
> Hi all,
> I would like to create a Papyrus model programatically in a a plugin to
> generate some UML elements and the apply some profiles to them.
>
> I already know how to create the model:
>
> ModelSet modelSet = new ModelSet();
> URI diURI = URI.createPlatformResourceURI("MyProject/MyModel.di", true);
>
> ModelsReader reader = new ModelsReader(); //Standard ModelsReader for
> Di + UML + Notation
> reader.readModel(modelSet);
>
> try {
> modelSet.loadModels(diURI);
> } catch (ModelMultiException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> modelSet.createModels(diURI); //Use an EMF URI instead of an Eclipse IFile
> ServicesRegistry registry;
> try {
> registry = new
> ExtensionServicesRegistry(org.eclipse.papyrus.infra.core.Activator.PLUGIN_ID);
>
> registry.add(ModelSet.class, Integer.MAX_VALUE, modelSet);
> registry.startRegistry();
> } catch (ServiceException ex) {
> log("Error in registry.", ex);
> }
>
>
> IModelCreationCommand creationCommand = new CreateUMLModelCommand();
> creationCommand.createModel(modelSet);
>
> try {
> modelSet.save(new NullProgressMonitor());
> } catch (IOException e) {
> e.printStackTrace();
> }
>
> This is working and I can create classes, etc.
>
> My problem is that I don't how to load the SysML and the custom profile
> to apply them to the model I created.
>
> After searching the forums I tried this code to register SysML profile,
> but I always get an error related to 'SysmlPackage.eINSTANCE'. It looks
> like it is not available. So I guess something must be done before
> registering the profile.
>
> public void registerSysmlProfile() {
> initPackageRegistry(EPackage.Registry.INSTANCE);
> initURIConverterURIMap(URIConverter.URI_MAP);
>
> UMLPlugin.getEPackageNsURIToProfileLocationMap().put(SysmlPackage.eNS_URI,
> URI.createURI(SysmlResource.SYSML_PROFILE_URI));
> }
>
> private void initURIConverterURIMap(final Map<URI, URI> uriMap) {
> final URI baseUri = URI.createURI(SysmlResource.SYSML_PROFILE_URI);
> uriMap.put(URI.createURI(SysmlResource.PROFILES_PATHMAP),
> baseUri.appendSegment("model").appendSegment(""));
> uriMap.put(URI.createURI(SysmlResource.LIBRARIES_PATHMAP),
> baseUri.appendSegment("libraries").appendSegment(""));
> }
>
> private void initPackageRegistry(final EPackage.Registry packageRegistry) {
> packageRegistry.put(org.eclipse.papyrus.sysml.SysmlPackage.eNS_URI,
> SysmlPackage.eINSTANCE);
> packageRegistry.put(BlocksPackage.eNS_URI, BlocksPackage.eINSTANCE);
> packageRegistry.put(RequirementsPackage.eNS_URI,
> RequirementsPackage.eINSTANCE);
> packageRegistry.put(ActivitiesPackage.eNS_URI, ActivitiesPackage.eINSTANCE);
> packageRegistry.put(AllocationsPackage.eNS_URI,
> AllocationsPackage.eINSTANCE);
> packageRegistry.put(ConstraintsPackage.eNS_URI,
> ConstraintsPackage.eINSTANCE);
> packageRegistry.put(InteractionsPackage.eNS_URI,
> InteractionsPackage.eINSTANCE);
> packageRegistry.put(ModelelementsPackage.eNS_URI,
> ModelelementsPackage.eINSTANCE);
> packageRegistry.put(PortandflowsPackage.eNS_URI,
> PortandflowsPackage.eINSTANCE);
> packageRegistry.put(StatemachinesPackage.eNS_URI,
> StatemachinesPackage.eINSTANCE);
> packageRegistry.put(UsecasesPackage.eNS_URI, UsecasesPackage.eINSTANCE);
> }
>
> Finally, to apply the profile I was trying this code:
>
> try {
> registerSysmlProfile();
> URI sysml_URI = URI.createURI(SysmlResource.SYSML_PROFILE_URI);
>
> Profile sysml = (Profile)
> PackageUtil.loadPackage(URI.createURI(SysmlResource.SYSML_PROFILE_URI),
> model.eResource().getResourceSet());
> if (sysml != null) {
> log("sysml is not null", null);
> PackageUtil.applyProfile(((org.eclipse.uml2.uml.Package) model),
> sysml, true);
> }
> } catch (Exception e) {
> e.printStackTrace();
> log("Can't apply SysML profile.", e);
> }
>
> One thing I notice is that probably I should register the profiles in
> the registry of the resources of my model, rather than in the general
> registry. I tried that also, but got the same error about
> 'SysmlPackage.eINSTANCE'.
>
> Could please someone tell me how to load SysML profile and how to load
> a custom profile?
>
> Thanks in advance,
>
> Victor
|
|
| |
Re: Using SysML and custom profile programmatically [message #1701674 is a reply to message #1701661] |
Tue, 14 July 2015 20:15 |
|
Hi, Victor,
Create a new empty SysML model in Papyrus, open the UML resource in a
text editor, and have a look at the HREF of the <appliedProfile>
element in the profile application. That will tell you the URI of the
SysML profile that you need to load into the resource set in your
plug-in.
HTH,
Christian
On 2015-07-14 18:30:09 +0000, Víctor López said:
> Thanks to both of you.
>
> @Christian, it is a plugin I call by using a popupmenu action in a
> Papyrus project. Do you mean I don't need any of the stuff I do when I
> call " registerSysmlProfile()" function?
>
> If it's not needed, how do I load SysML profile to apply it?
> Thanks a lot,
>
> Victor
|
|
|
Goto Forum:
Current Time: Thu Apr 25 06:45:56 GMT 2024
Powered by FUDForum. Page generated in 0.03933 seconds
|