Anylogic停车场景Delay块:如何获取空车位状态函数值?
Got it, let’s break down why you’re seeing that "undefined method" error and fix your parking spot check. The core issue here is that nFree() and spaceIndex() are only available for AnyLogic’s Storage component—they don’t exist for the Queue component you’re using.
Depending on how you’ve structured your parking simulation (Delay + Queue), here are the correct approaches to get free spot status:
1. If you’re using a Delay block with capacity as your parking lot (Queue for waiting vehicles)
This is the standard setup: The Delay block represents your parking area (set its capacity to your total number of spots), and the Queue holds vehicles waiting for an open spot.
- To get the number of free spots: Use the Delay block’s
getRemainingCapacity()method - To check if there’s at least one free spot: Use
yourDelayBlockName.getRemainingCapacity() > 0
Example code you can use in functions, triggers, or displays:
// Check if any parking spots are free boolean hasFreeSpot = parkingDelay.getRemainingCapacity() > 0; // Get exact count of free spots int freeSpots = parkingDelay.getRemainingCapacity();
2. If you’re using a Queue component as your parking lot
If you’ve set up a Queue with a fixed capacity (equal to total parking spots) to hold parked vehicles directly:
- Total spots =
yourQueueName.getCapacity() - Occupied spots =
yourQueueName.size() - Free spots =
yourQueueName.getCapacity() - yourQueueName.size() - Check for free spots =
yourQueueName.size() < yourQueueName.getCapacity()
Example snippet:
int freeParkingSpots = parkingQueue.getCapacity() - parkingQueue.size(); boolean isSpotAvailable = freeParkingSpots > 0;
Pro Tip
Whenever you’re unsure about a component’s available methods, right-click it in your model, select Help, and you’ll get a full list of all supported functions and their descriptions. It’s a quick way to avoid these mix-ups!
内容的提问来源于stack exchange,提问作者dihaben

