]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/typicals/ReadTypicalInfo.java
Fixed ComponentTypeCommands.setUnit to support unit == null
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / typicals / ReadTypicalInfo.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management in
3  * 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.typicals;
13
14 import gnu.trove.map.hash.THashMap;
15
16 import java.util.TreeSet;
17
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.common.request.ObjectsWithType;
21 import org.simantics.db.common.request.UnaryRead;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.diagram.stubs.DiagramResource;
24 import org.simantics.layer0.Layer0;
25 import org.simantics.modeling.ModelingResources;
26
27 /**
28  * Reads a TypicalInfo info structure for the specified typical diagram instance.
29  *  
30  * @author Tuukka Lehtonen
31  */
32 public class ReadTypicalInfo extends UnaryRead<Resource, TypicalInfoBean> {
33
34     public ReadTypicalInfo(Resource typicalInstance) {
35         super(typicalInstance);
36     }
37
38     @Override
39     public TypicalInfoBean perform(ReadGraph graph) throws DatabaseException {
40         Layer0 L0 = Layer0.getInstance(graph);
41         DiagramResource DIA = DiagramResource.getInstance(graph);
42         ModelingResources MOD = ModelingResources.getInstance(graph);
43
44         TypicalInfoBean info = new TypicalInfoBean();
45         info.instanceElements = new TreeSet<Resource>( graph.syncRequest( new ObjectsWithType(parameter, L0.ConsistsOf, DIA.Element) ) );
46         info.instanceToTemplate = new THashMap<Resource, Resource>( info.instanceElements.size() );
47         info.templateToInstance = new THashMap<Resource, Resource>( info.instanceElements.size() );
48         info.isTemplatized = new TreeSet<Resource>();
49         for (Resource instanceElement : info.instanceElements) {
50             Resource templateElement = graph.getPossibleObject(instanceElement, MOD.HasElementSource);
51             if (templateElement != null) {
52                 info.instanceToTemplate.put(instanceElement, templateElement);
53                 info.templateToInstance.put(templateElement, instanceElement);
54             }
55             if (graph.hasStatement(instanceElement, MOD.IsTemplatized))
56                 info.isTemplatized.add(instanceElement);
57         }
58         return info;
59     }
60
61 }