|
| 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 | +} |
0 commit comments