]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/ComponentUtils.java
Disable SCL Issue provider by default
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / ComponentUtils.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;
13
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;
19
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;
28
29 /**
30  * @author Tuukka Lehtonen
31  */
32 public final class ComponentUtils {
33
34     public static boolean componentHasDiagram(ReadGraph g, Resource component) throws DatabaseException {
35         return !getComponentDiagrams(g, component).isEmpty();
36     }
37
38     public static boolean compositeHasDiagram(ReadGraph g, Resource composite) throws DatabaseException {
39         return !getCompositeDiagrams(g, composite).isEmpty();
40     }
41
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();
49     }
50
51     public static Collection<Resource> getCompositeDiagrams(ReadGraph g, Resource composite) throws DatabaseException {
52         return g.getObjects(composite, ModelingResources.getInstance(g).CompositeToDiagram);
53     }
54
55     public static Resource getCompositeDiagram(ReadGraph g, Resource composite) throws DatabaseException {
56         return g.getSingleObject(composite, ModelingResources.getInstance(g).CompositeToDiagram);
57     }
58
59     public static Resource getPossibleCompositeDiagram(ReadGraph g, Resource composite) throws DatabaseException {
60         return g.getPossibleObject(composite, ModelingResources.getInstance(g).CompositeToDiagram);
61     }
62
63     public static Resource getPossibleDiagramComposite(ReadGraph g, Resource diagram) throws DatabaseException {
64         return g.getPossibleObject(diagram, ModelingResources.getInstance(g).DiagramToComposite);
65     }
66
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>();
70
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));
76             }
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));
82             }
83         }
84
85         return inputs;
86     }
87
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;
92 //
93 //        List<Resource> result = new ArrayList<Resource>();
94 //
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;
108 //        } else {
109 //            return ResourceArray.EMPTY;
110 //        }
111 //
112 //        while (component != null) {
113 ////            System.out.println("ADDING COMPONENT: " + NameUtils.getSafeName(g, component));
114 //            result.add(component);
115 //
116 //            Resource composite = g.getPossibleObject(component, b.PartOf);
117 //            if (composite == null)
118 //                break;
119 ////            System.out.println("  PART OF: " + NameUtils.getSafeName(g, composite));
120 //
121 //            Resource defines = g.getPossibleObject(composite, sr.Defines);
122 //            if (defines == null)
123 //                break;
124 ////            System.out.println("  WHICH DEFINES: " + NameUtils.getSafeName(g, defines));
125 //
126 //            component = g.getPossibleObject(defines, b.HasSingleInstance);
127 //        }
128 ////        System.out.println("FOUND PATH:");
129 ////        for (Resource r : result)
130 ////            System.out.println("    " + NameUtils.getSafeName(g, r));
131 //        return new ResourceArray(result);
132 //    }
133
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;
138
139         List<Resource> result = new ArrayList<Resource>();
140
141 //        System.out.println("checking composite or component: " + NameUtils.getSafeName(g, compositeOrComponent));
142
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;
150         } else {
151             return ResourceArray.EMPTY;
152         }
153 //        System.out.println("start path browsing from: " + NameUtils.getSafeName(g, component));
154
155         while (component != null) {
156 //            System.out.println("ADDING COMPONENT: " + NameUtils.getSafeName(g, component));
157             result.add(component);
158
159             Resource composite = g.getPossibleObject(component, b.PartOf);
160             if (composite == null)
161                 break;
162             if (!g.isInstanceOf(composite, sr.Composite)) {
163 //                System.out.println("  PART OF NON-COMPOSITE: " + NameUtils.getSafeName(g, composite));
164                 break;
165             }
166 //            System.out.println("  PART OF COMPOSITE: " + NameUtils.getSafeName(g, composite));
167             component = composite;
168         }
169 //        System.out.println("FOUND PATH:");
170 //        for (Resource r : result)
171 //            System.out.println("    " + NameUtils.getSafeName(g, r));
172         return new ResourceArray(result);
173     }
174
175     public static Resource getCompositeConfigurationRoot(ReadGraph g, Resource composite) throws DatabaseException {
176         StructuralResource2 sr = StructuralResource2.getInstance(g);
177
178         if (!g.isInstanceOf(composite, sr.Composite))
179             throw new IllegalArgumentException("argument " + NameUtils.getSafeName(g, composite) + " is not a composite component");
180
181         ResourceArray path = getStructuralPath(g, composite);
182         if (path.isEmpty()) {
183             return composite;
184         }
185
186         Resource topmostComponent = path.resources[path.resources.length - 1];
187         //Resource parent = g.getPossibleObject(topmostComponent, g.getBuiltins().PartOf);
188         //assert parent != null;
189         //return parent;
190         return topmostComponent;
191     }
192
193     public static Resource getComponentConfigurationRoot(ReadGraph g, Resource component) throws DatabaseException {
194         StructuralResource2 sr = StructuralResource2.getInstance(g);
195
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");
200
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;
205 //        return parent;
206         return topmostComponent;
207     }
208
209     public static Resource tryGetComponentContainer(ReadGraph g, Resource component) throws DatabaseException {
210         Layer0 l0 = Layer0.getInstance(g);
211         StructuralResource2 sr = StructuralResource2.getInstance(g);
212
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");
217
218         Resource container = g.getPossibleObject(component, l0.PartOf);
219         if (container == null)
220             return null;
221         return container;
222     }
223
224     public static Resource tryGetComponentConfigurationRoot(ReadGraph g, Resource component) throws DatabaseException {
225         StructuralResource2 sr = StructuralResource2.getInstance(g);
226
227         if (g.isInstanceOf(component, sr.Composite))
228             return null;
229         if (!g.isInstanceOf(component, sr.Component))
230             return null;
231
232         ResourceArray path = getStructuralPath(g, component);
233         Resource topmostComponent = path.isEmpty() ? component : path.resources[path.resources.length - 1];
234
235 //        Resource parent = g.getPossibleObject(topmostComponent, g.getBuiltins().PartOf);
236 //        return parent;
237         return topmostComponent;
238     }
239
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)
248                 return null;
249         }
250         return compositePath;
251     }
252
253 }