Commit 37d0d9e4 authored by Jonathan Shahen's avatar Jonathan Shahen

simple tests show that the reduction from Mohawk-T to ASAPTime NSA is...

simple tests show that the reduction from Mohawk-T to ASAPTime NSA is complete...still need to verify that the end result is correct, pushing to master because regression tests pass
parent 08dd77ac
Pipeline #7966 skipped
/* This test is to show that unique role names can be used, they must only contain [0-9a-zA-Z]+
/* Generated On : 2015/01/23 21:53:26.740
* Generated With : Mohawk Converter
* Number of Roles : 5
* Number of Timeslots : 4
* Number of Rules : 6
*
* This shows that NOT can have white space between it and the tilde (~)
*/
* Roles : [badguy, contractor, employee, manager, officer]
* Timeslots : [t0, t1, t2, t5]
*
*
* TESTCASE COMMENTS:
*
*/
Query: t0, [officer, manager, employee]
Expected: UNKNOWN
/*
* Number of Rules : 2
* Largest Precondition : 3
* Largest Role Schedule : 2
* Startable Rules : 1
* Truly Startable Rules : 0
*/
CanAssign {
<manager,t0-t5,TRUE,[t1,t1],employee> //(1)
<officer,t0-t0,employee & NOT~ manager & NOT~ badguy,[t1,t2],contractor> //(2)
/* 1 */ <manager, t0-t5, TRUE, [t1, t1], employee>
/* 2 */ <officer, t0, employee & NOT ~ manager & NOT ~ badguy, [t1, t2], contractor>
}
CanRevoke {
// same as above
}
CanRevoke { }
/*
* Number of Rules : 2
* Largest Precondition : 3
* Largest Role Schedule : 2
* Startable Rules : 1
* Truly Startable Rules : 0
*/
CanEnable {
<manager,t0-t5,TRUE,[t1,t1],employee> //(3)
<officer,t0-t0,employee & NOT ~ manager & NOT ~badguy,[t1,t2],contractor> //(4)
/* 1 */ <manager, t0-t5, TRUE, [t1, t1], employee>
/* 2 */ <officer, t0, employee & NOT ~ manager & NOT ~ badguy, [t1, t2], contractor>
}
/*
* Number of Rules : 2
* Largest Precondition : 3
* Largest Role Schedule : 2
* Startable Rules : 1
* Truly Startable Rules : 0
*/
CanDisable {
<manager,t0-t5,TRUE,[t1,t1],employee> //(5)
<officer,t0-t0,employee & NOT ~manager & NOT ~ badguy,[t1,t2],contractor> //(6)
}
Query: t0,[officer,manager,employee]
Expected: UNKNOWN
\ No newline at end of file
/* 1 */ <manager, t0-t5, TRUE, [t1, t1], employee>
/* 2 */ <officer, t0, employee & NOT ~ manager & NOT ~ badguy, [t1, t2], contractor>
}
\ No newline at end of file
/* This test is to show that the order of the blocks does not matter
/* Generated On : 2015/01/23 21:53:32.640
* Generated With : Mohawk Converter
* Number of Roles : 6
* Number of Timeslots : 4
* Number of Rules : 7
*
* Roles : [badguy, contractor, employee, manager, officer, person]
* Timeslots : [t0, t1, t2, t5]
*
*
* TESTCASE COMMENTS:
*
* This test shows that you can have all roles act as the administrator role
*/
Query : t0,[officer,manager,employee]
Expected : UNKNOWN
CanDisable {
<manager,t0-t5,TRUE,[t1,t1],employee> //(1)
<officer,t0-t0,employee & NOT~ manager & NOT~ badguy,[t1,t2],contractor> //(2)
}
Query: t0, [officer, manager, employee]
CanEnable {
<manager,t0-t5,TRUE,[t1,t1],employee> //(3)
<officer,t0-t0,employee & NOT~ manager & NOT~ badguy,[t1,t2],contractor> //(4)
}
Expected: UNKNOWN
CanRevoke {
// same as above
/*
* Number of Rules : 3
* Largest Precondition : 3
* Largest Role Schedule : 2
* Startable Rules : 1
* Truly Startable Rules : 0
*/
CanAssign {
/* 1 */ <manager, t0-t5, TRUE, [t1, t1], employee>
/* 2 */ <officer, t0, employee & NOT ~ manager & NOT ~ badguy, [t1, t2], contractor>
/* 3 */ <TRUE, t0, employee, [t1, t2], person>
}
CanAssign {
<manager,t0-t5,TRUE,[t1,t1],employee> //(5)
<officer,t0-t0,employee & NOT~ manager & NOT~ badguy,[t1,t2],contractor> //(6)
<TRUE,t0-t0,employee,[t1,t2],person> //(7)
CanRevoke { }
/*
* Number of Rules : 2
* Largest Precondition : 3
* Largest Role Schedule : 2
* Startable Rules : 1
* Truly Startable Rules : 0
*/
CanEnable {
/* 1 */ <manager, t0-t5, TRUE, [t1, t1], employee>
/* 2 */ <officer, t0, employee & NOT ~ manager & NOT ~ badguy, [t1, t2], contractor>
}
/*
* Number of Rules : 2
* Largest Precondition : 3
* Largest Role Schedule : 2
* Startable Rules : 1
* Truly Startable Rules : 0
*/
CanDisable {
/* 1 */ <manager, t0-t5, TRUE, [t1, t1], employee>
/* 2 */ <officer, t0, employee & NOT ~ manager & NOT ~ badguy, [t1, t2], contractor>
}
\ No newline at end of file
/* 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
/* Generated On : 2015/01/23 22:00:02.096
* Generated With : Mohawk Converter
* Number of Roles : 4
* Number of Timeslots : 4
* Number of Rules : 3
*
* Roles : [role0, role1, role2, role3]
* Timeslots : [t0, t1, t2, t5]
*
*
* TESTCASE COMMENTS:
*
* 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 {}
Query: t0, [role1, role0, role2]
CanEnable {}
Expected: UNKNOWN
CanDisable {}
/*
* Number of Rules : 3
* Largest Precondition : 3
* Largest Role Schedule : 2
* Startable Rules : 2
* Truly Startable Rules : 0
*/
CanAssign {
/* 1 */ <role0, t0-t5, TRUE, [t1, t5], role1>
/* 2 */ <role0, t0, role1 & NOT ~ role2 & role3, [t1, t2], role2>
/* 3 */ <role0, t0, NOT ~ role3, t1, role3>
}
// Expected solution: {REACHABLE, UNREACHABLE, UNKNOWN}; where for TRUE is means that we can achieve the Query
Expected: UNKNOWN
CanRevoke { }
CanEnable { }
// 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
CanDisable { }
\ No newline at end of file
......@@ -149,7 +149,8 @@ query
Query Colon t = timeslot Comma ra = roleArray
{
mohawkT.query._timeslot = new TimeSlot($t.val);
mohawkT.query._roles = $ra.r;
mohawkT.query._roles = $ra.r;
mohawkT.timeIntervalHelper.add(mohawkT.query._timeslot);
}
;
......@@ -253,14 +254,25 @@ timeslotArray returns [ArrayList<TimeSlot> t] @init {
}
:
LeftBracket a = timeslot
{$t.add(new TimeSlot($a.val));}
{
$t.add(new TimeSlot($a.val));
mohawkT.timeIntervalHelper.add(new TimeSlot($a.val));
}
(
',' b = timeslot
{$t.add(new TimeSlot($b.val));}
{
$t.add(new TimeSlot($b.val));
mohawkT.timeIntervalHelper.add(new TimeSlot($b.val));
}
)? RightBracket
| c = timeslot {$t.add(new TimeSlot($c.val));}
| c = timeslot
{
$t.add(new TimeSlot($c.val));
mohawkT.timeIntervalHelper.add(new TimeSlot($c.val));
}
;
timeInterval returns [Integer t0, Integer t1]
......@@ -273,6 +285,10 @@ timeInterval returns [Integer t0, Integer t1]
{$t1=$b.val;}
)?
{
mohawkT.timeIntervalHelper.add(new TimeInterval($t0, $t1));
}
;
timeslot returns [Integer val]
......@@ -280,7 +296,7 @@ timeslot returns [Integer val]
Timeslot
{
$val = new Integer($Timeslot.text.substring(1));
mohawkT.timeIntervalHelper.add(new TimeInterval($val));
}
;
......
package mohawk.converter.testing;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import mohawk.converter.generated.MohawkTARBACParser;
import mohawk.converter.logging.MohawkConsoleFormatter;
import mohawk.converter.to.asaptime.ConvertToASAPTimeNSA;
import mohawk.global.pieces.MohawkT;
import org.junit.Before;
import org.junit.Test;
public class ConvertToASAPTimeTests {
public static final Logger logger = Logger.getLogger("mohawk-converter");
public String folderbase = "data/regressiontests/";
public BooleanErrorListener error = new BooleanErrorListener();
@Before
public void setup() {
System.out.println("sadasdasdasdsadsadsaasd");
logger.setLevel(Level.FINE);
ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setFormatter(new MohawkConsoleFormatter());
consoleHandler.setLevel(Level.FINE);
logger.addHandler(consoleHandler);
}
@Test
public void printOut() throws IOException {
String file = "positive1.spec";
error.errorFound = false;
MohawkTARBACParser parser = loadfile(file);
MohawkT m = parser.mohawkT;
ConvertToASAPTimeNSA converter = new ConvertToASAPTimeNSA();
String convertedStr = converter.convert(m, null, false);
System.out.println("Printing Out: " + file);
System.out.println(convertedStr + "\n\n");
}
private MohawkTARBACParser loadfile(String file) throws IOException {
FileInputStream fis = new FileInputStream(folderbase + file);
return ConverterRegressionTests.runParser(fis, error);
}
}
<! Conversion to Ranise's Input Format !>
<! Conversion to ASAPTime NSA Input Format !>
<! !>
CONFIG <numRoles> <numTimeslots>
GOAL <goalRole> t<goalTimeslot>
<! !>
<! can_revoke 3 , t3 , true ; t5 , 14 !>
<rules_nsa:{r|<r.ruleType> <if(r.adminRole >= 0)><r.adminRole><else>true<endif> , 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_nsa:{r|<r.ruleType> <if(r.adminRole)><r.adminRole><else>true<endif> , t<r.adminTime> , <if(r.precondition)><r.precondition; separator=" & "><else>true<endif> ; t<r.roleTime> , <r.role><\n>}>
\ No newline at end of file
......@@ -6,6 +6,8 @@ import java.io.StringWriter;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.logging.Logger;
import mohawk.converter.to.ConvertTo;
......@@ -13,7 +15,9 @@ import mohawk.global.helper.RoleHelper;
import mohawk.global.helper.TimeIntervalHelper;
import mohawk.global.pieces.MohawkT;
import mohawk.global.pieces.Query;
import mohawk.global.pieces.Role;
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;
......@@ -23,11 +27,12 @@ public class ConvertToASAPTimeNSA extends ConvertTo {
public static final Logger logger = Logger.getLogger("mohawk-converter");
@Override
public String convert(MohawkT m, File f, Boolean writeToFile) {
public String convert(MohawkT m, File file, Boolean writeToFile) {
try {
String template = ConvertTo.readFile(this.getClass().getResource("ASAPTimeNSATemplate.st"),
Charset.defaultCharset());
ST st = new ST(template);
ArrayList<ASAPTimeNSA_Rule> rules = new ArrayList<ASAPTimeNSA_Rule>();
// Reduce Roles to Integers
m.roleHelper.allowZeroRole = false; // roles start from 1 NOT 0!
......@@ -36,15 +41,14 @@ public class ConvertToASAPTimeNSA extends ConvertTo {
m.timeIntervalHelper.allowZeroTimeslot = false; // time-slots start from t1 NOT t0!
m.timeIntervalHelper.reduceToTimeslots();
// Convert Query to Reduced ASAPTime NSA Query and add any extra rules as needed
ASAPTimeNSA_Query query = toASAPTimeNSA_Query(m.query, rules, m.roleHelper, m.timeIntervalHelper);
// Convert Rules into Reduced ASAPTime NSA Rules
ArrayList<ASAPTimeNSA_Rule> rules = new ArrayList<ASAPTimeNSA_Rule>();
for (Rule r : m.getAllRules()) {
rules.addAll(toASAPTimeNSA_Rules(r, m.roleHelper, m.timeIntervalHelper));
}
// Convert Query to Reduced ASAPTime NSA Query and add any extra rules as needed
ASAPTimeNSA_Query query = toASAPTimeNSA_Query(m.query, rules, m.roleHelper, m.timeIntervalHelper);
st.add("numRoles", m.roleHelper.size());
st.add("numTimeslots", m.timeIntervalHelper.sizeReduced());
st.add("goalRole", query.goalRole);
......@@ -54,7 +58,7 @@ public class ConvertToASAPTimeNSA extends ConvertTo {
convertedStr = st.render();
if (writeToFile) {
File convertedFile = new File(f.getAbsolutePath() + getFileExtenstion());
File convertedFile = new File(file.getAbsolutePath() + getFileExtenstion());
if (!convertedFile.exists()) {
convertedFile.createNewFile();
......@@ -75,6 +79,12 @@ public class ConvertToASAPTimeNSA extends ConvertTo {
}
/**
* Directly converts the Query's timeslot to the reduced timeslot and then it copies the Goal Role is there is only
* 1. If there is more than 1 Goal Role in the Query then it will create a new Goal Role that is unique and create a
* new Rule such that only someone who has reached the Goal Roles in the original Query can assign this new Goal
* Role and thus make the system REACHABLE.
*
* WARNING: if this function has to create a new role then it will call roleHelper.setupSortedRoles();
*
* @param query
* @param rules
......@@ -84,21 +94,78 @@ public class ConvertToASAPTimeNSA extends ConvertTo {
*/
public ASAPTimeNSA_Query toASAPTimeNSA_Query(Query query, ArrayList<ASAPTimeNSA_Rule> rules, RoleHelper roleHelper,
TimeIntervalHelper timeIntervalHelper) {
// TODO Auto-generated method stub
return null;
ASAPTimeNSA_Query q = new ASAPTimeNSA_Query();
// Set the Goal Timeslot
SortedSet<Integer> goalTimeslots = timeIntervalHelper.indexOfReduced(query._timeslot);
assert (goalTimeslots.size() == 1) : "The reduced query must have only one timeslot";
q.goalTimeslot = goalTimeslots.first();
// Set the Goal Role
assert (query._roles.size() > 0) : "The query must have atleast one Goal Role";
if (query._roles.size() == 1) {
q.goalRole = roleHelper.indexOf(query._roles.get(0));
} else {
// Create a new role to act as the Goal Role
Role newGoalRole = roleHelper.getUniqueRole("goalRole");
roleHelper.add(newGoalRole);
roleHelper.setupSortedRoles();
Rule newRule = new Rule(query, newGoalRole);
rules.addAll(toASAPTimeNSA_Rules(newRule, roleHelper, timeIntervalHelper));
q.goalRole = roleHelper.indexOf(newGoalRole);
}
return q;
}
/**
*
* @param r
* @param rule
* @param roleHelper
* @param timeIntervalHelper
* @return
*/
public ArrayList<ASAPTimeNSA_Rule> toASAPTimeNSA_Rules(Rule r, RoleHelper roleHelper,
public ArrayList<ASAPTimeNSA_Rule> toASAPTimeNSA_Rules(Rule rule, RoleHelper roleHelper,
TimeIntervalHelper timeIntervalHelper) {
// TODO Auto-generated method stub
return null;
ArrayList<ASAPTimeNSA_Rule> rules = new ArrayList<ASAPTimeNSA_Rule>();
SortedSet<Integer> adminInterval = timeIntervalHelper.indexOfReduced(rule._adminTimeInterval);
SortedSet<Integer> roleSchedule = new TreeSet<Integer>();
ArrayList<Integer> precondition = new ArrayList<Integer>();
// Convert RoleSchedule to the New Timeslots
for (TimeSlot ts : rule._roleSchedule) {
roleSchedule.addAll(timeIntervalHelper.indexOfReduced(ts));
}
// Convert Preconditions to the Role Indexes
for (Role r : rule._preconditions) {
precondition.add(roleHelper.indexOf(r));
}
ASAPTimeNSA_Rule rule_t;
for (Integer adminTimeslot : adminInterval) {
for (Integer roleTimeslot : roleSchedule) {
rule_t = new ASAPTimeNSA_Rule();
rule_t.ruleType = rule._type.toRanise();
if (rule._adminRole.isAllRoles()) {
rule_t.adminRole = null;
} else {
rule_t.adminRole = roleHelper.indexOf(rule._adminRole);
}
rule_t.adminTime = adminTimeslot;
rule_t.role = roleHelper.indexOf(rule._role);
rule_t.precondition = precondition;
rule_t.roleTime = roleTimeslot;
rules.add(rule_t);
}
}
return rules;
}
@Override
......
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