Skip to content

Commit f1d17fa

Browse files
committed
Add support for one-of operation
1 parent 034dbd7 commit f1d17fa

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

rosetta-interpreter/src/main/java/com/regnosys/rosetta/interpreternew/visitors/RosettaInterpreterRosettaConstructorExpressionInterpreter.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.regnosys.rosetta.rosetta.expression.Necessity;
2828
import com.regnosys.rosetta.rosetta.expression.RosettaConstructorExpression;
2929
import com.regnosys.rosetta.rosetta.expression.impl.ChoiceOperationImpl;
30+
import com.regnosys.rosetta.rosetta.expression.impl.OneOfOperationImpl;
3031
import com.regnosys.rosetta.rosetta.RosettaCardinality;
3132
import com.regnosys.rosetta.rosetta.interpreter.RosettaInterpreterValue;
3233
import com.regnosys.rosetta.rosetta.simple.Attribute;
@@ -202,6 +203,18 @@ private String verifyConditions(List<Condition> conditions, List<RosettaInterpre
202203

203204
return verifyChoiceOperation(choice.getNecessity(), choiceAttributesName, attr);
204205
}
206+
if (c.getExpression().getClass().equals(OneOfOperationImpl.class)) {
207+
List<String> allAttributesNames = attr.stream()
208+
.map(RosettaInterpreterTypedFeatureValue::getName)
209+
.collect(Collectors.toList());
210+
211+
int nonEmptyCount = countPresentAttributes(allAttributesNames, attr);
212+
if (nonEmptyCount != 1) {
213+
return "One-of condition not followed. "
214+
+ "Exactly one attribute should be defined.";
215+
}
216+
217+
}
205218
}
206219

207220
//if no errors up until this point, return null

rosetta-interpreter/src/test/java/com/regnosys/rosetta/interpreternew/RosettaInterpreterConstructorExpressionTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,4 +399,52 @@ public void testDataTypeChoiceThreeDots() {
399399

400400
assertEquals("Ob", result.getName());
401401
}
402+
403+
@Test
404+
public void testDataTypeOneOfGood() {
405+
RosettaModel model = modelHelper.parseRosetta("type Ob:"
406+
+ "one int (0..1) two int (0..*)"
407+
+ "condition: one-of "
408+
+ "func M: output: result Ob (1..1) set result: Ob { one: 1 }");
409+
410+
RosettaConstructorExpressionImpl constructor = ((RosettaConstructorExpressionImpl) ((
411+
FunctionImpl) model.getElements().get(1)).getOperations().get(0).getExpression());
412+
RosettaInterpreterTypedValue result = (RosettaInterpreterTypedValue) interpreter.interp(constructor);
413+
414+
assertEquals("Ob", result.getName());
415+
}
416+
417+
@Test
418+
public void testDataTypeOneOfBad() {
419+
RosettaModel model = modelHelper.parseRosetta("type Ob:"
420+
+ "one int (0..1) two int (0..*)"
421+
+ "condition: one-of "
422+
+ "func M: output: result Ob (1..1) set result: Ob { ... }");
423+
424+
RosettaConstructorExpressionImpl constructor = ((RosettaConstructorExpressionImpl) ((
425+
FunctionImpl) model.getElements().get(1)).getOperations().get(0).getExpression());
426+
RosettaInterpreterValue result = interpreter.interp(constructor);
427+
428+
RosettaInterpreterErrorValue expected = new RosettaInterpreterErrorValue(new RosettaInterpreterError(
429+
"One-of condition not followed. Exactly one attribute should be defined."));
430+
431+
assertEquals(expected, result);
432+
}
433+
434+
@Test
435+
public void testDataTypeOneOfNonOptional() {
436+
RosettaModel model = modelHelper.parseRosetta("type Ob:"
437+
+ "one int (0..1) two int (0..*) three int (1..1) four int (1..*)"
438+
+ "condition: one-of "
439+
+ "func M: output: result Ob (1..1) set result: Ob { three: 3, four: [4,5] }");
440+
441+
RosettaConstructorExpressionImpl constructor = ((RosettaConstructorExpressionImpl) ((
442+
FunctionImpl) model.getElements().get(1)).getOperations().get(0).getExpression());
443+
RosettaInterpreterValue result = interpreter.interp(constructor);
444+
445+
RosettaInterpreterErrorValue expected = new RosettaInterpreterErrorValue(new RosettaInterpreterError(
446+
"One-of condition not followed. Exactly one attribute should be defined."));
447+
448+
assertEquals(expected, result);
449+
}
402450
}

0 commit comments

Comments
 (0)