Layer0Utils.addL0Identifier to prevent possible differentiation of code
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / mapping / NamingCreationInstruction.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.mapping;
13
14 import gnu.trove.map.hash.TIntIntHashMap;
15 import gnu.trove.set.hash.TIntHashSet;
16
17 import org.simantics.datatypes.literal.GUID;
18 import org.simantics.db.Resource;
19 import org.simantics.db.WriteGraph;
20 import org.simantics.db.common.utils.CommonDBUtils;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.layer0.util.Layer0Utils;
23 import org.simantics.layer0.Layer0;
24 import org.simantics.layer0.utils.direct.GraphUtils;
25 import org.simantics.mapping.constraint.instructions.TypedBracketInstruction.CreationInstruction;
26 import org.simantics.modeling.services.ComponentNamingUtil;
27 import org.simantics.modeling.services.NamingException;
28 import org.simantics.project.IProject;
29
30 public class NamingCreationInstruction extends CreationInstruction {
31
32     IProject project;
33     int configurationRoot;
34     int lComponentType;
35     int lConfiguration;
36
37     public NamingCreationInstruction(IProject project, int configurationRoot, int variableId, int componentType,
38             int configuration) {
39         super(variableId);
40         this.project = project;
41         this.configurationRoot = configurationRoot;
42         lComponentType = componentType;
43         lConfiguration = configuration;
44     }
45
46     @Override
47     public Resource create(WriteGraph g, Object[] bindings) throws DatabaseException {
48         Resource componentType = (Resource) bindings[lComponentType];
49         Resource configuration = (Resource) bindings[lConfiguration];
50
51         try {
52                 CommonDBUtils.selectClusterSet(g, configuration);
53             String proposition = ComponentNamingUtil.findFreshInstanceName(g, project, (Resource)bindings[configurationRoot], configuration, componentType);
54                 Layer0 b = Layer0.getInstance(g);
55             Resource result = GraphUtils.create(g,
56                     b.HasName, proposition
57             );
58             Layer0Utils.addL0Identifier(g, result);
59             return result;
60         } catch (NamingException e1) {
61             throw new DatabaseException(e1);
62         }
63     }
64
65     @Override
66     public void mapVariables(TIntIntHashMap map) {
67         super.mapVariables(map);
68         lComponentType = map.get(lComponentType);
69         lConfiguration = map.get(lConfiguration);
70         configurationRoot = map.get(configurationRoot);
71     }
72     
73     @Override
74     public void collectVariables(TIntHashSet reads, TIntHashSet writes) {
75         reads.add(lComponentType);
76         reads.add(lConfiguration);
77         reads.add(configurationRoot);
78     }
79
80 }