| summaryrefslogtreecommitdiff | 
diff options
| author | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2017-08-03 11:10:55 +0200 | 
|---|---|---|
| committer | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2017-08-03 11:10:55 +0200 | 
| commit | c5a23ef9d6ab1e89b85016831fc8b2431f68f87f (patch) | |
| tree | fc83310559ffe00ea78f09b277fc7b2e93237947 /sol_pretty_printer/src | |
| parent | 69d904f3b2de5b914bc329af1729584f10644bf2 (diff) | |
Finishes the sol_pretty_printer, 'xcept Makefile.
Diffstat (limited to 'sol_pretty_printer/src')
| -rw-r--r-- | sol_pretty_printer/src/Main.java | 130 | ||||
| -rw-r--r-- | sol_pretty_printer/src/Models.java | 248 | ||||
| -rw-r--r-- | sol_pretty_printer/src/Parameters.java | 20 | ||||
| -rw-r--r-- | sol_pretty_printer/src/QuickParser.java | 2 | ||||
| -rw-r--r-- | sol_pretty_printer/src/QuickSolParser.java | 117 | ||||
| -rw-r--r-- | sol_pretty_printer/src/SolutionItem.java | 30 | ||||
| -rw-r--r-- | sol_pretty_printer/src/Solutions.java | 96 | ||||
| -rw-r--r-- | sol_pretty_printer/src/Strings.java | 89 | 
8 files changed, 726 insertions, 6 deletions
| diff --git a/sol_pretty_printer/src/Main.java b/sol_pretty_printer/src/Main.java index 21aebcb..7a7bfcd 100644 --- a/sol_pretty_printer/src/Main.java +++ b/sol_pretty_printer/src/Main.java @@ -1,7 +1,125 @@ +import java.util.List; + +import java.io.FileNotFoundException; +import java.io.IOException; +  public class Main  {     private static Parameters PARAMETERS; +   private static boolean load_model_files () +   { +      for (final String model_file: PARAMETERS.get_model_files()) +      { +         try +         { +            if (!Models.load_file(model_file)) +            { +               System.err.println +               ( +                  "[F] Something went wrong while loading the model file \"" +                  + model_file +                  + "\"" +               ); + +               return false; +            } +         } +         catch (final FileNotFoundException fnfe) +         { +            System.err.println +            ( +               "[F] Could not find model file \"" +               + model_file +               + "\"" +            ); + +            return false; +         } +      } + +      return Models.propagate_filenames(); +   } + +   private static boolean load_map_files () +   { +      for (final String map_file: PARAMETERS.get_map_files()) +      { +         try +         { +            if (!Strings.load_file(map_file)) +            { +               System.err.println +               ( +                  "[F] Something went wrong while loading the map file \"" +                  + map_file +                  + "\"" +               ); + +               return false; +            } +         } +         catch (final FileNotFoundException fnfe) +         { +            System.err.println +            ( +               "[F] Could not find map file \"" +               + map_file +               + "\"" +            ); + +            return false; +         } +      } + +      return true; +   } + +   private static void print_solutions () +   { +      final List<String> sol_files, pp_files; +      final int solutions_count; + +      sol_files = PARAMETERS.get_solution_files(); +      pp_files =  PARAMETERS.get_pretty_print_files(); + +      solutions_count = sol_files.size(); + +      if (solutions_count != pp_files.size()) +      { +         System.err.println +         ( +            "[F] Not as many solution files as pretty-print files." +         ); + +         return; +      } + +      for (int i = 0; i < solutions_count; ++i) +      { +         try +         { +            Solutions.print(sol_files.get(i), pp_files.get(i)); +         } +         catch (final IOException ioe) +         { +            System.err.println +            ( +               "[F] Something went wrong while printing the solution linked to" +               + " \"" +               + sol_files.get(i) +               + "\" and \"" +               + sol_files.get(i) +               + "\":" +            ); + +            ioe.printStackTrace(); + +            return; +         } +      } +   } +     public static void main (final String... args)     {        PARAMETERS = new Parameters(args); @@ -10,5 +128,17 @@ public class Main        {           return;        } + +      if (!load_map_files()) +      { +         return; +      } + +      if (!load_model_files()) +      { +         return; +      } + +      print_solutions();     }  } diff --git a/sol_pretty_printer/src/Models.java b/sol_pretty_printer/src/Models.java new file mode 100644 index 0000000..097e76f --- /dev/null +++ b/sol_pretty_printer/src/Models.java @@ -0,0 +1,248 @@ +import java.util.Map; +import java.util.HashMap; +import java.util.Set; + +import java.io.FileNotFoundException; +import java.io.IOException; + +public class Models +{ +   public static Map<String, String> IS_IN_FILE; +   public static Map<String, String> IS_IN_ARCHITECTURE; +   public static Map<String, String> IS_IN_ENTITY; + +   static +   { +      IS_IN_FILE = new HashMap<String, String>(); +      IS_IN_ARCHITECTURE = new HashMap<String, String>(); +      IS_IN_ENTITY = new HashMap<String, String>(); +   } + +   public static boolean load_file (final String filename) +   throws FileNotFoundException +   { +      final QuickParser qp; +      String[] input; + +      qp = new QuickParser(filename); + +      for (;;) +      { +         try +         { +            input = qp.parse_line(); + +            if (input == null) +            { +               qp.finalize(); + +               return false; +            } +            else if (input.length == 0) +            { +               qp.finalize(); + +               break; +            } +         } +         catch (final IOException e) +         { +            System.err.println +            ( +               "[E] IO error while parsing file \"" +               + filename +               + "\":" +               /* FIXME: can be null */ +               + e.getMessage() +            ); + +            return false; +         } + +         if (input[0].equals("set_function")) +         { +            handle_set_function(input); +         } +         else if (input[0].equals("is_waveform_of")) +         { +            handle_is_waveform_of(input); +         } +         else +         { +            handle_parent_predicate(input); +         } +      } + +      return true; +   } + +   private static void handle_is_waveform_of +   ( +      final String[] input +   ) +   { +      if (input.length != 3) +      { +         return; +      } + +      SolutionItem.handle_is_waveform_of +      ( +         input[1], +         input[2] +      ); +   } + +   private static void handle_set_function +   ( +      final String[] input +   ) +   { +      if (input.length != 4) +      { +         return; +      } + +      SolutionItem.handle_unary_set_function +      ( +         input[1], +         input[2], +         input[3] +      ); +   } + +   private static void handle_parent_predicate +   ( +      final String[] input +   ) +   { +      if (input.length != 3) +      { +         return; +      } + +      if (input[0].equals("is_in_file")) +      { +         IS_IN_FILE.put(input[1], input[2]); +      } +      else if (input[0].equals("belongs_to_architecture")) +      { +         IS_IN_ARCHITECTURE.put(input[1], input[2]); +      } +      else if +      ( +         input[0].equals("is_port_of") +         || input[0].equals("is_generic_of") +      ) +      { +         IS_IN_ENTITY.put(input[1], input[2]); +      } +   } + +   public static boolean propagate_filenames () +   { +      for (final Map.Entry<String, String> file_ref: IS_IN_FILE.entrySet()) +      { +         final SolutionItem file_si; + +         file_si = +            SolutionItem.get_item_from_id +            ( +               file_ref.getValue() +            ); + +         if (file_si == null) +         { +            System.err.println +            ( +               "[E] Can't find any file with id \"" +               + file_ref.getValue() +               + "\", yet the item with id \"" +               + file_ref.getKey() +               + "\" is supposed to be in it." +            ); + +            return false; +         } + +         SolutionItem.handle_unary_set_function +         ( +            "file", +            file_ref.getKey(), +            file_si.get_function_value("filename") +         ); +      } + +      for +      ( +         final Map.Entry<String, String> arch_ref: IS_IN_ARCHITECTURE.entrySet() +      ) +      { +         final SolutionItem arch_si; + +         arch_si = +            SolutionItem.get_item_from_id +            ( +               arch_ref.getValue() +            ); + +         if (arch_si == null) +         { +            System.err.println +            ( +               "[E] Can't find any architecture with id \"" +               + arch_ref.getValue() +               + "\", yet the item with id \"" +               + arch_ref.getKey() +               + "\" is supposed to be in it." +            ); + +            return false; +         } + +         SolutionItem.handle_unary_set_function +         ( +            "file", +            arch_ref.getKey(), +            arch_si.get_function_value("file") +         ); +      } + +      for +      ( +         final Map.Entry<String, String> entity_ref: IS_IN_ENTITY.entrySet() +      ) +      { +         final SolutionItem entity_si; + +         entity_si = +            SolutionItem.get_item_from_id +            ( +               entity_ref.getValue() +            ); + +         if (entity_si == null) +         { +            System.err.println +            ( +               "[E] Can't find any entity with id \"" +               + entity_ref.getValue() +               + "\", yet the item with id \"" +               + entity_ref.getKey() +               + "\" is supposed to be in it." +            ); + +            return false; +         } + +         SolutionItem.handle_unary_set_function +         ( +            "file", +            entity_ref.getKey(), +            entity_si.get_function_value("file") +         ); +      } + +      return true; +   } +} diff --git a/sol_pretty_printer/src/Parameters.java b/sol_pretty_printer/src/Parameters.java index 6d64e27..92e9074 100644 --- a/sol_pretty_printer/src/Parameters.java +++ b/sol_pretty_printer/src/Parameters.java @@ -6,6 +6,7 @@ public class Parameters     private final List<String> sol_files;     private final List<String> pp_files;     private final List<String> model_files; +   private final List<String> map_files;     private final boolean are_valid; @@ -15,14 +16,15 @@ public class Parameters        (           "Sol-Pretty-Printer\n"           + "USAGE:\n" -         + "\tjava Main <MODEL_FILE>+ <SOL_AND_PP_FILES>+\n" +         + "\tjava Main <MODEL_FILE|MAP_FILE>+ <SOL_AND_PP_FILES>+\n"           + "PARAMETERS:\n" -         + "\t- <MODEL_FILE>\tInstr. model files.\n" +         + "\t- <MODEL_FILE|MAP_FILE>\tInstr. model files, string map files.\n"           + "\t- <SOL_AND_PP_FILES>\tOne solution file, followed by one pretty"           + " print file."           + "NOTES:\n"           + "\t- Model files have a \".mod\" extension.\n"           + "\t- Solution files have a \".sol\" extension.\n" +         + "\t- Map files have a \".map\" extension.\n"           + "\t- Pretty-print files have a \".pp\" extension.\n"           + "\t- Solution files may contain any number of solutions.\n"        ); @@ -32,6 +34,7 @@ public class Parameters     {        boolean has_error, prev_was_a_sol; +      map_files = new ArrayList<String>();        model_files = new ArrayList<String>();        sol_files = new ArrayList<String>();        pp_files = new ArrayList<String>(); @@ -48,12 +51,16 @@ public class Parameters        has_error = false;        prev_was_a_sol = false; -      for (int i = 1; i < args.length; ++i) +      for (int i = 0; i < args.length; ++i)        {           if (args[i].endsWith(".mod"))           {              model_files.add(args[i]);           } +         else if (args[i].endsWith(".map")) +         { +            map_files.add(args[i]); +         }           else if (args[i].endsWith(".sol"))           {              sol_files.add(args[i]); @@ -85,6 +92,8 @@ public class Parameters                 System.exit(-1);              } +            pp_files.add(args[i]); +              prev_was_a_sol = false;           }           else @@ -103,6 +112,11 @@ public class Parameters        are_valid = !has_error;     } +   public List<String> get_map_files () +   { +      return map_files; +   } +     public List<String> get_model_files ()     {        return model_files; diff --git a/sol_pretty_printer/src/QuickParser.java b/sol_pretty_printer/src/QuickParser.java index 47cea27..19d29e7 100644 --- a/sol_pretty_printer/src/QuickParser.java +++ b/sol_pretty_printer/src/QuickParser.java @@ -10,7 +10,7 @@ public class QuickParser     static     { -      instr_pattern = Pattern.compile("\\((?<list>[a-z_0-9 \"]+)\\)"); +      instr_pattern = Pattern.compile("\\((?<list>[^)]+)\\)");     }     public QuickParser (final String filename)     throws FileNotFoundException diff --git a/sol_pretty_printer/src/QuickSolParser.java b/sol_pretty_printer/src/QuickSolParser.java new file mode 100644 index 0000000..e162096 --- /dev/null +++ b/sol_pretty_printer/src/QuickSolParser.java @@ -0,0 +1,117 @@ +/* FIXME: Finer imports */ +import java.io.*; +import java.util.regex.*; +import java.util.*; + +public class QuickSolParser +{ +   private static final Pattern SOL_ITEM_PATTERN; +   private final BufferedReader buffered_reader; + +   static +   { +      SOL_ITEM_PATTERN = Pattern.compile("\\((?<list>[a-zA-Z_0-9 \t]+)\\)"); +   } +   public QuickSolParser (final String filename) +   throws FileNotFoundException +   { +      buffered_reader = new BufferedReader(new FileReader(filename)); +   } + +   public void finalize () +   throws IOException +   { +      buffered_reader.close(); +   } + +   public List<String[]> next_solution () +   throws IOException +   { +      final List<String[]> result; +      final Matcher matcher; +      boolean has_started_sol; +      String line; + +      result = new ArrayList<String[]>(); +      has_started_sol = false; + +      matcher = SOL_ITEM_PATTERN.matcher(""); + +      for (;;) +      { +         line = buffered_reader.readLine(); + +         if (line == null) +         { +            return null; +         } + +         line = line.replaceAll("\\s+"," "); + +         if (line.equals(")")) +         { +            if (!has_started_sol) +            { +               throw +                  new IOException +                  ( +                     "[E] Incorrect solution structure. (found a \")\" before a" +                     + " \"(solution\"" +                  ); +            } + +            return result; +         } +         else if (line.equals("(solution")) +         { +            if (has_started_sol) +            { +               throw +                  new IOException +                  ( +                     "[E] Incorrect solution structure. (found a second" +                     + "\"(solution\" before the \")\" ending the previous one." +                  ); +            } + +            has_started_sol = true; +         } +         else if (line.startsWith(";") || line.length() < 3) +         { +            continue; +         } +         else +         { +            final String[] item; + +            matcher.reset(line); + +            if (!matcher.find()) +            { +               throw +                  new IOException +                  ( +                     "[E] Incorrect solution structure. \"" +                     + line +                     + "\" does not form a correct solution item." +                  ); +            } + +            item = matcher.group(1).split(" |\t"); + +            if (item.length != 3) +            { +               throw +                  new IOException +                  ( +                     "[E] Incorrect solution item. \"" +                     + line +                     + "\" should match the form \"(NAME ID TAG)\"." +                  ); +            } + +            result.add(item); +         } +      } +   } +} diff --git a/sol_pretty_printer/src/SolutionItem.java b/sol_pretty_printer/src/SolutionItem.java index 122dd04..f251b62 100644 --- a/sol_pretty_printer/src/SolutionItem.java +++ b/sol_pretty_printer/src/SolutionItem.java @@ -1,5 +1,6 @@  import java.util.Map;  import java.util.HashMap; +import java.util.Set;  public class SolutionItem  { @@ -10,6 +11,25 @@ public class SolutionItem        FROM_ID = new HashMap<String, SolutionItem>();     } +   public static void handle_is_waveform_of +   ( +      final String wfm_id, +      final String origin_id +   ) +   { +      SolutionItem si; + +      si = FROM_ID.get(origin_id); + +      if (si == null) +      { +         si = new SolutionItem(origin_id); + +         FROM_ID.put(origin_id, si); +      } + +      FROM_ID.put(wfm_id, si); +   }     public static void handle_unary_set_function     ( @@ -22,7 +42,7 @@ public class SolutionItem        si = FROM_ID.get(id); -      if (id == null) +      if (si == null)        {           si = new SolutionItem(id); @@ -46,7 +66,13 @@ public class SolutionItem     )     {        function_values = new HashMap<String, String>(); -      function_values.put("id", id); + +      function_values.put("ID", id); +   } + +   public Set<Map.Entry<String, String>> get_functions_data () +   { +      return function_values.entrySet();     }     public String get_function_value (final String fun) diff --git a/sol_pretty_printer/src/Solutions.java b/sol_pretty_printer/src/Solutions.java new file mode 100644 index 0000000..38a426a --- /dev/null +++ b/sol_pretty_printer/src/Solutions.java @@ -0,0 +1,96 @@ +import java.util.Map; +import java.util.List; + +import java.io.IOException; + +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.charset.StandardCharsets; + + +public class Solutions +{ +   private static String load_file (final String filename) +   throws IOException +   { +      return +         new String +         ( +            Files.readAllBytes(Paths.get(filename)), +            StandardCharsets.UTF_8 +         ); +   } + +   public static boolean print (final String sol_file, final String pp_file) +   throws IOException +   { +      final String pp_content; +      final QuickSolParser qsp; +      List<String[]> solution; + +      pp_content = load_file(pp_file); + +      qsp = new QuickSolParser(sol_file); + +      for (;;) +      { +         solution = qsp.next_solution(); + +         if (solution == null) +         { +            return true; +         } + +         if (!handle_solution(solution, pp_content)) +         { +            return false; +         } +      } +   } + +   private static boolean handle_solution +   ( +      final List<String[]> solution, +      String pp_content +   ) +   { +      for (final String[] sol_data: solution) +      { +         final SolutionItem si; + +         si = SolutionItem.get_item_from_id(sol_data[1]); + +         if (si == null) +         { +            System.err.println +            ( +               "[E] There is no element in the model with an ID of \"" +               + sol_data[1] +               + "\", yet the solution file refers to it." +            ); + +            return false; +         } + +         for (final Map.Entry<String, String> me: si.get_functions_data()) +         { +            pp_content = +               pp_content.replace +               ( +                  ( +                     "$" +                     + sol_data[0] +                     + "." +                     + me.getKey().toUpperCase() +                     + "$" +                  ), +                  Strings.get_string_from_id(me.getValue()) +               ); +         } +      } + +      System.out.println(pp_content); + +      return true; +   } +} diff --git a/sol_pretty_printer/src/Strings.java b/sol_pretty_printer/src/Strings.java new file mode 100644 index 0000000..31f6145 --- /dev/null +++ b/sol_pretty_printer/src/Strings.java @@ -0,0 +1,89 @@ +import java.io.FileNotFoundException; +import java.io.IOException; + +import java.util.Map; +import java.util.HashMap; + +public class Strings +{ +   private static final Map<String, String> FROM_ID; + +   static +   { +      FROM_ID = new HashMap<String, String>(); +   } + +   private static void add_mapping (final String id, final String str) +   { +      FROM_ID.put(id, str); +   } + +   public static String get_string_from_id (final String id) +   { +      return FROM_ID.get(id); +   } + +   private static boolean handle_mapping_instruction (final String... instr) +   { +      if (instr.length < 3) +      { +         return false; +      } + +      if (!instr[0].equals("string->instr")) +      { +         return false; +      } + +      add_mapping(instr[2], instr[1]); + +      return true; +   } + +   public static boolean load_file (final String filename) +   throws FileNotFoundException +   { +      final QuickParser qp; +      String[] input; + +      qp = new QuickParser(filename); + +      for (;;) +      { +         try +         { +            input = qp.parse_line(); + +            if (input == null) +            { +               qp.finalize(); + +               return false; +            } +            else if (input.length == 0) +            { +               qp.finalize(); + +               break; +            } +         } +         catch (final IOException e) +         { +            System.err.println +            ( +               "[E] IO error while parsing file \"" +               + filename +               + "\":" +               /* FIXME: can be null */ +               + e.getMessage() +            ); + +            return false; +         } + +         handle_mapping_instruction(input); +      } + +      return true; +   } +} | 


