API: emitter

Thread-safe streaming writer for STL statements.

Module: stl_parser.emitter Import: from stl_parser import STLEmitter


STLEmitter

STLEmitter(
    log_path: Optional[str] = None,
    namespace: Optional[str] = None,
    stream: Optional[TextIO] = None,
    auto_timestamp: bool = True,
    auto_validate: bool = False
)

Context manager for writing STL statements to files or streams.

Parameters:

ParameterTypeDefaultDescription
log_pathstrNoneFile path for output (append mode)
namespacestrNoneDefault namespace prefix for anchors
streamTextIONoneStream object (e.g., sys.stdout)
auto_timestampboolTrueInject timestamp modifier on each statement
auto_validateboolFalseValidate statements before emitting

At least one of log_path or stream must be provided.

Raises: STLEmitterError (E800-E801)


.emit()

STLEmitter.emit(
    source_anchor: str,
    target_anchor: str,
    **modifiers: Any
) -> Statement

Build and emit an STL statement. Applies namespace prefix and optionally injects timestamp.

Returns: The emitted Statement.

Example:

with STLEmitter("events.stl") as emitter:
    emitter.emit("[Sensor_A]", "[Reading]", confidence=0.95, rule="empirical")

.emit_statement()

STLEmitter.emit_statement(statement: Statement) -> Statement

Emit a pre-built Statement without modification.


.comment()

STLEmitter.comment(text: str) -> None

Write a comment line (# text) to outputs.


.section()

STLEmitter.section(name: str) -> None

Write a section separator comment (\n# name\n).


Context Manager

with STLEmitter("output.stl", namespace="Sys") as emitter:
    emitter.section("Events")
    emitter.emit("[Login]", "[Session]", confidence=1.0)
    emitter.comment("End of events")

Thread-safe: uses threading.Lock internally for concurrent emit calls.