1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.editor;
\r
14 import gnu.trove.TIntIntHashMap;
\r
16 import java.util.Collection;
\r
17 import java.util.HashMap;
\r
19 import org.simantics.db.Resource;
\r
20 import org.simantics.db.WriteGraph;
\r
21 import org.simantics.db.common.request.ObjectsWithType;
\r
22 import org.simantics.db.common.utils.NameUtils;
\r
23 import org.simantics.db.common.utils.URIStringUtils;
\r
24 import org.simantics.db.exception.DatabaseException;
\r
25 import org.simantics.layer0.Layer0;
\r
26 import org.simantics.layer0.utils.direct.GraphUtils;
\r
27 import org.simantics.mapping.constraint.instructions.TypedBracketInstruction.CreationInstruction;
\r
28 import org.simantics.modeling.services.ComponentNamingUtil;
\r
29 import org.simantics.modeling.services.NamingException;
\r
30 import org.simantics.operation.Layer0X;
\r
31 import org.simantics.project.IProject;
\r
32 import org.simantics.sysdyn.SysdynResource;
\r
33 import org.simantics.sysdyn.ui.elements.ShadowVariableReferenceDialogRunnable;
\r
34 import org.simantics.utils.threads.SWTThread;
\r
36 public class SysdynCreationInstruction extends CreationInstruction {
\r
39 Resource configurationRoot;
\r
43 public SysdynCreationInstruction(IProject project, Resource configurationRoot, int variableId, int componentType,
\r
44 int configuration) {
\r
46 this.project = project;
\r
47 this.configurationRoot = configurationRoot;
\r
48 lComponentType = componentType;
\r
49 lConfiguration = configuration;
\r
53 public Resource create(WriteGraph g, Object[] bindings) throws DatabaseException {
\r
54 Resource componentType = (Resource) bindings[lComponentType];
\r
55 Resource configuration = (Resource) bindings[lConfiguration];
\r
57 Layer0 l0 = Layer0.getInstance(g);
\r
58 Layer0X L0X = Layer0X.getInstance(g);
\r
63 String proposition = URIStringUtils.escape(ComponentNamingUtil.findFreshInstanceName(g, project, configuration, configuration, componentType));
\r
64 Resource result = GraphUtils.create(g,
\r
65 l0.HasName, proposition
\r
67 g.claim(result, L0X.Represents, result);
\r
71 // Async reference dialog for shadow variables
\r
72 SysdynResource SR = SysdynResource.getInstance(g);
\r
73 if(SR.Shadow.equals(componentType)) {
\r
74 Layer0 L0 = Layer0.getInstance(g);
\r
75 Collection<Resource> referrableVariables = g.syncRequest(new ObjectsWithType(configuration, L0.ConsistsOf, SR.IndependentVariable));
\r
76 referrableVariables.addAll( g.syncRequest(new ObjectsWithType(configuration, L0.ConsistsOf, SR.Input)));
\r
77 HashMap<String, Resource> names = new HashMap<String, Resource>();
\r
78 for(Resource var : referrableVariables) {
\r
79 String name = NameUtils.getSafeName(g, var);
\r
80 names.put(name, var);
\r
82 SWTThread.getThreadAccess().asyncExec(new ShadowVariableReferenceDialogRunnable(result, names));
\r
86 } catch (NamingException e1) {
\r
87 throw new DatabaseException(e1);
\r
92 public void mapVariables(TIntIntHashMap map) {
\r
93 super.mapVariables(map);
\r
94 lComponentType = map.get(lComponentType);
\r
95 lConfiguration = map.get(lConfiguration);
\r