|
31 | 31 |
|
32 | 32 | package org.scijava.plugins.scripting.matlab; |
33 | 33 |
|
| 34 | +import javax.script.ScriptEngine; |
| 35 | + |
34 | 36 | import matlabcontrol.extensions.MatlabNumericArray; |
35 | 37 |
|
| 38 | +import org.scijava.log.LogService; |
| 39 | +import org.scijava.plugin.AbstractSingletonService; |
36 | 40 | import org.scijava.plugin.Parameter; |
37 | 41 | import org.scijava.plugin.Plugin; |
38 | 42 | import org.scijava.script.ScriptService; |
39 | | -import org.scijava.service.AbstractService; |
40 | 43 | import org.scijava.service.Service; |
41 | 44 |
|
42 | 45 | /** |
| 46 | + * Default {@link MATLABService} implementation. |
| 47 | + * |
43 | 48 | * @author Mark Hiner |
44 | 49 | */ |
45 | 50 | @Plugin(type = Service.class) |
46 | | -public class DefaultMATLABService extends AbstractService implements |
47 | | - MATLABService |
| 51 | +public class DefaultMATLABService extends |
| 52 | + AbstractSingletonService<MATLABCommands> implements MATLABService |
48 | 53 | { |
49 | 54 |
|
50 | 55 | @Parameter |
51 | 56 | private ScriptService scriptService; |
52 | 57 |
|
| 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 | + |
53 | 88 | // -- Service methods -- |
54 | 89 |
|
55 | 90 | @Override |
56 | 91 | public void initialize() { |
| 92 | + // Register known data type aliases for use in script @parameters |
57 | 93 | scriptService.addAlias("matrix", MatlabNumericArray.class); |
58 | 94 | } |
| 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 | + } |
59 | 119 | } |
0 commit comments