Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 79d80a3e30bef831defd553765b60bf15b64c534 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*****************************************************************************
 * Copyright (c) 2013 CEA LIST.
 *
 *    
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *  IJI - Initial implementation
 * 
 *****************************************************************************/
package org.eclipse.papyrus.uml.alf;

import java.util.Collection;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.papyrus.uml.extensionpoints.library.IRegisteredLibrary;
import org.eclipse.papyrus.uml.extensionpoints.library.RegisteredLibrary;
import org.eclipse.uml2.uml.ElementImport;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.PackageableElement;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.UMLFactory;
import org.eclipse.uml2.uml.VisibilityKind;
import org.eclipse.uml2.uml.internal.impl.ModelImpl;
import org.eclipse.uml2.uml.resource.UMLResource;
import org.eclipse.uml2.uml.util.UMLUtil;

@SuppressWarnings("restriction")
public class Model extends ModelImpl {

	protected final String ALF_LIBRARY = "Alf Library";

	protected final String FUML_LIBRARY = "FoundationalModelLibrary";

	protected final String STANDARD_PROFILE = UMLResource.PROFILES_PATHMAP + "Standard.profile.uml";

	protected ResourceSet modelStorage;

	protected final String ALF_ROOT_PACKAGE = "Alf";

	public Model() {
		super();
		this.setName("EmptyModel");
		this.modelStorage = new ResourceSetImpl();
		this.loadLibraries();
		this.importLibraries();
		this.applyStandardProfile();
		this.registerTmpModel();
	}

	private void loadLibraries() {
		for (IRegisteredLibrary library : RegisteredLibrary.getRegisteredLibraries()) {
			if (library.getName() != null) {
				if (library.getName().equals(ALF_LIBRARY)) {
					this.modelStorage.getResource(library.getUri(), true);
				} else if (library.getName().equals(FUML_LIBRARY)) {
					this.modelStorage.getResource(library.getUri(), true);
				}
			}
		}
	}

	private void importLibraries() {
		/*
		 * Import ALF. Please note that ALF import fUML therefore it is not required to
		 * programmatically import fUML
		 */
		Collection<NamedElement> matchedElement = UMLUtil.findNamedElements(this.modelStorage, ALF_ROOT_PACKAGE);
		if (!matchedElement.isEmpty()) {
			NamedElement element = (NamedElement) matchedElement.toArray()[0];
			ElementImport elementImport = UMLFactory.eINSTANCE.createElementImport();
			elementImport.setImportedElement((PackageableElement) element);
			elementImport.setVisibility(VisibilityKind.PUBLIC_LITERAL);
			elementImport.setAlias(ALF_ROOT_PACKAGE);
			this.getElementImports().add(elementImport);
		}
	}

	private void applyStandardProfile() {
		Resource resource = this.modelStorage.getResource(URI.createURI(STANDARD_PROFILE), true);
		if (resource.getContents().get(0) instanceof Profile) {
			this.applyProfile((Profile) resource.getContents().get(0));
		}
	}

	private void registerTmpModel() {
		Resource r = this.modelStorage.createResource(URI.createURI("EMPTY_UML_CONTEXT_MODEL.uml"));
		r.getContents().add(this);
		this.modelStorage.getResources().add(r);
	}
	
	public void clean(){
		for(Resource resource : this.modelStorage.getResources()){
			resource.unload();
		}
	}
}

Back to the top