1 package org.simantics.structural2.variables;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashMap;
7 import java.util.HashSet;
12 import org.simantics.databoard.Bindings;
13 import org.simantics.db.ReadGraph;
14 import org.simantics.db.Resource;
15 import org.simantics.db.Statement;
16 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
17 import org.simantics.db.common.request.BinaryRead;
18 import org.simantics.db.common.request.ResourceRead;
19 import org.simantics.db.common.request.TransientUnaryRead;
20 import org.simantics.db.common.utils.CommonDBUtils;
21 import org.simantics.db.common.utils.NameUtils;
22 import org.simantics.db.common.utils.NearestOwnerFinder;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.exception.NoSingleResultException;
25 import org.simantics.db.layer0.exception.MissingVariableException;
26 import org.simantics.db.layer0.exception.MissingVariableValueException;
27 import org.simantics.db.layer0.request.VariableRead;
28 import org.simantics.db.layer0.variable.Variable;
29 import org.simantics.db.service.CollectionSupport;
30 import org.simantics.db.service.QueryControl;
31 import org.simantics.layer0.Layer0;
32 import org.simantics.modeling.ModelingResources;
33 import org.simantics.structural.stubs.StructuralResource2;
34 import org.simantics.structural2.Functions;
35 import org.simantics.structural2.Functions.InterfaceResolution;
36 import org.simantics.structural2.queries.ConnectionSet;
37 import org.simantics.structural2.utils.StructuralUtils;
38 import org.simantics.structural2.utils.StructuralUtils.StructuralComponentClass;
39 import org.simantics.structural2.variables.StandardProceduralChildVariable.FixedConnection;
40 import org.simantics.utils.datastructures.Pair;
42 import gnu.trove.map.hash.THashMap;
43 import gnu.trove.set.hash.THashSet;
45 public class ConnectionBrowser {
48 * Finds the components connected by the connection. Also connections
52 * @param connection A connection whose related modules are searched.
53 * @param configuration A variable that represents the composite where the connection belongs to.
54 * @return A map whose keys are components and they are mapped to
57 public static Collection<ResourceWithContext> findConnectedComponents(
58 ReadGraph graph, Resource connection, Variable configuration)
59 throws DatabaseException {
61 ArrayList<ResourceWithContext> result =
62 new ArrayList<ResourceWithContext>();
63 THashSet<Resource> visitedConnections = new THashSet<Resource>();
66 findConnectedComponents(graph, connection, configuration, result,
71 private static void findConnectedComponents(
72 ReadGraph graph, Resource connection, Variable configuration,
73 ArrayList<ResourceWithContext> result,
74 THashSet<Resource> visitedConnections) throws DatabaseException {
75 if(visitedConnections.add(connection)) {
76 StructuralResource2 STR = StructuralResource2.getInstance(graph);
77 Layer0 L0 = Layer0.getInstance(graph);
79 // Browse related components
80 for(Statement stat : graph.getStatements(connection, STR.Connects)) {
81 Resource component = stat.getObject();
82 Resource relation = graph.getInverse(stat.getPredicate());
83 //System.out.println(NameUtils.getSafeName(graph, component) + "." + NameUtils.getSafeName(graph, relation));
84 Resource boundConnection = graph.getPossibleObject(relation, STR.IsBoundBy);
85 Resource type = StructuralUtils.getPossibleComponentType(graph, configuration, component);
86 Resource def = type != null ? graph.getPossibleObject(type, STR.IsDefinedBy) : null;
87 if(boundConnection != null && def != null) {
88 // The connection point is bound in component type
89 Variable newContext = configuration.browsePossible(graph, component);
90 Resource newComposite = getCompositeOfConnection(graph, boundConnection);
91 if(newContext != null && newComposite != null) {
92 newContext = browse(graph, def, newContext, newComposite);
93 if (newContext != null)
94 findConnectedComponents(graph, boundConnection,
96 result, visitedConnections);
100 //System.out.println("added result");
101 // A primitive connection point
102 Variable context = configuration.browsePossible(graph, component);
104 result.add(new ResourceWithContext(component, context));
108 // Browse over connection joins
109 for(Resource join : graph.getObjects(connection, STR.IsJoinedBy))
110 for(Resource otherConnection : graph.getObjects(join, STR.Joins))
111 if(!connection.equals(otherConnection)) {
112 Resource sourceComposite = getCompositeOfConnection(graph, connection);
113 Resource targetComposite = getCompositeOfConnection(graph, otherConnection);
114 if (sourceComposite != null && targetComposite != null) {
115 Variable sibling = browseSibling(graph,
120 findConnectedComponents(graph, otherConnection,
121 sibling, result, visitedConnections);
127 for(Resource relation : graph.getObjects(connection, STR.Binds)) {
128 Resource composite = getCompositeOfConnection(graph, connection);
129 if (composite == null)
132 Variable curConfiguration = configuration;
133 while(!graph.hasStatement(composite, STR.Defines)) {
134 composite = graph.getSingleObject(composite, L0.PartOf);
135 curConfiguration = curConfiguration.getParent(graph);
137 Variable parent = curConfiguration.getParent(graph);
138 Resource component = curConfiguration.getRepresents(graph);
139 for(Resource c : graph.getObjects(component, relation))
140 findConnectedComponents(graph, c,
141 parent, result, visitedConnections);
143 } catch(NoSingleResultException e) {
144 } catch(MissingVariableException e) {
145 } catch(MissingVariableValueException e) {
150 public static Collection<VariableConnectionPointDescriptor> drill(ReadGraph graph, VariableConnectionPointDescriptor pair) throws DatabaseException {
152 Collection<InterfaceResolution> interfaceDescription = pair.getInterfaceDescription(graph);
153 if(interfaceDescription != null && interfaceDescription.size() > 0) {
155 Variable cp = pair.getVariable(graph);
156 Variable context = cp.getParent(graph);
157 String cpName = cp.getName(graph);
159 Collection<VariableConnectionPointDescriptor> result = new ArrayList<VariableConnectionPointDescriptor>();
160 for(InterfaceResolution r : interfaceDescription) {
161 if(r.interfaceName.equals(cpName)) {
162 String path = Functions.resolveInterfacePath(graph, context, r.componentName, r.connectionPoint);
163 result.add(new BrowseConnectionDescriptor(context, path));
167 if(result.isEmpty()) return null;
172 return Collections.singleton(pair);
177 public static class JoinConnections extends ResourceRead<Collection<Resource>> {
179 public JoinConnections(Resource join) {
184 public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {
185 ConnectionSet cs = new ConnectionSet(graph);
186 cs.addJoin(graph, resource);
187 return cs.getConnections();
193 public static final class VariableChildren extends TransientUnaryRead<Variable, Map<Resource,Variable>> {
195 public VariableChildren(ReadGraph graph, Variable variable) throws DatabaseException {
196 super(graph, variable);
199 public VariableChildren(ReadGraph graph, QueryControl qc, Variable variable) throws DatabaseException {
200 super(graph, qc, variable);
204 public Map<Resource, Variable> perform(ReadGraph graph, Variable parameter) throws DatabaseException {
205 CollectionSupport cs = graph.getService(CollectionSupport.class);
206 Map<Resource,Variable> result = cs.createMap(Variable.class);
207 for(Variable child : parameter.getChildren(graph)) {
208 Resource represents = child.getPossibleRepresents(graph);
209 if(represents != null) result.put(represents, child);
216 static Variable resolve(ReadGraph graph, Variable base, Resource component) throws DatabaseException {
217 Map<Resource,Variable> map = graph.syncRequest(new VariableChildren(graph, base), TransientCacheAsyncListener.<Map<Resource,Variable>>instance());
218 Variable result = map.get(component);
219 if(result != null) return result;
221 Layer0 L0 = Layer0.getInstance(graph);
222 Resource parent = graph.getPossibleObject(component, L0.PartOf);
223 if(parent == null) return null;
224 Variable v = resolve(graph, base, parent);
225 if (v == null) return null;
226 map = graph.syncRequest(new VariableChildren(graph, v), TransientCacheAsyncListener.<Map<Resource,Variable>>instance());
227 return map.get(component);
231 public static class ConnectionComponentsWithAncestor extends TransientUnaryRead<Resource, List<Resource>> {
233 final private List<Resource> result;
235 public ConnectionComponentsWithAncestor(ReadGraph graph, Resource conn) throws DatabaseException {
236 this(graph, conn, null);
239 public ConnectionComponentsWithAncestor(ReadGraph graph, QueryControl qc, Resource conn, List<Resource> result) throws DatabaseException {
240 super(graph, qc, conn);
241 this.result = result;
244 public ConnectionComponentsWithAncestor(ReadGraph graph, Resource conn, List<Resource> result) throws DatabaseException {
246 this.result = result;
249 private ConnectionSet connSet(ReadGraph graph, Resource r ) throws DatabaseException {
250 ConnectionSet cs = new ConnectionSet(graph);
251 cs.addConnection(graph, r);
256 public List<Resource> perform(ReadGraph graph, Resource resource) throws DatabaseException {
258 if(result != null) return result;
260 Layer0 L0 = Layer0.getInstance(graph);
261 StructuralResource2 STR = StructuralResource2.getInstance(graph);
262 CollectionSupport colls = graph.getService(CollectionSupport.class);
263 THashSet<Resource> ancestorGenerators = new THashSet<Resource>();
264 Set<Resource> parts = colls.createSet();
265 ConnectionSet cs = connSet(graph, resource);
266 for(Resource connRes : cs.getConnections()) {
267 for(Statement stm : graph.getStatements(connRes, STR.Connects)) {
268 Resource component = stm.getObject();
269 Resource parent = graph.getPossibleObject(component, L0.PartOf);
270 if(parent != null && !graph.isInstanceOf(component, ModelingResources.getInstance(graph).ReferenceElement))
271 ancestorGenerators.add(parent);
275 for (Resource join : cs.getJoins()) {
277 for (Resource composite : graph.getObjects(join, STR.JoinsComposite))
278 ancestorGenerators.add(composite);
280 Resource ancestor = ancestorGenerators.size() == 1 ? ancestorGenerators.iterator().next() : NearestOwnerFinder.getNearestOwnerFromDirectOwners(graph, ancestorGenerators);
282 List<Resource> result = colls.createList();
283 result.add(ancestor);
284 result.addAll(colls.asSortedList(parts));
286 if(parameter != WITH_PARENT) {
287 for(int i=1;i<result.size();i++) {
288 Resource r = result.get(i);
290 if(!r.equals(resource))
291 graph.syncRequest(new ConnectionComponentsWithAncestor(graph, r, result), TransientCacheAsyncListener.<List<Resource>>instance());
301 public static Collection<VariableConnectionPointDescriptor> climb(ReadGraph graph, Variable child, Resource cp, String subPath_) throws DatabaseException {
303 boolean isStructural = false;
305 Variable curConfiguration = child.getParent(graph);
309 Collection<InterfaceResolution> interfaceDescription = Functions.computeInterfacePaths(graph, curConfiguration);
310 if(interfaceDescription != null) {
311 isStructural = interfaceDescription != Functions.BUILTIN_STRUCTURAL_CPS;
312 if(interfaceDescription.size() > 0) {
314 if(subPath_ == null) {
316 String childName = child.getName(graph);
317 for(InterfaceResolution r : interfaceDescription) {
318 if(r.componentName.equals(childName) && r.connectionPoint.equals(cp)) {
319 Variable pConn = curConfiguration.getPossibleProperty(graph, r.interfaceName);
321 Resource cp2 = pConn.getPossiblePredicateResource(graph);
322 Collection<VariableConnectionPointDescriptor> res = climb(graph, curConfiguration, cp2, null);
323 if(res != null) return res;
325 return Collections.emptyList();
330 throw new UnsupportedOperationException("");
337 if(child instanceof StandardProceduralChildVariable) {
339 Variable conn = child.getPossibleProperty(graph, cp);
340 FixedConnection fc = (FixedConnection)conn.getValue(graph);
341 Set<VariableConnectionPointDescriptor> result = new THashSet<VariableConnectionPointDescriptor>(1+fc.cps.size());
342 result.add(new ComponentConnectionDescriptor(child, cp));// (graph, STR, curConfiguration, "/" + c.name + "#" + conn.getName(graph)));
343 for(Pair<String,Resource> cpzz : fc.cps) {
344 if(cpzz.first == null) {
345 throw new DatabaseException("Lifted connection was not resolved.");
347 result.add(new PairConnectionDescriptor(curConfiguration, cpzz));
354 Resource represents = child.getRepresents(graph);
358 Collection<Resource> conns = graph.getObjects(represents, res);
359 HashSet<VariableConnectionPointDescriptor> result = new HashSet<VariableConnectionPointDescriptor>();
360 for(Resource c : conns) {
361 List<Resource> rs = graph.syncRequest(new ConnectionComponentsWithAncestor(graph, c), TransientCacheAsyncListener.<List<Resource>>instance());
362 result.addAll(graph.syncRequest(ConnectionVariables.forStructural(graph, curConfiguration, rs)));
368 Resource connection = graph.getPossibleObject(represents, res);
369 if(connection != null) {
370 List<Resource> rs = graph.syncRequest(new ConnectionComponentsWithAncestor(graph, connection), TransientCacheAsyncListener.<List<Resource>>instance());
371 return graph.syncRequest(ConnectionVariables.forConfiguration(graph, curConfiguration, rs));
374 Collection<Resource> conns = graph.getObjects(represents, res);
375 HashSet<VariableConnectionPointDescriptor> result = new HashSet<VariableConnectionPointDescriptor>();
376 for(Resource c : conns) {
377 List<Resource> rs = graph.syncRequest(new ConnectionComponentsWithAncestor(graph, c), TransientCacheAsyncListener.<List<Resource>>instance());
378 result.addAll(graph.syncRequest(ConnectionVariables.forConfiguration(graph, curConfiguration, rs)));
389 public static void reportDescriptor(ReadGraph graph, VariableConnectionPointDescriptor d) throws DatabaseException {
391 if(d instanceof ActualConnectionDescriptor) {
392 ActualConnectionDescriptor d2 = (ActualConnectionDescriptor)d;
394 System.err.println("--ActualConnectionPointDescriptor2");
395 System.err.println("---root: " + d2.root.getURI(graph));
396 System.err.println("---component: " + graph.getPossibleURI(d2.component));
397 System.err.println("---type: " + graph.getPossibleURI(d2.componentType));
398 System.err.println("---cp: " + graph.getPossibleURI(d2.cp));
399 System.err.println("---var: " + d2.getVariable(graph).getURI(graph));
404 public static class ConnectionVariables extends BinaryRead<Variable, List<Resource>, Collection<VariableConnectionPointDescriptor>> {
406 private ConnectionVariables(Variable parameter1, List<Resource> parameter2) {
407 super(parameter1, parameter2);
410 public static ConnectionVariables forConfiguration(ReadGraph graph, Variable configuration, List<Resource> rs) throws DatabaseException {
411 return new ConnectionVariables(parent(graph, configuration, rs.get(0)), rs);
414 public static ConnectionVariables forStructural(ReadGraph graph, Variable configuration, List<Resource> rs) throws DatabaseException {
415 return new ConnectionVariables(configuration, rs);
419 * Finds the parent variable of <code>configuration</code> that
420 * represents <code>ancestor</code>.
423 * @param configuration
426 * @throws DatabaseException if no parent was found that represents ancestor
428 private static Variable parent(ReadGraph graph, Variable configuration, Resource ancestor) throws DatabaseException {
429 Variable v = configuration;
430 Resource represents = v.getRepresents(graph);
431 while(!represents.equals(ancestor)) {
432 v = v.getParent(graph);
434 throw new DatabaseException(
435 "parent representing ancestor not found for variable, configuration="
436 + safeURI(graph, configuration)
438 + NameUtils.getURIOrSafeNameInternal(graph, ancestor));
440 represents = v.getRepresents(graph);
446 public Collection<VariableConnectionPointDescriptor> perform(ReadGraph graph) throws DatabaseException {
447 if(parameter == null) return Collections.emptyList();
448 Layer0 L0 = Layer0.getInstance(graph);
449 StructuralResource2 STR = StructuralResource2.getInstance(graph);
450 ArrayList<VariableConnectionPointDescriptor> result = null;
451 for(int i=1;i<parameter2.size();i++) {
452 Resource connRes = parameter2.get(i);
453 for(Statement stm : graph.getStatements(connRes, STR.Connects)) {
454 Resource component = stm.getObject();
455 Resource connectionPoint = graph.getInverse(stm.getPredicate());
456 if(result == null) result = new ArrayList<VariableConnectionPointDescriptor>();
457 String componentName = graph.getRelatedValue(component, L0.HasName, Bindings.STRING);
458 Variable possibleChild = parameter.getPossibleChild(graph, componentName);
459 if(possibleChild != null) {
460 Resource type = possibleChild.getPossibleType(graph, STR.Component);
462 result.add(new ActualConnectionDescriptor(parameter, component, possibleChild.getType(graph), connectionPoint));
464 throw new DatabaseException("Child does not have a structural type: " + possibleChild.getURI(graph));
467 Resource type = graph.getPossibleType(component, STR.Component);
469 result.add(new ActualConnectionDescriptor(parameter, component, type, connectionPoint));
471 throw new DatabaseException("Child with name " + componentName + " does not have a structural type: " + parameter.getURI(graph));
476 if(result == null) return Collections.emptyList();
482 static class IsLeafType extends ResourceRead<Boolean> {
484 protected IsLeafType(Resource type) {
489 public Boolean perform(ReadGraph graph) throws DatabaseException {
491 StructuralComponentClass clazz = StructuralComponentClass.get(graph, resource);
492 return StructuralComponentClass.PRIMITIVE.equals(clazz);
498 static class ChildMapOfVariable extends VariableRead<Map<Resource,Variable>> {
500 public ChildMapOfVariable(Variable variable) {
505 public Map<Resource, Variable> perform(ReadGraph graph) throws DatabaseException {
506 HashMap<Resource,Variable> result = new HashMap<Resource,Variable>();
507 for(Variable child : variable.getChildren(graph)) {
508 Resource represents = child.getPossibleRepresents(graph);
509 if(represents != null) result.put(represents, child);
517 * Given a root composite, related variable and some other component inside the composite,
518 * finds the related variable for that component.
520 public static Variable browse(ReadGraph graph, Resource root, Variable rootContext, Resource target) throws DatabaseException {
521 if(target.equals(root))
524 Layer0 L0 = Layer0.getInstance(graph);
525 String name = (String)graph.getPossibleRelatedValue(target, L0.HasName, Bindings.STRING);
526 Resource parent = graph.getPossibleObject(target, L0.PartOf);
527 if(name == null || parent == null)
529 Variable parentVariable = browse(graph, root, rootContext, parent);
530 if(parentVariable == null)
532 return parentVariable.getPossibleChild(graph, name);
537 * Finds a variable whose location related to sourceContext is the same as
538 * between target and source. In other words, the method solves {@code targetContext}
539 * in the following equations:
541 * URI(source) = resourceURIBase + sourceSuffix
542 * URI(sourceContext) = variableURIBase + sourceSuffix
543 * URI(target) = resourceURIBase + targetSuffix
544 * URI(targetContext) = variableURIBase + targetSuffix
547 public static Variable browseSibling(ReadGraph graph, Resource source, Variable sourceContext, Resource target) throws DatabaseException {
548 Layer0 L0 = Layer0.getInstance(graph);
549 THashMap<Resource, Variable> sourceMap = new THashMap<Resource, Variable>();
550 while(source != null && sourceContext != null) {
551 sourceMap.put(source, sourceContext);
552 source = graph.getPossibleObject(source, L0.PartOf);
553 sourceContext = sourceContext.getParent(graph);
555 return browseSibling(graph, sourceMap, target);
558 private static Variable browseSibling(ReadGraph graph, THashMap<Resource, Variable> sourceMap, Resource target) throws DatabaseException {
559 Layer0 L0 = Layer0.getInstance(graph);
560 Variable result = sourceMap.get(target);
563 String name = (String)graph.getPossibleRelatedValue(target, L0.HasName, Bindings.STRING);
564 Resource parent = graph.getPossibleObject(target, L0.PartOf);
565 if(name == null || parent == null)
567 Variable parentVariable = browseSibling(graph, sourceMap, parent);
568 if(parentVariable == null)
570 return parentVariable.getPossibleChild(graph, name);
574 * Returns the composite where the connection given as a parameter resides.
576 public static Resource getCompositeOfConnection(ReadGraph graph, Resource connection) throws DatabaseException {
577 Layer0 L0 = Layer0.getInstance(graph);
578 StructuralResource2 STR = StructuralResource2.getInstance(graph);
579 // First from connected components
580 for(Resource component : graph.getObjects(connection, STR.Connects))
581 for(Resource composite : graph.getObjects(component, L0.PartOf))
583 // It could be that the connection is only supported by joins (input flag -> output flag) - use diagram info TODO!!
584 Resource connToDiagramConn = graph.getPossibleResource("http://www.simantics.org/Modeling-1.2/ConnectionToDiagramConnection");
585 if(connToDiagramConn != null) {
586 Resource diagramConnection = graph.getPossibleObject(connection, connToDiagramConn);
587 if(diagramConnection != null) {
588 Resource diagram = graph.getPossibleObject(diagramConnection, L0.PartOf);
589 if(diagram != null) {
590 Resource diagramToComposite = graph.getPossibleResource("http://www.simantics.org/Modeling-1.2/DiagramToComposite");
591 if(diagramToComposite != null) {
592 return graph.getPossibleObject(diagram, diagramToComposite);
600 static class Flatten extends BinaryRead<Variable,Resource,Collection<VariableConnectionPointDescriptor>> {
602 public Flatten(Variable parameter1,
603 Resource parameter2) {
604 super(parameter1, parameter2);
608 public Collection<VariableConnectionPointDescriptor> perform(ReadGraph graph)
609 throws DatabaseException {
610 return doFlatten(graph, parameter, parameter2, null);
615 public static Collection<VariableConnectionPointDescriptor> flatten(ReadGraph graph, Variable child, Resource cp, Resource relationType) throws DatabaseException {
617 if(relationType == null) return graph.syncRequest(new Flatten(child, cp));
619 return doFlatten(graph, child, cp, relationType);
623 public static Collection<VariableConnectionPointDescriptor> doFlatten(ReadGraph graph, Variable child, Resource cp, Resource relationType) throws DatabaseException {
625 Set<VariableConnectionPointDescriptor> result = null;
626 Set<VariableConnectionPointDescriptor> needDrill = null;
628 Collection<VariableConnectionPointDescriptor> climbed = climb(graph, child, cp, null);
629 for(VariableConnectionPointDescriptor desc : climbed) {
630 if(!desc.isLeaf(graph)) {
631 if(needDrill == null)
632 needDrill = new THashSet<VariableConnectionPointDescriptor>(climbed.size());
636 result = new THashSet<VariableConnectionPointDescriptor>(climbed.size());
641 if(needDrill == null) {
643 * All descriptors were already flat - just take case of filtering
645 if(relationType != null) {
646 ArrayList<VariableConnectionPointDescriptor> filtered = new ArrayList<VariableConnectionPointDescriptor>(climbed.size());
647 for(VariableConnectionPointDescriptor desc : climbed)
648 if(filterByRelationType(graph, desc, relationType))
658 * There were some descriptors that require drill
660 for(VariableConnectionPointDescriptor top : needDrill) {
661 Collection<VariableConnectionPointDescriptor> drilled = drill(graph, top);
662 if(drilled != null) {
663 for(VariableConnectionPointDescriptor drill : drilled) {
664 if(relationType != null) {
665 if(!filterByRelationType(graph, drill, relationType))
669 result = new THashSet<VariableConnectionPointDescriptor>(climbed.size());
678 private static boolean filterByRelationType(ReadGraph graph, VariableConnectionPointDescriptor desc, Resource relationType) throws DatabaseException {
679 Resource predicateResource = desc.getConnectionPointResource(graph);
680 return predicateResource != null && graph.isInstanceOf(predicateResource, relationType);
683 private static String safeURI(ReadGraph graph, Variable v) {
685 return "null variable";
687 return v.getURI(graph);
688 } catch (DatabaseException e) {