Search in sources :

Example 1 with KeyElements

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

the class ReferablePersistenceManager method getSubmodelElements.

/**
 * Get the submodel elements associated to the reference.
 * Supported are two possible parents of submodel elements:
 * <p>
 * <ul>
 * <li>{@link io.adminshell.aas.v3.model.Submodel}
 * <li>{@link io.adminshell.aas.v3.model.SubmodelElementCollection}
 * </ul>
 * <p>
 * If the semanticId is not null the submodel element list filtered by the semantic id
 *
 * @param reference to the submodel or submodel element collection
 * @param semanticId of the submodel elements
 * @return a list of the submodel elements associated to the parent reference
 */
public List<SubmodelElement> getSubmodelElements(Reference reference, Reference semanticId) throws ResourceNotFoundException {
    if (reference == null) {
        return null;
    }
    if (reference.getKeys() != null && reference.getKeys().size() > 0) {
        List<SubmodelElement> submodelElements = null;
        KeyElements lastKeyElementOfReference = reference.getKeys().get(reference.getKeys().size() - 1).getType();
        if (lastKeyElementOfReference == KeyElements.SUBMODEL) {
            Submodel submodel = AasUtils.resolve(reference, this.aasEnvironment, Submodel.class);
            if (submodel == null) {
                throw new ResourceNotFoundException(String.format("Resource not found with reference {}", AasUtils.asString(reference)));
            }
            Submodel deepCopiedSubmodel = DeepCopyHelper.deepCopy(submodel, submodel.getClass());
            submodelElements = deepCopiedSubmodel.getSubmodelElements();
        } else if (lastKeyElementOfReference == KeyElements.SUBMODEL_ELEMENT_COLLECTION) {
            SubmodelElementCollection submodelElementCollection = AasUtils.resolve(reference, this.aasEnvironment, SubmodelElementCollection.class);
            if (submodelElementCollection == null) {
                throw new ResourceNotFoundException(String.format("Resource not found with reference {}", AasUtils.asString(reference)));
            }
            SubmodelElementCollection deepCopiedSubmodelElementCollection = DeepCopyHelper.deepCopy(submodelElementCollection, submodelElementCollection.getClass());
            submodelElements = new ArrayList<>(deepCopiedSubmodelElementCollection.getValues());
        }
        if (semanticId != null) {
            assert submodelElements != null;
            submodelElements = submodelElements.stream().filter(x -> x.getSemanticId() != null && x.getSemanticId().equals(semanticId)).collect(Collectors.toList());
        }
        return submodelElements;
    }
    return null;
}
Also used : Submodel(io.adminshell.aas.v3.model.Submodel) SubmodelElement(io.adminshell.aas.v3.model.SubmodelElement) ArrayList(java.util.ArrayList) SubmodelElementCollection(io.adminshell.aas.v3.model.SubmodelElementCollection) KeyElements(io.adminshell.aas.v3.model.KeyElements) ResourceNotFoundException(de.fraunhofer.iosb.ilt.faaast.service.exception.ResourceNotFoundException)

Example 2 with KeyElements

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

the class ReferablePersistenceManager method putSubmodelElement.

/**
 * Create or update a submodel element.
 * Parent reference and reference of the submodel element must not both be null.
 * Otherwise the location of the submodel element cannot be determined.
 * Supported parent references could be references to a
 * <ul>
 * <li>{@link io.adminshell.aas.v3.model.Submodel} or to a
 * <li>{@link io.adminshell.aas.v3.model.SubmodelElementCollection}
 * </ul>
 * To add a new submodel element give the parent reference and the submodel element.
 * To update an existing submodel element give the reference to the submodel element and the submodel element.
 *
 * @param parent reference to the parent
 * @param referenceToSubmodelElement reference to the submodel element
 * @param submodelElement which should be updated or created
 * @return the updated or created submodel element
 */
public SubmodelElement putSubmodelElement(Reference parent, Reference referenceToSubmodelElement, SubmodelElement submodelElement) throws ResourceNotFoundException {
    if ((parent == null && referenceToSubmodelElement == null) || submodelElement == null) {
        return null;
    }
    KeyElements lastKeyElementOfParent;
    if (parent != null && parent.getKeys() != null && parent.getKeys().size() > 0) {
        lastKeyElementOfParent = parent.getKeys().get(parent.getKeys().size() - 1).getType();
    } else if (referenceToSubmodelElement != null && referenceToSubmodelElement.getKeys() != null && referenceToSubmodelElement.getKeys().size() > 1) {
        lastKeyElementOfParent = referenceToSubmodelElement.getKeys().get(referenceToSubmodelElement.getKeys().size() - 2).getType();
        parent = new DefaultReference.Builder().keys(referenceToSubmodelElement.getKeys().subList(0, referenceToSubmodelElement.getKeys().size() - 1)).build();
    } else {
        return null;
    }
    Predicate<SubmodelElement> filter = x -> x.getIdShort().equalsIgnoreCase(submodelElement.getIdShort());
    if (lastKeyElementOfParent == KeyElements.SUBMODEL) {
        Submodel submodel = AasUtils.resolve(parent, this.aasEnvironment, Submodel.class);
        if (submodel == null) {
            throw new ResourceNotFoundException(String.format("Resource not found with reference {}", AasUtils.asString(parent)));
        }
        submodel.getSubmodelElements().removeIf(filter);
        submodel.getSubmodelElements().add(submodelElement);
    } else if (lastKeyElementOfParent == KeyElements.SUBMODEL_ELEMENT_COLLECTION) {
        SubmodelElementCollection submodelElementCollection = AasUtils.resolve(parent, this.aasEnvironment, SubmodelElementCollection.class);
        if (submodelElementCollection == null) {
            throw new ResourceNotFoundException(String.format("Resource not found with reference {}", AasUtils.asString(parent)));
        }
        submodelElementCollection.getValues().removeIf(filter);
        submodelElementCollection.getValues().add(submodelElement);
    } else {
        return null;
    }
    return submodelElement;
}
Also used : KeyElements(io.adminshell.aas.v3.model.KeyElements) SubmodelElement(io.adminshell.aas.v3.model.SubmodelElement) Predicate(java.util.function.Predicate) Reference(io.adminshell.aas.v3.model.Reference) SubmodelElementCollection(io.adminshell.aas.v3.model.SubmodelElementCollection) Extend(de.fraunhofer.iosb.ilt.faaast.service.model.api.modifier.Extend) Collectors(java.util.stream.Collectors) AasUtils(io.adminshell.aas.v3.dataformat.core.util.AasUtils) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArrayList(java.util.ArrayList) Identifiable(io.adminshell.aas.v3.model.Identifiable) List(java.util.List) ResourceNotFoundException(de.fraunhofer.iosb.ilt.faaast.service.exception.ResourceNotFoundException) DeepCopyHelper(de.fraunhofer.iosb.ilt.faaast.service.util.DeepCopyHelper) Referable(io.adminshell.aas.v3.model.Referable) EnvironmentHelper(de.fraunhofer.iosb.ilt.faaast.service.persistence.memory.util.EnvironmentHelper) DefaultReference(io.adminshell.aas.v3.model.impl.DefaultReference) QueryModifier(de.fraunhofer.iosb.ilt.faaast.service.model.api.modifier.QueryModifier) Method(java.lang.reflect.Method) Submodel(io.adminshell.aas.v3.model.Submodel) Submodel(io.adminshell.aas.v3.model.Submodel) SubmodelElement(io.adminshell.aas.v3.model.SubmodelElement) SubmodelElementCollection(io.adminshell.aas.v3.model.SubmodelElementCollection) KeyElements(io.adminshell.aas.v3.model.KeyElements) ResourceNotFoundException(de.fraunhofer.iosb.ilt.faaast.service.exception.ResourceNotFoundException)

Example 3 with KeyElements

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

the class ReferablePersistenceManager method remove.

/**
 * Remove a {@link io.adminshell.aas.v3.model.Referable}
 *
 * @param reference of the referable which should be removed
 */
public void remove(Reference reference) throws ResourceNotFoundException {
    if (reference == null) {
        return;
    }
    if (reference.getKeys() != null && reference.getKeys().size() > 0) {
        KeyElements lastKeyElementOfReference = reference.getKeys().get(reference.getKeys().size() - 1).getType();
        Class clazz = AasUtils.keyTypeToClass(lastKeyElementOfReference);
        if (Identifiable.class.isAssignableFrom(clazz)) {
            // TODO: Build Identifier and forward remove reuquest to remove(Identifier id)
            return;
        }
        if (SubmodelElement.class.isAssignableFrom(clazz)) {
            clazz = SubmodelElement.class;
        }
        Referable referable = AasUtils.resolve(reference, this.aasEnvironment);
        if (referable == null) {
            throw new ResourceNotFoundException(String.format("Resource not found with reference {}", AasUtils.asString(reference)));
        }
        if (reference.getKeys().size() > 1) {
            Reference parent = new DefaultReference.Builder().keys(reference.getKeys().subList(0, reference.getKeys().size() - 1)).build();
            Referable parentReferable = AasUtils.resolve(parent, this.aasEnvironment);
            Method method = EnvironmentHelper.getGetReferableListMethod(clazz, parentReferable);
            if (method != null) {
                try {
                    List<Referable> referableList = (List<Referable>) method.invoke(parentReferable);
                    referableList.remove(referable);
                } catch (InvocationTargetException | IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
Also used : Referable(io.adminshell.aas.v3.model.Referable) Reference(io.adminshell.aas.v3.model.Reference) DefaultReference(io.adminshell.aas.v3.model.impl.DefaultReference) ArrayList(java.util.ArrayList) List(java.util.List) KeyElements(io.adminshell.aas.v3.model.KeyElements) Method(java.lang.reflect.Method) ResourceNotFoundException(de.fraunhofer.iosb.ilt.faaast.service.exception.ResourceNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

ResourceNotFoundException (de.fraunhofer.iosb.ilt.faaast.service.exception.ResourceNotFoundException)3 KeyElements (io.adminshell.aas.v3.model.KeyElements)3 ArrayList (java.util.ArrayList)3 Referable (io.adminshell.aas.v3.model.Referable)2 Reference (io.adminshell.aas.v3.model.Reference)2 Submodel (io.adminshell.aas.v3.model.Submodel)2 SubmodelElement (io.adminshell.aas.v3.model.SubmodelElement)2 SubmodelElementCollection (io.adminshell.aas.v3.model.SubmodelElementCollection)2 DefaultReference (io.adminshell.aas.v3.model.impl.DefaultReference)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 List (java.util.List)2 Extend (de.fraunhofer.iosb.ilt.faaast.service.model.api.modifier.Extend)1 QueryModifier (de.fraunhofer.iosb.ilt.faaast.service.model.api.modifier.QueryModifier)1 EnvironmentHelper (de.fraunhofer.iosb.ilt.faaast.service.persistence.memory.util.EnvironmentHelper)1 DeepCopyHelper (de.fraunhofer.iosb.ilt.faaast.service.util.DeepCopyHelper)1 AasUtils (io.adminshell.aas.v3.dataformat.core.util.AasUtils)1 Identifiable (io.adminshell.aas.v3.model.Identifiable)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1