1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package org.sat4j.pb;
31
32 import java.util.ArrayList;
33 import java.util.List;
34
35 public class DepdendenyNode<C> {
36 private final C name;
37 private List<DepdendenyNode<C>> children;
38 private final Explanation<C> explanation;
39
40 public DepdendenyNode(C name, Explanation<C> explanation) {
41 this.name = name;
42 this.explanation = explanation;
43 }
44
45 public C getName() {
46 return this.name;
47 }
48
49 public DepdendenyNode<C> newChild(C name) {
50 DepdendenyNode<C> newNode = this.explanation.newNode(name);
51 if (this.children == null) {
52 this.children = new ArrayList<DepdendenyNode<C>>();
53 }
54 this.children.add(newNode);
55 return newNode;
56 }
57
58 public boolean hasBranches() {
59 if (this.children == null || this.children.isEmpty()) {
60 return false;
61 }
62 if (this.children.size() > 1) {
63 return true;
64 }
65 return this.children.get(0).hasBranches();
66 }
67
68 public int getMaxDepth() {
69 if (this.children == null || this.children.isEmpty()) {
70 return 1;
71 }
72 int maxChildDepth = 0;
73 for (DepdendenyNode<C> child : this.children) {
74 int childDepth = child.getMaxDepth();
75 if (childDepth > maxChildDepth) {
76 maxChildDepth = childDepth;
77 }
78 }
79 return maxChildDepth + 1;
80 }
81
82 public DepdendenyNode<C> getOnlyChild() {
83 if (this.children == null || this.children.isEmpty()) {
84 return null;
85 }
86 if (this.children.size() > 1) {
87 throw new IllegalStateException(this + " has "
88 + this.children.size() + " children.");
89 }
90 return this.children.get(0);
91 }
92 }