Skip to content

Commit 5309908

Browse files
authored
Merge pull request #867 from VolodymyrLinuxovich/operation-type-855
Add OperationType to identify operations in ProgramValidator
2 parents 82b97b9 + 152f719 commit 5309908

8 files changed

Lines changed: 136 additions & 4 deletions

File tree

‎engine/src/main/java/org/kigalisim/lang/operation/ChangeOperation.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,10 @@ public void execute(PushDownMachine machine) {
7979
public String getStream() {
8080
return stream;
8181
}
82+
83+
/** {@inheritDoc} */
84+
@Override
85+
public OperationType getOperationType() {
86+
return OperationType.CHANGE;
87+
}
8288
}

‎engine/src/main/java/org/kigalisim/lang/operation/Operation.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,13 @@ public interface Operation {
2121
*/
2222
void execute(PushDownMachine machine);
2323

24+
/**
25+
* Get the kind of this operation.
26+
*
27+
* @return The type of this operation or OTHER if not specified.
28+
*/
29+
default OperationType getOperationType() {
30+
return OperationType.OTHER;
31+
}
32+
2433
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Enum describing the kind of an operation.
3+
*
4+
* <p>This enum lets code which inspects a parsed program, like validators, identify an
5+
* operation without checking its concrete class.</p>
6+
*
7+
* @license BSD-3-Clause
8+
*/
9+
10+
package org.kigalisim.lang.operation;
11+
12+
/**
13+
* Enum describing the kind of an operation.
14+
*/
15+
public enum OperationType {
16+
/** Retirement following a Weibull survival curve. */
17+
RETIRE_WEIBULL,
18+
/** Retirement of the cohort at an exact age. */
19+
RETIRE_EXACT,
20+
/** Set of a stream value. */
21+
SET,
22+
/** Change of a stream value. */
23+
CHANGE,
24+
/** Any other operation. */
25+
OTHER
26+
}

‎engine/src/main/java/org/kigalisim/lang/operation/RetireExactOperation.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ public boolean getAssumingNew() {
8787
return assumingNew;
8888
}
8989

90+
/** {@inheritDoc} */
91+
@Override
92+
public OperationType getOperationType() {
93+
return OperationType.RETIRE_EXACT;
94+
}
95+
9096
/**
9197
* Execute the exact retire operation on the given push-down machine.
9298
*

‎engine/src/main/java/org/kigalisim/lang/operation/RetireWeibullOperation.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ public boolean getAssumingNew() {
8181
return assumingNew;
8282
}
8383

84+
/** {@inheritDoc} */
85+
@Override
86+
public OperationType getOperationType() {
87+
return OperationType.RETIRE_WEIBULL;
88+
}
89+
8490
/**
8591
* Execute the Weibull retire operation on the given push-down machine.
8692
*

‎engine/src/main/java/org/kigalisim/lang/operation/SetOperation.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,10 @@ public void execute(PushDownMachine machine) {
8080
public String getStream() {
8181
return stream;
8282
}
83+
84+
/** {@inheritDoc} */
85+
@Override
86+
public OperationType getOperationType() {
87+
return OperationType.SET;
88+
}
8389
}

‎engine/src/main/java/org/kigalisim/lang/program/ProgramValidator.java‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.Map;
1313
import org.kigalisim.lang.operation.ChangeOperation;
1414
import org.kigalisim.lang.operation.Operation;
15+
import org.kigalisim.lang.operation.OperationType;
1516
import org.kigalisim.lang.operation.RetireExactOperation;
1617
import org.kigalisim.lang.operation.RetireWeibullOperation;
1718
import org.kigalisim.lang.operation.SetOperation;
@@ -133,16 +134,21 @@ private static void validateOperations(Iterable<Operation> operations) {
133134
boolean setsPriorEquipment = false;
134135

135136
for (Operation operation : operations) {
136-
if (operation instanceof RetireWeibullOperation weibull && !weibull.getAssumingNew()) {
137+
OperationType operationType = operation.getOperationType();
138+
if (operationType == OperationType.RETIRE_WEIBULL
139+
&& !((RetireWeibullOperation) operation).getAssumingNew()) {
137140
hasWeibullWithoutAssumingNew = true;
138141
}
139-
if (operation instanceof RetireExactOperation exact && !exact.getAssumingNew()) {
142+
if (operationType == OperationType.RETIRE_EXACT
143+
&& !((RetireExactOperation) operation).getAssumingNew()) {
140144
hasExactRetireWithoutAssumingNew = true;
141145
}
142-
if (operation instanceof SetOperation set && "priorEquipment".equals(set.getStream())) {
146+
if (operationType == OperationType.SET
147+
&& "priorEquipment".equals(((SetOperation) operation).getStream())) {
143148
setsPriorEquipment = true;
144149
}
145-
if (operation instanceof ChangeOperation change && "priorEquipment".equals(change.getStream())) {
150+
if (operationType == OperationType.CHANGE
151+
&& "priorEquipment".equals(((ChangeOperation) operation).getStream())) {
146152
setsPriorEquipment = true;
147153
}
148154
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Unit tests for the OperationType reported by operations.
3+
*
4+
* @license BSD-3-Clause
5+
*/
6+
7+
package org.kigalisim.lang.operation;
8+
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
11+
import java.math.BigDecimal;
12+
import org.junit.jupiter.api.Test;
13+
import org.kigalisim.engine.number.EngineNumber;
14+
15+
/**
16+
* Tests for the OperationType reported by operations.
17+
*/
18+
public class OperationTypeTest {
19+
20+
private final Operation valueOperation = new PreCalculatedOperation(
21+
new EngineNumber(BigDecimal.ONE, "units")
22+
);
23+
24+
/**
25+
* Test that a Weibull retire operation reports RETIRE_WEIBULL.
26+
*/
27+
@Test
28+
public void testRetireWeibullType() {
29+
Operation operation = new RetireWeibullOperation(BigDecimal.TEN, false, false);
30+
assertEquals(OperationType.RETIRE_WEIBULL, operation.getOperationType());
31+
}
32+
33+
/**
34+
* Test that an exact retire operation reports RETIRE_EXACT.
35+
*/
36+
@Test
37+
public void testRetireExactType() {
38+
Operation operation = new RetireExactOperation(valueOperation, 5, false, false);
39+
assertEquals(OperationType.RETIRE_EXACT, operation.getOperationType());
40+
}
41+
42+
/**
43+
* Test that a set operation reports SET.
44+
*/
45+
@Test
46+
public void testSetType() {
47+
Operation operation = new SetOperation("priorEquipment", valueOperation);
48+
assertEquals(OperationType.SET, operation.getOperationType());
49+
}
50+
51+
/**
52+
* Test that a change operation reports CHANGE.
53+
*/
54+
@Test
55+
public void testChangeType() {
56+
Operation operation = new ChangeOperation("priorEquipment", valueOperation);
57+
assertEquals(OperationType.CHANGE, operation.getOperationType());
58+
}
59+
60+
/**
61+
* Test that operations without a specific type report OTHER.
62+
*/
63+
@Test
64+
public void testDefaultType() {
65+
assertEquals(OperationType.OTHER, valueOperation.getOperationType());
66+
}
67+
}

0 commit comments

Comments
 (0)