Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package org.n52.matlab.control;
/*
* Copyright (c) 2013, Joshua Kaplan
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
* following conditions are met:
* - Redistributions of source code must retain the above copyright notice, this list of conditions and the following
* disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other materials provided with the distribution.
* - Neither the name of matlabcontrol nor the names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import com.mathworks.jmi.Matlab;
import com.mathworks.jmi.NativeMatlab;
import java.awt.AWTEvent;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.atomic.AtomicReference;
import org.n52.matlab.control.MatlabProxy.MatlabThreadCallable;
import org.n52.matlab.control.MatlabProxy.MatlabThreadProxy;
/**
* Interacts with MATLAB via the undocumented Java MATLAB Interface (JMI).
*
* This code is inspired by Kamin Whitehouse's
* MatlabControl. Fixes to concurrency
* bugs in this class have been aided by the feedback of several matlabcontrol users, thank you for your feedback!
*
* This class runs inside of MATLAB's Java Virtual Machine and relies upon the Java MATLAB Interface which is
* distributed by MathWorks as {@code jmi.jar}. It allows for Java to send {@code eval} and {@code feval} statements to
* MATLAB and receive results. {@code jmi.jar} is not distributed with matlabcontrol as it is the property of
* MathWorks. If you wish to compile the source code you will need to reference the version of {@code jmi.jar} that is
* distributed with your copy of MATLAB. It is located at {@code matlabroot/java/jar/jmi.jar} where {@code matlabroot}
* is the location of your MATLAB installation. The location of {@code matlabroot} can be determined by executing the
* {@code matlabroot} command in the MATLAB Command Window.
*
* This is the only class in matlabcontrol which directly links against code in {@code jmi.jar}. (And therefore also the
* only class that needs {@code jmi.jar} to be on the classpath in order to compile.) {@link Configuration} also uses
* code in {@code jmi.jar} but uses reflection to interact with it.
*
* @since 3.0.0
*
* @author Joshua Kaplan
*/
class JMIWrapper
{
private static final MatlabThreadOperations THREAD_OPERATIONS = new MatlabThreadOperations();
private static final EventQueue EVENT_QUEUE = Toolkit.getDefaultToolkit().getSystemEventQueue();
private static final Method EVENT_QUEUE_DISPATCH_METHOD;
static
{
try
{
EVENT_QUEUE_DISPATCH_METHOD = EventQueue.class.getDeclaredMethod("dispatchEvent", AWTEvent.class);
}
catch(NoSuchMethodException e)
{
throw new IllegalStateException("java.awt.EventQueue's protected void dispatchEvent(java.awt.AWTEvent) " +
"method could not be found", e);
}
EVENT_QUEUE_DISPATCH_METHOD.setAccessible(true);
}
private JMIWrapper() { }
/**
* Exits MATLAB without waiting for MATLAB to return, because MATLAB will not return when exiting.
*
* @throws MatlabInvocationException
*/
static void exit()
{
Runnable runnable = new Runnable()
{
@Override
public void run()
{
try
{
Matlab.mtFevalConsoleOutput("exit", null, 0);
}
//This should never fail, and if it does there is no way to consistently report it back to the caller
//because this method does not block
catch(Exception e) { }
}
};
if(NativeMatlab.nativeIsMatlabThread())
{
runnable.run();
}
else
{
Matlab.whenMatlabIdle(runnable);
}
}
//The following functions wait for MATLAB to complete the computation before returning
//See MatlabProxy for the method documentation, acts as if running inside MATLAB
//(A LocalMatlabProxy is just a thin wrapper around these methods)
static void setVariable(final String variableName, final Object value) throws MatlabInvocationException
{
invokeAndWait(new MatlabThreadCallable()
{
@Override
public Void call(MatlabThreadProxy proxy) throws MatlabInvocationException
{
proxy.setVariable(variableName, value);
return null;
}
});
}
static Object getVariable(final String variableName) throws MatlabInvocationException
{
return invokeAndWait(new MatlabThreadCallable