Ignore:
Timestamp:
09/23/13 18:54:07 (11 years ago)
Author:
psniegowski
Message:

HIGHLIGHTS:

  • add SimultorProviders? hierarchy
  • start Framsticks server over SSH
  • FJF compatible with Framsticks 4.0rc3
  • reading and writing of standard.expt
  • a proof-of-concept implementation of StandardExperiment?

CHANGELOG:
Optionally return FreeAccess? from registry.

Add SimulatorRange?.

StandardExperiment? with genotypes circulation.

Automate registration around StandardState?.

More improvements to StandardExperiment?.

Skeleton version of StandardExperiment?.

Test saving of StandardState?.

Standard experiment state is being loaded.

More development towards StandardState? reading.

Work on reading standard experiment state.

Add classes for standard experiment.

Update example standard.expt

Add FreeAccess? and FreeObject?.

Made compatible with version 4.0rc3

Change deserialization policy.

Improve SSH support.

Working running simulator over SSH.

Fix joining bug in Experiment.

Working version of SimulatorRunner?.

Add more SimulatorProviders?.

Working PrimeExperimentTest? with 4.0rc3

Add references to deserialization.

Add OpaqueObject? and it's serialization.

Add deserialization of dictionaries.

Partial implementation of deserialization.

Add more tests for deserialization.

Prepare tests for deserialization.

Add proper result to prime experiment test.

Minor fixes to simulators providers.

Draft version of SimulatorProvider?.

Add SimulatorProvider? interface.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • java/main/src/main/java/com/framsticks/params/ParamsUtil.java

    r105 r107  
    22
    33import java.util.ArrayList;
     4// import java.util.HashMap;
    45import java.util.List;
    56import java.util.Map;
     7import java.util.TreeMap;
     8import java.util.regex.Matcher;
     9import java.util.regex.Pattern;
    610
    711import javax.annotation.Nonnull;
    812
     13// import org.apache.logging.log4j.Logger;
     14// import org.apache.logging.log4j.LogManager;
     15
    916import com.framsticks.util.FramsticksException;
     17import com.framsticks.util.lang.Numbers;
    1018
    1119
     
    1624 */
    1725public abstract class ParamsUtil {
     26        // private final static Logger log = LogManager.getLogger(ParamsUtil.class.getName());
     27
    1828
    1929        public static String readSourceToString(Source source) {
     
    212222                                return true;
    213223                        }
     224                        if (type.equals(OpaqueObject.class)) {
     225                                builder.append(value.toString());
     226                                return true;
     227                        }
    214228                        throw new FramsticksException().msg("invalid type for serialization").arg("type", type);
    215229                }
     
    233247        }
    234248
     249        public static class DeserializationContext {
     250
     251                public static final Pattern OPAQUE_OBJECT_PATTERN = Pattern.compile("^(\\w+)<0x([a-fA-F0-9]+)>$");
     252
     253                @SuppressWarnings("serial")
     254                public static class Exception extends FramsticksException {
     255                }
     256
     257                protected final ArrayList<Object> objects = new ArrayList<>();
     258                protected int cursor = 0;
     259                protected final String input;
     260
     261                /**
     262                 * @param input
     263                 */
     264                public DeserializationContext(String input) {
     265                        this.input = input;
     266                        cursor = 0;
     267                }
     268
     269                protected boolean isFinished() {
     270                        return cursor == input.length();
     271                }
     272
     273                protected boolean is(char value) {
     274                        if (isFinished()) {
     275                                throw fail().msg("input ended");
     276                        }
     277                        return input.charAt(cursor) == value;
     278                }
     279
     280                protected boolean isOneOf(String values) {
     281                        if (isFinished()) {
     282                                throw fail().msg("input ended");
     283                        }
     284                        return values.indexOf(input.charAt(cursor)) != -1;
     285                }
     286
     287                protected char at() {
     288                        return input.charAt(cursor);
     289                }
     290
     291                protected char at(int pos) {
     292                        return input.charAt(pos);
     293                }
     294
     295                protected FramsticksException fail() {
     296                        return new Exception().arg("at", cursor).arg("input", input);
     297                }
     298
     299                protected void force(char value) {
     300                        if (!is(value)) {
     301                                throw fail().msg("invalid character").arg("expected", value).arg("found", at());
     302                        }
     303                        next();
     304                }
     305
     306                protected boolean isAndNext(char value) {
     307                        if (!is(value)) {
     308                                return false;
     309                        }
     310                        next();
     311                        return true;
     312                }
     313
     314                protected void next() {
     315                        ++cursor;
     316                        // log.info("at: {}|{}", input.substring(0, cursor), input.substring(cursor));
     317                }
     318
     319                protected void goToNext(char value) {
     320                        while (cursor < input.length()) {
     321                                if (is(value)) {
     322                                        return;
     323                                }
     324                                next();
     325                        }
     326                        throw fail().msg("passed end").arg("searching", value);
     327                }
     328
     329
     330                protected String forceStringRemaining() {
     331                        int start = cursor;
     332                        while (true) {
     333                                goToNext('"');
     334                                if (at(cursor - 1) != '\\') {
     335                                        next();
     336                                        return input.substring(start, cursor - 1);
     337                                }
     338                                next();
     339                                // it is finishing that loop, because of throwing in goToNext()
     340                        }
     341                        // throw fail();
     342                }
     343
     344                public Object deserialize() {
     345                        if (isAndNext('[')) {
     346                                List<Object> list = new ArrayList<>();
     347                                objects.add(list);
     348                                while (!isAndNext(']')) {
     349                                        if (!list.isEmpty()) {
     350                                                force(',');
     351                                        }
     352                                        Object child = deserialize();
     353                                        list.add(child);
     354                                }
     355                                return list;
     356                        }
     357
     358                        if (isAndNext('{')) {
     359                                Map<String, Object> map = new TreeMap<>();
     360                                objects.add(map);
     361                                while (!isAndNext('}')) {
     362                                        if (!map.isEmpty()) {
     363                                                force(',');
     364                                        }
     365                                        force('"');
     366                                        String key = forceStringRemaining();
     367                                        force(':');
     368                                        Object value = deserialize();
     369                                        map.put(key, value);
     370                                }
     371                                return map;
     372                        }
     373
     374                        if (isAndNext('"')) {
     375                                return forceStringRemaining();
     376                        }
     377                        int start = cursor;
     378                        while (!isFinished() && !isOneOf("]},")) {
     379                                next();
     380                        }
     381                        if (start == cursor) {
     382                                throw fail().msg("empty value");
     383                        }
     384
     385                        String value = input.substring(start, cursor);
     386                        if (value.equals("null")) {
     387                                //TODO: add this to list?
     388                                return null;
     389                        }
     390
     391                        Matcher matcher = OPAQUE_OBJECT_PATTERN.matcher(value);
     392                        if (matcher.matches()) {
     393                                return new OpaqueObject(matcher.group(1), Long.parseLong(matcher.group(2), 16));
     394                        }
     395
     396
     397                        Object number = DeserializationContext.tryParseNumber(value);
     398                        if (number != null) {
     399                                return number;
     400                        }
     401
     402                        if (value.charAt(0) == '^') {
     403                                Integer reference = Numbers.parse(value.substring(1), Integer.class);
     404                                if (reference == null) {
     405                                        throw fail().msg("invalid reference").arg("reference", reference);
     406                                }
     407                                return objects.get(reference);
     408                        }
     409                        //TODO: parse ^
     410                        //TODO: parse opaque object
     411                        throw fail().msg("unknown entity");
     412
     413                }
     414
     415                public static Object tryParseNumber(String value) {
     416                        Integer i = Numbers.parse(value, Integer.class);
     417                        if (i != null) {
     418                                return i;
     419                        }
     420
     421                        Double d = Numbers.parse(value, Double.class);
     422                        if (d != null) {
     423                                return d;
     424                        }
     425
     426                        return null;
     427                }
     428        }
     429
     430
     431        public static Object deserialize(String value) {
     432                Object number = DeserializationContext.tryParseNumber(value);
     433                if (number != null) {
     434                        return number;
     435                }
     436
     437                if (!value.startsWith(SERIALIZED)) {
     438                        return value;
     439                }
     440                return new DeserializationContext(value.substring(SERIALIZED.length())).deserialize();
     441        }
     442
    235443        public static <T> T deserialize(String value, Class<T> type) {
    236                 return null;
     444                Object object = deserialize(value);
     445
     446                return type.cast(object);
    237447        }
    238448
Note: See TracChangeset for help on using the changeset viewer.