use of io.adminshell.aas.v3.model.Asset 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.Asset in project FAAAST-Service by FraunhoferIOSB.
the class IdentifiablePersistenceManager method remove.
/**
* Remove an identifiable by its identifier.
* Following identifiables are supported:
* <ul>
* <li>{@link io.adminshell.aas.v3.model.AssetAdministrationShell}
* <li>{@link io.adminshell.aas.v3.model.Submodel}
* <li>{@link io.adminshell.aas.v3.model.ConceptDescription}
* <li>{@link io.adminshell.aas.v3.model.Asset}
* </ul>
*
* @param id of the indetifiable which should be removed
* @throws ResourceNotFoundException if there is no identifiable with such an identifer
*/
public void remove(Identifier id) throws ResourceNotFoundException {
if (id == null || this.aasEnvironment == null) {
return;
}
Predicate<Identifiable> removeFilter = x -> !x.getIdentification().getIdentifier().equalsIgnoreCase(id.getIdentifier());
Identifiable identifiable = getIdentifiableById(id);
if (identifiable == null) {
throw new ResourceNotFoundException(String.format(ERROR_MSG_RESOURCE_NOT_FOUND_BY_ID, IdentifierHelper.asString(id)));
}
// TODO: use reflection?
if (AssetAdministrationShell.class.isAssignableFrom(identifiable.getClass())) {
List<AssetAdministrationShell> newAASList;
newAASList = this.aasEnvironment.getAssetAdministrationShells().stream().filter(removeFilter).collect(Collectors.toList());
this.aasEnvironment.setAssetAdministrationShells(newAASList);
} else if (Submodel.class.isAssignableFrom(identifiable.getClass())) {
List<Submodel> newSubmodelList;
newSubmodelList = this.aasEnvironment.getSubmodels().stream().filter(removeFilter).collect(Collectors.toList());
this.aasEnvironment.setSubmodels(newSubmodelList);
Reference referenceOfIdentifiable = AasUtils.toReference(identifiable);
this.aasEnvironment.getAssetAdministrationShells().forEach(x -> x.getSubmodels().remove(referenceOfIdentifiable));
} else if (ConceptDescription.class.isAssignableFrom(identifiable.getClass())) {
List<ConceptDescription> newConceptDescriptionList;
newConceptDescriptionList = this.aasEnvironment.getConceptDescriptions().stream().filter(removeFilter).collect(Collectors.toList());
this.aasEnvironment.setConceptDescriptions(newConceptDescriptionList);
} else if (Asset.class.isAssignableFrom(identifiable.getClass())) {
List<Asset> newAssetList;
newAssetList = this.aasEnvironment.getAssets().stream().filter(removeFilter).collect(Collectors.toList());
this.aasEnvironment.setAssets(newAssetList);
}
}
use of io.adminshell.aas.v3.model.Asset in project FAAAST-Service by FraunhoferIOSB.
the class RequestHandler method syncWithAsset.
/**
* Check for each SubmodelElement if there is an AssetConnection.If yes read
* the value from it and compare it to the current value.If they differ from
* each other update the submodelelement with the value from the
* AssetConnection.
*
* @param parent of the SubmodelElement List
* @param submodelElements List of SubmodelElements which should be
* considered and updated
* @throws ResourceNotFoundException if reference does not point to valid
* element
* @throws AssetConnectionException if reading value from asset connection
* fails
* @throws
* de.fraunhofer.iosb.ilt.faaast.service.model.exception.ValueMappingException
* if mapping value read from asset connection fails
* @throws
* de.fraunhofer.iosb.ilt.faaast.service.exception.MessageBusException if
* publishing fails
*/
protected void syncWithAsset(Reference parent, Collection<SubmodelElement> submodelElements) throws ResourceNotFoundException, AssetConnectionException, ValueMappingException, MessageBusException {
if (parent == null || submodelElements == null) {
return;
}
for (SubmodelElement submodelElement : submodelElements) {
Reference reference = AasUtils.toReference(parent, submodelElement);
Optional<DataElementValue> newValue = assetConnectionManager.readValue(reference);
if (newValue.isPresent()) {
ElementValue oldValue = ElementValueMapper.toValue(submodelElement);
if (!Objects.equals(oldValue, newValue)) {
submodelElement = persistence.put(null, reference, ElementValueMapper.setValue(submodelElement, newValue.get()));
messageBus.publish(ElementUpdateEventMessage.builder().element(AasUtils.toReference(parent, submodelElement)).value(submodelElement).build());
}
} else if (SubmodelElementCollection.class.isAssignableFrom(submodelElement.getClass())) {
syncWithAsset(reference, ((SubmodelElementCollection) submodelElement).getValues());
}
}
}
use of io.adminshell.aas.v3.model.Asset in project FAAAST-Service by FraunhoferIOSB.
the class AssetOperationProvider method invoke.
/**
* Invokes as operation synchronously
*
* @param input input parameters
* @param inoutput inoutput parameters, i.e. parameters that are passed as
* input to the operation but can be modified while execution
* @return output variables of the operation
* @throws
* de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException
* when invoking operation on asset connection fails
* @throws IllegalArgumentException if provided inoutput arguments do not
* match actual inoutput arguments
*/
public default OperationVariable[] invoke(OperationVariable[] input, OperationVariable[] inoutput) throws AssetConnectionException {
final String BASE_ERROR_MSG = "inoutput argument mismatch";
final AtomicReference<OperationVariable[]> result = new AtomicReference<>();
final AtomicReference<OperationVariable[]> modifiedInoutput = new AtomicReference<>();
CountDownLatch condition = new CountDownLatch(1);
invokeAsync(input, inoutput, (x, y) -> {
result.set(x);
modifiedInoutput.set(y);
condition.countDown();
});
try {
condition.await();
} catch (InterruptedException x) {
Thread.currentThread().interrupt();
throw new AssetConnectionException("invoking operation failed because of timeout", x);
}
if (inoutput == null && result.get() != null && result.get().length > 0) {
throw new IllegalArgumentException(String.format("%s - provided: none, actual: %d", BASE_ERROR_MSG, result.get().length));
}
if (result.get() == null && inoutput != null && inoutput.length > 0) {
throw new IllegalArgumentException(String.format("%s - provided: %d, actual: none", BASE_ERROR_MSG, inoutput.length));
}
if (inoutput != null) {
if (inoutput.length != modifiedInoutput.get().length) {
throw new IllegalArgumentException(String.format("%s - provided: %d, actual: %d", BASE_ERROR_MSG, inoutput.length, result.get().length));
}
for (int i = 0; i < inoutput.length; i++) {
final OperationVariable variable = inoutput[i];
inoutput[i] = Stream.of(result.get()).filter(x -> Objects.equals(x.getValue().getIdShort(), variable.getValue().getIdShort())).findFirst().orElseThrow(() -> new IllegalArgumentException(String.format("%s - variable provided but not found in actual ouput (idShort: %s)", BASE_ERROR_MSG, variable.getValue().getIdShort())));
}
}
return result.get();
}
use of io.adminshell.aas.v3.model.Asset in project FAAAST-Service by FraunhoferIOSB.
the class AasServiceNodeManager method addAsset.
/**
* Adds an Asset to the given Node.
*
* @param node The UA node in which the Asset should be created
* @param asset The desired Asset
* @throws StatusException If the operation fails
*/
private void addAsset(UaNode node, Asset asset) throws StatusException {
if (node == null) {
throw new IllegalArgumentException(NODE_NULL);
} else if (asset == null) {
throw new IllegalArgumentException("asset = null");
}
try {
String name = asset.getIdShort();
String displayName = "Asset:" + name;
LOG.info("addAsset {}; to Node: {}", name, node);
QualifiedName browseName = UaQualifiedName.from(opc.i4aas.ObjectTypeIds.AASAssetType.getNamespaceUri(), name).toQualifiedName(getNamespaceTable());
NodeId nid = createNodeId(node, browseName);
AASAssetType assetNode = createInstance(AASAssetType.class, nid, browseName, LocalizedText.english(displayName));
addIdentifiable(assetNode, asset.getIdentification(), asset.getAdministration(), asset.getCategory());
// DataSpecifications
addEmbeddedDataSpecifications(assetNode, asset.getEmbeddedDataSpecifications());
node.addComponent(assetNode);
referableMap.put(AasUtils.toReference(asset), new ObjectData(asset, assetNode));
} catch (Exception ex) {
LOG.error("addAsset Exception", ex);
throw ex;
}
}
Aggregations