use of io.adminshell.aas.v3.model.Submodel in project FAAAST-Service by FraunhoferIOSB.
the class RequestHandlerManagerTest method testDeleteSubmodelElementByPathRequest.
@Test
public void testDeleteSubmodelElementByPathRequest() throws ResourceNotFoundException {
Submodel submodel = environment.getSubmodels().get(0);
Reference reference = ReferenceHelper.toReference(ReferenceHelper.toKeys(SUBMODEL_ELEMENT_REF), submodel.getIdentification(), Submodel.class);
when(persistence.get(reference, new QueryModifier())).thenReturn(environment.getSubmodels().get(0).getSubmodelElements().get(0));
DeleteSubmodelElementByPathRequest request = new DeleteSubmodelElementByPathRequest.Builder().id(submodel.getIdentification()).path(ReferenceHelper.toKeys(SUBMODEL_ELEMENT_REF)).build();
DeleteSubmodelElementByPathResponse response = manager.execute(request);
DeleteSubmodelElementByPathResponse expected = new DeleteSubmodelElementByPathResponse.Builder().statusCode(StatusCode.Success).build();
Assert.assertEquals(expected, response);
verify(persistence).remove(reference);
}
use of io.adminshell.aas.v3.model.Submodel in project FAAAST-Service by FraunhoferIOSB.
the class ReferenceHelper method completeReferenceWithProperKeyElements.
/**
* Browse the keys of a reference and try to find the referenced element in the
* asset administration shell environment to set the right {@link io.adminshell.aas.v3.model.KeyElements}
* of the key.
* All key types must be null or SUBMODEL_ELEMENT.
*
* @param reference with keys which should be completed
* @param env the asset administration shell environment which contains the referenced elements
* @throws ResourceNotFoundException if an element referenced by a key could not be found
*/
public static void completeReferenceWithProperKeyElements(Reference reference, AssetAdministrationShellEnvironment env) throws ResourceNotFoundException {
if (reference == null) {
return;
}
List<Key> keys = reference.getKeys();
if (keys.stream().allMatch(x -> x.getType() != null && x.getType() != KeyElements.SUBMODEL_ELEMENT)) {
return;
}
final Referable[] parent = { null };
for (Key k : keys) {
if (env.getAssetAdministrationShells().stream().anyMatch(x -> x.getIdentification().getIdentifier().equalsIgnoreCase(k.getValue()) || x.getIdShort().equalsIgnoreCase(k.getValue()))) {
k.setType(KeyElements.ASSET_ADMINISTRATION_SHELL);
continue;
}
env.getSubmodels().forEach(x -> {
if (x.getIdentification().getIdentifier().equalsIgnoreCase(k.getValue()) || x.getIdShort().equalsIgnoreCase(k.getValue())) {
k.setType(KeyElements.SUBMODEL);
parent[0] = x;
}
});
if (k.getType() != null && k.getType() != KeyElements.SUBMODEL_ELEMENT) {
continue;
}
if (env.getConceptDescriptions().stream().anyMatch(x -> x.getIdentification().getIdentifier().equalsIgnoreCase(k.getValue()) || x.getIdShort().equalsIgnoreCase(k.getValue()))) {
k.setType(KeyElements.CONCEPT_DESCRIPTION);
continue;
}
if (env.getAssets().stream().anyMatch(x -> x.getIdentification().getIdentifier().equalsIgnoreCase(k.getValue()) || x.getIdShort().equalsIgnoreCase(k.getValue()))) {
k.setType(KeyElements.ASSET);
continue;
}
if (parent[0] != null && Submodel.class.isAssignableFrom(parent[0].getClass())) {
Submodel submodel = (Submodel) parent[0];
submodel.getSubmodelElements().forEach(y -> {
if (y.getIdShort().equalsIgnoreCase(k.getValue())) {
k.setType(AasUtils.referableToKeyType(y));
parent[0] = y;
}
});
} else if (SubmodelElementCollection.class.isAssignableFrom(parent[0].getClass())) {
((SubmodelElementCollection) parent[0]).getValues().forEach(x -> {
if (x.getIdShort().equalsIgnoreCase(k.getValue())) {
k.setType(AasUtils.referableToKeyType(x));
parent[0] = x;
}
});
} else if (Operation.class.isAssignableFrom(parent[0].getClass())) {
Operation operation = (Operation) parent[0];
Stream.concat(Stream.concat(operation.getInoutputVariables().stream(), operation.getInputVariables().stream()), operation.getOutputVariables().stream()).forEach(x -> {
if (x.getValue().getIdShort().equalsIgnoreCase(k.getValue())) {
k.setType(AasUtils.referableToKeyType(x.getValue()));
parent[0] = x.getValue();
}
});
}
if (k.getType() == null) {
throw new ResourceNotFoundException("Resource with ID " + k.getValue() + " was not found!");
}
}
}
use of io.adminshell.aas.v3.model.Submodel in project FAAAST-Service by FraunhoferIOSB.
the class PostSubmodelRequestHandler method process.
@Override
public PostSubmodelResponse process(PostSubmodelRequest request) {
PostSubmodelResponse response = new PostSubmodelResponse();
try {
Submodel submodel = (Submodel) persistence.put(request.getSubmodel());
response.setPayload(submodel);
response.setStatusCode(StatusCode.SuccessCreated);
Reference reference = AasUtils.toReference(submodel);
readValueFromAssetConnectionAndUpdatePersistence(reference, submodel.getSubmodelElements());
publishElementCreateEventMessage(reference, submodel);
} catch (Exception ex) {
response.setStatusCode(StatusCode.ServerInternalError);
}
return response;
}
use of io.adminshell.aas.v3.model.Submodel in project FAAAST-Service by FraunhoferIOSB.
the class ElementValueMapperTest method testEntitySetValueMapping.
@Test
public void testEntitySetValueMapping() throws ValueFormatException {
SubmodelElement actual = new DefaultEntity.Builder().statement(new DefaultProperty.Builder().idShort("property").build()).build();
EntityValue value = EntityValue.builder().statement("property", PropertyValue.of(Datatype.String, "foo")).entityType(EntityType.SELF_MANAGED_ENTITY).globalAssetId(List.of(new DefaultKey.Builder().idType(KeyType.IRI).type(KeyElements.SUBMODEL).value("http://example.org/submodel/1").build(), new DefaultKey.Builder().idType(KeyType.ID_SHORT).type(KeyElements.PROPERTY).value("property1").build())).build();
SubmodelElement expected = new DefaultEntity.Builder().statement(new DefaultProperty.Builder().idShort(value.getStatements().keySet().iterator().next()).valueType(Datatype.String.getName()).value("foo").build()).entityType(value.getEntityType()).globalAssetId(new DefaultReference.Builder().keys(value.getGlobalAssetId()).build()).build();
ElementValueMapper.setValue(actual, value);
Assert.assertEquals(expected, actual);
}
use of io.adminshell.aas.v3.model.Submodel in project FAAAST-Service by FraunhoferIOSB.
the class ElementValueMapperTest method testRelationshipElementSetValueMapping.
@Test
public void testRelationshipElementSetValueMapping() {
SubmodelElement actual = new DefaultRelationshipElement.Builder().build();
RelationshipElementValue value = RelationshipElementValue.builder().first(List.of(new DefaultKey.Builder().idType(KeyType.IRI).type(KeyElements.SUBMODEL).value("http://example.org/submodel/1").build(), new DefaultKey.Builder().idType(KeyType.ID_SHORT).type(KeyElements.PROPERTY).value("property1").build())).second(List.of(new DefaultKey.Builder().idType(KeyType.IRI).type(KeyElements.SUBMODEL).value("http://example.org/submodel/2").build(), new DefaultKey.Builder().idType(KeyType.ID_SHORT).type(KeyElements.PROPERTY).value("property2").build())).build();
SubmodelElement expected = new DefaultRelationshipElement.Builder().first(new DefaultReference.Builder().keys(value.getFirst()).build()).second(new DefaultReference.Builder().keys(value.getSecond()).build()).build();
ElementValueMapper.setValue(actual, value);
Assert.assertEquals(expected, actual);
}
Aggregations