Commit 8d0c8b29 authored by Jonathan Shahen's avatar Jonathan Shahen

Initial Test of Conversion to Mohawk successful with Ahn's test.st

parent a53a2920
......@@ -7,3 +7,4 @@ logs/
*.ranise
*.ranise_nsa
*.ranise_sa
data/
package mohawk.converter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Logger;
......@@ -11,6 +15,7 @@ import org.apache.commons.cli.Options;
public class ConverterCUI {
public static final Logger logger = Logger.getLogger("mohawk-converter");
public static FileExtensions fileExt = new FileExtensions();
public static String previousCommandFilename = "ConverterCUIPreviousCommand.txt";
public static void main(String[] args) {
ConverterInstance inst = new ConverterInstance();
......@@ -26,8 +31,10 @@ public class ConverterCUI {
System.out.print("Enter Commandline Argument ('!exit' to run): ");
String quotedStr = "";
StringBuilder fullCommand = new StringBuilder();
while (true) {
cmd = user_input.next();
fullCommand.append(cmd + " ");
if (quotedStr.isEmpty() && cmd.startsWith("\"")) {
System.out.println("Starting: " + cmd);
......@@ -55,14 +62,24 @@ public class ConverterCUI {
System.out.println("Commands: " + argv);
FileWriter fw;
try {
fw = new FileWriter(previousCommandFilename, false);
fw.write(fullCommand.toString());
fw.close();
} catch (IOException e) {
System.out.println("[ERROR] Unable to write out previous command to: " + previousCommandFilename);
}
inst.run(argv.toArray(new String[1]));
}
public static void printCommonCommands() {
System.out.println("\n\n--- Common Commands ---");
System.out.println(OptionString.TO_ALL.c() + OptionString.SPECFILE.c() + "data/regressiontests/positive1.spec "
+ OptionString.LOGLEVEL.c() + "debug !exit");
System.out.println(OptionString.TO_ALL.c()
+ OptionString.SPECFILE.c("data/Mohawk/mixednocr/test04.mohawk.mohawk.T") + OptionString.LOGLEVEL.c()
+ "debug !exit");
System.out.println(OptionString.TO_ALL.c() + OptionString.SPECFILE.c() + "data/regressiontests/positive2.spec "
+ OptionString.LOGLEVEL.c() + "debug !exit");
System.out.println(OptionString.TO_ALL.c() + OptionString.SPECFILE.c() + "data/regressiontests/positive3.spec "
......@@ -91,5 +108,37 @@ public class ConverterCUI {
System.out.println(OptionString.TO_MOHAWK.c() + OptionString.SPECFILE.c("data/TestsuiteC/split1")
+ OptionString.BULK.c() + OptionString.LOGLEVEL.c("debug") + OptionString.SPECEXT.c(fileExt.Mohawk_T)
+ "!exit");
System.out.println("");
System.out.println(OptionString.TO_TROLE.c() + OptionString.SPECFILE.c("data/Mahesh/easy/")
+ OptionString.BULK.c() + OptionString.LOGLEVEL.c("debug") + OptionString.SPECEXT.c(".spec") + "!exit");
System.out.println(OptionString.TO_MOHAWK.c() + OptionString.SPECFILE.c("data/Mahesh/easy/")
+ OptionString.BULK.c() + OptionString.LOGLEVEL.c("debug") + OptionString.SPECEXT.c(".spec") + "!exit");
System.out.println(OptionString.TO_ASAPTIME_SA.c() + OptionString.SPECFILE.c("data/Mahesh/easy/")
+ OptionString.BULK.c() + OptionString.LOGLEVEL.c("debug") + OptionString.SPECEXT.c(".spec") + "!exit");
System.out.println(OptionString.TO_ASAPTIME_NSA.c() + OptionString.SPECFILE.c("data/Mahesh/easy/")
+ OptionString.BULK.c() + OptionString.LOGLEVEL.c("debug") + OptionString.SPECEXT.c(".spec") + "!exit");
System.out.println(OptionString.TO_TROLE.c() + OptionString.SPECFILE.c("data/Mahesh/medium/")
+ OptionString.BULK.c() + OptionString.LOGLEVEL.c("debug") + OptionString.SPECEXT.c(".spec") + "!exit");
System.out.println(OptionString.TO_MOHAWK.c() + OptionString.TO_ASAPTIME_SA.c()
+ OptionString.TO_ASAPTIME_NSA.c() + OptionString.SPECFILE.c("data/Mahesh/medium/")
+ OptionString.BULK.c() + OptionString.LOGLEVEL.c("debug") + OptionString.SPECEXT.c(".spec") + "!exit");
System.out.println(OptionString.TO_TROLE.c() + OptionString.SPECFILE.c("data/Mahesh/hard/")
+ OptionString.BULK.c() + OptionString.LOGLEVEL.c("debug") + OptionString.SPECEXT.c(".spec") + "!exit");
System.out.println(OptionString.TO_MOHAWK.c() + OptionString.TO_ASAPTIME_SA.c()
+ OptionString.TO_ASAPTIME_NSA.c() + OptionString.SPECFILE.c("data/Mahesh/hard/")
+ OptionString.BULK.c() + OptionString.LOGLEVEL.c("debug") + OptionString.SPECEXT.c(".spec") + "!exit");
System.out.println("");
try {
BufferedReader bfr = new BufferedReader(new FileReader(previousCommandFilename));
String previousCmd = bfr.readLine();
bfr.close();
System.out.println("Previous Command: " + previousCmd);
} catch (IOException e) {
System.out.println("[ERROR] Unable to load previous command!");
}
}
}
......@@ -8,6 +8,7 @@ import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
......@@ -35,8 +36,8 @@ public class ConverterInstance {
private static final String VERSION = "v1.0.2";
private static final String AUTHORS = "Jonathan Shahen <jmshahen@uwaterloo.ca>";
// Logger Fields
public static final Logger logger = Logger.getLogger("mohawk-converter");
private String Logger_filepath = "mohawk-converter.log";
public static final Logger logger = Logger.getLogger("mohawk");
private String Logger_filepath = "mohawk-converter.log.csv";
private String Logger_folderpath = "logs";
private ConsoleHandler consoleHandler = new ConsoleHandler();
private Level LoggerLevel;
......@@ -230,6 +231,10 @@ public class ConverterInstance {
}
logger.info("[EOF] Converter Instance done running");
for (Handler h : logger.getHandlers()) {
h.close(); //must call h.close or a .LCK file will remain.
}
return 0;
}
......@@ -340,7 +345,7 @@ public class ConverterInstance {
// Logging Level
logger.setUseParentHandlers(false);
consoleHandler.setFormatter(new MohawkConsoleFormatter());
setLoggerLevel(Level.INFO); // Default Level
setLoggerLevel(Level.FINE); // Default Level
if (cmd.hasOption(OptionString.LOGLEVEL.toString())) {
String loglevel = cmd.getOptionValue(OptionString.LOGLEVEL.toString());
if (loglevel.equalsIgnoreCase("quiet")) {
......
package mohawk.converter.testing;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Arrays;
import mohawk.global.pieces.Role;
import mohawk.global.pieces.Rule;
import mohawk.global.pieces.RuleType;
import mohawk.global.pieces.TimeSlot;
import org.junit.Test;
public class ConverterBasicTests {
@Test
public void test_toMohawk_Query() {
ArrayList<Rule> rules = new ArrayList<Rule>();
testFunc1(rules);
assertEquals(1, rules.size());
}
private void testFunc1(ArrayList<Rule> rules) {
Rule newRule = new Rule();
newRule._type = RuleType.ASSIGN;
newRule._adminRole = Role.allRoles();
newRule._adminTimeInterval = new TimeSlot(1);
newRule._preconditions.add(new Role("test"));
newRule._roleSchedule = new ArrayList<TimeSlot>(Arrays.asList(new TimeSlot(1)));
newRule._role = new Role("testGoalRole");
rules.add(newRule);
}
}
......@@ -64,7 +64,7 @@ public class ConverterRegressionTests {
assertEquals("Not the correct number of Rule in CanDisable Block", 0, m.canDisable.size());
// Correct number of roles and timeslots
assertEquals("Incorrect number of Roles", new Integer(4), m.roleHelper.size());
assertEquals("Incorrect number of Roles", new Integer(4), m.roleHelper.numberOfRoles());
// t0, t1, t2, t5
assertEquals("Incorrect number of Timeslots", new Integer(4), m.timeIntervalHelper.size());
// t0, t1, t2, t5
......@@ -90,7 +90,7 @@ public class ConverterRegressionTests {
assertEquals("Not the correct number of Rule in CanDisable Block", 2, m.canDisable.size());
// Correct number of roles and timeslots
assertEquals("Incorrect number of Roles", new Integer(5), m.roleHelper.size()); //
assertEquals("Incorrect number of Roles", new Integer(5), m.roleHelper.numberOfRoles()); //
// t0, t1, t2, t5
assertEquals("Incorrect number of Timeslots", new Integer(4), m.timeIntervalHelper.size());
// t0, t1, t2, t5
......@@ -116,7 +116,7 @@ public class ConverterRegressionTests {
assertEquals("Not the correct number of Rule in CanDisable Block", 2, m.canDisable.size());
// Correct number of roles and timeslots
assertEquals("Incorrect number of Roles", new Integer(6), m.roleHelper.size()); //
assertEquals("Incorrect number of Roles", new Integer(6), m.roleHelper.numberOfRoles()); //
// t0, t1, t2, t5
assertEquals("Incorrect number of Timeslots", new Integer(4), m.timeIntervalHelper.size());
// t0, t1, t2, t5
......
......@@ -71,7 +71,7 @@ public class ConvertToASAPTimeNSA extends ConvertTo {
/* Timing */timing.startTimer(tPrefix + "_" + "template");
String template = ConvertTo.readFile(this.getClass().getResource("ASAPTimeNSATemplate.st"));
ST st = new ST(template);
st.add("numRoles", roleHelper.size());
st.add("numRoles", roleHelper.numberOfRoles());
st.add("numTimeslots", timeIntervalHelper.sizeReduced());
st.add("goalRole", query.goalRole);
st.add("goalTimeslot", query.goalTimeslot);
......
......@@ -84,7 +84,7 @@ public class ConvertToASAPTimeSA extends ConvertTo {
/* Timing */timing.startTimer(tPrefix + "_" + "template");
String template = ConvertTo.readFile(this.getClass().getResource("ASAPTimeSATemplate.st"));
ST st = new ST(template);
st.add("numRoles", roleHelper.size());
st.add("numRoles", roleHelper.numberOfRoles());
st.add("numTimeslots", timeIntervalHelper.sizeReduced());
st.add("goalRole", query.goalRole);
st.add("goalTimeslot", query.goalTimeslot);
......@@ -107,7 +107,7 @@ public class ConvertToASAPTimeSA extends ConvertTo {
writer.write(convertedStr);
for (ASAPTimeSA_Rule r : newRules) {
writer.write(r.getString());
writer.write(r.getString() + "\n");
}
writer.flush();
writer.close();
......
......@@ -51,12 +51,15 @@ public class ConvertToMohawk extends ConvertTo {
/* Timing */timing.stopTimer(tPrefix + "_removeEnableDisableRules");
// Convert Query to Reduced ASAPTime SA Query and add any extra rules as needed
Mohawk_Query query = toMohawk_Query(workableQuery, workableRules, roleHelper, timeIntervalHelper);
//Mohawk_Query query = toMohawk_Query(workableQuery, workableRules, roleHelper, timeIntervalHelper);
/* Timing */timing.startTimer(tPrefix + "_alwaysOnRules");
workableRules = roleHelper.alwaysOnRules(workableRules, workableQuery, timeIntervalHelper); // (4)
/* Timing */timing.stopTimer(tPrefix + "_alwaysOnRules");
// Convert Query to Reduced ASAPTime SA Query and add any extra rules as needed
Mohawk_Query query = toMohawk_Query(workableQuery, workableRules, roleHelper, timeIntervalHelper);
/* Timing */timing.startTimer(tPrefix + "_removeUninvokablePrecondtions");
workableRules = roleHelper.removeUninvokablePrecondtions(workableRules, false); // (6)
/* Timing */timing.stopTimer(tPrefix + "_removeUninvokablePrecondtions");
......@@ -84,14 +87,14 @@ public class ConvertToMohawk extends ConvertTo {
/* Timing */timing.stopTimer(tPrefix + "_convertRules");
boolean queryFound = query.finalize(roleHelperTemporality);
assert (queryFound == true) : "The query could not find the Goal Role!";
if (queryFound == false) { throw new Exception("The query could not find the Goal Role!"); }
// Stats and logging
numberOfRules = newCARules.size() + newCRRules.size();
/* Timing */timing.startTimer(tPrefix + "_template");
String template = ConvertTo.readFile(this.getClass().getResource("mohawkTemplate.st"));
ST st = new ST(template);
st.add("roles", roleHelperTemporality._roles.toArray(new Role[0]));
st.add("roles", roleHelperTemporality.getArray());
st.add("users", new String[] { "AdminUser", "User" });
st.add("specuser", "User");
st.add("specrole", query.specRole);
......@@ -126,9 +129,9 @@ public class ConvertToMohawk extends ConvertTo {
}
private Mohawk_Query toMohawk_Query(Query query, ArrayList<Rule> newCARules, RoleHelper roleHelper,
TimeIntervalHelper timeIntervalHelper) {
TimeIntervalHelper timeIntervalHelper) throws Exception {
// Checking Conditions
assert (query._roles.size() > 0) : "The query must have atleast one Goal Role";
if (query._roles.size() <= 0) { throw new Exception("The query must have atleast one Goal Role"); }
Role newGoalRole = roleHelper.addUniqueRole("goalRole");
Mohawk_Query q = new Mohawk_Query(newGoalRole, query._timeslot);
......
......@@ -75,7 +75,7 @@ public class ConvertToTRole extends ConvertTo {
/* Timing */timing.startTimer(tPrefix + "_" + "template");
String template = ConvertTo.readFile(this.getClass().getResource("uzunTemplate.st"));
ST st = new ST(template);
st.add("numRoles", roleHelper.size());
st.add("numRoles", roleHelper.numberOfRoles());
st.add("numTimeSlots", timeIntervalHelper.getNumberOfTimeSlots());
st.add("goalStates", query.goalRoles);
String header = st.render();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment