source: java/main/src/main/java/com/framsticks/experiment/NetLoadSaveLogic.java @ 105

Last change on this file since 105 was 105, checked in by psniegowski, 11 years ago

HIGHLIGHTS:

  • import refactorization: move Tree, Path, etc.

from core to structure package

  • initial serialization implementation
  • improve PrimeExperiment? test
  • many organizational changes and convenience improvements

CHANGELOG:
Make registry in AbstractTree? final.

Move most classes from core to structure package.

Minor changes.

Switch names of Future and FutureHandler?.

Rename ExceptionResultHandler? to ExceptionHandler?.

Rename ExceptionHandler? to ExceptionDispatcherHandler?.

Fix bug in ParamCandidate? cache.

Add missing synchronization to the BufferedDispatcher?.

Develop @Serialized support.

Rework serialization further.

Add serialization/deserialization interface to ValueParam?.

Move getStorageType and isNumeric from Param down to params hierarchy.

Minor changes.

Improve param type induction.

Add TestSerializedClass? for testing new serialization.

Add info files gor GenePool? and Population.

Add standard.expt exemplary netfile.

Add type name field to PropertiesObject?.

Use PropertiesObject? for PropertiesAccess? instead of ordinary map.

Hide getFramsClass is several more places.

More unification accross FramsClass?, Access and Path.

Add ParamCollection?.

Simplify interface for getting params from FramsClass?, Access
or Path.

Make Access.call() interface variadic.

Add arguments(args) convenience wrapper around new Object[] {args}.

Upgrade to apache.commons.lang version 3.1

Minor improvement with Response constructors.

Develop proper result printing in ClientAtServer?.

Add experimentNetsave to PrimeExperiment?.

File size: 3.5 KB
Line 
1package com.framsticks.experiment;
2
3
4import org.apache.logging.log4j.Logger;
5import org.apache.logging.log4j.LogManager;
6
7import com.framsticks.params.EventListener;
8import com.framsticks.params.MessageLogger;
9import com.framsticks.params.annotations.FramsClassAnnotation;
10import com.framsticks.params.annotations.ParamAnnotation;
11import com.framsticks.structure.messages.ListChange;
12import com.framsticks.structure.messages.Message;
13import com.framsticks.util.dispatching.FutureHandler;
14import com.framsticks.util.dispatching.Future;
15
16@FramsClassAnnotation
17public abstract class NetLoadSaveLogic<NF extends NetFile> extends AbstractExperimentLogic {
18        private static final Logger log = LogManager.getLogger(NetLoadSaveLogic.class);
19
20        protected String option = "an option";
21
22        protected final Class<NF> netJavaClass;
23
24        protected final MessageLogger messages = new MessageLogger(NetLoadSaveLogic.class);
25
26        /**
27         * @param experiment
28         */
29        public NetLoadSaveLogic(Experiment parentExperiment, Class<NF> netJavaClassArg) {
30                super(parentExperiment);
31                this.netJavaClass = netJavaClassArg;
32
33                experiment.addSimulatorsListener(new EventListener<ListChange>() {
34
35                        @Override
36                        public void action(final ListChange change) {
37                                assert experiment.isActive();
38                                final Simulator simulator = experiment.getSimulators().get(change.getIdentifier());
39                                log.debug("processing list change: {}", change);
40
41                                if (change.getAction() == ListChange.Action.Add) {
42                                        log.debug("registering in {}", simulator);
43                                        simulator.getRemoteTree().getRegistry().registerAndBuild(netJavaClass);
44                                }
45
46                                if (!change.hasHint("stoped")) {
47                                        issueNetloadIfReady(change, simulator);
48                                        return;
49                                }
50
51                                log.debug("issuing netsave to: {}", simulator);
52                                simulator.netsave(netJavaClass, new Future<NF>(simulator) {
53
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                                });
61                        }
62                });
63        }
64
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 Future<NF>(simulator) {
71
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 Future<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, FutureHandler<NF> net);
93
94        public abstract void netsave(Simulator simulator, NF net);
95
96        /**
97         * @return the option
98         */
99        @ParamAnnotation
100        public String getOption() {
101                return option;
102        }
103
104        /**
105         * @param option the option to set
106         */
107        @ParamAnnotation
108        public void setOption(String option) {
109                this.option = option;
110        }
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
130}
Note: See TracBrowser for help on using the repository browser.