Commit 4b05d804 authored by Jonathan Shahen's avatar Jonathan Shahen

Gotten through the straight conversion to Uzun and have in-place the...

Gotten through the straight conversion to Uzun and have in-place the essentials to help convert the rest (See RoleHelper and TimeIntervalHelper)
parent 097e1613
......@@ -5,7 +5,11 @@
<classpathentry kind="lib" path="lib/commons-cli-1.2.jar"/>
<classpathentry kind="lib" path="lib/commons-lang3-3.3.2.jar"/>
<classpathentry kind="lib" path="lib/antlr-4.4-complete.jar"/>
<classpathentry kind="lib" path="lib/stringtemplate.jar"/>
<classpathentry kind="lib" path="lib/stringtemplate.jar">
<attributes>
<attribute name="javadoc_location" value="http://www.stringtemplate.org/api/"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="lib/junit-4.12.jar"/>
<classpathentry kind="lib" path="lib/hamcrest-core-1.3.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
......
......@@ -85,9 +85,12 @@ public class ConverterInstance {
if (specHelper.error.errorFound) {
logger.warning("[PARSING ERROR] Skipping this file due to a parsing error");
continue;
} else {
logger.info("No errors found while parsing file, continuing on to converting");
}
if (cmd.hasOption("ranise") || cmd.hasOption("all")) {
if (cmd.hasOption("to_ranise") || cmd.hasOption("to_all")) {
logger.info("[CONVERTING] Converting to Rainse...");
/* Timing */timing.startTimer("convertToRanise (" + i + ")");
ConvertToRanise toRanise = new ConvertToRanise();
......@@ -100,26 +103,49 @@ public class ConverterInstance {
}
/* Timing */timing.stopTimer("convertToRanise (" + i + ")");
logger.info("[CONVERTING] ...Done Converting to Rainse");
} else {
logger.info("[SKIP] Skipping converting to Ranise");
}
if (cmd.hasOption("uzun") || cmd.hasOption("all")) {
if (cmd.hasOption("to_uzun") || cmd.hasOption("to_all")) {
logger.info("[CONVERTING] Converting to Uzun...");
/* Timing */timing.startTimer("convertToUzun (" + i + ")");
// TODO: Convert imported spec files to Uzun format
ConvertToUzun toUzun = new ConvertToUzun();
String uzunStr = toUzun.convert(parser, specFile, true);
if (!uzunStr.startsWith("Error")) {
logger.info("[SUCCESS] Successfully converted to Uzun's Input Format");
} else {
logger.warning("[FAILURE] Failed to convert to Uzun's Input Format; Error code " + uzunStr);
}
/* Timing */timing.stopTimer("convertToUzun (" + i + ")");
logger.info("[CONVERTING] ...Done Converting to Uzun");
} else {
logger.info("[SKIP] Skipping converting to Uzun");
}
if (cmd.hasOption("mohawk") || cmd.hasOption("all")) {
if (cmd.hasOption("to_mohawk") || cmd.hasOption("to_all")) {
logger.info("[CONVERTING] Converting to Mohawk...");
/* Timing */timing.startTimer("convertToMohawk (" + i + ")");
// TODO: Convert imported spec files to Mohawk format
ConvertToMohawk toMohawk = new ConvertToMohawk();
String mohawkStr = toMohawk.convert(parser, specFile, true);
if (!mohawkStr.startsWith("Error")) {
logger.info("[SUCCESS] Successfully converted to Mohawk's Input Format");
} else {
logger.warning("[FAILURE] Failed to convert to Mohawk's Input Format; Error code " + mohawkStr);
}
/* Timing */timing.stopTimer("convertToMohawk (" + i + ")");
logger.info("[CONVERTING] ...Done Converting to Mohawk");
} else {
logger.info("[SKIP] Skipping converting to Mohawk");
}
}
/* Timing */timing.stopTimer("totalTime");
......
......@@ -11,6 +11,7 @@ import java.util.TreeSet;
import java.util.logging.Logger;
import org.apache.commons.lang3.StringUtils;
import mohawk.converter.helper.*;
import mohawk.converter.pieces.*;
}
......@@ -26,8 +27,8 @@ import mohawk.converter.pieces.*;
int tabsize = 1;
/* Global States */
public SortedSet<String> roles = new TreeSet<String>();
public SortedSet<Integer> timeslots = new TreeSet<Integer>();
public RoleHelper roleHelper = new RoleHelper();
public TimeIntervalHelper timeIntervalHelper = new TimeIntervalHelper();
/* Blocks */
public CanAssign canAssign = new CanAssign();
......@@ -51,18 +52,6 @@ init
stat
:
// {maxroles_found==false}?
//
// maxroles
// {maxroles_found=true;}
//
// |
// {maxtimeslots_found==false}?
//
// maxtimeslots
// {maxtimeslots_found=true;}
//
// |
{canassign_found==false}?
canassign
......@@ -100,17 +89,6 @@ stat
;
/*
maxroles
:
MaxRoles Colon INT
;
maxtimeslots
:
MaxTimeSlots Colon INT
;
*/
canassign
:
{logmsg("Entering Can Assign");tabsize++;}
......@@ -224,8 +202,7 @@ locals [Boolean adminAllRoles]
} else {
$r._adminRole = $adminRole.role;
}
$r._adminTimeSlots.setStartTime($ti.t0);
$r._adminTimeSlots.setFinishTime($ti.t1);
$r._adminTimeInterval = new TimeInterval($ti.t0, $ti.t1);
$r._preconditions = $pc.p;
$r._roleSchedule = $tsa.t;
$r._role = $role.role;
......@@ -312,7 +289,7 @@ timeslot returns [Integer val]
Timeslot
{
$val = new Integer($Timeslot.text.substring(1));
timeslots.add($val);
timeIntervalHelper.add(new TimeInterval($val));
}
;
......@@ -323,7 +300,7 @@ myrole returns [String rolet, Role role]
{
$rolet = $Role.getText();
$role = new Role($rolet);
roles.add($rolet);
roleHelper.add($role);
}
;
......
package mohawk.converter.helper;
import java.util.ArrayList;
import java.util.SortedSet;
import java.util.TreeSet;
import mohawk.converter.pieces.Role;
public class RoleHelper {
public SortedSet<Role> _roles = new TreeSet<Role>();
private ArrayList<Role> _sortedRoles = null;
public Boolean allowZeroRole = true;
public Integer size() {
return _roles.size();
}
public void add(Role role) {
_roles.add(role);
}
/**
*
* @param role
* @return returns the index from the sorted array of roles, -1 if not found
*/
public Integer indexOf(Role role) {
if (_sortedRoles == null) {
setupSortedRoles();
}
if (allowZeroRole) {
return _sortedRoles.indexOf(role);
} else {
Integer t = _sortedRoles.indexOf(role) + 1;
if (t == 0) { return -1; } //
return t;
}
}
public Role get(int index) {
if (_sortedRoles == null) {
setupSortedRoles();
}
return _sortedRoles.get(index);
}
private void setupSortedRoles() {
_sortedRoles = new ArrayList<Role>(_roles);
}
}
package mohawk.converter.helper;
import java.util.SortedSet;
import java.util.TreeSet;
import mohawk.converter.pieces.TimeInterval;
public class TimeIntervalHelper {
public SortedSet<TimeInterval> _timeslots = new TreeSet<TimeInterval>();
public Integer maxTimeSlot = 0;
public void add(TimeInterval ti) {
_timeslots.add(ti);
maxTimeSlot = Math.max(maxTimeSlot, ti._finish + 1);
}
/**
* Returns the maximum timeslot that has been seen returns -1 to indicate that no timeslots have been seen
*
* @return the greatest timeslot that is valid
*/
public Integer getMaxTimeSlotSeen() {
return maxTimeSlot - 1;
}
/**
* Returns the next available timeslot that has been seen returns 0 to indicate that no timeslots have been seen
*
* @return the greatest timeslot that is valid and hasn't been used
*/
public Integer getNextTimeslot() {
return maxTimeSlot;
}
public Integer size() {
return _timeslots.size();
}
@Override
public String toString() {
return _timeslots.toString();
}
}
package mohawk.converter.pieces;
public class LimitedRole extends Role {
public LimitedRole(Role role) {
super(role._name);
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Role) {
Role r = (Role) obj;
return this._name.equals(r._name);
}
if (obj instanceof LimitedRole) {
LimitedRole lr = (LimitedRole) obj;
return _name.equals(lr._name);
}
return false;
}
}
package mohawk.converter.pieces;
import java.util.ArrayList;
import java.util.Iterator;
import mohawk.converter.helper.RoleHelper;
public class Query {
public Integer _timeslot;
......@@ -10,4 +13,19 @@ public class Query {
public String toString() {
return "Query: t" + _timeslot + ", " + _roles;
}
public Integer[] toUzun(RoleHelper roleHelper) {
Integer[] roles = new Integer[roleHelper._roles.size()];
int j = 0;
for (Iterator<Role> i = roleHelper._roles.iterator(); i.hasNext(); j++) {
if (_roles.contains(i.next())) {
roles[j] = 1;
} else {
roles[j] = 0;
}
}
return roles;
}
}
......@@ -2,7 +2,7 @@ package mohawk.converter.pieces;
import java.util.HashMap;
public class Role {
public class Role implements Comparable<Role> {
public String _name;
public Boolean _not;
private Boolean _allRoles = false;
......@@ -34,4 +34,41 @@ public class Role {
if (_not) { return "-" + roles.get(_name); }
return "" + roles.get(_name);
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Role) {
Role r = (Role) obj;
if (_name.equals(r._name) && _not.equals(r._not) && _allRoles.equals(r._allRoles)) { return true; }
} else if (obj instanceof String) {
String r = (String) obj;
return _name.equals(r);
}
return false;
}
@Override
public int compareTo(Role o) {
if (this._allRoles && o._allRoles) { return 0; }
if (this._allRoles && !o._allRoles) { return 1; }
if (!this._allRoles && o._allRoles) { return -1; }
int nameCompare = _name.compareTo(o._name);
if (nameCompare == 0) {
if (this._not && !o._not) { return -1; }
if (!this._not && o._not) { return 1; }
return 0;
}
return nameCompare;
}
public LimitedRole toLimitedRole() {
return new LimitedRole(this);
}
}
......@@ -9,7 +9,7 @@ public class Rule {
public RuleType _type;
public Role _adminRole;
public TimeInterval _adminTimeSlots = new TimeInterval();
public TimeInterval _adminTimeInterval;
public ArrayList<Role> _preconditions = new ArrayList<Role>();
public ArrayList<Integer> _roleSchedule = new ArrayList<Integer>();
public Role _role;
......@@ -20,16 +20,20 @@ public class Rule {
s.append(_type.toRanise() + " ");
s.append(roles.get(_adminRole) + sep);
if (_adminTimeSlots.getStartTime() != _adminTimeSlots.getFinishTime()) {
if (_adminTimeInterval.getStartTime() != _adminTimeInterval.getFinishTime()) {
logger.severe("TODO: Implement the time interval splitting!");
}
s.append("t" + _adminTimeSlots.getStartTime() + sep);
s.append("t" + _adminTimeInterval.getStartTime() + sep);
s.append(Rule.toRanise(_preconditions, roles) + "; ");
// s.append("t" + _adminTimeSlots + sep);
return s.toString();
}
public Boolean noConditions() {
return _preconditions.size() == 0;
}
private static String toRanise(ArrayList<Role> pre, HashMap<String, Integer> roles) {
String sep = " ";
......@@ -51,7 +55,7 @@ public class Rule {
s.append(_type + " <");
s.append(_adminRole.toString() + ", ");
s.append("[t" + _adminTimeSlots.getStartTime() + "-t" + _adminTimeSlots.getFinishTime() + "], ");
s.append("[t" + _adminTimeInterval.getStartTime() + "-t" + _adminTimeInterval.getFinishTime() + "], ");
if (_preconditions.size() == 0) {
s.append("TRUE, ");
} else {
......
package mohawk.converter.pieces;
public enum RuleType {
ASSIGN("CanAssign"), DISABLE("CanDisable"), ENABLE("CanEnable"), REVOKE("CanRevoke");
ASSIGN("CanAssign", "A"), DISABLE("CanDisable", "D"), ENABLE("CanEnable", "E"), REVOKE("CanRevoke", "R");
private final String _name;
public final String _shortform;
RuleType(String name) {
RuleType(String name, String shortform) {
this._name = name;
this._shortform = shortform;
}
@Override
......@@ -20,4 +22,8 @@ public enum RuleType {
return tmp;
}
public String toUzun() {
return _shortform;
}
}
package mohawk.converter.pieces;
public class TimeInterval {
public class TimeInterval implements Comparable<TimeInterval> {
private final Boolean sortByStartTime = true;
public Integer _start = null;
public Integer _finish = null;
public TimeInterval() {
public TimeInterval(Integer startFinish) {
this(startFinish, startFinish);
}
public TimeInterval(Integer start, Integer finish) {
......@@ -33,6 +34,10 @@ public class TimeInterval {
return _start != null && _finish != null && _start <= _finish;
}
public Boolean isTimeslot() {
return _start.equals(_finish);
}
public Boolean isOverlapping(TimeInterval obj) {
return ((_start <= obj.getStartTime() && obj.getStartTime() <= _finish) || (obj.getStartTime() <= _start && _start <= obj
.getFinishTime()));
......@@ -50,4 +55,15 @@ public class TimeInterval {
public String toString() {
return "[t" + _start + ",t" + _finish + "]";
}
@Override
public int compareTo(TimeInterval o) {
if (equals(o)) { return 0; }
if (sortByStartTime) {
return _start.compareTo(o._start);
} else {
return _finish.compareTo(o._finish);
}
}
}
package mohawk.converter.pieces;
import mohawk.converter.helper.RoleHelper;
import mohawk.converter.helper.TimeIntervalHelper;
public class UzunRule {
public String ruleType;
public Integer startTime;
public Integer finishTime;
public Integer role;
public Integer[] precondition;
public UzunRule(Rule r, RoleHelper rh, TimeIntervalHelper tih) {
ruleType = r._type.toUzun();
startTime = r._adminTimeInterval.getStartTime();
finishTime = r._adminTimeInterval.getFinishTime();
role = rh.indexOf(r._role);
precondition = new Integer[rh.size()];
for (int i = 0; i < rh.size(); i++) {
Role role = rh.get(i);
// Ignore
int loc = r._preconditions.indexOf(role.toLimitedRole());
if (loc != -1) {
role = r._preconditions.get(loc);
if (role._not) {
precondition[i] = -1;
} else {
precondition[i] = 1;
}
} else {
precondition[i] = 0;
}
}
}
}
......@@ -60,9 +60,11 @@ public class RegressionTests {
assertEquals("Not the correct number of Rule in CanDisable Block", 0, parser.canDisable.size());
// Correct number of roles and timeslots
assertEquals("Incorrect number of Roles", 4, parser.roles.size());
assertEquals("Incorrect number of Timeslots", 4, parser.timeslots.size()); // t0, t1, t2, t5
assertEquals("Incorrect max Timeslot", new Integer(5), parser.timeslots.last()); // t0, t1, t2, t5
assertEquals("Incorrect number of Roles", new Integer(4), parser.roleHelper.size());
// t0, t1, t2, t5
assertEquals("Incorrect number of Timeslots", new Integer(4), parser.timeIntervalHelper.size());
// t0, t1, t2, t5
assertEquals("Incorrect max Timeslot", new Integer(5), parser.timeIntervalHelper.getMaxTimeSlotSeen());
}
@Test
......@@ -83,9 +85,11 @@ public class RegressionTests {
assertEquals("Not the correct number of Rule in CanDisable Block", 2, parser.canDisable.size());
// Correct number of roles and timeslots
assertEquals("Incorrect number of Roles", 5, parser.roles.size()); //
assertEquals("Incorrect number of Timeslots", 4, parser.timeslots.size()); // t0, t1, t2, t5
assertEquals("Incorrect max Timeslot", new Integer(5), parser.timeslots.last()); // t0, t1, t2, t5
assertEquals("Incorrect number of Roles", new Integer(5), parser.roleHelper.size()); //
// t0, t1, t2, t5
assertEquals("Incorrect number of Timeslots", new Integer(4), parser.timeIntervalHelper.size());
// t0, t1, t2, t5
assertEquals("Incorrect max Timeslot", new Integer(5), parser.timeIntervalHelper.getMaxTimeSlotSeen());
}
@Test
......@@ -106,9 +110,11 @@ public class RegressionTests {
assertEquals("Not the correct number of Rule in CanDisable Block", 2, parser.canDisable.size());
// Correct number of roles and timeslots
assertEquals("Incorrect number of Roles", 6, parser.roles.size()); //
assertEquals("Incorrect number of Timeslots", 4, parser.timeslots.size()); // t0, t1, t2, t5
assertEquals("Incorrect max Timeslot", new Integer(5), parser.timeslots.last()); // t0, t1, t2, t5
assertEquals("Incorrect number of Roles", new Integer(6), parser.roleHelper.size()); //
// t0, t1, t2, t5
assertEquals("Incorrect number of Timeslots", new Integer(4), parser.timeIntervalHelper.size());
// t0, t1, t2, t5
assertEquals("Incorrect max Timeslot", new Integer(5), parser.timeIntervalHelper.getMaxTimeSlotSeen());
}
@Test
......@@ -123,9 +129,10 @@ public class RegressionTests {
assertFalse("Errors were found!", error.errorFound);
ArrayList<Rule> rules = parser.canAssign.getRules();
assertEquals("Time interval not the same for full style", new TimeInterval(0, 5), rules.get(0)._adminTimeSlots);
assertEquals("Time interval not the same for full style", new TimeInterval(0, 5),
rules.get(0)._adminTimeInterval);
assertEquals("Time interval not the same for the half style", new TimeInterval(0, 0),
rules.get(1)._adminTimeSlots);
rules.get(1)._adminTimeInterval);
}
@Test
......@@ -181,8 +188,9 @@ public class RegressionTests {
parser.addErrorListener(errorListener);
parser.init();
System.out.println("Roles: " + parser.roles);
System.out.println("Timeslots: " + parser.timeslots);
System.out.println("Roles: " + parser.roleHelper);
System.out.println("Timeslots: " + parser.timeIntervalHelper);
System.out.println("Max Timeslot: " + parser.timeIntervalHelper.maxTimeSlot);
System.out.println(parser.query.toString());
System.out.println(parser.expectedResult.toString());
System.out.println(parser.canAssign.toString());
......
......@@ -2,6 +2,8 @@ package mohawk.converter.to;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
......@@ -22,8 +24,9 @@ public interface ConvertTo {
return templateFile;
}
public static String readFile(String path, Charset encoding) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path));
public static String readFile(URL url, Charset encoding) throws IOException, URISyntaxException {
byte[] encoded = Files.readAllBytes(Paths.get(url.toURI()));
return new String(encoded, encoding);
}
}
package mohawk.converter.to.mohawk;
import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.logging.Logger;
import mohawk.converter.generated.MohawkTARBACParser;
import mohawk.converter.to.ConvertTo;
import org.stringtemplate.v4.ST;
public