Changeset 90 for java/main/src
- Timestamp:
- 07/02/13 16:20:07 (11 years ago)
- Location:
- java/main/src
- Files:
-
- 27 added
- 7 deleted
- 85 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/communication/ClientConnection.java
r88 r90 45 45 protected void joinableStart() { 46 46 try { 47 log. info("connecting to " + address);47 log.debug("connecting to " + address); 48 48 49 49 socket = new Socket(hostName, port); … … 51 51 socket.setSoTimeout(500); 52 52 53 log. info("connected to " + hostName + ":" + port);53 log.debug("connected to " + hostName + ":" + port); 54 54 connected = true; 55 55 runThreads(); … … 145 145 146 146 public void dispatchResponseProcess(final Response response) { 147 Dispatching. invokeLaterOrNow(dispatcher, new RunAt<C>() {147 Dispatching.dispatchIfNotActive(dispatcher, new RunAt<C>() { 148 148 @Override 149 149 public void run() { … … 193 193 sentQuery.dispatcher = dispatcher; 194 194 195 senderThread. invokeLater(new RunAt<Connection>(){195 senderThread.dispatch(new RunAt<Connection>(){ 196 196 @Override 197 197 public void run() { -
java/main/src/main/java/com/framsticks/communication/Connection.java
r88 r90 135 135 Dispatching.use(threads, this); 136 136 137 receiverThread. invokeLater(new RunAt<Connection>() {137 receiverThread.dispatch(new RunAt<Connection>() { 138 138 @Override 139 139 public void run() { -
java/main/src/main/java/com/framsticks/communication/ServerConnection.java
r88 r90 66 66 67 67 protected final void respond(final Response response, final Integer id) { 68 senderThread. invokeLater(new RunAt<Connection>() {68 senderThread.dispatch(new RunAt<Connection>() { 69 69 @Override 70 70 public void run() { -
java/main/src/main/java/com/framsticks/communication/Subscription.java
r85 r90 75 75 76 76 public void dispatchCall(final List<File> files) { 77 Dispatching. invokeLaterOrNow(dispatcher, new RunAt<C>() {77 Dispatching.dispatchIfNotActive(dispatcher, new RunAt<C>() { 78 78 @Override 79 79 public void run() { -
java/main/src/main/java/com/framsticks/core/Framsticks.java
r88 r90 4 4 import com.framsticks.parsers.XmlLoader; 5 5 import com.framsticks.util.dispatching.JoinableCollection; 6 import com.framsticks.util.dispatching. JoinableMonitor;6 import com.framsticks.util.dispatching.Monitor; 7 7 8 8 import org.apache.log4j.Logger; … … 23 23 24 24 public static Framsticks loadConfiguration(InputStream stream) { 25 return new XmlLoader().load( stream, Framsticks.class);25 return new XmlLoader().load(Framsticks.class, stream); 26 26 } 27 27 28 28 public static void main(final String[] args) { 29 29 30 final Framsticks framsticks = loadConfiguration(Framsticks.class.getResourceAsStream("/configs/framsticks.xml")); 31 32 new JoinableMonitor(framsticks).use().waitFor().drop().join(); 33 34 // monitor.use(); 35 // monitor.waitFor(); 36 // monitor.drop(); 37 // monitor.join(); 30 new Monitor(loadConfiguration(Framsticks.class.getResourceAsStream("/configs/framsticks.xml"))) 31 .use() 32 .waitFor() 33 .drop() 34 .join(); 38 35 39 36 log.info("exiting main"); -
java/main/src/main/java/com/framsticks/core/Instance.java
r88 r90 8 8 9 9 import javax.annotation.Nonnull; 10 import javax.annotation.OverridingMethodsMustInvokeSuper;11 10 12 11 import org.apache.log4j.Logger; … … 14 13 import com.framsticks.communication.File; 15 14 import com.framsticks.params.AccessInterface; 15 import com.framsticks.params.CompositeParam; 16 16 import com.framsticks.params.ConstructionException; 17 17 import com.framsticks.params.FramsClass; 18 18 import com.framsticks.params.ListAccess; 19 19 import com.framsticks.params.Param; 20 import com.framsticks.params.ParamsPackage; 20 21 import com.framsticks.params.Registry; 21 22 import com.framsticks.params.ValueParam; 23 import com.framsticks.params.annotations.AutoAppendAnnotation; 22 24 import com.framsticks.params.annotations.FramsClassAnnotation; 23 25 import com.framsticks.params.types.ObjectParam; 26 import com.framsticks.params.types.ProcedureParam; 24 27 import com.framsticks.parsers.Loaders; 25 28 import com.framsticks.parsers.MultiParamLoader; 29 import com.framsticks.util.FramsticksException; 26 30 import com.framsticks.util.StateFunctor; 27 31 import com.framsticks.util.UnsupportedOperationException; … … 40 44 private static final Logger log = Logger.getLogger(Instance.class.getName()); 41 45 42 protected Node root; 46 private Node root; 47 48 protected @Nonnull Node setRoot(CompositeParam param, Object object) { 49 // if (isRootAssigned()) { 50 // throw new FramsticksException().msg("root is already assigned"); 51 // } 52 // assert isActive(); 53 root = new Node(param, object); 54 return root; 55 } 56 57 protected @Nonnull Node getRoot() { 58 // assert isActive(); 59 assert root != null; 60 return root; 61 } 62 63 public boolean isRootAssigned() { 64 // assert isActive(); 65 return root != null; 66 } 43 67 44 68 protected Set<InstanceListener> listeners = new HashSet<InstanceListener>(); … … 68 92 } 69 93 70 public void fetchValue(Path path, Param param, StateFunctor stateFunctor) { 71 stateFunctor.call(null); 72 } 73 74 public void fetchValues(Path path, StateFunctor stateFunctor) { 75 stateFunctor.call(null); 76 } 94 /** This is part of the Instance interface. 95 * 96 */ 97 public abstract void fetchValue(Path path, Param param, StateFunctor stateFunctor); 98 99 /** This is part of the Instance interface. 100 * 101 */ 102 public abstract void fetchValues(Path path, StateFunctor stateFunctor); 103 104 /** This is part of the Instance interface. 105 * 106 */ 107 public abstract void call(Path path, ProcedureParam param, Object[] arguments, StateFunctor stateFunctor); 77 108 78 109 protected void tryRegisterOnChangeEvents(Path path) { … … 82 113 public void storeValue(Path path, Param param, Object value, final StateFunctor stateFunctor) { 83 114 assert isActive(); 84 invokeLater(new RunAt<Instance>() {115 dispatch(new RunAt<Instance>() { 85 116 @Override 86 117 public void run() { … … 104 135 public void addListener(final InstanceListener listener) { 105 136 assert Dispatching.isThreadSafe(); 106 Dispatching. invokeLaterOrNow(this, new RunAt<Instance>() {137 Dispatching.dispatchIfNotActive(this, new RunAt<Instance>() { 107 138 @Override 108 139 public void run() { … … 114 145 public void removeListener(final InstanceListener listener) { 115 146 assert Dispatching.isThreadSafe(); 116 Dispatching. invokeLaterOrNow(this, new RunAt<Instance>() {147 Dispatching.dispatchIfNotActive(this, new RunAt<Instance>() { 117 148 @Override 118 149 public void run() { … … 142 173 public FramsClass getInfoFromCache(String id) { 143 174 assert isActive(); 144 return registry.get InfoFromCache(id);175 return registry.getFramsClass(id); 145 176 } 146 177 … … 169 200 } 170 201 202 public final AccessInterface bindAccess(String path) { 203 return bindAccess(getPath(path)); 204 } 205 171 206 public final AccessInterface bindAccess(Node node) { 207 assert isActive(); 172 208 assert node.getObject() != null; 173 209 174 210 try { 175 return registry.prepareAccess(node.getParam()).select(node.getObject()); 211 AccessInterface access = registry.prepareAccess(node.getParam()); 212 if (access == null) { 213 throw new FramsticksException().msg("failed to prepare access for param").arg("param", node.getParam()); 214 } 215 return access.select(node.getObject()); 176 216 } catch (ConstructionException e) { 177 217 log.error("failed to bind access for " + node.getParam() + ": " + e); … … 185 225 186 226 public final AccessInterface bindAccess(Path path) { 187 assert path.isResolved();227 path.assureResolved(); 188 228 return bindAccess(path.getTop()); 189 229 } … … 260 300 assert child != null; 261 301 if (path.size() == 1) { 262 root = new Node(root.getParam(), child);302 setRoot(getRoot().getParam(), child); 263 303 } else { 264 304 bindAccess(path.getUnder()).set(path.getTop().getParam(), child); … … 277 317 FramsClass framsClass = Loaders.loadFramsClass(file.getContent()); 278 318 if ("/".equals(file.getPath())) { 279 if ( root.getParam().getContainedTypeName() == null) {280 root = new Node(Param.build().name("Instance").id(getName()).type("o " + framsClass.getId()), root.getObject());281 } 282 } 283 registry.put InfoIntoCache(framsClass);319 if (getRoot().getParam().getContainedTypeName() == null) { 320 setRoot(Param.build().name("Instance").id(getName()).type("o " + framsClass.getId()).finish(CompositeParam.class), getRoot().getObject()); 321 } 322 } 323 registry.putFramsClass(framsClass); 284 324 return framsClass; 285 325 } … … 299 339 loader.go(); 300 340 fireFetch(path); 301 // for (NodeListener l : listeners) {302 // l.onChange(this);303 // }304 341 return; 305 342 } … … 318 355 String id = listAccess.computeIdentifierFor(accessInterface.getSelected()); 319 356 //TODO listAccessParam 320 Param param = Param.build(). type("o " + accessInterface.getId()).id(id).finish();357 Param param = Param.build().forAccess(accessInterface).id(id).finish(); 321 358 Object child = accessInterface.getSelected(); 322 359 accessInterface.select(null); … … 327 364 328 365 fireFetch(path); 329 // for (NodeListener l : listeners) {330 // l.onChange(this);331 // }332 366 } catch (Exception e) { 333 367 log.error("exception occurred while loading: " + e); … … 358 392 } 359 393 394 @AutoAppendAnnotation 395 public void usePackage(ParamsPackage paramsPackage) { 396 log.debug("using package " + paramsPackage + " in instance " + this); 397 paramsPackage.register(registry); 398 } 399 400 @AutoAppendAnnotation 401 public void takeFromRegistry(Registry registry) { 402 log.debug("taking from registry " + registry + " in instance " + this); 403 this.registry.takeAllFrom(registry); 404 } 405 360 406 @Override 361 @OverridingMethodsMustInvokeSuper 362 protected void firstTask() { 363 root = new Node(Param.build().name("Instance").id(getName()).type("o"), null); 364 com.framsticks.model.Package.register(registry); 407 protected void joinableStart() { 408 dispatch(new RunAt<Instance>() { 409 @Override 410 public void run() { 411 if (!isRootAssigned()) { 412 setRoot(Param.build().name("Instance").id(getName()).type("o").finish(CompositeParam.class), null); 413 } 414 } 415 }); 416 super.joinableStart(); 365 417 } 366 418 -
java/main/src/main/java/com/framsticks/core/LocalInstance.java
r88 r90 50 50 51 51 protected void acceptNext() { 52 acceptThread. invokeLater(new RunAt<Accept>() {52 acceptThread.dispatch(new RunAt<Accept>() { 53 53 @Override 54 54 public void run() { … … 57 57 assert socket != null; 58 58 log.debug("accepted socket: " + socket.getInetAddress().getHostAddress()); 59 invokeLater(new RunAt<LocalInstance>() {59 dispatch(new RunAt<LocalInstance>() { 60 60 @Override 61 61 public void run() { … … 74 74 75 75 public void tryBind(final Integer accept) { 76 acceptThread. invokeLater(new RunAt<Accept>() {76 acceptThread.dispatch(new RunAt<Accept>() { 77 77 @Override 78 78 public void run() { -
java/main/src/main/java/com/framsticks/core/Node.java
r87 r90 29 29 30 30 public Node(@Nonnull ParamBuilder builder, Object object) { 31 this( (CompositeParam) builder.finish(), object);31 this(builder.finish(CompositeParam.class), object); 32 32 } 33 33 -
java/main/src/main/java/com/framsticks/core/Path.java
r87 r90 4 4 import com.framsticks.params.CompositeParam; 5 5 import com.framsticks.params.Param; 6 import com.framsticks.util.FramsticksException; 6 7 import com.framsticks.util.dispatching.Dispatching; 7 8 … … 30 31 return null; 31 32 } 32 return (instance.registry.prepareAccess(param) != null) ? child : null; 33 try { 34 instance.registry.prepareAccess(param); 35 return child; 36 } catch (FramsticksException e) { 37 } 38 return null; 33 39 } 34 40 … … 119 125 this.instance = instance; 120 126 121 nodes.add(instance. root);122 Node current = instance. root;127 nodes.add(instance.getRoot()); 128 Node current = instance.getRoot(); 123 129 124 130 StringBuilder b = new StringBuilder(); … … 254 260 return ListUtils.unmodifiableList(nodes); 255 261 } 262 263 public void assureResolved() { 264 if (!isResolved()) { 265 throw new FramsticksException().msg("path is not resolved").arg("path", this); 266 } 267 } 256 268 } 257 269 -
java/main/src/main/java/com/framsticks/dumping/FileInstance.java
r88 r90 7 7 import com.framsticks.params.annotations.ParamAnnotation; 8 8 import com.framsticks.util.dispatching.Future; 9 import com.framsticks.util.dispatching.RunAt; 9 10 import com.framsticks.util.io.Encoding; 10 11 … … 17 18 import java.io.InputStreamReader; 18 19 19 import javax.annotation.OverridingMethodsMustInvokeSuper;20 20 21 21 /** … … 23 23 */ 24 24 @FramsClassAnnotation 25 public class FileInstance extends LocalInstance {25 public abstract class FileInstance extends LocalInstance { 26 26 27 27 private static final Logger log = Logger.getLogger(Instance.class.getName()); … … 42 42 43 43 @Override 44 @OverridingMethodsMustInvokeSuper45 protected void firstTask() {46 assert isActive();47 super.firstTask();48 try {49 LoadStream stream = new LoadStream(this.getRootPath(), new BufferedReader(new InputStreamReader(new FileInputStream(file), Encoding.getFramsticksCharset())), this, new Future<Path>() {50 @Override51 public void result(Path result, Exception e) {52 if (e != null) {53 log.error("failed to load file instance " + FileInstance.this + ": " + e);54 fireRun(e);55 return;56 }57 log.info("loaded file instance " + FileInstance.this);58 fireRun(null);59 }60 });61 stream.load();62 } catch (IOException e) {63 log.error("io failure: " + e);64 fireRun(e);65 }66 }67 68 @Override69 44 public String toString() { 70 45 return "file@" + file.getName(); 71 46 } 72 47 48 @Override 49 protected void joinableStart() { 50 dispatch(new RunAt<Instance>() { 73 51 52 @Override 53 public void run() { 54 assert isActive(); 55 56 try { 57 LoadStream stream = new LoadStream(FileInstance.this.getRootPath(), new BufferedReader(new InputStreamReader(new FileInputStream(file), Encoding.getFramsticksCharset())), FileInstance.this, new Future<Path>() { 58 @Override 59 public void result(Path result, Exception e) { 60 if (e != null) { 61 log.error("failed to load file instance " + FileInstance.this + ": " + e); 62 fireRun(e); 63 return; 64 } 65 log.info("loaded file instance " + FileInstance.this); 66 fireRun(null); 67 } 68 }); 69 stream.load(); 70 } catch (IOException e) { 71 log.error("io failure: " + e); 72 fireRun(e); 73 } 74 } 75 76 }); 77 super.joinableStart(); 78 } 74 79 75 80 } -
java/main/src/main/java/com/framsticks/dumping/SaveStream.java
r85 r90 44 44 protected void dispatchWrite(final Path path) { 45 45 ++dispatched; 46 instance. invokeLater(new RunAt<Instance>() {46 instance.dispatch(new RunAt<Instance>() { 47 47 @Override 48 48 public void run() { -
java/main/src/main/java/com/framsticks/gui/Browser.java
r88 r90 69 69 public void autoResolvePath(final String path, final Future<Path> future) { 70 70 final Instance i = instances.get("localhost"); 71 i. invokeLater(new RunAt<Instance>() {71 i.dispatch(new RunAt<Instance>() { 72 72 @Override 73 73 public void run() { … … 80 80 } 81 81 if (e == null) { 82 mainFrame. invokeLater(new RunAt<Frame>() {82 mainFrame.dispatch(new RunAt<Frame>() { 83 83 @Override 84 84 public void run() { … … 100 100 } 101 101 102 protected void firstTask() {102 protected void initializeGUI() { 103 103 assert isActive(); 104 log. info("executing first task");104 log.debug("executing first task"); 105 105 106 106 try { … … 129 129 130 130 for (final Instance i : instances) { 131 i. invokeLater(new RunAt<Instance>() {131 i.dispatch(new RunAt<Instance>() { 132 132 @Override 133 133 public void run() { 134 134 final Path p = i.getRootPath(); 135 invokeLater(new RunAt<Browser>() {135 dispatch(new RunAt<Browser>() { 136 136 @Override 137 137 public void run() { … … 183 183 Dispatching.use(instances, this); 184 184 185 invokeLater(new RunAt<Browser>() {185 dispatch(new RunAt<Browser>() { 186 186 @Override 187 187 public void run() { 188 firstTask();188 initializeGUI(); 189 189 } 190 190 }); … … 227 227 228 228 @Override 229 public void invokeLater(RunAt<? extends Browser> runnable) {230 SwingDispatcher.getInstance(). invokeLater(runnable);229 public void dispatch(RunAt<? extends Browser> runnable) { 230 SwingDispatcher.getInstance().dispatch(runnable); 231 231 } 232 232 -
java/main/src/main/java/com/framsticks/gui/Frame.java
r88 r90 285 285 286 286 @Override 287 public void invokeLater(RunAt<? extends Frame> runnable) {288 SwingDispatcher.getInstance(). invokeLater(runnable);287 public void dispatch(RunAt<? extends Frame> runnable) { 288 SwingDispatcher.getInstance().dispatch(runnable); 289 289 } 290 290 … … 427 427 super.joinableInterrupt(); 428 428 429 invokeLater(new RunAt<Frame>() {429 dispatch(new RunAt<Frame>() { 430 430 @Override 431 431 public void run() { -
java/main/src/main/java/com/framsticks/gui/Gui.java
r87 r90 14 14 import com.framsticks.gui.controls.CheckBoxControl; 15 15 import com.framsticks.gui.controls.Control; 16 import com.framsticks.gui.controls.ControlOwner; 16 17 import com.framsticks.gui.controls.EnumControl; 17 18 import com.framsticks.gui.controls.EventControl; … … 33 34 import com.framsticks.params.types.StringParam; 34 35 import com.framsticks.params.types.UniversalParam; 36 import com.framsticks.util.FramsticksException; 35 37 import com.framsticks.util.lang.Strings; 36 38 … … 92 94 } 93 95 94 public static void fillWithControls(JPanel panel, Collection<Param> params, Map<Param, Control> components) { 95 for (Param param : params) { 96 public static <P extends Param, C extends Control> void fillWithControls(ControlOwner owner, Collection<P> params, Map<P, C> components, Class<C> controlType) { 97 JPanel panel = owner.getPanel(); 98 for (P param : params) { 96 99 if (param.isUserHidden()) { 97 100 continue; 98 101 } 99 102 assert !(param instanceof CompositeParam); 100 Control c ontrol= Gui.createSuitable(param);101 if (control == null) { 102 log.error("component for param " + param + " of type " + param.getClass().getSimpleName() + " was not added");103 continue;103 Control c = Gui.createSuitable(param); 104 105 if (!controlType.isInstance(c)) { 106 throw new FramsticksException().msg("created control is not of required type").arg("control", c).arg("type", controlType); 104 107 } 108 109 C control = controlType.cast(c); 110 111 control.setOwner(owner); 112 105 113 log.debug("add component for " + param); 106 114 JPanel line = new JPanel(); 107 115 line.setLayout(new BoxLayout(line, BoxLayout.LINE_AXIS)); 108 116 line.setAlignmentX(JPanel.LEFT_ALIGNMENT); 109 JLabel label = new JLabel(Strings.notEmpty(param.getName()) ? param.getName() : "? (" + param.getId() + ")");117 JLabel label = new JLabel(Strings.notEmpty(param.getName()) ? param.getName() : (Strings.notEmpty(param.getId()) ? param.getId() : "?")); 110 118 label.setToolTipText(control.getToolTipText()); 111 119 label.setHorizontalAlignment(JLabel.RIGHT); -
java/main/src/main/java/com/framsticks/gui/ImageProvider.java
r84 r90 15 15 public class ImageProvider { 16 16 17 17 private final static Logger log = Logger.getLogger(ImageProvider.class.getName()); 18 18 19 19 20 20 /** 21 21 * HashMap stores icons. Key is icon path, Value is icon. 22 22 */ … … 80 80 return icons.get(imageName); 81 81 } 82 82 String resourceName = "/shared/res/network/" + imageName; 83 83 try { 84 84 ImageIcon icon = new ImageIcon(ImageProvider.class.getResource(resourceName)); 85 85 icons.put(imageName, icon); 86 86 return icon; 87 87 } catch (Exception ignored) { 88 88 log.error("failed to read icon: " + resourceName); 89 89 } 90 90 return null; -
java/main/src/main/java/com/framsticks/gui/InstanceAtFrame.java
r88 r90 72 72 List<Panel> panels = new ArrayList<Panel>(); 73 73 74 Panel.Parameters parameters = new Panel.Parameters(this, param, 75 framsClass); 74 Panel.Parameters parameters = new Panel.Parameters(this, param, framsClass); 76 75 for (PanelProvider pp : frame.browser.panelProviders) { 77 76 Panel p = pp.providePanel(parameters); … … 150 149 log.trace("fetched " + path); 151 150 152 frame. invokeLater(new RunAt<Frame>() {151 frame.dispatch(new RunAt<Frame>() { 153 152 @Override 154 153 public void run() { … … 159 158 final TreeNode result = (TreeNode) treePath.getLastPathComponent(); 160 159 // log.trace("found " + result + " == " + path); 161 instance. invokeLater(new RunAt<Instance>() {160 instance.dispatch(new RunAt<Instance>() { 162 161 @Override 163 162 public void run() { -
java/main/src/main/java/com/framsticks/gui/ObjectPanel.java
r85 r90 2 2 3 3 import com.framsticks.gui.controls.Control; 4 import com.framsticks.gui.controls.ControlOwner; 4 5 import com.framsticks.gui.controls.ValueControl; 5 6 import com.framsticks.gui.controls.ValueControlListener; … … 11 12 12 13 import javax.swing.*; 14 13 15 import java.util.Collection; 14 16 import java.util.HashMap; … … 18 20 19 21 @SuppressWarnings("serial") 20 public class ObjectPanel extends ModifiablePanel {22 public class ObjectPanel extends ModifiablePanel implements ControlOwner { 21 23 22 24 private static final Logger log = Logger.getLogger(ObjectPanel.class.getName()); … … 28 30 super(parameters); 29 31 30 Gui.fillWithControls(contentPanel, params, components); 32 Gui.fillWithControls(this, params, components, Control.class); 33 setName(framsClass.getId()); 31 34 32 35 for (final ValueControl c : filterInstanceof(components.values(), ValueControl.class)) { … … 44 47 }); 45 48 } 49 46 50 contentPanel.add(Box.createVerticalGlue()); 47 51 this.revalidate(); … … 74 78 } 75 79 76 frame. invokeLater(new RunAt<Frame>() {80 frame.dispatch(new RunAt<Frame>() { 77 81 @Override 78 82 public void run() { … … 97 101 } 98 102 103 @Override 104 public JPanel getPanel() { 105 return contentPanel; 106 } 107 108 @Override 109 public TreeNode getCurrentTreeNode() { 110 return super.getCurrentTreeNode(); 111 } 99 112 100 113 // public void updateValue() { 101 // 114 // //assert panel.getFrame().isActive(); 102 115 103 // 104 // 105 // 106 // 107 // 108 // 109 // 110 // 111 // 112 // 113 // 114 // 115 // 116 // 117 // 118 // 119 // 116 // final Node n = panel.getCurrentNode(); 117 // panel.getBrowser().getManager().invokeLater(new Runnable() { 118 // @Override 119 // public void run() { 120 // Object v = n.getAccess().get(param, Object.class); 121 // if (v == null) { 122 // v = param.getDef(Object.class); 123 // } 124 // final Object fv = v; 125 // panel.getBrowser().invokeLater(new Runnable() { 126 // @Override 127 // public void run() { 128 // setValueImpl(fv); 129 // } 130 // }); 131 // } 132 // }); 120 133 // } 121 134 -
java/main/src/main/java/com/framsticks/gui/SwingDispatcher.java
r85 r90 21 21 22 22 public SwingDispatcher() { 23 invokeLater(new RunAt<C>() {23 dispatch(new RunAt<C>() { 24 24 @Override 25 25 public void run() { … … 35 35 36 36 @Override 37 public final void invokeLater(RunAt<? extends C> runnable) {37 public final void dispatch(RunAt<? extends C> runnable) { 38 38 assert !(runnable instanceof Task); 39 39 SwingUtilities.invokeLater(runnable); -
java/main/src/main/java/com/framsticks/gui/TreeNode.java
r88 r90 62 62 iconName = TreeCellRenderer.findIconName(name, path.getTextual()); 63 63 tooltip = "?"; 64 path.getInstance(). invokeLater(new RunAt<Instance>() {64 path.getInstance().dispatch(new RunAt<Instance>() { 65 65 @Override 66 66 public void run() { … … 85 85 assert p.getInstance().isActive(); 86 86 if (Logging.log(log, "fetch", TreeNode.this, e)) { 87 frame. invokeLater(new RunAt<Frame>() {87 frame.dispatch(new RunAt<Frame>() { 88 88 @Override 89 89 public void run() { … … 95 95 } 96 96 updateChildren(p); 97 frame. invokeLater(new RunAt<Frame>() {97 frame.dispatch(new RunAt<Frame>() { 98 98 @Override 99 99 public void run() { … … 107 107 assert !frame.isActive(); 108 108 /** TODO those two actions could be merged into single closure */ 109 frame. invokeLater(new RunAt<Frame>() {109 frame.dispatch(new RunAt<Frame>() { 110 110 @Override 111 111 public void run() { … … 152 152 /**If some child were found, update in frame context.*/ 153 153 if (childrenPaths.size() > 0) { 154 frame. invokeLater(new RunAt<Frame>() {154 frame.dispatch(new RunAt<Frame>() { 155 155 @Override 156 156 public void run() { … … 189 189 final Path p = path; 190 190 191 p.getInstance(). invokeLater(new RunAt<Instance>() {191 p.getInstance().dispatch(new RunAt<Instance>() { 192 192 @Override 193 193 public void run() { … … 249 249 final String name = (nameParam != null ? access.get(nameParam, String.class) : path.getTop().getParam().getId()); 250 250 251 frame. invokeLater(new RunAt<Frame>() {251 frame.dispatch(new RunAt<Frame>() { 252 252 @Override 253 253 public void run() { … … 306 306 assert p.isResolved(); 307 307 panel.setCurrentTreeNode(this); 308 p.getInstance(). invokeLater(new RunAt<Instance>() {308 p.getInstance().dispatch(new RunAt<Instance>() { 309 309 @Override 310 310 public void run() { … … 312 312 panel.pullValuesFromLocalToUser(access); 313 313 314 frame. invokeLater(new RunAt<Frame>() {314 frame.dispatch(new RunAt<Frame>() { 315 315 @Override 316 316 public void run() { … … 344 344 final Path p = path; 345 345 log.debug("preparing panel: " + p); 346 p.getInstance(). invokeLater(new RunAt<Instance>() {346 p.getInstance().dispatch(new RunAt<Instance>() { 347 347 @Override 348 348 public void run() { … … 350 350 final CompositeParam param = p.getTop().getParam(); 351 351 final FramsClass framsClass = p.getInstance().getInfoFromCache(param.getContainedTypeName()); 352 frame. invokeLater(new RunAt<Frame>() {352 frame.dispatch(new RunAt<Frame>() { 353 353 @Override 354 354 public void run() { … … 503 503 final Map<ValueControl, Object> changes = localChanges; 504 504 localChanges = null; 505 instanceAtFrame.getInstance(). invokeLater(new RunAt<Instance>() {505 instanceAtFrame.getInstance().dispatch(new RunAt<Instance>() { 506 506 @Override 507 507 public void run() { … … 517 517 } 518 518 log.debug("applied changes for: " + p); 519 frame. invokeLater(new RunAt<Frame>() {519 frame.dispatch(new RunAt<Frame>() { 520 520 @Override 521 521 public void run() { -
java/main/src/main/java/com/framsticks/gui/controls/Control.java
r86 r90 22 22 protected Frame frame; 23 23 protected final Param param; 24 protected ControlOwner owner; 24 25 25 26 public Control(Param param) { 26 27 this.param = param; 28 setName(param.getId()); 27 29 28 30 this.setEnabled(!param.hasFlag(Flags.READONLY)); … … 38 40 } 39 41 42 public void setOwner(ControlOwner owner) { 43 this.owner = owner; 44 } 45 40 46 protected void addAsOnlyChild(JComponent component) { 41 47 this.setLayout(new BorderLayout()); -
java/main/src/main/java/com/framsticks/gui/controls/ProcedureControl.java
r84 r90 1 1 package com.framsticks.gui.controls; 2 2 3 import com.framsticks.core.Instance; 4 import com.framsticks.core.Path; 3 5 import com.framsticks.gui.Gui; 6 import com.framsticks.gui.TreeNode; 4 7 import com.framsticks.params.Param; 8 import com.framsticks.params.ValueParam; 5 9 import com.framsticks.params.types.ProcedureParam; 10 import com.framsticks.util.Logging; 11 import com.framsticks.util.StateFunctor; 12 import com.framsticks.util.dispatching.RunAt; 6 13 7 14 import javax.swing.*; 8 15 import javax.swing.border.BevelBorder; 16 17 import org.apache.log4j.Logger; 18 9 19 import java.awt.event.ActionEvent; 10 20 import java.awt.event.ActionListener; 11 21 import java.util.HashMap; 22 import java.util.LinkedList; 23 import java.util.List; 12 24 import java.util.Map; 13 25 14 26 @SuppressWarnings("serial") 15 public class ProcedureControl extends Control { 16 // private static final Logger log = Logger.getLogger(Control.class.getName()); 27 public class ProcedureControl extends Control implements ControlOwner { 28 29 private static final Logger log = Logger.getLogger(ProcedureControl.class); 17 30 18 31 protected final JButton procedureButton; 19 32 20 final protected Map< Param, Control> components = new HashMap<Param, Control>();33 final protected Map<ValueParam, ValueControl> components = new HashMap<>(); 21 34 22 35 public ProcedureControl(ProcedureParam procedureParam) { … … 24 37 25 38 procedureButton = new JButton("Call"); 39 procedureButton.setName("call"); 26 40 27 41 this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); 28 42 29 Gui.fillWithControls(this, procedureParam.getArgumentsType(), components );43 Gui.fillWithControls(this, procedureParam.getArgumentsType(), components, ValueControl.class); 30 44 31 45 if (components.size() != procedureParam.getArgumentsType().size()) { … … 39 53 @Override 40 54 public void actionPerformed(ActionEvent e) { 55 TreeNode treeNode = owner.getCurrentTreeNode(); 56 assert treeNode != null; 41 57 42 /* 43 Node node = panel.getCurrentNode(); 44 if (argumentWindow != null) { 45 argumentWindow.askForArguments(); 46 return; 58 log.debug("calling " + getParam() + " on " + treeNode); 59 final Path path = treeNode.getInstancePath(); 60 61 final List<Object> arguments = new LinkedList<Object>(); 62 for (Param arg : getParam().getArgumentsType()) { 63 arguments.add(((ValueControl) components.get(arg)).getCurrentValue()); 47 64 } 48 node.getConnection().send(new CallQuery().setMethod(param.getId()).setPath(node.getPath()).setCallback(new QueryCallback() { 65 66 path.getInstance().dispatch(new RunAt<Instance>() { 49 67 @Override 50 public void call(boolean ok, String comment, SourceInterface content) { 51 if (!ok) { 52 log.error("failed to call method: " + param); 53 return; 54 } 55 log.debug("successful call of method: " + param); 68 public void run() { 69 path.getInstance().call(path, getParam(), arguments.toArray(), new StateFunctor() { 70 @Override 71 public void call(Exception e) { 72 Logging.log(log, "call procedure", path, e); 73 } 74 }); 56 75 } 57 })); 58 */ 76 }); 59 77 } 60 78 }); … … 63 81 } 64 82 83 @Override 84 public JPanel getPanel() { 85 return this; 86 } 87 88 @Override 89 public TreeNode getCurrentTreeNode() { 90 return owner.getCurrentTreeNode(); 91 } 92 93 @Override 94 public ProcedureParam getParam() { 95 return (ProcedureParam) param; 96 } 97 65 98 } -
java/main/src/main/java/com/framsticks/gui/controls/TextFieldControl.java
r87 r90 26 26 super(valueParam); 27 27 textField = new JTextField(); 28 textField.setName("value"); 28 29 29 30 textField.setMinimumSize(new Dimension(0, Control.LINE_HEIGHT)); -
java/main/src/main/java/com/framsticks/gui/controls/ValueControl.java
r87 r90 78 78 } 79 79 80 /** This method is meant as a public interface to obtain current and correct value from control. 81 * 82 */ 83 public final Object getCurrentValue() { 84 return filterValueThroughConstraints(pullValueFromUserInterface()); 85 } 86 80 87 protected boolean notifyOfChange() { 81 88 if (!programmaticChange) { … … 83 90 return true; 84 91 } 85 return listener.onChange( filterValueThroughConstraints(pullValueFromUserInterface()));92 return listener.onChange(getCurrentValue()); 86 93 } 87 94 return true; -
java/main/src/main/java/com/framsticks/gui/view/TreeCellRenderer.java
r84 r90 97 97 return ImageProvider.NEURON_DEF_GROUP; 98 98 } 99 if (nodeName.equals("neuroconns") || path.endsWith("neuroconns")) { 100 return ImageProvider.NEURON_DEF_GROUP; 101 } 99 102 if (nodeName.equals("mechparts") || path.endsWith("mechparts")) { 100 103 return ImageProvider.MECH_PART_GROUP; … … 113 116 } 114 117 if (path.matches("^.*neurodefs/[0-9]+$")) { 118 return ImageProvider.NEURON_DEF; 119 } 120 if (path.matches("^.*neuroconns/[0-9]+$")) { 115 121 return ImageProvider.NEURON_DEF; 116 122 } … … 139 145 return ImageProvider.EVENT; 140 146 } 141 return ImageProvider.S ERVER;147 return ImageProvider.SIMULATOR; 142 148 } 143 149 -
java/main/src/main/java/com/framsticks/hosting/InstanceClient.java
r88 r90 43 43 @Override 44 44 public void handle(final ApplicationRequest request, final ResponseCallback<?> responseCallback) { 45 instance. invokeLater(new RunAt<Instance>() {45 instance.dispatch(new RunAt<Instance>() { 46 46 @Override 47 47 public void run() { -
java/main/src/main/java/com/framsticks/hosting/ServerInstance.java
r88 r90 16 16 * @author Piotr Sniegowski 17 17 */ 18 public class ServerInstance extends LocalInstance implements JoinableParent {18 public abstract class ServerInstance extends LocalInstance implements JoinableParent { 19 19 20 20 private final static Logger log = Logger.getLogger(ServerInstance.class.getName()); … … 23 23 24 24 public ServerInstance() { 25 }26 27 @Override28 protected void firstTask() {29 super.firstTask();30 assert hosted != null;31 Dispatching.use(hosted, this);32 25 } 33 26 … … 53 46 return null; 54 47 } 55 FramsClass cached = registry.get InfoFromCache(id);48 FramsClass cached = registry.getFramsClass(id); 56 49 if (cached != null) { 57 50 return cached; … … 67 60 68 61 registry.registerReflectedClass(null, id, nativeClass); 69 registry.put InfoIntoCache(framsClass);62 registry.putFramsClass(framsClass); 70 63 return framsClass; 71 64 } catch (ClassNotFoundException ignored) { … … 98 91 public void childChangedState(Joinable joinable, JoinableState state) { 99 92 proceedToState(state); 93 } 100 94 95 @Override 96 protected void joinableStart() { 97 super.joinableStart(); 98 assert hosted != null; 99 Dispatching.use(hosted, this); 101 100 } 102 101 103 102 @Override 104 103 protected void joinableInterrupt() { 104 super.joinableInterrupt(); 105 105 Dispatching.drop(hosted, this); 106 106 } 107 107 108 108 @Override 109 protected void joinableFinish() { 110 109 protected void joinableJoin() throws InterruptedException { 110 Dispatching.join(hosted); 111 super.joinableJoin(); 111 112 } 112 113 -
java/main/src/main/java/com/framsticks/model/BasePart.java
r86 r90 16 16 public void setPosition(Point3d p) { x = p.x; y = p.y; z = p.z; } 17 17 18 @ParamAnnotation(id = "m" )19 public double mass = 0.0;18 @ParamAnnotation(id = "m", stringType = "f 0.1 999.0 1.0") 19 public double mass = 1.0; 20 20 21 21 @ParamAnnotation(id = "s") -
java/main/src/main/java/com/framsticks/model/Creature.java
r86 r90 154 154 public final List<Joint> joints = new ArrayList<Joint>(); 155 155 @ParamAnnotation 156 public final List<NeuroDef > neurodefs = new ArrayList<NeuroDef>();156 public final List<NeuroDefinition> neurodefs = new ArrayList<NeuroDefinition>(); 157 157 158 158 public final List<Part> getParts() { return parts; } 159 159 public final List<Joint> getJoints() { return joints; } 160 public final List<NeuroDef > getNeuroDefs() { return neurodefs; }160 public final List<NeuroDefinition> getNeuroDefs() { return neurodefs; } 161 161 162 162 @ParamAnnotation -
java/main/src/main/java/com/framsticks/model/Joint.java
r86 r90 10 10 */ 11 11 @FramsClassAnnotation(id = "j") 12 public class Joint extends BaseJoint {12 public class Joint extends BaseJoint implements ModelComponent { 13 13 14 14 @ParamAnnotation(id = "i") -
java/main/src/main/java/com/framsticks/model/Model.java
r86 r90 3 3 import com.framsticks.params.annotations.FramsClassAnnotation; 4 4 import com.framsticks.params.annotations.ParamAnnotation; 5 import com.framsticks.util.lang.Casting;6 5 import com.framsticks.util.lang.Containers; 7 import com.framsticks.util.lang.IterableIterator;8 import com.framsticks.util.math.Orientation;9 import org.apache.log4j.Logger;10 6 11 7 import java.util.ArrayList; 12 import java.util.Iterator;13 8 import java.util.List; 14 9 … … 17 12 */ 18 13 @FramsClassAnnotation(id = "m") 19 public class Model { 20 21 private final static Logger log = Logger.getLogger(Model.class); 14 public class Model implements ModelComponent { 22 15 23 16 @ParamAnnotation(id = "se") … … 39 32 public final List<Joint> joints = new ArrayList<Joint>(); 40 33 41 @ParamAnnotation 42 public final List<NeuroDef> neurodefs = new ArrayList<NeuroDef>(); 34 @ParamAnnotation(id = "neurodefs") 35 public final List<NeuroDefinition> neuroDefinitions = new ArrayList<NeuroDefinition>(); 36 37 @ParamAnnotation(id = "neuroconns") 38 public final List<NeuroConnection> neuroConnections = new ArrayList<NeuroConnection>(); 43 39 44 40 //TODO: why those methods returns and accepts doubles? … … 48 44 public double getNumjoints() { return (double)joints.size(); } 49 45 @ParamAnnotation 50 public double getNumneurons() { return (double)neuro defs.size(); }46 public double getNumneurons() { return (double)neuroDefinitions.size(); } 51 47 52 48 //this is impossible to use, because numparts field is marked as readonly … … 56 52 public void setNumjoints(double numjoints) { Containers.resizeList(joints, (int)(double)numjoints); } 57 53 @ParamAnnotation 58 public void setNumneurons(double numneurons) { Containers.resizeList(neuro defs, (int)(double)numneurons); }54 public void setNumneurons(double numneurons) { Containers.resizeList(neuroDefinitions, (int)(double)numneurons); } 59 55 60 56 public List<Part> getParts() { return parts; } 61 57 public List<Joint> getJoints() { return joints; } 62 public List<NeuroDef> getNeuroDefs() { return neurodefs; } 58 public List<NeuroDefinition> getNeuroDefinitions() { return neuroDefinitions; } 59 public List<NeuroConnection> getNeuroConnections() { return neuroConnections; } 63 60 64 public static Model build(List<Object> objects) {65 Iterator<Object> i = objects.iterator();66 if (!i.hasNext()) {67 return null;68 }69 Model f0Genotype = Casting.tryCast(Model.class, i.next());70 if (f0Genotype == null) {71 log.fatal("first object is not a Model");72 return null;73 }74 for (Object object : new IterableIterator<Object>(i)) {75 if (object instanceof Joint) {76 f0Genotype.joints.add((Joint)object);77 continue;78 }79 if (object instanceof Part) {80 f0Genotype.parts.add((Part)object);81 continue;82 }83 if (object instanceof NeuroDef) {84 f0Genotype.neurodefs.add((NeuroDef) object);85 continue;86 }87 log.error("invalid class: " + object.getClass().getCanonicalName());88 }89 90 for (Part p : f0Genotype.getParts()) {91 p.setOrientation(new Orientation().rotate(p.getRotation()));92 }93 for (Joint j : f0Genotype.getJoints()) {94 /** based on c++ Joint::attachToParts*/95 Part p1 = f0Genotype.parts.get(j.part1);96 Part p2 = f0Genotype.parts.get(j.part2);97 assert p1 != null && p2 != null;98 Orientation o = new Orientation().rotate(j.getRotation());99 p2.setOrientation(p1.getOrientation().transform(o));100 p2.setPosition(p2.getOrientation().transform(j.getDelta()).add(p1.getPosition()));101 }102 return f0Genotype;103 }104 61 } -
java/main/src/main/java/com/framsticks/model/Part.java
r86 r90 10 10 */ 11 11 @FramsClassAnnotation(id = "p") 12 public class Part extends BasePart {12 public class Part extends BasePart implements ModelComponent { 13 13 14 14 @ParamAnnotation -
java/main/src/main/java/com/framsticks/model/f0/Schema.java
r88 r90 7 7 import com.framsticks.params.annotations.AutoAppendAnnotation; 8 8 import com.framsticks.params.annotations.FramsClassAnnotation; 9 import com.framsticks.parsers.XmlLoader; 10 import com.framsticks.util.DoubleMap; 11 import com.framsticks.util.FramsticksException; 9 import com.framsticks.params.annotations.ParamAnnotation; 10 import com.framsticks.util.lang.Casting; 12 11 13 import com.framsticks.model.Package;14 12 15 13 /** … … 27 25 28 26 /** The neuro classes (classess representing different types of neurons). */ 29 private DoubleMap<String, FramsClass> framsClasses = new DoubleMap<>(); 30 private DoubleMap<String, NeuroClass> neuroClasses = new DoubleMap<>(); 27 private final Registry framsClasses = new Registry(); 28 private final Registry neuroClasses = new Registry(); 29 // private DoubleMap<String, FramsClass> framsClasses = new DoubleMap<>(); 30 // private DoubleMap<String, NeuroClass> neuroClasses = new DoubleMap<>(); 31 31 32 32 public static InputStream getDefaultDefinitionAsStream() { … … 35 35 } 36 36 37 p ublic static Schema load(InputStream stream) {37 protected static Schema defaultSchema; 38 38 39 XmlLoader xmlLoader = new XmlLoader(); 40 xmlLoader.setUseLowerCase(true); 41 xmlLoader.getRegistry() 42 .registerAndBuild(Schema.class) 43 .registerAndBuild(FramsClassBuilder.class) 44 .registerAndBuild(NeuroClassBuilder.class) 45 .registerAndBuild(ParamBuilder.class) 46 .registerAndBuild(NeuroParamBuilder.class) 47 .registerAndBuild(GroupBuilder.class) 48 ; 49 50 Package.register(xmlLoader.getRegistry()); 51 52 Object object = xmlLoader.load(Schema.getDefaultDefinitionAsStream()); 53 if (!(object instanceof Schema)) { 54 throw new FramsticksException().msg("failed to load schema"); 39 public synchronized static Schema getDefaultSchema() { 40 if (defaultSchema == null) { 41 defaultSchema = new SchemaBuilder().stream(getDefaultDefinitionAsStream()).finish(); 55 42 } 56 return (Schema) object;43 return defaultSchema; 57 44 } 58 45 … … 60 47 } 61 48 49 @AutoAppendAnnotation 62 50 public void addClass(FramsClass framsClass) { 63 registry.put InfoIntoCache(framsClass);51 registry.putFramsClass(framsClass); 64 52 if (framsClass instanceof NeuroClass) { 65 neuroClasses.put (framsClass.getId(), framsClass.getName(), (NeuroClass)framsClass);53 neuroClasses.putFramsClass(framsClass); 66 54 return; 67 55 } 68 framsClasses.put (framsClass.getId(), framsClass.getName(),framsClass);56 framsClasses.putFramsClass(framsClass); 69 57 } 70 58 … … 74 62 } 75 63 76 public Set< NeuroClass> getNeuroClasses() {77 return neuroClasses.get Values();64 public Set<FramsClass> getNeuroClasses() { 65 return neuroClasses.getFramsClasses(); 78 66 } 79 67 80 68 public Set<FramsClass> getFramsClasses() { 81 return framsClasses.get Values();69 return framsClasses.getFramsClasses(); 82 70 } 83 71 84 72 public NeuroClass getNeuroClass(String identifier) { 85 return neuroClasses.get(identifier);73 return Casting.throwCast(NeuroClass.class, neuroClasses.getFramsClass(identifier)); 86 74 } 87 75 88 76 public FramsClass getFramsClass(String identifier) { 89 return framsClasses.get (identifier);77 return framsClasses.getFramsClass(identifier); 90 78 } 91 79 80 @ParamAnnotation 92 81 public final Registry getRegistry() { 93 82 return registry; 94 83 } 95 84 85 @ParamAnnotation 86 public final Registry getFramsRegistry() { 87 return framsClasses; 88 } 89 90 @ParamAnnotation 91 public final Registry getNeurosRegistry() { 92 return neuroClasses; 93 } 94 95 96 96 } -
java/main/src/main/java/com/framsticks/params/AccessInterface.java
r87 r90 2 2 3 3 import java.util.Collection; 4 5 import com.framsticks.params.types.ProcedureParam; 4 6 5 7 … … 20 22 String getId(); 21 23 22 Param getGroupMember(int gi, int n);23 24 24 int getParamCount(); 25 25 26 // void call(int i, Object args, Object ret); 27 // TODO: as for now it's not necessary to implement this method 26 Object call(String id, Object[] arguments); 27 28 Object call(ProcedureParam param, Object[] arguments); 28 29 29 30 <T> T get(int i, Class<T> type); -
java/main/src/main/java/com/framsticks/params/ArrayListAccess.java
r87 r90 32 32 public Param getParam(int i) { 33 33 //TODO listAccessParam 34 return Param.build().id(Integer.toString(i)). name(elementAccess.getId()).type("o " + elementAccess.getId()).finish();34 return Param.build().id(Integer.toString(i)).forAccess(elementAccess).finish(); 35 35 } 36 36 -
java/main/src/main/java/com/framsticks/params/FramsClass.java
r88 r90 4 4 import com.framsticks.params.annotations.ParamAnnotation; 5 5 import com.framsticks.util.FramsticksException; 6 import com.framsticks.util.lang.Containers; 6 7 // import com.framsticks.util.FramsticksException; 7 8 … … 51 52 // protected Map<String, Integer> paramIdMap = new HashMap<String, Integer>(); 52 53 53 public Collection<Param> getParamEntries() { 54 return paramList; 54 @ParamAnnotation(id = "props", name = "props") 55 public List<Param> getParamEntries() { 56 return Collections.unmodifiableList(paramList); 55 57 } 56 58 … … 60 62 this.name = builder.getName(); 61 63 this.description = builder.getDescription(); 62 this.groups = builder.groups;64 this.groups = Containers.build(builder.groupBuilders); 63 65 this.paramList = builder.params; 64 66 65 67 for (Param param : paramList) { 66 68 paramEntryMap.put(param.getId(), param); 67 try {68 Group group = groups.get(param.getGroup());69 if (group != null) {70 group.addProperty(param);71 }72 } catch (IndexOutOfBoundsException ignored) {73 74 }75 69 } 76 70 … … 88 82 } 89 83 90 /** 91 * Gets the group member. 92 * 93 * @param gi 94 * the offset of group 95 * @param pi 96 * the offset of member within a group 97 * @return the pi-th member of group gi 98 */ 99 public Param getGroupMember(int gi, int pi) { 100 if (gi < 0 || pi < 0 || gi >= groups.size()) { 101 return null; 102 } 103 Group group = groups.get(gi); 104 return (group != null ? group.getProperty(pi) : null); 84 public Group getGroup(int groupNumber) { 85 return Containers.getFromList(groups, groupNumber, "group", this); 105 86 } 106 87 107 /** 108 * Gets the group name. 109 * 110 * @param gi 111 * the offset of group 112 * @return the group name 113 */ 114 public String getGroupName(int gi) { 115 if (gi < 0 || gi >= groups.size()) 116 return null; 117 return groups.get(gi).name; 118 } 88 // /** 89 // * Gets the group member. 90 // * 91 // * @param gi 92 // * the offset of group 93 // * @param pi 94 // * the offset of member within a group 95 // * @return the pi-th member of group gi 96 // */ 97 // public Param getGroupMember(int gi, int pi) { 98 // if (gi < 0 || pi < 0 || gi >= groups.size()) { 99 // return null; 100 // } 101 // Group group = groups.get(gi); 102 // return (group != null ? group.getProperty(pi) : null); 103 // } 104 105 // /** 106 // * Gets the group name. 107 // * 108 // * @param gi 109 // * the offset of group 110 // * @return the group name 111 // */ 112 // public String getGroupName(int gi) { 113 // if (gi < 0 || gi >= groups.size()) 114 // return null; 115 // return groups.get(gi).name; 116 // } 119 117 120 118 @ParamAnnotation -
java/main/src/main/java/com/framsticks/params/FramsClassBuilder.java
r88 r90 22 22 import com.framsticks.parsers.Loaders; 23 23 import com.framsticks.util.Builder; 24 import com.framsticks.util. FramsticksException;24 import com.framsticks.util.lang.Containers; 25 25 import com.framsticks.util.lang.Strings; 26 26 … … 38 38 } 39 39 40 public static String getParamTypeForNativeType(Type type) { 40 public static ParamBuilder induceParamType(ParamBuilder builder, Type type) { 41 41 42 if (type instanceof ParameterizedType) { 42 43 ParameterizedType p = (ParameterizedType) type; … … 52 53 } 53 54 if (!(containedType instanceof Class)) { 54 return null;55 return builder; 55 56 } 56 57 Class<?> containedClass = (Class<?>) containedType; … … 60 61 if (fca == null) { 61 62 log.error("the class is not annotated: " + containedClass); 62 return null;63 return builder; 63 64 } 64 65 b.append(getName(fca, containedClass)); … … 67 68 } 68 69 69 return b.toString(); 70 builder.type(b.toString()); 71 return builder; 70 72 } 71 73 … … 87 89 // } 88 90 if (cl.equals(Integer.class) || cl.equals(int.class)) { 89 return "d"; 91 builder.type("d"); 92 return builder; 90 93 } 91 94 if (cl.equals(String.class)) { 92 return "s"; 95 builder.type("s"); 96 return builder; 93 97 } 94 98 if (cl.equals(Double.class) || cl.equals(double.class)) { 95 return "f"; 99 builder.type("f"); 100 return builder; 96 101 } 97 102 if (cl.equals(Boolean.class) || cl.equals(boolean.class)) { 98 return "d 0 1"; 103 builder.type( "d 0 1"); 104 return builder; 99 105 } 100 106 if (cl.equals(Object.class)) { 101 return "x"; 102 } 103 104 return "o " + ((Class<?>) type).getCanonicalName(); 107 builder.type("x"); 108 return builder; 109 } 110 111 // builder.type("o " + (cl).getCanonicalName()); 112 builder.type("o " + cl.getSimpleName()); 113 builder.fillStorageType(cl); 114 return builder; 105 115 } 106 116 107 117 throw new ConstructionException().msg("failed to find framsticks for native type").arg("type", type); 118 } 119 120 121 public static ParamBuilder induceParamType(ParamBuilder builder, ParamCandidate candidate) { 122 Method method = candidate.getCaller(); 123 if (method == null) { 124 return induceParamType(builder, candidate.getType()); 125 } 126 127 builder.resultType(induceParamType(Param.build(), method.getGenericReturnType()).finish(ValueParam.class)); 128 129 List<ValueParam> arguments = new ArrayList<>(); 130 int number = 0; 131 for (Type arg : method.getGenericParameterTypes()) { 132 arguments.add(induceParamType(Param.build(), arg).idAndName("arg" + (number++)).finish(ValueParam.class)); 133 } 134 builder.argumentsType(arguments); 135 136 return builder; 108 137 } 109 138 … … 183 212 name(getName(fca, javaClass)); 184 213 185 Map<String, ParamCandidate> candidates = ParamCandidate.getAllCandidates(javaClass); 186 187 for (ParamCandidate pc : candidates.values()) { 188 String type = getParamTypeForNativeType(pc.getType()); 189 if (type == null) { 190 throw new FramsticksException().msg("failed to find type for param candidate").arg("candidate", pc); 191 } 192 param(Param.build().id(pc.getId()).name(pc.getName()).type(type).flags(pc.getFlags())); 214 for (ParamCandidate pc : ParamCandidate.getAllCandidates(javaClass).getOrder()) { 215 ParamBuilder builder = Param.build().id(pc.getId()).name(pc.getName()).flags(pc.getFlags()); 216 217 induceParamType(builder, pc); 218 219 for (ParamAnnotation pa : pc.getAnnotations()) { 220 if (!"".equals(pa.def())) { 221 builder.def(pa.def()); 222 } 223 if (!"".equals(pa.help())) { 224 builder.help(pa.help()); 225 } 226 if (!"".equals(pa.min())) { 227 builder.min(pa.min()); 228 } 229 if (!"".equals(pa.max())) { 230 builder.max(pa.max()); 231 } 232 builder.extra(pa.extra()); 233 if (!"".equals(pa.stringType())) { 234 builder.type(pa.stringType()); 235 } 236 if (!pa.paramType().equals(Param.class)) { 237 builder.type(pa.paramType()); 238 } 239 } 240 param(builder); 193 241 } 194 242 … … 209 257 protected final List<Param> params = new LinkedList<>(); 210 258 211 protected List<Group > groups = new ArrayList<Group>();259 protected List<GroupBuilder> groupBuilders = new ArrayList<GroupBuilder>(); 212 260 213 261 @ParamAnnotation … … 242 290 } 243 291 244 public FramsClassBuilder append(Param param) {245 params.add(param);246 return this;247 }248 249 292 @AutoAppendAnnotation 250 293 public FramsClassBuilder param(ParamBuilder builder) { 251 return append(builder.finish()); 294 Param param = builder.finish(); 295 296 Integer group = param.getGroup(); 297 if (group != null) { 298 Containers.getFromList(groupBuilders, group, "group", this).addParam(param); 299 } 300 301 params.add(param); 302 return this; 252 303 } 253 304 254 305 @AutoAppendAnnotation 255 306 public FramsClassBuilder group(GroupBuilder builder) { 256 return group(builder.finish()); 257 } 258 259 @AutoAppendAnnotation 260 public FramsClassBuilder group(Group group) { 261 groups.add(group); 307 groupBuilders.add(builder); 262 308 return this; 263 309 } … … 288 334 289 335 public FramsClassBuilder group(String group) { 290 return group(new Group(group)); 291 } 292 293 public ParamBuilder param(String id) { 294 return new ParamBuilder(this).id(id); 295 } 336 return group(new GroupBuilder().name(group)); 337 } 338 296 339 297 340 } -
java/main/src/main/java/com/framsticks/params/Group.java
r87 r90 1 1 package com.framsticks.params; 2 2 3 import java.util.ArrayList;4 3 import java.util.List; 4 5 import javax.annotation.concurrent.Immutable; 5 6 6 7 import com.framsticks.params.annotations.FramsClassAnnotation; 7 8 import com.framsticks.params.annotations.ParamAnnotation; 9 import com.framsticks.util.lang.Containers; 8 10 9 11 /** … … 11 13 */ 12 14 @FramsClassAnnotation 15 @Immutable 13 16 public class Group { 14 17 15 18 @ParamAnnotation 16 protected String name;19 protected final String name; 17 20 18 21 /** 19 22 * Group members. 20 23 */ 21 List<Param> params = new ArrayList<Param>();24 protected final List<Param> params; 22 25 23 public Group(String name) { 24 this.name = name; 25 } 26 27 /** 28 * Adds new group member. 29 * 30 * @param p the new group member 31 */ 32 void addProperty(Param p) { 33 params.add(p); 26 public Group(GroupBuilder builder) { 27 this.name = builder.getName(); 28 this.params = builder.getParams(); 34 29 } 35 30 … … 40 35 * @return the property 41 36 */ 42 Param getProperty(int i) { 43 if (i < 0 || i >= params.size()) 44 return null; 45 return params.get(i); 37 public Param getParam(int number) { 38 return Containers.getFromList(params, number, "param", this); 46 39 } 47 40 48 41 @Override 49 42 public String toString() { 50 return name;43 return "group" + name; 51 44 } 52 45 … … 59 52 } 60 53 54 public int getCount() { 55 return params.size(); 56 } 57 61 58 } -
java/main/src/main/java/com/framsticks/params/GroupBuilder.java
r88 r90 1 1 package com.framsticks.params; 2 3 import java.util.ArrayList; 4 import java.util.List; 2 5 3 6 import com.framsticks.params.annotations.FramsClassAnnotation; 4 7 import com.framsticks.params.annotations.ParamAnnotation; 8 import com.framsticks.util.Builder; 5 9 6 10 @FramsClassAnnotation(id = "group", name = "group") 7 public class GroupBuilder {11 public class GroupBuilder implements Builder<Group> { 8 12 9 13 @ParamAnnotation 10 14 protected String name; 15 16 protected final List<Param> params = new ArrayList<Param>(); 11 17 12 18 public GroupBuilder() { … … 19 25 } 20 26 27 /** 28 * @return the name 29 */ 30 public String getName() { 31 return name; 32 } 33 34 /** 35 * @return the params 36 */ 37 public List<Param> getParams() { 38 return params; 39 } 40 21 41 public Group finish() { 22 return new Group(name); 42 return new Group(this); 43 } 44 45 public void addParam(Param param) { 46 params.add(param); 23 47 } 24 48 -
java/main/src/main/java/com/framsticks/params/ListAccess.java
r87 r90 4 4 5 5 import static com.framsticks.util.lang.Containers.filterInstanceof; 6 7 import com.framsticks.params.types.ProcedureParam; 6 8 7 9 /** … … 16 18 } 17 19 18 @Override19 public Param getGroupMember(int gi, int n) {20 return null;21 }20 // @Override 21 // public Param getGroupMember(int gi, int n) { 22 // return null; 23 // } 22 24 23 25 @Override … … 81 83 } 82 84 85 @Override 86 public Object call(String id, Object[] arguments) { 87 throw new InvalidOperationException().msg("list access does not support calling methods").arg("id", id); 88 } 89 90 @Override 91 public Object call(ProcedureParam param, Object[] arguments) { 92 throw new InvalidOperationException().msg("list access does not support calling methods").arg("param", param); 93 } 94 83 95 }; -
java/main/src/main/java/com/framsticks/params/Param.java
r88 r90 23 23 protected final String id; 24 24 25 //TODO26 /**27 * The parameter internal id. It's set by a user to use user's own getId in28 * code29 */30 protected final String internalId;31 32 25 /** The parameter name. */ 33 26 protected final String name; … … 48 41 public Param(ParamBuilder builder) { 49 42 id = builder.getId(); 50 internalId = builder.getInternalId();51 43 name = builder.getName(); 52 44 help = builder.getHelp(); … … 59 51 public String getId() { 60 52 return id; 61 }62 63 public String getInternalId() {64 return internalId;65 53 } 66 54 … … 87 75 public abstract String getFramsTypeName(); 88 76 89 90 public String getEffectiveId() { 91 return (internalId != null) ? internalId : id; 92 } 93 94 77 @ParamAnnotation 95 78 public Integer getExtra() { 96 79 return extra; -
java/main/src/main/java/com/framsticks/params/ParamBuilder.java
r88 r90 11 11 12 12 import java.lang.reflect.InvocationTargetException; 13 import java.util.ArrayList; 13 14 import java.util.Arrays; 14 15 import java.util.List; 16 import java.util.regex.Matcher; 17 import java.util.regex.Pattern; 18 19 import javax.annotation.Nonnull; 15 20 16 21 /** … … 37 42 private String id; 38 43 39 /**40 * The parameter internal id. It's set by a user to use user's own id in41 * code42 */43 private String internalId;44 45 44 /** The number of group, that parameter belongs to. */ 46 private Integer group = 0;45 private Integer group; 47 46 48 47 /** The flags stored as a bit sum. */ … … 76 75 } 77 76 77 protected ValueParam resultType; 78 79 protected List<ValueParam> argumentsType; 78 80 79 81 /** … … 114 116 115 117 /** 118 * @return the resultType 119 */ 120 public ValueParam getResultType() { 121 return resultType; 122 } 123 124 125 /** 126 * @param resultType the resultType to set 127 */ 128 public ParamBuilder resultType(ValueParam resultType) { 129 this.resultType = resultType; 130 return this; 131 } 132 133 /** 134 * @return the argumentsType 135 */ 136 public List<ValueParam> getArgumentsType() { 137 return argumentsType; 138 } 139 140 141 /** 142 * @param argumentsType the argumentsType to set 143 */ 144 public ParamBuilder argumentsType(List<ValueParam> argumentsType) { 145 this.argumentsType = argumentsType; 146 return this; 147 } 148 149 /** 116 150 * @return the enumValues 117 151 */ … … 121 155 122 156 /** 123 * @return the procedureSignature124 */125 public String getProcedureSignature() {126 return procedureSignature;127 }128 129 /**130 157 * @return the uid 131 158 */ 132 159 public String getUid() { 133 160 return uid; 161 } 162 163 public @Nonnull <T extends Param> T finish(Class<T> requested) { 164 Param param = finish(); 165 if (!requested.isInstance(param)) { 166 throw new FramsticksException().msg("param is of wrong type").arg("requested", requested).arg("actual", param.getClass()); 167 } 168 return requested.cast(param); 134 169 } 135 170 … … 141 176 * when Param getType is not defined 142 177 */ 143 public Param finish() {178 public @Nonnull Param finish() { 144 179 try { 145 180 if (paramType == null) { 146 181 throw new FramsticksException().msg("trying to finish incomplete param"); 147 182 } 148 149 183 return paramType.getConstructor(ParamBuilder.class).newInstance(this); 150 184 } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | FramsticksException e) { … … 165 199 } 166 200 167 // public <T extends Param> ParamBuilder type(T param) {168 // return internalSetType(param.getClass(), param);169 // }170 171 172 201 /** 173 202 * @return the id … … 178 207 } 179 208 180 /**181 * @return the internalId182 */183 public String getInternalId() {184 return internalId;185 }186 187 public ParamBuilder setInternalId(String internalId) {188 this.internalId = internalId;189 return this;190 }191 192 209 @ParamAnnotation 193 210 public ParamBuilder group(Integer group) { … … 208 225 } 209 226 210 protected <T extends Number> void parseMinMaxDefNumber(Class<T> type, String second, String third ) {227 protected <T extends Number> void parseMinMaxDefNumber(Class<T> type, String second, String third, String fourth) { 211 228 if (second != null) { 212 229 min = second; … … 214 231 if (third != null) { 215 232 max = third; 233 } 234 if (fourth != null) { 235 def = fourth; 216 236 } 217 237 } … … 223 243 return type(EnumParam.class); 224 244 } 225 226 protected String procedureSignature;227 245 228 246 protected String uid; … … 239 257 String second = typeSplitted.length > 1 ? typeSplitted[1] : null; 240 258 String third = typeSplitted.length > 2 ? typeSplitted[2] : null; 259 String fourth = typeSplitted.length > 3 ? typeSplitted[3] : null; 241 260 242 261 switch (first.charAt(0)) { … … 247 266 } 248 267 case 'p': { 249 procedureSignature = type.substring(1);250 268 type(ProcedureParam.class); 269 signature(type.substring(1)); 251 270 break; 252 271 } … … 281 300 } 282 301 if (DecimalParam.class.isAssignableFrom(this.paramType)) { 283 parseMinMaxDefNumber(Integer.class, second, third );302 parseMinMaxDefNumber(Integer.class, second, third, fourth); 284 303 } 285 304 break; … … 287 306 case 'f': { 288 307 type(FloatParam.class); 289 parseMinMaxDefNumber(Double.class, second, third );308 parseMinMaxDefNumber(Double.class, second, third, fourth); 290 309 break; 291 310 } … … 472 491 } 473 492 474 475 493 public Class<?> getStorageType() { 476 494 return storageType; 477 495 } 496 497 public ParamBuilder forAccess(AccessInterface access) { 498 return name(access.getId()).type("o " + access.getId()); 499 } 500 501 protected static ValueParam parseProcedureTypePart(String type, String name) { 502 return Param.build().type(type).name(name).id(name).finish(ValueParam.class); 503 } 504 505 private static Pattern addressPattern = Pattern.compile("^([^\\(]+)?\\(([^\\)]*)\\)$"); 506 507 public ParamBuilder signature(String signature) { 508 argumentsType = new ArrayList<>(); 509 510 if (!Strings.notEmpty(signature)) { 511 resultType = null; 512 return this; 513 } 514 Matcher matcher = addressPattern.matcher(signature); 515 if (!matcher.matches()) { 516 throw new FramsticksException().msg("invalid signature"); 517 } 518 String result = Strings.collapse(matcher.group(1)); 519 resultType = (result != null) ? parseProcedureTypePart(result, null) : null; 520 String arguments = matcher.group(2); 521 if (!Strings.notEmpty(arguments)) { 522 return this; 523 } 524 int number = 0; 525 for (String a : arguments.split(",")) { 526 int space = a.indexOf(' '); 527 String type; 528 String name; 529 if (space == -1) { 530 type = a; 531 name = "arg" + number; 532 } else { 533 type = a.substring(0, space); 534 name = a.substring(space + 1); 535 } 536 argumentsType.add(parseProcedureTypePart(type, name)); 537 ++number; 538 } 539 return this; 540 } 541 542 543 public ParamBuilder idAndName(String name) { 544 id(name); 545 name(name); 546 return this; 547 } 478 548 } 479 549 -
java/main/src/main/java/com/framsticks/params/ParamCandidate.java
r88 r90 10 10 import java.lang.reflect.ParameterizedType; 11 11 import java.lang.reflect.Type; 12 import java.util.Arrays; 12 13 import java.util.Collection; 14 import java.util.Collections; 15 import java.util.Comparator; 13 16 import java.util.HashMap; 17 import java.util.LinkedList; 18 import java.util.List; 14 19 import java.util.Map; 15 20 21 import com.framsticks.params.annotations.FramsClassAnnotation; 16 22 import com.framsticks.params.annotations.ParamAnnotation; 23 import com.framsticks.params.types.ProcedureParam; 17 24 18 25 public class ParamCandidate { … … 65 72 protected final OneTime<Method> setter = new OneTime<>("setter"); 66 73 protected final OneTime<Method> getter = new OneTime<>("getter"); 74 protected final OneTime<Method> caller = new OneTime<>("caller"); 75 76 protected final List<ParamAnnotation> annotations = new LinkedList<>(); 67 77 68 78 /** … … 124 134 } 125 135 136 /** 137 * @return the getter 138 */ 139 public Method getCaller() { 140 return caller.get(); 141 } 142 143 /** 144 * @return the annotations 145 */ 146 public List<ParamAnnotation> getAnnotations() { 147 return Collections.unmodifiableList(annotations); 148 } 149 126 150 void validate() throws ConstructionException { 127 151 try { 152 if (caller.has()) { 153 if (!isPublic(caller)) { 154 throw new ConstructionException().msg("method is not public"); 155 } 156 if (getter.has() || setter.has()) { 157 throw new ConstructionException().msg("getter or setter coexist with caller"); 158 } 159 return; 160 } 128 161 if (isPublic(field)) { 129 162 if (getter.has()) { … … 147 180 148 181 boolean isFinal() { 182 if (caller.has()) { 183 return false; 184 } 149 185 if (Collection.class.isAssignableFrom(getRawType())) { 150 186 return false; … … 160 196 161 197 boolean isReadOnly() { 198 if (caller.has()) { 199 return false; 200 } 162 201 if (Collection.class.isAssignableFrom(getRawType())) { 163 202 return false; … … 172 211 } 173 212 174 void add( Member member, String name) {213 void add(ParamAnnotation paramAnnotation, Member member, String name) { 175 214 this.name.set(name); 215 annotations.add(paramAnnotation); 176 216 if (member instanceof Field) { 177 this.field.set((Field) member);217 field.set((Field) member); 178 218 setType(field.get().getGenericType()); 179 219 return; … … 181 221 if (member instanceof Method) { 182 222 Method m = (Method) member; 223 if (!paramAnnotation.paramType().equals(Param.class)) { 224 if (paramAnnotation.paramType().equals(ProcedureParam.class)) { 225 caller.set(m); 226 return; 227 } 228 throw new ConstructionException().msg("explicit set of paramType different than ProcedureParam is not yet supported").arg("name", name).arg("method", m).arg("in", this); 229 } 183 230 Type[] ps = m.getGenericParameterTypes(); 184 231 if (ps.length == 0) { … … 222 269 } 223 270 224 public static <M extends Member & AnnotatedElement> void filterParamsCandidates( Map<String, ParamCandidate> params, M[] members) {271 public static <M extends Member & AnnotatedElement> void filterParamsCandidates(Set set, M[] members) { 225 272 for (M m : members) { 226 273 ParamAnnotation pa = m.getAnnotation(ParamAnnotation.class); … … 228 275 continue; 229 276 } 230 // if (!isPublic(m)) {231 // throw new ConstructionException().msg("field is not public").arg("field", m);232 // }233 277 String id = FramsClassBuilder.getId(pa, m); 234 278 ParamCandidate pc = null; 235 if ( params.containsKey(id)) {236 pc = params.get(id);279 if (set.getCandidates().containsKey(id)) { 280 pc = set.getCandidates().get(id); 237 281 } else { 238 282 pc = new ParamCandidate(id); 239 params.put(id, pc); 240 } 241 pc.add(m, FramsClassBuilder.getName(pa, m)); 242 243 } 244 } 245 246 public static Map<String, ParamCandidate> getAllCandidates(Class<?> javaClass) throws ConstructionException { 247 Map<String, ParamCandidate> candidates = new HashMap<>(); 248 283 set.getCandidates().put(id, pc); 284 set.getOrder().add(pc); 285 } 286 pc.add(pa, m, FramsClassBuilder.getName(pa, m)); 287 } 288 } 289 290 public static class Set { 291 protected final Map<String, ParamCandidate> candidates; 292 protected final List<ParamCandidate> order; 293 294 /** 295 * @param candidates 296 * @param order 297 */ 298 public Set(Map<String, ParamCandidate> candidates, List<ParamCandidate> order) { 299 this.candidates = candidates; 300 this.order = order; 301 } 302 303 /** 304 * @return the candidates 305 */ 306 public Map<String, ParamCandidate> getCandidates() { 307 return candidates; 308 } 309 310 /** 311 * @return the order 312 */ 313 public List<ParamCandidate> getOrder() { 314 return order; 315 } 316 } 317 318 public static Set getAllCandidates(Class<?> javaClass) throws ConstructionException { 319 320 List<Class<?>> javaClasses = new LinkedList<>(); 249 321 while (javaClass != null) { 250 filterParamsCandidates(candidates, javaClass.getDeclaredFields()); 251 filterParamsCandidates(candidates, javaClass.getDeclaredMethods()); 252 322 javaClasses.add(0, javaClass); 253 323 javaClass = javaClass.getSuperclass(); 254 324 } 255 325 256 for (ParamCandidate pc : candidates.values()) { 326 Set resultSet = new Set(new HashMap<String, ParamCandidate>(), new LinkedList<ParamCandidate>()); 327 328 for (Class<?> j : javaClasses) { 329 Set set = new Set(resultSet.getCandidates(), new LinkedList<ParamCandidate>()); 330 filterParamsCandidates(set, j.getDeclaredFields()); 331 filterParamsCandidates(set, j.getDeclaredMethods()); 332 333 FramsClassAnnotation fa = j.getAnnotation(FramsClassAnnotation.class); 334 if (fa != null) { 335 final List<String> order = Arrays.asList(fa.order()); 336 Collections.sort(set.getOrder(), new Comparator<ParamCandidate>() { 337 @Override 338 public int compare(ParamCandidate pc0, ParamCandidate pc1) { 339 int u0 = order.indexOf(pc0.getId()); 340 int u1 = order.indexOf(pc1.getId()); 341 if (u0 == -1 || u1 == -1) { 342 return 0; 343 } 344 return u0 - u1; 345 } 346 }); 347 } 348 resultSet.getOrder().addAll(0, set.getOrder()); 349 } 350 351 for (ParamCandidate pc : resultSet.getOrder()) { 257 352 pc.validate(); 258 353 } 259 354 260 return candidates;355 return resultSet; 261 356 } 262 357 263 358 public static Class<?> getRawClass(final Type type) { 359 if (type == null) { 360 throw new IllegalArgumentException(); 361 } 264 362 if (Class.class.isInstance(type)) { 265 363 return Class.class.cast(type); -
java/main/src/main/java/com/framsticks/params/PrimitiveParam.java
r88 r90 96 96 return tryCastAndReturn(def, type); 97 97 } 98 98 99 } -
java/main/src/main/java/com/framsticks/params/PropertiesAccess.java
r87 r90 3 3 import java.util.HashMap; 4 4 import java.util.Map; 5 6 import com.framsticks.params.types.ProcedureParam; 5 7 6 8 … … 93 95 } 94 96 97 @Override 98 public Object call(String id, Object[] arguments) { 99 throw new InvalidOperationException().msg("list access does not support calling methods").arg("id", id); 100 } 101 102 @Override 103 public Object call(ProcedureParam param, Object[] arguments) { 104 throw new InvalidOperationException().msg("list access does not support calling methods").arg("param", param); 105 } 106 95 107 } -
java/main/src/main/java/com/framsticks/params/ReflectionAccess.java
r88 r90 4 4 import java.lang.reflect.InvocationTargetException; 5 5 import java.lang.reflect.Method; 6 import java.util.ArrayList; 6 7 import java.util.Collections; 8 import java.util.Comparator; 7 9 import java.util.HashMap; 8 import java.util.LinkedList;9 10 import java.util.List; 10 11 import java.util.Map; … … 15 16 16 17 import com.framsticks.params.annotations.AutoAppendAnnotation; 18 import com.framsticks.params.types.ProcedureParam; 17 19 import com.framsticks.util.FramsticksException; 18 20 import com.framsticks.util.lang.Pair; … … 31 33 private final static Logger log = Logger.getLogger(ReflectionAccess.class.getName()); 32 34 33 protected final Class<?> reflectedClass;35 protected final Class<?> javaClass; 34 36 protected final Backend backend; 35 37 … … 41 43 protected static final Map<Pair<Class<?>, FramsClass>, Backend> synchronizedCache = Collections.synchronizedMap(new HashMap<Pair<Class<?>, FramsClass>, Backend>()); 42 44 43 public static class ReflectedValueParam { 44 public ReflectedSetter setter;45 public ReflectedGetter getter;45 46 public interface ReflectedGetter { 47 public <T> T get(Object object, Class<T> type) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException; 46 48 } 47 49 … … 50 52 } 51 53 52 public interface ReflectedGetter { 53 public abstract <T> T get(Object object, Class<T> type) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException; 54 } 55 56 protected final Map<ValueParam, ReflectedValueParam> params; 57 protected final List<Method> autoAppendMethods; 54 public interface ReflectedCaller { 55 public Object call(Object object, Object[] arguments) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException; 56 } 57 58 protected final Map<ValueParam, ReflectedSetter> setters = new HashMap<>(); 59 protected final Map<ValueParam, ReflectedGetter> getters = new HashMap<>(); 60 protected final Map<ProcedureParam, ReflectedCaller> callers = new HashMap<>(); 61 protected final List<Method> autoAppendMethods = new ArrayList<>(); 58 62 59 63 /** 60 64 * @param params 61 65 */ 62 public Backend(Map<ValueParam, ReflectedValueParam> params, List<Method> autoAppendMethods) { 63 // this.params = Collections.unmodifiableMap(params); 64 this.params = params; 65 this.autoAppendMethods = autoAppendMethods; 66 public Backend() { 66 67 } 67 68 … … 75 76 76 77 log.debug("constructing backend for " + id); 77 final Map<ValueParam, ReflectedValueParam> params = new HashMap<>(); 78 79 Map<String, ParamCandidate> candidates = ParamCandidate.getAllCandidates(reflectedClass); 80 81 try { 78 backend = new Backend(); 79 80 Map<String, ParamCandidate> candidates = ParamCandidate.getAllCandidates(reflectedClass).getCandidates(); 81 82 try { 83 for (final ProcedureParam pp : filterInstanceof(framsClass.getParamEntries(), ProcedureParam.class)) { 84 if (!candidates.containsKey(pp.getId())) { 85 log.trace("java class does implement method " + pp); 86 continue; 87 } 88 ParamCandidate pc = candidates.get(pp.getId()); 89 final Method method = pc.getCaller(); 90 91 backend.callers.put(pp, new ReflectedCaller() { 92 93 @Override 94 public Object call(Object object, Object[] arguments) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { 95 return method.invoke(object, arguments); 96 } 97 }); 98 99 } 100 82 101 for (final ValueParam vp : filterInstanceof(framsClass.getParamEntries(), ValueParam.class)) { 83 102 if (!candidates.containsKey(vp.getId())) { … … 92 111 } 93 112 94 ReflectedValueParam rvp = new ReflectedValueParam();95 params.put(vp, rvp);96 113 final boolean primitive = pc.isPrimitive(); 97 114 if (pc.getField() != null) { 98 115 final Field f = pc.getField(); 99 rvp.getter =new ReflectedGetter() {116 backend.getters.put(vp, new ReflectedGetter() { 100 117 @Override 101 118 public <T> T get(Object object, Class<T> type) throws IllegalArgumentException, IllegalAccessException { 102 119 return type.cast(f.get(object)); 103 120 } 104 } ;121 }); 105 122 if (!pc.isFinal()) { 106 rvp.setter =new ReflectedSetter() {123 backend.setters.put(vp, new ReflectedSetter() { 107 124 @Override 108 125 public <T> void set(Object object, T value) throws IllegalArgumentException, IllegalAccessException { … … 112 129 f.set(object, value); 113 130 } 114 } ;131 }); 115 132 } 116 133 } else { 117 134 final Method g = pc.getGetter(); 118 135 119 rvp.getter =new ReflectedGetter() {136 backend.getters.put(vp, new ReflectedGetter() { 120 137 @Override 121 138 public <T> T get(Object object, Class<T> type) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { 122 139 return type.cast(g.invoke(object)); 123 140 } 124 } ;141 }); 125 142 126 143 if (!pc.isFinal()) { 127 144 final Method s = pc.getSetter(); 128 rvp.setter =new ReflectedSetter() {145 backend.setters.put(vp, new ReflectedSetter() { 129 146 @Override 130 147 public <T> void set(Object object, T value) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { … … 134 151 s.invoke(object, value); 135 152 } 136 } ;153 }); 137 154 } 138 155 } … … 141 158 throw e.arg("java class", reflectedClass).arg("framsClass", framsClass); 142 159 } 143 144 List<Method> autoAppendMethods = new LinkedList<>();145 160 146 161 Class<?> javaClass = reflectedClass; … … 156 171 throw new ConstructionException().msg("invalid number of arguments in AutoAppend marked method").arg("method", m).arg("arguments", args.length); 157 172 } 158 autoAppendMethods.add(m);173 backend.autoAppendMethods.add(m); 159 174 } 160 175 … … 162 177 } 163 178 164 backend = new Backend(params, autoAppendMethods); 179 Collections.sort(backend.autoAppendMethods, new Comparator<Method>() { 180 181 @Override 182 public int compare(Method m0, Method m1) { 183 Class<?> arg0 = m0.getParameterTypes()[0]; 184 Class<?> arg1 = m1.getParameterTypes()[0]; 185 if (arg0.isAssignableFrom(arg1)) { 186 return 1; 187 } 188 if (arg1.isAssignableFrom(arg0)) { 189 return -1; 190 } 191 return 0; 192 } 193 }); 194 165 195 synchronizedCache.put(id, backend); 166 196 return backend; … … 194 224 public ReflectionAccess(Class<?> reflectedClass, FramsClass framsClass) throws ConstructionException { 195 225 super(framsClass); 196 this. reflectedClass = reflectedClass;226 this.javaClass = reflectedClass; 197 227 this.backend = Backend.getOrCreateFor(reflectedClass, framsClass); 198 // log.info("created ReflectionAccess " + this); 199 } 200 201 // private static String accessorName(boolean get, String id) { 202 // return (get ? "get" : "set") + id.substring(0, 1).toUpperCase() + id.substring(1); 203 // } 228 } 229 204 230 205 231 @Override … … 211 237 } 212 238 213 return backend. params.get(param).getter.get(object, type);239 return backend.getters.get(param).get(object, type); 214 240 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { 215 241 throw new FramsticksException().msg("failed to get").cause(e); … … 231 257 throw new FramsticksException().msg("no object set"); 232 258 } 233 Backend.ReflectedSetter s = backend. params.get(param).setter;259 Backend.ReflectedSetter s = backend.setters.get(param); 234 260 if (s == null) { 235 throw new FramsticksException().msg("trying to set final");261 throw new FramsticksException().msg("trying to set unsettable"); 236 262 } 237 263 s.set(object, value); … … 241 267 } catch (FramsticksException e) { 242 268 throw e.arg("param", param).arg("value", value).arg("access", this); 269 } 270 } 271 272 @Override 273 public Object call(String id, Object[] arguments) { 274 return call(framsClass.getParamEntry(id, ProcedureParam.class), arguments); 275 } 276 277 @Override 278 public Object call(ProcedureParam param, Object[] arguments) { 279 try { 280 try { 281 if (object == null) { 282 throw new FramsticksException().msg("no object set"); 283 } 284 Backend.ReflectedCaller c = backend.callers.get(param); 285 if (c == null) { 286 throw new FramsticksException().msg("method is not bound"); 287 } 288 return c.call(object, arguments); 289 } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) { 290 throw new FramsticksException().msg("failed to call").cause(e); 291 } 292 } catch (FramsticksException e) { 293 throw e.arg("param", param).arg("access", this); 243 294 } 244 295 } … … 273 324 @Override 274 325 public ReflectionAccess select(Object object) { 275 assert object == null || reflectedClass.isInstance(object);326 assert object == null || javaClass.isInstance(object); 276 327 this.object = object; 277 328 return this; … … 302 353 @Override 303 354 public ReflectionAccess cloneAccess() throws ConstructionException { 304 return new ReflectionAccess( reflectedClass, framsClass);355 return new ReflectionAccess(javaClass, framsClass); 305 356 } 306 357 … … 308 359 public Object createAccessee() { 309 360 try { 310 return reflectedClass.newInstance();361 return javaClass.newInstance(); 311 362 } catch (InstantiationException | IllegalAccessException e) { 312 363 e.printStackTrace(); 313 364 } 314 log.fatal("failed to create reflected object of class " + reflectedClass.getCanonicalName() + " for frams type " + framsClass.getId());365 log.fatal("failed to create reflected object of class " + javaClass.getCanonicalName() + " for frams type " + framsClass.getId()); 315 366 return null; 316 367 } … … 332 383 if (m.getParameterTypes()[0].isAssignableFrom(value.getClass())) { 333 384 try { 385 log.trace("auto appending with value " + value + " with method " + m); 334 386 m.invoke(object, value); 335 387 return true; … … 341 393 return false; 342 394 } 395 343 396 } 344 397 -
java/main/src/main/java/com/framsticks/params/Registry.java
r88 r90 4 4 5 5 import com.framsticks.params.annotations.FramsClassAnnotation; 6 import com.framsticks.params.annotations.ParamAnnotation; 6 7 import com.framsticks.util.DoubleMap; 7 8 import com.framsticks.util.FramsticksException; 8 9 10 import java.util.HashMap; 11 import java.util.Map; 9 12 import java.util.Set; 13 14 import javax.annotation.Nonnull; 10 15 11 16 /** 12 17 * Author: Piotr Śniegowski 13 18 */ 19 @FramsClassAnnotation 14 20 public class Registry { 15 21 private static final Logger log = Logger.getLogger(Registry.class.getName()); 16 22 17 // protected void internalRegisterClass(FramsClass framsClass, @Nullable Class<?> javaClass) {18 // assert framsClass.getName() != null;19 // assert framsClass.getId() != null;20 // infoCacheByName.put(framsClass.getName(), framsClass);21 // infoCacheById.put(framsClass.getId(), framsClass);22 23 // if (javaClass != null) {24 // reflectedClasses.put(framsClass, javaClass);25 // infoCacheByJavaName.put(javaClass.getCanonicalName(), framsClass);26 // }27 // }28 23 protected final DoubleMap<String, Class<?>> javaClasses = new DoubleMap<>(); 29 24 protected final DoubleMap<String, FramsClass> framsClasses = new DoubleMap<>(); 25 protected final Map<Class<?>, FramsClass> javaToFramsAssociation = new HashMap<>(); 30 26 31 public void registerReflectedClass(String name, String id, Class<?> reflectedClass) { 32 javaClasses.put(id, name, reflectedClass); 27 /** 28 * 29 */ 30 public Registry() { 31 // registerAndBuild(Registry.class); 32 // registerAndBuild(FramsClass.class); 33 // registerAndBuild(Param.class); 33 34 } 34 35 35 public Registry registerAndBuild(Class<?> reflectedClass) { 36 register(reflectedClass); 37 putInfoIntoCache(FramsClass.build().forClass(reflectedClass)); 36 public void registerReflectedClass(String name, String id, Class<?> javaClass) { 37 javaClasses.put(id, name, javaClass); 38 } 39 40 public void associate(Class<?> javaClass, FramsClass framsClass) { 41 javaToFramsAssociation.put(javaClass, framsClass); 42 } 43 44 public Registry registerAndBuild(Class<?> javaClass) { 45 register(javaClass); 46 associate(javaClass, putFramsClass(FramsClass.build().forClass(javaClass))); 38 47 return this; 39 48 } 40 49 41 public Registry register(Class<?> reflectedClass) {42 FramsClassAnnotation a = reflectedClass.getAnnotation(FramsClassAnnotation.class);50 public Registry register(Class<?> javaClass) { 51 FramsClassAnnotation a = javaClass.getAnnotation(FramsClassAnnotation.class); 43 52 if (a == null) { 44 throw new FramsticksException().msg("class is not annotated").arg("class", reflectedClass);53 throw new FramsticksException().msg("class is not annotated").arg("class", javaClass); 45 54 } 46 55 47 registerReflectedClass(FramsClassBuilder.getName(a, reflectedClass), FramsClassBuilder.getId(a, reflectedClass), reflectedClass);56 registerReflectedClass(FramsClassBuilder.getName(a, javaClass), FramsClassBuilder.getId(a, javaClass), javaClass); 48 57 return this; 49 58 } 50 59 51 public AccessInterface createAccess(String name, FramsClass framsClass) throws ConstructionException { 60 public @Nonnull ReflectionAccess createAccess(Class<?> javaClass) throws ConstructionException { 61 try { 62 if (!javaClasses.containsValue(javaClass)) { 63 throw new FramsticksException().msg("java class is not registered"); 64 } 65 if (!javaToFramsAssociation.containsKey(javaClass)) { 66 throw new FramsticksException().msg("java class is not associated with any frams class"); 67 } 68 return new ReflectionAccess(javaClass, javaToFramsAssociation.get(javaClass)); 69 } 70 catch (FramsticksException e) { 71 throw new FramsticksException().msg("failed to create access for java class").arg("class", javaClass).cause(e); 72 } 73 } 74 75 public @Nonnull AccessInterface createAccess(String name, FramsClass framsClass) throws ConstructionException { 52 76 // assert framsClasses.containsValue(framsClass); 53 77 if (javaClasses.containsKey(name)) { … … 57 81 } 58 82 59 public void putInfoIntoCache(FramsClass framsClass) {83 public FramsClass putFramsClass(FramsClass framsClass) { 60 84 log.debug("caching info for " + framsClass); 61 85 framsClasses.put(framsClass.getId(), framsClass.getName(), framsClass); 86 return framsClass; 62 87 } 63 88 64 public FramsClass getInfoFromCache(String identifier) { 65 if (identifier == null) { 66 return null; 67 } 89 public FramsClass getFramsClass(@Nonnull String identifier) { 68 90 return framsClasses.get(identifier); 69 91 } 70 92 71 public static AccessInterface wrapAccessWithListIfNeeded(CompositeParam param, AccessInterface access) { 72 if (access == null) { 73 return null; 74 } 93 public static @Nonnull AccessInterface wrapAccessWithListIfNeeded(@Nonnull CompositeParam param, @Nonnull AccessInterface access) { 75 94 return param.prepareAccessInterface(access); 76 95 } 77 96 78 public AccessInterface prepareAccess(CompositeParam param) throws ConstructionException {97 public @Nonnull AccessInterface prepareAccess(CompositeParam param) throws ConstructionException { 79 98 return wrapAccessWithListIfNeeded(param, createAccess(param.getContainedTypeName())); 80 99 } 81 100 82 public AccessInterface createAccess(String name) throws ConstructionException { 83 if (name == null) { 84 return null; 101 public @Nonnull AccessInterface createAccess(@Nonnull String name) throws ConstructionException { 102 try { 103 FramsClass framsClass = getFramsClass(name); 104 if (framsClass == null) { 105 throw new ConstructionException().msg("framsclass is missing"); 106 } 107 108 return createAccess(name, framsClass); 85 109 } 86 FramsClass framsClass = getInfoFromCache(name); 87 if (framsClass == null) { 88 return null; 110 catch (FramsticksException e) { 111 throw new FramsticksException().msg("failed to create access for name").arg("name", name).cause(e); 89 112 } 90 91 return createAccess(name, framsClass);92 113 } 93 114 … … 96 117 } 97 118 98 public Set<FramsClass> get InfoCache() {119 public Set<FramsClass> getFramsClasses() { 99 120 return framsClasses.getValues(); 100 121 } 101 122 123 @ParamAnnotation 124 public Map<String, FramsClass> getFramsClassesById() { 125 return framsClasses.getValuesById(); 126 } 127 128 public void takeAllFrom(Registry source) { 129 for (Class<?> javaClass : source.getReflectedClasses()) { 130 register(javaClass); 131 } 132 for (FramsClass framsClass : source.getFramsClasses()) { 133 putFramsClass(framsClass); 134 } 135 136 } 137 102 138 } -
java/main/src/main/java/com/framsticks/params/SimpleAbstractAccess.java
r87 r90 78 78 } 79 79 80 @Override81 public Param getGroupMember(int gi, int n) {82 return framsClass.getGroupMember(gi, n);83 }80 // @Override 81 // public Param getGroupMember(int gi, int n) { 82 // return framsClass.getGroupMember(gi, n); 83 // } 84 84 85 85 @Override -
java/main/src/main/java/com/framsticks/params/UniqueListAccess.java
r87 r90 45 45 } 46 46 //TODO list access here 47 return Param.build().id(id). name(elementAccess.getId()).type("o " + elementAccess.getId()).finish();47 return Param.build().id(id).forAccess(elementAccess).finish(); 48 48 } 49 49 -
java/main/src/main/java/com/framsticks/params/Util.java
r86 r90 3 3 import java.util.ArrayList; 4 4 import java.util.List; 5 6 import com.framsticks.util.FramsticksException; 5 7 6 8 … … 22 24 } 23 25 24 public static List<Object> stripAccessInterface(List<AccessInterface> accesses) {25 List< Object> result = new ArrayList<Object>();26 public static <T> List<T> stripAccessInterface(List<AccessInterface> accesses, Class<T> type) { 27 List<T> result = new ArrayList<T>(); 26 28 for (AccessInterface a : accesses) { 27 result.add(a.getSelected()); 29 Object object = a.getSelected(); 30 if (!type.isInstance(object)) { 31 throw new FramsticksException().msg("extracted object is of invalid type").arg("object", object).arg("desired", type).arg("actual", object.getClass()).arg("framsclass", a.getFramsClass()); 32 } 33 result.add(type.cast(object)); 28 34 } 29 35 return result; -
java/main/src/main/java/com/framsticks/params/annotations/FramsClassAnnotation.java
r86 r90 11 11 String name() default ""; 12 12 String id() default ""; 13 14 String[] order() default {}; 13 15 } -
java/main/src/main/java/com/framsticks/params/annotations/ParamAnnotation.java
r86 r90 6 6 import java.lang.annotation.Target; 7 7 8 import com.framsticks.params.Param; 9 8 10 @Retention(RetentionPolicy.RUNTIME) 9 11 @Target({ElementType.FIELD, ElementType.METHOD}) … … 11 13 String name() default ""; 12 14 String id() default ""; 15 16 Class<? extends Param> paramType() default Param.class; 17 String stringType() default ""; 18 19 String def() default ""; 20 String min() default ""; 21 String max() default ""; 22 23 String help() default ""; 24 // int flags() default 0; 25 int extra() default 0; 26 // String group() default ""; 13 27 } 14 28 -
java/main/src/main/java/com/framsticks/params/types/ObjectParam.java
r87 r90 15 15 public class ObjectParam extends CompositeParam { 16 16 17 protected final Class<?> storageType; 18 17 19 public ObjectParam(ParamBuilder builder) { 18 super(builder); 20 super(builder.fillStorageType(Object.class)); 21 storageType = builder.getStorageType(); 19 22 } 20 23 … … 26 29 @Override 27 30 public Class<?> getStorageType() { 28 return Object.class;31 return storageType; 29 32 } 30 33 31 34 @Override 32 35 public ReassignResult<Object> reassign(Object newValue, Object oldValue) throws CastFailure { 36 if (newValue != null && !storageType.isInstance(newValue)) { 37 throw new CastFailure(); 38 } 33 39 return ReassignResult.create(newValue); 34 40 } -
java/main/src/main/java/com/framsticks/params/types/ProcedureParam.java
r87 r90 3 3 import com.framsticks.params.Param; 4 4 import com.framsticks.params.ParamBuilder; 5 import com.framsticks.util.FramsticksException; 6 import com.framsticks.util.lang.Strings; 5 import com.framsticks.params.ValueParam; 7 6 8 import java.util.ArrayList;9 7 import java.util.List; 10 import java.util.regex.Matcher;11 import java.util.regex.Pattern;12 8 13 9 import javax.annotation.concurrent.Immutable; … … 18 14 @Immutable 19 15 public class ProcedureParam extends Param { 20 private final Param resultType; 21 private final List<Param> argumentsType = new ArrayList<Param>(); 22 23 private static Pattern addressPattern = Pattern.compile("^([^\\(]+)?\\(([^\\)]*)\\)$"); 16 private final ValueParam resultType; 17 private final List<ValueParam> argumentsType; 24 18 25 19 /** … … 28 22 public ProcedureParam(ParamBuilder builder) { 29 23 super(builder); 30 String signature = builder.getProcedureSignature(); 31 if (Strings.notEmpty(signature)) { 32 Matcher matcher = addressPattern.matcher(signature); 33 if (!matcher.matches()) { 34 throw new FramsticksException().msg("invalid signature"); 35 } 36 String result = Strings.collapse(matcher.group(1)); 37 resultType = (result != null) ? parseType(result, null) : null; 38 String arguments = matcher.group(2); 39 if (!Strings.notEmpty(arguments)) { 40 return; 41 } 42 for (String a : arguments.split(",")) { 43 int space = a.indexOf(' '); 44 String type; 45 String name; 46 if (space == -1) { 47 type = a; 48 name = null; 49 } else { 50 type = a.substring(0, space); 51 name = a.substring(space + 1); 52 } 53 argumentsType.add(parseType(type, name)); 54 } 55 } else { 56 resultType = null; 57 } 58 } 59 60 protected static Param parseType(String type, String name) { 61 return Param.build().type(type).name(name).finish(); 24 resultType = builder.getResultType(); 25 argumentsType = builder.getArgumentsType(); 26 assert argumentsType != null; 62 27 } 63 28 … … 67 32 } 68 33 69 public Param getResultType() {34 public ValueParam getResultType() { 70 35 return resultType; 71 36 } 72 37 73 public List< Param> getArgumentsType() {38 public List<ValueParam> getArgumentsType() { 74 39 return argumentsType; 75 40 } -
java/main/src/main/java/com/framsticks/parsers/F0Parser.java
r88 r90 18 18 import com.framsticks.params.Param; 19 19 import com.framsticks.params.PrimitiveParam; 20 import com.framsticks.util.FramsticksException; 20 21 import com.framsticks.util.lang.Containers; 21 22 import com.framsticks.util.lang.Pair; … … 82 83 * the parse exception 83 84 */ 84 public List<AccessInterface> parse() throws IOException, 85 ParseException { 85 public List<AccessInterface> parse() { 86 86 87 87 try (InputStreamReader reader = new InputStreamReader(is, "UTF-8")) { … … 114 114 result.add(0, processLine("m:")); 115 115 } 116 } catch (IOException e) { 117 throw new FramsticksException().msg("failed to parse f0").arg("parser", this).arg("schema", schema).cause(e); 116 118 } 117 119 -
java/main/src/main/java/com/framsticks/parsers/F0Writer.java
r88 r90 44 44 for (ValueParam param : filterInstanceof(access.getParams(), ValueParam.class)) { 45 45 if (param instanceof CompositeParam) { 46 AccessInterface a = schema.getRegistry().prepareAccess((CompositeParam) param);46 AccessInterface a = schema.getRegistry().prepareAccess((CompositeParam) param); 47 47 a.select(access.get((ValueParam) param, Object.class)); 48 48 write(a); … … 78 78 sink.print(line).breakLine(); 79 79 } 80 80 81 public void write() { 81 82 AccessInterface access = schema.getRegistry().createAccess("m"); -
java/main/src/main/java/com/framsticks/parsers/XmlLoader.java
r88 r90 2 2 3 3 import java.io.InputStream; 4 import java.util.LinkedList; 5 import java.util.List; 4 6 5 7 import javax.xml.parsers.DocumentBuilder; … … 15 17 import com.framsticks.params.AccessInterface; 16 18 import com.framsticks.params.Registry; 19 import com.framsticks.util.AutoBuilder; 17 20 import com.framsticks.util.FramsticksException; 18 21 … … 86 89 } 87 90 88 if (!access.tryAutoAppend(childObject)) { 89 throw new FramsticksException().msg("failed to auto append").arg("child", childObject).arg("parent", object); 91 List<Object> childrenObjects = new LinkedList<>(); 92 93 if (childObject instanceof AutoBuilder) { 94 childrenObjects.addAll(((AutoBuilder) childObject).autoFinish()); 95 } else { 96 childrenObjects.add(childObject); 97 } 98 99 for (Object child : childrenObjects) { 100 if (!access.tryAutoAppend(child)) { 101 throw new FramsticksException().msg("failed to auto append").arg("child", child).arg("parent", object); 102 } 90 103 } 91 104 } … … 112 125 } 113 126 114 public <T> T load( InputStream stream, Class<T> type) {127 public <T> T load(Class<T> type, InputStream stream) { 115 128 registry.registerAndBuild(type); 116 129 -
java/main/src/main/java/com/framsticks/portals/Portal.java
r88 r90 58 58 } 59 59 final String path = "/simulator/genepools/groups/0/genotypes"; 60 instance. invokeLater(new RunAt<Instance>() {60 instance.dispatch(new RunAt<Instance>() { 61 61 @Override 62 62 public void run() { -
java/main/src/main/java/com/framsticks/remote/RecursiveFetcher.java
r85 r90 52 52 if (childPath.isResolved() && instance.getInfoFromCache(childPath) != null) { 53 53 ++dispatched; 54 instance. invokeLater(new RunAt<Instance>() {54 instance.dispatch(new RunAt<Instance>() { 55 55 @Override 56 56 public void run() { -
java/main/src/main/java/com/framsticks/remote/RemoteInstance.java
r88 r90 12 12 import com.framsticks.params.annotations.ParamAnnotation; 13 13 import com.framsticks.params.types.EventParam; 14 import com.framsticks.params.types.ProcedureParam; 14 15 import com.framsticks.parsers.MultiParamLoader; 15 16 import com.framsticks.core.Instance; … … 82 83 } 83 84 84 invokeLater(new RunAt<Instance>() {85 dispatch(new RunAt<Instance>() { 85 86 @Override 86 87 public void run() { … … 103 104 @Override 104 105 public void call(List<File> files) { 105 invokeLater(new RunAt<Instance>() {106 dispatch(new RunAt<Instance>() { 106 107 @Override 107 108 public void run() { … … 361 362 log.debug("subscribed for change event for " + path); 362 363 // subscription.setDispatcher(RemoteInstance.this); 363 RemoteInstance.this. invokeLater(new RunAt<Instance>() {364 RemoteInstance.this.dispatch(new RunAt<Instance>() { 364 365 @Override 365 366 public void run() { … … 497 498 } 498 499 499 500 500 @Override 501 501 public void childChangedState(Joinable joinable, JoinableState state) { … … 503 503 } 504 504 505 @Override 506 public void call(Path path, ProcedureParam param, Object[] arguments, StateFunctor stateFunctor) { 507 throw new UnimplementedException(); 508 } 505 509 506 510 } -
java/main/src/main/java/com/framsticks/running/ExternalProcess.java
r88 r90 15 15 16 16 import com.framsticks.core.Entity; 17 import com.framsticks.params.annotations.AutoAppendAnnotation; 17 18 import com.framsticks.params.annotations.FramsClassAnnotation; 18 19 import com.framsticks.params.annotations.ParamAnnotation; 19 20 import com.framsticks.util.FramsticksException; 20 21 import com.framsticks.util.Misc; 22 import com.framsticks.util.dispatching.AbstractJoinable; 23 import com.framsticks.util.dispatching.Dispatching; 24 import com.framsticks.util.dispatching.Joinable; 25 import com.framsticks.util.dispatching.JoinableParent; 26 import com.framsticks.util.dispatching.JoinableState; 27 import com.framsticks.util.dispatching.RunAt; 21 28 import com.framsticks.util.dispatching.Thread; 22 29 import com.framsticks.util.io.Encoding; 23 30 24 31 @FramsClassAnnotation 25 public class ExternalProcess extends Thread<ExternalProcess> implementsEntity {32 public class ExternalProcess extends AbstractJoinable implements JoinableParent, Entity { 26 33 private static final Logger log = Logger.getLogger(ExternalProcess.class); 27 34 28 35 protected List<String> arguments = new ArrayList<>(); 29 36 protected Process process; 30 protected ProcessBuilder builder = new ProcessBuilder();31 protected Thread<ExternalProcess> readerThread ;37 protected final ProcessBuilder builder = new ProcessBuilder(); 38 protected Thread<ExternalProcess> readerThread = new Thread<ExternalProcess>(); 32 39 33 40 protected PrintWriter input; … … 37 44 protected final List<OutputListener> listeners = new LinkedList<>(); 38 45 46 @AutoAppendAnnotation 39 47 public void addListener(OutputListener listener) { 40 48 synchronized (listeners) { … … 50 58 setName("process"); 51 59 arguments.add(null); 60 builder.redirectErrorStream(true); 52 61 } 53 62 … … 68 77 } 69 78 70 @Override 71 protected void routine() { 79 protected void readerTask() { 72 80 73 81 String line; … … 91 99 } 92 100 log.debug("process ended " + this); 93 process = null;101 // process = null; 94 102 } catch (FramsticksException e) { 95 103 log.error("exception caught in process " + this, e); 96 104 } 97 105 interrupt(); 106 // finish(); 98 107 } 99 100 101 108 102 109 @ParamAnnotation … … 111 118 112 119 @Override 113 public void joinableStart() { 114 builder = new ProcessBuilder(); 120 protected void joinableStart() { 115 121 log.debug("running process with arguments: " + arguments); 116 122 builder.command(arguments); … … 119 125 input = new PrintWriter(new OutputStreamWriter(process.getOutputStream(), Encoding.getDefaultCharset())); 120 126 output = new BufferedReader(new InputStreamReader(process.getInputStream(), Encoding.getDefaultCharset())); 127 121 128 } catch (IOException e) { 122 129 throw new FramsticksException().msg("failed to start process").cause(e); 123 130 } 124 super.joinableStart();125 131 132 readerThread.dispatch(new RunAt<ExternalProcess>() { 133 134 @Override 135 public void run() { 136 readerTask(); 137 } 138 139 }); 140 Dispatching.use(readerThread, this); 126 141 } 127 142 128 143 @Override 129 144 public String toString() { 130 return super.toString() + "[" + Misc.returnNotNull(getCommand(), "?") + "]";145 return getName() + "[" + Misc.returnNotNull(getCommand(), "?") + "]"; 131 146 } 132 147 … … 140 155 @Override 141 156 protected void joinableInterrupt() { 142 super.joinableInterrupt(); 143 finish(); 157 process.destroy(); 158 Dispatching.drop(readerThread, this); 159 // finish(); 144 160 } 145 161 162 @Override 163 @ParamAnnotation 164 public String getName() { 165 return readerThread.getName(); 166 } 167 168 /** 169 * @param name the name to set 170 */ 171 @ParamAnnotation 172 public void setName(String name) { 173 readerThread.setName(name); 174 } 175 176 @Override 177 protected void joinableFinish() { 178 179 } 180 181 @Override 182 protected void joinableJoin() throws InterruptedException { 183 Dispatching.join(readerThread); 184 } 185 186 @Override 187 public void childChangedState(Joinable joinable, JoinableState state) { 188 proceedToState(state); 189 } 190 191 192 146 193 } -
java/main/src/main/java/com/framsticks/running/FramsServer.java
r88 r90 8 8 9 9 protected Integer port; 10 protected String path; 10 11 11 12 /** … … 14 15 public FramsServer() { 15 16 super(); 16 setCommand("/home/psniegowski/mgr/Framsticks32rc5/frams.linux"); 17 setDirectory("/home/psniegowski/mgr/Framsticks32rc5"); 17 setPath(System.getProperties().get("user.home") + "/opt/framsticks"); 18 18 setName("frams"); 19 19 } 20 20 21 21 22 /** … … 35 36 } 36 37 38 /** 39 * @return the path 40 */ 41 @ParamAnnotation 42 public String getPath() { 43 return path; 44 } 45 46 /** 47 * @param path the path to set 48 */ 49 @ParamAnnotation 50 public void setPath(String path) { 51 this.path = path; 52 setCommand(path + "/frams.linux"); 53 setDirectory(path); 54 } 55 37 56 @Override 38 p ublicvoid joinableStart() {57 protected void joinableStart() { 39 58 if (port != null) { 59 arguments.add("-n"); 40 60 arguments.add("-p" + port); 41 61 } -
java/main/src/main/java/com/framsticks/util/DoubleMap.java
r88 r90 50 50 } 51 51 52 public Map<K, V> getValuesById() { 53 return Collections.unmodifiableMap(byId); 54 } 55 52 56 } -
java/main/src/main/java/com/framsticks/util/Logging.java
r77 r90 7 7 */ 8 8 public abstract class Logging { 9 10 11 logger.error("failed to " + action + " " + subject + ": " +e);12 13 14 15 16 17 18 9 public static boolean log(Logger logger, String action, Object subject, Exception e) { 10 if (e != null) { 11 logger.error("failed to " + action + " " + subject + ": ", e); 12 return true; 13 } 14 if (logger.isDebugEnabled()) { 15 logger.debug("done: " + action + " " + subject); 16 } 17 return false; 18 } 19 19 } -
java/main/src/main/java/com/framsticks/util/PeriodicTask.java
r85 r90 16 16 this.period = period; 17 17 this.dispatcher = dispatcher; 18 dispatcher. invokeLater(this);18 dispatcher.dispatch(this); 19 19 } 20 20 21 21 22 22 public void again() { 23 dispatcher. invokeLater(new Task<C>(System.currentTimeMillis() + period) {23 dispatcher.dispatch(new Task<C>(System.currentTimeMillis() + period) { 24 24 @Override 25 25 public void run() { -
java/main/src/main/java/com/framsticks/util/dispatching/AbstractJoinable.java
r88 r90 64 64 } 65 65 } 66 this.notifyAll(); 66 67 } 67 68 report(); -
java/main/src/main/java/com/framsticks/util/dispatching/AtOnceDispatcher.java
r88 r90 20 20 21 21 @Override 22 public final void invokeLater(RunAt<? extends C> runnable) {22 public final void dispatch(RunAt<? extends C> runnable) { 23 23 runnable.run(); 24 24 } -
java/main/src/main/java/com/framsticks/util/dispatching/Dispatcher.java
r88 r90 6 6 public interface Dispatcher<C> { 7 7 public boolean isActive(); 8 public void invokeLater(RunAt<? extends C> runnable);8 public void dispatch(RunAt<? extends C> runnable); 9 9 10 10 } -
java/main/src/main/java/com/framsticks/util/dispatching/Dispatching.java
r88 r90 15 15 } 16 16 17 public static <C> void invokeLaterOrNow(Dispatcher<C> dispatcher, RunAt<? extends C> runnable) {17 public static <C> void dispatchIfNotActive(Dispatcher<C> dispatcher, RunAt<? extends C> runnable) { 18 18 if (dispatcher.isActive()) { 19 19 runnable.run(); 20 20 return; 21 21 } 22 dispatcher. invokeLater(runnable);22 dispatcher.dispatch(runnable); 23 23 } 24 24 25 25 //TODO RunAt StateFunctor 26 26 public static <C> void dispatchOk(Dispatcher<C> dispatcher, final StateFunctor stateFunctor) { 27 dispatcher. invokeLater(new RunAt<C>() {27 dispatcher.dispatch(new RunAt<C>() { 28 28 @Override 29 29 public void run() { … … 39 39 40 40 public static <P, C> void invokeDispatch(Dispatcher<P> dispatcher, final Dispatcher<C> finalDispatcher, final RunAt<C> runnable) { 41 dispatcher. invokeLater(new RunAt<P>() {41 dispatcher.dispatch(new RunAt<P>() { 42 42 @Override 43 43 public void run() { 44 finalDispatcher. invokeLater(runnable);44 finalDispatcher.dispatch(runnable); 45 45 } 46 46 }); 47 47 } 48 48 49 public static void sleep( int milliseconds) {49 public static void sleep(double seconds) { 50 50 try { 51 java.lang.Thread.sleep( milliseconds, 0);51 java.lang.Thread.sleep((long) (seconds * 1000)); 52 52 } catch (InterruptedException e) { 53 53 … … 58 58 public static void dispatcherGuardedInvoke(Joinable joinable, RunAt<?> runnable) { 59 59 if (joinable instanceof Dispatcher) { 60 invokeLaterOrNow(Dispatcher.class.cast(joinable), runnable);60 dispatchIfNotActive(Dispatcher.class.cast(joinable), runnable); 61 61 return; 62 62 } … … 72 72 } 73 73 } 74 75 74 76 75 public static void drop(final Joinable joinable, final JoinableParent owner) { … … 106 105 public static void wait(Object object, long millis) { 107 106 try { 108 object.wait(millis); 107 synchronized (object) { 108 object.wait(millis); 109 } 109 110 } catch (InterruptedException e) { 110 111 } … … 112 113 113 114 public static void joinAbsolutely(Joinable joinable) { 114 log. info("joining absolutely " + joinable);115 log.debug("joining absolutely " + joinable); 115 116 while (true) { 116 117 try { … … 120 121 // throw new FramsticksException().msg("failed to join").arg("dispatcher", dispatcher).cause(e); 121 122 } 122 log. info("waiting for " + joinable);123 sleep(500);123 log.debug("waiting for " + joinable); 124 wait(joinable, 500); 124 125 } 126 } 127 128 public interface Query<T> { 129 T get(); 130 } 131 132 public static class QueryRunner<T, C> extends RunAt<C> { 133 protected final Query<T> query; 134 T result; 135 boolean ready = false; 136 137 /** 138 * @param query 139 */ 140 public QueryRunner(Query<T> query) { 141 this.query = query; 142 } 143 144 @Override 145 public void run() { 146 result = query.get(); 147 synchronized (this) { 148 ready = true; 149 this.notifyAll(); 150 } 151 } 152 153 public T get() { 154 synchronized (this) { 155 while (!ready) { 156 try { 157 this.wait(100); 158 } catch (InterruptedException e) { 159 } 160 } 161 } 162 return result; 163 } 164 } 165 166 public static <T, C> T get(Dispatcher<C> dispatcher, Query<T> query) { 167 QueryRunner<T, C> runner = new QueryRunner<T, C>(query); 168 dispatcher.dispatch(runner); 169 return runner.get(); 125 170 } 126 171 127 172 128 173 129 130 174 } -
java/main/src/main/java/com/framsticks/util/dispatching/RunAt.java
r85 r90 7 7 8 8 public RunAt(Dispatcher<? super C> dispatcher) { 9 dispatcher. invokeLater(this);9 dispatcher.dispatch(this); 10 10 } 11 11 } -
java/main/src/main/java/com/framsticks/util/dispatching/Thread.java
r88 r90 6 6 import java.util.ListIterator; 7 7 8 import javax.annotation.OverridingMethodsMustInvokeSuper;9 8 10 9 import com.framsticks.params.annotations.ParamAnnotation; … … 31 30 } 32 31 33 @OverridingMethodsMustInvokeSuper34 protected void firstTask() {35 }36 32 37 33 public Thread(java.lang.Thread thread) { … … 51 47 protected void routine() { 52 48 log.debug("starting thread " + this); 53 firstTask();54 49 while (!java.lang.Thread.interrupted()) { 55 50 Task<? extends C> task; … … 78 73 task.run(); 79 74 } catch (Exception e) { 80 log.error("error in thread: " +e);75 log.error("error in thread: ", e); 81 76 } 82 77 } … … 115 110 116 111 @Override 117 public void invokeLater(final RunAt<? extends C> runnable) {112 public void dispatch(final RunAt<? extends C> runnable) { 118 113 if (!(runnable instanceof Task)) { 119 114 enqueueTask(new Task<C>() { -
java/main/src/main/java/com/framsticks/util/lang/Containers.java
r86 r90 1 1 package com.framsticks.util.lang; 2 2 3 import java.util.ArrayList; 3 4 import java.util.Collection; 4 5 import java.util.Iterator; … … 8 9 import org.apache.commons.collections.functors.NotPredicate; 9 10 import org.apache.commons.collections.iterators.FilterIterator; 11 12 import com.framsticks.util.Builder; 13 import com.framsticks.util.FramsticksException; 10 14 11 15 /** … … 37 41 return new IterableIterator<T>(new FilterIterator(c.iterator(), new NotPredicate(new InstanceofPredicate(type)))); 38 42 } 43 44 public static <T> T getFromList(List<T> list, int number, String name, Object context) { 45 if (number < 0 || number >= list.size()) { 46 throw new FramsticksException().msg("invalid " + name + " number").arg("number", number).arg("in", context); 47 } 48 return list.get(number); 49 } 50 51 public static <T> List<T> build(List<? extends Builder<T>> builders) { 52 List<T> result = new ArrayList<T>(); 53 for (Builder<T> builder : builders) { 54 result.add(builder.finish()); 55 } 56 return result; 57 } 39 58 } -
java/main/src/main/resources/configs/framsticks.xml
r88 r90 3 3 <import class="com.framsticks.gui.Browser" /> 4 4 <import class="com.framsticks.remote.RemoteInstance" /> 5 <import class="com.framsticks.model.ModelPackage" /> 6 <import class="com.framsticks.model.ModelBuilder" /> 7 <import class="com.framsticks.model.f0.SchemaBuilder" /> 8 <import class="com.framsticks.core.ObjectInstance" /> 9 <import class="com.framsticks.test.TestClass" /> 5 10 <Browser> 6 <RemoteInstance name="localhost:9009" address="localhost:9009" /> 11 <RemoteInstance name="localhost:9009" address="localhost:9009"> 12 <ModelPackage /> 13 </RemoteInstance> 14 <ObjectInstance name="model"> 15 <ModelBuilder resource="/examples/f0_example.txt" /> 16 </ObjectInstance> 17 <ObjectInstance name="f0schema"> 18 <SchemaBuilder /> 19 </ObjectInstance> 20 <ObjectInstance name="test"> 21 <TestClass /> 22 </ObjectInstance> 7 23 </Browser> 8 <!-- <Browser name="other"> -->9 <!-- <RemoteInstance name="localhost:9009" address="localhost:9009" /> -->10 <!-- </Browser> -->11 24 </Framsticks> -
java/main/src/main/resources/configs/log4j.properties
r88 r90 29 29 30 30 log4j.logger.com.framsticks=INFO 31 # log4j.logger.com.framsticks.core.ObjectInstance=DEBUG 32 # log4j.logger.com.framsticks.core.Instance=DEBUG 33 log4j.logger.com.framsticks.gui.controls.ProcedureControl=DEBUG 34 # log4j.logger.com.framsticks.params.ReflectionAccess=TRACE 31 35 # log4j.logger.com.framsticks.util.dispatching.Dispatching=DEBUG 32 36 # log4j.logger.com.framsticks.util.dispatching.AbstractJoinable=DEBUG -
java/main/src/main/resources/examples/f0_example.txt
r78 r90 12 12 n:j=0,d=@:p=0.25 13 13 n:p=1,d=Nu 14 #c:0,215 #c:0,2,5.416 #c:1,017 #c:2,0,3.418 #c:0,1,4.519 #c:1,0,5.620 #c:2,114 c:0,2 15 c:0,2,5.4 16 c:1,0 17 c:2,0,3.4 18 c:0,1,4.5 19 c:1,0,5.6 20 c:2,1 -
java/main/src/main/resources/parsers/f0def.xml
r78 r90 11 11 <PROP ID="joints" NAME="joints" GROUP="2" FLAGS="0" TYPE="l Joint" /> 12 12 <PROP ID="neurodefs" NAME="neurodefs" GROUP="2" FLAGS="0" TYPE="l NeuroDef" /> 13 <PROP ID="neuroconns" NAME="neuroconns" GROUP="2" FLAGS="0" TYPE="l NeuroConn" /> 13 14 </CLASS> 14 15 … … 158 159 <NEUROPROP ID="noDim" NAME="number of degrees of freedom" TYPE="d" MIN="" MAX="" DEF="" /> 159 160 <NEUROPROP ID="params" NAME="parameters" TYPE="s" MIN="" MAX="" DEF="" /> 160 </NEUROCLASS> 161 </NEUROCLASS> 161 162 162 163 <NEUROCLASS ID="Sti" NAME="Sticky [EXPERIMENTAL!]" DESCRIPTION="" INPUTS="1" OUTPUT="0" LOCATION="1" VISUALHINTS="16" > -
java/main/src/test/java/com/framsticks/gui/BrowserTest.java
r88 r90 1 1 package com.framsticks.gui; 2 2 3 import javax.swing.JFrame; 3 import static org.fest.assertions.Assertions.assertThat; 4 import static org.fest.swing.edt.GuiActionRunner.execute; 5 4 6 5 7 import org.apache.log4j.Logger; 6 import org.fest.swing.edt.FailOnThreadViolationRepaintManager;7 import org.fest.swing.edt.GuiActionRunner;8 import org.fest.swing.edt.GuiQuery;9 8 import org.fest.swing.edt.GuiTask; 10 import org.fest.swing.fixture.FrameFixture; 11 import org.fest.swing.fixture.JTreeFixture; 12 // import static org.fest.swing.edt.GuiActionRunner.*; 13 import org.testng.annotations.*; 9 import org.testng.annotations.Test; 14 10 11 import com.framsticks.model.ModelPackage; 15 12 import com.framsticks.remote.RemoteInstance; 16 import com.framsticks.test.TestConfiguration;17 import com.framsticks.util.dispatching.Dispatching;18 import com.framsticks.util.dispatching.JoinableMonitor;19 // import com.framsticks.util.dispatching.Dispatching;20 13 21 import static org.fest.assertions.Assertions.*; 22 import static org.fest.swing.core.BasicRobot.robotWithNewAwtHierarchy; 23 import static org.fest.swing.edt.GuiActionRunner.*; 24 import org.fest.swing.core.Robot; 14 public class BrowserTest extends BrowserBaseTest { 25 15 26 public class BrowserTest extends TestConfiguration {27 16 private static final Logger log = Logger.getLogger(BrowserTest.class); 28 17 29 JoinableMonitor monitor; 30 Browser browser; 31 Robot robot; 32 FrameFixture frame; 18 RemoteInstance localhost; 33 19 34 @BeforeClass 35 public void setUp() { 36 FailOnThreadViolationRepaintManager.install(); 37 assertThat(executeInEDT()).isTrue(); 20 @Override 21 protected void configureBrowser() { 22 browser = new Browser(); 38 23 39 robot = robotWithNewAwtHierarchy(); 40 41 browser = new Browser(); 42 monitor = new JoinableMonitor(browser); 43 44 RemoteInstance localhost = new RemoteInstance(); 24 localhost = new RemoteInstance(); 45 25 localhost.setName("localhost"); 46 26 localhost.setAddress("localhost:9009"); 27 localhost.usePackage(new ModelPackage()); 47 28 48 29 browser.addInstance(localhost); 49 30 50 monitor.use();51 // robot.waitForIdle();52 frame = new FrameFixture(robot,53 GuiActionRunner.execute(new GuiQuery<JFrame>() {54 @Override55 protected JFrame executeInEDT() throws Throwable {56 return browser.getMainFrame().getSwing();57 }58 }));59 60 log.info("frame fixture done");61 // frame.show();62 // log.info("frame fixture shown");63 31 } 64 32 65 public void clickAndExpandPath(JTreeFixture tree, String path) { 66 tree.clickPath(path).expandPath(path); 67 robot.waitForIdle(); 68 } 69 70 @Test 33 @Test(timeOut = 10000) 71 34 public void testShow() { 72 35 log.info("testing"); 73 JTreeFixture tree = frame.tree("tree");74 36 tree.clickRow(0).expandRow(0); 75 37 robot.waitForIdle(); … … 81 43 @Override 82 44 protected void executeInEDT() throws Throwable { 83 assertThat(frame.panel("o Simulator").component().isVisible()) 84 .isTrue(); 45 assertThat(frame.panel("Simulator").component().isVisible()).isTrue(); 85 46 } 86 47 }); 87 48 88 clickAndExpandPath(tree, "localhost/simulator/genepools"); 89 clickAndExpandPath(tree, "localhost/simulator/genepools/groups"); 90 clickAndExpandPath(tree, "localhost/simulator/genepools/groups/Genotypes"); 49 clickAndExpandPath("localhost/simulator/genepools"); 50 clickAndExpandPath("localhost/simulator/genepools/groups"); 51 clickAndExpandPath("localhost/simulator/genepools/groups/Genotypes"); 52 } 91 53 92 54 93 55 94 95 96 97 // tree.clickPath("localhost/simulator/genepools/groups/Genotypes/genotypes");98 // robot.waitForIdle();99 // sleep(2);100 // tree.expandPath("localhost/simulator/genepools/groups/Genotypes/genotypes");101 102 // tree.expandRow(tree.component().getLeadSelectionRow() + 1);103 // robot.waitForIdle();104 105 // sleep(2);106 }107 108 109 @AfterClass110 public void tearDown() {111 log.info("before close");112 113 monitor.drop();114 115 Dispatching.joinAbsolutely(browser);116 // frame.cleanUp();117 // log.info("before close");118 // browser.interrupt();119 120 // try {121 // // frame.close();122 // } catch (Throwable t) {123 // log.error("caught ", t);124 // }125 // log.info("after close");126 // // frame.close();127 // // frame.cleanUp();128 129 130 131 // Dispatching.join(browser);132 }133 134 56 } -
java/main/src/test/java/com/framsticks/model/f0/SchemaTest.java
r88 r90 3 3 import org.testng.annotations.*; 4 4 5 import com.framsticks.params.FramsClass; 5 6 import com.framsticks.params.types.FloatParam; 6 7 import com.framsticks.test.TestConfiguration; … … 15 16 @Test 16 17 public void loadSchemaWithXmlLoader() { 17 Schema schema = Schema.load(Schema.getDefaultDefinitionAsStream());18 Schema schema = new SchemaBuilder().stream(Schema.getDefaultDefinitionAsStream()).finish(); 18 19 assertThat(schema.getFramsClasses().size()).isEqualTo(5); 19 20 assertThat(schema.getNeuroClasses().size()).isEqualTo(21); … … 23 24 assertThat(schema.getNeuroClass("VEye").getParamEntry("p", FloatParam.class).getMax(Double.class)).isEqualTo(10.0, delta(0.0)); 24 25 25 assertThat(schema.getFramsClass("p").getParamEntry("as", FloatParam.class).getDef(Double.class)).isEqualTo(0.25, delta(0.0)); 26 FramsClass partClass = schema.getFramsClass("p"); 27 assertThat(partClass).isNotNull(); 28 assertThat(partClass.getParamEntry("as", FloatParam.class).getDef(Double.class)).isEqualTo(0.25, delta(0.0)); 29 assertThat(partClass.getGroupCount()).isEqualTo(3); 30 assertThat(partClass.getGroup(1).getName()).isEqualTo("Other properties"); 31 assertThat(partClass.getGroup(1).getCount()).isEqualTo(7); 32 assertThat(partClass.getGroup(1).getParam(2)).isEqualTo(partClass.getParam("dn")); 26 33 } 27 34 -
java/main/src/test/java/com/framsticks/parsers/F0ParserTest.java
r88 r90 4 4 5 5 import com.framsticks.model.*; 6 import com.framsticks.model.Package;7 6 import com.framsticks.model.f0.Schema; 7 import com.framsticks.model.f0.SchemaBuilder; 8 8 import com.framsticks.params.*; 9 9 import com.framsticks.params.types.FloatParam; … … 25 25 private Schema schema; 26 26 private List<AccessInterface> accesses; 27 private List< Object> objects;27 private List<ModelComponent> components; 28 28 private Model model; 29 29 30 30 @BeforeClass 31 31 public void setUp() throws Exception { 32 schema = Schema.load(Schema.getDefaultDefinitionAsStream()); 33 Package.register(schema.getRegistry()); 32 schema = new SchemaBuilder().stream(Schema.getDefaultDefinitionAsStream()).finish(); 34 33 } 35 34 36 35 @Test 36 public void checkFramsClasses() { 37 FramsClass modelClass = schema.getRegistry().getFramsClass("Model"); 38 assertThat(modelClass).isNotNull(); 39 assertThat(modelClass.getParam("se")).isInstanceOf(FloatParam.class); 40 // assertThat(modelClass.getParam("energ0")).isInstanceOf(FloatParam.class); 41 assertThat(modelClass.getParam("Vstyle")).isInstanceOf(StringParam.class); 42 assertThat(modelClass.getParamCount()).isEqualTo(6); 43 // assertThat(); 44 45 } 46 47 @Test(dependsOnMethods = "checkFramsClasses") 37 48 public void primitiveParam() { 38 49 FramsClass joint = schema.getFramsClass("j"); … … 43 54 assertThat(dx.getMin(Double.class)).isEqualTo(-2.0, delta(0.0)); 44 55 45 assertThat(schema.getRegistry().get InfoFromCache("n").getParamEntry("d", StringParam.class).getDef(String.class)).isEqualTo("N");56 assertThat(schema.getRegistry().getFramsClass("n").getParamEntry("d", StringParam.class).getDef(String.class)).isEqualTo("N"); 46 57 } 47 58 48 @Test 59 @Test(dependsOnMethods = "primitiveParam") 49 60 public void readF0() throws IOException, ParseException { 50 61 assertThat(schema.getFramsClass("p")).isInstanceOf(FramsClass.class); 51 assertThat(schema.getRegistry().get InfoFromCache("p").getParamEntry("as", FloatParam.class).getDef(Double.class)).isEqualTo(0.25, delta(0.0));62 assertThat(schema.getRegistry().getFramsClass("p").getParamEntry("as", FloatParam.class).getDef(Double.class)).isEqualTo(0.25, delta(0.0)); 52 63 53 64 accesses = new F0Parser(schema, F0ParserTest.class.getResourceAsStream("/parsers/f0_example.txt")).parse(); 54 65 55 assertThat(accesses.size()).isEqualTo(1 2);66 assertThat(accesses.size()).isEqualTo(19); 56 67 assertThat(accesses.get(0).getSelected()).isInstanceOf(Model.class); 57 68 assertThat(accesses.get(5).get("i", String.class)).isEqualTo("1,2,3,\"dsadsa,,,,"); … … 62 73 @Test(dependsOnMethods = {"readF0"}) 63 74 public void stripAccessInterface() { 64 objects = Util.stripAccessInterface(accesses);75 components = Util.stripAccessInterface(accesses, ModelComponent.class); 65 76 66 assertThat(objects.get(1)).isInstanceOf(Part.class); 67 assertThat(objects.get(4)).isInstanceOf(Joint.class); 68 assertThat(objects.get(6)).isInstanceOf(NeuroDef.class); 77 assertThat(components.get(1)).isInstanceOf(Part.class); 78 assertThat(components.get(4)).isInstanceOf(Joint.class); 79 assertThat(components.get(6)).isInstanceOf(NeuroDefinition.class); 80 assertThat(components.get(12)).isInstanceOf(NeuroConnection.class); 69 81 } 70 82 71 83 @Test(dependsOnMethods = {"stripAccessInterface"}) 72 84 public void buildModel() { 73 model = Model.build(objects);85 model = new ModelBuilder().addComponents(components).finish(); 74 86 75 87 assertThat(model.getParts().size()).isEqualTo(3); 76 assertThat(model.getNeuroDef s().size()).isEqualTo(6);88 assertThat(model.getNeuroDefinitions().size()).isEqualTo(6); 77 89 assertThat(model.getJoints().size()).isEqualTo(2); 78 79 assertThat(model.getJoints().get(0).part1).isEqualTo(0); 80 assertThat(model.getJoints().get(0).part2).isEqualTo(1); 81 assertThat(model.getNeuroDefs().size()).isEqualTo(6); 82 assertThat(model.getNeuroDefs().get(0).part).isEqualTo(1); 83 assertThat(model.getNeuroDefs().get(0).joint).isEqualTo(-1); 84 assertThat(model.getNeuroDefs().get(1).details).isEqualTo("|:p=0.25,r=1"); 85 assertThat(model.getNeuroDefs().get(3).details).isEqualTo("N"); 86 assertThat(model.getNeuroDefs().get(4).part).isEqualTo(-1); 90 assertThat(model.getNeuroConnections().size()).isEqualTo(7); 87 91 88 92 assertThat(model.getParts().get(1).getPosition().x).isEqualTo(2.0, delta(0.0)); 89 93 assertThat(model.getParts().get(2).getPosition().sub(new Point3d(2.27236, -0.0792596, -0.958924)).length()).isLessThan(0.0001); 90 94 assertThat(model.getParts().get(2).getOrientation().y.sub(new Point3d(0.870277, -0.404792, 0.280644)).length()).isLessThan(0.0001); 95 96 assertThat(model.getJoints().get(0).part1).isEqualTo(0); 97 assertThat(model.getJoints().get(0).part2).isEqualTo(1); 98 99 assertThat(model.getNeuroDefinitions().get(0).part).isEqualTo(1); 100 assertThat(model.getNeuroDefinitions().get(0).joint).isEqualTo(-1); 101 assertThat(model.getNeuroDefinitions().get(1).details).isEqualTo("|:p=0.25,r=1"); 102 assertThat(model.getNeuroDefinitions().get(3).details).isEqualTo("N"); 103 assertThat(model.getNeuroDefinitions().get(4).part).isEqualTo(-1); 104 105 assertThat(model.getNeuroConnections().get(2).connectedNeuro).isEqualTo(0); 106 assertThat(model.getNeuroConnections().get(5).weight).isEqualTo(5.6, delta(0.0)); 107 91 108 } 92 109 … … 94 111 public void print() throws Exception { 95 112 ListSink sink = new ListSink(); 96 97 113 98 114 new F0Writer(schema, model, sink).write(); … … 110 126 "n:j=0,d=@:p=0.25", 111 127 "n:p=1,d=Nu", 128 "c:0,2", 129 "c:0,2,5.4", 130 "c:1,0", 131 "c:2,0,3.4", 132 "c:0,1,4.5", 133 "c:1,0,5.6", 134 "c:2,1", 112 135 "m:" 113 136 ); -
java/main/src/test/java/com/framsticks/running/ExternalProcessTest.java
r88 r90 10 10 11 11 import com.framsticks.test.TestConfiguration; 12 import com.framsticks.util.dispatching. JoinableMonitor;12 import com.framsticks.util.dispatching.Monitor; 13 13 14 14 import static org.fest.assertions.Assertions.*; … … 17 17 public class ExternalProcessTest extends TestConfiguration { 18 18 19 @Test 19 @Test(timeOut = 1000) 20 20 public void runBash() throws InterruptedException { 21 21 final ExternalProcess process = new ExternalProcess(); … … 31 31 } 32 32 }); 33 JoinableMonitor monitor = new JoinableMonitor(process);33 Monitor monitor = new Monitor(process); 34 34 monitor.use(); 35 35 -
java/main/src/test/java/com/framsticks/running/FramsServerTest.java
r88 r90 4 4 5 5 import com.framsticks.core.Framsticks; 6 import com.framsticks.remote.RemoteInstance;7 6 import com.framsticks.test.TestConfiguration; 7 import com.framsticks.util.dispatching.Monitor; 8 8 9 9 import static org.fest.assertions.Assertions.*; … … 12 12 public class FramsServerTest extends TestConfiguration { 13 13 14 @Test 14 @Test(timeOut = 3000) 15 15 public void runServer() { 16 16 Framsticks framsticks = Framsticks.loadConfiguration(FramsServerTest.class.getResourceAsStream("/configs/FramsServerTest.xml")); 17 17 18 assertThat(framsticks.getObservables().size()).isEqualTo( 2);18 assertThat(framsticks.getObservables().size()).isEqualTo(1); 19 19 assertThat(framsticks.getObservables().get("frams")).isInstanceOf(FramsServer.class); 20 assertThat(framsticks.getObservables().get("remote")).isInstanceOf(RemoteInstance.class); 20 // assertThat(framsticks.getObservables().get("remote")).isInstanceOf(RemoteInstance.class); 21 22 new Monitor(framsticks).use().useFor(1.0).drop().join(); 23 24 // monitor.use(); 25 26 // Dispatching.sleep(1000); 27 28 // // monitor.waitFor(); 29 // monitor.drop(); 30 // monitor.join(); 21 31 22 32 // framsticks.start(); -
java/main/src/test/java/com/framsticks/test/TestConfiguration.java
r88 r90 1 1 package com.framsticks.test; 2 3 import java.util.LinkedList; 4 import java.util.List; 2 5 3 6 import org.apache.log4j.Logger; … … 5 8 import org.testng.annotations.*; 6 9 10 import com.framsticks.util.dispatching.Dispatcher; 11 import com.framsticks.util.dispatching.RunAt; 12 7 13 public class TestConfiguration { 8 14 9 private static final Logger log = 10 Logger.getLogger(TestConfiguration.class); 15 private static final Logger log = Logger.getLogger(TestConfiguration.class); 11 16 12 17 @BeforeClass … … 16 21 } 17 22 23 private final List<AsyncAssert<?>> asyncAssertions = new LinkedList<AsyncAssert<?>>(); 24 25 public class AsyncAssert<C> extends RunAt<C> { 26 final RunAt<? extends C> runnable; 27 boolean done = false; 28 AssertionError assertion; 29 30 /** 31 * @param runnable 32 */ 33 public AsyncAssert(RunAt<? extends C> runnable) { 34 this.runnable = runnable; 35 } 36 37 @Override 38 public void run() { 39 try { 40 runnable.run(); 41 } catch (AssertionError e) { 42 assertion = e; 43 } 44 synchronized (this) { 45 done = true; 46 this.notifyAll(); 47 } 48 } 49 } 50 51 public <C> void assertDispatch(Dispatcher<C> dispatcher, RunAt<? extends C> runnable) { 52 AsyncAssert<C> assertion = new AsyncAssert<C>(runnable); 53 synchronized (asyncAssertions) { 54 asyncAssertions.add(assertion); 55 } 56 dispatcher.dispatch(assertion); 57 } 58 59 @BeforeMethod 60 public void clearAsyncAsserts() { 61 synchronized (asyncAssertions) { 62 asyncAssertions.clear(); 63 } 64 } 65 66 @AfterMethod(timeOut = 5000) 67 public void waitForAsyncAsserts() { 68 while (true) { 69 AsyncAssert<?> assertion; 70 synchronized (asyncAssertions) { 71 if (asyncAssertions.isEmpty()) { 72 return; 73 } 74 assertion = asyncAssertions.get(0); 75 asyncAssertions.remove(0); 76 } 77 synchronized (assertion) { 78 while (!assertion.done) { 79 try { 80 assertion.wait(); 81 } catch (InterruptedException ignored) { 82 } 83 } 84 if (assertion.assertion != null) { 85 throw assertion.assertion; 86 } 87 } 88 } 89 } 18 90 } -
java/main/src/test/resources/configs/FramsServerTest.xml
r88 r90 3 3 <import class="com.framsticks.running.FramsServer" /> 4 4 <import class="com.framsticks.remote.RemoteInstance" /> 5 <FramsServer name="frams" port="9008" /> 6 <RemoteInstance name="remote" address="localhost:9008" /> 5 <import class="com.framsticks.running.LoggingOutputListener" /> 6 <FramsServer name="frams" port="9008"> 7 <LoggingOutputListener /> 8 </FramsServer> 9 <!-- <RemoteInstance name="remote" address="localhost:9008" /> --> 7 10 </Framsticks> -
java/main/src/test/resources/info/Part.info
r86 r90 3 3 4 4 prop: 5 id:x 5 6 name:position.x 6 id:x7 7 type:f 8 8 flags:1024 9 9 10 10 prop: 11 id:y 11 12 name:position.y 12 id:y13 13 type:f 14 14 flags:1024 15 15 16 16 prop: 17 id:z 17 18 name:position.z 18 id:z19 19 type:f 20 20 flags:1024 21 21 22 22 prop: 23 id:m 23 24 name:mass 24 id:m 25 type:f 26 flags:0 25 type:f 0.1 999.0 1.0 27 26 28 27 prop: 28 id:s 29 29 name:size 30 id:s 31 type:f 32 flags:0 30 type:f 0.1 10.0 1.0 33 31 34 32 prop: 33 id:dn 35 34 name:density 36 id:dn 37 type:f 38 flags:0 35 type:f 0.2 5.0 1.0 39 36 40 37 prop: 38 id:fr 41 39 name:friction 42 id:fr 43 type:f 44 flags:0 40 type:f 0.0 4.0 0.4 45 41 46 42 prop: 43 id:ing 47 44 name:ingestion 48 id:ing 49 type:f 50 flags:0 45 type:f 0.0 1.0 0.25 51 46 52 47 prop: 48 id:as 53 49 name:assimilation 54 id:as 55 type:f 56 flags:0 50 type:f 0.0 1.0 0.25 57 51 58 52 prop: 53 id:rx 59 54 name:rot.x 60 id:rx61 55 type:f 62 flags:063 56 64 57 prop: 58 id:ry 65 59 name:rot.y 66 id:ry67 60 type:f 68 61 flags:1024 69 62 70 63 prop: 64 id:rz 71 65 name:rot.z 72 id:rz73 66 type:f 74 67 flags:1024 75 68 76 69 prop: 70 id:i 77 71 name:info 78 id:i79 72 type:s 80 flags:081 73 82 74 prop: 75 id:Vstyle 83 76 name:vis_style 84 id:Vstyle 85 type:s 86 flags:0 77 type:s 0 -1 part 87 78 88 79 prop: 80 id:vs 89 81 name:visual thickness 90 id:vs 91 type:f 92 flags:0 82 type:f 0.05 0.7 0.3 93 83 94 84 prop: 85 id:vr 95 86 name:red component 96 id:vr 97 type:f 98 flags:0 87 type:f 0.0 1.0 0.5 99 88 100 89 prop: 90 id:vg 101 91 name:green component 102 id:vg 103 type:f 92 type:f 0.0 1.0 0.5 104 93 flags:1024 105 94 106 95 prop: 96 id:vb 107 97 name:blue component 108 id:vb 109 type:f 98 type:f 0.0 1.0 0.5 110 99 flags:1024 111 100 -
java/main/src/test/resources/log4j.properties
r88 r90 27 27 28 28 log4j.logger.com.framsticks=INFO 29 # log4j.logger.com.framsticks.util.dispatching.JoinableMonitor=DEBUG 30 # log4j.logger.com.framsticks.util.dispatching.Dispatching=DEBUG 29 # log4j.logger.com.framsticks.parsers.F0Writer=TRACE 30 # log4j.logger.com.framsticks.core.ObjectInstance=DEBUG 31 # log4j.logger.com.framsticks.core.Instance=DEBUG 32 # log4j.logger.com.framsticks.running=DEBUG 31 33 # log4j.logger.com.framsticks.parsers.XmlLoader=DEBUG 32 34 # log4j.logger.com.framsticks.parsers.MultiParamLoader=TRACE -
java/main/src/test/resources/parsers/f0_example.txt
r78 r90 12 12 n:j=0,d=@:p=0.25 13 13 n:p=1,d=Nu 14 #c:0,215 #c:0,2,5.416 #c:1,017 #c:2,0,3.418 #c:0,1,4.519 #c:1,0,5.620 #c:2,114 c:0,2 15 c:0,2,5.4 16 c:1,0 17 c:2,0,3.4 18 c:0,1,4.5 19 c:1,0,5.6 20 c:2,1
Note: See TracChangeset
for help on using the changeset viewer.