Commit c91ddee1 authored by Jonathan Shahen's avatar Jonathan Shahen

better naming convention; fixed build.xml

parent 8fad5ad4
......@@ -13,5 +13,6 @@
<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"/>
<classpathentry combineaccessrules="false" kind="src" path="/Mohawk-T Globals"/>
<classpathentry kind="output" path="bin"/>
</classpath>
......@@ -3,3 +3,7 @@ target/
src/mohawk/converter/generated/*.java
src/mohawk/converter/generated/*.tokens
logs/
*.uzun
*.ranise
*.ranise_nsa
*.ranise_sa
......@@ -16,7 +16,17 @@
<property name="antlr" location="${lib}/antlr-4.4-complete.jar" />
<property name="junit" location="${lib}/junit-4.12.jar" />
<property name="qtest" location="${data}/" />
<!-- TODO -->
<!-- Customize this line to point to the 'Mohawk-T Globals' Project's source folder -->
<property name="src.globals" location="../Mohawk-T Globals/src" />
<path id="test.classpath">
<pathelement location="${build}" />
<pathelement location="${junit}" />
<fileset dir="${lib}">
<include name="**/*.jar" />
</fileset>
</path>
<target name="init">
<!-- create the bin directory -->
......@@ -46,6 +56,8 @@
<target name="compile" depends="parser" description="compile the source ">
<!-- compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}" debug="on" deprecation="true">
<src path="${src}" />
<src path="${src.globals}" />
<classpath>
<fileset dir="${lib}">
<include name="**/*.jar" />
......@@ -89,10 +101,18 @@
<target name="regression" depends="dist" description="Performs a regression test">
<!-- @author Jonathan Shahen -->
<echo>java -cp ${dist}/${ant.project.name}.jar org.junit.runner.JUnitCore mohawk.testing.RegressionTests</echo>
<exec executable="cmd" dir="." failonerror="true">
<junit haltonfailure="true" printsummary="true">
<test name="mohawk.converter.testing.RegressionTests" />
<formatter type="plain" usefile="false" />
<classpath refid="test.classpath" />
</junit>
<!-- OLD Windows ONLY running JUnit through the commandline
<exec executable="cmd" dir="." failonerror="true" osfamily="windows">
<arg value="/c" />
<arg value="java -cp &quot;${dist}/${ant.project.name}.jar&quot; org.junit.runner.JUnitCore mohawk.converter.testing.RegressionTests" />
</exec>
-->
</target>
<target name="clean" description="clean up">
......
/*
* Question 3:
*
* Roles: role1, role2, role3
* Time-Slots: t0, t1
*
* If the roles are enabled by default then this example with succeed
*/
Query: t0,[role3]
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>
*/
<TRUE,t0,TRUE,t0,role2> //(1)
<TRUE,t0,role2,t0,role3> //(2)
}
CanRevoke {}
CanEnable {}
CanDisable {}
/*
* Question 3:
*
* Roles: role1, role2
* Time-Slots: t0, t1
*
* if any of the rules are triggered it will show that some roles are allowed to be activated without a can assign
*/
Query: t0,[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>
*/
<TRUE,t0,role1,t0,role2> //(1)
<TRUE,t0,role2,t0,role1> //(2)
/* Trying to include other time-slots to test with TRule */
<TRUE,t1,role1,t0,role2> //(3)
<TRUE,t1,role2,t0,role1> //(4)
}
CanRevoke {}
CanEnable {}
CanDisable {}
/*
* Question 2: Is time considered periodic?
*
* Roles: role1, role2, role3
* Time-Slots: t0
*
* rule 3 -> assign role 1 for t1 ->
* rule 2 -> assign role 2 to anyone with role 1 ->
* rule 1 -> assign role 3 to anyone with role 2
*/
Query: t2,[role3]
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>
*/
<TRUE,t0,role2,t2,role3> //(1)
<TRUE,t1,role1,t0,role2> //(2)
<TRUE,t2,TRUE,t1,role1> //(3)
}
CanRevoke {}
CanEnable {}
CanDisable {}
......@@ -13,9 +13,9 @@ 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.mohawk.ConvertToMohawk;
import mohawk.converter.to.ranise.ConvertToRanise;
import mohawk.converter.to.uzun.ConvertToUzun;
import mohawk.converter.to.tred.ConvertToTRedRule;
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
......@@ -92,7 +92,7 @@ public class ConverterInstance {
logger.info("[CONVERTING] Converting to Rainse...");
/* Timing */timing.startTimer("convertToRanise (" + i + ")");
ConvertToRanise toRanise = new ConvertToRanise();
ConvertToASAPTimeNSA toRanise = new ConvertToASAPTimeNSA();
String raniseStr = toRanise.convert(parser, specFile, true);
if (toRanise.lastError == null) {
......@@ -113,7 +113,7 @@ public class ConverterInstance {
/* Timing */timing.startTimer("convertToUzun (" + i + ")");
// TODO: Convert imported spec files to Uzun format
ConvertToUzun toUzun = new ConvertToUzun();
ConvertToTRedRule toUzun = new ConvertToTRedRule();
String uzunStr = toUzun.convert(parser, specFile, true);
if (toUzun.lastError == null) {
......
package mohawk.converter.pieces;
public class CanAssign extends CanBlock {
public CanAssign() {
super();
......
package mohawk.converter.pieces;
public class CanDisable extends CanBlock {
public CanDisable() {
super();
......
package mohawk.converter.pieces;
public class CanEnable extends CanBlock {
public CanEnable() {
super();
......
package mohawk.converter.pieces;
public class CanRevoke extends CanBlock {
public CanRevoke() {
super();
......
package mohawk.converter.pieces.rules;
import mohawk.converter.helper.RoleHelper;
import mohawk.converter.helper.TimeIntervalHelper;
import mohawk.converter.pieces.Role;
import mohawk.converter.pieces.Rule;
public class ASAPTimeNSA_Rule {
public String ruleType;
public Integer startTime;
public Integer finishTime;
public Integer role;
public Integer[] precondition;
public Integer[] roleSchedule;
public ASAPTimeNSA_Rule(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;
}
}
roleSchedule = new Integer[tih.getNextTimeSlot()];
for (Integer i = 0; i < roleSchedule.length; i++) {
if (r._roleSchedule.contains(i)) {
roleSchedule[i] = 1;
} else {
roleSchedule[i] = 0;
}
}
}
}
package mohawk.converter.pieces.rules;
import mohawk.converter.helper.RoleHelper;
import mohawk.converter.helper.TimeIntervalHelper;
import mohawk.converter.pieces.Role;
import mohawk.converter.pieces.Rule;
public class ASAPTimeSA_Rule {
public String ruleType;
public Integer startTime;
public Integer finishTime;
public Integer role;
public Integer[] precondition;
public Integer[] roleSchedule;
public ASAPTimeSA_Rule(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;
}
}
roleSchedule = new Integer[tih.getNextTimeSlot()];
for (Integer i = 0; i < roleSchedule.length; i++) {
if (r._roleSchedule.contains(i)) {
roleSchedule[i] = 1;
} else {
roleSchedule[i] = 0;
}
}
}
}
package mohawk.converter.pieces;
package mohawk.converter.pieces.rules;
import mohawk.converter.helper.RoleHelper;
import mohawk.converter.helper.TimeIntervalHelper;
import mohawk.converter.pieces.Role;
import mohawk.converter.pieces.Rule;
public class UzunRule {
public class Mohawk_Rule {
public String ruleType;
public Integer startTime;
public Integer finishTime;
......@@ -11,7 +13,7 @@ public class UzunRule {
public Integer[] precondition;
public Integer[] roleSchedule;
public UzunRule(Rule r, RoleHelper rh, TimeIntervalHelper tih) {
public Mohawk_Rule(Rule r, RoleHelper rh, TimeIntervalHelper tih) {
ruleType = r._type.toUzun();
startTime = r._adminTimeInterval.getStartTime();
finishTime = r._adminTimeInterval.getFinishTime();
......
package mohawk.converter.pieces.rules;
import mohawk.converter.helper.RoleHelper;
import mohawk.converter.helper.TimeIntervalHelper;
import mohawk.converter.pieces.Role;
import mohawk.converter.pieces.Rule;
public class TRedRole_Rule {
public String ruleType;
public Integer startTime;
public Integer finishTime;
public Integer role;
public Integer[] precondition;
public Integer[] roleSchedule;
public TRedRole_Rule(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;
}
}
roleSchedule = new Integer[tih.getNextTimeSlot()];
for (Integer i = 0; i < roleSchedule.length; i++) {
if (r._roleSchedule.contains(i)) {
roleSchedule[i] = 1;
} else {
roleSchedule[i] = 0;
}
}
}
}
package mohawk.converter.pieces.rules;
import mohawk.converter.helper.RoleHelper;
import mohawk.converter.helper.TimeIntervalHelper;
import mohawk.converter.pieces.Role;
import mohawk.converter.pieces.Rule;
public class TRedRule_Rule {
public String ruleType;
public Integer startTime;
public Integer finishTime;
public Integer role;
public Integer[] precondition;
public Integer[] roleSchedule;
public TRedRule_Rule(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;
}
}
roleSchedule = new Integer[tih.getNextTimeSlot()];
for (Integer i = 0; i < roleSchedule.length; i++) {
if (r._roleSchedule.contains(i)) {
roleSchedule[i] = 1;
} else {
roleSchedule[i] = 0;
}
}
}
}
......@@ -2,6 +2,7 @@ package mohawk.converter.testing;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream;
......@@ -15,6 +16,7 @@ import mohawk.converter.generated.MohawkTARBACLexer;
import mohawk.converter.generated.MohawkTARBACParser;
import mohawk.converter.pieces.Rule;
import mohawk.converter.pieces.TimeInterval;
import mohawk.global.FileExtensions;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.BaseErrorListener;
......@@ -64,7 +66,7 @@ public class RegressionTests {
// 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.getNumberOfTimeSlots());
assertEquals("Incorrect max Timeslot", new Integer(5), parser.timeIntervalHelper.maxTimeSlot);
}
@Test
......@@ -89,7 +91,7 @@ public class RegressionTests {
// 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.getNumberOfTimeSlots());
assertEquals("Incorrect max Timeslot", new Integer(5), parser.timeIntervalHelper.maxTimeSlot);
}
@Test
......@@ -114,7 +116,7 @@ public class RegressionTests {
// 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.getNumberOfTimeSlots());
assertEquals("Incorrect max Timeslot", new Integer(5), parser.timeIntervalHelper.maxTimeSlot);
}
@Test
......@@ -158,6 +160,20 @@ public class RegressionTests {
}
@Test
public void checkFileExtension() {
System.out.println("Checking to make sure FileExtensions is included "
+ "(This is mainly for developing and including the Mohawk-T Globals project)");
try {
FileExtensions fe = new FileExtensions();
assertNotNull("FileExtensions was not included!", fe);
} catch (Exception e) {
assertFalse("An exception was raised when trying to create the object FileExtension!\n"
+ "Make sure you have included the Mohawk-T Globals project when building the jar", true);
}
}
@Test
public void runNegative0() throws IOException {
System.out.println("Running Test: runNegative0\n\n");
......
<! 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
package mohawk.converter.to.asaptime;
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 class ConvertToASAPTimeNSA extends ConvertTo {
public static final Logger logger = Logger.getLogger("mohawk-converter");
@Override
public String convert(MohawkTARBACParser p, File f, Boolean writeToFile) {
try {
String template = ConvertTo.readFile(this.getClass().getResource("ASAPTimeNSATemplate.st"),
Charset.defaultCharset());
ST st = new ST(template);
st.add("numRoles", p.roleHelper.size());
st.add("numTimeslots", p.timeIntervalHelper.size());
st.add("goalRole", null);
st.add("goalTimeslot", null);
convertedStr = st.render();
if (writeToFile) {
File convertedFile = new File(f.getAbsolutePath() + getFileExtenstion());
if (!convertedFile.exists()) {
convertedFile.createNewFile();
}
Files.write(convertedFile.toPath(), convertedStr.getBytes());
}
lastError = null;
} 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());
lastError = "Error ConvertToRanise.convert.Exception #1";
}
return convertedStr;
}
@Override
public String getFileExtenstion() {
return ".ranise";
}
}
package mohawk.converter.to.ranise;
package mohawk.converter.to.asaptime;
import java.io.File;
import java.io.PrintWriter;
......@@ -12,13 +12,13 @@ import mohawk.converter.to.ConvertTo;
import org.stringtemplate.v4.ST;
public class ConvertToRanise extends ConvertTo {
public class ConvertToASAPTimeSA extends ConvertTo {
public static final Logger logger = Logger.getLogger("mohawk-converter");
@Override
public String convert(MohawkTARBACParser p, File f, Boolean writeToFile) {
try {
String template = ConvertTo.readFile(this.getClass().getResource("raniseTemplate.st"),
String template = ConvertTo.readFile(this.getClass().getResource("ASAPTimeSATemplate.st"),
Charset.defaultCharset());
ST st = new ST(template);
st.add("numRoles", p.roleHelper.size());
......
package mohawk.converter.to.uzun;
package mohawk.converter.to.tred;
import java.io.File;
import java.io.IOException;
......@@ -10,12 +10,12 @@ import java.util.logging.Logger;
import mohawk.converter.generated.MohawkTARBACParser;
import mohawk.converter.pieces.Rule;
import mohawk.converter.pieces.UzunRule;
import mohawk.converter.pieces.rules.TRedRule_Rule;
import mohawk.converter.to.ConvertTo;
import org.stringtemplate.v4.ST;
public class ConvertToUzun extends ConvertTo {
public class ConvertToTRedRole extends ConvertTo {
public static final Logger logger = Logger.getLogger("mohawk-converter");
@Override
......@@ -29,19 +29,19 @@ public class ConvertToUzun extends ConvertTo {
st.add("goalStates", p.query.toUzun(p.roleHelper));
for (Rule r : p.canAssign.getRules()) {
st.add("rules", new UzunRule(r, p.roleHelper, p.timeIntervalHelper));
st.add("rules", new TRedRule_Rule(r, p.roleHelper, p.timeIntervalHelper));
}
for (Rule r : p.canRevoke.getRules()) {
st.add("rules", new UzunRule(r, p.roleHelper, p.timeIntervalHelper));
st.add("rules", new TRedRule_Rule(r, p.roleHelper, p.timeIntervalHelper));
}
for (Rule r : p.canEnable.getRules()) {
st.add("rules", new UzunRule(r, p.roleHelper, p.timeIntervalHelper));
st.add("rules", new TRedRule_Rule(r, p.roleHelper, p.timeIntervalHelper));
}
for (Rule r : p.canDisable.getRules()) {
st.add("rules", new UzunRule(r, p.roleHelper, p.timeIntervalHelper));
st.add("rules", new TRedRule_Rule(r, p.roleHelper, p.timeIntervalHelper));
}
convertedStr = st.render();
......
package mohawk.converter.to.tred;
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.logging.Logger;
import mohawk.converter.generated.MohawkTARBACParser;
import mohawk.converter.pieces.Rule;
import mohawk.converter.pieces.rules.TRedRule_Rule;
import mohawk.converter.to.ConvertTo;
import org.stringtemplate.v4.ST;
public class ConvertToTRedRule extends ConvertTo {
public static final Logger logger = Logger.getLogger("mohawk-converter");
@Override
public String convert(MohawkTARBACParser p, File f, Boolean writeToFile) {
try {
String template = ConvertTo.readFile(this.getClass().getResource("uzunTemplate.st"),
Charset.defaultCharset());
ST st = new ST(template);
st.add("numRoles", p.roleHelper.size());
st.add("numTimeSlots", p.timeIntervalHelper.getNextTimeSlot());
st.add("goalStates", p.query.toUzun(p.roleHelper));
for (Rule r : p.canAssign.getRules()) {
st.add("rules", new TRedRule_Rule(r, p.roleHelper, p.timeIntervalHelper));
}
for (Rule r : p.canRevoke.getRules()) {
st.add("rules", new TRedRule_Rule(r, p.roleHelper, p.timeIntervalHelper));
}
for (Rule r : p.canEnable.getRules()) {
st.add("rules", new TRedRule_Rule(r, p.roleHelper, p.timeIntervalHelper));
}
for (Rule r : p.canDisable.getRules()) {
st.add("rules", new TRedRule_Rule(r, p.roleHelper, p.timeIntervalHelper));
}
convertedStr = st.render();
if (writeToFile) {
File convertedFile = new File(f.getAbsolutePath() + getFileExtenstion());
logger.info("[SAVE OUT]Saving converted format to file: " + convertedFile.getAbsolutePath()); </