org.javafmi.proxy.ModelDescriptionPeeker Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fmu-wrapper Show documentation
Show all versions of fmu-wrapper Show documentation
javaFMI is a Java Library for the functional mock-up interface (or FMI). FMI defines a standardized interface to be used in computer simulations. The FMI Standard has beed developed by a large number of software companies and research centers that have worked in a cooperation project under the name of MODELISAR. This library addresses the connection of a java application with a FMU (functional mock-up unit).
/*
* Copyright 2013-2016 SIANI - ULPGC
*
* This File is part of JavaFMI Project
*
* JavaFMI Project is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* JavaFMI Project is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JavaFMI. If not, see .
*/
package org.javafmi.proxy;
import org.javafmi.modeldescription.FmiVersion;
import java.io.*;
import java.util.LinkedHashMap;
import java.util.Map;
public class ModelDescriptionPeeker {
private static final String Version = "fmiVersion";
private static final String V1 = "1.0";
private static final String V2 = "2.0";
private static final Map versions;
static {
versions = new LinkedHashMap<>();
versions.put(V1, FmiVersion.One);
versions.put(V2, FmiVersion.Two);
}
public static String peekVersion(File modelDescriptionFile) {
return versions.get(readVersionFromFile(modelDescriptionFile));
}
private static String readVersionFromFile(File modelDescriptionFile) {
BufferedReader reader = getBufferedReader(modelDescriptionFile);
String line;
while (!(line = readLine(reader)).contains(Version)) ;
closeReader(reader);
return (line.contains(V1)) ? V1 : V2;
}
private static void closeReader(BufferedReader reader) {
try {
reader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static String readLine(BufferedReader reader) {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static BufferedReader getBufferedReader(File modelDescriptionFile) {
try {
return new BufferedReader(new FileReader(modelDescriptionFile));
} catch (FileNotFoundException e) {
throw new RuntimeException("Can not read the version from file " + modelDescriptionFile.getPath(), e);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy