Skip to content

Commit ee57124

Browse files
authored
First Draft of Rollback Implementation (#1)
* first draft of rollback implementation * clean up Leaf Node * minor fix * review based fixes * move comments out of function body * deal with intelliJ warnings * Working Tests * Working Tests
1 parent 7766e22 commit ee57124

16 files changed

+474
-163
lines changed

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,10 @@
33
.project
44
.classpath
55

6+
#JetBrains
7+
.idea/
8+
*.iml
9+
out/
10+
611
#Java
7-
*.class
12+
*.class

structure/And_Operator_Node.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,28 @@
55
/**
66
<b>
77
Purpose: And operator<br>
8-
Programmer: Gabriel Toban Harris <br>
8+
Programmer: Gabriel Toban Harris, Alexander Herman Oxorn <br>
99
Date: 2021-07-24
1010
</b>
1111
*/
1212

1313
public class And_Operator_Node<T> extends Binary_Operator_Node<T>
1414
{
1515
/**
16-
* Constructor, refer to {@link Binary_Operator_Node#Binary_Operator_Node(String, Evaluatable, Evaluatable)}.
16+
* Constructor, refer to {@link Binary_Operator_Node#Binary_Operator_Node(String, Evaluable, Evaluable)}.
1717
*/
18-
public And_Operator_Node(final Evaluatable<T> LEFT_CHILD, final Evaluatable<T> RIGHT_CHILD)
18+
public And_Operator_Node(final Evaluable<T> LEFT_CHILD, final Evaluable<T> RIGHT_CHILD)
1919
{
2020
super("AND", LEFT_CHILD, RIGHT_CHILD);
2121
}
2222

23+
// If LEFT_CHILD can take card(s), then make sure the RIGHT CHILD can also take card(s)
2324
@Override
24-
public <E extends Collection<T>> boolean evaluate(E hand)
25-
{
26-
if (!super.evaluated)
27-
{
28-
super.result = super.LEFT_CHILD.evaluate(hand) && super.RIGHT_CHILD.evaluate(hand);
29-
super.evaluated = true;
30-
}
31-
32-
return super.result;
25+
public <E extends Reservable> TestResult evaluate(Collection<E> hand, RollbackCallback next) {
26+
printDebugStep(hand);
27+
return LEFT_CHILD.evaluate(
28+
hand,
29+
() -> RIGHT_CHILD.evaluate(hand, next)
30+
);
3331
}
3432
}

structure/Base_Card.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public class Base_Card
2222
*/
2323
protected final int HASH_CODE;
2424

25+
protected boolean reserved;
26+
2527
/**
2628
* Constructor.
2729
*
@@ -42,20 +44,21 @@ public Base_Card(final Base_Card CARD)
4244
{
4345
this.NAME = CARD.NAME;
4446
this.HASH_CODE = CARD.HASH_CODE;
47+
this.reserved = false;
4548
}
4649

4750
@Override
4851
public boolean equals(final Object INPUT)
4952
{
5053
if (INPUT instanceof Base_Card)
51-
return this.equals(INPUT);
54+
return this.equals((Base_Card) INPUT);
5255
else
5356
return false;
5457
}
5558

5659
/**
5760
* Function to check card equality.
58-
*
61+
*
5962
* @param <T> anything which either is or extends, {@link Base_Card}.
6063
* @param CARD to be compared
6164
* @return true for equivalent, otherwise false

structure/Base_Node.java

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,20 @@
1010

1111
//TODO: add toString() methods with some reference to outputting as dot file format.
1212
//TODO: figure out edge cases in evaluation for both duplication (maybe paint) and overlap (maybe attempt condition evaluation in different combinations).
13-
public abstract class Base_Node<T> implements Evaluatable<T>
13+
public abstract class Base_Node<T> implements Evaluable<T>
1414
{
1515
/**
1616
* Unique identifier for this node.
1717
*/
18-
public final int UNIQUE_IDENTIFER;
18+
public final int UNIQUE_IDENTIFIER;
1919

2020
/**
2121
* The name of this node.
2222
*/
2323
public final String NAME;
2424

2525
/**
26-
* Used to prevent repeated evaluations.
27-
*/
28-
protected boolean evaluated = false;
29-
30-
/**
31-
* Used to store result.
32-
*/
33-
protected boolean result;
34-
35-
/**
36-
* Used to set {@link #UNIQUE_IDENTIFER}
26+
* Used to set {@link #UNIQUE_IDENTIFIER}
3727
*/
3828
private static int CREATED_NODES_COUNT = 0;
3929

@@ -44,12 +34,6 @@ public abstract class Base_Node<T> implements Evaluatable<T>
4434
public Base_Node(final String NAME)
4535
{
4636
this.NAME = NAME;
47-
this.UNIQUE_IDENTIFER = ++CREATED_NODES_COUNT;
48-
}
49-
50-
@Override
51-
public void reset()
52-
{
53-
this.evaluated = false;
37+
this.UNIQUE_IDENTIFIER = ++CREATED_NODES_COUNT;
5438
}
5539
}

structure/Binary_Operator_Node.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ public abstract class Binary_Operator_Node<T> extends Base_Node<T>
1414
/**
1515
* Left operand.
1616
*/
17-
public final Evaluatable<T> LEFT_CHILD;
17+
public final Evaluable<T> LEFT_CHILD;
1818

1919
/**
2020
* Right operand.
2121
*/
22-
public final Evaluatable<T> RIGHT_CHILD;
22+
public final Evaluable<T> RIGHT_CHILD;
2323

2424
/**
2525
* Constructor.
@@ -28,21 +28,10 @@ public abstract class Binary_Operator_Node<T> extends Base_Node<T>
2828
* @param LEFT_CHILD is the left operand of the operator
2929
* @param RIGHT_CHILD is the right operand of the operator
3030
*/
31-
public Binary_Operator_Node(String NAME, final Evaluatable<T> LEFT_CHILD, final Evaluatable<T> RIGHT_CHILD)
31+
public Binary_Operator_Node(String NAME, final Evaluable<T> LEFT_CHILD, final Evaluable<T> RIGHT_CHILD)
3232
{
3333
super(NAME);
3434
this.LEFT_CHILD = LEFT_CHILD;
3535
this.RIGHT_CHILD = RIGHT_CHILD;
3636
}
37-
38-
/**
39-
* Resets self and children for next hand.
40-
*/
41-
@Override
42-
public void reset()
43-
{
44-
super.reset();
45-
this.LEFT_CHILD.reset();
46-
this.RIGHT_CHILD.reset();
47-
}
4837
}

structure/Deck_Card.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,13 @@
33
/**
44
<b>
55
Purpose: To be used as the representation of cards in a deck.<br>
6-
Programmer: Gabriel Toban Harris <br>
6+
Programmer: Gabriel Toban Harris, Alexander Herman Oxorn <br>
77
Date: 2021-07-25
88
</b>
99
*/
1010

11-
public class Deck_Card extends Base_Card
11+
public class Deck_Card extends Base_Card implements Reservable
1212
{
13-
/**
14-
* For classes in {@link structure} to use to know that a card has already been allocated.
15-
*/
16-
public boolean used;
17-
1813
/**
1914
* See {@link Base_Card#Base_Card(String)}
2015
*/
@@ -31,13 +26,29 @@ public Deck_Card(final String NAME)
3126
public Deck_Card(final Deck_Card INPUT)
3227
{
3328
super(INPUT);
29+
this.reserved = false;
30+
}
31+
32+
@Override
33+
public boolean isReserved() {
34+
return reserved;
3435
}
3536

3637
/**
37-
* Calls {@link #equals(Base_Card)} and then sets {@link #used} to that value. Meant to be convenience method for {@link structure}.
38+
* Calls {@link #equals(Base_Card)} and then sets {@link #reserved} to that value. Meant to be convenience method for {@link structure}.
3839
*/
3940
public <T extends Base_Card> boolean convenience_comparison(final T CARD)
4041
{
41-
return this.used = this.equals(CARD);
42+
return this.reserved = this.equals(CARD);
43+
}
44+
45+
@Override
46+
public void reserve() {
47+
reserved = true;
48+
}
49+
50+
@Override
51+
public void release() {
52+
reserved = false;
4253
}
4354
}

structure/Evaluable.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package structure;
2+
3+
import java.util.Collection;
4+
import java.util.List;
5+
import java.util.Map;
6+
import java.util.stream.Collectors;
7+
8+
/**
9+
<b>
10+
Purpose: Requirement to be a node for evaluation purposes.<br>
11+
Programmer: Gabriel Toban Harris, Alexander Herman Oxorn <br>
12+
Date: 2021-07-24
13+
</b>
14+
*/
15+
16+
public interface Evaluable<T>
17+
{
18+
boolean debugMode = false;
19+
20+
enum TestResult {
21+
/**
22+
* The evaluation succeeded
23+
*/
24+
Success,
25+
/**
26+
* The evaluation of the child of NOT succeeded
27+
*/
28+
NotSuccess,
29+
/**
30+
* Evaluation failed given reserved cards
31+
*/
32+
Rollback
33+
}
34+
35+
interface RollbackCallback {
36+
TestResult call();
37+
}
38+
39+
/**
40+
* Function used to evaluate a node's condition using a rollback evaluation implementation.
41+
* Allows for hand to be in an arbitrary order
42+
*
43+
* @param hand to be checked {@link Collection}
44+
* @param next function to call when a leaf node takes a card from the hand
45+
* @return a {@link TestResult} used as a signal on what action to preform next
46+
*/
47+
<E extends Reservable> TestResult evaluate(final Collection<E> hand, final RollbackCallback next);
48+
49+
/**
50+
* Function used to deprecated_evaluate a node's condition using a rollback evaluation implementation.
51+
* Allows for hand to be in an arbitrary order
52+
*
53+
* Default entry point where the success callback returns true and the failure callback returns false
54+
*
55+
* @param hand to be checked {@link Collection}
56+
* @return If the hand meets a condition
57+
*/
58+
default <E extends Reservable> boolean evaluate(final Collection<E> hand)
59+
{
60+
TestResult result = evaluate(hand, () -> TestResult.Success);
61+
return result == TestResult.Success;
62+
}
63+
64+
/**
65+
* If debugMode is set, print current debug details about the currently executing node
66+
*
67+
* @param hand to be checked {@link Collection}
68+
*/
69+
default <E extends Reservable> void printDebugStep(final Collection<E> hand) {
70+
if (debugMode) {
71+
System.out.printf("%s ", this);
72+
Map<Boolean, List<E>> hand_partition = hand.stream().collect(Collectors.partitioningBy(Reservable::isReserved));
73+
System.out.printf("Used Cards: [%s] ", hand_partition.get(true).stream().map(Object::toString).collect(Collectors.joining(",")));
74+
System.out.printf("Unused Cards: [%s]\n", hand_partition.get(false).stream().map(Object::toString).collect(Collectors.joining(",")));
75+
}
76+
}
77+
}

structure/Evaluatable.java

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)