| summaryrefslogtreecommitdiff | 
diff options
Diffstat (limited to 'sol-pretty-printer/src')
| -rw-r--r-- | sol-pretty-printer/src/Main.java | 144 | ||||
| -rw-r--r-- | sol-pretty-printer/src/Models.java | 248 | ||||
| -rw-r--r-- | sol-pretty-printer/src/Parameters.java | 139 | ||||
| -rw-r--r-- | sol-pretty-printer/src/QuickParser.java | 58 | ||||
| -rw-r--r-- | sol-pretty-printer/src/QuickSolParser.java | 117 | ||||
| -rw-r--r-- | sol-pretty-printer/src/SolutionItem.java | 82 | ||||
| -rw-r--r-- | sol-pretty-printer/src/Solutions.java | 96 | ||||
| -rw-r--r-- | sol-pretty-printer/src/Strings.java | 89 | ||||
| -rw-r--r-- | sol-pretty-printer/src/Waveforms.java | 40 | 
9 files changed, 1013 insertions, 0 deletions
| diff --git a/sol-pretty-printer/src/Main.java b/sol-pretty-printer/src/Main.java new file mode 100644 index 0000000..7a7bfcd --- /dev/null +++ b/sol-pretty-printer/src/Main.java @@ -0,0 +1,144 @@ +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); + +      if (!PARAMETERS.are_valid()) +      { +         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 new file mode 100644 index 0000000..92e9074 --- /dev/null +++ b/sol-pretty-printer/src/Parameters.java @@ -0,0 +1,139 @@ +import java.util.List; +import java.util.ArrayList; + +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; + +   public static void print_usage () +   { +      System.out.println +      ( +         "Sol-Pretty-Printer\n" +         + "USAGE:\n" +         + "\tjava Main <MODEL_FILE|MAP_FILE>+ <SOL_AND_PP_FILES>+\n" +         + "PARAMETERS:\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" +      ); +   } + +   public Parameters (final String... args) +   { +      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>(); + +      if (args.length < 2) +      { +         print_usage(); + +         are_valid = false; + +         return; +      } + +      has_error = false; +      prev_was_a_sol = false; + +      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]); + +            if (prev_was_a_sol) +            { +               System.err.println +               ( +                  "[F] Two solution files followed one another. You must give" +                  + "<SOLUTION_FILE> <PRETTY_PRINT_FILE> pairs as parameters." +               ); + +               System.exit(-1); +            } + +            prev_was_a_sol = true; +         } +         else if (args[i].endsWith(".pp")) +         { +            if (!prev_was_a_sol) +            { +               System.err.println +               ( +                  "[F] Two pretty print files followed one another. You must" +                  + " give <SOLUTION_FILE> <PRETTY_PRINT_FILE> pairs as" +                  + " parameters." +               ); + +               System.exit(-1); +            } + +            pp_files.add(args[i]); + +            prev_was_a_sol = false; +         } +         else +         { +            System.err.println +            ( +               "[E] Unknown file type \"" +               + args[i] +               + "\"." +            ); + +            has_error = true; +         } +      } + +      are_valid = !has_error; +   } + +   public List<String> get_map_files () +   { +      return map_files; +   } + +   public List<String> get_model_files () +   { +      return model_files; +   } + +   public List<String> get_solution_files () +   { +      return sol_files; +   } + +   public List<String> get_pretty_print_files () +   { +      return pp_files; +   } + +   public boolean are_valid () +   { +      return are_valid; +   } +} diff --git a/sol-pretty-printer/src/QuickParser.java b/sol-pretty-printer/src/QuickParser.java new file mode 100644 index 0000000..19d29e7 --- /dev/null +++ b/sol-pretty-printer/src/QuickParser.java @@ -0,0 +1,58 @@ +/* FIXME: Finer imports */ +import java.io.*; +import java.util.regex.*; +import java.util.*; + +public class QuickParser +{ +   private static final Pattern instr_pattern; +   private final BufferedReader buffered_reader; + +   static +   { +      instr_pattern = Pattern.compile("\\((?<list>[^)]+)\\)"); +   } +   public QuickParser (final String filename) +   throws FileNotFoundException +   { +      buffered_reader = new BufferedReader(new FileReader(filename)); +   } + +   public void finalize () +   throws IOException +   { +      buffered_reader.close(); +   } + +   public String[] parse_line () +   throws IOException +   { +      final List<String> result; +      final Matcher matcher; +      String line; + +      do +      { +         line = buffered_reader.readLine(); + +         if (line == null) +         { +            return new String[0]; +         } + +         line = line.replaceAll("\\s+"," "); +      } +      while (line.length() < 3 || line.startsWith(";")); + +      matcher = instr_pattern.matcher(line); + +      if (!matcher.find()) +      { +         System.err.println("[E] Invalid instruction \"" + line + "\""); + +         return null; +      } + +      return matcher.group(1).split(" |\t"); +   } +} 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 new file mode 100644 index 0000000..f251b62 --- /dev/null +++ b/sol-pretty-printer/src/SolutionItem.java @@ -0,0 +1,82 @@ +import java.util.Map; +import java.util.HashMap; +import java.util.Set; + +public class SolutionItem +{ +   private static final Map<String, SolutionItem> FROM_ID; + +   static +   { +      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 +   ( +      final String function, +      final String id, +      final String value +   ) +   { +      SolutionItem si; + +      si = FROM_ID.get(id); + +      if (si == null) +      { +         si = new SolutionItem(id); + +         FROM_ID.put(id, si); +      } + +      si.function_values.put(function.toLowerCase(), value); +   } + +   public static SolutionItem get_item_from_id (final String id) +   { +      return FROM_ID.get(id); +   } + +   /** Non-Static *************************************************************/ +   private final Map<String, String> function_values; + +   private SolutionItem +   ( +      final String id +   ) +   { +      function_values = new HashMap<String, String>(); + +      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) +   { +      return function_values.get(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; +   } +} diff --git a/sol-pretty-printer/src/Waveforms.java b/sol-pretty-printer/src/Waveforms.java new file mode 100644 index 0000000..3a40869 --- /dev/null +++ b/sol-pretty-printer/src/Waveforms.java @@ -0,0 +1,40 @@ +import java.util.Map; +import java.util.HashMap; + +public class Waveforms +{ +   private static final Map<String, String> FROM_WAVEFORM; + +   static +   { +      FROM_WAVEFORM = new HashMap<String, String>(); +   } + +   private Waveforms () {} /* Utility class. */ + +   public static void register_map (final String wfm_id, final String elem_id) +   { +      FROM_WAVEFORM.put(wfm_id, elem_id); +   } + +   public static String get_id_from_waveform_id (final String wfm_id) +   { +      final String result; + +      result = FROM_WAVEFORM.get(wfm_id); + +      if (result == null) +      { +         System.err.println +         ( +            "[F] There is no element associated with waveform \"" +            + wfm_id +            + "\". Is the model complete?" +         ); + +         System.exit(-1); +      } + +      return result; +   } +} | 


