Experiment with a belief–desire–intention (BDI) agent for an old man living in a house.
To run the script, open the terminal in your Codespace and execute the following command:
npm startThis will run the src/main.js file using Node.js. Ensure that all dependencies are installed by running npm install before executing the script.
The project uses esbuild to bundle the source code into a single file for distribution. To build the project, run the following command in the terminal:
npm run buildThis command uses esbuild with the following parameters:
--bundle: Bundles all dependencies and source files into a single file.--outfile=dist/bundle.js: Specifies the output file location asdist/bundle.js.--format=esm: Ensures the output file is in ES Module format, preservingimport/exportsyntax.--target=esnext: Ensures the code is not transpiled and remains in ESNext syntax.--minify-whitespace: Removes unnecessary whitespace from the output file to reduce its size.
The bundled file is placed in the dist/ directory, making it ready for distribution or deployment.
To run the bundle.js file from the root directory of the project, use the following command in the terminal:
node dist/bundle.jsThis will execute the bundled code and display the output in the terminal.
esbuild was installed as a development dependency using the following command:
npm install esbuild --save-devThis ensures that esbuild is available for building the project but is not included in the production dependencies.
This project uses the Jest framework for unit testing. Jest is a JavaScript testing framework designed to ensure correctness of any JavaScript codebase.
To ensure that Jest can properly run tests with modern JavaScript syntax (including ES modules, JSX, and other features), Babel is used to transpile the code before it is executed by Jest. Babel transforms the source code into a format that is compatible with the Node.js environment used by Jest.
The Babel configuration is defined in the .babelrc file in the root of the project. This file specifies the presets and plugins that Babel should use when transpiling the code.
{
"presets": ["@babel/preset-env"]
}To run the tests, execute the following command in the terminal:
npm testThis will execute all test files located in the tests directory.
Jest was installed as a development dependency using the following command:
npm install --save-dev jestAll test files are located in the tests directory, which mirrors the structure of the src directory. For example:
/workspaces/emeritus-agent
├── src
│ ├── main.js
│ ├── bdiagent
│ │ ├── Agent.js
│ │ ├── AgentFactory.js
│ │ └── ...
│ └── ...
├── tests
│ ├── main.test.js
│ ├── bdiagent
│ │ ├── Agent.test.js
│ │ ├── AgentFactory.test.js
│ │ └── ...
├── package.json
└── jest.config.js
This structure ensures that test files are organized and easy to locate.
The jest.config.js file is located in the root of the project and is used to configure Jest. It specifies various settings that Jest uses when running tests, such as the test environment, module resolution, and code transformation.
/** @type {import('jest').Config} */
const config = {
testEnvironment: 'node',
transform: {
'^.+\\.js$': 'babel-jest',
},
moduleFileExtensions: ['js', 'mjs'],
moduleDirectories: ['node_modules', '<rootDir>/src'],
};
export default config;- Simulation: Orchestrates the simulation loop, manages agents, and coordinates time and scene updates.
- Agent: The main BDI agent class. Integrates belief, desire, intention, and motion systems. Runs the agent's decision and action loop.
- AgentFactory: Factory for creating and initializing agents with default or custom configurations.
- Belief: Abstract base class for agent beliefs.
- IntegerPercentageBelief: Belief with a value between 0 and 100 (e.g., hunger).
- BeliefUpdater: Abstract class for updating beliefs.
- IntegerPercentageBeliefUpdater: Updates an IntegerPercentageBelief by a rate.
- BeliefManager: Manages all beliefs and updaters for an agent.
- Desire: Represents a possible goal for the agent.
- DesireFactory: Factory for creating desires.
- DesireManager: Manages and evaluates all desires for the agent.
- Intention: Represents a plan or commitment to achieve a desire.
- IntentionFactory: Factory for creating intentions from desires.
- IntentionManager: Manages the agent's current intention and execution.
- Motion: Abstract base/interface for agent motion systems (e.g., walking, stationary).
- WalkMotion: Implements walking motion, pathfinding, and stepwise movement.
- NullMotion: Implements a no-op motion for stationary agents.
- Position: Immutable 2D coordinate (x, y).
- Location: Named point of interest in a room, with a position.
- Room: Area in the environment, with a name, position, size, and locations.
- Scene: Manages all rooms and locations, provides pathfinding.
- SceneFactory: Utility for creating standard scenes (e.g., a house).
- Path: Sequence of room names representing a route between locations.
- TimeManager: Tracks simulation time, steps, and day/night cycle.
- TimeOfDay: Represents hours and minutes, with formatting utilities.
- TypeUtils: Static type-checking and validation helpers.
- LogBuffer: Buffered logging utility for simulation output.
This project includes the following classes, grouped by their respective directories:
main.js: The entry point of the application. It sets up the simulation environment and agent, then runs the simulation loop.
Agent: Represents the core BDI agent. It integrates theBeliefManager,DesireManager, andMovementcomponents. It processes its beliefs, selects the most pressing desire, forms an intention, and executes actions (including movement) in each simulation tick.AgentFactory: A factory class responsible for creating and initializingAgentinstances with predefined configurations, including initial beliefs, desires, intentions, and the simulation scene.
Belief: An abstract base class representing a belief held by the agent. Beliefs have a name and a value.IntegerPercentageBelief: A concrete implementation ofBeliefrepresenting a value between 0 and 100 (e.g., hunger, fatigue, bladder level).IntegerPercentageBeliefUpdater: A class responsible for updating the value of a specificIntegerPercentageBeliefover time based on a defined rate.BeliefManager: Manages the agent's collection of beliefs. It allows adding, retrieving, and updating beliefs. It also handles the registration and execution ofBeliefUpdaterinstances during the simulation tick.
Desire: Represents a potential goal or state the agent wants to achieve. Each desire has conditions (based on beliefs) for activation and a priority level.DesireManager: Manages the agent's desires. It allows adding and retrieving desires. Its primary role is to evaluate active desires based on the current state of the agent's beliefs and select the desire with the highest priority.
Intention: Represents the agent's commitment to achieve a selected desire. It encapsulates the plan or sequence of actions (including target locations for movement) required to fulfill the desire. It has conditions for completion.IntentionFactory: A factory class used to create specificIntentioninstances based on the activatedDesire. It includes logic for creating a default "null intention" when no desire is active or achievable.
Position: Represents an immutable 2D coordinate (x, y) within the simulation environment. Used as the basic unit for defining locations and room boundaries.Location: Represents a named point of interest within aRoom(e.g., "Kitchen Sink", "Bed"). Defined by a name and aPosition. Objects are mutable.Room: Represents a distinct area (e.g., "Kitchen", "Bedroom") defined by a name, position, size, and connections to adjacent rooms. Contains a list ofLocationobjects within its boundaries. Objects are mutable.Scene: Manages the overall simulation environment, containing allRoomobjects and theirLocations. Provides pathfinding capabilities (findShortestPath) using Breadth-First Search (BFS) to determine the sequence of rooms needed to travel between two locations.SceneFactory: A utility class designed to create pre-configuredSceneobjects, such as a standard house layout with interconnected rooms.Path: Represents an ordered sequence of room names, defining a route between two locations as calculated byScene.findShortestPath.Movement: Manages an agent's physical presence and movement within theScene. It tracks the agent's currentPosition, targetLocation, and calculates position updates based on speed and the path required to reach the destination.Motion: Abstract base class/interface for all agent motion systems. Defines the required interface for movement-related methods (e.g.,getSpeed,getPosition,moveTo,update).WalkMotion: Implements theMotioninterface for walking agents (e.g., man, cat). Handles pathfinding, stepwise movement, and room transitions.NullMotion: Implements theMotioninterface as a no-op for agents that do not move. All movement methods are inert.
TimeManager: Tracks the simulation time, including the current step, time of day, and day count. Provides methods to advance time and query the current time state.TimeOfDay: Represents the time of day as hours and minutes, with utility methods for formatting and comparison.
TypeUtils: Provides static utility methods for runtime type checking, such as ensuring a value is an instance of a specific class or a primitive type (e.g.,isInstanceOf,isNumber).LogBuffer: (Located insrc/utils/log/LogBuffer.js) A simple logging utility for buffering and retrieving log messages during simulation.