summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2017-09-18 10:46:34 +0200
committerNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2017-09-18 10:46:34 +0200
commit2e15b9f8a8be3bea2b0ed1cb633c2f35582e39b8 (patch)
tree88587ce79357e1791075fdf3e8afb820225df864 /instance-calculator/src
parent079af0e3eacb62e9e0cecedef7e0c7ddffe74275 (diff)
parent77838a1342b53ca97d842ef47ff91f44f6a5bbeb (diff)
Merge branch 'master' of dreamhost:~/repositories/git/tabellion
Diffstat (limited to 'instance-calculator/src')
-rw-r--r--instance-calculator/src/.java_classpath1
-rw-r--r--instance-calculator/src/Main.java121
-rw-r--r--instance-calculator/src/ModelFile.java310
-rw-r--r--instance-calculator/src/OutputFile.java135
-rw-r--r--instance-calculator/src/Parameters.java64
-rw-r--r--instance-calculator/src/QuickParser.java62
-rw-r--r--instance-calculator/src/VHDLArchitecture.java179
-rw-r--r--instance-calculator/src/VHDLComponent.java146
-rw-r--r--instance-calculator/src/VHDLEntity.java150
-rw-r--r--instance-calculator/src/VHDLProcess.java253
-rw-r--r--instance-calculator/src/VHDLWaveform.java190
-rw-r--r--instance-calculator/src/Waveforms.java74
12 files changed, 1685 insertions, 0 deletions
diff --git a/instance-calculator/src/.java_classpath b/instance-calculator/src/.java_classpath
new file mode 100644
index 0000000..5e14ac9
--- /dev/null
+++ b/instance-calculator/src/.java_classpath
@@ -0,0 +1 @@
+../kodkod.jar
diff --git a/instance-calculator/src/Main.java b/instance-calculator/src/Main.java
new file mode 100644
index 0000000..b66a63b
--- /dev/null
+++ b/instance-calculator/src/Main.java
@@ -0,0 +1,121 @@
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Deque;
+
+import java.io.FileWriter;
+
+public class Main
+{
+ private static Parameters PARAMETERS;
+
+ public static void main (final String... args)
+ {
+ final FileWriter output;
+
+ PARAMETERS = new Parameters(args);
+
+ if (!PARAMETERS.are_valid())
+ {
+ return;
+ }
+
+ try
+ {
+ ModelFile.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;
+ }
+
+ create_instances();
+ OutputFile.close_all();
+ }
+
+ private static void create_instances ()
+ {
+ final Collection<VHDLEntity> futur_candidates;
+ final Deque<VHDLEntity> candidates;
+ final Collection<VHDLEntity> processed_candidates;
+ boolean is_stuck;
+
+ futur_candidates = new ArrayList<VHDLEntity>();
+ candidates = new ArrayDeque<VHDLEntity>();
+ processed_candidates = new ArrayList<VHDLEntity>();
+
+ futur_candidates.addAll(VHDLEntity.get_all());
+
+ while (!futur_candidates.isEmpty())
+ {
+ is_stuck = true;
+
+ candidates.addAll(futur_candidates);
+ futur_candidates.clear();
+
+ while (!candidates.isEmpty())
+ {
+ final VHDLEntity e;
+ boolean is_ready;
+
+ e = candidates.pop();
+
+ is_ready = true;
+
+ for (final VHDLComponent cmp: e.get_architecture().get_components())
+ {
+ if (!processed_candidates.contains(cmp.get_destination()))
+ {
+ is_ready = false;
+
+ break;
+ }
+ }
+
+ if (is_ready)
+ {
+ is_stuck = false;
+
+ e.generate_instance();
+
+ e.write_predicates();
+ processed_candidates.add(e);
+ }
+ else
+ {
+ futur_candidates.add(e);
+ }
+ }
+
+ if (is_stuck)
+ {
+ System.err.println("[F] Unable to create all the instances...");
+ System.err.println
+ (
+ "[E] The following entites might form a circular dependency:"
+ );
+
+ for (final VHDLEntity e: futur_candidates)
+ {
+ System.err.println("[E] Entity " + e.get_id());
+ }
+
+ System.exit(-1);
+ }
+ }
+ }
+
+ public static Parameters get_parameters ()
+ {
+ return PARAMETERS;
+ }
+}
diff --git a/instance-calculator/src/ModelFile.java b/instance-calculator/src/ModelFile.java
new file mode 100644
index 0000000..be84674
--- /dev/null
+++ b/instance-calculator/src/ModelFile.java
@@ -0,0 +1,310 @@
+import java.io.IOException;
+import java.io.FileNotFoundException;
+
+public class ModelFile
+{
+ 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("is_accessed_by"))
+ {
+ success = handle_is_accessed_by(input);
+ }
+ else if (input[0].equals("is_waveform_of"))
+ {
+ success = handle_is_waveform_of(input);
+ }
+ else if (input[0].equals("is_port_of"))
+ {
+ success = handle_is_port_of(input);
+ }
+ else if (input[0].equals("is_architecture_of"))
+ {
+ success = handle_is_architecture_of(input);
+ }
+ else if (input[0].equals("belongs_to_architecture"))
+ {
+ success = handle_belongs_to_architecture(input);
+ }
+ else if (input[0].equals("is_component_of"))
+ {
+ success = handle_is_component_of(input);
+ }
+ else if (input[0].equals("port_maps"))
+ {
+ success = handle_port_maps(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;
+ }
+ }
+
+ VHDLArchitecture.resolve_all_waveforms();
+
+ return true;
+ }
+
+ private static boolean handle_add_element
+ (
+ final String[] input
+ )
+ {
+ if (input.length != 3)
+ {
+ return false;
+ }
+
+ if (input[1].equals("entity"))
+ {
+ VHDLEntity.add_element(input[2]);
+ }
+ else if (input[1].equals("architecture"))
+ {
+ VHDLArchitecture.add_element(input[2]);
+ }
+ else if (input[1].equals("waveform"))
+ {
+ VHDLWaveform.add_element(input[2]);
+ }
+ else if (input[1].equals("process"))
+ {
+ VHDLProcess.add_element(input[2]);
+ }
+ else if (input[1].equals("component"))
+ {
+ VHDLComponent.add_element(input[2]);
+ }
+
+ return true;
+ }
+
+ private static boolean handle_is_accessed_by
+ (
+ final String[] input
+ )
+ {
+ final VHDLProcess ps;
+ final VHDLWaveform wfm;
+
+ if (input.length != 3)
+ {
+ return false;
+ }
+
+ ps = VHDLProcess.get_from_id(input[2]);
+ wfm = VHDLWaveform.find(input[1]);
+
+ if (wfm != null)
+ {
+ /*
+ * Assumes that otherwise it's a string, but that could also be due to
+ * an inconsistent model.
+ */
+
+ ps.add_accessed_wfm(wfm);
+ }
+
+ return true;
+ }
+
+ private static boolean handle_is_waveform_of
+ (
+ final String[] input
+ )
+ {
+ if (input.length != 3)
+ {
+ return false;
+ }
+
+ Waveforms.register_map(input[1], input[2]);
+
+ return true;
+ }
+
+ private static boolean handle_is_port_of
+ (
+ final String[] input
+ )
+ {
+ if (input.length != 3)
+ {
+ return false;
+ }
+
+ VHDLEntity.get_from_id(input[2]).add_port(input[1]);
+
+ return true;
+ }
+
+ private static boolean handle_is_architecture_of
+ (
+ final String[] input
+ )
+ {
+ final VHDLEntity e;
+ final VHDLArchitecture arch;
+
+ if (input.length != 3)
+ {
+ return false;
+ }
+
+ e = VHDLEntity.get_from_id(input[2]);
+ arch = VHDLArchitecture.get_from_id(input[1]);
+
+ e.set_architecture(arch);
+ arch.set_entity(e);
+
+ return true;
+ }
+
+ private static boolean handle_belongs_to_architecture
+ (
+ final String[] input
+ )
+ {
+ final VHDLArchitecture arch;
+ final VHDLProcess ps;
+ final VHDLComponent cmp;
+
+ if (input.length != 3)
+ {
+ return false;
+ }
+
+ arch = VHDLArchitecture.get_from_id(input[2]);
+
+ ps = VHDLProcess.find(input[1]);
+
+ if (ps != null)
+ {
+ arch.add_process(ps);
+ ps.set_architecture(arch);
+
+ return true;
+ }
+
+ cmp = VHDLComponent.find(input[1]);
+
+ if (cmp != null)
+ {
+ arch.add_component(cmp);
+ cmp.set_architecture(arch);
+
+ return true;
+ }
+
+ arch.add_futur_waveform(input[1]);
+
+ return true;
+ }
+
+ private static boolean handle_is_component_of
+ (
+ final String[] input
+ )
+ {
+ if (input.length != 3)
+ {
+ return false;
+ }
+
+ VHDLComponent.get_from_id(input[1]).set_destination
+ (
+ VHDLEntity.get_from_id(input[2])
+ );
+
+ return true;
+ }
+
+ private static boolean handle_port_maps
+ (
+ final String[] input
+ )
+ {
+ if (input.length != 4)
+ {
+ return false;
+ }
+
+ VHDLComponent.get_from_id(input[1]).add_port_map
+ (
+ input[2],
+ input[3]
+ );
+
+ return true;
+ }
+
+ private ModelFile () {} /* Utility Class */
+}
diff --git a/instance-calculator/src/OutputFile.java b/instance-calculator/src/OutputFile.java
new file mode 100644
index 0000000..201ca2b
--- /dev/null
+++ b/instance-calculator/src/OutputFile.java
@@ -0,0 +1,135 @@
+import java.util.ArrayList;
+import java.util.Collection;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.BufferedWriter;
+
+public class OutputFile
+{
+ private static Collection<OutputFile> ALL_OUTPUT_FILES;
+
+ static
+ {
+ ALL_OUTPUT_FILES = new ArrayList<OutputFile>();
+ }
+
+ public static void close_all ()
+ {
+ for (final OutputFile f: ALL_OUTPUT_FILES)
+ {
+ f.close();
+ }
+ }
+
+ public static OutputFile new_output_file (final String filename)
+ {
+ final OutputFile result;
+
+ result =
+ new OutputFile
+ (
+ Main.get_parameters().get_output_directory()
+ + "/"
+ + filename
+ );
+
+ ALL_OUTPUT_FILES.add(result);
+
+ return result;
+ }
+
+ /** Non-Static *************************************************************/
+ private final String filename;
+ private final BufferedWriter buffered_writer;
+
+ private OutputFile (final String filename)
+ {
+ BufferedWriter bf;
+
+ this.filename = filename;
+
+ try
+ {
+ bf = new BufferedWriter(new FileWriter(new File(filename)));
+ }
+ catch (final Exception e)
+ {
+ bf = null;
+
+ System.err.println
+ (
+ "[F] Could not create new output file \""
+ + filename
+ + "\":"
+ );
+
+ e.printStackTrace();
+
+ System.exit(-1);
+ }
+
+ buffered_writer = bf;
+ }
+
+ public void write (final String data)
+ {
+ try
+ {
+ buffered_writer.write(data);
+ }
+ catch (final Exception e)
+ {
+ System.err.println
+ (
+ "[F] Could not write to output file \""
+ + filename
+ + "\":"
+ );
+
+ e.printStackTrace();
+
+ System.exit(-1);
+ }
+ }
+
+ public void insert_newline ()
+ {
+ try
+ {
+ buffered_writer.newLine();
+ }
+ catch (final Exception e)
+ {
+ System.err.println
+ (
+ "[F] Could not write to output file \""
+ + filename
+ + "\":"
+ );
+
+ e.printStackTrace();
+
+ System.exit(-1);
+ }
+ }
+
+ private void close ()
+ {
+ try
+ {
+ buffered_writer.close();
+ }
+ catch (final Exception e)
+ {
+ System.err.println
+ (
+ "[E] Could not properly close output file \""
+ + filename
+ + "\":"
+ );
+
+ e.printStackTrace();
+ }
+ }
+}
diff --git a/instance-calculator/src/Parameters.java b/instance-calculator/src/Parameters.java
new file mode 100644
index 0000000..b8bba30
--- /dev/null
+++ b/instance-calculator/src/Parameters.java
@@ -0,0 +1,64 @@
+public class Parameters
+{
+ private final String model_file;
+ private final String id_prefix;
+ private final String output_dir;
+ private final boolean are_valid;
+
+ public static void print_usage ()
+ {
+ System.out.println
+ (
+ "Instance-Calculator\n"
+ + "USAGE:\n"
+ + "\tjava Main <INSTRUCTIONS> <ID_PREFIX> <OUTPUT_DIR>\n"
+ + "PARAMETERS:\n"
+ + "\t<INSTRUCTIONS>\tInstruction file describing the model.\n"
+ + "\t<ID_PREFIX>\tPrefix for the IDs of generated paths.\n"
+ + "\t<OUTPUT_DIR>\tDirectory in which to output the generated"
+ + " instruction files."
+ );
+ }
+
+ public Parameters (final String... args)
+ {
+ if (args.length != 3)
+ {
+ print_usage();
+
+ model_file = new String();
+ id_prefix = new String();
+ output_dir = new String();
+
+ are_valid = false;
+ }
+ else
+ {
+ model_file = args[0];
+ id_prefix = args[1];
+ output_dir = args[2];
+
+ are_valid = true;
+ }
+ }
+
+ public String get_model_file ()
+ {
+ return model_file;
+ }
+
+ public String get_id_prefix ()
+ {
+ return id_prefix;
+ }
+
+ public String get_output_directory ()
+ {
+ return output_dir;
+ }
+
+ public boolean are_valid ()
+ {
+ return are_valid;
+ }
+}
diff --git a/instance-calculator/src/QuickParser.java b/instance-calculator/src/QuickParser.java
new file mode 100644
index 0000000..383373e
--- /dev/null
+++ b/instance-calculator/src/QuickParser.java
@@ -0,0 +1,62 @@
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.FileNotFoundException;
+
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class QuickParser
+{
+ private static final Pattern instr_pattern;
+ private final BufferedReader buffered_reader;
+
+ static
+ {
+ instr_pattern = Pattern.compile("\\((?<list>[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<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/instance-calculator/src/VHDLArchitecture.java b/instance-calculator/src/VHDLArchitecture.java
new file mode 100644
index 0000000..a3aa7ac
--- /dev/null
+++ b/instance-calculator/src/VHDLArchitecture.java
@@ -0,0 +1,179 @@
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Deque;
+import java.util.Map;
+import java.util.HashMap;
+
+public class VHDLArchitecture
+{
+ private static final Map<String, VHDLArchitecture> FROM_ID;
+
+ static
+ {
+ FROM_ID = new HashMap<String, VHDLArchitecture>();
+ }
+
+ public static void resolve_all_waveforms ()
+ {
+ for (final VHDLArchitecture arch: FROM_ID.values())
+ {
+ arch.resolve_waveforms();
+ }
+ }
+
+ public static void add_element (final String id)
+ {
+ if (!FROM_ID.containsKey(id))
+ {
+ FROM_ID.put(id, new VHDLArchitecture(id));
+ }
+ }
+
+ public static VHDLArchitecture get_from_id (final String id)
+ {
+ final VHDLArchitecture result;
+
+ result = FROM_ID.get(id);
+
+ if (result == null)
+ {
+ System.err.println
+ (
+ "[E] Element "
+ + id
+ + " is used like an architecture, but is not declared as such"
+ + " before that use."
+ );
+
+ System.exit(-1);
+ }
+
+ return result;
+ }
+
+ public static VHDLArchitecture find (final String id)
+ {
+ return FROM_ID.get(id);
+ }
+
+/******************************************************************************/
+ private final Collection<VHDLProcess> processes;
+ private final Collection<VHDLComponent> components;
+ private final Collection<VHDLWaveform> waveforms;
+ private final Deque<String> futur_waveforms;
+ private final String id;
+
+ private VHDLEntity entity;
+
+ private VHDLArchitecture (final String id)
+ {
+ this.id = id;
+
+ processes = new ArrayList<VHDLProcess>();
+ components = new ArrayList<VHDLComponent>();
+ waveforms = new ArrayList<VHDLWaveform>();
+ futur_waveforms = new ArrayDeque<String>();
+ }
+
+ public VHDLEntity get_entity ()
+ {
+ return entity;
+ }
+
+ public void set_entity (final VHDLEntity e)
+ {
+ entity = e;
+ }
+
+ public void add_process (final VHDLProcess ps)
+ {
+ if (!processes.contains(ps))
+ {
+ processes.add(ps);
+ }
+ }
+
+ public void add_component (final VHDLComponent cmp)
+ {
+ if (!components.contains(cmp))
+ {
+ components.add(cmp);
+ }
+ }
+
+ public Collection<VHDLComponent> get_components ()
+ {
+ return components;
+ }
+
+ public void add_waveform (final VHDLWaveform wfm)
+ {
+ if (!waveforms.contains(wfm))
+ {
+ waveforms.add(wfm);
+ }
+ }
+
+ public void add_futur_waveform (final String fwfm)
+ {
+ if (!futur_waveforms.contains(fwfm))
+ {
+ futur_waveforms.add(fwfm);
+ }
+ }
+
+ public void resolve_waveforms ()
+ {
+ while (!futur_waveforms.isEmpty())
+ {
+ final String src_id, wfm_id;
+
+ src_id = futur_waveforms.pop();
+
+ wfm_id = Waveforms.get_waveform_id_from_id(src_id);
+ add_waveform(VHDLWaveform.get_from_id(wfm_id));
+ }
+ }
+
+ public void add_instance_to
+ (
+ final Collection<VHDLProcess.Instance> process_instances,
+ final Collection<VHDLWaveform.Instance> waveform_instances,
+ final Map<VHDLWaveform, VHDLWaveform.Instance> local_conversion
+ )
+ {
+ for (final VHDLWaveform wfm: waveforms)
+ {
+ final VHDLWaveform.Instance i_wfm;
+
+ i_wfm = wfm.add_instance(entity);
+
+ waveform_instances.add(i_wfm);
+
+ local_conversion.put(wfm, i_wfm);
+ }
+
+ for (final VHDLProcess ps: processes)
+ {
+ process_instances.add
+ (
+ ps.generate_base_instance
+ (
+ entity,
+ waveform_instances
+ )
+ );
+ }
+
+ for (final VHDLComponent cmp: components)
+ {
+ cmp.add_instance_content_to
+ (
+ process_instances,
+ waveform_instances,
+ local_conversion
+ );
+ }
+ }
+}
diff --git a/instance-calculator/src/VHDLComponent.java b/instance-calculator/src/VHDLComponent.java
new file mode 100644
index 0000000..76b1dc9
--- /dev/null
+++ b/instance-calculator/src/VHDLComponent.java
@@ -0,0 +1,146 @@
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Map;
+import java.util.HashMap;
+
+public class VHDLComponent
+{
+ private static final Map<String, VHDLComponent> FROM_ID;
+
+ static
+ {
+ FROM_ID = new HashMap<String, VHDLComponent>();
+ }
+
+ public static void add_element (final String id)
+ {
+ if (!FROM_ID.containsKey(id))
+ {
+ FROM_ID.put(id, new VHDLComponent(id));
+ }
+ }
+
+ public static VHDLComponent get_from_id (final String id)
+ {
+ final VHDLComponent result;
+
+ result = FROM_ID.get(id);
+
+ if (result == null)
+ {
+ System.err.println
+ (
+ "[E] Element "
+ + id
+ + " is used like a component instantiation, but is not declared as"
+ + " such before that use."
+ );
+
+ System.exit(-1);
+ }
+
+ return result;
+ }
+
+ public static VHDLComponent find (final String id)
+ {
+ return FROM_ID.get(id);
+ }
+
+/******************************************************************************/
+ private final Map<String, String> reverse_port_map;
+ private final String id;
+
+ private VHDLEntity destination;
+ private VHDLArchitecture parent;
+
+ public void set_destination (final VHDLEntity dest)
+ {
+ destination = dest;
+ }
+
+ public VHDLEntity get_destination ()
+ {
+ return destination;
+ }
+
+ public void set_architecture (final VHDLArchitecture arch)
+ {
+ parent = arch;
+ }
+
+ public void add_port_map (final String src, final String dest)
+ {
+ reverse_port_map.put(dest, src);
+ }
+
+ public void add_instance_content_to
+ (
+ final Collection<VHDLProcess.Instance> process_instances,
+ final Collection<VHDLWaveform.Instance> waveform_instances,
+ final Map<VHDLWaveform, VHDLWaveform.Instance> local_conversion
+ )
+ {
+ final Collection<VHDLProcess.Instance> dest_process_instances;
+ final Collection<VHDLWaveform.Instance> dest_waveform_instances;
+ final Map<VHDLWaveform.Instance, VHDLWaveform.Instance> wfm_map;
+
+ dest_process_instances = destination.get_process_instances();
+ dest_waveform_instances = destination.get_waveform_instances();
+
+ wfm_map = new HashMap<VHDLWaveform.Instance, VHDLWaveform.Instance>();
+
+ for (final VHDLWaveform.Instance i_wfm: dest_waveform_instances)
+ {
+ final String src_wfm, dest_port;
+ final VHDLWaveform.Instance replacement;
+
+ dest_port =
+ Waveforms.get_id_from_waveform_id
+ (
+ i_wfm.get_parent().get_id()
+ );
+
+ src_wfm = reverse_port_map.get(dest_port);
+
+ if (src_wfm == null)
+ {
+ /* i_wfm is not linked to a port, so it needs a new instance. */
+ replacement = i_wfm.get_parent().add_instance(parent.get_entity());
+ }
+ else
+ {
+ /*
+ * i_wfm is linked to a port. Replace it by what's connected to the
+ * port.
+ */
+ replacement =
+ local_conversion.get
+ (
+ VHDLWaveform.get_from_id
+ (
+ src_wfm
+ )
+ );
+ }
+
+ wfm_map.put(i_wfm, replacement);
+ waveform_instances.add(replacement);
+ }
+
+ for (final VHDLProcess.Instance i_ps: dest_process_instances)
+ {
+ process_instances.add
+ (
+ i_ps.add_instance(parent.get_entity(), wfm_map)
+ );
+ }
+ }
+
+ private VHDLComponent (final String id)
+ {
+ this.id = id;
+
+ reverse_port_map = new HashMap<String, String>();
+ }
+}
diff --git a/instance-calculator/src/VHDLEntity.java b/instance-calculator/src/VHDLEntity.java
new file mode 100644
index 0000000..d2a8e07
--- /dev/null
+++ b/instance-calculator/src/VHDLEntity.java
@@ -0,0 +1,150 @@
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Map;
+import java.util.HashMap;
+
+public class VHDLEntity
+{
+ private static final Map<String, VHDLEntity> FROM_ID;
+
+ static
+ {
+ FROM_ID = new HashMap<String, VHDLEntity>();
+ }
+
+ public static Collection<VHDLEntity> get_all ()
+ {
+ return FROM_ID.values();
+ }
+
+ public static void add_element (final String id)
+ {
+ if (!FROM_ID.containsKey(id))
+ {
+ FROM_ID.put(id, new VHDLEntity(id));
+ }
+ }
+
+ public static VHDLEntity get_from_id (final String id)
+ {
+ final VHDLEntity result;
+
+ result = FROM_ID.get(id);
+
+ if (result == null)
+ {
+ System.err.println
+ (
+ "[E] Element "
+ + id
+ + " is used like an entity, but is not declared as such before that"
+ + " use."
+ );
+
+ System.exit(-1);
+ }
+
+ return result;
+ }
+
+ public static VHDLEntity find (final String id)
+ {
+ return FROM_ID.get(id);
+ }
+
+/******************************************************************************/
+ private final Collection<VHDLProcess.Instance> process_instances;
+ private final Collection<VHDLWaveform.Instance> waveform_instances;
+
+ private final OutputFile output_file;
+ private final Collection<String> ports;
+ private final String id;
+
+ private VHDLArchitecture architecture;
+
+ private VHDLEntity (final String id)
+ {
+ this.id = id;
+ ports = new ArrayList<String>();
+
+ architecture = null;
+
+ this.process_instances = new ArrayList<VHDLProcess.Instance>();
+ this.waveform_instances = new ArrayList<VHDLWaveform.Instance>();
+
+ output_file = OutputFile.new_output_file("instances_in_" + id + ".mod");
+ }
+
+ public String get_id ()
+ {
+ return id;
+ }
+
+ public void add_port (final String pt)
+ {
+ if (!ports.contains(pt))
+ {
+ ports.add(pt);
+ }
+ }
+
+ public void set_architecture (final VHDLArchitecture arch)
+ {
+ architecture = arch;
+ }
+
+ public VHDLArchitecture get_architecture ()
+ {
+ return architecture;
+ }
+
+ public Collection<VHDLProcess.Instance> get_process_instances ()
+ {
+ return process_instances;
+ }
+
+ public Collection<VHDLWaveform.Instance> get_waveform_instances ()
+ {
+ return waveform_instances;
+ }
+
+ public void generate_instance ()
+ {
+ final Map<VHDLWaveform, VHDLWaveform.Instance> local_conversion;
+
+ local_conversion = new HashMap<VHDLWaveform, VHDLWaveform.Instance>();
+
+ for (final String pt: ports)
+ {
+ final VHDLWaveform wfm;
+ final VHDLWaveform.Instance i_wfm;
+
+ wfm = VHDLWaveform.get_from_id(Waveforms.get_waveform_id_from_id(pt));
+ i_wfm = wfm.add_instance(this);
+
+ waveform_instances.add(i_wfm);
+
+ local_conversion.put(wfm, i_wfm);
+ }
+
+ architecture.add_instance_to
+ (
+ process_instances,
+ waveform_instances,
+ local_conversion
+ );
+ }
+
+ public void write_predicates ()
+ {
+ for (final VHDLWaveform.Instance iwfm: waveform_instances)
+ {
+ iwfm.write_predicates_to(output_file);
+ }
+
+ for (final VHDLProcess.Instance ips: process_instances)
+ {
+ ips.write_predicates_to(output_file);
+ }
+ }
+}
diff --git a/instance-calculator/src/VHDLProcess.java b/instance-calculator/src/VHDLProcess.java
new file mode 100644
index 0000000..a4f150e
--- /dev/null
+++ b/instance-calculator/src/VHDLProcess.java
@@ -0,0 +1,253 @@
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Set;
+
+public class VHDLProcess
+{
+ private static final Map<String, VHDLProcess> FROM_ID;
+
+ static
+ {
+ FROM_ID = new HashMap<String, VHDLProcess>();
+ }
+
+ public static void add_element (final String id)
+ {
+ if (!FROM_ID.containsKey(id))
+ {
+ FROM_ID.put(id, new VHDLProcess(id));
+ }
+ }
+
+ public static VHDLProcess get_from_id (final String id)
+ {
+ final VHDLProcess result;
+
+ result = FROM_ID.get(id);
+
+ if (result == null)
+ {
+ System.err.println
+ (
+ "[E] Element "
+ + id
+ + " is used like a process, but is not declared as such before that"
+ + " use."
+ );
+
+ System.exit(-1);
+ }
+
+ return result;
+ }
+
+ public static VHDLProcess find (final String id)
+ {
+ return FROM_ID.get(id);
+ }
+
+/******************************************************************************/
+ private final Collection<VHDLWaveform> accessed_wfm;
+ private final Collection<VHDLProcess.Instance> instances;
+ private final String id;
+ private int instances_count;
+
+ private VHDLArchitecture architecture;
+
+ private VHDLProcess (final String id)
+ {
+ this.id = id;
+ accessed_wfm = new ArrayList<VHDLWaveform>();
+ instances = new ArrayList<VHDLProcess.Instance>();
+ instances_count = 0;
+ architecture = null;
+ }
+
+ public String get_id ()
+ {
+ return id;
+ }
+
+ public void add_accessed_wfm (final VHDLWaveform wfm)
+ {
+ if (!accessed_wfm.contains(wfm))
+ {
+ accessed_wfm.add(wfm);
+ }
+ }
+
+ public void set_architecture (final VHDLArchitecture arch)
+ {
+ architecture = arch;
+ }
+
+ public VHDLProcess.Instance generate_base_instance
+ (
+ final VHDLEntity visibility,
+ final Collection<VHDLWaveform.Instance> waveform_instances
+ )
+ {
+ final VHDLProcess.Instance result;
+ final Map<VHDLWaveform.Instance, VHDLWaveform> iwfm_map;
+
+ iwfm_map = new HashMap<VHDLWaveform.Instance, VHDLWaveform>();
+
+ for (final VHDLWaveform.Instance i_wfm: waveform_instances)
+ {
+ if (accessed_wfm.contains(i_wfm.get_parent()))
+ {
+ iwfm_map.put(i_wfm, i_wfm.get_parent());
+ }
+ }
+
+ result =
+ new VHDLProcess.Instance
+ (
+ this,
+ visibility,
+ iwfm_map
+ );
+
+ instances_count += 1;
+
+ return result;
+ }
+
+ public static class Instance
+ {
+ private static int instances_count;
+
+ static
+ {
+ instances_count = 0;
+ }
+
+ private final String id;
+ private final VHDLProcess parent;
+ private final Map<VHDLWaveform.Instance, VHDLWaveform> iwfm_map;
+ private final VHDLEntity visibility;
+
+ private Instance
+ (
+ final VHDLProcess parent,
+ final VHDLEntity visibility,
+ final Map<VHDLWaveform.Instance, VHDLWaveform> iwfm_map
+ )
+ {
+ this.id =
+ (
+ Main.get_parameters().get_id_prefix()
+ + "ps_"
+ + instances_count
+ );
+
+ instances_count += 1;
+
+ this.parent = parent;
+ this.visibility = visibility;
+ this.iwfm_map = iwfm_map;
+ }
+
+ public VHDLProcess get_parent ()
+ {
+ return parent;
+ }
+
+ public VHDLProcess.Instance add_instance
+ (
+ final VHDLEntity visibility,
+ final Map<VHDLWaveform.Instance, VHDLWaveform.Instance> convertion
+ )
+ {
+ final VHDLProcess.Instance result;
+ final Set<Map.Entry<VHDLWaveform.Instance, VHDLWaveform>> iwfm_set;
+ final Map<VHDLWaveform.Instance, VHDLWaveform> new_iwfm_map;
+
+ iwfm_set = iwfm_map.entrySet();
+
+ new_iwfm_map = new HashMap<VHDLWaveform.Instance, VHDLWaveform>();
+
+ for
+ (
+ final Map.Entry<VHDLWaveform.Instance, VHDLWaveform> me: iwfm_set
+ )
+ {
+ new_iwfm_map.put
+ (
+ convertion.get(me.getKey()),
+ me.getValue()
+ );
+ }
+
+ result =
+ new VHDLProcess.Instance
+ (
+ parent,
+ visibility,
+ new_iwfm_map
+ );
+
+ parent.instances_count += 1;
+
+ return result;
+ }
+
+ public void write_predicates_to (final OutputFile of)
+ {
+ final Set<Map.Entry<VHDLWaveform.Instance, VHDLWaveform>> iwfm_set;
+
+ iwfm_set = iwfm_map.entrySet();
+
+ try
+ {
+ of.write("(add_element ps_instance ");
+ of.write(id);
+ of.write(")");
+ of.insert_newline();
+
+ of.write("(is_ps_instance_of ");
+ of.write(id);
+ of.write(" ");
+ of.write(parent.get_id());
+ of.write(")");
+ of.insert_newline();
+
+ of.write("(is_visible_in ");
+ of.write(id);
+ of.write(" ");
+ of.write(visibility.get_id());
+ of.write(")");
+ of.insert_newline();
+
+ for
+ (
+ final Map.Entry<VHDLWaveform.Instance, VHDLWaveform> iwfm:
+ iwfm_set
+ )
+ {
+ of.write("(process_instance_maps ");
+ of.write(id);
+ of.write(" ");
+ of.write(iwfm.getKey().get_id());
+ of.write(" ");
+ of.write(iwfm.getValue().get_id());
+ of.write(")");
+ of.insert_newline();
+ }
+ }
+ catch (final Exception e)
+ {
+ System.err.println
+ (
+ "[F] Could not write to output file:"
+ );
+
+ e.printStackTrace();
+
+ System.exit(-1);
+ }
+ }
+ }
+}
diff --git a/instance-calculator/src/VHDLWaveform.java b/instance-calculator/src/VHDLWaveform.java
new file mode 100644
index 0000000..feb1e51
--- /dev/null
+++ b/instance-calculator/src/VHDLWaveform.java
@@ -0,0 +1,190 @@
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Map;
+import java.util.HashMap;
+
+public class VHDLWaveform
+{
+ private static final Map<String, VHDLWaveform> FROM_ID;
+
+ static
+ {
+ FROM_ID = new HashMap<String, VHDLWaveform>();
+ }
+
+ public static void add_element (final String id)
+ {
+ if (!FROM_ID.containsKey(id))
+ {
+ FROM_ID.put(id, new VHDLWaveform(id));
+ }
+ }
+
+ public static VHDLWaveform get_from_id (final String id)
+ {
+ final VHDLWaveform result;
+
+ result = FROM_ID.get(id);
+
+ if (result == null)
+ {
+ System.err.println
+ (
+ "[E] Element "
+ + id
+ + " is used like a waveform, but is not declared as such before"
+ + " that use."
+ );
+
+ new Exception().printStackTrace();
+
+ System.exit(-1);
+ }
+
+ return result;
+ }
+
+ public static VHDLWaveform find (final String id)
+ {
+ return FROM_ID.get(id);
+ }
+
+/******************************************************************************/
+ private final Collection<String> accessed_wfm;
+ private final Collection<VHDLWaveform.Instance> instances;
+ private final String id;
+ private int instances_count;
+
+ private VHDLArchitecture architecture;
+
+ private VHDLWaveform (final String id)
+ {
+ this.id = id;
+ accessed_wfm = new ArrayList<String>();
+ instances = new ArrayList<VHDLWaveform.Instance>();
+ instances_count = 0;
+ architecture = null;
+ }
+
+ public VHDLWaveform.Instance add_instance
+ (
+ final VHDLEntity visibility
+ )
+ {
+ final VHDLWaveform.Instance result;
+
+ result =
+ new VHDLWaveform.Instance
+ (
+ this,
+ visibility
+ );
+
+ instances.add(result);
+
+ instances_count += 1;
+
+ return result;
+ }
+
+ public String get_id ()
+ {
+ return id;
+ }
+
+ public void set_architecture (final VHDLArchitecture arch)
+ {
+ architecture = arch;
+ }
+
+ @Override
+ public String toString ()
+ {
+ return id;
+ }
+
+ public static class Instance
+ {
+ private static int instances_count;
+
+ static
+ {
+ instances_count = 0;
+ }
+
+ private final String id;
+ private final VHDLWaveform parent;
+ private final VHDLEntity visibility;
+
+ private Instance
+ (
+ final VHDLWaveform parent,
+ final VHDLEntity visibility
+ )
+ {
+ this.id =
+ (
+ Main.get_parameters().get_id_prefix()
+ + "wfm_"
+ + instances_count
+ );
+
+ instances_count += 1;
+
+ this.parent = parent;
+ this.visibility = visibility;
+ }
+
+ public String get_id ()
+ {
+ return id;
+ }
+
+ public VHDLWaveform get_parent ()
+ {
+ return parent;
+ }
+
+ public void write_predicates_to (final OutputFile of)
+ {
+ try
+ {
+ of.write("(add_element wfm_instance ");
+ of.write(id);
+ of.write(")");
+ of.insert_newline();
+
+ of.write("(is_wfm_instance_of ");
+ of.write(id);
+ of.write(" ");
+ of.write(parent.get_id());
+ of.write(")");
+ of.insert_newline();
+
+ of.write("(is_visible_in ");
+ of.write(id);
+ of.write(" ");
+ of.write(visibility.get_id());
+ of.write(")");
+ of.insert_newline();
+ }
+ catch (final Exception e)
+ {
+ System.err.println
+ (
+ "[F] Could not write to output file:"
+ );
+
+ e.printStackTrace();
+
+ System.exit(-1);
+ }
+ }
+
+ @Override
+ public String toString ()
+ {
+ return id;
+ }
+ }
+}
diff --git a/instance-calculator/src/Waveforms.java b/instance-calculator/src/Waveforms.java
new file mode 100644
index 0000000..9492d16
--- /dev/null
+++ b/instance-calculator/src/Waveforms.java
@@ -0,0 +1,74 @@
+import java.util.Map;
+import java.util.HashMap;
+
+public class Waveforms
+{
+ private static final Map<String, String> FROM_WAVEFORM;
+ private static final Map<String, String> TO_WAVEFORM;
+
+ static
+ {
+ FROM_WAVEFORM = new HashMap<String, String>();
+ TO_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);
+ TO_WAVEFORM.put(elem_id, wfm_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;
+ }
+
+ public static String get_waveform_id_from_id (final String src_id)
+ {
+ final String result;
+
+ result = TO_WAVEFORM.get(src_id);
+
+ if (result == null)
+ {
+ System.err.println
+ (
+ "[F] There is no waveform associated with the element \""
+ + src_id
+ + "\". Is the model complete?"
+ );
+
+ System.exit(-1);
+ }
+
+ return result;
+ }
+
+ public static String find_id_from_waveform_id (final String wfm_id)
+ {
+ return FROM_WAVEFORM.get(wfm_id);
+ }
+
+ public static String find_waveform_id_from_id (final String src_id)
+ {
+ return TO_WAVEFORM.get(src_id);
+ }
+}