01: package org.drools.eclipse.editors.rete.model;
02:
03: import org.drools.reteoo.BaseVertex;
04:
05: /**
06: * A connection between two distinct vertices.
07: */
08: public class Connection extends ModelElement {
09:
10: private boolean isConnected;
11:
12: private BaseVertex source;
13:
14: private BaseVertex target;
15:
16: /**
17: * Creating a connection between two distinct vertices.
18: *
19: * @param source a source endpoint
20: * @param target a target endpoint
21: * @throws IllegalArgumentException if any of the parameters are null or source == target
22: */
23: public Connection(BaseVertex source, BaseVertex target) {
24: this .source = source;
25: this .target = target;
26: source.addConnection(this );
27: target.addConnection(this );
28: isConnected = true;
29: }
30:
31: /**
32: * Disconnect this connection from the vertices it is attached to.
33: */
34: public void disconnect() {
35: if (isConnected) {
36: source.removeConnection(this );
37: target.removeConnection(this );
38: isConnected = false;
39: }
40: }
41:
42: /**
43: * Returns the source endpoint of this connection.
44: *
45: * @return BaseVertex vertex
46: */
47: public BaseVertex getSource() {
48: return source;
49: }
50:
51: /**
52: * Returns the target endpoint of this connection.
53: *
54: * @return BaseVertex vertex
55: */
56: public BaseVertex getTarget() {
57: return target;
58: }
59:
60: /**
61: * Gets opposite of specified vertex.
62: *
63: * Returning <code>null</code> if specified not does not belong into this connection.
64: *
65: * @param vertex
66: * @return opposite of vertex
67: */
68: public BaseVertex getOpposite(BaseVertex vertex) {
69: // If null or not part of this connection
70: if (vertex == null
71: || (!vertex.equals(getSource()) && !vertex
72: .equals(getTarget()))) {
73: return null;
74: }
75: if (vertex.equals(getSource())) {
76: return getTarget();
77: }
78: return getSource();
79: }
80:
81: }
|