Skip to content

Commit

Permalink
Merge pull request #16 from goeuropa/fix_manual_assignment
Browse files Browse the repository at this point in the history
Fix manual assignment
  • Loading branch information
wkulesza authored May 22, 2024
2 parents 7aaeb20 + 1b25472 commit 0ba2bd5
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,10 @@ public Response getVehiclesDetails(

Collection<IpcVehicleConfig> vehicleConfigs = inter.getVehicleConfigs();

Map<String, List<IpcVehicle>> vehiclesGrouped = vehicles.stream().collect(Collectors.groupingBy(IpcVehicle::getId));
Map<String, List<IpcVehicleConfig>> vehiclesConfigsGrouped = vehicleConfigs.stream().collect(Collectors.groupingBy(IpcVehicleConfig::getId));
Map<String, List<IpcVehicle>> vehiclesGrouped = vehicles.stream()
.collect(Collectors.groupingBy(IpcVehicle::getId));
Map<String, List<IpcVehicleConfig>> vehiclesConfigsGrouped = vehicleConfigs.stream()
.collect(Collectors.groupingBy(IpcVehicleConfig::getId));

for (String id : vehiclesGrouped.keySet()) {
List<IpcVehicleConfig> configs = vehiclesConfigsGrouped.getOrDefault(id, List.of());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,14 @@ public static boolean ignoreAvlAssignments() {
+ "frequently. Especially important for agencies with high "
+ "reporting rates. So this param allows one to limit how "
+ "frequently auto assigner called for vehicle");

public static final BooleanConfigValue isManualAssignmentEnabled = new BooleanConfigValue(
"transitclock.blockAssigner.manualAssignmentEnabled",
false,
"Set to true to enable the manual assignment behavior where \"\n" +
" + \"the system tries to assign vehicle to a block regarding 'vehicle_to_block_configs' table.");

public static boolean isManualAssignmentEnabled() {
return isManualAssignmentEnabled.getValue();
}
}
12 changes: 7 additions & 5 deletions core/src/main/java/org/transitclock/core/AvlProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,14 @@ public void matchNewFixForPredictableVehicle(VehicleState vehicleState) {

logger.warn("For vehicleId={} {}", vehicleState.getVehicleId(), eventDescription);

// Remove the predictions for the vehicle
makeVehicleUnpredictable(vehicleState.getVehicleId(), eventDescription, VehicleEvent.NO_MATCH);
if (!BlockAssignerConfig.isManualAssignmentEnabled() && !vehicleState.getAvlReport().getAssignmentId()
.equals(bestTemporalMatch.getBlock().getId())) {
// Remove the predictions for the vehicle
makeVehicleUnpredictable(vehicleState.getVehicleId(), eventDescription, VehicleEvent.NO_MATCH);

// Remove block assignment from vehicle
vehicleState.unsetBlock(BlockAssignmentMethod.COULD_NOT_MATCH);
// Remove block assignment from vehicle
vehicleState.unsetBlock(BlockAssignmentMethod.COULD_NOT_MATCH);
}
}
} else {
logger.info(
Expand Down Expand Up @@ -702,7 +705,6 @@ private boolean matchVehicleToBlockAssignment(Block block, VehicleState vehicleS
// Update the state of the vehicle
updateVehicleStateFromAssignment(
bestMatch, vehicleState, BlockAssignmentMethod.AVL_FEED_BLOCK_ASSIGNMENT, block.getId(), "block");

// Return true if predictable
return bestMatch != null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.Serializable;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.time.LocalTime;
import java.util.*;
import java.util.stream.Collectors;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import org.hibernate.Transaction;
import org.hibernate.annotations.DynamicUpdate;
import org.transitclock.Core;
import org.transitclock.core.BlockAssignmentMethod;
import org.transitclock.core.VehicleState;
import org.transitclock.core.dataCache.VehicleStateManager;

import java.io.Serializable;
import java.util.Date;
Expand Down Expand Up @@ -116,14 +119,21 @@ public static void updateVehicleToBlockConfig(VehicleToBlockConfig vehicleToBloc
}

public static void deleteVehicleToBlockConfig(long id, Session session) throws HibernateException {
String vehicleId = session
.createQuery("FROM VehicleToBlockConfig WHERE id = :id", VehicleToBlockConfig.class)
.setParameter("id", id).getSingleResult().getVehicleId();

Transaction transaction = session.beginTransaction();
try {
session
.createMutationQuery("delete from VehicleToBlockConfig where id = :id")
.setParameter("id", id)
.executeUpdate();

transaction.commit();
VehicleState vehicleState = VehicleStateManager.getInstance()
.getVehicleState(vehicleId);
vehicleState.unsetBlock(BlockAssignmentMethod.ASSIGNMENT_TERMINATED);
vehicleState.setMatch(null);
} catch (Throwable t) {
transaction.rollback();
throw t;
Expand Down

0 comments on commit 0ba2bd5

Please sign in to comment.