1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.LinkedList;
18 import java.util.List;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.common.ResourceArray;
23 import org.simantics.db.common.utils.NameUtils;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.layer0.Layer0;
26 import org.simantics.simulation.ontology.SimulationResource;
27 import org.simantics.structural.stubs.StructuralResource2;
30 * @author Tuukka Lehtonen
32 public final class ComponentUtils {
34 public static boolean componentHasDiagram(ReadGraph g, Resource component) throws DatabaseException {
35 return !getComponentDiagrams(g, component).isEmpty();
38 public static boolean compositeHasDiagram(ReadGraph g, Resource composite) throws DatabaseException {
39 return !getCompositeDiagrams(g, composite).isEmpty();
42 public static Collection<Resource> getComponentDiagrams(ReadGraph g, Resource component) throws DatabaseException {
43 StructuralResource2 sr = StructuralResource2.getInstance(g);
44 Resource componentType = g.getPossibleType(component, sr.Component);
45 Resource composite = componentType != null ? g.getPossibleObject(componentType, sr.IsDefinedBy) : null;
46 if (composite != null)
47 return getCompositeDiagrams(g, composite);
48 return Collections.emptyList();
51 public static Collection<Resource> getCompositeDiagrams(ReadGraph g, Resource composite) throws DatabaseException {
52 return g.getObjects(composite, ModelingResources.getInstance(g).CompositeToDiagram);
55 public static Resource getCompositeDiagram(ReadGraph g, Resource composite) throws DatabaseException {
56 return g.getSingleObject(composite, ModelingResources.getInstance(g).CompositeToDiagram);
59 public static Resource getPossibleCompositeDiagram(ReadGraph g, Resource composite) throws DatabaseException {
60 return g.getPossibleObject(composite, ModelingResources.getInstance(g).CompositeToDiagram);
63 public static Resource getPossibleDiagramComposite(ReadGraph g, Resource diagram) throws DatabaseException {
64 return g.getPossibleObject(diagram, ModelingResources.getInstance(g).DiagramToComposite);
67 public static Collection<ResourceArray> formInputs(ReadGraph g, Resource compositeOrComponent) throws DatabaseException {
68 ResourceArray path = getStructuralPath(g, compositeOrComponent);
69 Collection<ResourceArray> inputs = new ArrayList<ResourceArray>();
71 if (compositeHasDiagram(g, compositeOrComponent)) {
72 for (Resource dia : getCompositeDiagrams(g, compositeOrComponent)) {
73 //System.out.println(": " + NameUtils.getSafeName(g, compositeOrComponent));
74 //System.out.println(": " + NameUtils.getSafeName(g, dia));
75 inputs.add(path.prepended(dia));
77 } else if (componentHasDiagram(g, compositeOrComponent)) {
78 for (Resource dia : getComponentDiagrams(g, compositeOrComponent)) {
79 //System.out.println("* " + NameUtils.getSafeName(g, compositeOrComponent));
80 //System.out.println("* " + NameUtils.getSafeName(g, dia));
81 inputs.add(path.prepended(dia));
88 // public static ResourceArray getStructuralPath(ReadGraph g, Resource compositeOrComponent) throws DatabaseException {
89 // Builtins b = g.getBuiltins();
90 // StructuralResource2 sr = StructuralResource2.getInstance(g);
91 // Resource component = null;
93 // List<Resource> result = new ArrayList<Resource>();
95 // // Find component where to start the path browsing
96 // if (g.isInstanceOf(compositeOrComponent, sr.Composite)) {
97 //// System.out.println("got composite: " + NameUtils.getSafeName(g, compositeOrComponent));
98 // Resource defines = g.getPossibleObject(compositeOrComponent, sr.Defines);
99 // if (defines == null)
100 // return ResourceArray.EMPTY;
101 // component = g.getPossibleObject(defines, b.HasSingleInstance);
102 // if (component == null)
103 // return ResourceArray.EMPTY;
104 //// System.out.println("start path browsing from: " + NameUtils.getSafeName(g, component));
105 // } else if (g.isInstanceOf(compositeOrComponent, sr.Component)) {
106 //// System.out.println("got component: " + NameUtils.getSafeName(g, compositeOrComponent));
107 // component = compositeOrComponent;
109 // return ResourceArray.EMPTY;
112 // while (component != null) {
113 //// System.out.println("ADDING COMPONENT: " + NameUtils.getSafeName(g, component));
114 // result.add(component);
116 // Resource composite = g.getPossibleObject(component, b.PartOf);
117 // if (composite == null)
119 //// System.out.println(" PART OF: " + NameUtils.getSafeName(g, composite));
121 // Resource defines = g.getPossibleObject(composite, sr.Defines);
122 // if (defines == null)
124 //// System.out.println(" WHICH DEFINES: " + NameUtils.getSafeName(g, defines));
126 // component = g.getPossibleObject(defines, b.HasSingleInstance);
128 //// System.out.println("FOUND PATH:");
129 //// for (Resource r : result)
130 //// System.out.println(" " + NameUtils.getSafeName(g, r));
131 // return new ResourceArray(result);
134 public static ResourceArray getStructuralPath(ReadGraph g, Resource compositeOrComponent) throws DatabaseException {
135 Layer0 b = Layer0.getInstance(g);
136 StructuralResource2 sr = StructuralResource2.getInstance(g);
137 Resource component = null;
139 List<Resource> result = new ArrayList<Resource>();
141 // System.out.println("checking composite or component: " + NameUtils.getSafeName(g, compositeOrComponent));
143 // Find component where to start the path browsing
144 if (g.isInstanceOf(compositeOrComponent, sr.Composite)) {
145 // System.out.println("got composite: " + NameUtils.getSafeName(g, compositeOrComponent));
146 component = compositeOrComponent;
147 } else if (g.isInstanceOf(compositeOrComponent, sr.Component)) {
148 // System.out.println("got component: " + NameUtils.getSafeName(g, compositeOrComponent));
149 component = compositeOrComponent;
151 return ResourceArray.EMPTY;
153 // System.out.println("start path browsing from: " + NameUtils.getSafeName(g, component));
155 while (component != null) {
156 // System.out.println("ADDING COMPONENT: " + NameUtils.getSafeName(g, component));
157 result.add(component);
159 Resource composite = g.getPossibleObject(component, b.PartOf);
160 if (composite == null)
162 if (!g.isInstanceOf(composite, sr.Composite)) {
163 // System.out.println(" PART OF NON-COMPOSITE: " + NameUtils.getSafeName(g, composite));
166 // System.out.println(" PART OF COMPOSITE: " + NameUtils.getSafeName(g, composite));
167 component = composite;
169 // System.out.println("FOUND PATH:");
170 // for (Resource r : result)
171 // System.out.println(" " + NameUtils.getSafeName(g, r));
172 return new ResourceArray(result);
175 public static Resource getCompositeConfigurationRoot(ReadGraph g, Resource composite) throws DatabaseException {
176 StructuralResource2 sr = StructuralResource2.getInstance(g);
178 if (!g.isInstanceOf(composite, sr.Composite))
179 throw new IllegalArgumentException("argument " + NameUtils.getSafeName(g, composite) + " is not a composite component");
181 ResourceArray path = getStructuralPath(g, composite);
182 if (path.isEmpty()) {
186 Resource topmostComponent = path.resources[path.resources.length - 1];
187 //Resource parent = g.getPossibleObject(topmostComponent, g.getBuiltins().PartOf);
188 //assert parent != null;
190 return topmostComponent;
193 public static Resource getComponentConfigurationRoot(ReadGraph g, Resource component) throws DatabaseException {
194 StructuralResource2 sr = StructuralResource2.getInstance(g);
196 if (g.isInstanceOf(component, sr.Composite))
197 throw new IllegalArgumentException("argument " + NameUtils.getSafeName(g, component) + " is a composite component");
198 if (!g.isInstanceOf(component, sr.Component))
199 throw new IllegalArgumentException("argument " + NameUtils.getSafeName(g, component) + " is not a component");
201 ResourceArray path = getStructuralPath(g, component);
202 Resource topmostComponent = path.isEmpty() ? component : path.resources[path.resources.length - 1];
203 // Resource parent = g.getPossibleObject(topmostComponent, g.getBuiltins().PartOf);
204 // assert parent != null;
206 return topmostComponent;
209 public static Resource tryGetComponentContainer(ReadGraph g, Resource component) throws DatabaseException {
210 Layer0 l0 = Layer0.getInstance(g);
211 StructuralResource2 sr = StructuralResource2.getInstance(g);
213 if (g.isInstanceOf(component, sr.Composite))
214 throw new IllegalArgumentException("argument " + NameUtils.getSafeName(g, component) + " is a composite component");
215 if (!g.isInstanceOf(component, sr.Component))
216 throw new IllegalArgumentException("argument " + NameUtils.getSafeName(g, component) + " is not a component");
218 Resource container = g.getPossibleObject(component, l0.PartOf);
219 if (container == null)
224 public static Resource tryGetComponentConfigurationRoot(ReadGraph g, Resource component) throws DatabaseException {
225 StructuralResource2 sr = StructuralResource2.getInstance(g);
227 if (g.isInstanceOf(component, sr.Composite))
229 if (!g.isInstanceOf(component, sr.Component))
232 ResourceArray path = getStructuralPath(g, component);
233 Resource topmostComponent = path.isEmpty() ? component : path.resources[path.resources.length - 1];
235 // Resource parent = g.getPossibleObject(topmostComponent, g.getBuiltins().PartOf);
237 return topmostComponent;
240 public static List<Resource> getCompositePathToConfiguration(ReadGraph graph, Resource composite) throws DatabaseException {
241 Layer0 L0 = Layer0.getInstance(graph);
242 SimulationResource SIMU = SimulationResource.getInstance(graph);
243 LinkedList<Resource> compositePath = new LinkedList<Resource>();
244 while (!graph.hasStatement(composite, SIMU.IsConfigurationOf)) {
245 compositePath.addFirst(composite);
246 composite = graph.getPossibleObject(composite, L0.PartOf);
247 if (composite == null)
250 return compositePath;