summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornsensfel <SpamShield0@noot-noot.org>2018-05-28 17:43:50 +0200
committernsensfel <SpamShield0@noot-noot.org>2018-05-28 17:43:50 +0200
commit32e195da6941067000c8574515b97c50bcbd4466 (patch)
tree2c0a601bc2959d4ec1ad7abb22a9ceb96a427dd2
parent84831716add27b53954142c3bbd72539b9168021 (diff)
downloadhastabel2idp-32e195da6941067000c8574515b97c50bcbd4466.zip
hastabel2idp-32e195da6941067000c8574515b97c50bcbd4466.tar.bz2
Converts the IDP result to HaStABeL.
-rw-r--r--src/hastabel2idp/HastabelResult.java132
-rw-r--r--src/hastabel2idp/Main.java26
-rw-r--r--src/hastabel2idp/Parameters.java5
3 files changed, 158 insertions, 5 deletions
diff --git a/src/hastabel2idp/HastabelResult.java b/src/hastabel2idp/HastabelResult.java
new file mode 100644
index 0000000..7b40e2d
--- /dev/null
+++ b/src/hastabel2idp/HastabelResult.java
@@ -0,0 +1,132 @@
+package hastabel2idp;
+
+import hastabel2idp.OutputFile;
+
+import hastabel.lang.Type;
+
+import java.util.List;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class HastabelResult
+{
+ private final OutputFile model, level;
+
+ public HastabelResult (final Parameters params)
+ {
+ model =
+ OutputFile.new_output_file
+ (
+ params.get_output_dir()
+ + "99_"
+ + params.get_property_name()
+ + ".sol.mod"
+ );
+
+ level =
+ OutputFile.new_output_file
+ (
+ params.get_output_dir()
+ + "99_"
+ + params.get_property_name()
+ + ".sol.lvl"
+ );
+ }
+
+ public void add_solution
+ (
+ final String predicate,
+ final List<Type> signature,
+ final String idp_solution
+ )
+ {
+ add_solution_to_level(predicate, signature);
+ add_solution_to_model(predicate, idp_solution);
+ level.write(predicate);
+ }
+
+ private void add_solution_to_level
+ (
+ final String predicate,
+ final List<Type> signature
+ )
+ {
+ boolean is_first;
+
+ is_first = true;
+
+ level.write("add_predicate ");
+ level.write(predicate);
+ level.write("(");
+
+ for (final Type type: signature)
+ {
+ if (is_first)
+ {
+ is_first = false;
+ }
+ else
+ {
+ level.write(", ");
+ }
+
+ level.write(type.get_name());
+ }
+
+ level.write(")");
+ level.insert_newline();
+ }
+
+ private void add_solution_to_model
+ (
+ final String predicate,
+ final String idp_solution
+ )
+ {
+ final Pattern pattern;
+ final Matcher matcher;
+
+ pattern = Pattern.compile(predicate + " = \\{(.*)\\}");
+ matcher = pattern.matcher(idp_solution);
+
+ if (matcher.find())
+ {
+ final String[] solutions;
+
+ solutions = matcher.group(1).split(";");
+
+ System.out.println("# " + solutions.length + " solutions found.");
+
+ for (final String solution: solutions)
+ {
+ boolean is_first;
+
+ is_first = true;
+
+ model.write(predicate);
+ model.write("(");
+
+ for (final String param: solution.split(","))
+ {
+ if (is_first)
+ {
+ is_first = false;
+ }
+ else
+ {
+ model.write(", ");
+ }
+ model.write(param.trim());
+ }
+
+ model.write(")");
+ model.insert_newline();
+ }
+ }
+ else
+ {
+ System.out.println("# No solutions found.");
+ }
+ }
+}
diff --git a/src/hastabel2idp/Main.java b/src/hastabel2idp/Main.java
index f4201de..c6105ce 100644
--- a/src/hastabel2idp/Main.java
+++ b/src/hastabel2idp/Main.java
@@ -5,6 +5,8 @@ import hastabel2idp.idp.Project;
import hastabel.World;
import hastabel.lang.Formula;
+import java.util.stream.Collectors;
+
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
@@ -16,9 +18,12 @@ public class Main
final Parameters params;
final World world;
final Formula property;
+ final HastabelResult result;
+ final String idp_predicate_result;
System.out.println("#### HaStABeL to IDP ####");
System.out.println("# Parsing parameters...");
+
params = new Parameters(args);
if (!params.are_valid())
@@ -67,12 +72,23 @@ public class Main
System.out.println("# Running IDP...");
- System.out.println
+ idp_predicate_result = run_idp(world, params);
+
+ System.out.println("# Adding result to model...");
+
+ result = new HastabelResult(params);
+
+ result.add_solution
(
- "# Result: "
- + run_idp(world, params)
+ params.get_property_name(),
+ world.get_variables_manager().get_all_seeked().stream().map
+ (
+ variable -> variable.get_type()
+ ).collect(Collectors.toList()),
+ idp_predicate_result
);
+ System.out.println("# Done.");
OutputFile.close_all();
}
@@ -164,7 +180,7 @@ public class Main
project = new Project(params);
- project.generate("my_property", world, property);
+ project.generate(params.get_property_name(), world, property);
}
private static String run_idp
@@ -206,7 +222,7 @@ public class Main
)
).start();
- target = "my_property = {";
+ target = params.get_property_name() + " = {";
idp_stdout =
new BufferedReader(new InputStreamReader(idp.getInputStream()));
diff --git a/src/hastabel2idp/Parameters.java b/src/hastabel2idp/Parameters.java
index 18f5c45..a2f72ae 100644
--- a/src/hastabel2idp/Parameters.java
+++ b/src/hastabel2idp/Parameters.java
@@ -222,6 +222,11 @@ public class Parameters
return "./99_idp_main.txt";
}
+ public String get_property_name ()
+ {
+ return "my_property";
+ }
+
public boolean be_verbose ()
{
return be_verbose;