Fakultas Ilmu Komputer UI

Commit 9652b320 authored by Muhammad Agil Ghifari's avatar Muhammad Agil Ghifari
Browse files

Implementing Selector class

parent 1f07b480
......@@ -51,6 +51,7 @@ public class TrackingInitializer {
eventMonitorRepository.addEventMonitor(dm.getName(), dm);
eventMonitorRepository.addEventMonitor(sm.getName(), sm);
// TODO : INSERT EXTRA BEHAVIOURS HERE
k2.addExtraBehaviour(new Selector(sm, Selector.ACCEPT, Selector.OR));
}
}
......@@ -14,6 +14,10 @@ public class Courier implements RoadUser {
private Map<String, EventMonitor> subscriptions;
private Map<String, String> locations;
// Extra behaviours
private Selector extraBehaviours = null;
private Selector lastSelector = null; // pointer to last selector
/*
speed: the speed of the courier
allowedRoutes: name of all possible routes this courier can visit
......@@ -54,11 +58,32 @@ public class Courier implements RoadUser {
String dragoLocation = locations.get("Drago Monitor");
for (String route : allRoutes) {
if (!route.equals(dragoLocation))
boolean takeRoute = !route.equals(dragoLocation);
Selector extraBehaviour = extraBehaviours;
while (extraBehaviour != null) {
takeRoute = extraBehaviour.excecute(
takeRoute, route, locations.get(extraBehaviour.getMonitor())
);
extraBehaviour = extraBehaviour.getNext();
}
if (takeRoute)
allowedRoutes.add(route);
}
}
@Override
public void addExtraBehaviour(Selector selector) {
if (lastSelector == null) {
extraBehaviours = selector;
lastSelector = selector;
}
else {
lastSelector.setNext(selector);
}
}
@Override
public int getSpeed() {
// TODO complete me
......
......@@ -13,6 +13,10 @@ public class DragoHunter implements RoadUser {
private Map<String, EventMonitor> subscriptions;
private Map<String, String> locations;
// Extra behaviours
private Selector extraBehaviours = null;
private Selector lastSelector = null; // pointer to last selector
/*
speed: the speed of the courier
allowedRoutes: name of all possible routes this courier can visit
......@@ -65,6 +69,17 @@ public class DragoHunter implements RoadUser {
return speed;
}
@Override
public void addExtraBehaviour(Selector selector) {
if (lastSelector == null) {
extraBehaviours = selector;
lastSelector = selector;
}
else {
lastSelector.setNext(selector);
}
}
@Override
public String getName() {
return name;
......
......@@ -10,4 +10,6 @@ public interface RoadUser {
List<String> getAllowedRoutes();
void checkSafeRoute();
int getSpeed();
void addExtraBehaviour(Selector selector);
}
package id.ac.ui.cs.advprog.tutorial1.tracking.core;
public class Selector {
public static final int ACCEPT = 1;
public static final int REJECT = 2;
public static final int AND = 1;
public static final int OR = 2;
private int mode; // ACCEPT or REJECT
private int operation; // AND or OR
private String monitor;
private Selector next;
public Selector(EventMonitor monitor, int mode, int operation, Selector next) {
this.monitor = monitor.getName();
this.mode = mode;
this.operation = operation;
this.next = next;
}
public Selector(EventMonitor monitor, int mode, int operation) {
this.monitor = monitor.getName();
this.mode = mode;
this.operation = operation;
this.next = null;
}
public void setNext(Selector next) {
this.next = next;
}
public Selector getNext() {
return next;
}
public boolean excecute(boolean input, String currRoute, String selectedRoute) {
boolean accRoute;
if (mode == ACCEPT) accRoute = currRoute.equals(selectedRoute);
else accRoute = !currRoute.equals(selectedRoute);
if (operation == AND) return input && accRoute;
else return input || accRoute;
}
public String getMonitor() {
return monitor;
}
}
Supports Markdown
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