org.javafmi.skeleton.SimulationSkeleton 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.skeleton;
import org.javafmi.framework.FmiSimulation;
import org.javafmi.framework.Logger;
import org.javafmi.skeleton.actions.Terminate;
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
import static org.javafmi.skeleton.ActionFactory.get;
public class SimulationSkeleton {
private static final boolean AutoFlush = true;
public static void main(String[] args) throws FileNotFoundException {
try {
new Proxy(instantiate(className())).listen();
} catch (Throwable e) {
show(e);
}
}
public static FmiSimulation instantiate(String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
Class> aClass = Class.forName(className);
FmiSimulation simulation = (FmiSimulation) aClass.newInstance();
simulation.logger(new Logger(className, System.out));
return simulation;
}
private static String className() throws FileNotFoundException {
return new Scanner(new File("MANIFEST")).useDelimiter("\\Z").next();
}
private static void show(Throwable e) throws FileNotFoundException {
PrintWriter printWriter = new PrintWriter(new FileOutputStream(className() + "_error.log"));
e.printStackTrace(printWriter);
printWriter.close();
}
static class Proxy {
private final FmiSimulation simulation;
private boolean continueListening;
private BufferedReader reader;
private PrintWriter writer;
Proxy(FmiSimulation simulation) {
this.simulation = simulation;
this.reader = new BufferedReader(new InputStreamReader(System.in));
this.writer = new PrintWriter(System.err, AutoFlush);
}
void listen() {
continueListening = true;
String message;
while (continueListening) {
if ((message = receiveMessage()) == null) break;
processRequest(message);
}
}
String receiveMessage() {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
void processRequest(String command) {
Request request = new Request(command);
Action action = get(request.action());
writer.println(action.executeOn(simulation, request.arguments()));
if (action instanceof Terminate) close();
}
void close() {
continueListening = false;
}
private class Request {
private final String[] arguments;
private final String commandName;
Request(String request) {
String[] dividedRequest = request.split(" ");
this.commandName = dividedRequest[0];
this.arguments = Arrays.copyOfRange(dividedRequest, 1, dividedRequest.length);
}
String[] arguments() {
return arguments;
}
String action() {
return commandName;
}
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy