Skip to content

Commit 165385c

Browse files
committed
Adds ARE for create Submodel
1 parent 9326538 commit 165385c

4 files changed

Lines changed: 112 additions & 34 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*******************************************************************************
2+
* Copyright (C) 2023 the Eclipse BaSyx Authors
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files (the
6+
* "Software"), to deal in the Software without restriction, including
7+
* without limitation the rights to use, copy, modify, merge, publish,
8+
* distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so, subject to
10+
* the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*
23+
* SPDX-License-Identifier: MIT
24+
******************************************************************************/
25+
26+
package org.eclipse.digitaltwin.basyx.core.exceptions;
27+
28+
/**
29+
* Indicates that the requested submodel element is not a File SubmodelElement
30+
*
31+
* @author danish
32+
*
33+
*/
34+
@SuppressWarnings("serial")
35+
public class SubmodelElementNotADataElementException extends RuntimeException {
36+
public SubmodelElementNotADataElementException() {
37+
}
38+
39+
public SubmodelElementNotADataElementException(String elementId) {
40+
super(getMsg(elementId));
41+
}
42+
43+
private static String getMsg(String elementId) {
44+
return "SubmodelElement with Id " + elementId + " is not a Data Element";
45+
}
46+
}

basyx.submodelservice/basyx.submodelservice-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/backend/MongoDbSubmodelOperations.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,9 @@
3030
import java.util.List;
3131

3232
import org.bson.Document;
33-
import org.eclipse.digitaltwin.aas4j.v3.model.Entity;
34-
import org.eclipse.digitaltwin.aas4j.v3.model.Submodel;
35-
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement;
36-
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementCollection;
37-
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementList;
33+
import org.eclipse.digitaltwin.aas4j.v3.model.*;
3834
import org.eclipse.digitaltwin.basyx.core.exceptions.ElementDoesNotExistException;
35+
import org.eclipse.digitaltwin.basyx.core.exceptions.SubmodelElementNotADataElementException;
3936
import org.eclipse.digitaltwin.basyx.core.pagination.CursorResult;
4037
import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo;
4138
import org.eclipse.digitaltwin.basyx.submodelservice.backend.SubmodelOperations;
@@ -186,6 +183,13 @@ public void createSubmodelElement(String submodelId, String idShortPath, Submode
186183
} else if (parentSme instanceof Entity entity) {
187184
List<SubmodelElement> submodelElements = entity.getStatements();
188185
submodelElements.add(submodelElement);
186+
} else if (parentSme instanceof AnnotatedRelationshipElement are) {
187+
try {
188+
List<DataElement> annotations = are.getAnnotations();
189+
annotations.add((DataElement) submodelElement);
190+
} catch (ClassCastException e) {
191+
throw new SubmodelElementNotADataElementException(submodelElement.getIdShort());
192+
}
189193
}
190194

191195
updateSubmodelElement(submodelId, idShortPath, parentSme);

basyx.submodelservice/basyx.submodelservice-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/backend/MongoFilterBuilder.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Deque;
3030
import java.util.List;
3131

32+
import org.eclipse.digitaltwin.aas4j.v3.model.AnnotatedRelationshipElement;
3233
import org.eclipse.digitaltwin.aas4j.v3.model.Entity;
3334
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement;
3435
import org.eclipse.digitaltwin.basyx.submodelservice.backend.IdShortPathParser.GenericPath;
@@ -52,6 +53,7 @@
5253
public final class MongoFilterBuilder{
5354
static final String KEY_VALUE = "value";
5455
static final String KEY_STATEMENTS = "statements";
56+
static final String KEY_ANNOTATIONS = "annotations";
5557
static final String KEY_SUBMODEL_ELEMENTS = "submodelElements";
5658
static final String KEY_ID_SHORT = "idShort";
5759

@@ -88,11 +90,8 @@ public static MongoFilterResult parse(@NonNull String idShortPath, @NonNull List
8890

8991
while (!paths.isEmpty()) {
9092
GenericPath segment = paths.pop();
91-
// Determine whether to use 'statements' (for Entity) or 'value' (for others)
92-
String childKey = KEY_VALUE;
93-
if (parentIndex < parentElements.size() && parentElements.get(parentIndex) instanceof Entity) {
94-
childKey = KEY_STATEMENTS;
95-
}
93+
// Determine the correct child key based on parent element type
94+
String childKey = getChildKey(parentElements, parentIndex);
9695
updateKey.append(".").append(childKey);
9796
parentIndex++;
9897

@@ -125,21 +124,44 @@ public static List<AggregationOperation> buildAggregationOperations(@NonNull Str
125124
currentPath = paths.pop();
126125
ops.add(new UnwindOperation(Fields.field("$"+KEY_VALUE), true));
127126
ops.add(new UnwindOperation(Fields.field("$"+KEY_STATEMENTS), true));
127+
ops.add(new UnwindOperation(Fields.field("$"+KEY_ANNOTATIONS), true));
128128
if (currentPath instanceof IdShortPath idPath) {
129129
Criteria inValue = Criteria.where(joinKeys(KEY_VALUE, KEY_ID_SHORT)).is(idPath.idShort());
130130
Criteria inStatements = Criteria.where(joinKeys(KEY_STATEMENTS, KEY_ID_SHORT)).is(idPath.idShort());
131-
ops.add(Aggregation.match(new Criteria().orOperator(inValue, inStatements)));
131+
Criteria inAnnotations = Criteria.where(joinKeys(KEY_ANNOTATIONS, KEY_ID_SHORT)).is(idPath.idShort());
132+
ops.add(Aggregation.match(new Criteria().orOperator(inValue, inStatements, inAnnotations)));
132133
} else if (currentPath instanceof IndexPath ixPath) {
133134
ops.add(Aggregation.skip(ixPath.index()));
134135
ops.add(Aggregation.limit(1));
135136
}
136-
ops.add(Aggregation.replaceRoot(IfNull.ifNull("$"+KEY_VALUE).then("$"+KEY_STATEMENTS)));
137+
ops.add(Aggregation.replaceRoot(
138+
IfNull.ifNull("$"+KEY_VALUE)
139+
.thenValueOf(IfNull.ifNull("$"+KEY_STATEMENTS).then("$"+KEY_ANNOTATIONS))));
137140

138141
}
139142

140143
return ops;
141144
}
142145

146+
/**
147+
* Determines the correct child key based on the parent element type.
148+
* - Entity uses 'statements'
149+
* - AnnotatedRelationshipElement uses 'annotations'
150+
* - Others (SubmodelElementList, SubmodelElementCollection) use 'value'
151+
*/
152+
private static String getChildKey(List<SubmodelElement> parentElements, int parentIndex) {
153+
if (parentIndex >= parentElements.size()) {
154+
return KEY_VALUE;
155+
}
156+
SubmodelElement parent = parentElements.get(parentIndex);
157+
if (parent instanceof Entity) {
158+
return KEY_STATEMENTS;
159+
} else if (parent instanceof AnnotatedRelationshipElement) {
160+
return KEY_ANNOTATIONS;
161+
}
162+
return KEY_VALUE;
163+
}
164+
143165
static CriteriaDefinition buildCriteria(String key, String value) {
144166
return Criteria.where(key).is(value);
145167
}

basyx.submodelservice/basyx.submodelservice-core/src/test/java/org/eclipse/digitaltwin/basyx/submodelservice/SubmodelServiceSuite.java

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,9 @@
4141
import org.apache.commons.io.FileUtils;
4242
import org.apache.commons.io.FilenameUtils;
4343
import org.apache.commons.lang3.NotImplementedException;
44-
import org.eclipse.digitaltwin.aas4j.v3.model.DataTypeDefXsd;
45-
import org.eclipse.digitaltwin.aas4j.v3.model.Entity;
46-
import org.eclipse.digitaltwin.aas4j.v3.model.File;
47-
import org.eclipse.digitaltwin.aas4j.v3.model.LangStringTextType;
48-
import org.eclipse.digitaltwin.aas4j.v3.model.OperationVariable;
49-
import org.eclipse.digitaltwin.aas4j.v3.model.Property;
50-
import org.eclipse.digitaltwin.aas4j.v3.model.Range;
51-
import org.eclipse.digitaltwin.aas4j.v3.model.Submodel;
52-
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement;
53-
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementCollection;
54-
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementList;
55-
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultEntity;
56-
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultFile;
57-
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultLangStringTextType;
58-
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultProperty;
59-
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodel;
60-
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodelElementCollection;
61-
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodelElementList;
62-
import org.eclipse.digitaltwin.basyx.core.exceptions.ElementDoesNotExistException;
63-
import org.eclipse.digitaltwin.basyx.core.exceptions.ElementNotAFileException;
64-
import org.eclipse.digitaltwin.basyx.core.exceptions.FileDoesNotExistException;
65-
import org.eclipse.digitaltwin.basyx.core.exceptions.NotInvokableException;
44+
import org.eclipse.digitaltwin.aas4j.v3.model.*;
45+
import org.eclipse.digitaltwin.aas4j.v3.model.impl.*;
46+
import org.eclipse.digitaltwin.basyx.core.exceptions.*;
6647
import org.eclipse.digitaltwin.basyx.core.pagination.CursorResult;
6748
import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo;
6849
import org.eclipse.digitaltwin.basyx.submodelservice.value.FileBlobValue;
@@ -689,6 +670,31 @@ public void addEntityToEntity() {
689670
assertEquals(sub_subEntity.getIdShort(), sme.getIdShort());
690671
}
691672

673+
@Test
674+
public void addDataElementToARE(){
675+
List<SubmodelElement> submodelElements = new ArrayList<>();
676+
AnnotatedRelationshipElement topAre = new DefaultAnnotatedRelationshipElement.Builder().idShort("MainAre").build();
677+
Property property = createDummyProperty("Test");
678+
submodelElements.add(topAre);
679+
Submodel submodel = buildDummySubmodelWithSmElement(ID, submodelElements);
680+
SubmodelService submodelService = getSubmodelService(submodel);
681+
682+
submodelService.createSubmodelElement("MainAre", property);
683+
SubmodelElement sme = submodelService.getSubmodelElement("MainAre.Test");
684+
assertEquals(sme.getIdShort(), sme.getIdShort());
685+
}
686+
687+
@Test(expected = SubmodelElementNotADataElementException.class)
688+
public void addNonDataElementToAre(){
689+
List<SubmodelElement> submodelElements = new ArrayList<>();
690+
AnnotatedRelationshipElement topAre = new DefaultAnnotatedRelationshipElement.Builder().idShort("MainAre").build();
691+
AnnotatedRelationshipElement subAre = new DefaultAnnotatedRelationshipElement.Builder().idShort("SubAre").build();
692+
submodelElements.add(topAre);
693+
Submodel submodel = buildDummySubmodelWithSmElement(ID, submodelElements);
694+
SubmodelService submodelService = getSubmodelService(submodel);
695+
submodelService.createSubmodelElement("MainAre", subAre);
696+
}
697+
692698
protected Submodel buildDummySubmodelWithSmElement(String id, List<SubmodelElement> submodelElements) {
693699
return new DefaultSubmodel.Builder().id(id).submodelElements(submodelElements).build();
694700
}

0 commit comments

Comments
 (0)