1 package org.simantics.modeling.tests.commands;
3 import java.awt.geom.Point2D;
4 import java.util.Arrays;
5 import java.util.Collections;
7 import junit.framework.Assert;
9 import org.simantics.db.Resource;
10 import org.simantics.db.WriteGraph;
11 import org.simantics.db.common.utils.OrderedSetUtils;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.diagram.content.ConnectionUtil;
14 import org.simantics.diagram.query.DiagramRequests;
15 import org.simantics.diagram.synchronization.graph.AddElement;
16 import org.simantics.layer0.Layer0;
17 import org.simantics.modeling.tests.cases.ModelingCommandSequenceTest;
18 import org.simantics.modeling.tests.traits.SingleResourceTrait;
19 import org.simantics.modeling.tests.traits.SingleResourceTraitImpl;
20 import org.simantics.structural.stubs.StructuralResource2;
21 import org.simantics.structural2.modelingRules.CPTerminal;
22 import org.simantics.structural2.modelingRules.ConnectionJudgement;
23 import org.simantics.structural2.modelingRules.IConnectionPoint;
24 import org.simantics.structural2.modelingRules.IModelingRules;
27 public class Connect extends ResourceWriteCommand<ModelingCommandSequenceTest> implements SingleResourceTrait {
29 private SingleResourceTrait diagram;
30 private SingleResourceTrait m1;
31 private SingleResourceTrait m2;
32 private SingleResourceTrait c1;
33 private SingleResourceTrait c2;
35 public Connect(SingleResourceTrait diagram, SingleResourceTrait m1, SingleResourceTrait c1, SingleResourceTrait m2, SingleResourceTrait c2) {
36 this.diagram = diagram;
43 public Connect(SingleResourceTrait diagram, SingleResourceTrait m1, SingleResourceTrait c1, SingleResourceTrait m2, Resource c2) {
44 this.diagram = diagram;
48 this.c2 = new SingleResourceTraitImpl(c2);
51 public Connect(SingleResourceTrait diagram, SingleResourceTrait m1, Resource c1, SingleResourceTrait m2, SingleResourceTrait c2) {
52 this.diagram = diagram;
54 this.c1 = new SingleResourceTraitImpl(c1);
59 public Connect(SingleResourceTrait diagram, SingleResourceTrait m1, Resource c1, SingleResourceTrait m2, Resource c2) {
60 this.diagram = diagram;
62 this.c1 = new SingleResourceTraitImpl(c1);
64 this.c2 = new SingleResourceTraitImpl(c2);
68 protected Resource run(WriteGraph graph, ModelingCommandSequenceTest environment) throws DatabaseException {
70 ConnectionUtil cu = new ConnectionUtil(graph);
71 Resource conn = cu.createConnectionWithCorners(graph, m1.getResource(graph), c1.getResource(graph), m2.getResource(graph), c2.getResource(graph), Collections.<Point2D>emptyList());
73 Layer0 L0 = Layer0.getInstance(graph);
74 StructuralResource2 STR = StructuralResource2.getInstance(graph);
75 graph.claim(conn, L0.PartOf, diagram.getResource());
77 AddElement.claimFreshElementName(graph, diagram.getResource(), conn);
78 OrderedSetUtils.addFirst(graph, diagram.getResource(), conn);
80 IModelingRules modelingRules = graph.syncRequest(DiagramRequests.getModelingRules(diagram.getResource(), null));
82 IConnectionPoint cp1 = new CPTerminal(m1.getResource(graph), c1.getResource(graph));
83 IConnectionPoint cp2 = new CPTerminal(m2.getResource(graph), c2.getResource(graph));
85 ConnectionJudgement judgement = modelingRules.judgeConnection(graph, Arrays.asList(cp1, cp2));
87 Assert.assertNotNull(judgement.connectionType);
89 graph.claim(conn, STR.HasConnectionType, null, judgement.connectionType);