Search in sources :

Example 1 with Key

use of io.adminshell.aas.v3.model.Key in project FAAAST-Service by FraunhoferIOSB.

the class RequestHandlerManagerTest method testReadValueFromAssetConnectionAndUpdatePersistence.

@Test
public void testReadValueFromAssetConnectionAndUpdatePersistence() throws AssetConnectionException, ResourceNotFoundException {
    RequestHandler requestHandler = new DeleteSubmodelByIdRequestHandler(persistence, messageBus, assetConnectionManager);
    Reference parentRef = new DefaultReference.Builder().key(new DefaultKey.Builder().value("sub").idType(KeyType.IRI).type(KeyElements.SUBMODEL).build()).build();
    SubmodelElement prop1 = new DefaultProperty.Builder().idShort("prop1").value("test").valueType("string").build();
    SubmodelElement range = new DefaultRange.Builder().idShort("range1").max("1.0").min("0").valueType("double").build();
    SubmodelElement prop2 = new DefaultProperty.Builder().idShort("prop2").value("test").valueType("string").build();
    SubmodelElementCollection collection = new DefaultSubmodelElementCollection.Builder().idShort("col1").value(prop2).build();
    SubmodelElement prop1_new = new DefaultProperty.Builder().idShort("prop1").value("testNew").valueType("string").build();
    SubmodelElement range_new = new DefaultRange.Builder().idShort("range1").max("1.0").min("0").valueType("double").build();
    SubmodelElement prop2_new = new DefaultProperty.Builder().idShort("prop2").value("testNew").valueType("string").build();
    List<SubmodelElement> submodelElements = List.of(prop1, range, collection);
    AssetValueProvider provider_prop1 = mock(AssetValueProvider.class);
    AssetValueProvider provider_prop2 = mock(AssetValueProvider.class);
    AssetValueProvider provider_range1 = mock(AssetValueProvider.class);
    when(assetConnectionManager.hasValueProvider(any())).thenReturn(true);
    // mock value prop1
    when(assetConnectionManager.getValueProvider(AasUtils.toReference(parentRef, prop1))).thenReturn(provider_prop1);
    when(provider_prop1.getValue()).thenReturn(ElementValueMapper.toValue(prop1_new));
    // mock value prop2
    when(assetConnectionManager.getValueProvider(AasUtils.toReference(AasUtils.toReference(parentRef, collection), prop2))).thenReturn(provider_prop2);
    when(provider_prop2.getValue()).thenReturn(ElementValueMapper.toValue(prop2_new));
    // mock value range
    when(assetConnectionManager.getValueProvider(AasUtils.toReference(parentRef, range))).thenReturn(provider_range1);
    when(provider_range1.getValue()).thenReturn(ElementValueMapper.toValue(range_new));
    requestHandler.readValueFromAssetConnectionAndUpdatePersistence(parentRef, submodelElements);
    verify(persistence).put(null, AasUtils.toReference(parentRef, prop1), prop1_new);
    verify(persistence).put(null, AasUtils.toReference(AasUtils.toReference(parentRef, collection), prop2), prop2_new);
    Assert.assertEquals(prop1_new, prop1);
    Assert.assertEquals(prop2_new, prop2);
    Assert.assertEquals(range_new, range);
}
Also used : DeleteSubmodelByIdRequestHandler(de.fraunhofer.iosb.ilt.faaast.service.request.handler.DeleteSubmodelByIdRequestHandler) SubmodelElement(io.adminshell.aas.v3.model.SubmodelElement) RequestHandler(de.fraunhofer.iosb.ilt.faaast.service.request.handler.RequestHandler) DeleteSubmodelByIdRequestHandler(de.fraunhofer.iosb.ilt.faaast.service.request.handler.DeleteSubmodelByIdRequestHandler) DefaultRange(io.adminshell.aas.v3.model.impl.DefaultRange) Reference(io.adminshell.aas.v3.model.Reference) DefaultReference(io.adminshell.aas.v3.model.impl.DefaultReference) AtomicReference(java.util.concurrent.atomic.AtomicReference) DefaultSubmodelElementCollection(io.adminshell.aas.v3.model.impl.DefaultSubmodelElementCollection) SubmodelElementCollection(io.adminshell.aas.v3.model.SubmodelElementCollection) DefaultSubmodelElementCollection(io.adminshell.aas.v3.model.impl.DefaultSubmodelElementCollection) AssetValueProvider(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetValueProvider) Test(org.junit.Test)

Example 2 with Key

use of io.adminshell.aas.v3.model.Key in project FAAAST-Service by FraunhoferIOSB.

the class ReferenceHelper method toReference.

/**
 * Combines a list of keys of a child element with a parent to a reference
 *
 * @param keys of the child
 * @param parentId of the parent
 * @param parentClass type of the parent
 * @return the full reference to the child element
 */
public static Reference toReference(List<Key> keys, Identifier parentId, Class<?> parentClass) {
    Reference parentReference = toReference(parentId, parentClass);
    Reference childReference = new DefaultReference.Builder().keys(keys).build();
    return toReference(parentReference, childReference);
}
Also used : Reference(io.adminshell.aas.v3.model.Reference) DefaultReference(io.adminshell.aas.v3.model.impl.DefaultReference)

Example 3 with Key

use of io.adminshell.aas.v3.model.Key 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!");
        }
    }
}
Also used : Submodel(io.adminshell.aas.v3.model.Submodel) KeyElements(io.adminshell.aas.v3.model.KeyElements) Operation(io.adminshell.aas.v3.model.Operation) SubmodelElement(io.adminshell.aas.v3.model.SubmodelElement) Reference(io.adminshell.aas.v3.model.Reference) AssetAdministrationShellEnvironment(io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment) SubmodelElementCollection(io.adminshell.aas.v3.model.SubmodelElementCollection) Collectors(java.util.stream.Collectors) AasUtils(io.adminshell.aas.v3.dataformat.core.util.AasUtils) Identifier(io.adminshell.aas.v3.model.Identifier) ArrayList(java.util.ArrayList) ReflectionHelper(io.adminshell.aas.v3.dataformat.core.ReflectionHelper) Key(io.adminshell.aas.v3.model.Key) List(java.util.List) Stream(java.util.stream.Stream) ResourceNotFoundException(de.fraunhofer.iosb.ilt.faaast.service.exception.ResourceNotFoundException) Referable(io.adminshell.aas.v3.model.Referable) DefaultReference(io.adminshell.aas.v3.model.impl.DefaultReference) KeyType(io.adminshell.aas.v3.model.KeyType) DefaultKey(io.adminshell.aas.v3.model.impl.DefaultKey) Submodel(io.adminshell.aas.v3.model.Submodel) Referable(io.adminshell.aas.v3.model.Referable) SubmodelElementCollection(io.adminshell.aas.v3.model.SubmodelElementCollection) Operation(io.adminshell.aas.v3.model.Operation) ResourceNotFoundException(de.fraunhofer.iosb.ilt.faaast.service.exception.ResourceNotFoundException) Key(io.adminshell.aas.v3.model.Key) DefaultKey(io.adminshell.aas.v3.model.impl.DefaultKey)

Example 4 with Key

use of io.adminshell.aas.v3.model.Key in project FAAAST-Service by FraunhoferIOSB.

the class ElementValueMapperTest method testReferenceElementSetValueMapping.

@Test
public void testReferenceElementSetValueMapping() {
    SubmodelElement actual = new DefaultReferenceElement.Builder().value(new DefaultReference.Builder().build()).build();
    ReferenceElementValue value = ReferenceElementValue.builder().key(KeyType.IRI, KeyElements.SUBMODEL, "http://example.org/submodel/1").key(KeyType.ID_SHORT, KeyElements.PROPERTY, "property1").build();
    SubmodelElement expected = new DefaultReferenceElement.Builder().value(new DefaultReference.Builder().keys(value.getKeys()).build()).build();
    ElementValueMapper.setValue(actual, value);
    Assert.assertEquals(expected, actual);
}
Also used : ReferenceElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.ReferenceElementValue) SubmodelElement(io.adminshell.aas.v3.model.SubmodelElement) DefaultReference(io.adminshell.aas.v3.model.impl.DefaultReference) Test(org.junit.Test)

Example 5 with Key

use of io.adminshell.aas.v3.model.Key in project FAAAST-Service by FraunhoferIOSB.

the class OpcUaEndpoint method writeValue.

/**
 * Writes the Value of the given SubmodelElement into the service.
 *
 * @param element The desired SubmodelElement including the new value
 * @param submodel The corresponding submodel
 * @param refElement The reference to the SubmodelElement
 * @return True if the write succeeded, false otherwise
 */
public boolean writeValue(SubmodelElement element, Submodel submodel, Reference refElement) {
    boolean retval = false;
    if (element == null) {
        throw new IllegalArgumentException("element == null");
    } else if (submodel == null) {
        throw new IllegalArgumentException("submodel == null");
    }
    try {
        SetSubmodelElementValueByPathRequest request = new SetSubmodelElementValueByPathRequest();
        List<Key> path = new ArrayList<>();
        path.addAll(refElement.getKeys());
        request.setId(submodel.getIdentification());
        request.setPath(path);
        request.setValueParser(new OpcUaElementValueParser());
        if (element instanceof MultiLanguageProperty) {
            MultiLanguageProperty mlp = (MultiLanguageProperty) element;
            if ((mlp.getValues() != null) && (mlp.getValues().size() > 1)) {
                for (int i = 0; i < mlp.getValues().size(); i++) {
                    logger.info("writeValue: MLP " + i + ": " + mlp.getValues().get(i).getValue());
                }
            }
        }
        request.setRawValue(ElementValueMapper.toValue(element));
        if (request.getRawValue() instanceof MultiLanguagePropertyValue) {
            MultiLanguagePropertyValue mlpv = (MultiLanguagePropertyValue) request.getRawValue();
            if ((mlpv.getLangStringSet() != null) && (mlpv.getLangStringSet().size() > 1)) {
                for (int i = 0; i < mlpv.getLangStringSet().size(); i++) {
                    logger.info("writeValue: MLPV " + i + ": " + mlpv.getLangStringSet().toArray()[i]);
                }
            }
        }
        Response response = service.execute(request);
        logger.info("writeValue: Submodel " + submodel.getIdentification().getIdentifier() + "; Element " + element.getIdShort() + "; Status: " + response.getStatusCode());
        if (isSuccess(response.getStatusCode())) {
            retval = true;
        }
    } catch (Exception ex) {
        logger.error("writeValue error", ex);
    }
    return retval;
}
Also used : SetSubmodelElementValueByPathRequest(de.fraunhofer.iosb.ilt.faaast.service.model.request.SetSubmodelElementValueByPathRequest) InvokeOperationSyncResponse(de.fraunhofer.iosb.ilt.faaast.service.model.api.response.InvokeOperationSyncResponse) Response(de.fraunhofer.iosb.ilt.faaast.service.model.api.Response) MultiLanguageProperty(io.adminshell.aas.v3.model.MultiLanguageProperty) ArrayList(java.util.ArrayList) Key(io.adminshell.aas.v3.model.Key) Endpoint(de.fraunhofer.iosb.ilt.faaast.service.endpoint.Endpoint) StatusException(com.prosysopc.ua.StatusException) MultiLanguagePropertyValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.MultiLanguagePropertyValue)

Aggregations

DefaultReference (io.adminshell.aas.v3.model.impl.DefaultReference)26 DefaultKey (io.adminshell.aas.v3.model.impl.DefaultKey)24 Test (org.junit.Test)24 Reference (io.adminshell.aas.v3.model.Reference)20 ArrayList (java.util.ArrayList)18 SubmodelElement (io.adminshell.aas.v3.model.SubmodelElement)13 Key (io.adminshell.aas.v3.model.Key)11 UaClient (com.prosysopc.ua.client.UaClient)10 QualifiedName (com.prosysopc.ua.stack.builtintypes.QualifiedName)10 BrowsePathResult (com.prosysopc.ua.stack.core.BrowsePathResult)10 RelativePath (com.prosysopc.ua.stack.core.RelativePath)10 RelativePathElement (com.prosysopc.ua.stack.core.RelativePathElement)10 SubmodelElementCollection (io.adminshell.aas.v3.model.SubmodelElementCollection)10 DefaultIdentifier (io.adminshell.aas.v3.model.impl.DefaultIdentifier)10 KeyType (io.adminshell.aas.v3.model.KeyType)9 LangString (io.adminshell.aas.v3.model.LangString)9 Submodel (io.adminshell.aas.v3.model.Submodel)9 List (java.util.List)9 AssetAdministrationShell (io.adminshell.aas.v3.model.AssetAdministrationShell)8 AssetAdministrationShellEnvironment (io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment)8