Commit 62c31a09 authored by Jonathan Shahen's avatar Jonathan Shahen

Speed boost and the ability to handle larger files without having a...

Speed boost and the ability to handle larger files without having a OutOfMemeoryError occurring (this is done by not compiling the string and writing each role to a FileOutputStream)

Comparison between straight FileOutputStream, RandomAccessFile and BufferedWriter:
FileOutputStream:     convertToTRole (1):   83,476 ms
BufferedWriter:           convertToTRole (1): 113,001 ms
RandomAccessFile: convertToTRole (1):   98,838 ms
parent 1b504e1d
package mohawk.converter.to.tred;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Logger;
......@@ -46,11 +46,6 @@ public class ConvertToTRole extends ConvertTo {
// Convert Query to Reduced ASAPTime SA Query and add any extra rules as needed
TRole_Query query = toTRole_Query(workableQuery, workableRules, roleHelper, timeIntervalHelper);
// Convert Rules into Reduced ASAPTime NSA Rules
for (Rule r : workableRules) {
newRules.add(new TRole_Rule(r, roleHelper, timeIntervalHelper));
}
// Finalize the Query
query.finalize(roleHelper);
......@@ -65,18 +60,33 @@ public class ConvertToTRole extends ConvertTo {
st.add("numRoles", roleHelper.size());
st.add("numTimeSlots", timeIntervalHelper.getNumberOfTimeSlots());
st.add("goalStates", query.goalRoles);
st.add("uzunRules", newRules);
convertedStr = st.render();
String header = st.render();
// Potentially write the converted string out to "file + getFileExtenstion()"
if (writeToFile) {
File convertedFile = new File(file.getAbsolutePath() + getFileExtenstion());
FileOutputStream out = new FileOutputStream(convertedFile);
// Convert Rules into Reduced ASAPTime NSA Rules
for (Rule r : workableRules) {
//newRules.add(new TRole_Rule(r, roleHelper, timeIntervalHelper));
out.write(new TRole_Rule(r, roleHelper, timeIntervalHelper).getString().getBytes());
}
out.flush();
out.close();
convertedStr = "Outputed string to the file: " + convertedFile.getAbsolutePath();
} else {
StringBuilder sb = new StringBuilder(header);
if (!convertedFile.exists()) {
convertedFile.createNewFile();
// Convert Rules into Reduced ASAPTime NSA Rules
for (Rule r : workableRules) {
//newRules.add(new TRole_Rule(r, roleHelper, timeIntervalHelper));
sb.append(new TRole_Rule(r, roleHelper, timeIntervalHelper).getString());
}
Files.write(convertedFile.toPath(), convertedStr.getBytes());
convertedStr = sb.toString();
}
lastError = null;
} catch (Exception e) {
StringWriter errors = new StringWriter();
......
......@@ -3,4 +3,4 @@
<goalStates; separator=" "><! Goal States {0,1} x numRoles !>
<! !>
<! RuleType StartTime FinsihTime RoleToGive !>
<uzunRules:{r|<r.ruleType> <r.startTime> <r.finishTime> <r.role><\n><\t><r.precondition; separator=" "><\n><\t><r.roleSchedule; separator=" "><\n>}>
\ No newline at end of file
<!uzunRules:{r|<r.ruleType> <r.startTime> <r.finishTime> <r.role><\n><\t><r.precondition; separator=" "><\n><\t><r.roleSchedule; separator=" "><\n>}!>
\ No newline at end of file
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