1 package com.github.dexecutor.executor.graph;
2
3 import java.util.Collection;
4 import java.util.LinkedHashSet;
5 import java.util.Set;
6
7 /**
8 * Dependency would be construced based on this APIs, Dexecutor uses this data structure to represet the dependencies between tasks
9 * @author Nadeem Mohammad
10 *
11 * @param <T>
12 */
13 public interface Graph<T extends Comparable<T>> {
14 /**
15 * Should add the two nodes in the datastructure in such a way that {@code evalFirstValue} should be evaluated before {@code evalAfterValue}.
16 * Nodes should be created only if it is not already added.
17 *
18 * @param evalFirstValue
19 * @param evalAfterValue
20 */
21 void addDependency(final T evalFirstValue, final T evalAfterValue);
22 /**
23 * Adds the given node to the datastructure whith out any dependency
24 * Nodes should be created only if it is not already added.
25 * @param nodeValue
26 */
27 void addIndependent(final T nodeValue);
28 /**
29 * Returns the Set of nodes for which there is no incoming dependencies.
30 * @return set of initial nodes
31 */
32 Set<Node<T>> getInitialNodes();
33 /**
34 * Retruns the set of nodes for which there is no outgoing dependencies.
35 * @return set of leaf nodes
36 */
37 Set<Node<T>> getLeafNodes();
38 /**
39 * Returns all nodes in this graph
40 * @return all nodes in this graph
41 */
42 Collection<Node<T>> allNodes();
43 /**
44 * Returns the total number of nodes in this graph
45 *
46 * @return total number of nodes in this graph
47 */
48 int size();
49
50 /**
51 * A node representation in this graph, every node may have set of incoming edges and outgoing edges, a node is represented by unique value
52 *
53 * @author Nadeem Mohammad
54 *
55 * @param <T>
56 */
57 public final class Node<T> {
58 /**
59 * Unique id of the node
60 */
61 private T value;
62 /**
63 * incoming dependencies for this node
64 */
65 private Set<Node<T>> inComingEdges = new LinkedHashSet<Graph.Node<T>>();
66 /**
67 * outgoing dependencies for this node
68 */
69 private Set<Node<T>> outGoingEdges = new LinkedHashSet<Graph.Node<T>>();
70 /**
71 * Constructs the node with the given node Id
72 * @param val
73 */
74 public Node(final T val) {
75 this.value = val;
76 }
77 /**
78 * Add the given node, to the set of incoming nodes
79 * @param node
80 */
81 public void addInComingNode(final Node<T> node) {
82 this.inComingEdges.add(node);
83 }
84 /**
85 * add the given to the set of out going nodes
86 * @param node
87 */
88 public void addOutGoingNode(final Node<T> node) {
89 this.outGoingEdges.add(node);
90 }
91 /**
92 *
93 * @return the set of incoming nodes
94 */
95 public Set<Node<T>> getInComingNodes() {
96 return this.inComingEdges;
97 }
98 /**
99 *
100 * @return set of out going nodes
101 */
102 public Set<Node<T>> getOutGoingNodes() {
103 return this.outGoingEdges;
104 }
105 /**
106 *
107 * @return the node's value
108 */
109 public T getValue() {
110 return this.value;
111 }
112
113 @Override
114 public int hashCode() {
115 final int prime = 31;
116 int result = 1;
117 result = prime * result + ((this.value == null) ? 0 : this.value.hashCode());
118 return result;
119 }
120
121 @Override
122 public boolean equals(final Object obj) {
123 if (obj == this) {
124 return true;
125 }
126 if (obj == null || obj.getClass() != this.getClass()) {
127 return false;
128 }
129 @SuppressWarnings("unchecked")
130 Node<T> other = (Node<T>) obj;
131
132 return this.value.equals(other.value);
133 }
134
135 @Override
136 public String toString() {
137 return String.valueOf(this.value);
138 }
139 }
140 }