Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0fc3bd93a0b9cfc9f75b830a42276ca819d913c7 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*****************************************************************************
 * 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 v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Ansgar Radermacher  ansgar.radermacher@cea.fr  
 *
 *****************************************************************************/

package org.eclipse.papyrus.qompass.designer.core.deployment;

import java.util.Stack;

import org.eclipse.papyrus.qompass.designer.core.Messages;
import org.eclipse.papyrus.qompass.designer.core.extensions.ILangSupport;
import org.eclipse.papyrus.qompass.designer.core.transformations.Copy;
import org.eclipse.papyrus.qompass.designer.core.transformations.TransformationException;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.InstanceSpecification;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.PackageableElement;
import org.eclipse.uml2.uml.Slot;

/*
 * This file is part of Qompass GenTools
 * Copyright (C) 2008 CEA LIST (http://www-list.cea.fr/)

 * initial developer : Ansgar Radermacher, Christophe JOUVRAY from CEA LIST 
 */

public class Deploy {

	/**
	 * distribute an instance, its contained sub-instances and the referenced
	 * classifiers to a certain node
	 * 
	 * @param copy
	 * @param node
	 * @param nodeIndex
	 * @param numberOfNodes
	 * @param instance
	 * @throws TransformationException
	 */
	public static Deploy distributeToNode(Copy copy, ILangSupport langSupport, InstanceSpecification node,
		int nodeIndex, int numberOfNodes, InstanceSpecification instance)
		throws TransformationException
	{
		Deploy deploy = new Deploy();
		deploy.bootLoaderGen = new BootLoaderGen(copy, nodeIndex, numberOfNodes);
		deploy.node = node;

		// change to flat copy eventually later (not yet working)
		deploy.depInstance = new PartialCopy();
		
		deploy.depInstance.init(copy, deploy.bootLoaderGen, node);

		// set a copy listener in order to assure that indirectly added classes
		// are taken into account as well
		copy.preCopyListeners.add(new GatherConfigData(langSupport));
		// TODO: not nice at all (make non-static?)
		Stack<Slot> slotPath = new Stack<Slot>();
		deploy.distributeToNode(false, slotPath, instance);
/*
		for (Slot topLevelSlot : instance.getSlots()) {
			InstanceSpecification topLevelInstance = DepUtils.getInstance(topLevelSlot);
			if ((topLevelInstance != null) && AllocUtils.getAllNodes(topLevelInstance).contains(node)) {
				slotPath.push(topLevelSlot);
				deploy.distributeToNode(false, slotPath, topLevelInstance);
				slotPath.pop();
			}
		}
*/
		Package cdp = instance.getNearestPackage();
		// deploy singletons (difficult to embed singletons into main instance,
		// since there is no attribute for these)
		for(PackageableElement pe : cdp.getPackagedElements()) {
			if((pe instanceof InstanceSpecification) && (pe.getName().startsWith(DeployConstants.singletonPrefix) &&
					(!pe.getName().contains(".")))) { //$NON-NLS-1$
				deploy.distributeToNode(false, slotPath, (InstanceSpecification)pe);
			}
		}
		deploy.bootLoaderGen.addCreateConnections();
		deploy.bootLoaderGen.addInit();
		return deploy;
	}

	/**
	 * Distribute an instance specification to the node by this 
	 * @param allocAll
	 * @param slotPath
	 * @param instance
	 * @throws TransformationException
	 */
	public void distributeToNode(boolean allocAll, Stack<Slot> slotPath, InstanceSpecification instance)
		throws TransformationException {

		// once an instance is explicitly allocated on a partition (use of getNodes instead of getAllNodes)
		// all of its sub-instances are allocated on the node as well

		if(AllocUtils.getNodesOrThreads(instance).contains(node)) {
			allocAll = true;
		}

		// obtain implementation within source model
		Classifier smImplementation = DepUtils.getClassifier(instance);
		if(smImplementation == null) {
			throw new TransformationException(String.format(
				Messages.Deploy_0, instance.getName()));
		}

		// copy implementation into node specific model
		Classifier tmImplementation = depInstance.deployInstance(instance, slotPath);
		// Classifier tmImplementation = copy.getCopy(smImplementation);

		for(Slot slot : instance.getSlots()) {
			InstanceSpecification containedInstance = DepUtils.getInstance(slot);

			if(containedInstance != null) {
				if(allocAll || AllocUtils.getAllNodes(containedInstance).contains(node)) {
					// if(!containedInstance.getName().startsWith(singletonPrefix)) {
					slotPath.push(slot);
					distributeToNode(allocAll, slotPath, containedInstance);
					slotPath.pop();
				}
			} else {
				// slot contains configuration of primitive attribute (no
				// sub-instance, but primitive value)
				slotPath.push(slot);
				bootLoaderGen.instanceConfig(slotPath, instance);
				slotPath.pop();
			}
		}

		// ... and add the instance to the bootloader (this is done after
		// copying the slots to
		// ensure that the bootloader generator has the information about
		// contained parts & connectors
		// TODO: really necessary?
		if(tmImplementation instanceof Class) {
			bootLoaderGen.addInstance(slotPath, instance, (Class)tmImplementation, node);
		}
	}


	public Class getBootloader() {
		return bootLoaderGen.getUML();
	}

	protected BootLoaderGen bootLoaderGen;

	protected InstanceSpecification node;

	protected InstanceDeployer depInstance;
}

Back to the top