| summaryrefslogtreecommitdiff |
diff options
| author | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2018-05-26 09:49:49 +0200 |
|---|---|---|
| committer | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2018-05-26 09:49:49 +0200 |
| commit | e25341f06679e3afadc9f3bcab5aaf55d264cbda (patch) | |
| tree | 5788a0ece2251cabc75bd2ab2e15adc3b6d09334 | |
| parent | 9b7c8cd0088f06d09bc723d4a6eaddf07da31521 (diff) | |
Splits IDP.java into multiple classes.
| -rw-r--r-- | src/hastabel2idp/IDP.java | 329 | ||||
| -rw-r--r-- | src/hastabel2idp/Main.java | 10 | ||||
| -rw-r--r-- | src/hastabel2idp/Parameters.java | 39 | ||||
| -rw-r--r-- | src/hastabel2idp/idp/Project.java | 97 | ||||
| -rw-r--r-- | src/hastabel2idp/idp/Structure.java | 148 | ||||
| -rw-r--r-- | src/hastabel2idp/idp/Theory.java | 32 | ||||
| -rw-r--r-- | src/hastabel2idp/idp/Vocabulary.java | 95 |
7 files changed, 407 insertions, 343 deletions
diff --git a/src/hastabel2idp/IDP.java b/src/hastabel2idp/IDP.java deleted file mode 100644 index 641d65f..0000000 --- a/src/hastabel2idp/IDP.java +++ /dev/null @@ -1,329 +0,0 @@ -package hastabel2idp; - -import hastabel.World; - -import hastabel.lang.Predicate; -import hastabel.lang.Type; -import hastabel.lang.Element; - -import java.util.Collection; -import java.util.List; - -public class IDP -{ - public static void generate - ( - final World world, - final Parameters params - ) - { - generate - ( - world, - OutputFile.new_output_file("/tmp/hasta_vocabulary.txt"), - OutputFile.new_output_file("/tmp/hasta_structure.txt"), - OutputFile.new_output_file("/tmp/hasta_theory.txt") - ); - } - - private static void generate - ( - final World world, - final OutputFile vocabulary, - final OutputFile structure, - final OutputFile theory - ) - { - final Collection<Type> types; - final Collection<Predicate> predicates; - - types = world.get_types_manager().get_all(); - predicates = world.get_predicates_manager().get_all(); - - vocabulary.write("vocabulary V {"); - vocabulary.insert_newline(); - - theory.write("theory T:V {"); - theory.insert_newline(); - - structure.write("structure S:V {"); - structure.insert_newline(); - - for (final Type type: types) - { - if (type.is_used()) - { - add_type_to_vocabulary(vocabulary, type); - add_type_to_structure(structure, type); - } - } - - for (final Predicate predicate: predicates) - { - if (predicate.is_used()) - { - final Collection<List<Type>> relevant_signatures, partial_signatures; - - relevant_signatures = predicate.get_relevant_signatures(); - partial_signatures = predicate.get_partial_signatures(); - - add_predicate_to_vocabulary - ( - vocabulary, - predicate, - relevant_signatures, - partial_signatures - ); - - add_predicate_to_structure - ( - structure, - predicate, - relevant_signatures, - partial_signatures - ); - } - } - - vocabulary.write("}"); - vocabulary.insert_newline(); - - theory.write("}"); - theory.insert_newline(); - - structure.write("}"); - structure.insert_newline(); - } - - private static void add_type_to_vocabulary - ( - final OutputFile vocabulary, - final Type type - ) - { - vocabulary.write(" type "); - vocabulary.write(type.get_name()); - vocabulary.insert_newline(); - } - - private static void add_type_to_structure - ( - final OutputFile structure, - final Type type - ) - { - boolean is_first_member; - - structure.write(" "); - structure.write(type.get_name()); - structure.write("={"); - structure.insert_newline(); - - is_first_member = true; - - for (final Element member: type.get_elements()) - { - if (is_first_member) - { - is_first_member = false; - structure.write(" "); - } - else - { - structure.write(";"); - structure.insert_newline(); - structure.write(" "); - } - - structure.write(member.get_name()); - } - - structure.insert_newline(); - structure.write(" }"); - structure.insert_newline(); - } - - private static void add_predicate_signature_to_vocabulary - ( - final OutputFile vocabulary, - final Predicate predicate, - final List<Type> signature - ) - { - boolean is_first; - - is_first = true; - - vocabulary.write(" "); - vocabulary.write(predicate.get_name()); - vocabulary.write(signature_to_suffix(signature)); - vocabulary.write("("); - - for (final Type sig_type: signature) - { - if (sig_type == null) - { - continue; - } - - if (is_first) - { - is_first = false; - } - else - { - vocabulary.write(", "); - } - - vocabulary.write(sig_type.get_name()); - } - - vocabulary.write(")"); - vocabulary.insert_newline(); - } - - private static void add_predicate_to_vocabulary - ( - final OutputFile vocabulary, - final Predicate predicate, - final Collection<List<Type>> relevant_signatures, - final Collection<List<Type>> partial_signatures - ) - { - for (final List<Type> signature: relevant_signatures) - { - add_predicate_signature_to_vocabulary - ( - vocabulary, - predicate, - signature - ); - } - - for (final List<Type> signature: partial_signatures) - { - add_predicate_signature_to_vocabulary - ( - vocabulary, - predicate, - signature - ); - } - } - - private static void add_predicate_signature_to_structure - ( - final OutputFile structure, - final Predicate predicate, - final List<Type> signature, - final boolean is_partial - ) - { - boolean is_first_member; - final Collection<List<Element>> relevant_members; - - structure.write(" "); - structure.write(predicate.get_name()); - structure.write(signature_to_suffix(signature)); - structure.write("={"); - structure.insert_newline(); - - is_first_member = true; - - if (is_partial) - { - relevant_members = predicate.get_relevant_partial_members(signature); - } - else - { - relevant_members = predicate.get_relevant_members(signature); - } - - for (final List<Element> member: relevant_members) - { - boolean is_first_member_param; - - is_first_member_param = true; - - if (is_first_member) - { - is_first_member = false; - structure.write(" "); - } - else - { - structure.write(";"); - structure.insert_newline(); - structure.write(" "); - } - - for (final Element member_param: member) - { - if (is_first_member_param) - { - is_first_member_param = false; - } - else - { - structure.write(", "); - } - - structure.write(member_param.get_name()); - } - } - - structure.insert_newline(); - structure.write(" }"); - structure.insert_newline(); - - } - - private static void add_predicate_to_structure - ( - final OutputFile structure, - final Predicate predicate, - final Collection<List<Type>> relevant_signatures, - final Collection<List<Type>> partial_signatures - ) - { - for (final List<Type> signature: relevant_signatures) - { - add_predicate_signature_to_structure - ( - structure, - predicate, - signature, - false - ); - } - - for (final List<Type> signature: partial_signatures) - { - add_predicate_signature_to_structure - ( - structure, - predicate, - signature, - true - ); - } - } - - private static String signature_to_suffix (final List<Type> signature) - { - final StringBuilder sb; - - sb = new StringBuilder(); - - for (final Type t: signature) - { - if (t != null) - { - sb.append("_"); - sb.append(t.get_name()); - } - } - - return sb.toString(); - } -} diff --git a/src/hastabel2idp/Main.java b/src/hastabel2idp/Main.java index 3f5e95b..05266ff 100644 --- a/src/hastabel2idp/Main.java +++ b/src/hastabel2idp/Main.java @@ -1,5 +1,7 @@ package hastabel2idp; +import hastabel2idp.idp.Project; + import hastabel.World; import java.io.IOException; @@ -38,6 +40,8 @@ public class Main return; } + OutputFile.close_all(); + System.out.println("# Done."); } @@ -121,10 +125,12 @@ public class Main final Parameters params ) { + final Project project; + System.out.println("# Generating IDP..."); - IDP.generate(world, params); + project = new Project(params); - OutputFile.close_all(); + project.generate(world); } } diff --git a/src/hastabel2idp/Parameters.java b/src/hastabel2idp/Parameters.java index a46e820..12f6285 100644 --- a/src/hastabel2idp/Parameters.java +++ b/src/hastabel2idp/Parameters.java @@ -8,7 +8,7 @@ public class Parameters private final List<String> level_files; private final List<String> model_files; private final String property_file; - private final String output_file; + private final String output_dir; private final boolean be_verbose; private final boolean are_valid; @@ -19,9 +19,9 @@ public class Parameters ( "HaStABeL to IDP\n" + "USAGE:\n" - + "\thastabel2idp.sh <OUTPUT_FILE> <FILES|OPTIONS>+\n" + + "\tjava -jar hastabel2idp.jar <OUTPUT_DIR> <FILES|OPTIONS>+\n" + "PARAMETERS:\n" - + "\t- <OUTPUT_FILE>\tFile to write the solutions in.\n" + + "\t- <OUTPUT_DIR>\tDirectory in which to write the IDP model.\n" + "\t- <FILES>\tList of files to be loaded.\n" + "OPTIONS:\n" + "\t- -v|--verbose\tPrint informative messages to STDOUT.\n" @@ -49,7 +49,7 @@ public class Parameters print_usage(); property_file = new String(); - output_file = new String(); + output_dir = new String(); are_valid = false; be_verbose = false; @@ -60,11 +60,11 @@ public class Parameters has_pro_file = false; has_error = false; - output_file = args[0]; + output_dir = args[0]; if ( - (output_file.equals("-v") || output_file.equals("--verbose")) + (output_dir.equals("-v") || output_dir.equals("--verbose")) /* || ... */ ) { @@ -80,9 +80,9 @@ public class Parameters if ( - output_file.endsWith(".lvl") - || output_file.endsWith(".mod") - || output_file.endsWith(".pro") + output_dir.endsWith(".lvl") + || output_dir.endsWith(".mod") + || output_dir.endsWith(".pro") ) { print_usage(); @@ -94,7 +94,7 @@ public class Parameters + " output file, meaning that one of the input files was about to" + " be written over. So likely, in fact, that we'll abort here. The" + " output file you indicated was \"" - + output_file + + output_dir + "\"." ); @@ -179,9 +179,24 @@ public class Parameters return property_file; } - public String get_output_file () + public String get_output_dir () { - return output_file; + return output_dir; + } + + public String get_theory_filename () + { + return output_dir + "/theory.txt"; + } + + public String get_vocabulary_filename () + { + return output_dir + "/vocabulary.txt"; + } + + public String get_structure_filename () + { + return output_dir + "/structure.txt"; } public boolean be_verbose () diff --git a/src/hastabel2idp/idp/Project.java b/src/hastabel2idp/idp/Project.java new file mode 100644 index 0000000..fcb37b5 --- /dev/null +++ b/src/hastabel2idp/idp/Project.java @@ -0,0 +1,97 @@ +package hastabel2idp.idp; + +import hastabel2idp.Parameters; + +import hastabel.World; + +import hastabel.lang.Predicate; +import hastabel.lang.Type; +import hastabel.lang.Element; + +import java.util.Collection; +import java.util.List; + +public class Project +{ + private final Vocabulary vocabulary; + private final Structure structure; + private final Theory theory; + + public Project (final Parameters params) + { + vocabulary = new Vocabulary(params.get_vocabulary_filename()); + structure = new Structure(params.get_structure_filename()); + theory = new Theory(params.get_theory_filename()); + } + + public void generate (final World world) + { + final Collection<Type> types; + final Collection<Predicate> predicates; + + types = world.get_types_manager().get_all(); + predicates = world.get_predicates_manager().get_all(); + + vocabulary.write_header(); + structure.write_header(); + theory.write_header(); + + for (final Type type: types) + { + if (type.is_used()) + { + vocabulary.add_type(type); + structure.add_type(type); + } + } + + for (final Predicate predicate: predicates) + { + if (predicate.is_used()) + { + final Collection<List<Type>> relevant_signatures; + final Collection<List<Type>> partial_signatures; + + relevant_signatures = predicate.get_relevant_signatures(); + partial_signatures = predicate.get_partial_signatures(); + + vocabulary.add_predicate + ( + predicate, + relevant_signatures, + partial_signatures + ); + + structure.add_predicate + ( + predicate, + relevant_signatures, + partial_signatures + ); + + } + } + + vocabulary.write_footer(); + structure.write_footer(); + theory.write_footer(); + } + + public static String signature_to_suffix (final List<Type> signature) + { + final StringBuilder sb; + + sb = new StringBuilder(); + + for (final Type t: signature) + { + if (t != null) + { + sb.append("_"); + sb.append(t.get_name()); + } + } + + return sb.toString(); + } +} diff --git a/src/hastabel2idp/idp/Structure.java b/src/hastabel2idp/idp/Structure.java new file mode 100644 index 0000000..56a8360 --- /dev/null +++ b/src/hastabel2idp/idp/Structure.java @@ -0,0 +1,148 @@ +package hastabel2idp.idp; + +import hastabel2idp.OutputFile; + +import hastabel.lang.Predicate; +import hastabel.lang.Type; +import hastabel.lang.Element; + +import java.util.Collection; +import java.util.List; + +public class Structure +{ + private final OutputFile out; + + public Structure (final String filename) + { + out = OutputFile.new_output_file(filename); + } + + public void write_header () + { + out.write("structure S:V {"); + out.insert_newline(); + } + + public void write_footer () + { + out.write("}"); + out.insert_newline(); + } + + public void add_type (final Type type) + { + boolean is_first_member; + + out.write(" "); + out.write(type.get_name()); + out.write("={"); + out.insert_newline(); + + is_first_member = true; + + for (final Element member: type.get_elements()) + { + if (is_first_member) + { + is_first_member = false; + out.write(" "); + } + else + { + out.write(";"); + out.insert_newline(); + out.write(" "); + } + + out.write(member.get_name()); + } + + out.insert_newline(); + out.write(" }"); + out.insert_newline(); + } + + private void add_predicate_signature + ( + final Predicate predicate, + final List<Type> signature, + final boolean is_partial + ) + { + boolean is_first_member; + final Collection<List<Element>> relevant_members; + + out.write(" "); + out.write(predicate.get_name()); + out.write(Project.signature_to_suffix(signature)); + out.write("={"); + out.insert_newline(); + + is_first_member = true; + + if (is_partial) + { + relevant_members = predicate.get_relevant_partial_members(signature); + } + else + { + relevant_members = predicate.get_relevant_members(signature); + } + + for (final List<Element> member: relevant_members) + { + boolean is_first_member_param; + + is_first_member_param = true; + + if (is_first_member) + { + is_first_member = false; + out.write(" "); + } + else + { + out.write(";"); + out.insert_newline(); + out.write(" "); + } + + for (final Element member_param: member) + { + if (is_first_member_param) + { + is_first_member_param = false; + } + else + { + out.write(", "); + } + + out.write(member_param.get_name()); + } + } + + out.insert_newline(); + out.write(" }"); + out.insert_newline(); + } + + public void add_predicate + ( + final Predicate predicate, + final Collection<List<Type>> relevant_signatures, + final Collection<List<Type>> partial_signatures + ) + { + for (final List<Type> signature: relevant_signatures) + { + add_predicate_signature(predicate, signature, false); + } + + for (final List<Type> signature: partial_signatures) + { + add_predicate_signature(predicate, signature, true); + } + } +} diff --git a/src/hastabel2idp/idp/Theory.java b/src/hastabel2idp/idp/Theory.java new file mode 100644 index 0000000..fa48c5d --- /dev/null +++ b/src/hastabel2idp/idp/Theory.java @@ -0,0 +1,32 @@ +package hastabel2idp.idp; + +import hastabel2idp.OutputFile; + +//import hastabel.lang.Predicate; +//import hastabel.lang.Type; +//import hastabel.lang.Element; + +//import java.util.Collection; +//import java.util.List; + +public class Theory +{ + private final OutputFile out; + + public Theory (final String filename) + { + out = OutputFile.new_output_file(filename); + } + + public void write_header () + { + out.write("theory T:V {"); + out.insert_newline(); + } + + public void write_footer () + { + out.write("}"); + out.insert_newline(); + } +} diff --git a/src/hastabel2idp/idp/Vocabulary.java b/src/hastabel2idp/idp/Vocabulary.java new file mode 100644 index 0000000..c9c9e44 --- /dev/null +++ b/src/hastabel2idp/idp/Vocabulary.java @@ -0,0 +1,95 @@ +package hastabel2idp.idp; + +import hastabel2idp.OutputFile; + +import hastabel.lang.Predicate; +import hastabel.lang.Type; +import hastabel.lang.Element; + +import java.util.Collection; +import java.util.List; + +public class Vocabulary +{ + private final OutputFile out; + + public Vocabulary (final String filename) + { + out = OutputFile.new_output_file(filename); + } + + public void write_header () + { + out.write("vocabulary V {"); + out.insert_newline(); + } + + public void write_footer () + { + out.write("}"); + out.insert_newline(); + } + + public void add_type (final Type type) + { + out.write(" type "); + out.write(type.get_name()); + out.insert_newline(); + } + + private void add_predicate_signature + ( + final Predicate predicate, + final List<Type> signature + ) + { + boolean is_first; + + is_first = true; + + out.write(" "); + out.write(predicate.get_name()); + out.write(Project.signature_to_suffix(signature)); + out.write("("); + + for (final Type sig_type: signature) + { + if (sig_type == null) + { + continue; + } + + if (is_first) + { + is_first = false; + } + else + { + out.write(", "); + } + + out.write(sig_type.get_name()); + } + + out.write(")"); + out.insert_newline(); + } + + public void add_predicate + ( + final Predicate predicate, + final Collection<List<Type>> relevant_signatures, + final Collection<List<Type>> partial_signatures + ) + { + for (final List<Type> signature: relevant_signatures) + { + add_predicate_signature(predicate, signature); + } + + for (final List<Type> signature: partial_signatures) + { + add_predicate_signature(predicate, signature); + } + } +} |


