Skip to content

Commit 99ffa7b

Browse files
committed
MATLABService supports MATLABCommands
MATLABService is now a SingletonService typed on MATLABCommands. It is responsible for querying all MATLABCommands and creating appropriate variables within MATLAB to access their utility methods.
1 parent 96c2bbc commit 99ffa7b

File tree

2 files changed

+100
-6
lines changed

2 files changed

+100
-6
lines changed

src/main/java/org/scijava/plugins/scripting/matlab/DefaultMATLABService.java

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,89 @@
3131

3232
package org.scijava.plugins.scripting.matlab;
3333

34+
import javax.script.ScriptEngine;
35+
3436
import matlabcontrol.extensions.MatlabNumericArray;
3537

38+
import org.scijava.log.LogService;
39+
import org.scijava.plugin.AbstractSingletonService;
3640
import org.scijava.plugin.Parameter;
3741
import org.scijava.plugin.Plugin;
3842
import org.scijava.script.ScriptService;
39-
import org.scijava.service.AbstractService;
4043
import org.scijava.service.Service;
4144

4245
/**
46+
* Default {@link MATLABService} implementation.
47+
*
4348
* @author Mark Hiner
4449
*/
4550
@Plugin(type = Service.class)
46-
public class DefaultMATLABService extends AbstractService implements
47-
MATLABService
51+
public class DefaultMATLABService extends
52+
AbstractSingletonService<MATLABCommands> implements MATLABService
4853
{
4954

5055
@Parameter
5156
private ScriptService scriptService;
5257

58+
@Parameter
59+
private LogService logService;
60+
61+
private boolean initializedCommands = false;
62+
63+
@Override
64+
public String commandHelp() {
65+
String helpMessage = "--- MATLAB Command Plugins ---\n\n";
66+
67+
for (final MATLABCommands command : getInstances()) {
68+
helpMessage += command.usage();
69+
}
70+
71+
helpMessage += "\n";
72+
73+
return helpMessage;
74+
}
75+
76+
@Override
77+
public void initializeCommands() {
78+
if (!initializedCommands) createCommandVariables();
79+
}
80+
81+
@Override
82+
public void makeMATLABVariable(final String name, final Object value) {
83+
final ScriptEngine engine =
84+
scriptService.getLanguageByName("MATLAB").getScriptEngine();
85+
engine.put(name, value);
86+
}
87+
5388
// -- Service methods --
5489

5590
@Override
5691
public void initialize() {
92+
// Register known data type aliases for use in script @parameters
5793
scriptService.addAlias("matrix", MatlabNumericArray.class);
5894
}
95+
96+
// -- Typed methods --
97+
98+
@Override
99+
public Class<MATLABCommands> getPluginType() {
100+
return MATLABCommands.class;
101+
}
102+
103+
// -- Helper methods --
104+
105+
/**
106+
* Helper method to create variables for each {@link MATLABCommands} within
107+
* MATLAB.
108+
*/
109+
private synchronized void createCommandVariables() {
110+
if (!initializedCommands) {
111+
for (final MATLABCommands command : getInstances()) {
112+
final String name = command.getInfo().getName();
113+
114+
makeMATLABVariable(name, command);
115+
}
116+
initializedCommands = true;
117+
}
118+
}
59119
}

src/main/java/org/scijava/plugins/scripting/matlab/MATLABService.java

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,45 @@
3030
*/
3131
package org.scijava.plugins.scripting.matlab;
3232

33-
import org.scijava.service.SciJavaService;
33+
import org.scijava.plugin.Plugin;
34+
import org.scijava.plugin.SingletonService;
35+
import org.scijava.service.Service;
3436

3537
/**
38+
* {@link Service} performing any utility operations required for MATLAB.
39+
*
3640
* @author Mark Hiner
3741
*/
38-
public interface MATLABService extends SciJavaService {
39-
// NB: No methods to declare
42+
public interface MATLABService extends SingletonService<MATLABCommands> {
43+
44+
/**
45+
* Builds a complete usage message by querying the
46+
* {@link MATLABCommands#usage()} method of all available commands.
47+
*
48+
* @return A formatted string of all available {@link MATLABCommands}.
49+
*/
50+
String commandHelp();
51+
52+
/**
53+
* Creates a MATLAB variable for each {@link MATLABCommands} plugin using the
54+
* {@link Plugin#name()} property as an identifier.
55+
* <p>
56+
* For example, if you create an {@code AwesomeMATLABCommands} class with
57+
* {@code name=FOO} and method {@code public void bar()}, after calling this
58+
* method you will be able to execute {@code FOO.bar} from within MATLAB.
59+
* </p>
60+
* <p>
61+
* NB: execution of this method requires a valid MATLAB installation.
62+
* </p>
63+
*/
64+
void initializeCommands();
65+
66+
/**
67+
* Attempts to create a variable in MATLAB with the give name and value.
68+
* <p>
69+
* NB: execution of this method should only be called from within a valid
70+
* MATLAB installation.
71+
* </p>
72+
*/
73+
void makeMATLABVariable(String name, Object value);
4074
}

0 commit comments

Comments
 (0)