3.2 KiB
Codelet System
Overview
The codelet system is a fundamental component of the Copycat architecture that defines the basic structure and behavior of codelets. Codelets are small, specialized agents that perform specific operations in the workspace, forming the basis of the system's parallel processing capabilities.
Key Features
- Codelet structure
- Behavior definition
- Priority management
- State tracking
- Execution control
Codelet Types
-
Basic Codelets
- Scout codelets
- Builder codelets
- Evaluator codelets
- Breaker codelets
-
Specialized Codelets
- Group codelets
- Bond codelets
- Correspondence codelets
- Rule codelets
-
Control Codelets
- Temperature codelets
- Pressure codelets
- Urgency codelets
- Cleanup codelets
Usage
Codelets are created and managed through the codelet system.
The behavior parameter in codelet.set_behavior would typically be a reference to one of the many codelet methods defined in codeletMethods.py. For example, a codelet might be assigned a behavior like:
- build_group - to create a new group of related elements
- evaluate_bond - to assess the strength of a bond
- scout_for_correspondence - to look for potential mappings between structures The behavior is what gives each codelet its purpose and role in the system's parallel processing architecture. When the codelet is executed via codelet.run(), it performs this assigned behavior in the context of the current workspace state.
# Create a codelet
codelet = Codelet(name, priority)
# Set behavior
codelet.set_behavior(behavior)
# Execute codelet
result = codelet.run()
Codelet Decorator
The @codelet decorator is defined in codeletMethods.py and is used to mark functions as codelet behaviors. Here's its implementation:
def codelet(name):
"""Decorator for otherwise-unused functions that are in fact used as codelet behaviors"""
def wrap(f):
# Verify that the decorated function has exactly two parameters:
# 1. ctx - the context object containing workspace, slipnet, etc.
# 2. codelet - the codelet instance itself
# The None values in the tuple represent: no default args, no *args, no **kwargs
assert tuple(inspect.getargspec(f)) == (['ctx', 'codelet'], None, None, None)
# Mark this function as a valid codelet method
f.is_codelet_method = True
# Store the codelet type name for reference
f.codelet_name = name
return f
return wrap
The decorator:
- Takes a
nameparameter that identifies the type of codelet - Wraps the decorated function with additional metadata:
- Marks the function as a codelet method with
is_codelet_method = True - Stores the codelet name with
codelet_name = name
- Marks the function as a codelet method with
- Verifies that the decorated function has the correct signature (must take
ctxandcodeletparameters)
Example usage:
@codelet('breaker')
def breaker(ctx, codelet):
# Codelet behavior implementation
pass
Dependencies
- Python 3.x
- No external dependencies required
Related Components
- Coderack: Manages codelet execution
- CodeletMethods: Provides codelet behaviors
- Workspace: Environment for codelets
- Temperature: Influences codelet behavior