Skip to content

Commit 0940b74

Browse files
Added support for dot file output
Changed Evaluable<T> from an Interface to Abstract Class. Added toString functions and support for breath first construction of dot file.
1 parent 3a3d00d commit 0940b74

File tree

5 files changed

+183
-20
lines changed

5 files changed

+183
-20
lines changed

structure/Base_Node.java

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package structure;
22

3+
import java.util.Collection;
4+
35
/**
46
<b>
57
Purpose: To be the base node which others will extend.<br>
@@ -8,31 +10,40 @@
810
</b>
911
*/
1012

11-
//TODO: add toString() methods with some reference to outputting as dot file format.
12-
public abstract class Base_Node<T> implements Evaluable<T>
13+
public abstract class Base_Node<T> extends Evaluable<T>
1314
{
14-
/**
15-
* Unique identifier for this node.
16-
*/
17-
public final int UNIQUE_IDENTIFIER;
18-
1915
/**
2016
* The name of this node.
2117
*/
2218
public final String NAME;
2319

24-
/**
25-
* Used to set {@link #UNIQUE_IDENTIFIER}
26-
*/
27-
private static int CREATED_NODES_COUNT = 0;
28-
2920
/**
3021
* Constructor of {@link #Base_Node}.
3122
* @param NAME the name to be given to this {@link #Base_Node}
3223
*/
3324
public Base_Node(final String NAME)
3425
{
3526
this.NAME = NAME;
36-
this.UNIQUE_IDENTIFIER = ++CREATED_NODES_COUNT;
27+
}
28+
29+
/**
30+
* for dot format
31+
*/
32+
public String toString()
33+
{
34+
StringBuilder output = new StringBuilder(64);
35+
36+
output.append(this.UNIQUE_IDENTIFIER);
37+
output.append("[label=\"");
38+
output.append(this.NAME.replace(">", "\\>").replace("<", "\\<").replace("\"", "\\\""));//escape certain characters
39+
output.append("\"];\n");
40+
41+
return output.toString();
42+
}
43+
44+
@Override
45+
protected Collection<? extends Evaluable<T>> continue_breath_search()
46+
{
47+
return null;
3748
}
3849
}

structure/Binary_Operator_Node.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package structure;
22

3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
36
/**
47
<b>
58
Purpose: Base of the binary operators.<br>
@@ -33,4 +36,37 @@ public Binary_Operator_Node(String NAME, final Evaluable<T> LEFT_CHILD, final Ev
3336
this.LEFT_CHILD = LEFT_CHILD;
3437
this.RIGHT_CHILD = RIGHT_CHILD;
3538
}
39+
40+
/**
41+
* for dot format
42+
*/
43+
public String toString()
44+
{
45+
StringBuilder output = new StringBuilder(128);
46+
47+
output.append(super.toString()); //call standard part
48+
49+
//left child
50+
output.append(this.UNIQUE_IDENTIFIER);
51+
output.append("->");
52+
output.append(this.LEFT_CHILD.UNIQUE_IDENTIFIER);
53+
output.append(";\n");
54+
55+
//right child
56+
output.append(this.UNIQUE_IDENTIFIER);
57+
output.append("->");
58+
output.append(this.RIGHT_CHILD.UNIQUE_IDENTIFIER);
59+
output.append(";\n");
60+
61+
return output.toString();
62+
}
63+
64+
@Override
65+
protected Collection<? extends Evaluable<T>> continue_breath_search()
66+
{
67+
Collection<Evaluable<T>> to_return = new ArrayList<Evaluable<T>>();
68+
to_return.add(this.LEFT_CHILD);
69+
to_return.add(this.RIGHT_CHILD);
70+
return to_return;
71+
}
3672
}

structure/Evaluable.java

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package structure;
22

3+
import java.util.ArrayList;
34
import java.util.Collection;
45
import java.util.List;
56
import java.util.Map;
@@ -12,10 +13,9 @@
1213
</b>
1314
*/
1415

15-
public interface Evaluable<T>
16+
public abstract class Evaluable<T>
1617
{
17-
boolean debugMode = false;
18-
18+
//TODO: add javadoc
1919
enum TestResult {
2020
/**
2121
* The evaluation succeeded
@@ -31,10 +31,31 @@ enum TestResult {
3131
Rollback
3232
}
3333

34+
/**
35+
* Lambda for {@link Evaluable#evaluate(Collection, RollbackCallback)}.
36+
*/
3437
interface RollbackCallback {
3538
TestResult call();
3639
}
3740

41+
//TODO: add javadoc
42+
static boolean debugMode = false;
43+
44+
/**
45+
* Unique identifier for this node.
46+
*/
47+
public final int UNIQUE_IDENTIFIER;
48+
49+
/**
50+
* Used to set {@link #UNIQUE_IDENTIFIER}
51+
*/
52+
private static int CREATED_NODES_COUNT = 0;
53+
54+
public Evaluable()
55+
{
56+
this.UNIQUE_IDENTIFIER = ++CREATED_NODES_COUNT;
57+
}
58+
3859
/**
3960
* Function used to evaluate a node's condition using a rollback evaluation implementation.
4061
* Allows for hand to be in an arbitrary order
@@ -43,7 +64,7 @@ interface RollbackCallback {
4364
* @param next function to call when a leaf node takes a card from the hand
4465
* @return a {@link TestResult} used as a signal on what action to preform next
4566
*/
46-
<E extends Reservable> TestResult evaluate(final Collection<E> hand, final RollbackCallback next);
67+
abstract <E extends Reservable> TestResult evaluate(final Collection<E> hand, final RollbackCallback next);
4768

4869
/**
4970
* Function used to deprecated_evaluate a node's condition using a rollback evaluation implementation.
@@ -54,18 +75,59 @@ interface RollbackCallback {
5475
* @param hand to be checked {@link Collection}
5576
* @return If the hand meets a condition
5677
*/
57-
default <E extends Reservable> boolean evaluate(final Collection<E> hand)
78+
public <E extends Reservable> boolean evaluate(final Collection<E> hand)
5879
{
5980
TestResult result = evaluate(hand, () -> TestResult.Success);
6081
return result == TestResult.Success;
6182
}
6283

84+
/**
85+
* Output whole tree in dot file format.
86+
*
87+
* @param START of breath first search
88+
*/
89+
public static String print_whole_subtree(final Evaluable<?> START)
90+
{
91+
StringBuilder output = new StringBuilder();
92+
Evaluable<?> placeholder;
93+
Collection<? extends Evaluable<?>> children;
94+
ArrayList<Evaluable<?>> traverse_nodes = new ArrayList<Evaluable<?>>();
95+
traverse_nodes.add(START);
96+
97+
output.append("digraph {\nnode [shape=record];\nnode [fontname=Sans];charset=\"UTF-8\" splines=true splines=spline rankdir =LR\n");
98+
99+
//children
100+
do
101+
{
102+
placeholder = traverse_nodes.get(0);
103+
output.append(placeholder.toString()); // print out top node
104+
105+
children = placeholder.continue_breath_search();
106+
if (children != null)
107+
traverse_nodes.addAll(children); // add children
108+
109+
traverse_nodes.remove(0); // remove first
110+
} while (!traverse_nodes.isEmpty());
111+
112+
output.append('}');
113+
114+
return output.toString();
115+
}
116+
117+
/**
118+
* Expected to be defined to pass along children for {@link #print_whole_subtree}.
119+
*
120+
* @return null or children
121+
*/
122+
abstract protected Collection<? extends Evaluable<T>> continue_breath_search();
123+
63124
/**
64125
* If debugMode is set, print current debug details about the currently executing node
65126
*
66127
* @param hand to be checked {@link Collection}
67128
*/
68-
default <E extends Reservable> void printDebugStep(final Collection<E> hand) {
129+
<E extends Reservable> void printDebugStep(final Collection<E> hand)
130+
{
69131
if (debugMode) {
70132
System.out.printf("%s ", this);
71133
Map<Boolean, List<E>> hand_partition = hand.stream().collect(Collectors.partitioningBy(Reservable::isReserved));

structure/Not_Operator_Node.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package structure;
22

3+
import java.util.ArrayList;
34
import java.util.Collection;
45

56
/**
@@ -32,4 +33,30 @@ public <E extends Reservable> TestResult evaluate(Collection<E> hand, RollbackCa
3233
// result should only ever be TestResult.Rollback here
3334
return next.call();
3435
}
36+
37+
/**
38+
* for dot format
39+
*/
40+
public String toString()
41+
{
42+
StringBuilder output = new StringBuilder(128);
43+
44+
output.append(super.toString()); //call standard part
45+
46+
//child
47+
output.append(this.UNIQUE_IDENTIFIER);
48+
output.append("->");
49+
output.append(this.CHILD.UNIQUE_IDENTIFIER);
50+
output.append(";\n");
51+
52+
return output.toString();
53+
}
54+
55+
@Override
56+
protected Collection<? extends Evaluable<T>> continue_breath_search()
57+
{
58+
Collection<Evaluable<T>> to_return = new ArrayList<Evaluable<T>>();
59+
to_return.add(this.CHILD);
60+
return to_return;
61+
}
3562
}

structure/Scenario.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</b>
1010
*/
1111

12-
public class Scenario<T> implements Evaluable<T>
12+
public class Scenario<T> extends Evaluable<T>
1313
{
1414
/**
1515
* Controls whether this should be displayed in final results.
@@ -53,4 +53,31 @@ public <E extends Reservable> TestResult evaluate(Collection<E> hand, RollbackCa
5353
printDebugStep(hand);
5454
return TREE_CONDITION.evaluate(hand, next);
5555
}
56+
57+
/**
58+
* for dot format
59+
*/
60+
public String toString()
61+
{
62+
StringBuilder output = new StringBuilder(128);
63+
64+
output.append(this.UNIQUE_IDENTIFIER);
65+
output.append("[label=\"");
66+
output.append(this.NAME.replace(">", "\\>").replace("<", "\\<").replace("\"", "\\\""));//escape certain characters
67+
output.append("\"];\n");
68+
69+
//child
70+
output.append(this.UNIQUE_IDENTIFIER);
71+
output.append("->");
72+
output.append(this.TREE_CONDITION.UNIQUE_IDENTIFIER);
73+
output.append(";\n");
74+
75+
return output.toString();
76+
}
77+
78+
@Override
79+
protected Collection<? extends Evaluable<T>> continue_breath_search()
80+
{
81+
return null;//TODO: consider changing to adding TREE_CONDITION.
82+
}
5683
}

0 commit comments

Comments
 (0)