1 package org.simantics.structural2.utils;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
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;
34 import gnu.trove.set.hash.THashSet;
37 * A utility class for manipulating structural models.
39 * @author Hannu Niemistö
41 public class StructuralUtils {
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));
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))
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))
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);
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);
79 public static Resource createConnectionPoint(WriteGraph g, Resource componentType, Resource copy, String name) throws DatabaseException {
80 return createConnectionPointP(g, componentType, copy, name).first;
83 public static Pair<Resource,Resource> createConnectionPointP(WriteGraph g, Resource componentType, Resource copy, String name) throws DatabaseException {
85 Layer0 L0 = Layer0.getInstance(g);
86 StructuralResource2 STR = StructuralResource2.getInstance(g);
88 // Create the connection point
89 Resource connectionPoint = g.newResource();
90 Resource connectionPointInv = g.newResource();
92 for (Resource superrelation : g.getObjects(copy, L0.SubrelationOf)) {
93 g.claim(connectionPoint, L0.SubrelationOf, null, superrelation);
94 Resource inverse = g.getPossibleInverse(superrelation);
96 g.claim(connectionPointInv, L0.SubrelationOf, null, inverse);
98 for (Resource type : g.getObjects(copy, L0.InstanceOf)) {
99 g.claim(connectionPoint, L0.InstanceOf, null, type);
101 for (Resource attachment : g.getObjects(copy, STR.HasAttachmentRelation)) {
102 g.claim(connectionPoint, STR.HasAttachmentRelation, attachment);
104 for (Statement stm : g.getStatements(copy, STR.AllowsConnectionType)) {
105 if (!stm.isAsserted(copy)) {
106 g.claim(connectionPoint, stm.getPredicate(), stm.getObject());
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);
115 StructuralUtils.addConnectionPoint(g, componentType, connectionPoint);
117 g.claim(componentType, L0.ConsistsOf, connectionPoint);
119 return Pair.make(connectionPoint, connectionPointInv);
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.
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.
132 * @throws DatabaseException
134 public static Resource newComponent(WriteGraph g, Resource parent, String name, Resource componentType) throws DatabaseException {
135 Layer0 L0 = Layer0.getInstance(g);
137 HashMap<String,Object> parameters = new HashMap<String,Object>();
138 parameters.put("parent", parent);
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);
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));
154 * Returns all child components of a composite.
156 public static Collection<Resource> getChildComponents(ReadGraph g, Resource parent) throws DatabaseException {
157 Layer0 L0 = Layer0.getInstance(g);
158 StructuralResource2 STR = StructuralResource2.getInstance(g);
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))
170 * Returns the component type of the given component or null if the
171 * parameter is not a component.
173 public static Resource getComponentType(ReadGraph g, Resource component) throws DatabaseException {
174 StructuralResource2 STR = StructuralResource2.getInstance(g);
175 return g.getPossibleType(component, STR.Component);
179 * Returns the definitions of the component type or null, if the component
180 * type does not have a definition.
182 public static Resource getComponentTypeDefinition(ReadGraph g, Resource componentType) throws DatabaseException {
183 StructuralResource2 STR = StructuralResource2.getInstance(g);
184 return g.getPossibleObject(componentType, STR.IsDefinedBy);
188 * Returns all (component,connectionRelation) pairs that are connected
189 * to the given component and connectionRelation.
191 * @param bindingRelation
194 public static Set<Terminal> getRelatedTerminals(RequestProcessor g, Resource component, Resource connectionRelation) throws DatabaseException {
195 return g.syncRequest(new ConnectedTo(component, connectionRelation));
199 * Returns all connections that are reachable from the given connection
200 * with IsJoinedBy-relation including the given connection itself.
202 public static Set<Resource> getRelatedConnections(RequestProcessor g, Resource connection) throws DatabaseException {
203 return g.syncRequest(new RelatedConnections(connection));
207 * Returns all connections that are reachable from the given connection join. *
209 public static Set<Resource> getRelatedConnectionsOfConnectionJoin(RequestProcessor g, Resource connectionJoin) throws DatabaseException {
210 return g.syncRequest(new RelatedConnectionsOfConnectionJoin(connectionJoin));
214 * Returns all connections that are reachable from the given connection join
215 * without visiting the resources in the collection excludedConnections.
217 public static Set<Resource> getRelatedConnectionsOfConnectionJoin(RequestProcessor g, Resource connectionJoin, Collection<Resource> excludedConnections) throws DatabaseException {
218 return g.syncRequest(new RelatedConnectionsOfConnectionJoin(connectionJoin, excludedConnections));
222 * Returns true if the given composite is a parent of some/all components
223 * where the connection is attached to.
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))
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);
241 joinedComposites.retainAll(joined);
244 if (joinedComposites != null && joinedComposites.size() == 1) {
245 return joinedComposites.contains(composite);
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);
257 public static Variable getPossibleConnectionPoint(ReadGraph graph, Variable component, Resource relation) throws DatabaseException {
259 return getConnectionPoint(graph, component, relation);
260 } catch (MissingVariableException e) {
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;
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)
292 public static List<Variable> structuralConnectionConnectionPoints(ReadGraph graph, Connection conn, Resource relationType) throws DatabaseException {
293 return new ArrayList<Variable>(conn.getConnectionPoints(graph, relationType));