summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2018-05-25 10:31:18 +0200
committerNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2018-05-25 10:31:18 +0200
commitfcff0374a7134e20b7c51d2d8b2889aab21b83e4 (patch)
tree6f2fe73a503031859f0f14394d7439b40c1fab0a
parentdd77774b6529f6050ab551cec370b896912c20fe (diff)
Starting to write some IDP...
-rw-r--r--src/hastabel2idp/IDP.java226
-rw-r--r--src/hastabel2idp/Main.java67
-rw-r--r--src/hastabel2idp/OutputFile.java131
3 files changed, 405 insertions, 19 deletions
diff --git a/src/hastabel2idp/IDP.java b/src/hastabel2idp/IDP.java
new file mode 100644
index 0000000..ceb9d7d
--- /dev/null
+++ b/src/hastabel2idp/IDP.java
@@ -0,0 +1,226 @@
+package hastabel2idp;
+
+import hastabel.World;
+
+import hastabel.lang.Predicate;
+import hastabel.lang.Type;
+import hastabel.lang.Element;
+
+import java.util.Collection;
+import java.util.Set;
+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 Set<List<Type>> relevant_signatures;
+
+ relevant_signatures = predicate.get_relevant_signatures();
+
+ add_predicate_to_vocabulary(vocabulary, predicate, relevant_signatures);
+ add_predicate_to_structure(structure, predicate, relevant_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_to_vocabulary
+ (
+ final OutputFile vocabulary,
+ final Predicate predicate,
+ final Set<List<Type>> relevant_signatures
+ )
+ {
+ for (final List<Type> signature: relevant_signatures)
+ {
+ boolean is_first;
+
+ is_first = true;
+
+ vocabulary.write(" ");
+ vocabulary.write(predicate.get_name());
+ vocabulary.write("(");
+
+ for (final Type sig_type:signature)
+ {
+ 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_structure
+ (
+ final OutputFile structure,
+ final Predicate predicate,
+ final Set<List<Type>> relevant_signatures
+ )
+ {
+ boolean is_first_member;
+
+ structure.write(" ");
+ structure.write(predicate.get_name());
+ structure.write("={");
+ structure.insert_newline();
+
+ is_first_member = true;
+
+ for
+ (
+ final List<Element> member:
+ predicate.get_relevant_members(relevant_signatures)
+ )
+ {
+ 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();
+ }
+}
diff --git a/src/hastabel2idp/Main.java b/src/hastabel2idp/Main.java
index f8bea8d..b590cb4 100644
--- a/src/hastabel2idp/Main.java
+++ b/src/hastabel2idp/Main.java
@@ -22,6 +22,45 @@ public class Main
world = new World();
+ load_model(world, params);
+
+ if (!world.is_valid())
+ {
+ return;
+ }
+
+ write_idp(world, params);
+
+ System.out.println("# Done.");
+ }
+
+ private static void load_file (final World world, final String filename)
+ {
+ try
+ {
+ System.out.println("# Loading \"" + filename + "\"...");
+ world.load(filename);
+ }
+ catch (final IOException ioe)
+ {
+ System.err.println
+ (
+ "[E] IOException when loading \""
+ + filename
+ + "\":\n"
+ + ioe.getMessage()
+ );
+
+ world.invalidate();
+ }
+ }
+
+ private static void load_model
+ (
+ final World world,
+ final Parameters params
+ )
+ {
for (final String level_file: params.get_level_files())
{
load_file(world, level_file);
@@ -67,28 +106,18 @@ public class Main
world.invalidate();
}
-
- System.out.println("# Done.");
}
- private static void load_file (final World world, final String filename)
+ private static void write_idp
+ (
+ final World world,
+ final Parameters params
+ )
{
- try
- {
- System.out.println("# Loading \"" + filename + "\"...");
- world.load(filename);
- }
- catch (final IOException ioe)
- {
- System.err.println
- (
- "[E] IOException when loading \""
- + filename
- + "\":\n"
- + ioe.getMessage()
- );
+ System.out.println("# Generating IDP...");
- world.invalidate();
- }
+ IDP.generate(world, params);
+
+ OutputFile.close_all();
}
}
diff --git a/src/hastabel2idp/OutputFile.java b/src/hastabel2idp/OutputFile.java
new file mode 100644
index 0000000..79af6e9
--- /dev/null
+++ b/src/hastabel2idp/OutputFile.java
@@ -0,0 +1,131 @@
+package hastabel2idp;
+
+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 (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();
+ }
+ }
+}