]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/utils/StructuralUtils.java
Layer0Utils.addL0Identifier to prevent possible differentiation of code
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / utils / StructuralUtils.java
1 package org.simantics.structural2.utils;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Set;
8
9 import org.simantics.databoard.Bindings;
10 import org.simantics.datatypes.literal.GUID;
11 import org.simantics.db.ReadGraph;
12 import org.simantics.db.RequestProcessor;
13 import org.simantics.db.Resource;
14 import org.simantics.db.Statement;
15 import org.simantics.db.WriteGraph;
16 import org.simantics.db.common.CommentMetadata;
17 import org.simantics.db.common.request.ObjectsWithType;
18 import org.simantics.db.common.request.PossibleTypedParent;
19 import org.simantics.db.common.utils.NameUtils;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.layer0.exception.MissingVariableException;
22 import org.simantics.db.layer0.request.InstantiateRequest;
23 import org.simantics.db.layer0.util.Layer0Utils;
24 import org.simantics.db.layer0.variable.Variable;
25 import org.simantics.layer0.Layer0;
26 import org.simantics.structural.stubs.StructuralResource2;
27 import org.simantics.structural2.internal.queries.ConnectedTo;
28 import org.simantics.structural2.internal.queries.RelatedConnections;
29 import org.simantics.structural2.internal.queries.RelatedConnectionsOfConnectionJoin;
30 import org.simantics.structural2.queries.Terminal;
31 import org.simantics.structural2.variables.Connection;
32 import org.simantics.utils.datastructures.Pair;
33
34 import gnu.trove.set.hash.THashSet;
35
36 /**
37  * A utility class for manipulating structural models.
38  * 
39  * @author Hannu Niemistö
40  */
41 public class StructuralUtils {
42     
43     public static Collection<Resource> getConnectionRelations(ReadGraph graph, Resource componentType) throws DatabaseException {
44         Layer0 L0 = Layer0.getInstance(graph);
45         StructuralResource2 STR = StructuralResource2.getInstance(graph);
46         return graph.syncRequest(new ObjectsWithType(componentType, L0.DomainOf, STR.ConnectionRelation));
47     }
48
49     public static Collection<Resource> getPropertyRelations(ReadGraph graph, Resource componentType) throws DatabaseException {
50         Layer0 L0 = Layer0.getInstance(graph);
51         ArrayList<Resource> result = new ArrayList<Resource>();
52         for(Resource relation : graph.getObjects(componentType, L0.DomainOf))
53             if(graph.isSubrelationOf(relation, L0.HasProperty))
54                 result.add(relation);
55         return result;
56     }
57     
58     public static boolean isDomainOfRelation(ReadGraph graph,
59             Resource componentType, Resource connectionRelation) throws DatabaseException {
60         Layer0 L0 = Layer0.getInstance(graph);
61         for(Resource domain : graph.getObjects(connectionRelation, L0.HasDomain))
62             if(graph.isInheritedFrom(componentType, domain))
63                 return true;
64         return false;
65     }
66     
67     public static void addConnectionPoint(WriteGraph g, Resource componentType,
68             Resource connectionPoint) throws DatabaseException {
69         Layer0 L0 = Layer0.getInstance(g);
70         g.claim(connectionPoint, L0.HasDomain, componentType);
71     }
72     
73     public static Resource createConnectionPoint(WriteGraph g, Resource componentType, Resource copy) throws DatabaseException {
74         String proposition = NameUtils.getSafeName(g, copy);
75         String newName = NameUtils.findFreshName(g, proposition, componentType);
76         return createConnectionPoint(g, componentType, copy, newName);
77     }
78     
79     public static Resource createConnectionPoint(WriteGraph g, Resource componentType, Resource copy, String name) throws DatabaseException {
80         return createConnectionPointP(g, componentType, copy, name).first;
81     }
82     
83     public static Pair<Resource,Resource> createConnectionPointP(WriteGraph g, Resource componentType, Resource copy, String name) throws DatabaseException {
84         
85         Layer0 L0 = Layer0.getInstance(g);
86         StructuralResource2 STR = StructuralResource2.getInstance(g);
87
88         // Create the connection point
89         Resource connectionPoint = g.newResource();
90         Resource connectionPointInv = g.newResource();
91
92         for (Resource superrelation : g.getObjects(copy, L0.SubrelationOf)) {
93             g.claim(connectionPoint, L0.SubrelationOf, null, superrelation);
94             Resource inverse = g.getPossibleInverse(superrelation);
95             if (inverse != null)
96                 g.claim(connectionPointInv, L0.SubrelationOf, null, inverse);
97         }
98         for (Resource type : g.getObjects(copy, L0.InstanceOf)) {
99             g.claim(connectionPoint, L0.InstanceOf, null, type);
100         }
101         for (Resource attachment : g.getObjects(copy, STR.HasAttachmentRelation)) {
102             g.claim(connectionPoint, STR.HasAttachmentRelation, attachment);
103         }
104         for (Statement stm : g.getStatements(copy, STR.AllowsConnectionType)) {
105             if (!stm.isAsserted(copy)) {
106                 g.claim(connectionPoint, stm.getPredicate(), stm.getObject());
107             }
108         }
109
110         g.claim(connectionPoint, L0.InverseOf, connectionPointInv);
111         g.claimLiteral(connectionPoint, L0.HasName, name, Bindings.STRING);
112         g.claim(connectionPoint, L0.ConsistsOf, connectionPointInv);
113         g.claimLiteral(connectionPointInv, L0.HasName, "Inverse", Bindings.STRING);
114
115         StructuralUtils.addConnectionPoint(g, componentType, connectionPoint);
116
117         g.claim(componentType, L0.ConsistsOf, connectionPoint);
118
119         return Pair.make(connectionPoint, connectionPointInv);
120         
121     }
122
123     /**
124      * Creates a new component and the templates associated with it.
125      * This method does not check whether there already exists 
126      * a component with the same name and parent.
127      * @param g
128      * @param parent The parent composite of the new component.
129      * @param name The name of the new component.
130      * @param componentType The type of the new component.
131      * @return
132      * @throws DatabaseException
133      */
134     public static Resource newComponent(WriteGraph g, Resource parent, String name, Resource componentType) throws DatabaseException {
135         Layer0 L0 = Layer0.getInstance(g);
136         
137         HashMap<String,Object> parameters = new HashMap<String,Object>();
138         parameters.put("parent", parent);
139
140         InstantiateRequest ir = new InstantiateRequest(componentType, parameters);
141         Resource component = ir.perform(g);        
142         g.claim(component, L0.HasName, Layer0Utils.literal(g, name));
143         g.claim(component, L0.HasLabel, Layer0Utils.literal(g, ""));
144         g.claim(parent, L0.ConsistsOf, component);
145         // Add identifier
146         Layer0Utils.addL0Identifier(g, component);
147         // Add comment to change set.
148         CommentMetadata cm = g.getMetadata(CommentMetadata.class);
149         g.addMetadata(cm.add("Created component " + component));
150         return component;
151     }
152     
153     /**
154      * Returns all child components of a composite.
155      */
156     public static Collection<Resource> getChildComponents(ReadGraph g, Resource parent) throws DatabaseException {
157         Layer0 L0 = Layer0.getInstance(g);
158         StructuralResource2 STR = StructuralResource2.getInstance(g);
159         
160         Collection<Resource> allChildren = g.getObjects(parent, L0.ConsistsOf);
161         ArrayList<Resource> result = new ArrayList<Resource>(allChildren.size());
162         for(Resource child : allChildren)
163             // Composite may contain other things than components, therefore we must do checking
164             if(g.isInstanceOf(child, STR.Component))
165                 result.add(child);
166         return result;
167     }
168
169     /**
170      * Returns the component type of the given component or null if the 
171      * parameter is not a component.
172      */
173     public static Resource getComponentType(ReadGraph g, Resource component) throws DatabaseException {
174         StructuralResource2 STR = StructuralResource2.getInstance(g);
175         return g.getPossibleType(component, STR.Component);
176     }
177     
178     /**
179      * Returns the definitions of the component type or null, if the component
180      * type does not have a definition.
181      */
182     public static Resource getComponentTypeDefinition(ReadGraph g, Resource componentType) throws DatabaseException {
183         StructuralResource2 STR = StructuralResource2.getInstance(g);
184         return g.getPossibleObject(componentType, STR.IsDefinedBy);
185     }
186     
187     /**
188      * Returns all (component,connectionRelation) pairs that are connected
189      * to the given component and connectionRelation.
190      * @param component
191      * @param bindingRelation
192      * @return
193      */
194     public static Set<Terminal> getRelatedTerminals(RequestProcessor g, Resource component, Resource connectionRelation) throws DatabaseException {
195         return g.syncRequest(new ConnectedTo(component, connectionRelation));
196     }
197     
198     /**
199      * Returns all connections that are reachable from the given connection
200      * with IsJoinedBy-relation including the given connection itself. 
201      */ 
202     public static Set<Resource> getRelatedConnections(RequestProcessor g, Resource connection) throws DatabaseException {
203         return g.syncRequest(new RelatedConnections(connection));
204     }
205     
206     /**
207      * Returns all connections that are reachable from the given connection join.     * 
208      */ 
209     public static Set<Resource> getRelatedConnectionsOfConnectionJoin(RequestProcessor g, Resource connectionJoin) throws DatabaseException {
210         return g.syncRequest(new RelatedConnectionsOfConnectionJoin(connectionJoin));
211     }
212     
213     /**
214      * Returns all connections that are reachable from the given connection join
215      * without visiting the resources in the collection excludedConnections.
216      */
217     public static Set<Resource> getRelatedConnectionsOfConnectionJoin(RequestProcessor g, Resource connectionJoin, Collection<Resource> excludedConnections) throws DatabaseException {
218         return g.syncRequest(new RelatedConnectionsOfConnectionJoin(connectionJoin, excludedConnections));
219     }
220     
221     /**
222      * Returns true if the given composite is a parent of some/all components 
223      * where the connection is attached to.
224      */
225     public static boolean isConnectionInComposite(ReadGraph g, Resource connection, Resource composite) throws DatabaseException {
226         StructuralResource2 STR = StructuralResource2.getInstance(g);
227         Layer0 L0 = Layer0.getInstance(g);
228         Collection<Resource> connects = g.getObjects(connection, STR.Connects);
229         if (!connects.isEmpty()) {
230             for(Resource component : connects)
231                 for(Resource parent : g.getObjects(component, L0.PartOf))
232                     if(parent.equals(composite))
233                         return true;
234         } else {
235             Set<Resource> joinedComposites = null;
236             for(Resource join : g.getObjects(connection, STR.IsJoinedBy)) {
237                 Collection<Resource> joined = g.getObjects(join, STR.JoinsComposite);
238                 if (joinedComposites == null) {
239                     joinedComposites = new THashSet<Resource>(joined);
240                 } else {
241                     joinedComposites.retainAll(joined);
242                 }
243             }
244             if (joinedComposites != null && joinedComposites.size() == 1) {
245                 return joinedComposites.contains(composite);
246             }
247         }
248         return false;
249     }
250     
251     public static Variable getConnectionPoint(ReadGraph graph, Variable component, Resource relation) throws DatabaseException {
252         Layer0 L0 = Layer0.getInstance(graph);
253         String relationName = graph.getRelatedValue(relation, L0.HasName, Bindings.STRING);
254         return component.getProperty(graph, relationName);
255     }
256
257     public static Variable getPossibleConnectionPoint(ReadGraph graph, Variable component, Resource relation) throws DatabaseException {
258         try {
259                 return getConnectionPoint(graph, component, relation);
260         } catch (MissingVariableException e) {
261                 return null;
262         }
263     }
264
265     public static Variable getPossibleConnectionTo(ReadGraph graph, Variable component, Resource relation) throws DatabaseException {
266         Variable property = component.getPossibleProperty(graph, relation);
267         if(property == null) return null;
268         Connection conn = property.getPossibleValue(graph);
269         if(conn == null) return null;
270         Collection<Variable> cps = conn.getConnectionPoints(graph, null);
271         if(cps.size() == 2) {
272                 for(Variable var : cps) {
273                         if(property.equals(var)) continue;
274                         return var;
275                 }
276         }
277         return null;
278     }
279
280     public static boolean isImmutable(ReadGraph graph, Resource r) throws DatabaseException {
281         StructuralResource2 STR = StructuralResource2.getInstance(graph);
282         Resource uc = graph.syncRequest(new PossibleTypedParent(r, STR.ComponentType));
283         return graph.isImmutable(r)
284                 // Anything under a published or locked user component is published as well
285                 || (uc != null && (Layer0Utils.isPublished(graph, uc)
286                          || graph.hasStatement(uc, STR.ComponentType_Locked)))
287                 // Anything under a published container (shared library) is published as well
288                 || Layer0Utils.isContainerPublished(graph, r)
289                 ;
290     }
291     
292     public static List<Variable> structuralConnectionConnectionPoints(ReadGraph graph, Connection conn, Resource relationType) throws DatabaseException {
293         return new ArrayList<Variable>(conn.getConnectionPoints(graph, relationType));
294     }
295
296 }