Commit 74860bb1 authored by Jonathan Shahen's avatar Jonathan Shahen

added regression tests; made the input language more forgiving with...

added regression tests; made the input language more forgiving with time-intervals and role schedules (they can now be single time-slots without the extra input); started the toRanise conversion; updated the base template for conversion
parent 14f4e3c8
/* This test is to show that the admin time interval can be written
* as a timeslot to save time only if the interval is tX-tX => tX
*
* This test is to show that the role schedule can be represented as
* an array [tX] or as a time slot tX, if the role schedule only
* contains one time slot
*/
CanAssign {
/* <Admin role,
* starting timeslot - end timeslot for admin role,
* role preconditions,
* [time slot rule gives role to user for, another time slot (optional)...],
* role to give to the user>
*/
<role0,t0-t5,TRUE,[t1,t5],role1> //(1)
<role0,t0,role1 & NOT~ role2 & role3,[t1,t2],role2> //(2)
<role0,t0,NOT ~ role3,t1,role3> //(3)
}
CanRevoke {}
CanEnable {}
CanDisable {}
// Expected solution: {TRUE, FALSE, UNKNOWN}; where for TRUE is means that we can achieve the Query
Expected: UNKNOWN
// Query: timeslot to check, [set of roles that the user must have in the timeslot (no NOTs allowed)]
Query: t0,[role1,role0,role2]
\ No newline at end of file
......@@ -9,8 +9,8 @@ CanAssign {
* [time slot rule gives role to user for, another time slot (optional)...],
* role to give to the user>
*/
<role0,t0-t0,TRUE,[t0],role1> //(1)
<role0,t0-t0,role1,[t0],role2> //(2)
<role0,t0,TRUE,[t0],role1> //(1)
<role0,t0,role1,[t0],role2> //(2)
}
CanRevoke {}
......
/* Taken from sharelatex's "Working Files/Mohawk Examples/old-simple-tests.txt"
*
* Converted simple_test2.txt into the new format
*/
Query: t1,[role2]
Expected: TRUE
CanAssign {
/* <Admin role,
* starting timeslot - end timeslot for admin role,
* role preconditions,
* [time slot rule gives role to user for, another time slot (optional)...],
* role to give to the user>
*/
<role0,t0,TRUE,[t1],role0> //(1)
<role0,t1,TRUE,[t1],role1> //(2)
<role0,t0,role1,[t1],role2> //(3)
}
CanRevoke {}
CanEnable {}
CanDisable {}
/* Taken from sharelatex's "Working Files/Mohawk Examples/old-simple-tests.txt"
*
* Converted simple_test3.txt into the new format
*/
Query: t1,[role2]
Expected: TRUE
CanAssign {
/* <Admin role,
* starting timeslot - end timeslot for admin role,
* role preconditions,
* [time slot rule gives role to user for, another time slot (optional)...],
* role to give to the user>
*/
<role0,t0,TRUE,[t1],role0> //(1)
<role0,t1,role0,[t1],role1> //(2)
<role0,t1,role1&-role0,[t1],role2> //(3)
}
CanRevoke {
<role0,t1,TRUE,[t1],role0> //(4)
}
CanEnable {
<role0,t0,TRUE,[t1],role0> //(5)
}
CanDisable {
<role0,t1,TRUE,[t1],role0> //(6)
}
/* Taken from sharelatex's "Working Files/Mohawk Examples/old-simple-tests.txt"
*
* Converted simple_test4.txt into the new format
*/
Query: t1,[role2]
Expected: FALSE // {TRUE, FALSE, UNKNOWN}
CanAssign {
/* <Admin role,
* starting timeslot - end timeslot for admin role,
* role preconditions,
* [time slot rule gives role to user for, another time slot (optional)...],
* role to give to the user>
*/
<role0,t0,TRUE,t1,role0> // (5)
<role0,t1,role0,t1,role1> // (6)
<role0,t1,role1 & NOT role0,t1,role2> // (7)
}
CanRevoke {
<TRUE,t0,TRUE,t0,role1> // (1)
<TRUE,t1,TRUE,t0,role1> // (2)
<TRUE,t1,TRUE,t0,role1> // (3)
<TRUE,t1,TRUE,t1,role1> // (4)
}
CanEnable {
<role0,t0,TRUE,t1,role0> // (8)
}
CanDisable {
<role0,t1,TRUE,t1,role0> // (9)
}
......@@ -90,9 +90,14 @@ public class ConverterInstance {
if (cmd.hasOption("ranise") || cmd.hasOption("all")) {
/* Timing */timing.startTimer("convertToRanise (" + i + ")");
// TODO: Convert imported spec files to Ranise format
ConvertToRanise toRanise = new ConvertToRanise();
int rainseError = toRanise.convert(parser, specFile);
String raniseStr = toRanise.convert(parser, specFile, true);
if (!raniseStr.startsWith("Error")) {
logger.info("[SUCCESS] Successfully converted to Ranise's Input Format");
} else {
logger.warning("[FAILURE] Failed to convert to Raines's Input Format; Error code " + raniseStr);
}
/* Timing */timing.stopTimer("convertToRanise (" + i + ")");
}
......@@ -102,7 +107,7 @@ public class ConverterInstance {
// TODO: Convert imported spec files to Uzun format
ConvertToUzun toUzun = new ConvertToUzun();
int uzunError = toUzun.convert(parser, specFile);
String uzunStr = toUzun.convert(parser, specFile, true);
/* Timing */timing.stopTimer("convertToUzun (" + i + ")");
}
......@@ -112,7 +117,7 @@ public class ConverterInstance {
// TODO: Convert imported spec files to Mohawk format
ConvertToMohawk toMohawk = new ConvertToMohawk();
int rainseError = toMohawk.convert(parser, specFile);
String mohawkStr = toMohawk.convert(parser, specFile, true);
/* Timing */timing.stopTimer("convertToMohawk (" + i + ")");
}
......
package mohawk.converter;
public class Parser {
}
......@@ -224,8 +224,8 @@ locals [Boolean adminAllRoles]
} else {
$r._adminRole = $adminRole.role;
}
$r._adminTimeSlots[0] = $ti.t0;
$r._adminTimeSlots[1] = $ti.t1;
$r._adminTimeSlots.setStartTime($ti.t0);
$r._adminTimeSlots.setFinishTime($ti.t1);
$r._preconditions = $pc.p;
$r._roleSchedule = $tsa.t;
$r._role = $role.role;
......@@ -292,16 +292,19 @@ timeslotArray returns [ArrayList<Integer> t] @init {
{$t.add($b.val);}
)? RightBracket
| c = timeslot {$t.add($c.val);}
;
timeInteral returns [Integer t0, Integer t1]
:
a = timeslot '-' b = timeslot
{
$t0=$a.val;
$t1=$b.val;
}
a = timeslot
{$t0 = $a.val; $t1=$t0;}
(
'-' b = timeslot
{$t1=$b.val;}
)?
;
timeslot returns [Integer val]
......
package mohawk.converter.pieces;
import java.util.HashMap;
public class Role {
public String _name;
public Boolean _not;
......@@ -27,4 +29,9 @@ public class Role {
if (_not == false) { return _name; }
return "NOT " + _name;
}
public String toRanise(HashMap<String, Integer> roles) {
if (_not) { return "-" + roles.get(_name); }
return "" + roles.get(_name);
}
}
package mohawk.converter.pieces;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Logger;
public class Rule {
public static final Logger logger = Logger.getLogger("mohawk-converter");
public RuleType _type;
public Role _adminRole;
public Integer[] _adminTimeSlots = new Integer[2];
public TimeInterval _adminTimeSlots = new TimeInterval();
public ArrayList<Role> _preconditions = new ArrayList<Role>();
public ArrayList<Integer> _roleSchedule = new ArrayList<Integer>();
public Role _role;
public Rule() {
public String toRanise(HashMap<String, Integer> roles) {
String sep = " , ";
StringBuffer s = new StringBuffer();
s.append(_type.toRanise() + " ");
s.append(roles.get(_adminRole) + sep);
if (_adminTimeSlots.getStartTime() != _adminTimeSlots.getFinishTime()) {
logger.severe("TODO: Implement the time interval splitting!");
}
s.append("t" + _adminTimeSlots.getStartTime() + sep);
s.append(Rule.toRanise(_preconditions, roles) + "; ");
// s.append("t" + _adminTimeSlots + sep);
return s.toString();
}
public void parse(String input) throws IOException {
private static String toRanise(ArrayList<Role> pre, HashMap<String, Integer> roles) {
String sep = " ";
if (pre.size() == 0) { return "true "; }
StringBuffer s = new StringBuffer();
for (Role r : pre) {
s.append(r.toRanise(roles) + sep);
}
return s.toString();
}
@Override
......@@ -27,17 +51,36 @@ public class Rule {
s.append(_type + " <");
s.append(_adminRole.toString() + ", ");
s.append("[t" + _adminTimeSlots[0] + "-t" + _adminTimeSlots[1] + "], ");
s.append("[t" + _adminTimeSlots.getStartTime() + "-t" + _adminTimeSlots.getFinishTime() + "], ");
if (_preconditions.size() == 0) {
s.append("TRUE, ");
} else {
s.append(_preconditions.toString() + ", ");
}
s.append(_roleSchedule.toString() + ", ");
s.append(getRoleScheduleString(_roleSchedule) + ", ");
s.append(_role.toString());
s.append(">");
return s.toString();
}
public static String getRoleScheduleString(ArrayList<Integer> roleSchedule) {
if (roleSchedule.size() == 0) { return "TRUE"; }
if (roleSchedule.size() == 1) { return "t" + roleSchedule.get(0); }
StringBuffer s = new StringBuffer();
s.append("[");
for (int i = 0; i < roleSchedule.size(); i++) {
s.append("t" + roleSchedule.get(i));
if (i != roleSchedule.size() - 1) {
s.append(", ");
}
}
s.append("]");
return s.toString();
}
}
......@@ -13,4 +13,11 @@ public enum RuleType {
public String toString() {
return this._name;
}
public String toRanise() {
String tmp = _name.toLowerCase();
tmp.replaceFirst("can", "can_");
return tmp;
}
}
package mohawk.converter.pieces;
public class TimeInterval {
public Integer _start = null;
public Integer _finish = null;
public TimeInterval() {
}
public TimeInterval(Integer start, Integer finish) {
this._start = start;
this._finish = finish;
}
public Integer getFinishTime() {
return _finish;
}
public Integer getStartTime() {
return _start;
}
public void setFinishTime(Integer finish) {
_finish = finish;
}
public void setStartTime(Integer start) {
_start = start;
}
public Boolean isValidInterval() {
return _start != null && _finish != null && _start <= _finish;
}
public Boolean isOverlapping(TimeInterval obj) {
return ((_start <= obj.getStartTime() && obj.getStartTime() <= _finish) || (obj.getStartTime() <= _start && _start <= obj
.getFinishTime()));
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof TimeInterval)) { return false; }
TimeInterval _obj = (TimeInterval) obj;
return (this._start.equals(_obj.getStartTime()) && this._finish.equals(_obj.getFinishTime()));
}
@Override
public String toString() {
return "[t" + _start + ",t" + _finish + "]";
}
}
......@@ -8,10 +8,13 @@ import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.logging.Logger;
import mohawk.converter.generated.MohawkTARBACLexer;
import mohawk.converter.generated.MohawkTARBACParser;
import mohawk.converter.pieces.Rule;
import mohawk.converter.pieces.TimeInterval;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.BaseErrorListener;
......@@ -108,6 +111,46 @@ public class RegressionTests {
assertEquals("Incorrect max Timeslot", new Integer(5), parser.timeslots.last()); // t0, t1, t2, t5
}
@Test
public void runPositive4() throws IOException {
System.out.println("Running Test: runPositive4\n\n");
String file = "positive4.spec";
error.errorFound = false;
MohawkTARBACParser parser = loadfile(file);
// Check for Parsing errors
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 the half style", new TimeInterval(0, 0),
rules.get(1)._adminTimeSlots);
}
@Test
public void testTimeInterval() {
System.out.println("Running Test: testTimeInterval\n\n");
TimeInterval t1 = new TimeInterval(1, 5);
TimeInterval t2 = new TimeInterval(1, 5);
TimeInterval t3 = new TimeInterval(2, 3);
TimeInterval t4 = new TimeInterval(3, 3);
TimeInterval t5 = new TimeInterval(4, 6);
TimeInterval t6 = new TimeInterval(0, 3);
TimeInterval t7 = new TimeInterval(6, 7);
TimeInterval t8 = new TimeInterval(0, 0);
assertTrue("isEqual", t1.equals(t2));
assertTrue("isOverlapping same", t1.isOverlapping(t2));
assertTrue("isOverlapping within", t1.isOverlapping(t3));
assertTrue("isOverlapping timeslot", t1.isOverlapping(t4));
assertTrue("isOverlapping in and after", t1.isOverlapping(t5));
assertTrue("isOverlapping before and in", t1.isOverlapping(t6));
assertFalse("isOverlapping outside after", t1.isOverlapping(t7));
assertFalse("isOverlapping outside before timeslot", t1.isOverlapping(t8));
}
@Test
public void runNegative0() throws IOException {
System.out.println("Running Test: runNegative0\n\n");
......
package mohawk.converter.to;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import mohawk.converter.generated.MohawkTARBACParser;
public interface ConvertTo {
public int convert(MohawkTARBACParser p, File f);
public String convert(MohawkTARBACParser p, File f, Boolean writeToFile);
public String getFileExtenstion();
// public File getTemplateFile() throws IOException;
public static File getTemplateFile(String filename) throws IOException {
File templateFile = new File(filename);
if (!templateFile.exists()) { throw new IOException("Unable to find template file: " + filename); }
return templateFile;
}
public static String readFile(String path, Charset encoding) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
}
......@@ -6,11 +6,15 @@ import mohawk.converter.generated.MohawkTARBACParser;
import mohawk.converter.to.ConvertTo;
public class ConvertToMohawk implements ConvertTo {
@Override
public String getFileExtenstion() {
return ".mohawk";
}
@Override
public int convert(MohawkTARBACParser p, File f) {
public String convert(MohawkTARBACParser p, File f, Boolean writeToFile) {
// TODO Auto-generated method stub
return 0;
return null;
}
}
package mohawk.converter.to.ranise;
import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.util.logging.Logger;
import mohawk.converter.generated.MohawkTARBACParser;
import mohawk.converter.to.ConvertTo;
import org.stringtemplate.v4.ST;
public class ConvertToRanise implements ConvertTo {
public static final Logger logger = Logger.getLogger("mohawk-converter");
@Override
public String convert(MohawkTARBACParser p, File f, Boolean writeToFile) {
try {
File convertedFile = new File(f.getAbsolutePath() + getFileExtenstion());
if (!convertedFile.exists()) {
convertedFile.createNewFile();
}
String template = ConvertTo.readFile("raniseTemplate.st", Charset.defaultCharset());
ST st = new ST(template);
st.add("numRoles", p.roles.size());
st.add("numTimeslots", null);
st.add("goalRole", null);
st.add("goalTimeslot", null);
String raniseStr = st.render();
return raniseStr;
} catch (Exception e) {
StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
logger.fine(errors.toString());
logger.warning("[ERROR] Unable to convert to Ranise: " + e.getMessage());
return "Error ConvertToRanise.convert.Exception #1";
}
}
@Override
public int convert(MohawkTARBACParser p, File f) {
// TODO Auto-generated method stub
return 0;
public String getFileExtenstion() {
return ".ranise";
}
}
<! Conversion to Ranise's Input Format !>
CONFIG <numRoles> <numTimeslots>
GOAL <goalRole> <goalTimeslot>
<! can_revoke 3 , t3 , true ; t5 , 14 !>
<rules; separator="\n">
\ No newline at end of file
......@@ -8,9 +8,14 @@ import mohawk.converter.to.ConvertTo;
public class ConvertToUzun implements ConvertTo {
@Override
public int convert(MohawkTARBACParser p, File f) {
public String getFileExtenstion() {
return ".uzun";
}
@Override
public String convert(MohawkTARBACParser p, File f, Boolean writeToFile) {
// TODO Auto-generated method stub
return 0;
return null;
}
}
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