source: java/main/src/main/java/com/framsticks/params/SimpleAbstractAccess.java @ 90

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

HIGHLIGHTS:

CHANGELOG:
Make ProcedureParam? hold only ValueParams?.

Use id instead of names when naming gui components internally.

Basic procedure calling in GUI.

The actual procedure call is currently only backed
by the ObjectInstance?.

Add UnimplementedException?.

Improve naming of various gui elements.

Allow easy navigating in FEST Swing testing.

Add optional explicit order attribute to FramsClassAnnotation?.

That's because java reflection does return declared members
in any specific order. That ordering is needed only for
classes that have no representation in framsticks and need
a deterministic ordering of params.

Add ControlOwner? interface.

Add test for procedure calling in Browser.

First version of ParamAnnotation? for procedures.

Development of ProcedureParam?.

Add draft version of ProcedureParam? implementation in ReflectionAccess?.

Allow viewing FramsClasses? in gui Browser.

Extract ResourceBuilder? from ModelBuilder?.

Remove internalId from Param.

It was currently completely not utilised. Whether it is still needed
after introduction of ParamAnnotation? is arguable.

Add remaining param attributes to ParamAnnotation?.

Change AutoBuilder? semantics.

AutoBuilder? returns list of objects that are to be appended
with methods @AutoAppendAnnotation?.

This allows to omit explicit addition of ModelPackage? to instance
if the instance uses ModelBuilder? (registration of ModelPackage? comes
from schema).

Fix params ordering problem in auto created FramsClasses?.

Improve ObjectInstance?.

Several fixes to ModelBuilder?.

Improve test for ObjectInstance? in Browser.

Make initialization of robot static.

With robot recreated for second browser test, the test hanged
deep in AWT.

Add base convenience base test for Browser tests.

More tests to ObjectInstance?.

Rename Dispatcher.invokeLater() to dispatch().

Add assertDispatch.

It allows assertions in other threads, than TestNGInvoker.
Assertions are gathered after each method invocation and rethrown.

Use timeOut annotation attribute for tests involving some waiting.

Remove firstTask method (merge with joinableStart).

Clean up leftovers.

Remove unused FavouritesXMLFactory (the reading part is already
completely done with generic XmlLoader?, and writing part will be done
based on the same approach if needed).
Move UserFavourite? to the com.framsticks.gui.configuration package.

Remove GenotypeBrowser? as to specific.

This functionality will be available in ObjectInstance?.

Add interface ParamsPackage?.

Package containing registration of Java classes meant to use with
ReflectionAccess? may be in Instance using configuration.

Minor changes.

Make Group immutable.

Add AutoBuilder? interface extending Builder - only those would
be used to automatically build from XML.

Fix groups in FramsClass?.

Minor naming cleanup in Registry.

Add ModelComponent? interface.

All class creating the Model are implementing that interface.

Extract Model.build into ModelBuilder?.

ModelBuilder? will be compatible with other builders
and allow using it from configuration.

Fix NeuroConnection?.

Add synchronous get operation for dispatchers.

Rename JoinableMonitor? to Monitor.

Add ObjectInstance?.

This class is mainly for demonstration
and testing purposes.

Improve FramsServer? runner.

  • improve ExternalProcess? runner,
  • runner can kill the server but also react properly, when the server exists on it's own,
  • set default path to search for framsticks server installation,
  • add LoggingOutputListener?.
File size: 7.0 KB
Line 
1package com.framsticks.params;
2
3import java.io.IOException;
4import java.util.Collection;
5
6import static com.framsticks.util.lang.Containers.filterInstanceof;
7
8import org.apache.log4j.Logger;
9
10/**
11 * The Class SimpleAbstractAccess implements all the methods of AccessInterface
12 * which actions can be implemented with usage of {@link AccessInterface} methods
13 * or concern schema, which is stored in {@link #framsClass}
14 *
15 * Based on c++ class SimpleAbstractParam located in: cpp/gdk/param.*
16 *
17 * @author Jarek Szymczak <name.surname@gmail.com>, Mateusz Jarus (please
18 *         replace name and surname with my personal data)
19 *
20 * @author Piotr Sniegowski
21 */
22public abstract class SimpleAbstractAccess implements AccessInterface {
23
24        private final static Logger log = Logger.getLogger(SimpleAbstractAccess.class.getName());
25
26        protected final FramsClass framsClass;
27
28        /**
29         * @param framsClass
30         */
31        public SimpleAbstractAccess(FramsClass framsClass) {
32                this.framsClass = framsClass;
33        }
34
35        @Override
36        public final FramsClass getFramsClass() {
37                return framsClass;
38        }
39
40        /**
41         * Simple String key, value class.
42         */
43        public static class Entry {
44
45                public final String key;
46                public final String value;
47
48                public Entry(String key, String value) {
49                        this.key = key;
50                        this.value = value;
51                }
52
53                @Override
54                public String toString() {
55                        return key + " = " + value;
56                }
57        }
58
59
60        @Override
61        public String getId() {
62                return framsClass.getId();
63        }
64
65        @Override
66        public int getParamCount() {
67                return framsClass.getParamCount();
68        }
69
70        @Override
71        public Param getParam(int i) {
72                return framsClass.getParam(i);
73        }
74
75        @Override
76        public Param getParam(String id) {
77                return framsClass.getParam(id);
78        }
79
80        // @Override
81        // public Param getGroupMember(int gi, int n) {
82        //      return framsClass.getGroupMember(gi, n);
83        // }
84
85        @Override
86        public <T> T get(int i, Class<T> type) {
87                return get(framsClass.getParamEntry(i, ValueParam.class), type);
88        }
89
90        @Override
91        public <T> T get(String id, Class<T> type) {
92                return get(framsClass.getParamEntry(id, ValueParam.class), type);
93        }
94
95        @Override
96        public <T> int set(int i, T value) {
97                return set(framsClass.getParamEntry(i, ValueParam.class), value);
98        }
99
100        @Override
101        public <T> int set(String id, T value) {
102                return set(framsClass.getParamEntry(id, ValueParam.class), value);
103        }
104
105        @Override
106        public <T> int set(ValueParam param, T value) {
107                int flags = 0;
108
109                //String id = param.getEffectiveId();
110                try {
111                        Object oldValue = get(param, param.getStorageType());
112                        ReassignResult<?> result = param.reassign(value, oldValue);
113                        Object casted = result.getValue();
114                        if (!casted.equals(oldValue)) {
115                                internalSet(param, casted);
116                        }
117                        flags = result.getFlags();
118                } catch (CastFailure e) {
119                        log.error("casting failure while set: ", e);
120                }
121                return flags;
122        }
123
124        @Override
125        public void setDefault(boolean numericOnly) {
126                for (int i = 0; i < framsClass.getParamCount(); i++) {
127                        setDefault(i, numericOnly);
128                }
129        }
130
131        @Override
132        public void setDefault(int i, boolean numericOnly) {
133                ValueParam entry = framsClass.getParamEntry(i, ValueParam.class);
134                if ((entry != null)     && (!numericOnly || entry.isNumeric())) {
135                        set(i, entry.getDef(entry.getStorageType()));
136                }
137        }
138
139        @Override
140        public void setMin() {
141                for (int i = 0; i < framsClass.getParamCount(); i++) {
142                        setMin(i);
143                }
144        }
145
146        @Override
147        public void setMin(int i) {
148                PrimitiveParam<?> entry = framsClass.getParamEntry(i, PrimitiveParam.class);
149                if (entry == null) {
150                        return;
151                }
152                Object min = entry.getMin(entry.getStorageType());
153                if (min != null) {
154                        set(i, min);
155                }
156        }
157
158        @Override
159        public void setMax() {
160                for (int i = 0; i < framsClass.getParamCount(); i++) {
161                        setMax(i);
162                }
163        }
164
165        @Override
166        public void setMax(int i) {
167                PrimitiveParam<?> entry = framsClass.getParamEntry(i, PrimitiveParam.class);
168                if (entry == null) {
169                        return;
170                }
171                Object max = entry.getMax(entry.getStorageType());
172                if (max != null) {
173                        set(i, max);
174                }
175        }
176
177        @Override
178        public void copyFrom(AccessInterface src) {
179                clearValues();
180                //TODO: iterate over self, and pull from src
181                /*
182                for (int i = 0; i < src.getFramsClass().size(); i++) {
183                        this.set(i, src.get(i, Object.class));
184                }
185                */
186        }
187
188
189
190
191        @Override
192        public void save(SinkInterface sink) {
193                assert framsClass != null;
194                sink.print(framsClass.getId()).print(":").breakLine();
195                for (PrimitiveParam<?> p : filterInstanceof(framsClass.getParamEntries(), PrimitiveParam.class)) {
196                        Object value = get(p, Object.class);
197                        if (value == null) {
198                                continue;
199                        }
200                        sink.print(p.getId()).print(":");
201                        p.save(sink, value);
202                        sink.breakLine();
203                }
204                sink.breakLine();
205        }
206
207        private Entry readEntry(SourceInterface source)
208                        throws IOException {
209
210                String line;
211                String key = null;
212                StringBuilder value = null;
213                while ((line = source.readLine()) != null)
214                {
215                        if (key == null) {
216                                int colonIndex = line.indexOf(':');
217                                if (colonIndex == -1) {
218                                        return null;
219                                }
220                                key = line.substring(0, colonIndex);
221                                String inlineValue = line.substring(colonIndex + 1);
222
223
224                                if (!inlineValue.startsWith("~")) {
225                                        return new Entry(key, inlineValue);
226                                }
227                                value = new StringBuilder();
228                                value.append(inlineValue.substring(1));
229                                continue;
230                        }
231                        if (value.length() != 0) {
232                                value.append(System.getProperty("line.separator"));
233                        }
234                        if (line.contains("~")) {
235                                value.append(line.substring(0, line.indexOf("~")));
236                                return new Entry(key, value.toString());
237                        }
238                        value.append(line);
239                        /*
240                        if (line.contains("~")) {
241                                String lastLine = line.substring(0, line.indexOf("~"));
242                                if (lastLine.length() > 0) {
243                                        appendToValue(value, lastLine);
244                                }
245                                return new Entry(key, value.toString());
246                        }
247                        appendToValue(value, line);
248                        */
249                }
250                return null;
251        }
252
253        @Override
254        public void load(SourceInterface source) throws Exception {
255                //TODO not clearing values, because get from manager gives only fields, not children
256                //this.clearValues();
257
258                Entry entry;
259                while ((entry = readEntry(source)) != null) {
260                        Param param = getParam(entry.key);
261                        if (param == null) {
262                                continue;
263                        }
264                        if (!(param instanceof ValueParam)) {
265                                log.warn("param " + param + " is not a ValueParam");
266                                continue;
267                        }
268                        if ((param.getFlags() & Flags.DONTLOAD) != 0) {
269                                log.debug("DontLoad flag was set - not loading...");
270                        } else {
271                                int retFlags = this.set((ValueParam) param, entry.value);
272                                if ((retFlags & (Flags.PSET_HITMIN | Flags.PSET_HITMAX)) != 0) {
273                                        String which = ((retFlags & Flags.PSET_HITMIN) != 0) ? "small" : "big";
274                                        log.warn("value of key '" + entry.key + "' was too " + which + ", adjusted");
275                                }
276                        }
277                }
278        }
279
280        protected abstract <T> void internalSet(ValueParam param, T value);
281
282        @Override
283        public Collection<Param> getParams() {
284                return framsClass.getParamEntries();
285        }
286
287        /*
288        protected <T extends Comparable<T>> int setAndCut(Param param, Object value, Class<T> type) {
289                int flags = 0;
290                T val = type.cast(value);
291                T min = param.getMin(type);
292                T max = param.getMax(type);
293                if (min != null && val.compareTo(min) < 0) {
294                        val = min;
295                        flags |= Flags.PSET_HITMIN;
296                }
297                if (max != null && val.compareTo(max) > 0) {
298                        val = max;
299                        flags |= Flags.PSET_HITMAX;
300                }
301                internalSet(param, val);
302                return flags;
303        }*/
304
305
306}
Note: See TracBrowser for help on using the repository browser.