Ignore:
Timestamp:
01/09/13 00:09:10 (11 years ago)
Author:
psniegowski
Message:

Add f0 parsing and f0->Model transformation.

Location:
java/main/src/main/java/com/framsticks/params
Files:
1 added
8 edited

Legend:

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

    r77 r78  
    8080
    8181        /**
    82          * Load values from single line String.
    83          *
    84          * @param line
    85          *            the line with values
    86          * @return the list of not severe exceptions which occurred while loading
    87          * @throws Exception
    88          *             the severe exception occurred while loading
    89          */
    90         List<Exception> load2(String line) throws Exception;
    91 
    92         /**
    9382         * Removes all the properties values.
    9483         */
     
    10796    FramsClass getFramsClass();
    10897
     98
    10999}
  • java/main/src/main/java/com/framsticks/params/FramsClass.java

    r77 r78  
    55import com.framsticks.params.types.FloatParam;
    66import com.framsticks.params.types.StringParam;
     7import com.framsticks.parsers.FileSource;
     8import com.framsticks.parsers.Loaders;
    79import com.framsticks.util.Casting;
    810import org.apache.log4j.Logger;
    911
    1012import javax.lang.model.element.TypeElement;
     13import java.io.InputStream;
    1114import java.lang.reflect.*;
    1215import java.util.*;
     
    5861
    5962        public Collection<Param> getParamEntries() {
    60                 return paramEntryMap.values();
     63                return paramList;
    6164        }
    6265
     
    214217                }
    215218            }
     219                        if (rawType.equals(List.class)) {
     220                                Type containedType = p.getActualTypeArguments()[0];
     221                                if (containedType instanceof Class) {
     222                                        return "l " + ((Class) containedType).getCanonicalName();
     223                                }
     224                        }
    216225            return null;
    217226        }
     
    234243    public static final String GENERATE_HELP_PREFIX = "automatically generated from: ";
    235244
    236 
    237 
    238     public static class Constructor {
     245        public static FramsClass readFromStream(InputStream stream) {
     246                return Loaders.loadFramsClass(new FileSource(stream));
     247        }
     248
     249        public static class Constructor {
    239250        protected final FramsClass result;
    240251        protected Class currentClass;
     
    249260                currentClass = currentClass.getSuperclass();
    250261            }
     262                        currentClass = src;
    251263        }
    252264
     
    254266            return result;
    255267        }
     268
     269                public Constructor allFields() {
     270                        for (Field f : currentClass.getFields()) {
     271                                field(f.getName());
     272                        }
     273                        return this;
     274                }
    256275
    257276        public Constructor method(String name, Class<?> ... arguments) {
  • java/main/src/main/java/com/framsticks/params/ListAccess.java

    r77 r78  
    7373        }
    7474
    75         @Override
    76         public List<Exception> load2(String line) throws Exception {
    77                 return null;
    78         }
    79 
    8075        public AccessInterface getElementAccess() {
    8176                return elementAccess;
  • java/main/src/main/java/com/framsticks/params/Param.java

    r77 r78  
    109109        } catch (ClassCastException e) {
    110110            throw new ClassCastException("property \"" + name
    111                     + "\" getType is \"" + value.getClass().getName()
     111                    + "\" type is \"" + value.getClass().getName()
    112112                    + "\", not \"" + type.getName() + "\"");
    113113        }
  • java/main/src/main/java/com/framsticks/params/PropertiesAccess.java

    r77 r78  
    2020    @Override
    2121        public Map<String, Object> createAccessee() {
     22                return PropertiesAccess.createPropertiesMap();
     23        }
     24
     25        public static Map<String, Object> createPropertiesMap() {
    2226                return new HashMap<String, Object>();
    2327        }
    24 
    2528
    2629        public PropertiesAccess(FramsClass framsClass) {
     
    8386
    8487
    85 
    86 
    8788}
  • java/main/src/main/java/com/framsticks/params/ReflectionAccess.java

    r77 r78  
    33import java.lang.reflect.Field;
    44import java.lang.reflect.InvocationTargetException;
     5import java.lang.reflect.Modifier;
     6import java.lang.reflect.Type;
    57import java.util.List;
    68
     
    4345                        String id = param.getId();
    4446                        try {
    45                                 return type.cast(object.getClass().getField(id).get(object));
     47                                return type.cast(reflectedClass.getField(id).get(object));
    4648                        } catch (NoSuchFieldException ignored) {
    4749                        }
    4850                        try {
    49                                 return type.cast(object.getClass().getMethod(accessorName(true, id)).invoke(object));
     51                                return type.cast(reflectedClass.getMethod(accessorName(true, id)).invoke(object));
    5052                        } catch (NoSuchMethodException ex) {
    5153                                //ex.printStackTrace();
     
    6870        }
    6971
    70         private <T> void setValue(Param param, T val) {
     72        private <T> void setValue(Param param, T value) {
    7173                if (object == null) {
    7274                        return;
     
    7577                        String id = param.getId();
    7678                        try {
    77                                 object.getClass().getField(id).set(object, val);
     79                                Field f = reflectedClass.getField(id);
     80                                Class t = f.getType();
     81                                if (Modifier.isFinal(f.getModifiers())) {
     82                                        return;
     83                                }
     84                                if (value != null || (!t.isPrimitive())) {
     85                                        f.set(object, value);
     86                                }
    7887                                return;
    7988                        } catch (NoSuchFieldException ignored) {
    8089                        }
    8190                        try {
    82                                 object.getClass().getMethod(accessorName(false, id), new Class[]{val.getClass()}).invoke(object, val);
    83                         } catch (InvocationTargetException e) {
    84                                 //e.printStackTrace();
     91                                reflectedClass.getMethod(accessorName(false, id), new Class[]{param.getStorageType()}).invoke(object, value);
     92                        } catch (InvocationTargetException ignored) {
    8593                        } catch (NoSuchMethodException ignored) {
    8694                        }
    87                 } catch (IllegalAccessException ex) {
    88                         LOGGER.warn("illegal access error occurred while trying to access returnedObject");
     95                } catch (Exception ex) {
    8996                        ex.printStackTrace();
    9097                }
     
    103110                resetErrors();
    104111
    105                 Field[] fields;
    106112                try {
    107                         // TODO: conceptually invalid - should clean only fields that are specified in FramsClass
    108                         fields = object.getClass().getFields();
    109 
    110                         for (Field field : fields) {
    111                                 field.set(object, null);
     113                        for (Param p : framsClass.getParamEntries()) {
     114                                setValue(p, p.getDef(Object.class));
    112115                        }
    113                 } catch (IllegalAccessException ex) {
    114                         LOGGER.warn("IllegalAccessException thrown while trying to reset object values");
     116                } catch (IllegalArgumentException ex) {
    115117                        ex.printStackTrace();
    116118                }
     
    125127        @Override
    126128        public void select(Object object) {
     129                assert object == null || reflectedClass.isInstance(object);
    127130                this.object = object;
    128131        }
     
    166169        }
    167170        LOGGER.fatal("failed to create reflected object of class " + reflectedClass.getCanonicalName() + " for frams type " + framsClass.getId());
    168 
    169171        return null;
    170172    }
  • java/main/src/main/java/com/framsticks/params/SimpleAbstractAccess.java

    r77 r78  
    3838                this.framsClass = framsClass;
    3939        }
    40 
    41 
    42 
    43 
    4440    /**
    4541         * Simple String key, value class.
    4642         */
    47         private static class Entry {
    48 
    49                 String key;
    50                 String value;
    51 
    52                 Entry(String key, String value) {
     43        public static class Entry {
     44
     45                public final String key;
     46                public final String value;
     47
     48                public Entry(String key, String value) {
    5349                        this.key = key;
    5450                        this.value = value;
     
    331327
    332328        Entry entry;
    333 
    334329        while ((entry = readEntry(source)) != null) {
    335330            Param param = getParam(entry.key);
     
    351346    }
    352347
    353     @Override
    354         public List<Exception> load2(String line) throws Exception {
    355                 this.clearValues();
    356 
    357                 // list of not terminable exceptions that occured
    358                 List<Exception> exceptions = new ArrayList<Exception>();
    359 
    360                 int indexOfColon = line.indexOf(':');
    361                 if (indexOfColon < 0)
    362                         indexOfColon = line.length();
    363                 String classId = line.substring(0, indexOfColon).trim();
    364                 if (!getId().equals(classId))
    365                         throw new Exception(
    366                                         "Inappropriate getId of param interface (class), specified \""
    367                                                         + classId + "\" while should be \"" + getId()
    368                                                         + "\"");
    369                 String parameters;
    370                 if (indexOfColon == line.length())
    371                         parameters = "";
    372                 else
    373                         parameters = line.substring(indexOfColon + 1);
    374 
    375                 // tokenize
    376                 boolean doubleQuotes = false;
    377                 char previousChar = ',';
    378                 List<Entry> result = new ArrayList<Entry>();
    379                 StringBuilder stringBuilder = new StringBuilder();
    380                 String key = "";
    381                 if (parameters.trim().length() > 0) {
    382                         for (char currentChar : parameters.toCharArray()) {
    383                                 if (!doubleQuotes && currentChar == '=' && "".equals(key)) {
    384                                         key = stringBuilder.toString();
    385                                         stringBuilder = new StringBuilder();
    386                                 } else if (!doubleQuotes && currentChar == ',') {
    387                                         if (previousChar == ',') {
    388                                                 result.add(new Entry(key.trim(), null));
    389                                         } else {
    390                                                 result.add(new Entry(key.trim(), stringBuilder
    391                                                                 .toString().trim()));
    392                                         }
    393                                         stringBuilder = new StringBuilder();
    394                                         key = "";
    395                                 } else if (currentChar == '"') {
    396                                         if (previousChar == '\\') {
    397                                                 stringBuilder.deleteCharAt(stringBuilder.length() - 1);
    398                                                 stringBuilder.append(currentChar);
    399                                         } else
    400                                                 doubleQuotes = !doubleQuotes;
    401                                 } else {
    402                                         stringBuilder.append(currentChar);
    403                                 }
    404 
    405                                 previousChar = currentChar;
    406                         }
    407 
    408                         String last = stringBuilder.toString().trim();
    409                         // if (last.length() > 0 || previousChar == '\"'
    410                         // || previousChar == ':')
    411                         result.add(new Entry(key.trim(), last));
    412 
    413                         if (doubleQuotes)
    414                                 throw new Exception(
    415                                                 "Double quotes expected while end of line met");
    416                 }
    417 
    418                 // if successfully parsed set all necessary values
    419 
    420                 //TODO: name omitting
    421                 Param currentParam = null;
    422                 boolean illegallyOmittedName = false;
    423                 for (Entry pair : result) {
    424                         try {
    425 
    426                                 if (pair.key != null && !"".equals(pair.key)) {
    427                                         Param param = getParam(pair.key);
    428                                         if (param == null) {
    429                                                 illegallyOmittedName = true;
    430                                                 throw new Exception("No parameter with such id: "
    431                                                                 + pair.key);
    432                                         } else {
    433                                                 currentParam = param;
    434                                                 illegallyOmittedName = false;
    435                                         }
    436                                 } else if (illegallyOmittedName
    437                                                 || (currentParam.getFlags() & Flags.CANOMITNAME) == 0) {
    438                                         throw new Exception(
    439                                                         "Parameter with offset: "
    440                                                                         + currentParam
    441                                                                         + " is not set, "
    442                                                                         + "because it's definition or definition of one of the previous params "
    443                                                                         + "does not contain flag, which allow to skip the getName (flag 1024)");
    444                                 }
    445 
    446                                 if (pair.value != null) {
    447                                         int setFlag = this.set(currentParam, pair.value);
    448                                         if ((setFlag & Flags.PSET_HITMIN) != 0) {
    449                                                 exceptions.add(createBoundaryHitException(currentParam, pair.value, Flags.PSET_HITMIN));
    450                                         }
    451 
    452                                         if ((setFlag & Flags.PSET_HITMAX) != 0) {
    453                                                 exceptions.add(createBoundaryHitException(currentParam, pair.value, Flags.PSET_HITMAX));
    454                                         }
    455 
    456                                         if ((setFlag & Flags.PSET_RONLY) != 0) {
    457                                                 throw (new Exception(
    458                                                                 "Tried to set a read-only attribute \""
    459                                                                                 + currentParam.getId()
    460                                                                                 + "\" in class \"" + getId() + "\""));
    461                                         }
    462                                 }
    463 
    464                         } catch (Exception e) {
    465                                 exceptions.add(e);
    466                         //} finally {
    467                         //  currentProperty++;
    468                         }
    469                 }
    470                 return exceptions;
    471         }
    472 
    473         private Exception createBoundaryHitException(Param param, String value, int flag) {
    474                 boolean minimum = (flag & Flags.PSET_HITMIN) != 0;
    475                 String boundary = (minimum ? param.getMin(Object.class) : param.getMax(Object.class)).toString();
    476                 String name =  (minimum ? "minimum" : "maximum");
    477                 return new Exception("Tried to set attribute \""
    478                                 + param.getId()
    479                                 + "\" in class \""
    480                                 + getId()
    481                                 + "\" to value which exceeds " + name + " ("
    482                                 + value
    483                                 + "), truncated to: "
    484                                 + boundary);
    485         }
    486        
     348
     349
    487350        protected abstract <T> void internalSet(Param param, T value);
    488351
  • java/main/src/main/java/com/framsticks/params/Util.java

    r77 r78  
    11package com.framsticks.params;
     2
     3import java.util.ArrayList;
     4import java.util.List;
    25
    36/**
     
    1417        return result.toString();
    1518    }
     19        public static List<Object> stripAccessInterface(List<AccessInterface> accesses) {
     20                List<Object> result = new ArrayList<Object>();
     21                for (AccessInterface a : accesses) {
     22                        result.add(a.getSelected());
     23                }
     24                return result;
     25        }
     26        public static int copyParams(AccessInterface to, AccessInterface from) {
     27                int copied = 0;
     28                for (Param f : from.getParams()) {
     29                        Param t = from.getParam(f.getId());
     30                        if (t == null) {
     31                                continue;
     32                        }
     33                        if (to.getClass() != f.getClass()) {
     34                                continue;
     35                        }
     36                        to.set(t, from.get(f, Object.class));
     37                        ++copied;
     38                }
     39                return copied;
     40        }
    1641}
Note: See TracChangeset for help on using the changeset viewer.