Search in sources :

Example 1 with AssetAdministrationShellEnvironment

use of io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment 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 2 with AssetAdministrationShellEnvironment

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

the class Application method run.

@Override
public void run() {
    try {
        ConfigFactory configFactory = new ConfigFactory();
        AASEnvironmentFactory environmentFactory = new AASEnvironmentFactory();
        readConfigurationParametersOverEnvironmentVariables();
        readFilePathsOverEnvironmentVariables();
        List<Config> customConfigComponents = getCustomConfigComponents();
        ServiceConfig config = configFactory.toServiceConfig(configFilePath, autoCompleteConfiguration, properties, customConfigComponents);
        AssetAdministrationShellEnvironment environment = null;
        if (useEmptyAASEnvironment) {
            LOGGER.info("Using empty Asset Administration Shell Environment");
            environment = environmentFactory.getEmptyAASEnvironment();
        } else {
            environment = environmentFactory.getAASEnvironment(aasEnvironmentFilePath);
            LOGGER.info("Successfully parsed Asset Administration Shell Environment");
        }
        if (validateAASEnv) {
            validate(environment);
        }
        service = new Service(config);
        service.setAASEnvironment(environment);
        service.start();
        LOGGER.info("FAAAST Service is running!");
    } catch (Exception ex) {
        if (service != null) {
            service.stop();
        }
        LOGGER.error(ex.getMessage());
        LOGGER.error("Abort starting FAAAST Service");
    }
}
Also used : ServiceConfig(de.fraunhofer.iosb.ilt.faaast.service.config.ServiceConfig) EndpointConfig(de.fraunhofer.iosb.ilt.faaast.service.endpoint.EndpointConfig) HttpEndpointConfig(de.fraunhofer.iosb.ilt.faaast.service.endpoint.http.HttpEndpointConfig) ServiceConfig(de.fraunhofer.iosb.ilt.faaast.service.config.ServiceConfig) Config(de.fraunhofer.iosb.ilt.faaast.service.config.Config) Service(de.fraunhofer.iosb.ilt.faaast.service.Service) AssetAdministrationShellEnvironment(io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment)

Example 3 with AssetAdministrationShellEnvironment

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

the class AASEnvironmentFactory method guessDeserializerAndTryToParse.

private AssetAdministrationShellEnvironment guessDeserializerAndTryToParse(String envFilePath, String env) {
    Deserializer approxDeserializer = null;
    String fileEnding = envFilePath.split("\\.")[1];
    if (!envFilePath.equalsIgnoreCase(Application.DEFAULT_AASENV_PATH)) {
        LOGGER.debug("Looking for Deserializer for file ending '" + fileEnding + "'");
        approxDeserializer = deserializer.getOrDefault(deserializer.keySet().stream().filter(x -> x.equalsIgnoreCase(fileEnding)).findFirst().orElseGet(null), null);
    }
    if (approxDeserializer != null) {
        try {
            LOGGER.debug("Try resolving with '" + approxDeserializer.getClass().getSimpleName() + "'");
            AssetAdministrationShellEnvironment environment = approxDeserializer.read(env);
            // but returns an empty AASEnvironment
            if (!Objects.equals(environment, new DefaultAssetAdministrationShellEnvironment())) {
                return environment;
            }
        } catch (Exception ignored) {
            LOGGER.debug("Resolving with '" + approxDeserializer.getClass().getSimpleName() + "' was not successfull. Try other Deserializers.");
        }
    }
    return null;
}
Also used : DefaultAssetAdministrationShellEnvironment(io.adminshell.aas.v3.model.impl.DefaultAssetAdministrationShellEnvironment) JsonDeserializer(io.adminshell.aas.v3.dataformat.json.JsonDeserializer) AmlDeserializer(io.adminshell.aas.v3.dataformat.aml.AmlDeserializer) Deserializer(io.adminshell.aas.v3.dataformat.Deserializer) I4AASDeserializer(io.adminshell.aas.v3.dataformat.i4aas.I4AASDeserializer) XmlDeserializer(io.adminshell.aas.v3.dataformat.xml.XmlDeserializer) AssetAdministrationShellEnvironment(io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment) DefaultAssetAdministrationShellEnvironment(io.adminshell.aas.v3.model.impl.DefaultAssetAdministrationShellEnvironment) DeserializationException(io.adminshell.aas.v3.dataformat.DeserializationException) IOException(java.io.IOException)

Example 4 with AssetAdministrationShellEnvironment

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

the class AASEnvironmentFactory method getAASEnvironment.

/**
 * Parses the content in the given file path to an
 * {@link io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment}.
 * Supported formats of the file:
 * <p>
 * <ul>
 * <li>json
 * <li>aml
 * <li>xml
 * <li>opcua nodeset (also as .xml)
 * <li>rdf
 * <li>json-ld
 * </ul>
 * The method retrieves the right deserializer and parses the content to an
 * {@link io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment}.
 *
 * @param envFilePath of the file which contains the Asset Administration Shell Environment
 * @return the parsed Asset Administration Shell Environment object
 * @throws Exception
 */
public AssetAdministrationShellEnvironment getAASEnvironment(String envFilePath) throws Exception {
    initDeserializer();
    String env = getFileContent(envFilePath);
    LOGGER.info("Try to resolve Asset Administration Shell Environment from file '" + envFilePath + "'");
    AssetAdministrationShellEnvironment parsedEnvironment = guessDeserializerAndTryToParse(envFilePath, env);
    if (parsedEnvironment != null) {
        return parsedEnvironment;
    }
    // else try every deserializer
    String formats = "";
    for (Map.Entry<String, Deserializer> deserializer : deserializer.entrySet()) {
        try {
            LOGGER.debug("Try resolving with '" + deserializer.getValue().getClass().getSimpleName() + "'");
            formats += "\t" + deserializer.getKey() + "\n";
            return deserializer.getValue().read(env);
        } catch (DeserializationException ex) {
        }
    }
    throw new Exception("Could not deserialize content to an Asset Administration Shell Environment. Used File: " + envFilePath + "\nSupported Formats:\n" + formats);
}
Also used : JsonDeserializer(io.adminshell.aas.v3.dataformat.json.JsonDeserializer) AmlDeserializer(io.adminshell.aas.v3.dataformat.aml.AmlDeserializer) Deserializer(io.adminshell.aas.v3.dataformat.Deserializer) I4AASDeserializer(io.adminshell.aas.v3.dataformat.i4aas.I4AASDeserializer) XmlDeserializer(io.adminshell.aas.v3.dataformat.xml.XmlDeserializer) HashMap(java.util.HashMap) Map(java.util.Map) AssetAdministrationShellEnvironment(io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment) DefaultAssetAdministrationShellEnvironment(io.adminshell.aas.v3.model.impl.DefaultAssetAdministrationShellEnvironment) DeserializationException(io.adminshell.aas.v3.dataformat.DeserializationException) DeserializationException(io.adminshell.aas.v3.dataformat.DeserializationException) IOException(java.io.IOException)

Example 5 with AssetAdministrationShellEnvironment

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

the class StarterTest method testGetAASEnvironmentDefault.

@Test
public void testGetAASEnvironmentDefault() {
    AssetAdministrationShellEnvironment expected = new DefaultAssetAdministrationShellEnvironment();
    AssetAdministrationShellEnvironment actual = environmentFactory.getEmptyAASEnvironment();
    Assert.assertEquals(expected, actual);
}
Also used : DefaultAssetAdministrationShellEnvironment(io.adminshell.aas.v3.model.impl.DefaultAssetAdministrationShellEnvironment) AssetAdministrationShellEnvironment(io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment) DefaultAssetAdministrationShellEnvironment(io.adminshell.aas.v3.model.impl.DefaultAssetAdministrationShellEnvironment) Test(org.junit.Test)

Aggregations

AssetAdministrationShellEnvironment (io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment)6 DefaultAssetAdministrationShellEnvironment (io.adminshell.aas.v3.model.impl.DefaultAssetAdministrationShellEnvironment)4 DeserializationException (io.adminshell.aas.v3.dataformat.DeserializationException)2 Deserializer (io.adminshell.aas.v3.dataformat.Deserializer)2 AmlDeserializer (io.adminshell.aas.v3.dataformat.aml.AmlDeserializer)2 I4AASDeserializer (io.adminshell.aas.v3.dataformat.i4aas.I4AASDeserializer)2 JsonDeserializer (io.adminshell.aas.v3.dataformat.json.JsonDeserializer)2 XmlDeserializer (io.adminshell.aas.v3.dataformat.xml.XmlDeserializer)2 IOException (java.io.IOException)2 Service (de.fraunhofer.iosb.ilt.faaast.service.Service)1 Config (de.fraunhofer.iosb.ilt.faaast.service.config.Config)1 ServiceConfig (de.fraunhofer.iosb.ilt.faaast.service.config.ServiceConfig)1 EndpointConfig (de.fraunhofer.iosb.ilt.faaast.service.endpoint.EndpointConfig)1 HttpEndpointConfig (de.fraunhofer.iosb.ilt.faaast.service.endpoint.http.HttpEndpointConfig)1 ResourceNotFoundException (de.fraunhofer.iosb.ilt.faaast.service.exception.ResourceNotFoundException)1 ReflectionHelper (io.adminshell.aas.v3.dataformat.core.ReflectionHelper)1 AasUtils (io.adminshell.aas.v3.dataformat.core.util.AasUtils)1 Identifier (io.adminshell.aas.v3.model.Identifier)1 Key (io.adminshell.aas.v3.model.Key)1 KeyElements (io.adminshell.aas.v3.model.KeyElements)1