Commit 2c9354de authored by Jonathan Shahen's avatar Jonathan Shahen

Reorganizing files and small fix needed in the parser: changed Role to MyRole...

Reorganizing files and small fix needed in the parser: changed Role to MyRole because of a class with the class Role, and the Query now takes a TimeSlot instead of an Integer
parent 295c0f9a
......@@ -12,7 +12,7 @@ import java.util.logging.Logger;
import org.apache.commons.lang3.StringUtils;
import mohawk.converter.helper.*;
import mohawk.converter.pieces.*;
import mohawk.global.pieces.*;
}
@members {
......@@ -157,7 +157,7 @@ query
:
Query Colon t = timeslot Comma ra = roleArray
{
query._timeslot = $t.val;
query._timeslot = new TimeSlot($t.val);
query._roles = $ra.r;
}
......@@ -198,7 +198,7 @@ locals [Boolean adminAllRoles]
$r = new Rule();
$r._type = rt;
if($adminAllRoles == true) {
$r._adminRole = mohawk.converter.pieces.Role.allRoles();
$r._adminRole = Role.allRoles();
} else {
$r._adminRole = $adminRole.role;
}
......@@ -296,9 +296,9 @@ timeslot returns [Integer val]
myrole returns [String rolet, Role role]
:
Role
MyRole
{
$rolet = $Role.getText();
$rolet = $MyRole.getText();
$role = new Role($rolet);
roleHelper.add($role);
}
......@@ -408,7 +408,8 @@ RoleStrict
'role' INT
;
*/
Role
/* Called MyRole because there exists a class named Role */
MyRole
:
[A-Za-z0-9]+
;
......
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;
public class RoleTimeSlotHelper {
}
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 = -1;
public Integer minTimeSlot = -1;
public void add(TimeInterval ti) {
_timeslots.add(ti);
maxTimeSlot = Math.max(maxTimeSlot, ti._finish);
minTimeSlot = Math.min(minTimeSlot, ti._start);
}
/**
* 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 getNumberOfTimeSlots() {
if (maxTimeSlot == -1) { return -1; }
return maxTimeSlot - minTimeSlot + 1; // if maxTimeSlot == minTimeSlot => returns 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 + 1;
}
public Integer size() {
return _timeslots.size();
}
@Override
public String toString() {
return _timeslots.toString();
}
}
package mohawk.converter.pieces;
public class CanAssign extends CanBlock {
public CanAssign() {
super();
_type = RuleType.ASSIGN;
}
}
package mohawk.converter.pieces;
import java.util.ArrayList;
public class CanBlock {
protected RuleType _type;
protected ArrayList<Rule> _rules = new ArrayList<Rule>();
public int size() {
return _rules.size();
}
public ArrayList<Rule> getRules() {
return _rules;
}
public Rule[] getRulesArray() {
return _rules.toArray(new Rule[size()]);
}
public void addRule(Rule r) {
_rules.add(r);
}
@Override
public String toString() {
return _type + " Block (" + _rules.size() + ")";
}
}
package mohawk.converter.pieces;
public class CanDisable extends CanBlock {
public CanDisable() {
super();
_type = RuleType.DISABLE;
}
}
package mohawk.converter.pieces;
public class CanEnable extends CanBlock {
public CanEnable() {
super();
_type = RuleType.ENABLE;
}
}
package mohawk.converter.pieces;
public class CanRevoke extends CanBlock {
public CanRevoke() {
super();
_type = RuleType.REVOKE;
}
}
package mohawk.converter.pieces;
public enum ExpectedResult {
TRUE("Query Reachable"), FALSE("Query Unreachable"), UNKNOWN("Unknown");
private String _str;
ExpectedResult(String str) {
_str = str;
}
@Override
public String toString() {
return "Expected Result: " + _str;
}
}
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;
public ArrayList<Role> _roles;
@Override
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;
}
}
package mohawk.converter.pieces;
import java.util.HashMap;
public class Role implements Comparable<Role> {
public String _name;
public Boolean _not;
private Boolean _allRoles = false;
public Role(String name, Boolean not) {
this._name = name;
this._not = not;
}
public Role(String name) {
this(name, false);
}
public static Role allRoles() {
Role r = new Role("");
r._allRoles = true;
return r;
}
@Override
public String toString() {
if (_allRoles == true) { return "TRUE"; }
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);
}
@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);
}
}
package mohawk.converter.pieces;
public class RoleStrict {
public static String _prefix = "role";
public Integer _name;
public Boolean _not;
public RoleStrict(Integer name, Boolean not) {
this._name = name;
this._not = not;
}
@Override
public String toString() {
if (_not == false) {
return _prefix + _name;
} else {
return "NOT " + _prefix + _name;
}
}
}
package mohawk.converter.pieces;
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 TimeInterval _adminTimeInterval;
public ArrayList<Role> _preconditions = new ArrayList<Role>();
public ArrayList<Integer> _roleSchedule = new ArrayList<Integer>();
public Role _role;
public String toRanise(HashMap<String, Integer> roles) {
String sep = " , ";
StringBuffer s = new StringBuffer();
s.append(_type.toRanise() + " ");
s.append(roles.get(_adminRole) + sep);
if (_adminTimeInterval.getStartTime() != _adminTimeInterval.getFinishTime()) {
logger.severe("TODO: Implement the time interval splitting!");
}
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 = " ";
if (pre.size() == 0) { return "true "; }
StringBuffer s = new StringBuffer();
for (Role r : pre) {
s.append(r.toRanise(roles) + sep);
}
return s.toString();
}
@Override
public String toString() {
if (_type == null) { return "<Unknown Type Rule>"; }
StringBuffer s = new StringBuffer();
s.append(_type + " <");
s.append(_adminRole.toString() + ", ");
s.append("[t" + _adminTimeInterval.getStartTime() + "-t" + _adminTimeInterval.getFinishTime() + "], ");
if (_preconditions.size() == 0) {
s.append("TRUE, ");
} else {
s.append(_preconditions.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();
}
}
package mohawk.converter.pieces;
public enum RuleType {
ASSIGN("CanAssign", "A"), DISABLE("CanDisable", "D"), ENABLE("CanEnable", "E"), REVOKE("CanRevoke", "R");
private final String _name;
public final String _shortform;
RuleType(String name, String shortform) {
this._name = name;
this._shortform = shortform;
}
@Override
public String toString() {
return this._name;
}
public String toRanise() {
String tmp = _name.toLowerCase();
tmp.replaceFirst("can", "can_");
return tmp;
}
public String toUzun() {
return _shortform;
}
}
package mohawk.converter.pieces;
public class TimeInterval implements Comparable<TimeInterval> {
private final Boolean sortByStartTime = true;
public Integer _start = null;
public Integer _finish = null;
public TimeInterval(Integer startFinish) {
this(startFinish, startFinish);
}
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 isTimeslot() {
return _start.equals(_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 + "]";
}
@Override
public int compareTo(TimeInterval o) {
if (equals(o)) { return 0; }
if (sortByStartTime) {
return _start.compareTo(o._start);
} else {
return _finish.compareTo(o._finish);
}
}
}
......@@ -2,8 +2,8 @@ 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;
import mohawk.global.pieces.Role;
import mohawk.global.pieces.Rule;
public class ASAPTimeNSA_Rule {
public String ruleType;
......
......@@ -2,8 +2,8 @@ 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;
import mohawk.global.pieces.Role;
import mohawk.global.pieces.Rule;
public class ASAPTimeSA_Rule {
public String ruleType;
......
......@@ -2,8 +2,8 @@ package mohawk.converter.pieces.rules;
import mohawk.converter.helper.RoleHelper;