-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerson.java
More file actions
155 lines (141 loc) · 4.62 KB
/
Person.java
File metadata and controls
155 lines (141 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Copyright 2018 David Prentiss
*/
package sim.app.agentcity;
import sim.util.*;
import sim.engine.*;
public class Person implements Steppable, VehicleClient {
// MASON
private static final long serialVersionUID = 1;
Stoppable stopper;
long step;
// Properties
public final int idNum;
// Variables
private AgentCity ac;
private Intersection origin = null;
private Vehicle vehicle = null;
private Intersection destination = null;
private boolean inVehicle = false;
private boolean atDestination = false;
private int stepsAlive = 0;
private int stepsWaiting = 0;
private int stepsTraveling = 0;
private long lastStep = -1;
private long firstStep = -1;
private int cellsTraveled = 0;
private int cellsPlanned = 0;
// Accessors
public int getStepsTraveling() { return stepsTraveling; }
public Intersection getOrigin() { return origin; }
public Vehicle getVehicle() { return vehicle; }
public Intersection getDestination() { return destination; }
public boolean inVehicle() { return inVehicle; }
public boolean atDestination() {
atDestination =
ac.intersectionGrid
.field[vehicle.getLocation().x][vehicle.getLocation().y]
== destination.idNum;
return(atDestination);
}
/** Constructor */
public Person(int id,
Intersection origin,
Intersection destination, Vehicle vehicle) {
idNum = id;
this.origin = origin;
this.destination = destination;
if (vehicle != null) {
this.boardVehicle(vehicle);
}
if (origin != null) {
cellsPlanned = Math.abs(destination.minX - origin.minX) +
Math.abs(destination.minY - origin.minY);
}
}
/** Constructor */
public Person(int id,
Intersection origin, Intersection destination) {
this(id, origin, destination, null);
}
@Override
public String toString() {
return new StringBuilder()
.append("{\"PersonAgent\": {")
.append("\"idNum\": " + idNum)
.append(", ")
.append("\"originIdNum\": " + origin.idNum)
.append(", ")
.append("\"destinationIdNum\": " + destination.idNum)
.append(", ")
.append("\"inVehicle\": " + inVehicle)
.append(", ")
.append("\"atDestination\": " + atDestination)
.append(", ")
.append("\"stepsAlive\": " + stepsAlive)
.append(", ")
.append("\"stepsWaiting\": " + stepsWaiting)
.append(", ")
.append("\"stepsTraveling\": " + stepsTraveling)
.append(", ")
.append("\"cellsPlanned\": " + cellsPlanned)
.append(", ")
.append("\"cellsTraveled\": " + cellsTraveled)
.append(", ")
.append("\"firstStep\": " + firstStep) .append(", ")
.append("\"lastStep\": " + lastStep)
.append("}},\n")
.toString();
}
private boolean boardVehicle(Vehicle v) {
if (v.boardVehicle(this)) {
this.vehicle = v;
inVehicle = true;
return true;
} else {
return false;
}
}
private boolean exitVehicle() {
if (vehicle.exitVehicle(this)) {
this.vehicle = null;
inVehicle = false;
return true;
} else {
return false;
}
}
public void step(final SimState state) {
// get Simulation state
ac = (AgentCity)state;
if (firstStep < 0) firstStep = ac.schedule.getSteps();
stepsAlive++;
if (!inVehicle) {
stepsWaiting++;
Bag vehicles = origin.getController().getVehicles();
Vehicle v;
for (int i = 0; i < vehicles.numObjs; i++) {
v = (Vehicle)vehicles.objs[i];
if (boardVehicle(v)) {
break;
}
}
} else {
stepsTraveling++;
cellsTraveled += vehicle.getSpeed();
if (atDestination()) {
if (exitVehicle()) {
lastStep = ac.schedule.getSteps();
ac.reportTrip(this);
}
if (ac.removeTraveler(this)) {
this.stopper.stop();
}
if (origin != null) {
//if (ac.CONSOLE_OUT) { System.out.print(this); }
//if (ac.FILE_OUT) { ac.fileout.print(this); }
}
}
}
}
}