Commit a53a2920 authored by Jonathan Shahen's avatar Jonathan Shahen

speed upgrades and timing

parent 421fe54e
Pipeline #7959 skipped
......@@ -87,5 +87,9 @@ public class ConverterCUI {
+ OptionString.LOGLEVEL.c("debug") + OptionString.SPECEXT.c(fileExt.Mohawk_T) + "!exit");
System.out.println(OptionString.TO_ALL.c() + OptionString.SPECFILE.c("data/TestsuiteC") + OptionString.BULK.c()
+ OptionString.LOGLEVEL.c("debug") + OptionString.SPECEXT.c(fileExt.Mohawk_T) + "!exit");
System.out.println("");
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");
}
}
......@@ -15,13 +15,13 @@ import mohawk.converter.generated.MohawkTARBACParser;
import mohawk.converter.helper.SpecHelper;
import mohawk.converter.logging.MohawkCSVFileFormatter;
import mohawk.converter.logging.MohawkConsoleFormatter;
import mohawk.converter.logging.MohawkTiming;
import mohawk.converter.to.asaptime.ConvertToASAPTimeNSA;
import mohawk.converter.to.asaptime.ConvertToASAPTimeSA;
import mohawk.converter.to.mohawk.ConvertToMohawk;
import mohawk.converter.to.tred.ConvertToTRole;
import mohawk.global.FileExtensions;
import mohawk.global.pieces.MohawkT;
import mohawk.global.timing.MohawkTiming;
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
......@@ -119,7 +119,7 @@ public class ConverterInstance {
logger.info("[CONVERTING] Converting to ASAPTime NSA... " + specFile.getName());
/* Timing */timing.startTimer("convertToASAPTimeNSA (" + i + ")");
ConvertToASAPTimeNSA toASAPTimeNSA = new ConvertToASAPTimeNSA();
ConvertToASAPTimeNSA toASAPTimeNSA = new ConvertToASAPTimeNSA(timing);
toASAPTimeNSA.convert(m, specFile, true);
if (toASAPTimeNSA.lastError == null) {
......@@ -138,7 +138,7 @@ public class ConverterInstance {
logger.info("[CONVERTING] Converting to ASAPTime SA... " + specFile.getName());
/* Timing */timing.startTimer("convertToASAPTimeSA (" + i + ")");
ConvertToASAPTimeSA toASAPTimeSA = new ConvertToASAPTimeSA();
ConvertToASAPTimeSA toASAPTimeSA = new ConvertToASAPTimeSA(timing);
toASAPTimeSA.convert(m, specFile, true);
if (toASAPTimeSA.lastError == null) {
......@@ -156,7 +156,7 @@ public class ConverterInstance {
logger.info("[CONVERTING] Converting to TRole... " + specFile.getName());
/* Timing */timing.startTimer("convertToTRole (" + i + ")");
ConvertToTRole toTRole = new ConvertToTRole();
ConvertToTRole toTRole = new ConvertToTRole(timing);
toTRole.convert(m, specFile, true);
if (toTRole.lastError == null) {
......@@ -174,7 +174,7 @@ public class ConverterInstance {
// logger.info("[CONVERTING] Converting to TRule... " + specFile.getName());
// /* Timing */timing.startTimer("convertToTRule (" + i + ")");
//
// ConvertToTRule toTRule = new ConvertToTRule();
// ConvertToTRule toTRule = new ConvertToTRule(timing);
// toTRule.convert(m, specFile, true);
//
// if (toTRule.lastError == null) {
......@@ -192,7 +192,7 @@ public class ConverterInstance {
logger.info("[CONVERTING] Converting to Mohawk... " + specFile.getName());
/* Timing */timing.startTimer("convertToMohawk (" + i + ")");
ConvertToMohawk toMohawk = new ConvertToMohawk();
ConvertToMohawk toMohawk = new ConvertToMohawk(timing);
toMohawk.convert(m, specFile, true);
if (toMohawk.lastError == null) {
......
package mohawk.converter.logging;
import java.util.Hashtable;
import java.util.Map;
public class MohawkTiming {
private Map<String, Long> timings = new Hashtable<String, Long>();
public Map<String, Long> getTimings() {
return timings;
}
public void blankTimer(String key) {
timings.put(key, null);
}
public void startTimer(String key) {
timings.put(key, System.currentTimeMillis());
}
public void stopTimer(String key) {
if (timings.get(key) != null) {
timings.put(key, System.currentTimeMillis() - (Long) timings.get(key));
}
}
/**
* Used to indicate that the operation failed after a certain amount of time by calculating the time difference and
* then multiplying by -1. If the elapsed time is zero then the time will be changed to -1.
*
* @param key
*/
public void cancelTimer(String key) {
Long time = (System.currentTimeMillis() - (Long) timings.get(key)) * -1;
if (time == 0) {
time = (long) -1;
}
timings.put(key, time);
}
@Override
public String toString() {
StringBuilder t = new StringBuilder();
int i = 0;
for (Map.Entry<String, Long> entry : timings.entrySet()) {
t.append(entry.getKey() + ": ");
t.append(entry.getValue());
t.append("ms");
i++;
if (i != timings.size()) {
t.append(" - ");
}
}
return t.toString();
}
}
......@@ -11,6 +11,7 @@ import mohawk.converter.logging.MohawkConsoleFormatter;
import mohawk.converter.to.asaptime.ConvertToASAPTimeNSA;
import mohawk.converter.to.asaptime.ConvertToASAPTimeSA;
import mohawk.global.pieces.MohawkT;
import mohawk.global.timing.MohawkTiming;
import org.junit.Before;
import org.junit.Test;
......@@ -18,6 +19,7 @@ import org.junit.Test;
public class ConvertToASAPTimeTests {
public static final Logger logger = Logger.getLogger("mohawk-converter");
public MohawkTiming timing = new MohawkTiming();
public String folderbase = "data/regressiontests/";
public BooleanErrorListener error = new BooleanErrorListener();
......@@ -39,7 +41,7 @@ public class ConvertToASAPTimeTests {
MohawkTARBACParser parser = loadfile(file);
MohawkT m = parser.mohawkT;
ConvertToASAPTimeNSA converter = new ConvertToASAPTimeNSA();
ConvertToASAPTimeNSA converter = new ConvertToASAPTimeNSA(timing);
String convertedStr = converter.convert(m, null, false);
......@@ -56,7 +58,7 @@ public class ConvertToASAPTimeTests {
MohawkTARBACParser parser = loadfile(file);
MohawkT m = parser.mohawkT;
ConvertToASAPTimeSA converter = new ConvertToASAPTimeSA();
ConvertToASAPTimeSA converter = new ConvertToASAPTimeSA(timing);
String convertedStr = converter.convert(m, null, false);
......
......@@ -10,6 +10,7 @@ import mohawk.converter.generated.MohawkTARBACParser;
import mohawk.converter.logging.MohawkConsoleFormatter;
import mohawk.converter.to.tred.ConvertToTRole;
import mohawk.global.pieces.MohawkT;
import mohawk.global.timing.MohawkTiming;
import org.junit.Before;
import org.junit.Test;
......@@ -17,6 +18,7 @@ import org.junit.Test;
public class ConvertToTRoleTests {
public static final Logger logger = Logger.getLogger("mohawk-converter");
public MohawkTiming timing = new MohawkTiming();
public String folderbase = "data/regressiontests/";
public BooleanErrorListener error = new BooleanErrorListener();
......@@ -38,7 +40,7 @@ public class ConvertToTRoleTests {
MohawkTARBACParser parser = loadfile(file);
MohawkT m = parser.mohawkT;
ConvertToTRole converter = new ConvertToTRole();
ConvertToTRole converter = new ConvertToTRole(timing);
String convertedStr = converter.convert(m, null, false);
......
......@@ -10,12 +10,20 @@ import java.nio.file.Paths;
import mohawk.global.FileExtensions;
import mohawk.global.pieces.MohawkT;
import mohawk.global.timing.MohawkTiming;
public class ConvertTo {
public FileExtensions fileExt = new FileExtensions(); // allows for custom file extensions
public String convertedStr = null;
public String lastError = null;
public Integer numberOfRules = null;
public MohawkTiming timing = null;
protected String tPrefix = "ConvertTo";
public ConvertTo(MohawkTiming timing) {
this.timing = timing;
}
public String convert(MohawkT m, File f, Boolean writeToFile) {
return null;
......
......@@ -4,4 +4,4 @@ CONFIG <numRoles> <numTimeslots>
GOAL <goalRole> <goalTimeslot>
<! !>
<! can_revoke t3 , 28 -5 -3 -8 ; t5 , 14 !>
<rules_sa:{r|<r.ruleType> t<r.adminTime> , <if(r.precondition)><r.precondition; separator=" "><else>true<endif> ; t<r.roleTime> , <r.role><\n>}>
\ No newline at end of file
<!rules_sa:{r|<r.ruleType> t<r.adminTime> , <if(r.precondition)><r.precondition; separator=" "><else>true<endif> ; t<r.roleTime> , <r.role><\n>}!>
\ No newline at end of file
......@@ -19,36 +19,48 @@ import mohawk.global.pieces.Rule;
import mohawk.global.pieces.TimeSlot;
import mohawk.global.pieces.reduced.query.ASAPTimeNSA_Query;
import mohawk.global.pieces.reduced.rules.ASAPTimeNSA_Rule;
import mohawk.global.timing.MohawkTiming;
import org.stringtemplate.v4.ST;
public class ConvertToASAPTimeNSA extends ConvertTo {
public static final Logger logger = Logger.getGlobal();
public ConvertToASAPTimeNSA(MohawkTiming timing) {
super(timing);
tPrefix = "ConvertToASAPTimeNSA";
}
@Override
public String convert(MohawkT m, File file, Boolean writeToFile) {
try {
ArrayList<ASAPTimeNSA_Rule> rules = new ArrayList<ASAPTimeNSA_Rule>();
// Deep Copy Helpers
/* Timing */timing.startTimer(tPrefix + "_" + "deepcopy");
RoleHelper roleHelper = new RoleHelper(m.roleHelper);
TimeIntervalHelper timeIntervalHelper = new TimeIntervalHelper(m.timeIntervalHelper);
Query workableQuery = new Query(m.query);
/* Timing */timing.stopTimer(tPrefix + "_" + "deepcopy");
/* Timing */timing.startTimer(tPrefix + "_" + "sortRolesReduceTimeslots");
// Reduce Roles to Integers
roleHelper.allowZeroRole = false; // roles start from 1 NOT 0!
roleHelper.setupSortedRoles();
// Reduce TimeIntervals to Timeslots
timeIntervalHelper.allowZeroTimeslot = false; // time-slots start from t1 NOT t0!
timeIntervalHelper.reduceToTimeslots();
/* Timing */timing.stopTimer(tPrefix + "_" + "sortRolesReduceTimeslots");
// Convert Query to Reduced ASAPTime NSA Query and add any extra rules as needed
ASAPTimeNSA_Query query = toASAPTimeNSA_Query(workableQuery, rules, roleHelper, timeIntervalHelper);
// Convert Rules into Reduced ASAPTime NSA Rules
/* Timing */timing.startTimer(tPrefix + "_" + "toASAPTimeNSA_Rules");
for (Rule r : m.getAllRules()) {
rules.addAll(toASAPTimeNSA_Rules(r, roleHelper, timeIntervalHelper));
}
/* Timing */timing.stopTimer(tPrefix + "_" + "toASAPTimeNSA_Rules");
// Stats and logging
numberOfRules = rules.size();
......@@ -56,6 +68,7 @@ public class ConvertToASAPTimeNSA extends ConvertTo {
logger.finest("Reduced Rules: " + rules);
// Generate the Converted String
/* Timing */timing.startTimer(tPrefix + "_" + "template");
String template = ConvertTo.readFile(this.getClass().getResource("ASAPTimeNSATemplate.st"));
ST st = new ST(template);
st.add("numRoles", roleHelper.size());
......@@ -64,15 +77,18 @@ public class ConvertToASAPTimeNSA extends ConvertTo {
st.add("goalTimeslot", query.goalTimeslot);
st.add("rules_nsa", rules);
convertedStr = st.render();
/* Timing */timing.stopTimer(tPrefix + "_" + "template");
// Potentially write the converted string out to "file + getFileExtenstion()"
if (writeToFile) {
/* Timing */timing.startTimer(tPrefix + "_" + "Files.write");
File convertedFile = new File(file.getAbsolutePath() + getFileExtenstion());
if (!convertedFile.exists()) {
convertedFile.createNewFile();
}
Files.write(convertedFile.toPath(), convertedStr.getBytes());
/* Timing */timing.stopTimer(tPrefix + "_" + "Files.write");
}
lastError = null;
} catch (Exception e) {
......
package mohawk.converter.to.asaptime;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.SortedSet;
......@@ -19,12 +22,18 @@ import mohawk.global.pieces.Rule;
import mohawk.global.pieces.TimeSlot;
import mohawk.global.pieces.reduced.query.ASAPTimeSA_Query;
import mohawk.global.pieces.reduced.rules.ASAPTimeSA_Rule;
import mohawk.global.timing.MohawkTiming;
import org.stringtemplate.v4.ST;
public class ConvertToASAPTimeSA extends ConvertTo {
public static final Logger logger = Logger.getGlobal();
public ConvertToASAPTimeSA(MohawkTiming timing) {
super(timing);
tPrefix = "ConvertToASAPTimeSA";
}
@Override
public String convert(MohawkT m, File file, Boolean writeToFile) {
try {
......@@ -32,28 +41,39 @@ public class ConvertToASAPTimeSA extends ConvertTo {
ArrayList<Rule> workableRules = new ArrayList<Rule>();
// Deep Copy Helpers
/* Timing */timing.startTimer(tPrefix + "_" + "deepcopy");
RoleHelper roleHelper = new RoleHelper(m.roleHelper);
TimeIntervalHelper timeIntervalHelper = new TimeIntervalHelper(m.timeIntervalHelper);
Query workableQuery = new Query(m.query);
/* Timing */timing.stopTimer(tPrefix + "_" + "deepcopy");
// removeEnableDisableRules and alwaysOnRules are Deep Copies
/* Timing */timing.startTimer(tPrefix + "_" + "removeEnableDisableRules");
workableRules = roleHelper.removeEnableDisableRules(m.getAllRules());
/* Timing */timing.stopTimer(tPrefix + "_" + "removeEnableDisableRules");
/* Timing */timing.startTimer(tPrefix + "_" + "alwaysOnRules");
workableRules = roleHelper.alwaysOnRules(workableRules, workableQuery, timeIntervalHelper);
/* Timing */timing.stopTimer(tPrefix + "_" + "alwaysOnRules");
/* Timing */timing.startTimer(tPrefix + "_" + "reduceToTimeslots");
// Reduce TimeIntervals to Timeslots
timeIntervalHelper.allowZeroTimeslot = false; // time-slots start from t1 NOT t0!
timeIntervalHelper.reduceToTimeslots();
// Reduce Roles to Integers
roleHelper.allowZeroRole = false; // roles start from 1 NOT 0!
roleHelper.setupSortedRoles();
/* Timing */timing.stopTimer(tPrefix + "_" + "reduceToTimeslots");
// Convert Query to Reduced ASAPTime SA Query and add any extra rules as needed
ASAPTimeSA_Query query = toASAPTimeSA_Query(workableQuery, workableRules, roleHelper, timeIntervalHelper);
// Convert Rules into Reduced ASAPTime SA Rules
/* Timing */timing.startTimer(tPrefix + "_" + "toASAPTimeSA_Rules");
for (Rule r : workableRules) {
newRules.addAll(toASAPTimeSA_Rules(r, roleHelper, timeIntervalHelper));
}
/* Timing */timing.stopTimer(tPrefix + "_" + "toASAPTimeSA_Rules");
// Stats and logging
numberOfRules = newRules.size();
......@@ -61,23 +81,41 @@ public class ConvertToASAPTimeSA extends ConvertTo {
logger.finest("Reduced Rules: " + newRules);
// Generate the Converted String
/* 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("numTimeslots", timeIntervalHelper.sizeReduced());
st.add("goalRole", query.goalRole);
st.add("goalTimeslot", query.goalTimeslot);
st.add("rules_sa", newRules);
//st.add("rules_sa", newRules);
convertedStr = st.render();
/* Timing */timing.stopTimer(tPrefix + "_" + "template");
// Potentially write the converted string out to "file + getFileExtenstion()"
if (writeToFile) {
/* Timing */timing.startTimer(tPrefix + "_" + "Files.write");
File convertedFile = new File(file.getAbsolutePath() + getFileExtenstion());
if (!convertedFile.exists()) {
/*if (!convertedFile.exists()) {
convertedFile.createNewFile();
}
Files.write(convertedFile.toPath(), convertedStr.getBytes());
Files.write(convertedFile.toPath(), convertedStr.getBytes());*/
Charset charset = Charset.forName("US-ASCII");
try (BufferedWriter writer = Files.newBufferedWriter(convertedFile.toPath(), charset)) {
writer.write(convertedStr);
for (ASAPTimeSA_Rule r : newRules) {
writer.write(r.getString());
}
writer.flush();
writer.close();
} catch (IOException x) {
System.err.format("IOException: %s%n", x);
}
/* Timing */timing.stopTimer(tPrefix + "_" + "Files.write");
}
lastError = null;
} catch (Exception e) {
......
......@@ -20,12 +20,18 @@ import mohawk.global.pieces.TimeSlot;
import mohawk.global.pieces.reduced.query.Mohawk_Query;
import mohawk.global.pieces.reduced.rules.MohawkCA_Rule;
import mohawk.global.pieces.reduced.rules.MohawkCR_Rule;
import mohawk.global.timing.MohawkTiming;
import org.stringtemplate.v4.ST;
public class ConvertToMohawk extends ConvertTo {
public static final Logger logger = Logger.getLogger("mohawk-converter");
public ConvertToMohawk(MohawkTiming timing) {
super(timing);
tPrefix = "ConvertToMohawk";
}
@Override
public String convert(MohawkT m, File f, Boolean writeToFile) {
try {
......@@ -34,23 +40,38 @@ public class ConvertToMohawk extends ConvertTo {
ArrayList<Rule> workableRules = new ArrayList<Rule>();
// Deep Copy Helpers
/* Timing */timing.startTimer(tPrefix + "_" + "deepcopy");
RoleHelper roleHelper = new RoleHelper(m.roleHelper);
TimeIntervalHelper timeIntervalHelper = new TimeIntervalHelper(m.timeIntervalHelper);
Query workableQuery = new Query(m.query);
/* Timing */timing.stopTimer(tPrefix + "_deepcopy");
/* Timing */timing.startTimer(tPrefix + "_removeEnableDisableRules");
workableRules = roleHelper.removeEnableDisableRules(m.getAllRules()); // (3)
/* 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);
/* Timing */timing.startTimer(tPrefix + "_alwaysOnRules");
workableRules = roleHelper.alwaysOnRules(workableRules, workableQuery, timeIntervalHelper); // (4)
/* Timing */timing.stopTimer(tPrefix + "_alwaysOnRules");
/* Timing */timing.startTimer(tPrefix + "_removeUninvokablePrecondtions");
workableRules = roleHelper.removeUninvokablePrecondtions(workableRules, false); // (6)
/* Timing */timing.stopTimer(tPrefix + "_removeUninvokablePrecondtions");
/* Timing */timing.startTimer(tPrefix + "_removePrecondtionsCanRevoke");
workableRules = roleHelper.removePrecondtionsCanRevoke(workableRules, false); // (6)
/* Timing */timing.stopTimer(tPrefix + "_removePrecondtionsCanRevoke");
/* Timing */timing.startTimer(tPrefix + "_removeTemporality");
RoleHelper roleHelperTemporality = new RoleHelper(roleHelper);
ArrayList<Object> reducedRules = roleHelperTemporality.removeTemporality(workableRules, timeIntervalHelper);
/* Timing */timing.stopTimer(tPrefix + "_removeTemporality");
// Convert Rules into Reduced Mohawk CA and CR Rules (5)
/* Timing */timing.startTimer(tPrefix + "_convertRules");
for (Object r : reducedRules) {
if (r instanceof MohawkCA_Rule) {
newCARules.add((MohawkCA_Rule) r);
......@@ -60,12 +81,14 @@ public class ConvertToMohawk extends ConvertTo {
throw new IllegalArgumentException("Can only convert Can Assign and Can Revoke Rules in Mohawk");
}
}
/* Timing */timing.stopTimer(tPrefix + "_convertRules");
boolean queryFound = query.finalize(roleHelperTemporality);
assert (queryFound == true) : "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]));
......@@ -77,14 +100,17 @@ public class ConvertToMohawk extends ConvertTo {
st.add("canrevoke", newCRRules);
st.add("canassign", newCARules);
convertedStr = st.render();
/* Timing */timing.stopTimer(tPrefix + "_template");
if (writeToFile) {
/* Timing */timing.startTimer(tPrefix + "_writeFile");
File convertedFile = new File(f.getAbsolutePath() + getFileExtenstion());
if (!convertedFile.exists()) {
convertedFile.createNewFile();
}
Files.write(convertedFile.toPath(), convertedStr.getBytes());
/* Timing */timing.stopTimer(tPrefix + "_writeFile");
}
lastError = null;
......
package mohawk.converter.to.tred;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Logger;
......@@ -19,12 +23,18 @@ import mohawk.global.pieces.RuleType;
import mohawk.global.pieces.TimeSlot;
import mohawk.global.pieces.reduced.query.TRole_Query;
import mohawk.global.pieces.reduced.rules.TRole_Rule;
import mohawk.global.timing.MohawkTiming;
import org.stringtemplate.v4.ST;
public class ConvertToTRole extends ConvertTo {
public static final Logger logger = Logger.getGlobal();
public ConvertToTRole(MohawkTiming timing) {
super(timing);
tPrefix = "ConvertToTRole";
}
@Override
public String convert(MohawkT m, File file, Boolean writeToFile) {
try {
......@@ -32,12 +42,19 @@ public class ConvertToTRole extends ConvertTo {
ArrayList<Rule> workableRules = new ArrayList<Rule>();
// Deep Copy Helpers
/* Timing */timing.startTimer(tPrefix + "_" + "deepcopy");
RoleHelper roleHelper = new RoleHelper(m.roleHelper);
TimeIntervalHelper timeIntervalHelper = new TimeIntervalHelper(m.timeIntervalHelper);
Query workableQuery = new Query(m.query);
/* Timing */timing.stopTimer(tPrefix + "_" + "deepcopy");
/* Timing */timing.startTimer(tPrefix + "_" + "removeEnableDisableRules");
workableRules = roleHelper.removeEnableDisableRules(m.getAllRules());
/* Timing */timing.stopTimer(tPrefix + "_" + "removeEnableDisableRules");
/* Timing */timing.startTimer(tPrefix + "_" + "alwaysOnRules");
workableRules = roleHelper.alwaysOnRules(workableRules, workableQuery, timeIntervalHelper);
/* Timing */timing.stopTimer(tPrefix + "_" + "alwaysOnRules");
// Reduce Roles to Integers
roleHelper.allowZeroRole = false; // roles start from 1 NOT 0!
......@@ -55,15 +72,19 @@ public class ConvertToTRole extends ConvertTo {
logger.fine("Reduced Rules: " + newRules);
// Generate the Converted String
/* 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("numTimeSlots", timeIntervalHelper.getNumberOfTimeSlots());
st.add("goalStates", query.goalRoles);
String header = st.render();
/* Timing */timing.stopTimer(tPrefix + "_" + "template");
// Potentially write the converted string out to "file + getFileExtenstion()"
if (writeToFile) {
/* Timing */timing.startTimer(tPrefix + "_" + "FileOutputStream");
/*
File convertedFile = new File(file.getAbsolutePath() + getFileExtenstion());
FileOutputStream out = new FileOutputStream(convertedFile);
......@@ -75,9 +96,25 @@ public class ConvertToTRole extends ConvertTo {
}
out.flush();
out.close();
*/
Charset charset = Charset.forName("US-ASCII");
try (BufferedWriter writer = Files.newBufferedWriter(
Paths.get(file.getAbsolutePath() + getFileExtenstion()), charset)) {
writer.write(header);
// Convert Rules into Reduced ASAPTime NSA Rules
for (Rule r : workableRules) {
//newRules.add(new TRole_Rule(r, roleHelper, timeIntervalHelper));
writer.write(new TRole_Rule(r, roleHelper, timeIntervalHelper).getString());
}
} catch (IOException x) {
System.err.format("IOException: %s%n", x);
}
convertedStr = "Outputed string to the file: " + convertedFile.getAbsolutePath();
convertedStr = "Outputed string to the file: " + file.getAbsolutePath() + getFileExtenstion();
/* Timing */timing.stopTimer(tPrefix + "_" + "FileOutputStream");
} else {
/* Timing */timing.startTimer(tPrefix + "_" + "StringBuilder");
StringBuilder sb = new StringBuilder(header);
// Convert Rules into Reduced ASAPTime NSA Rules
......@@ -86,6 +123,7 @@ public class ConvertToTRole extends ConvertTo {
sb.append(new TRole_Rule(r, roleHelper, timeIntervalHelper).getString());
}
convertedStr = sb.toString();
/* Timing */timing.stopTimer(tPrefix + "_" + "StringBuilder");
}
lastError = null;
......
package mohawk.converter.to.tred;
import mohawk.global.timing.MohawkTiming;
public class ConvertToTRule extends ConvertToTRole {
public ConvertToTRule(MohawkTiming timing) {
super(timing);
}
@Override
public String getFileExtenstion() {
return fileExt.TRule;
......
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