matlabcontrol.extensions.CallbackMatlabProxy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of matconsolectl Show documentation
Show all versions of matconsolectl Show documentation
MatConsoleCtl - control MATLAB from Java
/*
* Code licensed under new-style BSD (see LICENSE).
* All code up to tags/original: Copyright (c) 2013, Joshua Kaplan
* All code after tags/original: Copyright (c) 2016, DiffPlug
*/
package matlabcontrol.extensions;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import matlabcontrol.MatlabInvocationException;
import matlabcontrol.MatlabProxy;
import matlabcontrol.MatlabProxy.MatlabThreadCallable;
/**
* @deprecated This class was provided as a workaround when {@link MatlabProxy} could not operate on the Event Dispatch
* Thread (EDT) used by AWT and Swing; {@code MatlabProxy} no longer has this limitation.
*
* Wraps around a {@link MatlabProxy} making the method calls which interact with MATLAB operate with callbacks instead
* of return values. For each method in {@code MatlabProxy} that interacts with MATLAB, the same method exists but has
* one additional parameter that is either {@link MatlabCallback} or {@link MatlabDataCallback}. Method invocations do
* not throw {@link MatlabInvocationException}s, but if the proxy throws a {@code MatlabInvocationException} it will be
* provided to the callback.
*
* All interactions with the proxy will be done in a single threaded manner. The underlying proxy methods will be
* completed in the order their corresponding methods in this class were called. Because method invocations on the
* proxy occur on a separate thread from the one calling the methods in this class, it can be used from within MATLAB on
* the Event Dispatch Thread (EDT).
*
* This class is unconditionally thread-safe. There are no guarantees about the relative ordering of method completion
* when methods are invoked both on an instance of this class and on the proxy provided to it.
*
* @since 4.0.0
*
* @author Joshua Kaplan
*/
public class CallbackMatlabProxy {
/**
* Executor that manages the single daemon thread used to invoke methods on the proxy.
*/
private final ExecutorService _executor = Executors.newFixedThreadPool(1, new DaemonThreadFactory());
private final MatlabProxy _proxy;
/**
* Constructs an instance of this class, delegating all method invocations to the {@code proxy}.
*
* @param proxy
*/
public CallbackMatlabProxy(MatlabProxy proxy) {
_proxy = proxy;
}
/**
* Returns a brief description. The exact details of this representation are unspecified and are subject to change.
*
* @return
*/
@Override
public String toString() {
return "[" + this.getClass().getName() + " proxy=" + _proxy + "]";
}
/**
* Delegates to the proxy, calling the {@code callback} when the proxy's corresponding method has completed.
*/
public void isConnected(final MatlabDataCallback callback) {
_executor.submit(new Runnable() {
@Override
public void run() {
boolean connected = _proxy.isConnected();
callback.invocationSucceeded(connected);
}
});
}
/**
* Delegates to the proxy, calling the {@code callback} when the proxy's corresponding method has completed.
*
* @param callback
*/
public void disconnect(final MatlabDataCallback callback) {
_executor.submit(new Runnable() {
@Override
public void run() {
boolean succeeded = _proxy.disconnect();
callback.invocationSucceeded(succeeded);
}
});
}
/**
* Delegates to the proxy, calling the {@code callback} when the proxy's corresponding method has completed.
*
* @param callback
*/
public void exit(final MatlabCallback callback) {
_executor.submit(new Runnable() {
@Override
public void run() {
try {
_proxy.exit();
callback.invocationSucceeded();
} catch (MatlabInvocationException e) {
callback.invocationFailed(e);
}
}
});
}
/**
* Delegates to the proxy, calling the {@code callback} when the proxy's corresponding method has completed.
*
* @param callback
* @param command
*/
public void eval(final MatlabCallback callback, final String command) {
_executor.submit(new Runnable() {
@Override
public void run() {
try {
_proxy.eval(command);
callback.invocationSucceeded();
} catch (MatlabInvocationException e) {
callback.invocationFailed(e);
}
}
});
}
/**
* Delegates to the proxy, calling the {@code callback} when the proxy's corresponding method has completed.
*
* @param callback
* @param command
* @param nargout
*/
public void returningEval(final MatlabDataCallback
© 2015 - 2024 Weber Informatics LLC | Privacy Policy