Skip to content

Environment Context

Benjamin Sommerfeld edited this page Jan 30, 2026 · 5 revisions

Static configuration is boring. Sometimes, the path depends on who is walking, not just where they are walking.

  • A Ghost ignores walls.

  • A Boat ignores water costs but hates land.

  • A VIP has a keycard for Door #3.

The EnvironmentContext is a dynamic data suitcase you pass to a specific search request. It overrides the global rules.


The Interface (It's a Bucket)

It is a Marker Interface. It has no methods. It is literally just a type-safe bucket for you to dump your data into.

1. Define the Payload

Create a class that implements EnvironmentContext. Put whatever you want in it.

public class DroneContext implements EnvironmentContext {
    private final double batteryLevel;
    private final boolean hasClearance;

    public DroneContext(double battery, boolean clearance) {
        this.batteryLevel = battery;
        this.hasClearance = clearance;
    }

    // Getters...
    public boolean hasClearance() { return hasClearance; }
}

2. Inject the Logic

You can access this context inside any Processor or Provider. The Catch: Since the interface is generic, you have to cast it. It's Java. Deal with it.

public class SecurityDoorValidator implements ValidationProcessor {
    @Override
    public boolean isValid(EvaluationContext context) {
        // 1. Unwrap the payload
        EnvironmentContext payload = context.getEnvironmentContext();

        // 2. Check if it's the right type
        if (payload instanceof DroneContext) {
             DroneContext drone = (DroneContext) payload;
             
             // 3. Apply dynamic logic
             if (drone.hasClearance()) {
                 return true; // Let them pass
             }
        }
        
        // Default: Block access
        return false;
    }
}

3. Warp Reality (Providers)

This is the killer feature. You can use the context in your NavigationPointProvider to change the fundamental physics of the world per request.

public class DroneWorldProvider implements NavigationPointProvider {
    @Override
    public NavigationPoint getNavigationPoint(PathPosition pos, EnvironmentContext ctx) {
        
        if (ctx instanceof DroneContext) {
            // If it's storming, the SKY becomes a WALL.
            if (((DroneContext) ctx).isStormy()) {
                return () -> false; // Non-traversable
            }
        }
        
        // Normal logic...
        return () -> true;
    }
}

4. Fire the Request

Pass the context when you start the search.

// Search A: High Clearance
pathfinder.findPath(start, end, new DroneContext(100, true));

// Search B: Low Clearance (Will find a different path!)
pathfinder.findPath(start, end, new DroneContext(100, false));

Clone this wiki locally