All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.javafmi.proxy.ProxyFactory Maven / Gradle / Ivy

Go to download

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).

There is a newer version: 2.26.5
Show newest version
/*
 *  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.kernel.OS;
import org.javafmi.modeldescription.FmiVersion;
import org.javafmi.modeldescription.ModelDescription;
import org.javafmi.proxy.v2.Fmu;
import org.javafmi.proxy.v2.JavaFmuProxy;

import java.io.File;

import static org.javafmi.kernel.Convention.FrameworkManufacturer;
import static org.javafmi.kernel.Convention.FrameworkVersion;

public class ProxyFactory {

	public static FmiProxy createProxy(ModelDescription modelDescription, String libraryPath) {
		if(!new File(libraryPath).exists()) throw new RuntimeException("FMU is not compatible with this system " + OS.name() + OS.architecture() + ". File not found: " + libraryPath);
		if (modelDescription.getFmiVersion().equals(FmiVersion.One))
			return new org.javafmi.proxy.v1.FmuProxy(libraryPath, (org.javafmi.modeldescription.v1.ModelDescription) modelDescription);
		else {
			org.javafmi.modeldescription.v2.ModelDescription md = (org.javafmi.modeldescription.v2.ModelDescription) modelDescription;
			if (isJavaFMU(modelDescription) && isSameVersion(modelDescription)) return new JavaFmuProxy(md);
            else if(modelDescription.getFmiVersion().equals(FmiVersion.Two))
                return new org.javafmi.proxy.v2.FmuProxy(new Fmu(libraryPath, md), md);
            else if(modelDescription.getFmiVersion().equals(FmiVersion.TwoHybrid))
                return new org.javafmi.proxy.v21.FmuProxy(new org.javafmi.proxy.v21.Fmu(libraryPath, modelDescription.getGenerationTool()), md);
		}
        System.out.println("FMI version not found " + modelDescription.getFmiVersion());
        return null;
    }

	private static boolean isJavaFMU(ModelDescription modelDescription) {
		return modelDescription.getGenerationTool().startsWith(FrameworkManufacturer);
	}

	private static boolean isSameVersion(ModelDescription modelDescription) {
		return warn(modelDescription.getGenerationTool().endsWith(FrameworkVersion));
	}

	private static boolean warn(boolean sameVersion) {
		if (!sameVersion) System.out.println("WARNING: This wrapper version is not compatible with this JavaFMU. Performance will be worse");
		return sameVersion;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy