Skip to content

Commit

Permalink
Issue 39 schedule (#40)
Browse files Browse the repository at this point in the history
* Schedule and ScheduleComponent started

* tasks can be added to schedule
  • Loading branch information
duartencar authored Jun 2, 2021
1 parent b1da372 commit e616203
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/agents/Vehicle.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logic.Request;

import map.GraphNode;
import map.Path;
import logic.Path;

import utils.Utils;

Expand Down
77 changes: 77 additions & 0 deletions src/logic/AlphaSchedule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package logic;

import map.Graph;
import map.GraphNode;

import java.util.Vector;

import static utils.Utils.log;

public class AlphaSchedule {
// must be threadSafe
Vector<ScheduleComponent> tasks;

public AlphaSchedule() {
tasks = new Vector<ScheduleComponent>();
}

public short getTotalScheduleCost() {
short sum = 0;

for(ScheduleComponent t : tasks) {
sum += t.getCost();
}

return sum;
}

public void addTask(ScheduleComponent task) {
GraphNode start = task.getStart(), end = task.getEnd();

if(tasks.isEmpty()) {
tasks.add(task);
}
// if new task end is the scheduled starting point
else if(tasks.get(0).getStart().getId() == end.getId()) {
tasks.add(0, task);
}
// if new task start is the scheduled last point
else if(tasks.get(tasks.size()-1).getEnd().getId() == start.getId()) {
tasks.add(task);
}
else {
for(int i = 1; i < tasks.size() - 1; i++) {
if(tasks.get(i).getStart().getId() == start.getId()) {
// replace with new task
tasks.add(i, task);

try {
Path newPathForNextTask = new Path (Graph.getInstance().getGraphToSearch().findPath(task.getEnd().getId(), tasks.get(i+1).getStart().getId()));
tasks.get(i+1).setNewPath(newPathForNextTask);
} catch (Exception e) {
log(e.getMessage());
}
}
}
}
}

@Override
public String toString() {
if(tasks.size() == 0) {
return "Empty";
}

String toReturn = tasks.get(0).getStart().getId() + " -> ";

for(int i = 0; i < tasks.size(); i++) {
toReturn += tasks.get(i).getEnd().getId();

if(i != tasks.size() - 1) {
toReturn += " -> ";
}
}

return toReturn;
}
}
6 changes: 5 additions & 1 deletion src/map/Path.java → src/logic/Path.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
package map;
package logic;

import map.Graph;
import map.GraphEdge;
import map.GraphNode;

import java.util.ArrayList;

Expand Down
50 changes: 50 additions & 0 deletions src/logic/ScheduleComponent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package logic;

import map.GraphNode;

import java.util.Date;

public class ScheduleComponent {
private Path pathToTarget;
private int requestId;
private Date deliveryTime;

public ScheduleComponent(Path path, int id, Date time) throws Exception {

if(path.getNumberOfSteps() < 2) {
throw new Exception("Path has to have at least two nodes.");
}

pathToTarget = path;
requestId = id;
deliveryTime = time;
}

public int getRequestId() {
return requestId;
}

public GraphNode getStart() {
return pathToTarget.getNodes().get(0);
}

public void setNewPath(Path p) {
pathToTarget = p;
}

public short getCost() {
return pathToTarget.getWeight();
}

public GraphNode getEnd() {
return pathToTarget.getNodes().get(pathToTarget.getNodes().size() - 1);
}

public Date getDeliveryTime() {
return deliveryTime;
}

public int compareTo(ScheduleComponent sc) {
return deliveryTime.compareTo(sc.getDeliveryTime());
}
}
35 changes: 35 additions & 0 deletions test/java/logic/AlphaSheduleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package logic;

import map.Graph;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import utils.Utils;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class AlphaSheduleTest {
private Graph getGraph() {
Graph a = Graph.getInstance();

final Document testFile = Utils.openAndParseXmlFile("test/resources/testGraph.xml");

NodeList nodeList = testFile.getElementsByTagName("node");

NodeList edgeList = testFile.getElementsByTagName("edge");

a.init(nodeList, edgeList);

return a;
}

@Test
public void testCreate() {
getGraph();

AlphaSchedule toTest = new AlphaSchedule();

assertEquals("Empty", toTest.toString(), "Path should be: Empty");
assertEquals((short)0, toTest.getTotalScheduleCost(), "Total cost should be 0");
}
}
1 change: 1 addition & 0 deletions test/java/map/PathTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package map;

import logic.Path;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
Expand Down

0 comments on commit e616203

Please sign in to comment.