org.javafmi.skeleton.actions.GetReal Maven / Gradle / Ivy
/*
* 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.actions;
import org.javafmi.framework.FmiSimulation;
import org.javafmi.skeleton.Action;
import static org.javafmi.framework.FmiSimulation.Status;
public class GetReal implements Action {
@Override
public String executeOn(FmiSimulation simulation, String... args) {
try {
return buildResponse(simulation.getReal(extractValueReferencesFromArgs(args)));
} catch (Exception e) {
return Status.ERROR.toString();
}
}
public int[] extractValueReferencesFromArgs(String[] args) {
int[] valueReferences = new int[args.length];
for (int i = 0; i < args.length; i++) {
valueReferences[i] = Integer.valueOf(args[i]);
}
return valueReferences;
}
public String buildResponse(double[] realValues) {
StringBuilder responseBuilder = new StringBuilder(Status.OK.toString() + " ");
for (double real : realValues) {
responseBuilder.append(real).append(" ");
}
return responseBuilder.toString().trim();
}
}