Skip to content
Snippets Groups Projects
Commit f863c9e7 authored by René Schöne's avatar René Schöne
Browse files

New heuristic port order and flexible driver.

- heuristic port order compares input and output ports and sorts input ports depending on number of matching bits
- Driver has now three required arguments: result type, computation and port order
- JastAddTest extended to use the two port orders, and some refactoring
parent 311e30f5
Branches
No related tags found
No related merge requests found
......@@ -4,3 +4,9 @@
* This is a general purpose Gradle build.
* Learn how to create Gradle builds at https://guides.gradle.org/creating-new-gradle-builds/
*/
task benchmark(type: Exec) {
workingDir 'scripts'
commandLine 'python3', 'run.py', '-m'
}
......@@ -3,4 +3,4 @@ default=./gradlew installDist
skipTests=./gradlew installDist
[run]
cmd=JAVA_OPTS="-Xms4g" build/install/RelationalRAGSolution/bin/RelationalRAGSolution
cmd=JAVA_OPTS="-Xms4g" build/install/RelationalRAGSolution/bin/RelationalRAGSolution bdt case natural
......@@ -21,7 +21,8 @@ aspect Navigation {
inh ASTNode ASTNode.root();
eq TruthTable.getPort().root() = this;
eq TruthTable.getRow().root() = this;
eq TruthTable.getPortOrder().root() = this;
eq TruthTable.getNaturalPortOrder().root() = this;
eq TruthTable.getHeuristicPortOrder().root() = this;
eq BDT.getTree().root() = this;
......@@ -36,7 +37,8 @@ aspect Navigation {
inh TruthTable Cell.containingTruthTable();
eq TruthTable.getPort().containingTruthTable() = this;
eq TruthTable.getRow().containingTruthTable() = this;
eq TruthTable.getPortOrder().containingTruthTable() = this;
eq TruthTable.getNaturalPortOrder().containingTruthTable() = this;
eq TruthTable.getHeuristicPortOrder().containingTruthTable() = this;
syn List<OutputPort> TruthTable.outputPorts() {
List<OutputPort> result = new ArrayList<>();
......
......@@ -135,18 +135,6 @@ aspect SimpleBDT {
return result;
}
syn PortOrder TruthTable.getPortOrder() {
// a port order defines the order, in which the input ports are evaluated
// for the simple case, this order is defined by the list in which the ports are defined
PortOrder result = new PortOrder();
for (Port port : getPortList()) {
if (port.isInput()) {
result.addPort(port.asInput());
}
}
return result;
}
public Tree TruthTable.subTree(List<InputPort> remainingPorts) {
return null;
}
......
LocatedElement ::= <Location:String>;
TruthTable:LocatedElement ::= <Name:String> Port:Port* Row:Row* /PortOrder/;
TruthTable : LocatedElement ::= <Name:String> Port:Port* Row:Row* /NaturalPortOrder:PortOrder/ /HeuristicPortOrder:PortOrder/ ;
rel TruthTable.PortOrder -> PortOrder ;
abstract Port : LocatedElement ::= <Name:String>;
InputPort : Port;
OutputPort : Port;
......
package de.tudresden.inf.st.ttc19;
import de.tudresden.inf.st.ttc19.jastadd.model.BDD;
import de.tudresden.inf.st.ttc19.jastadd.model.BDT;
import de.tudresden.inf.st.ttc19.jastadd.model.TruthTable;
import de.tudresden.inf.st.ttc19.parser.TruthTableParser;
......@@ -11,19 +12,116 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
public class Driver {
private static String RunIndex;
private static String Model;
private static String Tool;
private static String Computation;
private static String PortOrderType;
private static long stopwatch;
private static SolutionHandler solutionHandler;
private static TruthTable truthTable;
private static String ModelPath;
private static Logger logger = LogManager.getLogger(Driver.class);
private static abstract class SolutionHandler {
void computeSolution(TruthTable tt) {
stopwatch = System.nanoTime();
computeSolution0(tt);
stopwatch = System.nanoTime() - stopwatch;
report(BenchmarkPhase.Run);
}
protected abstract void computeSolution0(TruthTable tt);
void writeResult() throws IOException {
String result = getResultAsString();
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get("output.xmi"))) {
writer.write(result);
}
}
protected abstract String getResultAsString();
}
private static class BDTSolutionHandler extends SolutionHandler {
BDT lastResult;
@Override
protected void computeSolution0(TruthTable tt) {
switch (Computation) {
case "simple":
lastResult = tt.simpleBDT();
break;
case "case":
lastResult = tt.caseBDT();
break;
default:
System.err.println("Invalid computation type for BDT: " + Computation);
}
}
@Override
protected String getResultAsString() {
StringBuilder sb = new StringBuilder();
lastResult.writeBDT(sb);
return sb.toString();
}
}
private static class BDDSolutionHandler extends SolutionHandler {
BDD lastResult;
@Override
protected void computeSolution0(TruthTable tt) {
switch (Computation) {
case "case":
lastResult = tt.caseBDD();
break;
case "reduction":
lastResult = tt.reductionOBDD();
break;
default:
System.err.println("Invalid computation type for BDD: " + Computation);
}
}
@Override
protected String getResultAsString() {
StringBuilder sb = new StringBuilder();
lastResult.writeBDD(sb);
return sb.toString();
}
}
public static void main(String[] args) {
if (args.length != 3) {
System.err.println("Usage: java -jar Driver RESULT_TYPE COMPUTATION PORT_ORDER");
System.err.println("RESULT_TYPE = bdd|bdt");
System.err.println("COMPUTATION for bdt: simple|case. for bdd: case|reduction");
System.err.println("PORT_ORDER = natural|heuristic");
System.exit(1);
}
args = Arrays.stream(args).map(String::toLowerCase).toArray(String[]::new);
switch (args[0]) {
case "bdt":
solutionHandler = new BDTSolutionHandler();
break;
case "bdd":
solutionHandler = new BDDSolutionHandler();
break;
default:
System.err.println("Invalid result type: " + args[0]);
System.exit(1);
}
Computation = args[1];
PortOrderType = args[2];
try {
initialize();
load();
......@@ -39,6 +137,17 @@ public class Driver {
try (final FileInputStream stream = new FileInputStream(ModelPath)) {
truthTable = new TruthTableParser().parse(stream);
switch (PortOrderType) {
case "natural":
truthTable.setPortOrder(truthTable.getNaturalPortOrder());
break;
case "heuristic":
truthTable.setPortOrder(truthTable.getHeuristicPortOrder());
break;
default:
System.err.println("Invalid port order type: " + PortOrderType);
System.exit(1);
}
} catch (IOException e) {
logger.error("Unable to load model from '" + ModelPath + "'", e);
}
......@@ -60,16 +169,8 @@ public class Driver {
}
private static void run() throws IOException {
stopwatch = System.nanoTime();
BDT solution = truthTable.caseBDT();
stopwatch = System.nanoTime() - stopwatch;
report(BenchmarkPhase.Run);
StringBuilder bddBuilder = new StringBuilder();
solution.writeBDT(bddBuilder);
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get("output.xmi"))) {
writer.write(bddBuilder.toString());
}
solutionHandler.computeSolution(truthTable);
solutionHandler.writeResult();
}
private static void report(BenchmarkPhase phase) {
......
......@@ -2,6 +2,7 @@ package de.tudresden.inf.st.ttc19;
import de.tudresden.inf.st.ttc19.jastadd.model.BDD;
import de.tudresden.inf.st.ttc19.jastadd.model.BDT;
import de.tudresden.inf.st.ttc19.jastadd.model.PortOrder;
import de.tudresden.inf.st.ttc19.jastadd.model.TruthTable;
import de.tudresden.inf.st.ttc19.parser.TruthTableParser;
import org.apache.logging.log4j.LogManager;
......@@ -14,6 +15,7 @@ import org.junit.jupiter.params.provider.MethodSource;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
......@@ -109,73 +111,63 @@ class JastAddTest {
@ParameterizedTest
@MethodSource("truthTableFiles")
void testSimpleBDT(String pathName) throws IOException {
TruthTable tt = loadTruthTable(pathName);
File inputFile = new File(pathName);
BDT simpleBDT = tt.simpleBDT();
StringBuilder simpleBuilder = new StringBuilder();
simpleBDT.writeBDT(simpleBuilder);
Path outputPath = Files.createTempFile("relrag-test-simpleBDT", ".bddmodel");
outputPath.toFile().deleteOnExit();
try (BufferedWriter writer = Files.newBufferedWriter(outputPath)) {
writer.write(simpleBuilder.toString());
}
validate(inputFile.getAbsolutePath(), outputPath.toString());
testBDT(pathName, "relrag-test-natural-simpleBDT", TruthTable::simpleBDT, TruthTable::getNaturalPortOrder);
testBDT(pathName, "relrag-test-heuristic-simpleBDT", TruthTable::simpleBDT, TruthTable::getHeuristicPortOrder);
}
@ParameterizedTest
@MethodSource("truthTableFiles")
void testCaseBDT(String pathName) throws IOException {
TruthTable tt = loadTruthTable(pathName);
File inputFile = new File(pathName);
BDT caseBdd = tt.caseBDT();
StringBuilder bddBuilder = new StringBuilder();
caseBdd.writeBDT(bddBuilder);
Path outputPath = Files.createTempFile("relrag-test-caseBDT", ".bddmodel");
outputPath.toFile().deleteOnExit();
try (BufferedWriter writer = Files.newBufferedWriter(outputPath)) {
writer.write(bddBuilder.toString());
testBDT(pathName, "relrag-test-natural-caseBDT", TruthTable::caseBDT, TruthTable::getNaturalPortOrder);
testBDT(pathName, "relrag-test-heuristic-caseBDT", TruthTable::caseBDT, TruthTable::getHeuristicPortOrder);
}
validate(inputFile.getAbsolutePath(), outputPath.toString());
@ParameterizedTest
@MethodSource("truthTableFiles")
void testCaseBDD(String pathName) throws IOException {
testBDD(pathName, "relrag-test-natural-caseBDD", TruthTable::caseBDD, TruthTable::getNaturalPortOrder);
testBDD(pathName, "relrag-test-heuristic-caseBDD", TruthTable::caseBDD, TruthTable::getHeuristicPortOrder);
}
@ParameterizedTest
@MethodSource("truthTableFiles")
void testCaseBDD(String pathName) throws IOException {
void testReductionOBDD(String pathName) throws IOException {
testBDD(pathName, "relrag-test-natural-reductionOBDD", TruthTable::reductionOBDD, TruthTable::getNaturalPortOrder);
testBDD(pathName, "relrag-test-heuristic-reductionOBDD", TruthTable::reductionOBDD, TruthTable::getHeuristicPortOrder);
}
private void testBDT(String pathName, String outputFileName,
Function<TruthTable, BDT> generate, Function<TruthTable, PortOrder> portOrder)
throws IOException{
TruthTable tt = loadTruthTable(pathName);
tt.setPortOrder(portOrder.apply(tt));
File inputFile = new File(pathName);
BDD caseBdd = tt.caseBDD();
BDT caseBdt = generate.apply(tt);
StringBuilder bddBuilder = new StringBuilder();
caseBdd.writeBDD(bddBuilder);
Path outputPath = Files.createTempFile("relrag-test-caseBDD", ".bddmodel");
// outputPath.toFile().deleteOnExit();
StringBuilder bdtBuilder = new StringBuilder();
caseBdt.writeBDT(bdtBuilder);
Path outputPath = Files.createTempFile(outputFileName, ".bddmodel");
outputPath.toFile().deleteOnExit();
try (BufferedWriter writer = Files.newBufferedWriter(outputPath)) {
writer.write(bddBuilder.toString());
writer.write(bdtBuilder.toString());
}
Assertions.assertTrue(new Validator().validate(tt, caseBdd));
validate(inputFile.getAbsolutePath(), outputPath.toString());
}
@ParameterizedTest
@MethodSource("truthTableFiles")
void testReductionOBDD(String pathName) throws IOException {
private void testBDD(String pathName, String outputFileName,
Function<TruthTable, BDD> generate, Function<TruthTable, PortOrder> portOrder)
throws IOException{
TruthTable tt = loadTruthTable(pathName);
tt.setPortOrder(portOrder.apply(tt));
File inputFile = new File(pathName);
BDD caseBdd = tt.reductionOBDD();
BDD caseBdd = generate.apply(tt);
StringBuilder bddBuilder = new StringBuilder();
caseBdd.writeBDD(bddBuilder);
Path outputPath = Files.createTempFile("relrag-test-reductionOBDD", ".bddmodel");
Path outputPath = Files.createTempFile(outputFileName, ".bddmodel");
outputPath.toFile().deleteOnExit();
try (BufferedWriter writer = Files.newBufferedWriter(outputPath)) {
writer.write(bddBuilder.toString());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment