Changeset 103
- Timestamp:
- 07/18/13 23:52:25 (11 years ago)
- Location:
- java/main/src
- Files:
-
- 7 added
- 1 deleted
- 43 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/communication/ClientSideManagedConnection.java
r102 r103 80 80 setDescription("client connection"); 81 81 protocolVersion = -1; 82 requestedFeatures.add("request_id"); 83 requestedFeatures.add("call_empty_result"); 84 requestedFeatures.add("needfile_id"); 82 85 } 83 86 … … 192 195 } 193 196 197 private void sendNextUseRequest(final Iterator<String> featuresIterator, final Future<Void> future) { 198 if (!featuresIterator.hasNext()) { 199 future.pass(null); 200 return; 201 } 202 final String feature = featuresIterator.next(); 203 204 send(new UseRequest().feature(feature), new ClientSideResponseFuture(future) { 205 206 @Override 207 protected void processOk(Response response) { 208 if (feature.equals("request_id")) { 209 requestIdEnabled = true; 210 } 211 sendNextUseRequest(featuresIterator, future); 212 } 213 }); 214 } 215 194 216 private void sendQueryVersion(final int version, final Future<Void> future) { 195 217 send(new VersionRequest().version(version), new ClientSideResponseFuture(future) { … … 202 224 return; 203 225 } 204 send(new UseRequest().feature("request_id"), new ClientSideResponseFuture(future) { 205 206 @Override 207 protected void processOk(Response response) { 208 requestIdEnabled = true; 209 future.pass(null); 210 } 211 }); 226 sendNextUseRequest(requestedFeatures.iterator(), future); 212 227 213 228 } … … 323 338 324 339 protected void send(final File result) { 325 log. info("sending file: " + result);340 log.debug("sending file: " + result); 326 341 needFiles.clear(id); 327 342 sendFile(null, result, id, ClientSideManagedConnection.this); 328 329 343 } 330 344 -
java/main/src/main/java/com/framsticks/communication/ManagedConnection.java
r97 r103 1 1 package com.framsticks.communication; 2 3 import java.util.HashSet; 4 import java.util.Set; 2 5 3 6 import com.framsticks.util.lang.Pair; … … 8 11 9 12 protected int protocolVersion = -1; 13 14 protected Set<String> requestedFeatures = new HashSet<String>(); 10 15 11 16 public ManagedConnection() { -
java/main/src/main/java/com/framsticks/communication/ServerSideManagedConnection.java
r102 r103 53 53 if (feature.equals("request_id")) { 54 54 requestIdEnabled = true; 55 responseCallback.pass(new Response(true, null, null)); 56 return; 57 } 58 if (feature.equals("call_empty_result")) { 59 responseCallback.pass(new Response(true, null, null)); 60 return; 61 } 62 if (feature.equals("needfile_id")) { 55 63 responseCallback.pass(new Response(true, null, null)); 56 64 return; -
java/main/src/main/java/com/framsticks/core/AbstractTree.java
r102 r103 102 102 public AbstractTree() { 103 103 setName("tree"); 104 105 registry.registerAndBuild(Result.class); 106 registry.registerAndBuild(ValueChange.class); 107 registry.registerAndBuild(ListChange.class); 108 registry.registerAndBuild(Message.class); 104 109 } 105 110 -
java/main/src/main/java/com/framsticks/core/LocalTree.java
r102 r103 21 21 import com.framsticks.util.dispatching.Joinable; 22 22 import com.framsticks.util.dispatching.JoinableState; 23 import com.framsticks.util.lang.Casting; 23 24 24 25 import static com.framsticks.core.TreeOperations.*; … … 90 91 91 92 @Override 92 public void call(Path path, ProcedureParam param, Object[] arguments, Future<Object> future) {93 public <R> void call(Path path, ProcedureParam param, Object[] arguments, Class<R> resultType, Future<R> future) { 93 94 assert isActive(); 94 95 try { 95 future.pass(bindAccess(path).call(param, arguments)); 96 Object result = bindAccess(path).call(param, arguments); 97 future.pass(Casting.nullOrThrowCast(resultType, result)); 96 98 } catch (FramsticksException e) { 97 99 future.handle(e); -
java/main/src/main/java/com/framsticks/core/Tree.java
r102 r103 40 40 public void get(Path path, Future<Path> future); 41 41 42 public void call(Path path, ProcedureParam param, Object[] arguments, Future<Object> future);42 public <R> void call(Path path, ProcedureParam param, Object[] arguments, Class<R> resultType, Future<R> future); 43 43 44 44 /** -
java/main/src/main/java/com/framsticks/core/TreeOperations.java
r102 r103 236 236 } 237 237 238 public static void call(final Path path, final String procedureName, final Object[] arguments, final Future<Object> future) {238 public static <R> void call(final Path path, final String procedureName, final Object[] arguments, final Class<R> resultType, final Future<R> future) { 239 239 final Tree tree = path.getTree(); 240 240 … … 243 243 protected void runAt() { 244 244 path.assureResolved(); 245 tree.call(path, tree.getRegistry().getFramsClass(path.getTop().getParam()).getParamEntry(procedureName, ProcedureParam.class), arguments, future);246 } 247 }); 248 } 249 250 public static void call(final Path path, final ProcedureParam param, final Object[] arguments, final Future<Object> future) {251 final Tree tree = path.getTree(); 252 253 dispatchIfNotActive(tree, new RunAt<Tree>(future) { 254 @Override 255 protected void runAt() { 256 tree.call(path, param, arguments, future);245 tree.call(path, tree.getRegistry().getFramsClass(path.getTop().getParam()).getParamEntry(procedureName, ProcedureParam.class), arguments, resultType, future); 246 } 247 }); 248 } 249 250 public static <R> void call(final Path path, final ProcedureParam param, final Object[] arguments, final Class<R> resultType, final Future<R> future) { 251 final Tree tree = path.getTree(); 252 253 dispatchIfNotActive(tree, new RunAt<Tree>(future) { 254 @Override 255 protected void runAt() { 256 tree.call(path, param, arguments, resultType, future); 257 257 } 258 258 }); -
java/main/src/main/java/com/framsticks/experiment/Experiment.java
r102 r103 45 45 protected String expdef; 46 46 47 47 48 /** 48 49 * … … 53 54 54 55 Dispatching.dispatchLog(this, log, Level.DEBUG, "first task"); 55 56 57 simulators.addListener(new EventListener<ListChange>() {58 @Override59 public void action(ListChange argument) {60 if (argument.getAction() == ListChange.Action.Add) {61 simulators.fireChildrenChange(argument, ListChange.Action.Modify, "ready");62 }63 }64 });65 66 56 } 67 57 … … 114 104 this.expdef = expdef; 115 105 } 106 116 107 117 108 @ParamAnnotation(id = "simulators_changed") … … 129 120 simulators.add(simulator); 130 121 simulatorAsJoinables.add(simulator); 131 132 133 134 122 simulators.fireChildrenChange(simulator, ListChange.Action.Modify, "ready"); 135 123 136 124 } -
java/main/src/main/java/com/framsticks/experiment/NetLoadSaveLogic.java
r102 r103 5 5 import org.apache.logging.log4j.LogManager; 6 6 7 import com.framsticks.communication.File;8 7 import com.framsticks.core.ListChange; 8 import com.framsticks.core.Message; 9 9 import com.framsticks.params.EventListener; 10 import com.framsticks.params.MessageLogger; 10 11 import com.framsticks.params.annotations.FramsClassAnnotation; 11 12 import com.framsticks.params.annotations.ParamAnnotation; … … 14 15 15 16 @FramsClassAnnotation 16 public abstract class NetLoadSaveLogic extends AbstractExperimentLogic {17 public abstract class NetLoadSaveLogic<NF extends NetFile> extends AbstractExperimentLogic { 17 18 private static final Logger log = LogManager.getLogger(NetLoadSaveLogic.class); 18 19 20 protected String option = "an option"; 19 21 20 protected String option = "an option"; 22 protected final Class<NF> netJavaClass; 23 24 protected final MessageLogger messages = new MessageLogger(NetLoadSaveLogic.class); 21 25 22 26 /** 23 27 * @param experiment 24 28 */ 25 public NetLoadSaveLogic(Experiment parentExperiment ) {29 public NetLoadSaveLogic(Experiment parentExperiment, Class<NF> netJavaClassArg) { 26 30 super(parentExperiment); 31 this.netJavaClass = netJavaClassArg; 27 32 28 33 experiment.addSimulatorsListener(new EventListener<ListChange>() { 29 34 30 35 @Override 31 public void action( ListChange argument) {36 public void action(final ListChange change) { 32 37 assert experiment.isActive(); 38 final Simulator simulator = experiment.getSimulators().get(change.getIdentifier()); 39 log.debug("processing list change: {}", change); 33 40 34 if (argument.hasHint("ready")) { 35 final Simulator simulator = experiment.getSimulators().get(argument.getIdentifier()); 36 log.debug("simulator is ready: {}", simulator); 41 if (change.getAction() == ListChange.Action.Add) { 42 log.debug("registering in {}", simulator); 43 simulator.getRemoteTree().getRegistry().registerAndBuild(netJavaClass); 44 } 37 45 38 netload(simulator, new FutureHandler<File>(simulator) { 46 if (!change.hasHint("stoped")) { 47 issueNetloadIfReady(change, simulator); 48 return; 49 } 39 50 40 @Override 41 protected void result(final File file) { 42 simulator.uploadNet(file, new FutureHandler<Object>(this) { 51 log.debug("issuing netsave to: {}", simulator); 52 simulator.netsave(netJavaClass, new FutureHandler<NF>(simulator) { 43 53 44 @Override 45 protected void result(Object result) { 46 log.debug("netload of {} done", file); 47 } 48 }); 49 } 50 }); 51 } 54 @Override 55 protected void result(NF net) { 56 log.debug("netsave of {} done: {}", simulator, net.getShortDescription()); 57 netsave(simulator, net); 58 issueNetloadIfReady(change, simulator); 59 } 60 }); 52 61 } 53 62 }); 54 63 } 55 64 56 public abstract void netload(Simulator simulator, Future<File> net); 65 protected void issueNetloadIfReady(ListChange change, final Simulator simulator) { 66 if (!change.hasHint("ready")) { 67 return; 68 } 69 log.debug("issuing netload to: {}", simulator); 70 netload(simulator, new FutureHandler<NF>(simulator) { 57 71 58 public abstract void netsave(Simulator simulator, File net); 72 @Override 73 protected void result(final NF net) { 74 if (net == null) { 75 log.debug("no file for upload provided - leaving simulator idle"); 76 return; 77 } 78 79 simulator.netload(net, new FutureHandler<Object>(this) { 80 81 @Override 82 protected void result(Object result) { 83 NetLoadSaveLogic.this.messages.info("netload", "done " + net.getShortDescription()); 84 log.debug("netload of {} done", net.getShortDescription()); 85 simulator.start(); 86 } 87 }); 88 } 89 }); 90 } 91 92 public abstract void netload(Simulator simulator, Future<NF> net); 93 94 public abstract void netsave(Simulator simulator, NF net); 59 95 60 96 /** … … 74 110 } 75 111 112 /** 113 * @return the netJavaClass 114 */ 115 @ParamAnnotation(name = "Java class representing netfile") 116 public String getNetJavaClassName() { 117 return netJavaClass.getCanonicalName(); 118 } 119 120 @ParamAnnotation(id = "messages") 121 public void addMessageListener(EventListener<Message> listener) { 122 messages.add(listener); 123 } 124 125 @ParamAnnotation(id = "messages") 126 public void removeMessageListener(EventListener<Message> listener) { 127 messages.remove(listener); 128 } 129 76 130 } -
java/main/src/main/java/com/framsticks/experiment/Simulator.java
r102 r103 4 4 import com.framsticks.communication.queries.NeedFile; 5 5 import com.framsticks.communication.queries.NeedFileAcceptor; 6 import com.framsticks.core.ListChange; 6 7 import com.framsticks.core.Path; 7 8 import com.framsticks.core.Tree; 8 9 import com.framsticks.core.ValueChange; 10 import com.framsticks.params.AccessOperations; 11 import com.framsticks.params.CastFailure; 9 12 import com.framsticks.params.EventListener; 10 13 import com.framsticks.params.FramsClass; … … 12 15 import com.framsticks.params.annotations.FramsClassAnnotation; 13 16 import com.framsticks.params.annotations.ParamAnnotation; 17 import com.framsticks.params.types.BooleanParam; 14 18 import com.framsticks.params.types.EventParam; 15 19 import com.framsticks.params.types.ProcedureParam; … … 59 63 assert experiment.isActive(); 60 64 61 log. debug("simulator ready {}", this);65 log.info("simulator ready {}", this); 62 66 63 67 runningListener = new EventListener<ValueChange>() { 64 68 @Override 65 69 public void action(ValueChange argument) { 66 log.debug("running state of {} changed: {}", this, argument); 70 try { 71 boolean running = simulatorClass.getParamEntry("running", BooleanParam.class).reassign(argument.value, null).getValue(); 72 log.debug("running state of {} changed: {}", Simulator.this, running); 73 if (!running) { 74 Simulator.this.experiment.simulators.fireChildrenChange(Simulator.this, ListChange.Action.Modify, "ready", "stoped"); 75 } 76 } catch (CastFailure e) { 77 log.error("failure: ", e); 78 } 67 79 } 68 80 }; … … 147 159 @ParamAnnotation(paramType = ProcedureParam.class) 148 160 public void start() { 161 log.debug("starting simulator {}", this); 162 call(simulatorPath, "start", new Object[] {}, Object.class, FutureHandler.doNothing(Object.class, this)); 149 163 } 150 164 151 165 @ParamAnnotation(paramType = ProcedureParam.class) 152 166 public void stop() { 167 log.debug("stoping simulator {}", this); 153 168 } 154 169 … … 156 171 public void abort() { 157 172 assert isActive(); 158 log. debug("explicitly aborting {}", this);173 log.info("explicitly aborting {}", this); 159 174 experiment.removeSimulator(this); 160 175 interruptJoinable(); … … 184 199 protected final AtomicInteger netloadIdCounter = new AtomicInteger(); 185 200 186 public void uploadNet(final File file, final Future<Object> future) {201 public <N> void netload(final N net, final Future<Object> future) { 187 202 final String netloadId = "NetLoadSaveLogic" + netloadIdCounter.getAndIncrement(); 188 203 204 final File file = AccessOperations.convert(File.class, net, getRemoteTree().getRegistry()); 189 205 log.debug("uploading file {} to {} identified by {}", file, simulatorPath, netloadId); 190 206 … … 215 231 simulatorPath.getTree().addNeedFileAcceptor(Integer.MIN_VALUE, acceptor.get()); 216 232 217 call(simulatorPath, getFramsClass(simulatorPath).getParamEntry("netload_id", ProcedureParam.class), new Object[] { netloadId }, new FutureHandler<Object>(future) {233 call(simulatorPath, getFramsClass(simulatorPath).getParamEntry("netload_id", ProcedureParam.class), new Object[] { netloadId }, Object.class, new FutureHandler<Object>(future) { 218 234 219 235 @Override … … 225 241 226 242 } 243 244 public <N> void netsave(Class<N> netJavaClass, final Future<N> futureNet) { 245 call(simulatorPath, getFramsClass(simulatorPath).getParamEntry("netsave", ProcedureParam.class), new Object[] { }, netJavaClass, new FutureHandler<N>(futureNet) { 246 247 @Override 248 protected void result(N net) { 249 log.debug("download of {} done", net); 250 futureNet.pass(net); 251 } 252 }); 253 254 } 227 255 } -
java/main/src/main/java/com/framsticks/experiment/WorkPackage.java
r102 r103 1 1 package com.framsticks.experiment; 2 2 3 public interface WorkPackage { 3 public interface WorkPackage<S extends WorkPackage<S>> extends NetFile { 4 5 public String sumUpTask(); 6 public String sumUpResult(); 7 8 S getRemainder(S result); 4 9 5 10 } -
java/main/src/main/java/com/framsticks/experiment/WorkPackageLogic.java
r102 r103 1 1 package com.framsticks.experiment; 2 3 import java.util.IdentityHashMap; 4 import java.util.LinkedList; 5 import java.util.Map; 2 6 3 7 import org.apache.logging.log4j.Logger; 4 8 import org.apache.logging.log4j.LogManager; 5 9 6 import com.framsticks.communication.File; 7 import com.framsticks.params.ListSource; 10 import com.framsticks.core.Message; 11 import com.framsticks.core.ValueChange; 12 import com.framsticks.params.EventListener; 13 import com.framsticks.params.MessageLogger; 14 import com.framsticks.params.SimplePrimitive; 8 15 import com.framsticks.params.annotations.FramsClassAnnotation; 9 16 import com.framsticks.params.annotations.ParamAnnotation; … … 11 18 import com.framsticks.util.dispatching.FutureHandler; 12 19 13 @FramsClassAnnotation 14 public abstract class WorkPackageLogic<WP extends WorkPackage > extends AbstractExperimentLogic {20 @FramsClassAnnotation(order = {"netLoadSaveLogic", "newestTaskScheduled", "newestResultReceived"}) 21 public abstract class WorkPackageLogic<WP extends WorkPackage<WP>> extends AbstractExperimentLogic { 15 22 16 23 private static final Logger log = LogManager.getLogger(WorkPackageLogic.class); 17 24 18 25 @ParamAnnotation 19 public final NetLoadSaveLogic netLoadSaveLogic; 26 public final NetLoadSaveLogic<WP> netLoadSaveLogic; 27 28 protected final Class<WP> packageJavaClass; 29 30 protected final MessageLogger messages = new MessageLogger(WorkPackageLogic.class); 31 32 protected final Map<Simulator, WP> sentPackages = new IdentityHashMap<>(); 33 protected final LinkedList<WP> queuedPackages = new LinkedList<>(); 34 35 protected final SimplePrimitive<String> newestTaskSent = new SimplePrimitive<>(); 36 protected final SimplePrimitive<String> newestResultReceived = new SimplePrimitive<>(); 37 20 38 21 39 /** 22 40 * @param experiment 23 41 */ 24 public WorkPackageLogic(Experiment experiment ) {42 public WorkPackageLogic(Experiment experiment, Class<WP> packageJavaClass) { 25 43 super(experiment); 44 this.packageJavaClass = packageJavaClass; 26 45 27 netLoadSaveLogic = new NetLoadSaveLogic (experiment) {46 netLoadSaveLogic = new NetLoadSaveLogic<WP>(experiment, packageJavaClass) { 28 47 29 48 @Override 30 public void netload( Simulator simulator, final Future<File> net) {49 public void netload(final Simulator simulator, final Future<WP> netFuture) { 31 50 assert experiment.isActive(); 32 51 log.debug("providing netload file for {}", simulator); 33 52 34 generateNextPackage(new FutureHandler<WP> (net) {53 findNextPackage(new FutureHandler<WP> (netFuture) { 35 54 36 55 @Override 37 protected void result(WP result) { 38 log.debug("sending package: {}", result); 39 File file = new File("test-file", ListSource.createFrom("# a test")); 56 protected void result(WP net) { 57 assert experiment.isActive(); 40 58 41 net.pass(file); 59 if (net == null) { 60 log.debug("no more packages left"); 61 return; 62 } 63 WorkPackageLogic.this.messages.info("netload", net.sumUpTask()); 64 log.debug("sending package: {}", net); 65 newestTaskSent.set(net.sumUpTask()); 66 sentPackages.put(simulator, net); 67 68 netFuture.pass(net); 42 69 } 43 70 }); … … 46 73 47 74 @Override 48 public void netsave(Simulator simulator, File net) { 75 public void netsave(Simulator simulator, WP netResult) { 76 assert experiment.isActive(); 77 78 log.debug("received package from {}: {}", simulator, netResult); 79 WorkPackageLogic.this.messages.info("netsave", netResult.toString()); 80 81 WP netSent = sentPackages.get(simulator); 82 if (netSent == null) { 83 log.error("no task found in {} for received result {}", simulator, netResult); 84 return; 85 } 86 sentPackages.remove(simulator); 87 88 try { 89 WP netRemainder = netSent.getRemainder(netResult); 90 if (netRemainder != null) { 91 log.warn("queueing remainder: {}", netRemainder); 92 queuedPackages.add(netRemainder); 93 } 94 } catch (InvalidWorkPackage e) { 95 log.error("in simulator {}, result {} is in no relation to task {} ({}), rescheduling", simulator, netResult, netSent, e.getShortMessage(new StringBuilder())); 96 queuedPackages.add(netSent); 97 return; 98 } 99 100 returnPackage(netResult); 49 101 // processFile(); 50 102 } … … 53 105 54 106 107 108 protected void findNextPackage(final Future<WP> future) { 109 if (!queuedPackages.isEmpty()) { 110 WP workPackage = queuedPackages.pollFirst(); 111 future.pass(workPackage); 112 return; 113 } 114 115 generateNextPackage(new FutureHandler<WP>(experiment) { 116 117 @Override 118 protected void result(WP result) { 119 if (result == null) { 120 log.info("no more packages left"); 121 future.pass(null); 122 return; 123 } 124 future.pass(result); 125 } 126 }); 127 } 128 55 129 protected abstract void generateNextPackage(Future<WP> future); 130 protected abstract void returnPackage(WP workPackage); 131 132 /** 133 * @return the newestTaskScheduled 134 */ 135 @ParamAnnotation(id = "newest_task_sent") 136 public String getNewestTaskSent() { 137 return newestTaskSent.get(); 138 } 139 140 /** 141 * @return the newestResultReceived 142 */ 143 @ParamAnnotation(id = "newest_result_received") 144 public String getNewestResultReceived() { 145 return newestResultReceived.get(); 146 } 147 148 @ParamAnnotation(id = "newest_task_sent_changed") 149 public void addNewestTaskSentListener(EventListener<ValueChange> listener) { 150 newestTaskSent.addListener(listener); 151 } 152 153 @ParamAnnotation(id = "newest_task_sent_changed") 154 public void removeNewestTaskSentListener(EventListener<ValueChange> listener) { 155 newestTaskSent.removeListener(listener); 156 } 157 158 @ParamAnnotation(id = "newest_result_received_changed") 159 public void addNewestResultReceivedListener(EventListener<ValueChange> listener) { 160 newestResultReceived.addListener(listener); 161 } 162 163 @ParamAnnotation(id = "newest_result_received_changed") 164 public void removeNewestResultReceivedListener(EventListener<ValueChange> listener) { 165 newestResultReceived.removeListener(listener); 166 } 167 168 @ParamAnnotation(id = "messages") 169 public void addMessageListener(EventListener<Message> listener) { 170 messages.add(listener); 171 } 172 173 @ParamAnnotation(id = "messages") 174 public void removeMessageListener(EventListener<Message> listener) { 175 messages.remove(listener); 176 } 177 178 56 179 57 180 } -
java/main/src/main/java/com/framsticks/gui/controls/ProcedureControl.java
r102 r103 73 73 assert path.getTree().isActive(); 74 74 75 path.getTree().call(path, param, arguments, new FutureHandler<Object>(frame) {75 path.getTree().call(path, param, arguments, Object.class, new FutureHandler<Object>(frame) { 76 76 77 77 @Override -
java/main/src/main/java/com/framsticks/gui/tree/TreeModel.java
r101 r103 19 19 import com.framsticks.core.SideNoteKey; 20 20 import com.framsticks.core.TreeOperations; 21 import com.framsticks.core.ValueChange; 21 22 import com.framsticks.gui.Frame; 22 23 import com.framsticks.params.Access; … … 321 322 322 323 323 protected void registerForEventParam(final TreeNode treeNode, Path path, final EventParam eventParam, ValueParam valueParam) {324 protected void registerForEventParam(final TreeNode treeNode, Path path, final EventParam eventParam, final ValueParam valueParam) { 324 325 /** TODO make this listener not bind hold the reference to this TreeNode, maybe hold WeakReference internally */ 325 326 if (valueParam instanceof PrimitiveParam) { … … 328 329 @Override 329 330 public void action(Object argument) { 330 loadPath(treeNode.assurePath(), true); 331 assert treeNode.getTree().isActive(); 332 if (argument instanceof ValueChange) { 333 ValueChange valueChange = (ValueChange) argument; 334 Path path = treeNode.assurePath(); 335 bindAccess(path).set(valueParam, valueChange.value); 336 frame.updatePanelIfIsLeadSelection(path); 337 } else { 338 loadPath(treeNode.assurePath(), true); 339 } 331 340 } 332 341 }); -
java/main/src/main/java/com/framsticks/hosting/Cli.java
r101 r103 28 28 protected final ClientAtServer client; 29 29 30 protected final String address; 31 30 32 @ParamAnnotation 31 33 public final Map<String, CliEvent> events = new TreeMap<>(); … … 37 39 this.client = client; 38 40 this.tree = client.getTree(); 41 this.address = client.connection.getAddress(); 39 42 } 40 43 … … 78 81 } 79 82 83 /** 84 * @return the address 85 */ 86 @ParamAnnotation 87 public String getAddress() { 88 return address; 89 } 90 80 91 } -
java/main/src/main/java/com/framsticks/hosting/CliEvent.java
r100 r103 23 23 24 24 protected EventListener<Object> listener; 25 // protected String name;26 25 27 28 29 /**30 * @param id31 * @param listener32 * @param tree33 * @param name34 * @param path35 * @param param36 */37 26 public CliEvent() { 38 27 } … … 62 51 @ParamAnnotation 63 52 public String getName() { 64 return id;53 return name; 65 54 } 66 55 -
java/main/src/main/java/com/framsticks/hosting/ClientAtServer.java
r102 r103 76 76 .param(Param.build().id(id).name(framsClass.getName()).type("o " + framsClass.getId())) 77 77 .param(Param.build().id("cli").name("CLI").type("o Cli")) 78 .param(Param.build().id("system").name("Operating system").type("s").flags(ParamFlags.READONLY)) 79 .param(Param.build().id("user").name("User name").type("s").flags(ParamFlags.READONLY)) 78 80 .finish(); 79 81 … … 89 91 access.set(id, treeRootObject); 90 92 access.set("cli", cliObject); 93 access.set("system", System.getProperties().getProperty("os.name") + " " + System.getProperties().getProperty("os.version") + " " + System.getProperties().getProperty("os.arch")); 94 access.set("user", System.getProperties().getProperty("user.name")); 91 95 92 96 rootTree.assignRootParam(access.buildParam(new ParamBuilder()).name(rootFramsClassId).finish(CompositeParam.class)); … … 157 161 if (request instanceof CallRequest) { 158 162 final CallRequest callRequest = (CallRequest) request; 159 tree.call(path, access.getFramsClass().getParamEntry(callRequest.getProcedure(), ProcedureParam.class), callRequest.getArguments().toArray(), new FutureHandler<Object>(responseCallback) {163 tree.call(path, access.getFramsClass().getParamEntry(callRequest.getProcedure(), ProcedureParam.class), callRequest.getArguments().toArray(), Object.class, new FutureHandler<Object>(responseCallback) { 160 164 @Override 161 165 protected void result(Object result) { 162 166 ListSink sink = new ListSink(); 163 167 sink.print("Result:").breakLine(); 164 sink.print("value:").print("["); 168 sink.print("value:"); 169 sink.print("["); 165 170 if (result != null) { 166 171 sink.print(result); -
java/main/src/main/java/com/framsticks/params/AccessOperations.java
r102 r103 1 1 package com.framsticks.params; 2 3 import java.util.LinkedList; 4 import java.util.ListIterator; 5 6 import javax.annotation.Nullable; 2 7 3 8 import org.apache.logging.log4j.Logger; 4 9 import org.apache.logging.log4j.LogManager; 5 10 11 import com.framsticks.communication.File; 12 import com.framsticks.params.types.ListParam; 13 import com.framsticks.parsers.MultiParamLoader; 6 14 import com.framsticks.util.FramsticksException; 15 import com.framsticks.util.FramsticksUnsupportedOperationException; 16 import com.framsticks.util.Misc; 17 import com.framsticks.util.UnimplementedException; 18 import com.framsticks.util.lang.Casting; 7 19 import com.framsticks.util.lang.Containers; 8 20 import com.framsticks.util.lang.Holder; 21 import com.framsticks.util.lang.Pair; 9 22 // import com.framsticks.util.lang.Containers; 10 23 … … 14 27 public final class AccessOperations { 15 28 16 private final static Logger log = LogManager.getLogger( SimpleAbstractAccess.class.getName());29 private final static Logger log = LogManager.getLogger(AccessOperations.class); 17 30 18 31 /** … … 75 88 } 76 89 77 public static void save(Access access, Sink sink) { 90 public static <A extends Access> A assureSelected(A access) { 91 if (access.getSelected() == null) { 92 access.select(access.createAccessee()); 93 } 94 return access; 95 } 96 97 public static Access loadAll(@Nullable final Access rootAccess, Source source, final Registry registry) { 98 final MultiParamLoader loader = new MultiParamLoader(); 99 loader.setNewSource(source); 100 final LinkedList<Access> accessesStack = new LinkedList<>(); 101 if (rootAccess != null) { 102 assureSelected(rootAccess); 103 accessesStack.add(rootAccess); 104 } 105 final Holder<Boolean> first = new Holder<>(true); 106 final Holder<Boolean> needAdd = new Holder<>(); 107 final Holder<Access> currentAccess = new Holder<>(); 108 final Holder<Pair<Access, CompositeParam>> parent = new Holder<>(); 109 110 loader.setAccessProvider(new AccessProvider() { 111 @Override 112 public Access getAccess(String name) { 113 if (first.get()) { 114 first.set(false); 115 if (rootAccess != null) { 116 if (name.equals(rootAccess.getId())) { 117 needAdd.set(false); 118 currentAccess.set(rootAccess); 119 return rootAccess; 120 } 121 } else { 122 Access access = registry.createAccess(name); 123 needAdd.set(false); 124 currentAccess.set(access); 125 return access; 126 127 } 128 } 129 130 ListIterator<Access> accessIterator = accessesStack.listIterator(accessesStack.size()); 131 parent.set(null); 132 // log.debug("accesses stack: {}", accessesStack); 133 134 while (accessIterator.hasPrevious()) { 135 Access a = accessIterator.previous(); 136 assert a != null; 137 138 for (CompositeParam p : Containers.filterInstanceof(a.getParams(), CompositeParam.class)) { 139 if (p.getContainedTypeName().equals(name)) { 140 141 if (parent.get() != null) { 142 throw new FramsticksException().msg("ambiguity encountered during loading").arg("name", name); 143 } 144 145 if (p instanceof ListParam) { 146 ListAccess listAccess = Casting.assertCast(ListAccess.class, registry.prepareAccess(p)); 147 listAccess.select(a.get(p, Object.class)); 148 parent.set(new Pair<Access, CompositeParam>(listAccess, listAccess.prepareParamFor(Integer.toString(listAccess.getParamCount())))); 149 150 } else { 151 parent.set(Pair.make(a, p)); 152 } 153 } 154 } 155 156 if (parent.get() == null) { 157 accessIterator.remove(); 158 } 159 } 160 161 if (parent.get() == null) { 162 throw new FramsticksException().msg("failed to find place for loaded object").arg("name", name); 163 } 164 165 currentAccess.set(registry.prepareAccess(parent.get().second)); 166 Object object = parent.get().first.get(parent.get().second, Object.class); 167 if (object != null) { 168 currentAccess.get().select(object); 169 needAdd.set(false); 170 } else { 171 object = currentAccess.get().createAccessee(); 172 currentAccess.get().select(object); 173 needAdd.set(true); 174 } 175 176 return currentAccess.get(); 177 } 178 }); 179 180 loader.addListener(MultiParamLoader.Status.AfterObject, new MultiParamLoader.StatusListener() { 181 @Override 182 public void onStatusChange() { 183 if (needAdd.get()) { 184 parent.get().first.set(parent.get().second, currentAccess.get().getSelected()); 185 } 186 if (currentAccess.get() != rootAccess) { 187 accessesStack.add(currentAccess.get()); 188 } 189 currentAccess.set(null); 190 } 191 }); 192 193 loader.go(); 194 if (accessesStack.isEmpty()) { 195 throw new FramsticksException().msg("failed to load from source").arg("source", source); 196 } 197 return accessesStack.get(0); 198 } 199 200 public static void saveAll(Access access, Sink sink, Registry registry) { 201 if (access instanceof ObjectAccess) { 202 savePrimitives(access, sink); 203 } 204 for (CompositeParam p : filterInstanceof(access.getParams(), CompositeParam.class)) { 205 Object child = access.get(p, Object.class); 206 if (child == null) { 207 continue; 208 } 209 saveAll(registry.prepareAccess(p).select(child), sink, registry); 210 } 211 } 212 213 public static void saveComposites(Access access, Sink sink, Registry registry) { 214 for (CompositeParam p : filterInstanceof(access.getParams(), CompositeParam.class)) { 215 Object child = access.get(p, Object.class); 216 if (child == null) { 217 continue; 218 } 219 savePrimitives(registry.prepareAccess(p).select(child), sink); 220 } 221 } 222 223 public static void savePrimitives(Access access, Sink sink) { 78 224 if (access instanceof ObjectAccess) { 79 225 ObjectAccess objectAccess = (ObjectAccess) access; 80 226 FramsClass framsClass = access.getFramsClass(); 81 227 assert framsClass != null; 82 sink.print(framsClass.getId()).print(":").breakLine(); 228 boolean headerNeeded = true; 229 // sink.print(framsClass.getId()).print(":").breakLine(); 83 230 for (PrimitiveParam<?> p : filterInstanceof(framsClass.getParamEntries(), PrimitiveParam.class)) { 231 84 232 Object value = objectAccess.get(p, Object.class); 85 233 if ((value == null) || value.equals(p.getDef(Object.class))) { 86 234 continue; 87 235 } 236 237 if (headerNeeded) { 238 sink.print(framsClass.getId()).print(":").breakLine(); 239 headerNeeded = false; 240 } 241 88 242 sink.print(p.getId()).print(":"); 89 243 p.save(sink, value); 90 244 sink.breakLine(); 91 245 } 92 sink.breakLine(); 246 if (!headerNeeded) { 247 sink.breakLine(); 248 } 93 249 return; 94 250 } 251 throw new FramsticksException().msg("invalid type of access for primitive save").arg("access", access); 252 } 253 254 public static void save(Access access, Sink sink) { 255 if (access instanceof ObjectAccess) { 256 savePrimitives(access, sink); 257 return; 95 258 } 96 259 if (access instanceof ListAccess) { … … 107 270 } 108 271 272 public static void loadComposites(Access access, Source source, final Registry registry) { 273 if (access instanceof ObjectAccess) { 274 final ObjectAccess objectAccess = (ObjectAccess) access; 275 276 MultiParamLoader loader = new MultiParamLoader(); 277 278 loader.setNewSource(source); 279 280 loader.setAccessProvider(new AccessProvider() { 281 @Override 282 public Access getAccess(String name) { 283 CompositeParam result = null; 284 for (CompositeParam p : filterInstanceof(objectAccess.getParams(), CompositeParam.class)) { 285 if (p.getContainedTypeName().equals(name)) { 286 if (result != null) { 287 throw new FramsticksException().msg("class name is ambiguous in access").arg("name", name).arg("first candidate", result).arg("second candidate", p); 288 } 289 result = p; 290 291 } 292 } 293 if (result == null) { 294 throw new FramsticksException().msg("class name is unknown").arg("name", name).arg("in", objectAccess); 295 } 296 297 return registry.prepareAccess(result).select(objectAccess.get(result, Object.class)); 298 } 299 }); 300 301 302 loader.go(); 303 304 return; 305 } 306 throw new UnimplementedException().msg("unknown access category").arg("access", access); 307 } 308 109 309 public static void load(Access access, Source source) { 110 310 if (!(access instanceof ObjectAccess)) { … … 128 328 } 129 329 } 130 131 330 } 132 331 … … 215 414 } 216 415 416 417 /** 418 * 419 * If both arguments are File, than do nothing; otherwise: 420 * 421 * If from argument is a File: 422 * - if toJavaClass is Object.class, than try read using registry 423 * - otherwise: try use loadComposites 424 * 425 * If to argument is a File: 426 * - use Registry to saveAll 427 * 428 */ 429 public static <T, F> T convert(Class<T> toJavaClass, F from, Registry registry) { 430 if (toJavaClass.equals(from.getClass())) { 431 return toJavaClass.cast(from); 432 } 433 if (from instanceof File) { 434 File file = (File) from; 435 return Casting.throwCast(toJavaClass, loadAll((toJavaClass.equals(Object.class) ? null : registry.createAccess(toJavaClass)), file.getContent(), registry).getSelected()); 436 } 437 if (toJavaClass.equals(File.class)) { 438 ListSink sink = new ListSink(); 439 saveAll(registry.createAccess(from.getClass()).select(from), sink, registry); 440 return Casting.throwCast(toJavaClass, new File("", new ListSource(sink.getOut()))); 441 } 442 443 throw new FramsticksUnsupportedOperationException().msg("conversion").arg("from", from.getClass()).arg("to", toJavaClass); 444 } 445 446 @SuppressWarnings("serial") 447 public static class EqualityException extends FramsticksException { 448 } 449 450 451 public static void assureEquality(Access a, Access b, Registry registry) { 452 try { 453 if (a.getParamCount() != b.getParamCount()) { 454 throw new EqualityException().msg("param count not equal").arg("left", a.getParamCount()).arg("right", b.getParamCount()); 455 } 456 for (ValueParam avp : Containers.filterInstanceof(a.getParams(), ValueParam.class)) { 457 Param bp = b.getParam(avp.getId()); 458 if (bp == null) { 459 throw new EqualityException().msg("param from left not present in right").arg("param", avp); 460 } 461 Misc.checkEquals(avp.getClass(), bp.getClass(), "params type not equals", null); 462 ValueParam bvp = (ValueParam) bp; 463 464 Object oa = a.get(avp, Object.class); 465 Object ob = b.get(avp, Object.class); 466 467 if (avp instanceof CompositeParam) { 468 assureEquality(registry.prepareAccess((CompositeParam) avp).select(oa), registry.prepareAccess((CompositeParam) bvp).select(ob), registry); 469 continue; 470 } 471 Misc.checkEquals(oa, ob, "values not equal", null); 472 } 473 } catch (EqualityException e) { 474 throw e.arg("left", a).arg("right", b); 475 } 476 } 477 478 public static boolean areEqual(Access a, Access b, Registry registry) { 479 try { 480 assureEquality(a, b, registry); 481 return true; 482 } catch (EqualityException e) { 483 } 484 return false; 485 } 486 217 487 } -
java/main/src/main/java/com/framsticks/params/AccessProvider.java
r101 r103 4 4 5 5 public Access getAccess(String name); 6 public void addAccess(Access access);7 6 8 7 } -
java/main/src/main/java/com/framsticks/params/ListSource.java
r101 r103 73 73 b.append(list.get(i)); 74 74 } 75 if (list.size() > print) { 76 b.append(" ..."); 77 } 75 78 return b.toString(); 76 79 } -
java/main/src/main/java/com/framsticks/params/Registry.java
r101 r103 8 8 import com.framsticks.util.DoubleMap; 9 9 import com.framsticks.util.FramsticksException; 10 import com.framsticks.util.FramsticksUnsupportedOperationException;11 10 import com.framsticks.util.lang.Strings; 12 11 … … 166 165 } 167 166 168 @Override 169 public void addAccess(Access access) { 170 throw new FramsticksUnsupportedOperationException().msg("adding accesses to Registry"); 167 public Access bindAccessFor(Object object) { 168 return createAccess(object.getClass()).select(object); 171 169 } 172 170 171 173 172 } -
java/main/src/main/java/com/framsticks/params/SimpleUniqueList.java
r102 r103 74 74 return Collections.unmodifiableMap(children); 75 75 } 76 77 public Map<String, T> getMap() { 78 return children; 79 } 76 80 } -
java/main/src/main/java/com/framsticks/params/UniqueListAccess.java
r101 r103 205 205 @Override 206 206 public <T> int set(int i, T value) { 207 throw new FramsticksUnsupportedOperationException().msg("accesing unique list through index"); 207 if (i != map.size()) { 208 throw new FramsticksUnsupportedOperationException().msg("setting element in unique list through index is available only for addition"); 209 } 210 set(getUidOf(value), value); 211 return 0; 208 212 } 209 213 -
java/main/src/main/java/com/framsticks/parsers/AccessStash.java
r101 r103 16 16 } 17 17 18 @Override 19 public void addAccess(Access access) { 18 public AccessStash add(Access access) { 20 19 /**TODO: by id or by name? rather by id, because from file is always lowercase*/ 21 20 accesses.put(access.getId(), access); 21 return this; 22 22 } 23 23 24 24 25 } -
java/main/src/main/java/com/framsticks/parsers/GenotypeLoader.java
r100 r103 56 56 57 57 Access reflectionParam = new PropertiesAccess(entries); 58 addAccess(reflectionParam);58 setAccessProvider(new AccessStash().add(reflectionParam)); 59 59 addBreakCondition(Status.AfterObject); 60 60 } -
java/main/src/main/java/com/framsticks/parsers/Loaders.java
r101 r103 22 22 Access paramBuilderAccess = new ReflectionAccess(ParamBuilder.class, FramsClass.build().forClass(ParamBuilder.class)); 23 23 framsClassAccess.select(builder); 24 loader.addAccess(framsClassAccess); 25 loader.addAccess(paramBuilderAccess); 24 loader.setAccessProvider(new AccessStash().add(framsClassAccess).add(paramBuilderAccess)); 26 25 27 26 loader.addListener(MultiParamLoader.Status.AfterObject, new MultiParamLoader.StatusListener() { -
java/main/src/main/java/com/framsticks/parsers/MultiParamLoader.java
r102 r103 2 2 3 3 import com.framsticks.params.*; 4 import com.framsticks.util.Misc; 5 4 6 import org.apache.logging.log4j.Logger; 5 7 import org.apache.logging.log4j.LogManager; … … 87 89 * List of known classes. 88 90 */ 89 protected AccessProvider accessProvider = n ew AccessStash();91 protected AccessProvider accessProvider = null; 90 92 91 93 /** … … 123 125 */ 124 126 public Status go() { 127 Misc.throwIfNull(accessProvider); 125 128 log.trace("go"); 126 129 … … 310 313 311 314 /** 312 * Adds another class.313 */314 public void addAccess(Access access) {315 accessProvider.addAccess(access);316 }317 318 /**319 315 * Checks whether execution is finished. 320 316 */ … … 443 439 final MultiParamLoader loader = new MultiParamLoader(); 444 440 loader.setNewSource(source); 445 loader. addAccess(access);441 loader.setAccessProvider(new AccessStash().add(access)); 446 442 loader.addListener(MultiParamLoader.Status.AfterObject, new StatusListener() { 447 443 @Override -
java/main/src/main/java/com/framsticks/remote/RemoteTree.java
r102 r103 17 17 import com.framsticks.params.types.ProcedureParam; 18 18 import com.framsticks.parsers.Loaders; 19 import com.framsticks.parsers.MultiParamLoader;20 19 import com.framsticks.util.*; 21 20 import com.framsticks.util.dispatching.AtOnceDispatcher; … … 29 28 import com.framsticks.util.dispatching.JoinableState; 30 29 import com.framsticks.util.dispatching.ThrowExceptionHandler; 31 import com.framsticks.util.lang.Casting;32 30 import com.framsticks.util.dispatching.RunAt; 33 31 import static com.framsticks.core.TreeOperations.*; … … 269 267 270 268 @Override 271 public void call(@Nonnull final Path path, @Nonnull final ProcedureParam procedure, @Nonnull Object[] arguments, final Future<Object> future) {269 public <R> void call(@Nonnull final Path path, @Nonnull final ProcedureParam procedure, @Nonnull Object[] arguments, final Class<R> resultType, final Future<R> future) { 272 270 assert isActive(); 273 271 assert path.isResolved(); … … 278 276 protected void processOk(Response response) { 279 277 assert isActive(); 280 // InstanceUtils.processFetchedValues(path, response.getFiles()); 281 future.pass(null); 278 279 if (response.getFiles().size() == 0) { 280 future.pass(null); 281 return; 282 } 283 if (response.getFiles().size() == 1) { 284 future.pass(AccessOperations.convert(resultType, response.getFiles().get(0), registry)); 285 return; 286 } 287 throw new FramsticksUnsupportedOperationException().msg("call result with multiple files"); 288 282 289 } 283 290 }); … … 303 310 protected void runAt() { 304 311 assert isActive(); 305 if (argumentType.equals(File.class)) { 306 listener.action(Casting.tryCast(argumentType, file)); 307 return; 308 } 309 // Access access = registry.createAccess(argumentType); 310 311 // log.info("executing event with argument {}", argumentType); 312 MultiParamLoader loader = new MultiParamLoader(); 313 loader.setNewSource(file.getContent()); 314 loader.addBreakCondition(MultiParamLoader.Status.AfterObject); 315 loader.setAccessProvider(registry); 316 // loader.addAccess(access); 317 loader.go(); 318 Object argument = loader.getLastAccess().getSelected(); 319 // Object argument = access.getSelected(); 320 if (argument == null) { 321 listener.action(null); 322 } 323 if (!argumentType.isInstance(argument)) { 324 throw new FramsticksException().msg("created argument is of wrong type").arg("expected", argumentType).arg("created", argument.getClass()); 325 } 326 listener.action(argumentType.cast(argument)); 312 313 listener.action(AccessOperations.convert(argumentType, file, registry)); 327 314 } 328 315 }); -
java/main/src/main/java/com/framsticks/running/ExternalProcess.java
r102 r103 130 130 @Override 131 131 protected void joinableStart() { 132 log. debug("running process {}", this);132 log.info("running process {}", this); 133 133 builder.command(arguments); 134 134 try { -
java/main/src/main/java/com/framsticks/test/PrimeExperiment.java
r102 r103 1 1 package com.framsticks.test; 2 3 import java.util.Iterator; 4 import java.util.LinkedList; 5 import java.util.List; 2 6 3 7 import org.apache.logging.log4j.Logger; 4 8 import org.apache.logging.log4j.LogManager; 5 9 10 import com.framsticks.core.ValueChange; 6 11 import com.framsticks.experiment.Experiment; 7 12 import com.framsticks.experiment.WorkPackageLogic; 13 import com.framsticks.params.EventListener; 14 import com.framsticks.params.EventListeners; 8 15 import com.framsticks.params.annotations.FramsClassAnnotation; 9 16 import com.framsticks.params.annotations.ParamAnnotation; 17 import com.framsticks.test.prime.ExpParams; 10 18 import com.framsticks.test.prime.PrimePackage; 19 import com.framsticks.util.FramsticksException; 11 20 import com.framsticks.util.dispatching.Future; 12 21 … … 18 27 public final WorkPackageLogic<PrimePackage> workPackageLogic; 19 28 20 protected int maxNumber = 3000; 29 final PrimePackage task = new PrimePackage(); 30 21 31 protected int step = 100; 22 protected int numberCounter = 1; 32 33 protected int nextWaitNumber = 1; 34 protected List<PrimePackage> waitingPackages = new LinkedList<>(); 35 protected List<Integer> primes = new LinkedList<>(); 36 37 protected final EventListeners<ValueChange> primesListeners = new EventListeners<>(); 23 38 24 39 /** … … 28 43 setExpdef("prime"); 29 44 30 workPackageLogic = new WorkPackageLogic<PrimePackage>(this) { 45 task.params.from_number = 1; 46 task.params.to_number = 1000; 47 task.state.current_number = 1; 48 49 workPackageLogic = new WorkPackageLogic<PrimePackage>(this, PrimePackage.class) { 31 50 32 51 @Override 33 52 protected void generateNextPackage(Future<PrimePackage> future) { 34 53 assert isActive(); 35 PrimePackage wp = new PrimePackage(); 54 if (task.state.current_number > task.params.to_number) { 55 log.debug("no more packages in range left"); 56 future.pass(null); 57 return; 58 } 36 59 37 log.debug("generated package: {}", wp); 38 wp.params.from_number = numberCounter; 39 numberCounter += step; 40 wp.params.to_number = numberCounter - 1; 41 wp.state.current_number = wp.params.from_number; 42 wp.state.getResultList().clear(); 60 PrimePackage wp = PrimePackage.compose(task.state.current_number, Math.min(task.params.to_number, task.state.current_number + step - 1)); 61 62 task.state.current_number += step; 63 64 log.debug("generated package: {}", wp.getShortDescription()); 43 65 44 66 future.pass(wp); 67 } 68 69 @Override 70 protected void returnPackage(PrimePackage result) { 71 log.debug("returned package: {}", result); 72 73 if (tryAddResult(result)) { 74 while (tryMergeWaitingPackages()) { 75 } 76 } else if (result.params.from_number > nextWaitNumber) { 77 waitingPackages.add(result); 78 } 79 80 try { 81 isComplete(); 82 } catch (FramsticksException e) { 83 log.debug("experiment is not done yet: {}", e.getShortMessage(new StringBuilder())); 84 return; 85 } 86 log.info("experiment is done, {} primes found", primes.size()); 87 log.debug("primes: {}", primes); 45 88 } 46 89 }; 47 90 } 48 91 49 protected void processFile() { 92 protected boolean tryAddResult(PrimePackage result) { 93 if (result.params.from_number > nextWaitNumber) { 94 return false; 95 } 96 if (result.params.from_number < nextWaitNumber) { 97 throw new FramsticksException().msg("duplicate result").arg("result", result); 98 } 99 100 primes.addAll(result.state.getResultList()); 101 nextWaitNumber = result.params.to_number + 1; 102 103 primesListeners.actionForAll(new ValueChange(getDescription())); 104 105 return true; 106 } 107 108 protected boolean tryMergeWaitingPackages() { 109 Iterator<PrimePackage> i = waitingPackages.iterator(); 110 while (i.hasNext()) { 111 if (tryAddResult(i.next())) { 112 i.remove(); 113 return true; 114 } 115 } 116 return false; 117 } 118 119 protected void isComplete() { 120 if (nextWaitNumber - 1 != task.params.to_number) { 121 throw new FramsticksException().msg("not ready yet").arg("done to", nextWaitNumber - 1); 122 } 123 } 124 125 /** 126 * @return the maxNumber 127 */ 128 @ParamAnnotation 129 public int getMaxNumber() { 130 return task.params.to_number; 131 } 132 133 /** 134 * @param maxNumber the maxNumber to set 135 */ 136 @ParamAnnotation 137 public void setMaxNumber(int maxNumber) { 138 task.params.to_number = maxNumber; 139 } 140 141 @ParamAnnotation 142 public String getDescription() { 143 return primes.size() + " found in range " + new ExpParams(task.params.from_number, nextWaitNumber - 1); 144 } 145 146 @ParamAnnotation(id = "description_changed") 147 public void addPrimesListener(EventListener<ValueChange> listener) { 148 primesListeners.add(listener); 149 } 150 151 @ParamAnnotation(id = "description_changed") 152 public void removePrimesListener(EventListener<ValueChange> listener) { 153 primesListeners.remove(listener); 50 154 } 51 155 -
java/main/src/main/java/com/framsticks/test/TestChild.java
r101 r103 10 10 protected String name = "child"; 11 11 12 protected final TestClass parent; 12 protected TestClass parent; 13 13 14 /** 14 15 * 15 16 */ 16 public TestChild(TestClass parent) { 17 this.parent = parent; 17 public TestChild() { 18 18 } 19 19 … … 32 32 public void setName(String name) { 33 33 this.name = name; 34 parent.children.modify(this); 34 if (parent != null) { 35 parent.children.modify(this); 36 } 37 } 38 39 @Override 40 public String toString() { 41 return "test child " + name; 35 42 } 36 43 -
java/main/src/main/java/com/framsticks/test/TestClass.java
r101 r103 7 7 8 8 import com.framsticks.core.ListChange; 9 import com.framsticks.core.ValueChange; 9 10 import com.framsticks.params.EventListener; 10 import com.framsticks.params. EventListeners;11 import com.framsticks.params.SimplePrimitive; 11 12 import com.framsticks.params.SimpleUniqueList; 13 import com.framsticks.params.annotations.AutoAppendAnnotation; 12 14 import com.framsticks.params.annotations.FramsClassAnnotation; 13 15 import com.framsticks.params.annotations.ParamAnnotation; … … 27 29 ) 28 30 public class TestClass { 29 private static final Logger log = 30 LogManager.getLogger(TestClass.class); 31 private static final Logger log = LogManager.getLogger(TestClass.class); 31 32 32 33 33 34 protected String name = "test"; 34 protected String history = "initial|"; 35 protected final EventListeners<TestChangeEvent> historyListeners = new EventListeners<>(); 35 protected final SimplePrimitive<String> history = new SimplePrimitive<>("initial|"); 36 36 37 37 protected final SimpleUniqueList<TestChild> children = new SimpleUniqueList<>(TestChild.class, 'c'); … … 64 64 @ParamAnnotation 65 65 public String getHistory() { 66 return history ;66 return history.get(); 67 67 } 68 68 … … 72 72 @ParamAnnotation 73 73 public void setHistory(String history) { 74 this.history = history;74 this.history.set(history); 75 75 } 76 76 … … 80 80 @ParamAnnotation 81 81 public Map<String, TestChild> getChildren() { 82 return children.get View();82 return children.getMap(); 83 83 } 84 84 … … 86 86 public int appendHistory(String line) { 87 87 log.debug("appending '{}'", line); 88 history = history + line + "|";89 fireHistoryChange();90 return history. length();88 history.set(history.get() + line + "|"); 89 // fireHistoryChange(); 90 return history.get().length(); 91 91 } 92 92 … … 94 94 public void resetHistory() { 95 95 log.debug("reseting"); 96 history = ""; 97 fireHistoryChange(); 96 history.set(""); 98 97 } 99 98 100 99 @ParamAnnotation(paramType = ProcedureParam.class) 101 100 public void createChild(String name) { 102 TestChild child = new TestChild( this);101 TestChild child = new TestChild(); 103 102 child.name = name; 103 addChild(child); 104 } 105 106 @AutoAppendAnnotation 107 public void addChild(TestChild child) { 108 child.parent = this; 104 109 children.add(child); 105 110 } 106 111 107 protected void fireHistoryChange() {108 TestChangeEvent event = new TestChangeEvent();109 event.history = history;110 historyListeners.actionForAll(event);111 }112 113 114 112 @ParamAnnotation(id = "history_changed") 115 public void addHistoryListener(EventListener< TestChangeEvent> listener) {116 history Listeners.add(listener);113 public void addHistoryListener(EventListener<ValueChange> listener) { 114 history.addListener(listener); 117 115 } 118 116 119 117 @ParamAnnotation(id = "history_changed") 120 public void removeHistoryListener(EventListener< TestChangeEvent> listener) {121 history Listeners.remove(listener);118 public void removeHistoryListener(EventListener<ValueChange> listener) { 119 history.removeListener(listener); 122 120 } 123 121 -
java/main/src/main/java/com/framsticks/test/prime/ExpParams.java
r102 r103 13 13 public int to_number; 14 14 15 /** 16 * 17 */ 18 public ExpParams() { 19 } 20 21 /** 22 * @param from_number 23 * @param to_number 24 */ 25 public ExpParams(int from_number, int to_number) { 26 this.from_number = from_number; 27 this.to_number = to_number; 28 } 29 15 30 @Override 16 31 public String toString() { 17 32 return "[" + from_number + ":" + to_number + "]"; 18 33 } 34 35 public boolean isInsideOf(ExpParams outer) { 36 return from_number >= outer.from_number && to_number <= outer.to_number; 37 } 19 38 } -
java/main/src/main/java/com/framsticks/test/prime/PrimePackage.java
r102 r103 1 1 package com.framsticks.test.prime; 2 2 3 import com.framsticks.experiment.InvalidWorkPackage; 3 4 import com.framsticks.experiment.WorkPackage; 4 5 import com.framsticks.params.annotations.FramsClassAnnotation; 6 import com.framsticks.params.annotations.ParamAnnotation; 5 7 6 @FramsClassAnnotation 7 public class PrimePackage implements WorkPackage {8 @FramsClassAnnotation(order = {"params", "state"}) 9 public class PrimePackage implements WorkPackage<PrimePackage> { 8 10 11 @ParamAnnotation 9 12 public final ExpParams params = new ExpParams(); 10 13 14 @ParamAnnotation 11 15 public final ExpState state = new ExpState(); 12 16 … … 16 20 } 17 21 22 @Override 23 public String sumUpTask() { 24 return params.toString(); 25 } 26 27 @Override 28 public String sumUpResult() { 29 return state.toString(); 30 } 31 32 @Override 33 public String getShortDescription() { 34 return params.toString(); 35 } 36 37 public static PrimePackage compose(int from, int to) { 38 PrimePackage wp = new PrimePackage(); 39 wp.params.from_number = from; 40 wp.params.to_number = to; 41 wp.state.current_number = from; 42 return wp; 43 } 44 45 @Override 46 public PrimePackage getRemainder(PrimePackage result) { 47 if (!result.params.isInsideOf(params)) { 48 throw new InvalidWorkPackage().msg("result is not inside of task"); 49 } 50 if (result.params.from_number != params.from_number) { 51 throw new InvalidWorkPackage().msg("result is not subpackage"); 52 } 53 if (result.state.current_number == params.to_number + 1) { 54 return null; 55 } 56 return compose(result.state.current_number, params.to_number); 57 } 18 58 } -
java/main/src/main/java/com/framsticks/util/Misc.java
r102 r103 59 59 return; 60 60 } 61 throw new FramsticksException().msg(message).arg("expected", expected).arg("found", found).arg("in", context); 61 FramsticksException e = new FramsticksException().msg(message).arg("expected", expected).arg("found", found); 62 if (context != null) { 63 e.arg("in", context); 64 } 65 throw e; 62 66 } 63 67 } -
java/main/src/main/resources/log4j2.xml
r102 r103 17 17 <!-- <logger name="com.framsticks.util.dispatching.AbstractJoinable.Report" level="info" /> --> 18 18 <!-- <logger name="com.framsticks.util.dispatching.Monitor" level="debug" /> --> 19 < logger name="com.framsticks.util.dispatching.BufferedDispatcher" level="debug" />19 <!-- <logger name="com.framsticks.util.dispatching.BufferedDispatcher" level="debug" /> --> 20 20 <!-- <logger name="com.framsticks.util.dispatching.Dispatching" level="debug" /> --> 21 < logger name="com.framsticks.running" level="debug" />21 <!-- <logger name="com.framsticks.running" level="debug" /> --> 22 22 <!-- <logger name="com.framsticks.communication" level="debug" /> --> 23 < logger name="com.framsticks.experiment" level="debug" />24 < logger name="com.framsticks.experiment.Simulator" level="debug" />23 <!-- <logger name="com.framsticks.experiment" level="debug" /> --> 24 <!-- <logger name="com.framsticks.experiment.Simulator" level="debug" /> --> 25 25 <root level="info"> 26 26 <appender-ref ref="Console"/> -
java/main/src/main/resources/prime.xml
r102 r103 8 8 <Server port="9007"> 9 9 <LocalTree name="prime-local-tree"> 10 <PrimeExperiment >10 <PrimeExperiment maxNumber="30000"> 11 11 <SimulatorConnector address="localhost:9100" /> 12 12 <SimulatorConnector address="localhost:9101" /> -
java/main/src/test/java/com/framsticks/gui/ProcedureBrowserTest.java
r101 r103 10 10 import com.framsticks.core.Tree; 11 11 import com.framsticks.core.LocalTree; 12 import com.framsticks.core.ValueChange; 12 13 import com.framsticks.params.Access; 13 14 import com.framsticks.params.EventListener; … … 17 18 import com.framsticks.params.types.StringParam; 18 19 import com.framsticks.parsers.XmlLoader; 19 import com.framsticks.test.TestChangeEvent;20 20 import com.framsticks.test.TestClass; 21 21 import com.framsticks.util.dispatching.FutureHandler; … … 82 82 waitForIdle(); 83 83 84 final EventListener< TestChangeEvent> listener = new EventListener<TestChangeEvent>() {84 final EventListener<ValueChange> listener = new EventListener<ValueChange>() { 85 85 86 86 @Override 87 public void action( TestChangeEventargument) {88 assertThat(argument. history).isEqualTo("");87 public void action(ValueChange argument) { 88 assertThat(argument.value).isEqualTo(""); 89 89 } 90 90 }; … … 96 96 assertThat(access.get("history", String.class)).isEqualTo("initial|Żółw|"); 97 97 98 tree.addListener(Path.to(tree, "/"), access.getFramsClass().getParamEntry("history_changed", EventParam.class), listener, TestChangeEvent.class, FutureHandler.doNothing(Void.class, failOnException));98 tree.addListener(Path.to(tree, "/"), access.getFramsClass().getParamEntry("history_changed", EventParam.class), listener, ValueChange.class, FutureHandler.doNothing(Void.class, failOnException)); 99 99 } 100 100 }); -
java/main/src/test/java/com/framsticks/hosting/ServerTest.java
r101 r103 11 11 import com.framsticks.core.LocalTree; 12 12 import com.framsticks.core.Path; 13 import com.framsticks.core.Result; 13 14 import com.framsticks.core.TreeOperations; 15 import com.framsticks.core.ValueChange; 14 16 import com.framsticks.core.XmlBasedTest; 15 17 import com.framsticks.remote.RemoteTree; 16 18 17 import com.framsticks.test.TestChangeEvent;18 19 import com.framsticks.test.TestClass; 19 20 import com.framsticks.core.Tree; … … 45 46 protected LocalTree hosted; 46 47 protected TestClass hostedObject; 47 protected EventListener< TestChangeEvent> listener;48 protected EventListener<ValueChange> listener; 48 49 protected EventListener<ListChange> childListener; 49 50 … … 123 124 public void registerListener() { 124 125 final Waiter waiter = produceWaiter(1.0); 125 listener = new EventListener< TestChangeEvent>() {126 127 @Override 128 public void action( TestChangeEventargument) {129 listenerArguments.add(argument. history);126 listener = new EventListener<ValueChange>() { 127 128 @Override 129 public void action(ValueChange argument) { 130 listenerArguments.add(argument.value.toString()); 130 131 } 131 132 }; … … 138 139 }); 139 140 140 addListener(remotePath, remoteTestFramsClass.getParamEntry("history_changed", EventParam.class), listener, TestChangeEvent.class, produceWaiter(1.0).passInFuture(Void.class));141 addListener(remotePath, remoteTestFramsClass.getParamEntry("history_changed", EventParam.class), listener, ValueChange.class, produceWaiter(1.0).passInFuture(Void.class)); 141 142 } 142 143 … … 145 146 final Waiter waiter = produceWaiter(2.0); 146 147 147 call(remotePath, remoteTestFramsClass.getParamEntry("resetHistory", ProcedureParam.class), new Object[] {}, produceWaiter(2.0).passInFuture(Object.class));148 149 call(remotePath, remoteTestFramsClass.getParamEntry("appendHistory", ProcedureParam.class), new Object[] {"next word"}, new FutureHandler<Object>(failOnException) {150 @Override 151 protected void result( Object result) {148 call(remotePath, remoteTestFramsClass.getParamEntry("resetHistory", ProcedureParam.class), new Object[] {}, Object.class, produceWaiter(2.0).passInFuture(Object.class)); 149 150 call(remotePath, remoteTestFramsClass.getParamEntry("appendHistory", ProcedureParam.class), new Object[] {"next word"}, Result.class, new FutureHandler<Result>(failOnException) { 151 @Override 152 protected void result(final Result result) { 152 153 hosted.dispatch(new RunAt<Tree>(failOnException) { 153 154 @Override 154 155 protected void runAt() { 156 // assert 157 155 158 assertThat(hostedObject.getHistory()).isEqualTo("next word|"); 156 159 waiter.pass(); … … 186 189 public void createChild() { 187 190 final Waiter waiter = produceWaiter(2.0); 188 call(remotePath, "createChild", new Object[] { "a child" }, produceWaiter(2.0).passInFuture(Object.class));189 call(remotePath, "createChild", new Object[] { "another child" }, produceWaiter(2.0).passInFuture(Object.class));191 call(remotePath, "createChild", new Object[] { "a child" }, Object.class, produceWaiter(2.0).passInFuture(Object.class)); 192 call(remotePath, "createChild", new Object[] { "another child" }, Object.class, produceWaiter(2.0).passInFuture(Object.class)); 190 193 191 194 tryGet(remote, "/testClass/children/c0", new FutureHandler<Path>(failOnException) { -
java/main/src/test/java/com/framsticks/params/FramsClassBuilderTest.java
r101 r103 6 6 import org.testng.annotations.Test; 7 7 8 import com.framsticks.core.ValueChange; 8 9 import com.framsticks.params.types.EventParam; 9 10 import com.framsticks.params.types.ProcedureParam; 10 11 import com.framsticks.params.types.StringParam; 11 12 import com.framsticks.parsers.Savers; 12 import com.framsticks.test.TestChangeEvent;13 13 import com.framsticks.test.TestClass; 14 14 import com.framsticks.test.TestConfiguration; … … 69 69 "id:history_changed", 70 70 "name:HistoryListener", 71 "type:e TestChangeEvent",71 "type:e ValueChange", 72 72 "", 73 73 "prop:", … … 128 128 final Holder<String> called = new Holder<>(); 129 129 130 final EventListener< TestChangeEvent> listener = new EventListener<TestChangeEvent>() {130 final EventListener<ValueChange> listener = new EventListener<ValueChange>() { 131 131 132 132 @Override 133 public void action( TestChangeEventargument) {134 called.set(argument. history);133 public void action(ValueChange argument) { 134 called.set(argument.value.toString()); 135 135 } 136 136 }; -
java/main/src/test/java/com/framsticks/params/ParamBuilderTest.java
r101 r103 5 5 import com.framsticks.params.types.FloatParam; 6 6 import com.framsticks.params.types.StringParam; 7 import com.framsticks.parsers.AccessStash; 7 8 import com.framsticks.parsers.MultiParamLoader; 8 9 import com.framsticks.test.TestConfiguration; … … 34 35 MultiParamLoader loader = new MultiParamLoader(); 35 36 loader.setNewSource(source); 36 loader. addAccess(access);37 loader.setAccessProvider(new AccessStash().add(access)); 37 38 loader.addBreakCondition(MultiParamLoader.Status.AfterObject); 38 39 -
java/main/src/test/java/com/framsticks/test/prime/PrimePackageTest.java
r102 r103 1 1 package com.framsticks.test.prime; 2 2 3 //import static org.fest.assertions.Assertions.*;3 import static org.fest.assertions.Assertions.*; 4 4 5 import java.util.Arrays; 6 import java.util.List; 7 8 import org.testng.annotations.BeforeClass; 5 9 import org.testng.annotations.Test; 6 10 11 import com.framsticks.params.AccessOperations; 12 import com.framsticks.params.FramsClass; 13 import com.framsticks.params.ListSink; 14 import com.framsticks.params.ListSource; 15 // import com.framsticks.params.ListSource; 16 import com.framsticks.params.Registry; 17 import com.framsticks.params.types.ObjectParam; 7 18 import com.framsticks.test.TestConfiguration; 8 19 … … 10 21 public class PrimePackageTest extends TestConfiguration { 11 22 23 Registry registry; 12 24 25 protected final List<String> printedPrimePackage = Arrays.asList( 26 "ExpParams:", 27 "from_number:100", 28 "to_number:200", 29 "", 30 "ExpState:", 31 "current_number:201", 32 "result:151,157,163,167,173,179,181,191,193,197,199", 33 "" 34 ); 13 35 36 @BeforeClass 37 public void setupRegistry() { 38 registry = new Registry(); 39 registry.registerAndBuild(PrimePackage.class); 40 } 14 41 15 42 @Test 43 public void primePackageFramsClass() { 44 FramsClass framsClass = registry.getFramsClassForJavaClass(PrimePackage.class); 45 assertThat(framsClass).isNotNull(); 46 assertThat(framsClass.getParamCount()).isEqualTo(2); 47 assertThat(framsClass.getParam(0)).isInstanceOf(ObjectParam.class); 48 assertThat(framsClass.getParam(1)).isInstanceOf(ObjectParam.class); 49 } 50 51 @Test(dependsOnMethods = "primePackageFramsClass") 16 52 public void printPrimePackage() { 53 PrimePackage primePackage = new PrimePackage(); 54 primePackage.params.from_number = 100; 55 primePackage.params.to_number = 200; 56 57 primePackage.state.current_number = 201; 58 primePackage.state.getResultList().addAll(Arrays.asList(151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199)); 59 60 assertThat(primePackage.state.getResult()).isEqualTo("151,157,163,167,173,179,181,191,193,197,199"); 61 62 ListSink sink = new ListSink(); 63 64 AccessOperations.saveAll(registry.createAccess(PrimePackage.class).select(primePackage), sink, registry); 65 66 assertThat(sink.getOut()).isEqualTo(printedPrimePackage); 17 67 } 18 // ExpParams:19 // from_number:10020 // to_number:20021 68 22 // ExpState: 23 // current_number:201 24 // result:151,157,163,167,173,179,181,191,193,197,199 69 @Test(dependsOnMethods = "printPrimePackage") 70 public void loadPrimePackage() { 71 72 // PrimePackage primePackage = new PrimePackage(); 73 74 ListSource source = new ListSource(printedPrimePackage); 75 76 Object result = AccessOperations.loadAll(registry.createAccess(PrimePackage.class), source, registry).getSelected(); 77 assertThat(result).isInstanceOf(PrimePackage.class); 78 PrimePackage primePackage = (PrimePackage) result; 79 80 assertThat(primePackage.params.from_number).isEqualTo(100); 81 assertThat(primePackage.params.to_number).isEqualTo(200); 82 83 assertThat(primePackage.state.current_number).isEqualTo(201); 84 assertThat(primePackage.state.getResultList()).isEqualTo(Arrays.asList(151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199)); 85 86 } 25 87 } -
java/main/src/test/resources/log4j2.xml
r102 r103 8 8 <loggers> 9 9 <logger name="com.framsticks" level="info" /> 10 <logger name="com.framsticks.running.ExternalProcess" level="debug" />11 10 <logger name="com.framsticks.test.TestConfiguration" level="info" /> 12 <logger name="com.framsticks.experiment" level="debug" /> 13 <logger name="com.framsticks.test.PrimeExperiment" level="debug" /> 11 12 <!-- <logger name="com.framsticks.running.ExternalProcess" level="debug" /> --> 13 <!-- <logger name="com.framsticks.experiment" level="debug" /> --> 14 <!-- <logger name="com.framsticks.test.PrimeExperiment" level="debug" /> --> 15 <!-- <logger name="com.framsticks.experiment.NetLoadSaveLogic" level="debug" /> --> 16 <!-- <logger name="com.framsticks.experiment.WorkPackageLogic" level="debug" /> --> 17 <!-- <logger name="com.framsticks.params.AccessOperations" level="debug" /> --> 14 18 <!-- <logger name="com.framsticks.util.dispatching.AbstractJoinable" level="debug" /> --> 15 19 <!-- <logger name="com.framsticks.util.dispatching.AbstractJoinable.Report" level="info" /> -->
Note: See TracChangeset
for help on using the changeset viewer.