From 90bb7e959496c3a12bebe055f6344b9f06f22809 Mon Sep 17 00:00:00 2001 From: Nathanael Sensfelder Date: Thu, 3 Aug 2017 15:28:17 +0200 Subject: Improving clarity through better Makefiles. --- Makefile | 34 +++ ast-to-instr/Makefile | 59 +++-- ast-to-instr/src/Main.java | 11 +- ast-to-instr/src/OutputFile.java | 8 +- ast-to-instr/src/Parameters.java | 18 +- cfg-to-paths/Makefile | 35 --- cfg-to-paths/src/ControlFlow.java | 142 ------------- cfg-to-paths/src/Main.java | 114 ---------- cfg-to-paths/src/Node.java | 104 --------- cfg-to-paths/src/Parameters.java | 72 ------- cfg-to-paths/src/Path.java | 102 --------- cfg-to-paths/src/QuickParser.java | 58 ----- instr-to-kodkod/Makefile | 138 ++++++++---- instr-to-kodkod/cfg-to-paths/Makefile | 35 +++ instr-to-kodkod/cfg-to-paths/src/ControlFlow.java | 142 +++++++++++++ instr-to-kodkod/cfg-to-paths/src/Main.java | 114 ++++++++++ instr-to-kodkod/cfg-to-paths/src/Node.java | 104 +++++++++ instr-to-kodkod/cfg-to-paths/src/Parameters.java | 72 +++++++ instr-to-kodkod/cfg-to-paths/src/Path.java | 102 +++++++++ instr-to-kodkod/cfg-to-paths/src/QuickParser.java | 58 +++++ sol-pretty-printer/src/Main.java | 144 +++++++++++++ sol-pretty-printer/src/Models.java | 248 ++++++++++++++++++++++ sol-pretty-printer/src/Parameters.java | 139 ++++++++++++ sol-pretty-printer/src/QuickParser.java | 58 +++++ sol-pretty-printer/src/QuickSolParser.java | 117 ++++++++++ sol-pretty-printer/src/SolutionItem.java | 82 +++++++ sol-pretty-printer/src/Solutions.java | 96 +++++++++ sol-pretty-printer/src/Strings.java | 89 ++++++++ sol-pretty-printer/src/Waveforms.java | 40 ++++ sol_pretty_printer/src/Main.java | 144 ------------- sol_pretty_printer/src/Models.java | 248 ---------------------- sol_pretty_printer/src/Parameters.java | 139 ------------ sol_pretty_printer/src/QuickParser.java | 58 ----- sol_pretty_printer/src/QuickSolParser.java | 117 ---------- sol_pretty_printer/src/SolutionItem.java | 82 ------- sol_pretty_printer/src/Solutions.java | 96 --------- sol_pretty_printer/src/Strings.java | 89 -------- sol_pretty_printer/src/Waveforms.java | 40 ---- 38 files changed, 1841 insertions(+), 1707 deletions(-) create mode 100644 Makefile delete mode 100644 cfg-to-paths/Makefile delete mode 100644 cfg-to-paths/src/ControlFlow.java delete mode 100644 cfg-to-paths/src/Main.java delete mode 100644 cfg-to-paths/src/Node.java delete mode 100644 cfg-to-paths/src/Parameters.java delete mode 100644 cfg-to-paths/src/Path.java delete mode 100644 cfg-to-paths/src/QuickParser.java create mode 100644 instr-to-kodkod/cfg-to-paths/Makefile create mode 100644 instr-to-kodkod/cfg-to-paths/src/ControlFlow.java create mode 100644 instr-to-kodkod/cfg-to-paths/src/Main.java create mode 100644 instr-to-kodkod/cfg-to-paths/src/Node.java create mode 100644 instr-to-kodkod/cfg-to-paths/src/Parameters.java create mode 100644 instr-to-kodkod/cfg-to-paths/src/Path.java create mode 100644 instr-to-kodkod/cfg-to-paths/src/QuickParser.java create mode 100644 sol-pretty-printer/src/Main.java create mode 100644 sol-pretty-printer/src/Models.java create mode 100644 sol-pretty-printer/src/Parameters.java create mode 100644 sol-pretty-printer/src/QuickParser.java create mode 100644 sol-pretty-printer/src/QuickSolParser.java create mode 100644 sol-pretty-printer/src/SolutionItem.java create mode 100644 sol-pretty-printer/src/Solutions.java create mode 100644 sol-pretty-printer/src/Strings.java create mode 100644 sol-pretty-printer/src/Waveforms.java delete mode 100644 sol_pretty_printer/src/Main.java delete mode 100644 sol_pretty_printer/src/Models.java delete mode 100644 sol_pretty_printer/src/Parameters.java delete mode 100644 sol_pretty_printer/src/QuickParser.java delete mode 100644 sol_pretty_printer/src/QuickSolParser.java delete mode 100644 sol_pretty_printer/src/SolutionItem.java delete mode 100644 sol_pretty_printer/src/Solutions.java delete mode 100644 sol_pretty_printer/src/Strings.java delete mode 100644 sol_pretty_printer/src/Waveforms.java diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4846880 --- /dev/null +++ b/Makefile @@ -0,0 +1,34 @@ +## Makefile Parameters ######################################################### +LEVEL_FILES = $(wildcard $(PWD)/data/level/*.lvl) +PROPERTY_FILES = $(wildcard $(PWD)/data/property/*.pro) +AST_FILE = $(PWD)/data/ast/best_chronometer_ever.xml + +TMP_DIR = /tmp/tabellion +MODEL_DIR = $(TMP_DIR)/mod +SOL_DIR = $(TMP_DIR)/sol + +## Sub-programs ################################################################ +AST_TO_INSTR = ast-to-instr +SOLVER = instr-to-kodkod +PRETTY_PRINTER = sol-pretty-printer + +export + +run: $(TMP_DIR) $(MODEL_DIR) $(SOL_DIR) + $(MAKE) -C $(AST_TO_INSTR) + $(MAKE) -C $(SOLVER) + $(MAKE) -C $(PRETTY_PRINTER) + +clean: + $(MAKE) -C $(AST_TO_INSTR) clean + $(MAKE) -C $(SOLVER) clean + $(MAKE) -C $(PRETTY_PRINTER) clean + +$(TMP_DIR): + mkdir -p $@ + +$(MODEL_DIR): + mkdir -p $@ + +$(SOL_DIR): + mkdir -p $@ diff --git a/ast-to-instr/Makefile b/ast-to-instr/Makefile index 4a19d4f..86a6d88 100644 --- a/ast-to-instr/Makefile +++ b/ast-to-instr/Makefile @@ -1,35 +1,60 @@ -INPUT_FILE = ../data/ast/best_chronometer_ever.xml +## Parameters ################################################################## +#### GHDL's xml AST output. +ifndef AST_FILE +AST_FILE = +endif + +#### Where to output the models. +ifndef MODEL_DIR +MODEL_DIR = +endif + +#### Binaries +###### JRE binary +ifndef JAVA +JAVA = java +endif -## Executables ################################################################# +###### JDK binary +ifndef JAVAC JAVAC = javac -JAVA = java -DOWNLOADER = wget +endif + +## Parameters Sanity Check ##################################################### +ifeq ($(strip $(AST_FILE)),) +$(error No AST_FILE defined as parameter.) +endif + +ifeq ($(strip $(MODEL_DIR)),) +$(error No output directory defined for the models.) +endif + +ifeq ($(strip $(JAVA)),) +$(error No Java executable defined as parameter.) +endif + +ifeq ($(strip $(JAVAC)),) +$(error No Java compiler defined as parameter.) +endif ## Java Config ################################################################# CLASSPATH = "./src/" -## Dependencies ################################################################ -JAR_SOURCE = https://noot-noot.org/onera_2017/jar/ -REQUIRED_JARS = - ## Makefile Magic ############################################################## SOURCES = $(wildcard src/*.java) CLASSES = $(SOURCES:.java=.class) ## Makefile Rules ############################################################## -all: structural.mod +$(MODEL_DIR)/structural.mod: $(CLASSES) $(AST_FILE) + $(JAVA) -cp $(CLASSPATH) Main $(AST_FILE) $(MODEL_DIR) clean: rm -f $(CLASSES) - rm -f *.mod - -structural.mod: $(CLASSES) $(REQUIRED_JARS) $(INPUT_FILE) - $(JAVA) -cp $(CLASSPATH) Main $(INPUT_FILE) + rm -f $(MODEL_DIR)/*.mod -%.class: %.java $(REQUIRED_JARS) +%.class: %.java $(JAVAC) -cp $(CLASSPATH) $< -%.jar: - echo "Attempting to download missing jar '$@'" - $(DOWNLOADER) "$(JAR_SOURCE)/$@" +$(MODEL_DIR): + mkdir -p $@ diff --git a/ast-to-instr/src/Main.java b/ast-to-instr/src/Main.java index 0e24f57..4f33ae1 100644 --- a/ast-to-instr/src/Main.java +++ b/ast-to-instr/src/Main.java @@ -55,11 +55,7 @@ public class Main return; } - MAIN_OUTPUT = - OutputFile.new_output_file - ( - PARAMETERS.get_main_output_filename() - ); + MAIN_OUTPUT = OutputFile.new_output_file("structural.mod"); try { @@ -176,4 +172,9 @@ public class Main { return MAIN_OUTPUT; } + + public static Parameters get_parameters () + { + return PARAMETERS; + } } diff --git a/ast-to-instr/src/OutputFile.java b/ast-to-instr/src/OutputFile.java index 42a6ad5..201ca2b 100644 --- a/ast-to-instr/src/OutputFile.java +++ b/ast-to-instr/src/OutputFile.java @@ -26,7 +26,13 @@ public class OutputFile { final OutputFile result; - result = new OutputFile(filename); + result = + new OutputFile + ( + Main.get_parameters().get_output_directory() + + "/" + + filename + ); ALL_OUTPUT_FILES.add(result); diff --git a/ast-to-instr/src/Parameters.java b/ast-to-instr/src/Parameters.java index 0407bb4..a7552d8 100644 --- a/ast-to-instr/src/Parameters.java +++ b/ast-to-instr/src/Parameters.java @@ -4,6 +4,7 @@ import java.util.ArrayList; public class Parameters { private final String xml_file; + private final String output_dir; private final boolean are_valid; @@ -13,27 +14,30 @@ public class Parameters ( "AST-to-Instr\n" + "USAGE:\n" - + "\tjava Main \n" + + "\tjava Main \n" + "PARAMETERS:\n" - + "\t- \tThe AST (XML format)." + + "\t- \tThe AST (XML format).\n" + + "\t- \tThe output directory (must already exist)." ); } public Parameters (String... args) { - if (args.length != 1) + if (args.length != 2) { print_usage(); xml_file = new String(); + output_dir = new String(); are_valid = false; return; } - are_valid = true; - xml_file = args[0]; + output_dir = args[1]; + + are_valid = true; } public String get_xml_file () @@ -41,9 +45,9 @@ public class Parameters return xml_file; } - public String get_main_output_filename() + public String get_output_directory () { - return "structural.mod"; + return output_dir; } public boolean are_valid () diff --git a/cfg-to-paths/Makefile b/cfg-to-paths/Makefile deleted file mode 100644 index c9d14be..0000000 --- a/cfg-to-paths/Makefile +++ /dev/null @@ -1,35 +0,0 @@ -## Target(s) Configuration ##################################################### -MODEL_FILES = $(wildcard ../ast-to-instr/cfg_*.mod) -OUTPUT_FILES = $(patsubst ../ast-to-instr/%.mod,%.cfg.mod,$(MODEL_FILES)) - -## Executables ################################################################# -JAVAC = javac -JAVA = java - -## Java Config ################################################################# -CLASSPATH = "./src/" - -## Makefile Magic ############################################################## -SOURCES = $(wildcard src/*.java) -CLASSES = $(SOURCES:.java=.class) - -## Makefile Rules ############################################################## - -all: $(CLASSES) $(OUTPUT_FILES) - -clean: - rm -f $(CLASSES) - rm -f *.mod - -#run: $(CLASSES) -# $(JAVA) -cp $(CLASSPATH) Main $(MODEL_FILE) $(ROOT_NODE) $(ID_PREFIX) \ -# $(OUTPUT_FILE) - -%.class: %.java - $(JAVAC) -cp $(CLASSPATH) $< - -PATTERN = 's/\(is_start_node ([0-9]+).*/\1/p' - -%.cfg.mod: ../ast-to-instr/%.mod $(CLASSES) - $(eval ROOT_NODE := $(shell cat $< | grep 'is_start_node' | sed -En $(PATTERN))) - $(JAVA) -cp $(CLASSPATH) Main $< $(ROOT_NODE) "p$(ROOT_NODE)_" $@ diff --git a/cfg-to-paths/src/ControlFlow.java b/cfg-to-paths/src/ControlFlow.java deleted file mode 100644 index 93d20ba..0000000 --- a/cfg-to-paths/src/ControlFlow.java +++ /dev/null @@ -1,142 +0,0 @@ -import java.io.*; - -public class ControlFlow -{ - public static boolean load_file - ( - final String filename - ) - throws FileNotFoundException - { - final QuickParser qp; - String[] input; - boolean success; - - 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("add_element")) - { - success = handle_add_element(input); - } - else if (input[0].equals("node_connect")) - { - success = handle_add_connect_to(input); - } - else if (input[0].equals("is_terminal")) - { - success = handle_is_terminal(input); - } - else - { - continue; - } - - if (!success) - { - System.err.println - ( - "[E] An erroneous instruction was found in file \"" - + filename - + "\"." - ); - - try - { - qp.finalize(); - } - catch (final Exception e) - { - System.err.println("[E] Additionally:"); - e.printStackTrace(); - } - - return false; - } - } - - return true; - } - - private static boolean handle_add_element - ( - final String[] input - ) - { - if (input.length != 3) - { - return false; - } - - if (!input[1].equals("node")) - { - return true; - } - - Node.handle_add_node(input[2]); - - return true; - } - - private static boolean handle_is_terminal - ( - final String[] input - ) - { - if (input.length != 2) - { - return false; - } - - Node.handle_is_terminal(input[1]); - - return true; - } - - private static boolean handle_add_connect_to - ( - final String[] input - ) - { - if ((input.length != 3)) - { - return false; - } - - return Node.handle_connect_to(input[1], input[2]); - } - - private ControlFlow () {} /* Utility Class */ -} diff --git a/cfg-to-paths/src/Main.java b/cfg-to-paths/src/Main.java deleted file mode 100644 index d5cc650..0000000 --- a/cfg-to-paths/src/Main.java +++ /dev/null @@ -1,114 +0,0 @@ -/* FIXME: Finer imports */ -import java.util.*; - -import java.io.*; - -public class Main -{ - private static Parameters PARAMETERS; - private static int path_counter = 0; - - public static void main (final String... args) - { - final FileWriter output; - final Collection all_paths; - final Collection> all_subpaths; - - PARAMETERS = new Parameters(args); - - if (!PARAMETERS.are_valid()) - { - return; - } - - try - { - ControlFlow.load_file(PARAMETERS.get_model_file()); - } - catch (final Exception e) - { - System.err.println - ( - "[E] Could not load model file \"" - + PARAMETERS.get_model_file() - + "\":" - ); - - e.printStackTrace(); - - return; - } - - all_paths = Path.get_all_paths_from(PARAMETERS.get_root_node()); - - all_subpaths = new ArrayList>(); - - for (final Path p: all_paths) - { - all_subpaths.addAll(p.get_all_subpaths()); - } - - try - { - output = new FileWriter(PARAMETERS.get_output_file()); - - for (final List tuple: all_subpaths) - { - node_tuple_to_predicates(tuple, output); - } - - output.close(); - } - catch (final Exception e) - { - System.err.println - ( - "[E] Could not write to output file \"" - + PARAMETERS.get_model_file() - + "\":" - ); - - e.printStackTrace(); - - return; - } - } - - private static void node_tuple_to_predicates - ( - final List tuple, - final FileWriter out - ) - throws IOException - { - final String id; - final int tuple_size; - - tuple_size = tuple.size(); - - id = (PARAMETERS.get_id_prefix() + path_counter); - path_counter += 1; - - out.write("(add_element path " + id + ")\n"); - out.write("(is_path_of " + id + " " + tuple.get(0) + ")\n"); - - for (int i = 0; i < tuple_size; ++i) - { - out.write("(contains_node " + id + " " + tuple.get(i) + ")\n"); - - for (int j = (i + 1); j < tuple_size; ++j) - { - out.write - ( - "(is_before " - + id - + " " - + tuple.get(i) - + " " - + tuple.get(j) - + ")\n" - ); - } - } - } -} diff --git a/cfg-to-paths/src/Node.java b/cfg-to-paths/src/Node.java deleted file mode 100644 index 3b6f2c1..0000000 --- a/cfg-to-paths/src/Node.java +++ /dev/null @@ -1,104 +0,0 @@ -import java.util.*; - -public class Node -{ - /** Static *****************************************************************/ - private static final Map NODE_FROM_STRING; - - static - { - NODE_FROM_STRING = new HashMap(); - } - - public static Node get_node (final String s) - { - return NODE_FROM_STRING.get(s); - } - - public static boolean handle_add_node (final String a) - { - if (!NODE_FROM_STRING.containsKey(a)) - { - NODE_FROM_STRING.put(a, new Node(a)); - } - - return true; - } - - public static boolean handle_is_terminal (final String a) - { - Node n; - - n = NODE_FROM_STRING.get(a); - - if (n == (Node) null) - { - n = new Node(a); - - NODE_FROM_STRING.put(a, n); - } - - n.set_as_terminal(); - - return true; - } - - public static boolean handle_connect_to (final String a, final String b) - { - final Node n_a, n_b; - - n_a = NODE_FROM_STRING.get(a); - n_b = NODE_FROM_STRING.get(b); - - if ((n_a == null) || (n_b == null)) - { - System.err.println - ( - "[E] Causality issue: Nodes \"" - + a - + "\" or \"" - + b - + "\" used but not defined." - ); - - return false; - } - n_a.next_nodes.add(n_b); - - return true; - } - - /** Non-Static *************************************************************/ - private final Collection next_nodes; - private final String name; - private boolean is_terminal; - - private Node (final String name) - { - this.name = name; - - next_nodes = new ArrayList(); - is_terminal = false; - } - - private void set_as_terminal () - { - is_terminal = true; - } - - public Collection next_nodes () - { - return next_nodes; - } - - public boolean is_terminal () - { - return is_terminal; - } - - @Override - public String toString () - { - return name; - } -} diff --git a/cfg-to-paths/src/Parameters.java b/cfg-to-paths/src/Parameters.java deleted file mode 100644 index 91303d2..0000000 --- a/cfg-to-paths/src/Parameters.java +++ /dev/null @@ -1,72 +0,0 @@ -public class Parameters -{ - private final String model_file; - private final String root_node; - private final String id_prefix; - private final String output_file; - private final boolean are_valid; - - public static void print_usage () - { - System.out.println - ( - "Instr-to-kodkod\n" - + "USAGE:\n" - + "\tjava Main \n" - + "PARAMETERS:\n" - + "\t\tInstruction file describing the model.\n" - + "\t\tID of the root node for this DAG.\n" - + "\t\tPrefix for the IDs of generated paths.\n" - + "\t\tFile in which to output the generated" - + " instructions." - ); - } - - public Parameters (final String... args) - { - if (args.length != 4) - { - print_usage(); - - model_file = new String(); - root_node = new String(); - id_prefix = new String(); - output_file = new String(); - - are_valid = false; - } - else - { - model_file = args[0]; - root_node = args[1]; - id_prefix = args[2]; - output_file = args[3]; - are_valid = true; - } - } - - public String get_model_file () - { - return model_file; - } - - public String get_root_node () - { - return root_node; - } - - public String get_id_prefix () - { - return id_prefix; - } - - public String get_output_file () - { - return output_file; - } - - public boolean are_valid () - { - return are_valid; - } -} diff --git a/cfg-to-paths/src/Path.java b/cfg-to-paths/src/Path.java deleted file mode 100644 index 9e0897f..0000000 --- a/cfg-to-paths/src/Path.java +++ /dev/null @@ -1,102 +0,0 @@ -import java.util.*; - -public class Path -{ - private final ArrayList nodes; - private final Node last_node; - - public static Collection get_all_paths_from (final String root) - { - final Collection result; - final Stack waiting_list; - final Node root_node; - - root_node = Node.get_node(root); - - if (root_node == null) - { - System.err.println - ( - "[E] Could not find root node \"" - + root - + "\"." - ); - - return null; - } - - result = new ArrayList(); - waiting_list = new Stack(); - - waiting_list.push((new Path(Node.get_node(root)))); - - while (!waiting_list.empty()) - { - final Path current_path; - final Node current_node; - final Collection next_nodes; - - current_path = waiting_list.pop(); - current_node = current_path.last_node; - next_nodes = current_node.next_nodes(); - - if (next_nodes.isEmpty()) - { - result.add(current_path); - } - else - { - if (current_node.is_terminal()) - { - result.add(current_path); - } - for (final Node next: next_nodes) - { - waiting_list.push(current_path.add_step(next)); - } - } - } - - return result; - } - - private Path (final Node start) - { - nodes = new ArrayList(); - - nodes.add(start); - - last_node = start; - } - - private Path (final ArrayList nodes, final Node last_node) - { - this.nodes = nodes; - this.last_node = last_node; - - this.nodes.add(last_node); - } - - @SuppressWarnings("unchecked") - /* 'nodes' is an ArrayList, and so should be its clone. */ - private Path add_step (final Node n) - { - return new Path((ArrayList) nodes.clone(), n); - } - - public Collection> get_all_subpaths () - { - final Collection> result; - final int path_length; - - result = new ArrayList>(); - path_length = nodes.size(); - - for (int i = 0; i < path_length; ++i) - { - result.add(nodes.subList(i, path_length)); - } - - return result; - } -} diff --git a/cfg-to-paths/src/QuickParser.java b/cfg-to-paths/src/QuickParser.java deleted file mode 100644 index 47cea27..0000000 --- a/cfg-to-paths/src/QuickParser.java +++ /dev/null @@ -1,58 +0,0 @@ -/* 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("\\((?[a-z_0-9 \"]+)\\)"); - } - 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 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/instr-to-kodkod/Makefile b/instr-to-kodkod/Makefile index 9823919..815c527 100644 --- a/instr-to-kodkod/Makefile +++ b/instr-to-kodkod/Makefile @@ -1,60 +1,124 @@ -## Target(s) Configuration ##################################################### -#MODEL_FILES = $(wildcard ../data/instructions/*.mod) -MODEL_FILES = \ - ../ast-to-instr/structural.mod \ - ../ast-to-instr/depths.mod \ - $(wildcard ../ast-to-instr/cfg_*.mod) \ - $(wildcard ../cfg-to-paths/*.mod) -MAP_FILES = $(wildcard ../ast-to-instr/*.map) -LEVEL_DIR = $(wildcard ../data/level/*.lvl) +## Parameters ################################################################## +#### Where to find the level files +ifndef LEVEL_FILES +LEVEL_FILES = +endif -PROPERTIES = $(wildcard ../data/property/cnes/*.pro) +#### Where to find the properties to verify +ifndef PROPERTY_FILES +PROPERTY_FILES = +endif -## Executables ################################################################# -JAVAC = javac +#### Where to find the model +ifndef MODEL_DIR +MODEL_DIR = +endif + +#### Where to store the CFG models +ifndef CFG_MODEL_DIR +CFG_MODEL_DIR = $(MODEL_DIR)/cfg/ +endif + +#### Where to output the solutions. +ifndef SOL_DIR +SOL_DIR = +endif + +#### Where to get the missing Jar files. +ifndef JAR_SOURCE +JAR_SOURCE = "https://noot-noot.org/tabellion/jar/" +endif + +#### Binaries +###### JRE binary +ifndef JAVA JAVA = java +endif + +###### JDK binary +ifndef JAVAC +JAVAC = javac +endif + +##### Downloader +ifndef DOWNLOADER DOWNLOADER = wget +endif -## Java Config ################################################################# -CLASSPATH = "kodkod.jar:./src/:./parser/:org.sat4j.core.jar:antlr-4.7-complete.jar" +## Parameters Sanity Check ##################################################### +ifeq ($(strip $(LEVEL_FILES)),) +$(error No LEVEL_FILES defined as parameter.) +endif + +ifeq ($(strip $(PROPERTY_FILES)),) +$(error No PROPERTY_FILES defined as parameter.) +endif + +ifeq ($(strip $(MODEL_DIR)),) +$(error No MODEL_DIR defined as parameter.) +endif + +ifeq ($(strip $(CFG_MODEL_DIR)),) +$(error No CFG_MODEL_DIR defined as parameter.) +endif -## Dependencies ################################################################ -JAR_SOURCE = https://noot-noot.org/onera_2017/jar/ +ifeq ($(strip $(SOL_DIR)),) +$(error No SOL_DIR defined as parameter.) +endif + +ifeq ($(strip $(JAR_SOURCE)),) +$(error No JAR_SOURCE defined as parameter.) +endif + +ifeq ($(strip $(JAVA)),) +$(error No Java executable defined as parameter.) +endif + +ifeq ($(strip $(JAVAC)),) +$(error No Java compiler defined as parameter.) +endif + +ifeq ($(strip $(DOWNLOADER)),) +$(error No Java executable defined as parameter.) +endif + +################################################################################ +CLASSPATH = "kodkod.jar:./src/:./parser/:org.sat4j.core.jar:antlr-4.7-complete.jar" REQUIRED_JARS = kodkod.jar org.sat4j.core.jar antlr-4.7-complete.jar ## Makefile Magic ############################################################## -GLOBAL_INPUT_FILES = $(MODEL_FILES) $(LEVEL_DIR) $(MAP_FILES) -SOURCES = $(wildcard src/*.java parser/*.java) -GRAMMARS = $(wildcard parser/*.g4) +SOURCES = $(wildcard src/*.java) CLASSES = $(SOURCES:.java=.class) -SOLUTIONS = $(PROPERTIES:.pro=.sol) +SOLUTION_FILES = $(addprefix $(SOL_DIR)/,$(notdir $(PROPERTY_FILES:.pro=.sol))) +MODEL_FILES = \ + $(MODEL_DIR)/structural.mod \ + $(filter-out %structural.mod,$(wildcard $(MODEL_DIR)/*.mod)) +export ## Makefile Rules ############################################################## -run: $(SOLUTIONS) +run: cfg-generator parser $(SOLUTION_FILES) + +cfg-generator: + $(MAKE) -C cfg-to-paths -all: parser/PropertyParser.java $(CLASSES) +parser: antlr-4.7-complete.jar $(MAKE) -C parser - $(MAKE) -C ../ast-to-instr - $(MAKE) -C ../cfg-to-paths clean: - rm -f $(CLASSES) $(MAKE) -C parser clean - $(MAKE) -C ../ast-to-instr clean - $(MAKE) -C ../cfg-to-paths clean + $(MAKE) -C cfg-to-paths clean + rm -f $(CLASSES) -%.sol: %.pro parser/PropertyParser.java $(CLASSES) $(REQUIRED_JARS) - echo "Solving \"$<\"..." - $(MAKE) -C ../ast-to-instr - $(MAKE) -C ../cfg-to-paths - $(JAVA) -cp $(CLASSPATH) Main $@ $< $(GLOBAL_INPUT_FILES) -v +%.sol: cfg-generator parser $(CLASSES) $(PROPERTY_FILES) $(MODEL_FILES) $(LEVEL_FILES) + $(JAVA) -cp $(CLASSPATH) Main $@ \ + $(filter %$(basename $(notdir $@)).pro,$(PROPERTY_FILES)) \ + $(LEVEL_FILES) \ + $(MODEL_FILES) + -v -%.class: %.java $(REQUIRED_JARS) parser/PropertyParser.java +%.class: %.java $(REQUIRED_JARS) parser $(JAVAC) -cp $(CLASSPATH) $< %.jar: - echo "Attempting to download missing jar '$@'" + echo "Attempting to download missing jar '$@'..." $(DOWNLOADER) "$(JAR_SOURCE)/$@" - -parser/PropertyParser.java: antlr-4.7-complete.jar $(GRAMMAR) - $(MAKE) -C parser diff --git a/instr-to-kodkod/cfg-to-paths/Makefile b/instr-to-kodkod/cfg-to-paths/Makefile new file mode 100644 index 0000000..c9d14be --- /dev/null +++ b/instr-to-kodkod/cfg-to-paths/Makefile @@ -0,0 +1,35 @@ +## Target(s) Configuration ##################################################### +MODEL_FILES = $(wildcard ../ast-to-instr/cfg_*.mod) +OUTPUT_FILES = $(patsubst ../ast-to-instr/%.mod,%.cfg.mod,$(MODEL_FILES)) + +## Executables ################################################################# +JAVAC = javac +JAVA = java + +## Java Config ################################################################# +CLASSPATH = "./src/" + +## Makefile Magic ############################################################## +SOURCES = $(wildcard src/*.java) +CLASSES = $(SOURCES:.java=.class) + +## Makefile Rules ############################################################## + +all: $(CLASSES) $(OUTPUT_FILES) + +clean: + rm -f $(CLASSES) + rm -f *.mod + +#run: $(CLASSES) +# $(JAVA) -cp $(CLASSPATH) Main $(MODEL_FILE) $(ROOT_NODE) $(ID_PREFIX) \ +# $(OUTPUT_FILE) + +%.class: %.java + $(JAVAC) -cp $(CLASSPATH) $< + +PATTERN = 's/\(is_start_node ([0-9]+).*/\1/p' + +%.cfg.mod: ../ast-to-instr/%.mod $(CLASSES) + $(eval ROOT_NODE := $(shell cat $< | grep 'is_start_node' | sed -En $(PATTERN))) + $(JAVA) -cp $(CLASSPATH) Main $< $(ROOT_NODE) "p$(ROOT_NODE)_" $@ diff --git a/instr-to-kodkod/cfg-to-paths/src/ControlFlow.java b/instr-to-kodkod/cfg-to-paths/src/ControlFlow.java new file mode 100644 index 0000000..93d20ba --- /dev/null +++ b/instr-to-kodkod/cfg-to-paths/src/ControlFlow.java @@ -0,0 +1,142 @@ +import java.io.*; + +public class ControlFlow +{ + public static boolean load_file + ( + final String filename + ) + throws FileNotFoundException + { + final QuickParser qp; + String[] input; + boolean success; + + 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("add_element")) + { + success = handle_add_element(input); + } + else if (input[0].equals("node_connect")) + { + success = handle_add_connect_to(input); + } + else if (input[0].equals("is_terminal")) + { + success = handle_is_terminal(input); + } + else + { + continue; + } + + if (!success) + { + System.err.println + ( + "[E] An erroneous instruction was found in file \"" + + filename + + "\"." + ); + + try + { + qp.finalize(); + } + catch (final Exception e) + { + System.err.println("[E] Additionally:"); + e.printStackTrace(); + } + + return false; + } + } + + return true; + } + + private static boolean handle_add_element + ( + final String[] input + ) + { + if (input.length != 3) + { + return false; + } + + if (!input[1].equals("node")) + { + return true; + } + + Node.handle_add_node(input[2]); + + return true; + } + + private static boolean handle_is_terminal + ( + final String[] input + ) + { + if (input.length != 2) + { + return false; + } + + Node.handle_is_terminal(input[1]); + + return true; + } + + private static boolean handle_add_connect_to + ( + final String[] input + ) + { + if ((input.length != 3)) + { + return false; + } + + return Node.handle_connect_to(input[1], input[2]); + } + + private ControlFlow () {} /* Utility Class */ +} diff --git a/instr-to-kodkod/cfg-to-paths/src/Main.java b/instr-to-kodkod/cfg-to-paths/src/Main.java new file mode 100644 index 0000000..d5cc650 --- /dev/null +++ b/instr-to-kodkod/cfg-to-paths/src/Main.java @@ -0,0 +1,114 @@ +/* FIXME: Finer imports */ +import java.util.*; + +import java.io.*; + +public class Main +{ + private static Parameters PARAMETERS; + private static int path_counter = 0; + + public static void main (final String... args) + { + final FileWriter output; + final Collection all_paths; + final Collection> all_subpaths; + + PARAMETERS = new Parameters(args); + + if (!PARAMETERS.are_valid()) + { + return; + } + + try + { + ControlFlow.load_file(PARAMETERS.get_model_file()); + } + catch (final Exception e) + { + System.err.println + ( + "[E] Could not load model file \"" + + PARAMETERS.get_model_file() + + "\":" + ); + + e.printStackTrace(); + + return; + } + + all_paths = Path.get_all_paths_from(PARAMETERS.get_root_node()); + + all_subpaths = new ArrayList>(); + + for (final Path p: all_paths) + { + all_subpaths.addAll(p.get_all_subpaths()); + } + + try + { + output = new FileWriter(PARAMETERS.get_output_file()); + + for (final List tuple: all_subpaths) + { + node_tuple_to_predicates(tuple, output); + } + + output.close(); + } + catch (final Exception e) + { + System.err.println + ( + "[E] Could not write to output file \"" + + PARAMETERS.get_model_file() + + "\":" + ); + + e.printStackTrace(); + + return; + } + } + + private static void node_tuple_to_predicates + ( + final List tuple, + final FileWriter out + ) + throws IOException + { + final String id; + final int tuple_size; + + tuple_size = tuple.size(); + + id = (PARAMETERS.get_id_prefix() + path_counter); + path_counter += 1; + + out.write("(add_element path " + id + ")\n"); + out.write("(is_path_of " + id + " " + tuple.get(0) + ")\n"); + + for (int i = 0; i < tuple_size; ++i) + { + out.write("(contains_node " + id + " " + tuple.get(i) + ")\n"); + + for (int j = (i + 1); j < tuple_size; ++j) + { + out.write + ( + "(is_before " + + id + + " " + + tuple.get(i) + + " " + + tuple.get(j) + + ")\n" + ); + } + } + } +} diff --git a/instr-to-kodkod/cfg-to-paths/src/Node.java b/instr-to-kodkod/cfg-to-paths/src/Node.java new file mode 100644 index 0000000..3b6f2c1 --- /dev/null +++ b/instr-to-kodkod/cfg-to-paths/src/Node.java @@ -0,0 +1,104 @@ +import java.util.*; + +public class Node +{ + /** Static *****************************************************************/ + private static final Map NODE_FROM_STRING; + + static + { + NODE_FROM_STRING = new HashMap(); + } + + public static Node get_node (final String s) + { + return NODE_FROM_STRING.get(s); + } + + public static boolean handle_add_node (final String a) + { + if (!NODE_FROM_STRING.containsKey(a)) + { + NODE_FROM_STRING.put(a, new Node(a)); + } + + return true; + } + + public static boolean handle_is_terminal (final String a) + { + Node n; + + n = NODE_FROM_STRING.get(a); + + if (n == (Node) null) + { + n = new Node(a); + + NODE_FROM_STRING.put(a, n); + } + + n.set_as_terminal(); + + return true; + } + + public static boolean handle_connect_to (final String a, final String b) + { + final Node n_a, n_b; + + n_a = NODE_FROM_STRING.get(a); + n_b = NODE_FROM_STRING.get(b); + + if ((n_a == null) || (n_b == null)) + { + System.err.println + ( + "[E] Causality issue: Nodes \"" + + a + + "\" or \"" + + b + + "\" used but not defined." + ); + + return false; + } + n_a.next_nodes.add(n_b); + + return true; + } + + /** Non-Static *************************************************************/ + private final Collection next_nodes; + private final String name; + private boolean is_terminal; + + private Node (final String name) + { + this.name = name; + + next_nodes = new ArrayList(); + is_terminal = false; + } + + private void set_as_terminal () + { + is_terminal = true; + } + + public Collection next_nodes () + { + return next_nodes; + } + + public boolean is_terminal () + { + return is_terminal; + } + + @Override + public String toString () + { + return name; + } +} diff --git a/instr-to-kodkod/cfg-to-paths/src/Parameters.java b/instr-to-kodkod/cfg-to-paths/src/Parameters.java new file mode 100644 index 0000000..91303d2 --- /dev/null +++ b/instr-to-kodkod/cfg-to-paths/src/Parameters.java @@ -0,0 +1,72 @@ +public class Parameters +{ + private final String model_file; + private final String root_node; + private final String id_prefix; + private final String output_file; + private final boolean are_valid; + + public static void print_usage () + { + System.out.println + ( + "Instr-to-kodkod\n" + + "USAGE:\n" + + "\tjava Main \n" + + "PARAMETERS:\n" + + "\t\tInstruction file describing the model.\n" + + "\t\tID of the root node for this DAG.\n" + + "\t\tPrefix for the IDs of generated paths.\n" + + "\t\tFile in which to output the generated" + + " instructions." + ); + } + + public Parameters (final String... args) + { + if (args.length != 4) + { + print_usage(); + + model_file = new String(); + root_node = new String(); + id_prefix = new String(); + output_file = new String(); + + are_valid = false; + } + else + { + model_file = args[0]; + root_node = args[1]; + id_prefix = args[2]; + output_file = args[3]; + are_valid = true; + } + } + + public String get_model_file () + { + return model_file; + } + + public String get_root_node () + { + return root_node; + } + + public String get_id_prefix () + { + return id_prefix; + } + + public String get_output_file () + { + return output_file; + } + + public boolean are_valid () + { + return are_valid; + } +} diff --git a/instr-to-kodkod/cfg-to-paths/src/Path.java b/instr-to-kodkod/cfg-to-paths/src/Path.java new file mode 100644 index 0000000..9e0897f --- /dev/null +++ b/instr-to-kodkod/cfg-to-paths/src/Path.java @@ -0,0 +1,102 @@ +import java.util.*; + +public class Path +{ + private final ArrayList nodes; + private final Node last_node; + + public static Collection get_all_paths_from (final String root) + { + final Collection result; + final Stack waiting_list; + final Node root_node; + + root_node = Node.get_node(root); + + if (root_node == null) + { + System.err.println + ( + "[E] Could not find root node \"" + + root + + "\"." + ); + + return null; + } + + result = new ArrayList(); + waiting_list = new Stack(); + + waiting_list.push((new Path(Node.get_node(root)))); + + while (!waiting_list.empty()) + { + final Path current_path; + final Node current_node; + final Collection next_nodes; + + current_path = waiting_list.pop(); + current_node = current_path.last_node; + next_nodes = current_node.next_nodes(); + + if (next_nodes.isEmpty()) + { + result.add(current_path); + } + else + { + if (current_node.is_terminal()) + { + result.add(current_path); + } + for (final Node next: next_nodes) + { + waiting_list.push(current_path.add_step(next)); + } + } + } + + return result; + } + + private Path (final Node start) + { + nodes = new ArrayList(); + + nodes.add(start); + + last_node = start; + } + + private Path (final ArrayList nodes, final Node last_node) + { + this.nodes = nodes; + this.last_node = last_node; + + this.nodes.add(last_node); + } + + @SuppressWarnings("unchecked") + /* 'nodes' is an ArrayList, and so should be its clone. */ + private Path add_step (final Node n) + { + return new Path((ArrayList) nodes.clone(), n); + } + + public Collection> get_all_subpaths () + { + final Collection> result; + final int path_length; + + result = new ArrayList>(); + path_length = nodes.size(); + + for (int i = 0; i < path_length; ++i) + { + result.add(nodes.subList(i, path_length)); + } + + return result; + } +} diff --git a/instr-to-kodkod/cfg-to-paths/src/QuickParser.java b/instr-to-kodkod/cfg-to-paths/src/QuickParser.java new file mode 100644 index 0000000..47cea27 --- /dev/null +++ b/instr-to-kodkod/cfg-to-paths/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("\\((?[a-z_0-9 \"]+)\\)"); + } + 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 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/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 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 IS_IN_FILE; + public static Map IS_IN_ARCHITECTURE; + public static Map IS_IN_ENTITY; + + static + { + IS_IN_FILE = new HashMap(); + IS_IN_ARCHITECTURE = new HashMap(); + IS_IN_ENTITY = new HashMap(); + } + + 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 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 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 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 sol_files; + private final List pp_files; + private final List model_files; + private final List map_files; + + private final boolean are_valid; + + public static void print_usage () + { + System.out.println + ( + "Sol-Pretty-Printer\n" + + "USAGE:\n" + + "\tjava Main + +\n" + + "PARAMETERS:\n" + + "\t- \tInstr. model files, string map files.\n" + + "\t- \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(); + model_files = new ArrayList(); + sol_files = new ArrayList(); + pp_files = new ArrayList(); + + 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" + + " 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 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 get_map_files () + { + return map_files; + } + + public List get_model_files () + { + return model_files; + } + + public List get_solution_files () + { + return sol_files; + } + + public List 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("\\((?[^)]+)\\)"); + } + 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 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("\\((?[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 next_solution () + throws IOException + { + final List result; + final Matcher matcher; + boolean has_started_sol; + String line; + + result = new ArrayList(); + 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 FROM_ID; + + static + { + FROM_ID = new HashMap(); + } + + 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 function_values; + + private SolutionItem + ( + final String id + ) + { + function_values = new HashMap(); + + function_values.put("ID", id); + } + + public Set> 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 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 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 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 FROM_ID; + + static + { + FROM_ID = new HashMap(); + } + + 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 FROM_WAVEFORM; + + static + { + FROM_WAVEFORM = new HashMap(); + } + + 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; + } +} diff --git a/sol_pretty_printer/src/Main.java b/sol_pretty_printer/src/Main.java deleted file mode 100644 index 7a7bfcd..0000000 --- a/sol_pretty_printer/src/Main.java +++ /dev/null @@ -1,144 +0,0 @@ -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 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 deleted file mode 100644 index 097e76f..0000000 --- a/sol_pretty_printer/src/Models.java +++ /dev/null @@ -1,248 +0,0 @@ -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 IS_IN_FILE; - public static Map IS_IN_ARCHITECTURE; - public static Map IS_IN_ENTITY; - - static - { - IS_IN_FILE = new HashMap(); - IS_IN_ARCHITECTURE = new HashMap(); - IS_IN_ENTITY = new HashMap(); - } - - 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 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 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 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 deleted file mode 100644 index 92e9074..0000000 --- a/sol_pretty_printer/src/Parameters.java +++ /dev/null @@ -1,139 +0,0 @@ -import java.util.List; -import java.util.ArrayList; - -public class Parameters -{ - private final List sol_files; - private final List pp_files; - private final List model_files; - private final List map_files; - - private final boolean are_valid; - - public static void print_usage () - { - System.out.println - ( - "Sol-Pretty-Printer\n" - + "USAGE:\n" - + "\tjava Main + +\n" - + "PARAMETERS:\n" - + "\t- \tInstr. model files, string map files.\n" - + "\t- \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(); - model_files = new ArrayList(); - sol_files = new ArrayList(); - pp_files = new ArrayList(); - - 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" - + " 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 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 get_map_files () - { - return map_files; - } - - public List get_model_files () - { - return model_files; - } - - public List get_solution_files () - { - return sol_files; - } - - public List 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 deleted file mode 100644 index 19d29e7..0000000 --- a/sol_pretty_printer/src/QuickParser.java +++ /dev/null @@ -1,58 +0,0 @@ -/* 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("\\((?[^)]+)\\)"); - } - 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 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 deleted file mode 100644 index e162096..0000000 --- a/sol_pretty_printer/src/QuickSolParser.java +++ /dev/null @@ -1,117 +0,0 @@ -/* 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("\\((?[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 next_solution () - throws IOException - { - final List result; - final Matcher matcher; - boolean has_started_sol; - String line; - - result = new ArrayList(); - 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 deleted file mode 100644 index f251b62..0000000 --- a/sol_pretty_printer/src/SolutionItem.java +++ /dev/null @@ -1,82 +0,0 @@ -import java.util.Map; -import java.util.HashMap; -import java.util.Set; - -public class SolutionItem -{ - private static final Map FROM_ID; - - static - { - FROM_ID = new HashMap(); - } - - 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 function_values; - - private SolutionItem - ( - final String id - ) - { - function_values = new HashMap(); - - function_values.put("ID", id); - } - - public Set> 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 deleted file mode 100644 index 38a426a..0000000 --- a/sol_pretty_printer/src/Solutions.java +++ /dev/null @@ -1,96 +0,0 @@ -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 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 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 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 deleted file mode 100644 index 31f6145..0000000 --- a/sol_pretty_printer/src/Strings.java +++ /dev/null @@ -1,89 +0,0 @@ -import java.io.FileNotFoundException; -import java.io.IOException; - -import java.util.Map; -import java.util.HashMap; - -public class Strings -{ - private static final Map FROM_ID; - - static - { - FROM_ID = new HashMap(); - } - - 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 deleted file mode 100644 index 3a40869..0000000 --- a/sol_pretty_printer/src/Waveforms.java +++ /dev/null @@ -1,40 +0,0 @@ -import java.util.Map; -import java.util.HashMap; - -public class Waveforms -{ - private static final Map FROM_WAVEFORM; - - static - { - FROM_WAVEFORM = new HashMap(); - } - - 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; - } -} -- cgit v1.2.3-70-g09d2