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!");
}
}
}
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");
}
}
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;
}
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);
}
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);
}
Aggregations