Skip to content

Commit

Permalink
Fix inoutvariables not being processed in SubmodelService/Repository …
Browse files Browse the repository at this point in the history
…Controllers :: invokeOperation (eclipse-basyx#396)

* Fix inoutvariables not being processed in controller invokeOperation(...)

* Apply the fix for the SubmodelRepositoryApiHttpController
  • Loading branch information
mateusmolina-iese authored Aug 28, 2024
1 parent b34d232 commit 1f06ab7
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,28 @@ private ResponseEntity<SubmodelElement> handleSubmodelElementValueNormalGetReque

@Override
public ResponseEntity<OperationResult> invokeOperationSubmodelRepo(Base64UrlEncodedIdentifier submodelIdentifier, String idShortPath, @Valid OperationRequest body, @Valid Boolean async) {
OperationVariable[] result = repository.invokeOperation(submodelIdentifier.getIdentifier(), idShortPath, body.getInputArguments().toArray(new OperationVariable[0]));
List<OperationVariable> inVars = new ArrayList<>();
inVars.addAll(body.getInputArguments());
inVars.addAll(body.getInoutputArguments());

return new ResponseEntity<OperationResult>(createOperationResult(result), HttpStatus.OK);
List<OperationVariable> result = Arrays.asList(repository.invokeOperation(submodelIdentifier.getIdentifier(), idShortPath, inVars.toArray(new OperationVariable[0])));

List<OperationVariable> outVars = new ArrayList<>(result);
List<OperationVariable> inoutputVars = new ArrayList<>();

if (!body.getInoutputArguments().isEmpty()) {
List<String> inoutputVarsIdShorts = body.getInoutputArguments().stream().map(OperationVariable::getValue).map(SubmodelElement::getIdShort).toList();

inoutputVars = result.stream().filter(opVar -> inoutputVarsIdShorts.contains(opVar.getValue().getIdShort())).toList();

outVars.removeAll(inoutputVars);
}

return ResponseEntity.ok(createOperationResult(outVars, inoutputVars));
}

private OperationResult createOperationResult(OperationVariable[] result) {
return new DefaultOperationResult.Builder().outputArguments(Arrays.asList(result)).build();
private OperationResult createOperationResult(List<OperationVariable> outputVars, List<OperationVariable> inoutputVars) {
return new DefaultOperationResult.Builder().outputArguments(outputVars).inoutputArguments(inoutputVars).build();
}

private String getEncodedCursorFromCursorResult(CursorResult<?> cursorResult) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ public class SubmodelServiceHelper {
public static final String SUBMODEL_TECHNICAL_DATA_SUBMODEL_ELEMENT_LIST_CATEGORY = "PARAMETER";

public static final String SUBMODEL_TECHNICAL_DATA_OPERATION_ID = "square";
public static final String SUBMODEL_TECHNICAL_DATA_OPERATIONINOUT_ID = "sum";

public static SubmodelElement getDummySubmodelElement(Submodel technicalData, String idShort) {
return technicalData.getSubmodelElements()
Expand Down Expand Up @@ -333,7 +334,7 @@ public static List<SubmodelElement> getAllSubmodelElements() {
Lists.newArrayList(createPropertySubmodelElement(), createRangeSubmodelElement(), createMultiLanguagePropertySubmodelElement(), createFileSubmodelElement(), createEntitySubmodelElement(),
createReferenceElementSubmodelElement(),
createRelationshipElementSubmodelElement(), createAnnotatedRelationshipElementSubmodelElement(), createBlobSubmodelElement(), createSubmodelElementCollection(), createSubmodelElementList(),
createInvokableOperation()));
createInvokableOperation(), createInvokableInOutOperation()));
return list;
}

Expand Down Expand Up @@ -370,6 +371,11 @@ private static Operation createInvokableOperation() {
.invokable(SubmodelServiceHelper::square).build();
}

private static Operation createInvokableInOutOperation() {
return new InvokableOperation.Builder().idShort(SUBMODEL_TECHNICAL_DATA_OPERATIONINOUT_ID).inputVariables(createIntOperationVariable("input")).inoutputVariables(createIntOperationVariable("stack"))
.invokable(SubmodelServiceHelper::sum).build();
}

private static Operation createOperation() {
return new DefaultOperation.Builder().idShort(SUBMODEL_TECHNICAL_DATA_OPERATION_ID).inputVariables(createIntOperationVariable("input")).outputVariables(createIntOperationVariable("result"))
.build();
Expand All @@ -385,6 +391,16 @@ private static OperationVariable[] square(OperationVariable[] inputs) {
return new OperationVariable[] { createOperationVariable(in) };
}

private static OperationVariable[] sum(OperationVariable[] inputs) {
Property in = (Property) inputs[0].getValue();
Property stack = (Property) inputs[1].getValue();
Integer inVal = Integer.valueOf(in.getValue());
Integer stackVal = Integer.valueOf(stack.getValue());
Integer sumResult = inVal + stackVal;
stack.setValue(sumResult.toString());
return new OperationVariable[] { createOperationVariable(stack) };
}

private static DefaultOperationVariable createIntOperationVariable(String idShort) {
return new DefaultOperationVariable.Builder().value(new DefaultProperty.Builder().idShort(idShort).valueType(DataTypeDefXsd.INT).build()).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -236,15 +237,29 @@ public ResponseEntity<Void> patchSubmodelValueOnly(@Parameter(in = ParameterIn.D
public ResponseEntity<OperationResult> invokeOperation(
@Parameter(in = ParameterIn.PATH, description = "IdShort path to the submodel element (dot-separated)", required = true, schema = @Schema()) @PathVariable("idShortPath") String idShortPath,
@Parameter(in = ParameterIn.DEFAULT, description = "Operation request object", required = true, schema = @Schema()) @Valid @RequestBody OperationRequest body) {
OperationVariable[] result = service.invokeOperation(idShortPath, body.getInputArguments().toArray(new OperationVariable[0]));
List<OperationVariable> inVars = new ArrayList<>();
inVars.addAll(body.getInputArguments());
inVars.addAll(body.getInoutputArguments());

return new ResponseEntity<OperationResult>(createOperationResult(result), HttpStatus.OK);
List<OperationVariable> result = Arrays.asList(service.invokeOperation(idShortPath, inVars.toArray(new OperationVariable[0])));

List<OperationVariable> outVars = new ArrayList<>(result);
List<OperationVariable> inoutputVars = new ArrayList<>();

if (!body.getInoutputArguments().isEmpty()) {
List<String> inoutputVarsIdShorts = body.getInoutputArguments().stream().map(OperationVariable::getValue).map(SubmodelElement::getIdShort).toList();

inoutputVars = result.stream().filter(opVar -> inoutputVarsIdShorts.contains(opVar.getValue().getIdShort())).toList();

outVars.removeAll(inoutputVars);
}

return ResponseEntity.ok(createOperationResult(outVars, inoutputVars));
}

private OperationResult createOperationResult(OperationVariable[] result) {
private OperationResult createOperationResult(List<OperationVariable> outputVars, List<OperationVariable> inoutputVars) {
return new DefaultOperationResult.Builder()
.outputArguments(Arrays.asList(result))
.outputArguments(outputVars).inoutputArguments(inoutputVars)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,17 @@ public void invokeOperation() throws FileNotFoundException, IOException, ParseEx

}

@Test
public void invokeInOutOperation() throws IOException, ParseException {
String parameters = getJSONValueAsString("operation/parameters-inout.json");
CloseableHttpResponse response = requestOperationInvocation(SubmodelServiceHelper.SUBMODEL_TECHNICAL_DATA_OPERATIONINOUT_ID, parameters);

assertEquals(HttpStatus.OK.value(), response.getCode());
String expectedValue = getJSONValueAsString("operation/result-inout.json");
BaSyxHttpTestUtils.assertSameJSONContent(expectedValue, BaSyxHttpTestUtils.getResponseAsString(response));

}

@Test
public void updateFileSMEWithNonFileSME() throws FileNotFoundException, IOException, ParseException {
String element = getJSONValueAsString("PropertySubmodelElementUpdateWithNewIdShort.json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,28 @@
}
]
},
{
"modelType": "Operation",
"idShort": "sum",
"inputVariables": [
{
"value": {
"modelType": "Property",
"valueType": "xs:int",
"idShort": "input"
}
}
],
"inoutputVariables": [
{
"value": {
"modelType": "Property",
"valueType": "xs:int",
"idShort": "stack"
}
}
]
},
{
"modelType": "Property",
"value": "4370",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,28 @@
}
}
]
},
{
"modelType": "Operation",
"idShort": "sum",
"inputVariables": [
{
"value": {
"modelType": "Property",
"valueType": "xs:int",
"idShort": "input"
}
}
],
"inoutputVariables": [
{
"value": {
"modelType": "Property",
"valueType": "xs:int",
"idShort": "stack"
}
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"inputArguments": [
{
"value": {
"modelType": "Property",
"valueType": "xs:int",
"value": "5",
"idShort": "input"
}
}
],
"inoutputArguments": [
{
"value": {
"modelType": "Property",
"valueType": "xs:int",
"idShort": "stack",
"value": "3"
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"inoutputArguments": [
{
"value": {
"modelType": "Property",
"valueType": "xs:int",
"idShort": "stack",
"value": "8"
}
}
]
}

0 comments on commit 1f06ab7

Please sign in to comment.