MatchOutputParser.java

  1. // @formatter:off
  2.  /*******************************************************************************
  3.  *
  4.  * This file is part of JMad.
  5.  *
  6.  * Copyright (c) 2008-2011, CERN. All rights reserved.
  7.  *
  8.  * Licensed under the Apache License, Version 2.0 (the "License");
  9.  * you may not use this file except in compliance with the License.
  10.  * You may obtain a copy of the License at
  11.  *
  12.  *     http://www.apache.org/licenses/LICENSE-2.0
  13.  *
  14.  * Unless required by applicable law or agreed to in writing, software
  15.  * distributed under the License is distributed on an "AS IS" BASIS,
  16.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17.  * See the License for the specific language governing permissions and
  18.  * limitations under the License.
  19.  *
  20.  ******************************************************************************/
  21. // @formatter:on

  22. package cern.accsoft.steering.jmad.io;

  23. import java.io.File;
  24. import java.util.List;

  25. import cern.accsoft.steering.jmad.domain.result.match.MatchResultImpl;
  26. import cern.accsoft.steering.jmad.domain.result.match.output.MadxVaryResultImpl;
  27. import cern.accsoft.steering.jmad.domain.result.match.output.MatchConstraintResultGlobal;
  28. import cern.accsoft.steering.jmad.domain.result.match.output.MatchConstraintResultLocal;
  29. import cern.accsoft.steering.jmad.kernel.task.RunMatch;
  30. import cern.accsoft.steering.jmad.util.io.TextFileParser;
  31. import cern.accsoft.steering.jmad.util.io.TextFileParserException;
  32. import cern.accsoft.steering.jmad.util.io.impl.TextFileParserImpl;

  33. /**
  34.  * This class provides an interface to read MadX Matching-Output. The read data is returned in a Result - Object.
  35.  *
  36.  * @author muellerg
  37.  */
  38. public class MatchOutputParser {

  39.     public static enum MatchingOutputTag {
  40.         // madx internal tag for the final Penalty function value //
  41.         // updated after each Matching Command
  42.         FINAL_PENALTY("TAR"), //

  43.         LOCAL_CONSTRAINTS("Local Constraints:"), //
  44.         GLOBAL_CONSTAINTS("Global Constraints:"), //

  45.         VARY_PARAMETERS("Vary Parameter Results:"), //

  46.         // Should never Occure...
  47.         NONE("not set");

  48.         private String name;

  49.         private MatchingOutputTag(String name) {
  50.             this.name = name;
  51.         }

  52.         @Override
  53.         public String toString() {
  54.             return this.name;
  55.         }

  56.         public static MatchingOutputTag getTagForLine(String line) {
  57.             for (MatchingOutputTag tag : values()) {
  58.                 if (line.equals(tag.toString())) {
  59.                     return tag;
  60.                 }
  61.             }

  62.             return null;
  63.         }
  64.     }

  65.     /* The file from where to read */
  66.     private File outPut = null;

  67.     /* The result - object, where all read Data will be stored. */
  68.     private MatchResultImpl result = null;

  69.     /**
  70.      * @param file the file to parse.
  71.      */
  72.     public MatchOutputParser(File file) {
  73.         super();
  74.         this.outPut = file;
  75.     }

  76.     /**
  77.      * Parses the file and stores the data in internal Variables.
  78.      *
  79.      * @throws MatchOutputParserException if the parsing fails
  80.      */
  81.     public void parse() throws MatchOutputParserException {

  82.         TextFileParser parser = new TextFileParserImpl();

  83.         List<String> lines;
  84.         try {
  85.             lines = parser.parse(this.outPut);
  86.         } catch (TextFileParserException e) {
  87.             throw new MatchOutputParserException("Error while parsing MadX-Matching Result file '"
  88.                     + this.outPut.getAbsolutePath() + "'.", e);
  89.         }

  90.         if (!lines.get(0).equals(MatchingOutputTag.FINAL_PENALTY.toString())) {
  91.             throw new MatchOutputParserException("Missing Final Penalty Function Value "
  92.                     + "at the beginning of Matching Output");
  93.         }

  94.         double actValue = Double.NaN;

  95.         try {
  96.             actValue = getValue(lines.get(1)) / RunMatch.FINAL_PENALTY_FACTOR;
  97.             this.result = new MatchResultImpl(actValue);
  98.         } catch (Exception ex) {
  99.             throw new MatchOutputParserException("Error retrieving Final Penalty Function Value "
  100.                     + "from Matching Output", ex);
  101.         }

  102.         MatchingOutputTag actTag = MatchingOutputTag.NONE;
  103.         MatchingOutputTag newTag = null;
  104.         String actVarName = null;

  105.         for (String line : lines.subList(2, lines.size())) {

  106.             newTag = MatchingOutputTag.getTagForLine(line);
  107.             if (newTag != null) {
  108.                 actTag = newTag;
  109.                 continue;
  110.             }

  111.             actValue = getValue(line);
  112.             actVarName = line.split("=")[0].trim();

  113.             switch (actTag) {
  114.             case LOCAL_CONSTRAINTS:
  115.                 this.result.addConstrainParameterResult(new MatchConstraintResultLocal(actVarName, actValue));
  116.                 break;
  117.             case GLOBAL_CONSTAINTS:
  118.                 this.result.addConstrainParameterResult(new MatchConstraintResultGlobal(actVarName, actValue));
  119.                 break;
  120.             case VARY_PARAMETERS:
  121.                 this.result.addVaryParameterResult(new MadxVaryResultImpl(actVarName, actValue));
  122.                 break;
  123.             default:
  124.                 throw new MatchOutputParserException("Error while parsing Matching Output "
  125.                         + "--> parse line without defined Tag");
  126.             }
  127.         }
  128.     }

  129.     private static double getValue(String line) throws MatchOutputParserException {

  130.         String[] tokens = line.split("=");
  131.         if (tokens.length < 2) {
  132.             throw new MatchOutputParserException("Could not interpret Matching Output (Value) Line --> " + line);
  133.         }

  134.         String actValueString = tokens[1].replace(";", "").trim();
  135.         double actValue = Double.NaN;

  136.         try {
  137.             actValue = Double.parseDouble(actValueString);
  138.         } catch (Exception ex) {
  139.             throw new MatchOutputParserException("Could not convert Value [" + actValueString + "] to double.", ex);
  140.         }

  141.         return actValue;
  142.     }

  143.     public MatchResultImpl getResult() {
  144.         return result;
  145.     }
  146. }