Sensors and Observed Beliefs
A sensor updates the agent’s observations as part of its reasoning cycle. Use it when Java state or an environment may change independently of the agent’s own action. The light-device module keeps acting and sensing separate: the action changes device state, while the sensor publishes evidence of its state.
Starting Point
A sensor is a public method carrying @SENSOR, with no arguments and a void return type. The light device’s sensor is:
@SENSOR
public void sense() {
String current = state;
if (current.equals(observed)) return;
if (belief != null) agent.beliefs().dropBelief(belief);
belief = new Predicate("light", new Term[] {
Primitive.newPrimitive(current)
});
agent.beliefs().addBelief(belief);
observed = current;
}
The enclosing module declares state, observed and belief; its complete class and imports are in Chapter 5. Reading state into a local value ensures this observation is internally consistent even if another thread changes the device immediately afterward.
observed starts as null, so the first sensor call publishes the initial state. Later calls do nothing unless the state changes. The previous belief is removed before the new one is added, preserving the model’s single-current-state meaning.
Using the Module
The agent declares formula light(string); and can wait for the initial observation before using it:
wait(light("off"), 2000);
device.set("on");
wait(light("on"), 2000);
A plan such as plan +light(string State) reacts to a newly published state. Those event-driven reactions run separately from the action that caused the change. Do not assume that completing the action means every reaction has already finished.
Polling and External Changes
The simulated device is a Java field, so sensing is cheap. A real module might read a cached device value maintained by a callback, then publish changes from the reasoning thread. A sensor that performs blocking network I/O can delay the entire agent cycle.
Polling observes samples, not every transition. If the device changes on and off between sensor calls, the intermediate state may never become a belief. Use a custom event when each transition matters, and expose a term or formula when current state must remain queryable.
Ownership and Consistency
Choose one mechanism to maintain a particular observed predicate. If both a sensor and ordinary plans overwrite it independently, the agent can report a desired state as if it were observed, or repeatedly restore stale knowledge.
Module instances belong to their agents. A sensor updates its associated agent’s beliefs; it does not broadcast them to other agents. Share observations deliberately through messages or an environment interface.
Continue with Custom Events for callback notifications and Handling Uncertainty for bounded confirmation waits.