Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » UML2 » Registering and loading a profile programatically (standalone)
Registering and loading a profile programatically (standalone) [message #1573733] Mon, 19 January 2015 22:47
Ernesto Posse is currently offline Ernesto PosseFriend
Messages: 438
Registered: March 2011
Senior Member
Hi. I'm trying to load a model with a profile in a standalone application.

I've followed the tutorial at http://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles#Loading_Models

but that still doesn't clarify how to register a third-party profile.

Some related threads suggest looking at UMLResourcesUtil to see how standard profiles are registered. Following the pattern there, I manage to register the profile (see attached file), and at first it seems to work, but as soon as I try to apply it, I get the following exception:

applying profile: org.eclipse.uml2.uml.internal.impl.ProfileImpl@3e5c7b6b (name: UMLRealTime, visibility: <unset>) (URI: http://www.eclipse.org/papyrus/umlrt)
java.lang.NullPointerException
	at org.eclipse.emf.ecore.resource.impl.ResourceImpl.getEObject(ResourceImpl.java:737)
	at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getEObject(ResourceSetImpl.java:223)
	at org.eclipse.uml2.uml.util.UMLUtil.getProfile(UMLUtil.java:11298)
	at org.eclipse.uml2.uml.util.UMLUtil.getNamedElement(UMLUtil.java:11494)
	at org.eclipse.uml2.uml.util.UMLUtil.getNamedElement(UMLUtil.java:11444)
	at org.eclipse.uml2.uml.util.UMLUtil.getStereotype(UMLUtil.java:11506)
	at org.eclipse.uml2.uml.util.UMLUtil.getStereotype(UMLUtil.java:11533)
	at org.eclipse.uml2.uml.internal.operations.ElementOperations.getStereotypeApplications(ElementOperations.java:213)
	at org.eclipse.uml2.uml.internal.impl.ElementImpl.getStereotypeApplications(ElementImpl.java:186)
	at org.eclipse.uml2.uml.internal.operations.PackageOperations.applyProfile(PackageOperations.java:672)
	at org.eclipse.uml2.uml.internal.impl.PackageImpl.applyProfile(PackageImpl.java:1083)
	at eposse.experiments.profiletest.ProfileTest.applyProfiles(ProfileTest.java:75)
	at eposse.experiments.profiletest.ProfileTest.main(ProfileTest.java:34)


If I comment out the line applying the profiles, I still get the same exception when invoking "getAppliedStereotypes".

What's the problem what am I doing wrong?

Thanks.

public class ProfileTest {

    static String UMLRT_PROFILE_ID = "org.eclipse.papyrus.umlrt";
    static String UMLRT_NS_URI = UMLRealTimePackage.eNS_URI; //"http://www.eclipse.org/papyrus/umlrt"
    static UMLRealTimePackage UMLRT_PKG = UMLRealTimePackage.eINSTANCE;
    static String UMLRT_PATHMAP = "pathmap://UML_RT_PROFILE/uml-rt.profile.uml";
    static ResourceSet resourceSet;
    
    public static void main(String[] args) throws IOException {
        String modelName = "EmptyCapsule";
        String modelProjectName = "EmptyCapsule";
        String projectContainer = "../../../git4/umlrt.general";
        init();
        Model model = loadModel(projectContainer, modelProjectName, modelName);
        applyProfiles(model);
        for (Element element: model.allOwnedElements()) {
            System.out.println("element = " + element);
            System.out.println("  applicable stereotypes = " + element.getApplicableStereotypes());
            System.out.println("  applied stereotypes = " + element.getAppliedStereotypes());
        }
    }
    
    static void init() {
        // Create the resource set
        resourceSet = new ResourceSetImpl();
        // Initialize global registry
        UMLResourcesUtil.init(resourceSet);
        // Register profile package in the resourceSet package registry
        resourceSet.getPackageRegistry().put(UMLRT_NS_URI, UMLRT_PKG);
        // Register the profile's NS_URI to its pathmap location
        UMLPlugin.getEPackageNsURIToProfileLocationMap().put(UMLRT_NS_URI, URI.createURI(UMLRT_PATHMAP));
        // Find the plugin that contains the profile
        PluginFinder pluginFinder = new PluginFinder(UMLRT_PROFILE_ID);
        pluginFinder.resolve();
        String umlrtProfileLocation = pluginFinder.get(UMLRT_PROFILE_ID) + "umlProfile/uml-rt.profile.uml";
        // Register the actual profile location with the URIConverter
        resourceSet.getURIConverter().getURIMap().put(URI.createURI(UMLRT_PATHMAP), URI.createURI(umlrtProfileLocation));
    }

    static Model loadModel(String projectContainer, String projectName, String modelName) throws IOException {
        File projectContainerPath = new File(projectContainer);
        String basePath = projectContainerPath.getCanonicalPath();
        URI baseURI = URI.createURI(basePath);
        URI fullURI = baseURI
                .appendSegment(projectName)
                .appendSegment(modelName)
                .appendFileExtension("uml");
        Resource resource = resourceSet.getResource(fullURI, true);
        return (Model) EcoreUtil.getObjectByType(resource.getContents(), UMLPackage.Literals.PACKAGE);
    }

    static void applyProfiles(Model model) {
        for (ProfileApplication profileApplication : model.getProfileApplications()) {
            Profile profile = profileApplication.getAppliedProfile();
            System.out.println("applying profile: " + profile);
            model.applyProfile(profile);
        }
    }
}

[Updated on: Mon, 19 January 2015 22:48]

Report message to a moderator

Previous Topic:Stereotype owning two specialization
Next Topic:Registering and loading a profile programatically (standalone)
Goto Forum:
  


Current Time: Thu Apr 25 17:01:09 GMT 2024

Powered by FUDForum. Page generated in 0.02711 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top