Package fr.soleil.lib.flyscan.model.parsing.configuration.section

Examples of fr.soleil.lib.flyscan.model.parsing.configuration.section.Section


        component.setBorder(BorderFactory.createLineBorder(Color.red));
    }

    protected String getErrorLocation(Entry entry, EntryContainer entryContainer) {
        FSObject fsObject = null;
        Section section = null;
        if (entryContainer instanceof FSObject) {
            fsObject = (FSObject) entryContainer;
            final EntryContainer fsObjectParent = ((FSObject) entryContainer).getParent();
            if (fsObjectParent instanceof Section) {
                section = (Section) fsObjectParent;
View Full Code Here


//            System.out.println(s);
//        }

        configName = configName.replace(ParsingUtil.UTF8_BOM, "");
        Configuration configuration = new Configuration(configName);
        Section currentSection = null;
        FSObject currentObject = null;
        Entry severalLinesEntry = null;
        String comment = null;

        Map<Actuator<?>, FSParsingException> trajectoryparsingExceptionBuffer = new HashMap<Actuator<?>, FSParsingException>();

        Section[] sections = new Section[SECTION_BUFFER_LENGTH];
        int index = 0;

        for (String sCurrentLine : content) {
            if (!sCurrentLine.trim().isEmpty()) {
                sCurrentLine = sCurrentLine.replace(ParsingUtil.UTF8_BOM, "").trim();
                if (sCurrentLine.endsWith(ParsingUtil.BRACKET_CLOSING)
                        && sCurrentLine.startsWith(ParsingUtil.BRACKET_OPENNING)) {
                    String name = sCurrentLine.replace(ParsingUtil.BRACKET_OPENNING, "")
                            .replace(ParsingUtil.BRACKET_CLOSING, "").trim();
                    // Register formerly processed object and section
                    if (currentSection != null) {
                        if (currentObject != null) {
                            currentSection.addFSObject(currentObject);
                            currentObject = null;
                        }
                        sections[index++] = currentSection;
                        if (index == SECTION_BUFFER_LENGTH) {
                            configuration.addSections(sections);
                            sections = new Section[SECTION_BUFFER_LENGTH];
                            index = 0;
                        }
//                        configuration.addSections(currentSection);
                    } else if (currentObject != null) {
                        throw new FSParsingException("Fly scan object " + currentObject.getName()
                                + " must exist within a section");
                    }

                    // Handle new section
                    if (name.equals(ParsingUtil.ACQUISITION)) {
                        currentSection = new AcquisitionSection();
                    } else if (name.equals(ParsingUtil.ACTORS)) {
                        currentSection = new ActorSection();
                    } else if (name.equals(ParsingUtil.BEHAVIOR)) {
                        currentSection = new BehaviorSection();
                    } else if (name.equals(ParsingUtil.INFO)) {
                        currentSection = new InfoSection();
                    } else if (name.equals(ParsingUtil.RECORDING)) {
                        currentSection = new RecordingSection();
                    } else {
                        currentSection = new Section(name);
                    }

                } else if (sCurrentLine.endsWith(ParsingUtil.COLON)) {
                    String name = sCurrentLine.replace(ParsingUtil.COLON, "").trim();
                    if (currentSection == null) {
                        throw new FSParsingException("Fly scan object " + name + " must exist within a section");
                    } else if (currentObject != null) {
                        // TODO VERIFIER SI TIRET FACULTATIF; SI OUI CLOTURER OBJECT CURRENTOBJECT
                        throw new FSParsingException("No end character has been found for fly scan object "
                                + currentObject.name);
                    } else {
                        // Handle new object
                        if (currentSection instanceof BehaviorSection) {
                            if (name.equals(ParsingUtil.CONTEXT_MONITOR)) {
                                currentObject = new ContextMonitorBehavior(currentSection);
                            } else if (name.equals(ParsingUtil.ACTUATOR)) {
                                currentObject = new ActuatorBehavior(currentSection);
                            } else if (name.equals(ParsingUtil.SENSOR)) {
                                currentObject = new SensorBehavior(currentSection);
                            } else if (name.equals(ParsingUtil.TIMEBASE)) {
                                currentObject = new TimeBaseBehavior(currentSection);
                            } else if (name.equals(ParsingUtil.HOOK)) {
                                currentObject = new HookBehavior(currentSection);
                            } else if (name.equals(ParsingUtil.MONITOR)) {
                                currentObject = new MonitorBehavior(currentSection);
                            }
                        } else if (currentSection instanceof ActorSection) {
                            if (name.equals(ParsingUtil.CONTEXT_MONITOR)) {
                                currentObject = new ContextMonitor(currentSection);
                            } else if (name.equals(ParsingUtil.ACTUATOR)) {
                                currentObject = new StepByStepActuator(currentSection);
                            } else if (name.equals(ParsingUtil.CONTINUOUS_ACTUATOR)) {
                                currentObject = new ContinuousActuator(currentSection);
                            }
//                        else if (name.equals(ParsingUtil.CUSTOM_ACTUATOR)) {
//                            currentObject = new CustomActuator(currentSection);
//                        }
                            else if (name.equals(ParsingUtil.SENSOR)) {
                                currentObject = new Sensor(currentSection);
                            } else if (name.equals(ParsingUtil.TIMEBASE)) {
                                currentObject = new TimeBase(currentSection);
                            } else if (name.equals(ParsingUtil.HOOK)) {
                                currentObject = new Hook(currentSection);
                            } else if (name.equals(ParsingUtil.MONITOR)) {
                                currentObject = new Monitor(currentSection);
                            } else if (name.startsWith(ParsingUtil.CONSTRAINT)) {
                                // Hierarchical constraint case processing
                                currentObject = new HierarchicalConstraint(currentSection);
                                String[] parts = name.split("/");
                                if (parts.length == 3) {
                                    String type = parts[1].trim();
                                    String actorName = parts[2].trim();
                                    ((HierarchicalConstraint) currentObject).setActorName(actorName);
                                    ((HierarchicalConstraint) currentObject).setActorType(ActorType.valueOf(type));

                                }

                            }
                        } else {
                            currentObject = new FSObject(name, currentSection);
                        }
                    }

                } else if (sCurrentLine.contains(ParsingUtil.EQUALS)) {
                    String[] entryPair = sCurrentLine.split(ParsingUtil.EQUALS);
                    String key = entryPair[0].trim();
                    // Method used to prevent bug in case the value of the entry contains '=' character
                    StringBuffer valueSB = new StringBuffer();
                    for (int i = 1; i < entryPair.length; i++) {
                        valueSB.append(entryPair[i]);
                    }
                    String value = valueSB.toString().trim();

                    if ((currentSection == null) && (currentObject == null)) {
                        throw new FSParsingException("Entry " + key + " must exist within a section or an object");
                    } else {
                        // Handle new entry
                        Entry entry = null;
                        if (key.startsWith(ParsingUtil.PARAMETER + ParsingUtil.COLON)) {
                            // Entry entry = null;
                            String[] keyParts = key.split(ParsingUtil.COLON);
                            if (keyParts.length >= 3) {
                                String name = keyParts[2].trim();
                                if (ParsingUtil.LEVEL.equals(keyParts[1].trim())) {
                                    if (currentObject != null) {
                                        entry = getExistingEntry(currentObject, name);
                                    } else if (currentSection != null) {
                                        // TODO Cas limite a tester: un parametre peut il etre specifie a la fois dans
                                        // un
                                        // object et dans son parent section?
                                        entry = getExistingEntry(currentSection, name);
                                    }
                                    if (entry != null) {
                                        entry.setExpert(true);
                                    }

                                } else {
                                    if (currentObject != null) {
                                        entry = getExistingParameterEntry(currentObject, name);
                                    } else if (currentSection != null) {
                                        // TODO Cas limite a tester: un parametre peut il etre specifie a la fois dans
                                        // un
                                        // object et dans son parent section?
                                        entry = getExistingParameterEntry(currentSection, name);
                                    }

                                    if (entry == null) {
                                        entry = new ParameterEntry();
                                    }

                                    entry.setKey(name);
                                    if (ParsingUtil.VALUE.equals(keyParts[1].trim())) {
                                        entry.setValue(value);
                                    } else if (ParsingUtil.CONSTRAINT.equals(keyParts[1].trim())) {
                                        Constraint constraint = null;
                                        if (value.startsWith(ParsingUtil.IN_LIST_KEYWORD)) {
                                            String[] lStr = value.replace(ParsingUtil.BRACKET_OPENNING, "")
                                                    .replace(ParsingUtil.BRACKET_CLOSING, "")
                                                    .replace(ParsingUtil.IN_LIST_KEYWORD, "").split(ParsingUtil.COMMA);
                                            List<Double> listDouble = new ArrayList<Double>();
                                            for (String s : lStr) {
                                                try {
                                                    Double d = Double.parseDouble(s.trim());
                                                    listDouble.add(d);

                                                } catch (NumberFormatException e) {

                                                    List<String> listStr = new ArrayList<String>();
                                                    for (String str : lStr) {
                                                        listStr.add(str);
                                                    }
                                                    constraint = new InConstraintStr(listStr);
                                                }
                                            }
                                            constraint = new InConstraint(listDouble);

                                        } else if (value.startsWith(ParsingUtil.IN_RANGE_KEYWORD)) {
                                            String[] lStr = value.replace(ParsingUtil.BRACKET_OPENNING, "")
                                                    .replace(ParsingUtil.BRACKET_CLOSING, "")
                                                    .replace(ParsingUtil.IN_RANGE_KEYWORD, "").split(ParsingUtil.COMMA);
                                            if (lStr.length != 2) {
                                                throw new FSParsingException(
                                                        "Can not parse in range constraint value: " + value);
                                            }
                                            try {
                                                Double min = Double.parseDouble(lStr[0].trim());
                                                Double max = Double.parseDouble(lStr[1].trim());
                                                constraint = new MinMaxConstraint(min, max);
                                            } catch (NumberFormatException e) {
                                                throw new FSParsingException(
                                                        "Failed to parse number value for parameter: " + key
                                                                + " (value=" + value + ")", e);
                                            }

                                        } else if (ParsingUtil.CONSTANT_KEYWORD.equals(value)) {
                                            constraint = new ConstantConstraint();
                                        }
                                        if (constraint != null && (entry instanceof ParameterEntry)) {
                                            ((ParameterEntry) entry).addConstraint(constraint);
                                        }

                                    }

                                    if (entry != null) {
                                        registerEntry(entry, currentObject, currentSection);
                                    }

                                }

                            }

                        } else if ((currentSection instanceof AcquisitionSection) && (currentObject == null)) {

                            if (key.equals(ParsingUtil.CONTINUOUS_RANGES) || key.equals(ParsingUtil.PERIOD)) {
                                entry = new PredefinedEntry(key, value, EntryType.INT);
                                registerEntry(entry, currentObject, currentSection);
                            } else if (key.equals(ParsingUtil.INFINITE_MODE) || key.equals(ParsingUtil.MANUAL_MODE)
                                    || key.equals(ParsingUtil.ZIGZAG)) {
                                entry = new PredefinedEntry(key, value, EntryType.BOOL);
                                registerEntry(entry, currentObject, currentSection);
                            } else if (key.equals(ParsingUtil.SCAN_DIMENSIONS)) {
                                entry = new PredefinedEntry(key, value, EntryType.MATRIX);
                                registerEntry(entry, currentObject, currentSection);
                            } else if (key.equals(ParsingUtil.SCAN_MODE)) {
                                entry = new PredefinedEntry(key, value, EntryType.STRING);
                                registerEntry(entry, currentObject, currentSection);
                            } else if (key.equals(ParsingUtil.CONTINUOUS)) {
                                entry = new PredefinedEntry(key, value, EntryType.BOOL);
                                registerEntry(entry, currentObject, currentSection);
                            } else if (key.equals(ParsingUtil.DIMENSIONS)) {
                                entry = new PredefinedEntry(key, value, EntryType.MATRIX);
                                registerEntry(entry, currentObject, currentSection);
                            } else {
                                entry = new Entry(key, value, false);
                                registerEntry(entry, currentObject, currentSection);
                            }
                        } else if ((currentSection instanceof BehaviorSection) && (currentObject == null)) {
                            if (ParsingUtil.ERROR_STRATEGY.equals(key)) {
                                entry = new PredefinedEntry(key, value, EntryType.ENUM);
                                registerEntry(entry, currentObject, currentSection);
                            }
                        } else if ((currentSection instanceof InfoSection) && (currentObject == null)) {

                            if (key.equals(ParsingUtil.AUTHORS) || key.equals(ParsingUtil.COMMENT)
                                    || key.equals(ParsingUtil.CONFIG_NAME) || key.equals(ParsingUtil.CONFIG_VERSION)
                                    || key.equals(ParsingUtil.FLYSCAN_VERSION) || key.equals(ParsingUtil.KEYWORDS)
                                    || key.equals(ParsingUtil.REVISION) || key.equals(ParsingUtil.VERSION)) {
                                entry = new PredefinedEntry(key, value, EntryType.STRING);
                                registerEntry(entry, currentObject, currentSection);
                            } else {
                                entry = new Entry(key, value, false);
                                registerEntry(entry, currentObject, currentSection);
                            }
                        } else if ((currentSection instanceof RecordingSection) && (currentObject == null)) {
                            if (key.equals(ParsingUtil.SPLIT) || key.equals(ParsingUtil.MAX_SCAN_LINES_PER_FILE)
                                    || key.equals(ParsingUtil.MAX_STEPS_PER_FILE)) {
                                entry = new PredefinedEntry(key, value, EntryType.NUMBER);
                                registerEntry(entry, currentObject, currentSection);
                            }

                        } else if ((currentObject != null) && ParsingUtil.BUFFER.equals(currentObject.getName())) {
                            if (key.equals(ParsingUtil.NAME)) {
                                entry = new PredefinedEntry(key, value, EntryType.STRING);
                                registerEntry(entry, currentObject, currentSection);
                            }
                        } else if (currentObject instanceof Actor) {
                            if (key.equals(ParsingUtil.IDENTIFIER) || key.equals(ParsingUtil.NAME)) {
                                entry = new PredefinedEntry(key, value, EntryType.STRING);
                                registerEntry(entry, currentObject, currentSection);
                            } else if (key.equals(ParsingUtil.DIMENSION)) {
                                entry = new PredefinedEntry(key, value, EntryType.INT);
                                registerEntry(entry, currentObject, currentSection);
                            } else if (key.equals(ParsingUtil.ENABLE)) {
                                entry = new PredefinedEntry(key, value, EntryType.BOOL);
                                registerEntry(entry, currentObject, currentSection);
                            } else if (key.equals(ParsingUtil.TYPE)) {
                                entry = new PredefinedEntry(key, value, EntryType.STRING);
                                registerEntry(entry, currentObject, currentSection);
                            } else if (currentObject instanceof Actuator) {
                                if (key.equals(ParsingUtil.TRAJECTORY)) {

                                    TrajectoryType type = null;
                                    if (((Actuator<?>) currentObject).isCustomTrajectory()) {
                                        type = TrajectoryType.CUSTOM;
                                        entry = new PredefinedEntry(key, value, EntryType.STRING);
                                    } else {
                                        entry = new TrajectoryEntry(key, value);
                                        if (currentObject instanceof ContinuousActuator) {
                                            type = TrajectoryType.CONTINUOUS;
                                        } else if (currentObject instanceof StepByStepActuator) {
                                            type = TrajectoryType.STEP_BY_STEP;
                                        }
//                                    else {
//                                        type = TrajectoryType.CUSTOM;
//                                    }
                                        try {
                                            Trajectory trajectory = TrajectoryParser.getInstance().parse(value, type);
                                            ((TrajectoryEntry) entry).setTrajectory(trajectory);
                                        } catch (FSParsingException e) {
                                            // Bufferize FSParsingException in case of custom trajectory and
                                            // 'custom_trajectory' keyword has not been read yet
                                            trajectoryparsingExceptionBuffer.put((Actuator<?>) currentObject, e);
                                        }
                                    }

                                    registerEntry(entry, currentObject, currentSection);
                                } else if (key.equals(ParsingUtil.CUSTOM_TRAJECTORY)) {
                                    entry = new PredefinedEntry(key, value, EntryType.BOOL);
                                    registerEntry(entry, currentObject, currentSection);
                                    boolean isCustomTrajectory = Boolean.parseBoolean(value.trim());
                                    ((Actuator<?>) currentObject).setCustomTrajectory(isCustomTrajectory);

                                    if (isCustomTrajectory) {
                                        // Verify if a trajectory has been specified before and overwrite it
                                        // This is the case where trajectory entry appears prior to
                                        // custom_trajectory=true
                                        // line
                                        for (Entry e : currentObject.getEntries()) {
                                            if (e instanceof TrajectoryEntry) {
                                                String k = e.getKey();
                                                String v = e.getValue();
                                                e = new PredefinedEntry(k, v, EntryType.STRING);
                                            }
                                        }
                                        // Delete map entry related to FSParsingException raised when parsing the custom
                                        // trajectory fails
                                        trajectoryparsingExceptionBuffer.remove(currentObject);
                                    }
                                } else {
                                    entry = new Entry(key, value, false);
                                    registerEntry(entry, currentObject, currentSection);
                                }
                            } else if (currentObject instanceof Hook) {
                                if (key.equals(ParsingUtil.ACTION)) {
                                    entry = new PredefinedEntry(key, value, EntryType.ENUM);
                                    registerEntry(entry, currentObject, currentSection);
                                }
                            } else {
                                entry = new Entry(key, value, false);
                                registerEntry(entry, currentObject, currentSection);
                            }
                        } else if (currentObject != null && currentObject instanceof BehaviorObject) {
                            if (ParsingUtil.DELAY.equals(key) || ParsingUtil.TIMEOUT.equals(key)) {
                                entry = new PredefinedEntry(key, value, EntryType.INT);
                                registerEntry(entry, currentObject, currentSection);
                            } else if (ParsingUtil.ERROR_STRATEGY.equals(key)) {
                                entry = new PredefinedEntry(key, value, EntryType.ENUM);
                                registerEntry(entry, currentObject, currentSection);
                            } else {
                                entry = new Entry(key, value, false);
                                registerEntry(entry, currentObject, currentSection);
                            }
                        } else if (currentObject != null && currentObject instanceof HierarchicalConstraint) {
                            // TODO syntaxe des contraintes hiérarchiques à clarifier
                            if (key.endsWith("enum")) {
                                List<String> possibleValues = Arrays.asList(value.replaceAll(" ", "").split(","));
                                Constraint inConstraint = new InConstraintStr(possibleValues);
                                ((HierarchicalConstraint) currentObject).setConstraint(inConstraint);
                            } else if (key.endsWith(ParsingUtil.VALUE)) {
                                String constant = value.trim();
                                Constraint constantConstraint = new ConstantConstraintStr(constant);
                                ((HierarchicalConstraint) currentObject).setPrerequisite(constantConstraint);
                            } else if (key.endsWith("min")) {
                                Constraint constraint = ((HierarchicalConstraint) currentObject).getConstraint();
                                if (constraint == null) {
                                    constraint = new MinMaxConstraint();
                                }
                                ((MinMaxConstraint) constraint).setMin(Double.parseDouble(value.trim()));
                                ((HierarchicalConstraint) currentObject).setConstraint(constraint);
                            } else if (key.endsWith("max")) {
                                Constraint constraint = ((HierarchicalConstraint) currentObject).getConstraint();
                                if (constraint == null) {
                                    constraint = new MinMaxConstraint();
                                }
                                ((MinMaxConstraint) constraint).setMax(Double.parseDouble(value.trim()));
                                ((HierarchicalConstraint) currentObject).setConstraint(constraint);
                            }
                        } else {

                            /*Entry */entry = new Entry(key, value, false);
                            registerEntry(entry, currentObject, currentSection);

                        }

                        if (entry != null && comment != null) {
                            entry.setComment(comment);
                            comment = null;
                        }

                        if (value.endsWith("\\")) {
                            severalLinesEntry = entry;
                        }

                    }

                } else if (sCurrentLine.trim().equals(ParsingUtil.HYPHEN)) {
                    // Register formerly processed object
                    currentSection.addFSObject(currentObject);
                    if (currentObject instanceof Actuator) {
                        // If a FSParsingException has been catched while trying to parse a trajectory, throw it if
                        // custom_trajectory attribute value is false
                        FSParsingException fsParsingException = trajectoryparsingExceptionBuffer.get(currentObject);
                        if (fsParsingException != null && !((Actuator<?>) currentObject).isCustomTrajectory()) {
                            throw fsParsingException;
                        }
                    }
                    // Release currentObject object
                    currentObject = null;
                } else if (sCurrentLine.trim().startsWith("#")) {
                    comment = sCurrentLine.trim().replace("#", "");
                } else {

                    if (severalLinesEntry != null) {
                        String value = severalLinesEntry.getValue();
                        value = value + sCurrentLine.trim();
                        value = value.replace("\\", " ");
                        severalLinesEntry.setValue(value);
                        registerEntry(severalLinesEntry, currentObject, currentSection);
                        if (!sCurrentLine.trim().endsWith("\\")) {
                            severalLinesEntry = null;
                        }
                    }

                }
            }
        }

        if (currentSection != null) {
            if (currentObject != null) {
                currentSection.addFSObject(currentObject);
                currentObject = null;
            }
            sections[index++] = currentSection;
            if (index == SECTION_BUFFER_LENGTH) {
                configuration.addSections(sections);
View Full Code Here

TOP

Related Classes of fr.soleil.lib.flyscan.model.parsing.configuration.section.Section

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.