1 | package com.github.dakusui.pcond.core; | |
2 | ||
3 | import java.util.ArrayList; | |
4 | import java.util.LinkedList; | |
5 | import java.util.List; | |
6 | import java.util.function.BiFunction; | |
7 | import java.util.function.Function; | |
8 | ||
9 | import static com.github.dakusui.pcond.core.EvaluationEntry.Type.*; | |
10 | import static java.util.Objects.requireNonNull; | |
11 | ||
12 | /** | |
13 | * The new design: | |
14 | * | |
15 | * Evaluator: Concentrates on "evaluate" an individual evaluable (form). No aware of how to compose evaluation entries. | |
16 | */ | |
17 | ||
18 | public class EvaluationContext<T> { | |
19 | final List<EvaluationEntry> evaluationEntries = new LinkedList<>(); | |
20 | final List<EvaluationEntry> visitorLineage = new LinkedList<>(); | |
21 | ||
22 | boolean expectationFlipped = false; | |
23 | ||
24 | public EvaluationContext() { | |
25 | } | |
26 | ||
27 | public EvaluationContext(EvaluationContext<?> parent) { | |
28 | this.expectationFlipped = parent.isExpectationFlipped(); | |
29 | } | |
30 | ||
31 | /** | |
32 | * @param evaluableIo An object to hold a form and its I/O. | |
33 | * @param evaluatorCallback A callback that executes a logic specific to the {@code evaluable}. | |
34 | */ | |
35 | public <E extends Evaluable<T>, O> void evaluate(EvaluableIo<T, E, O> evaluableIo, BiFunction<E, ValueHolder<T>, ValueHolder<O>> evaluatorCallback) { | |
36 |
1
1. evaluate : removed call to com/github/dakusui/pcond/core/EvaluationContext::evaluate → KILLED |
evaluate(resolveEvaluationEntryType(evaluableIo.evaluable()), evaluableIo, evaluatorCallback); |
37 | } | |
38 | ||
39 | public <E extends Evaluable<T>, O> void evaluate(EvaluationEntry.Type type, EvaluableIo<T, E, O> evaluableIo, BiFunction<E, ValueHolder<T>, ValueHolder<O>> evaluatorCallback) { | |
40 |
2
1. evaluate : removed call to com/github/dakusui/pcond/core/EvaluationContext::evaluate → KILLED 2. lambda$evaluate$0 : replaced return value with null for com/github/dakusui/pcond/core/EvaluationContext::lambda$evaluate$0 → KILLED |
evaluate(type, evaluableIo, io -> evaluatorCallback.apply(io.evaluable(), io.input())); |
41 | } | |
42 | ||
43 | public <E extends Evaluable<T>, O> void evaluate(EvaluationEntry.Type type, EvaluableIo<T, E, O> evaluableIo, Function<EvaluableIo<T, E, O>, ValueHolder<O>> function) { | |
44 |
1
1. evaluate : removed call to com/github/dakusui/pcond/core/EvaluationContext::evaluate → KILLED |
evaluate(type, formNameOf(evaluableIo), evaluableIo, function); |
45 | } | |
46 | ||
47 | public <E extends Evaluable<T>, O> void evaluate(EvaluationEntry.Type type, String formName, EvaluableIo<T, E, O> evaluableIo, Function<EvaluableIo<T, E, O>, ValueHolder<O>> function) { | |
48 | requireNonNull(evaluableIo); | |
49 | EvaluableIo<T, E, O> evaluableIoWork = this.enter(evaluableIo.input(), type, formName, evaluableIo.evaluable()); | |
50 |
1
1. evaluate : removed call to com/github/dakusui/pcond/core/EvaluationContext::leave → KILLED |
this.leave(evaluableIoWork, function.apply(evaluableIoWork)); |
51 |
1
1. evaluate : removed call to com/github/dakusui/pcond/core/DebuggingUtils::printTo → SURVIVED |
DebuggingUtils.printTo(this, System.err, 1); |
52 |
1
1. evaluate : removed call to com/github/dakusui/pcond/core/EvaluationContext::updateEvaluableIo → KILLED |
updateEvaluableIo(evaluableIo, evaluableIoWork); |
53 | } | |
54 | ||
55 | public static String formNameOf(EvaluableIo<?, ?, ?> evaluableIo) { | |
56 |
1
1. formNameOf : replaced return value with "" for com/github/dakusui/pcond/core/EvaluationContext::formNameOf → KILLED |
return formNameOf(evaluableIo.evaluableType(), evaluableIo.evaluable()); |
57 | } | |
58 | ||
59 | public static String formNameOf(EvaluationEntry.Type type, Evaluable<?> e) { | |
60 |
1
1. formNameOf : replaced return value with "" for com/github/dakusui/pcond/core/EvaluationContext::formNameOf → KILLED |
return type.formName(e); |
61 | } | |
62 | ||
63 | public boolean isExpectationFlipped() { | |
64 |
2
1. isExpectationFlipped : replaced boolean return with false for com/github/dakusui/pcond/core/EvaluationContext::isExpectationFlipped → KILLED 2. isExpectationFlipped : replaced boolean return with true for com/github/dakusui/pcond/core/EvaluationContext::isExpectationFlipped → KILLED |
return this.expectationFlipped; |
65 | } | |
66 | ||
67 | public void flipExpectation() { | |
68 |
1
1. flipExpectation : negated conditional → KILLED |
this.expectationFlipped = !expectationFlipped; |
69 | } | |
70 | ||
71 | private static <T, E extends Evaluable<T>, O> void updateEvaluableIo(EvaluableIo<T, E, O> evaluableIo, EvaluableIo<T, E, O> evaluableIoWork) { | |
72 |
1
1. updateEvaluableIo : removed call to com/github/dakusui/pcond/core/EvaluableIo::output → KILLED |
evaluableIo.output(evaluableIoWork.output()); |
73 | } | |
74 | ||
75 | public static <T> EvaluationEntry.Type resolveEvaluationEntryType(Evaluable<T> evaluable) { | |
76 |
3
1. resolveEvaluationEntryType : negated conditional → KILLED 2. resolveEvaluationEntryType : negated conditional → KILLED 3. resolveEvaluationEntryType : negated conditional → KILLED |
if (evaluable instanceof Evaluable.LeafPred || evaluable instanceof Evaluable.CurriedContextPred || evaluable instanceof Evaluable.StreamPred) |
77 |
1
1. resolveEvaluationEntryType : replaced return value with null for com/github/dakusui/pcond/core/EvaluationContext::resolveEvaluationEntryType → KILLED |
return LEAF; |
78 |
1
1. resolveEvaluationEntryType : negated conditional → KILLED |
if (evaluable instanceof Evaluable.Func) |
79 |
1
1. resolveEvaluationEntryType : replaced return value with null for com/github/dakusui/pcond/core/EvaluationContext::resolveEvaluationEntryType → KILLED |
return FUNCTION; |
80 |
1
1. resolveEvaluationEntryType : negated conditional → KILLED |
if (evaluable instanceof Evaluable.Conjunction) |
81 |
1
1. resolveEvaluationEntryType : replaced return value with null for com/github/dakusui/pcond/core/EvaluationContext::resolveEvaluationEntryType → KILLED |
return AND; |
82 |
1
1. resolveEvaluationEntryType : negated conditional → KILLED |
if (evaluable instanceof Evaluable.Disjunction) |
83 |
1
1. resolveEvaluationEntryType : replaced return value with null for com/github/dakusui/pcond/core/EvaluationContext::resolveEvaluationEntryType → KILLED |
return OR; |
84 |
1
1. resolveEvaluationEntryType : negated conditional → KILLED |
if (evaluable instanceof Evaluable.Negation) |
85 |
1
1. resolveEvaluationEntryType : replaced return value with null for com/github/dakusui/pcond/core/EvaluationContext::resolveEvaluationEntryType → KILLED |
return NOT; |
86 |
1
1. resolveEvaluationEntryType : negated conditional → KILLED |
if (evaluable instanceof Evaluable.Transformation) |
87 |
1
1. resolveEvaluationEntryType : replaced return value with null for com/github/dakusui/pcond/core/EvaluationContext::resolveEvaluationEntryType → KILLED |
return TRANSFORM_AND_CHECK; |
88 | throw new IllegalArgumentException(); | |
89 | } | |
90 | ||
91 | @SuppressWarnings("unchecked") | |
92 | private <E extends Evaluable<T>, O> EvaluableIo<T, E, O> enter(ValueHolder<T> input, EvaluationEntry.Type type, String formName, E evaluable) { | |
93 | EvaluableIo<T, Evaluable<T>, O> ret = createEvaluableIo(input, type, formName, evaluable); | |
94 | this.evaluationEntries.add(createEvaluationEntry(this, ret)); | |
95 |
1
1. enter : Replaced integer subtraction with addition → KILLED |
this.visitorLineage.add(evaluationEntries.get(evaluationEntries.size() - 1)); |
96 |
1
1. enter : replaced return value with null for com/github/dakusui/pcond/core/EvaluationContext::enter → KILLED |
return (EvaluableIo<T, E, O>) ret; |
97 | } | |
98 | ||
99 | private <E extends Evaluable<T>, O> void leave(EvaluableIo<T, E, O> evaluableIo, ValueHolder<O> output) { | |
100 |
1
1. leave : Replaced integer subtraction with addition → KILLED |
EvaluationEntry.Impl currentEvaluationEntry = (EvaluationEntry.Impl) this.visitorLineage.remove(this.visitorLineage.size() - 1); |
101 |
1
1. leave : removed call to com/github/dakusui/pcond/core/EvaluableIo::output → KILLED |
evaluableIo.output(output); |
102 |
1
1. leave : removed call to com/github/dakusui/pcond/core/EvaluationEntry$Impl::finalizeValues → KILLED |
currentEvaluationEntry.finalizeValues(); |
103 | } | |
104 | ||
105 | private static <T, O> EvaluableIo<T, Evaluable<T>, O> createEvaluableIo(ValueHolder<T> input, EvaluationEntry.Type type, String formName, Evaluable<T> evaluable) { | |
106 |
1
1. createEvaluableIo : replaced return value with null for com/github/dakusui/pcond/core/EvaluationContext::createEvaluableIo → KILLED |
return new EvaluableIo<>(input, type, formName, evaluable); |
107 | } | |
108 | ||
109 | private static <T, E extends Evaluable<T>> EvaluationEntry createEvaluationEntry( | |
110 | EvaluationContext<T> evaluationContext, | |
111 | EvaluableIo<T, E, ?> evaluableIo) { | |
112 |
1
1. createEvaluationEntry : replaced return value with null for com/github/dakusui/pcond/core/EvaluationContext::createEvaluationEntry → KILLED |
return new EvaluationEntry.Impl(evaluationContext, evaluableIo); |
113 | } | |
114 | ||
115 | public List<EvaluationEntry> resultEntries() { | |
116 |
1
1. resultEntries : replaced return value with Collections.emptyList for com/github/dakusui/pcond/core/EvaluationContext::resultEntries → KILLED |
return new ArrayList<>(this.evaluationEntries); |
117 | } | |
118 | ||
119 | public <R> void importEntries(EvaluationContext<R> childContext) { | |
120 |
1
1. importEntries : removed call to com/github/dakusui/pcond/core/EvaluationContext::importEntries → KILLED |
importEntries(childContext, currentIndentLevel()); |
121 | } | |
122 | ||
123 | public <R> void importEntries(EvaluationContext<R> childContext, int indentLevelGap) { | |
124 |
2
1. importEntries : removed call to java/util/List::forEach → SURVIVED 2. lambda$importEntries$1 : Replaced integer addition with subtraction → SURVIVED |
childContext.evaluationEntries.forEach(each -> each.level += indentLevelGap); |
125 | this.evaluationEntries.addAll(childContext.resultEntries()); | |
126 | } | |
127 | ||
128 | public int currentIndentLevel() { | |
129 |
1
1. currentIndentLevel : replaced int return with 0 for com/github/dakusui/pcond/core/EvaluationContext::currentIndentLevel → SURVIVED |
return this.visitorLineage.size(); |
130 | } | |
131 | } | |
Mutations | ||
36 |
1.1 |
|
40 |
1.1 2.2 |
|
44 |
1.1 |
|
50 |
1.1 |
|
51 |
1.1 |
|
52 |
1.1 |
|
56 |
1.1 |
|
60 |
1.1 |
|
64 |
1.1 2.2 |
|
68 |
1.1 |
|
72 |
1.1 |
|
76 |
1.1 2.2 3.3 |
|
77 |
1.1 |
|
78 |
1.1 |
|
79 |
1.1 |
|
80 |
1.1 |
|
81 |
1.1 |
|
82 |
1.1 |
|
83 |
1.1 |
|
84 |
1.1 |
|
85 |
1.1 |
|
86 |
1.1 |
|
87 |
1.1 |
|
95 |
1.1 |
|
96 |
1.1 |
|
100 |
1.1 |
|
101 |
1.1 |
|
102 |
1.1 |
|
106 |
1.1 |
|
112 |
1.1 |
|
116 |
1.1 |
|
120 |
1.1 |
|
124 |
1.1 2.2 |
|
129 |
1.1 |