Search in sources :

Example 1 with Deserializer

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

use of io.adminshell.aas.v3.dataformat.Deserializer 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 3 with Deserializer

use of io.adminshell.aas.v3.dataformat.Deserializer in project FAAAST-Service by FraunhoferIOSB.

the class AASEnvironmentFactory method initDeserializer.

private void initDeserializer() {
    // TODO: AASX Deserializer seems to be a little bit different since it needs an input in constructor
    deserializer = new HashMap<>();
    deserializer.put("JSON", new JsonDeserializer());
    deserializer.put("AML", new AmlDeserializer());
    deserializer.put("XML", new XmlDeserializer());
    deserializer.put("I4AAS/OPC UA Nodeset", new I4AASDeserializer());
    deserializer.put("RDF", new io.adminshell.aas.v3.dataformat.rdf.Serializer());
    deserializer.put("JSON-LD", new io.adminshell.aas.v3.dataformat.jsonld.Serializer());
}
Also used : AmlDeserializer(io.adminshell.aas.v3.dataformat.aml.AmlDeserializer) XmlDeserializer(io.adminshell.aas.v3.dataformat.xml.XmlDeserializer) I4AASDeserializer(io.adminshell.aas.v3.dataformat.i4aas.I4AASDeserializer) JsonDeserializer(io.adminshell.aas.v3.dataformat.json.JsonDeserializer)

Example 4 with Deserializer

use of io.adminshell.aas.v3.dataformat.Deserializer in project FAAAST-Service by FraunhoferIOSB.

the class StarterTest method testAASEnvironment.

private void testAASEnvironment(String filePath, Deserializer deserializer) throws Exception, FileNotFoundException, DeserializationException {
    AssetAdministrationShellEnvironment expected = deserializer.read(new File(filePath));
    AssetAdministrationShellEnvironment actual = environmentFactory.getAASEnvironment(filePath);
    Assert.assertEquals(expected, actual);
}
Also used : File(java.io.File) AssetAdministrationShellEnvironment(io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment) DefaultAssetAdministrationShellEnvironment(io.adminshell.aas.v3.model.impl.DefaultAssetAdministrationShellEnvironment)

Aggregations

AmlDeserializer (io.adminshell.aas.v3.dataformat.aml.AmlDeserializer)3 I4AASDeserializer (io.adminshell.aas.v3.dataformat.i4aas.I4AASDeserializer)3 JsonDeserializer (io.adminshell.aas.v3.dataformat.json.JsonDeserializer)3 XmlDeserializer (io.adminshell.aas.v3.dataformat.xml.XmlDeserializer)3 AssetAdministrationShellEnvironment (io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment)3 DefaultAssetAdministrationShellEnvironment (io.adminshell.aas.v3.model.impl.DefaultAssetAdministrationShellEnvironment)3 DeserializationException (io.adminshell.aas.v3.dataformat.DeserializationException)2 Deserializer (io.adminshell.aas.v3.dataformat.Deserializer)2 IOException (java.io.IOException)2 File (java.io.File)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1