2dda48dff77447087d4331e40ba173e5df7c57d3
[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.layer0.Layer0;
23 import org.simantics.layer0.utils.direct.GraphUtils;
24 import org.simantics.mapping.constraint.instructions.TypedBracketInstruction.CreationInstruction;
25 import org.simantics.modeling.services.ComponentNamingUtil;
26 import org.simantics.modeling.services.NamingException;
27 import org.simantics.project.IProject;
28
29 public class NamingCreationInstruction extends CreationInstruction {
30
31     IProject project;
32     int configurationRoot;
33     int lComponentType;
34     int lConfiguration;
35
36     public NamingCreationInstruction(IProject project, int configurationRoot, int variableId, int componentType,
37             int configuration) {
38         super(variableId);
39         this.project = project;
40         this.configurationRoot = configurationRoot;
41         lComponentType = componentType;
42         lConfiguration = configuration;
43     }
44
45     @Override
46     public Resource create(WriteGraph g, Object[] bindings) throws DatabaseException {
47         Resource componentType = (Resource) bindings[lComponentType];
48         Resource configuration = (Resource) bindings[lConfiguration];
49
50         try {
51                 CommonDBUtils.selectClusterSet(g, configuration);
52             String proposition = ComponentNamingUtil.findFreshInstanceName(g, project, (Resource)bindings[configurationRoot], configuration, componentType);
53                 Layer0 b = Layer0.getInstance(g);
54             Resource result = GraphUtils.create(g,
55                     b.HasName, proposition
56             );
57             g.addLiteral(result, b.identifier, b.identifier_Inverse, b.GUID, GUID.random(), GUID.BINDING);
58             return result;
59         } catch (NamingException e1) {
60             throw new DatabaseException(e1);
61         }
62     }
63
64     @Override
65     public void mapVariables(TIntIntHashMap map) {
66         super.mapVariables(map);
67         lComponentType = map.get(lComponentType);
68         lConfiguration = map.get(lConfiguration);
69         configurationRoot = map.get(configurationRoot);
70     }
71     
72     @Override
73     public void collectVariables(TIntHashSet reads, TIntHashSet writes) {
74         reads.add(lComponentType);
75         reads.add(lConfiguration);
76         reads.add(configurationRoot);
77     }
78
79 }