client

package
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2026 License: LGPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplySchema

func ApplySchema(ctx context.Context, baseURL, envID string, schema *SchemaBuilder) error

ApplySchema sends the schema configuration to an AChemDB server. The baseURL is the server's base URL (e.g., "http://localhost:8080"), and envID is the environment ID where the schema should be applied.

Example
package main

import (
	"context"

	"github.com/daniacca/achemdb/pkg/client"
)

func main() {
	ctx := context.Background()
	schema := client.NewSchema("test").
		Species("Test", "Test species", nil)

	// This would send the schema to the server
	// Uncomment to actually send:
	// err := client.ApplySchema(ctx, "http://localhost:8080", "test-env", schema)
	// if err != nil {
	// 	log.Fatal(err)
	// }

	_ = ctx
	_ = schema
}

func Ref

func Ref(field string) string

Ref creates a reference to a molecule field that can be used in payload values. The reference will be resolved to the actual value from the input molecule when the reaction fires. Accepts either "field" (becomes "$m.field") or "m.field" (becomes "$m.field").

func WhereEq

func WhereEq(field string, value any) func(*InputBuilder)

WhereEq is a helper function that returns a function to add equality conditions to an input builder. This is useful for chaining conditions when calling ReactionBuilder.Input.

Types

type CatalystBuilder

type CatalystBuilder struct {
	// contains filtered or unexported fields
}

CatalystBuilder provides a fluent API for building catalyst configurations. Catalysts increase the reaction rate when matching molecules are present in the environment.

func NewCatalyst

func NewCatalyst(species string) *CatalystBuilder

NewCatalyst creates a new catalyst builder for the specified species.

func (*CatalystBuilder) Build

func (cb *CatalystBuilder) Build() achem.CatalystConfig

Build converts the builder to a CatalystConfig.

func (*CatalystBuilder) MaxRate

func (cb *CatalystBuilder) MaxRate(max float64) *CatalystBuilder

MaxRate sets the maximum effective reaction rate, even when multiple catalysts are present. If not set, the rate can exceed 1.0.

func (*CatalystBuilder) RateBoost

func (cb *CatalystBuilder) RateBoost(boost float64) *CatalystBuilder

RateBoost sets the amount by which the reaction rate is increased for each matching catalyst molecule. The default is 0.1.

func (*CatalystBuilder) WhereEq

func (cb *CatalystBuilder) WhereEq(field string, value any) *CatalystBuilder

WhereEq adds an equality condition to filter catalyst molecules.

type CountMoleculesBuilder

type CountMoleculesBuilder struct {
	// contains filtered or unexported fields
}

CountMoleculesBuilder provides a fluent API for building count_molecules conditions. These conditions count molecules matching certain criteria and compare the count with a threshold.

func NewCountMolecules

func NewCountMolecules(species string) *CountMoleculesBuilder

NewCountMolecules creates a new count molecules builder for the specified species.

func (*CountMoleculesBuilder) Build

Build converts the builder to a CountMoleculesConfig.

func (*CountMoleculesBuilder) Op

func (cmb *CountMoleculesBuilder) Op(operator string, value any) *CountMoleculesBuilder

Op sets the comparison operator and threshold value for the count. Supported operators: "eq", "ne", "gt", "gte", "lt", "lte". Example: Op("gte", 3) means "count >= 3".

func (*CountMoleculesBuilder) WhereEq

func (cmb *CountMoleculesBuilder) WhereEq(field string, value any) *CountMoleculesBuilder

WhereEq adds an equality condition to filter which molecules are counted.

type CreateEffectBuilder

type CreateEffectBuilder struct {
	// contains filtered or unexported fields
}

CreateEffectBuilder provides a fluent API for building create effects. Create effects generate new molecules when a reaction fires.

func Create

func Create(species string) *CreateEffectBuilder

Create creates an effect builder for creating new molecules of the specified species when the reaction fires.

func (*CreateEffectBuilder) Build

Build converts the builder to a CreateEffectConfig.

func (*CreateEffectBuilder) Energy

func (ceb *CreateEffectBuilder) Energy(energy float64) *CreateEffectBuilder

Energy sets the initial energy value for the created molecule. If not set, the molecule will use the default energy value.

func (*CreateEffectBuilder) Payload

func (ceb *CreateEffectBuilder) Payload(field string, value any) *CreateEffectBuilder

Payload adds a field to the payload of the created molecule. The value can be a literal or a reference using Ref() to copy values from the input molecule.

func (*CreateEffectBuilder) Stability

func (ceb *CreateEffectBuilder) Stability(stability float64) *CreateEffectBuilder

Stability sets the initial stability value for the created molecule. If not set, the molecule will use the default stability value.

type EffectBuilder

type EffectBuilder struct {
	// contains filtered or unexported fields
}

EffectBuilder provides a fluent API for building reaction effects. Effects define what happens when a reaction fires, such as consuming molecules, creating new ones, or updating existing ones.

func Consume

func Consume() *EffectBuilder

Consume creates an effect that consumes (removes) the input molecule when the reaction fires.

func (*EffectBuilder) Build

func (eb *EffectBuilder) Build() achem.EffectConfig

Build converts the builder to an EffectConfig.

type IfConditionBuilder

type IfConditionBuilder struct {
	// contains filtered or unexported fields
}

IfConditionBuilder provides a fluent API for building conditional effects. Conditions can check molecule fields or count molecules in the environment.

func NewIfCount

func NewIfCount(cmb *CountMoleculesBuilder) *IfConditionBuilder

NewIfCount creates a count-based condition that checks the number of molecules matching certain criteria in the environment.

func NewIfField

func NewIfField(field, op string, value any) *IfConditionBuilder

NewIfField creates a field-based condition that compares a molecule field with a value. Supported operators: "eq", "ne", "gt", "gte", "lt", "lte".

func (*IfConditionBuilder) Build

Build converts the builder to an IfConditionConfig.

func (*IfConditionBuilder) Else

Else adds effects to execute if the condition is false.

func (*IfConditionBuilder) Then

Then adds effects to execute if the condition is true.

type IfEffectBuilder

type IfEffectBuilder struct {
	// contains filtered or unexported fields
}

IfEffectBuilder wraps an IfConditionBuilder to provide Then/Else methods for conditional effect execution.

func If

If creates a conditional effect builder that executes different effects based on a condition. Use NewIfField or NewIfCount to create the condition.

func (*IfEffectBuilder) Else

func (ieb *IfEffectBuilder) Else(ebs ...interface{}) *IfEffectBuilder

Else adds effects to execute if the condition is false. Accepts EffectBuilder, CreateEffectBuilder, or UpdateEffectBuilder.

func (*IfEffectBuilder) Then

func (ieb *IfEffectBuilder) Then(ebs ...interface{}) *IfEffectBuilder

Then adds effects to execute if the condition is true. Accepts EffectBuilder, CreateEffectBuilder, or UpdateEffectBuilder.

type InputBuilder

type InputBuilder struct {
	// contains filtered or unexported fields
}

InputBuilder provides a fluent API for building input configurations. Inputs define which molecules can trigger a reaction, including species filtering, where conditions, and partner requirements.

func NewInput

func NewInput(species string) *InputBuilder

NewInput creates a new input builder for the specified species.

func (*InputBuilder) Build

func (ib *InputBuilder) Build() achem.InputConfig

Build converts the builder to an InputConfig.

func (*InputBuilder) Partner

func (ib *InputBuilder) Partner(pb *PartnerBuilder) *InputBuilder

Partner adds a partner molecule requirement to the input. Partners are additional molecules that must be present for the reaction to fire.

func (*InputBuilder) WhereEq

func (ib *InputBuilder) WhereEq(field string, value any) *InputBuilder

WhereEq adds an equality condition to the where clause. Only molecules with the specified field matching the value will match.

type NotificationBuilder

type NotificationBuilder struct {
	// contains filtered or unexported fields
}

NotificationBuilder provides a fluent API for building notification configurations. Notifications allow external systems to be notified when reactions fire, either through webhooks, WebSocket, or callbacks.

Example (Callbacks)
package main

import (
	"github.com/daniacca/achemdb/pkg/client"
)

func main() {
	// Example showing how to enable notifications without notifiers
	// This is useful when using Achem as a Go library with callbacks
	schema := client.NewSchema("callback-demo").
		Species("Input", "Input species", nil).
		Species("Output", "Output species", nil).
		Reaction(client.NewReaction("transform").
			Input("Input").
			Rate(1.0).
			Effect(
				client.Consume(),
				client.Create("Output").
					Payload("message", "Transformed"),
			).
			// Enable notifications without notifiers - callbacks will be called
			Notify(client.NewNotification().
				Enabled(true),
			// No Notifiers() call - empty notifiers list is valid for callbacks
			),
		)

	_ = schema
}

func NewNotification

func NewNotification() *NotificationBuilder

NewNotification creates a new notification builder with notifications enabled by default.

func (*NotificationBuilder) Build

Build converts the builder to a NotificationConfig.

func (*NotificationBuilder) Enabled

func (nb *NotificationBuilder) Enabled(enabled bool) *NotificationBuilder

Enabled sets whether notifications are enabled for this reaction.

func (*NotificationBuilder) Notifier

Notifier adds a notifier ID to the list of notifiers to use. Notifiers must be registered with the server separately.

func (*NotificationBuilder) Notifiers

func (nb *NotificationBuilder) Notifiers(ids ...string) *NotificationBuilder

Notifiers adds multiple notifier IDs to the list.

type PartnerBuilder

type PartnerBuilder struct {
	// contains filtered or unexported fields
}

PartnerBuilder provides a fluent API for building partner molecule configurations. Partners are additional molecules required for a reaction to fire.

func NewPartner

func NewPartner(species string) *PartnerBuilder

NewPartner creates a new partner builder for the specified species.

func (*PartnerBuilder) Build

func (pb *PartnerBuilder) Build() achem.PartnerConfig

Build converts the builder to a PartnerConfig.

func (*PartnerBuilder) Count

func (pb *PartnerBuilder) Count(count int) *PartnerBuilder

Count sets the required number of partner molecules. The default is 1 if not specified.

func (*PartnerBuilder) WhereEq

func (pb *PartnerBuilder) WhereEq(field string, value any) *PartnerBuilder

WhereEq adds an equality condition to filter partner molecules.

type ReactionBuilder

type ReactionBuilder struct {
	// contains filtered or unexported fields
}

ReactionBuilder provides a fluent API for building reaction configurations. Reactions define how molecules of a specific species transform, including input patterns, rates, catalysts, and effects.

func NewReaction

func NewReaction(id string) *ReactionBuilder

NewReaction creates a new reaction builder with the given ID. The ID must be unique within a schema. The name defaults to the ID but can be overridden with the Name method.

func (*ReactionBuilder) Build

func (rb *ReactionBuilder) Build() achem.ReactionConfig

Build converts the builder to a ReactionConfig that can be used in schema definitions.

func (*ReactionBuilder) Catalyst

func (rb *ReactionBuilder) Catalyst(cb *CatalystBuilder) *ReactionBuilder

Catalyst adds a catalyst configuration to the reaction. Catalysts increase the reaction rate when matching molecules are present.

func (*ReactionBuilder) Effect

func (rb *ReactionBuilder) Effect(ebs ...interface{}) *ReactionBuilder

Effect adds one or more effects to the reaction. Effects define what happens when the reaction fires, such as consuming the input molecule, creating new molecules, or updating existing ones. Accepts EffectBuilder, CreateEffectBuilder, UpdateEffectBuilder, or IfEffectBuilder.

func (*ReactionBuilder) Input

func (rb *ReactionBuilder) Input(species string, whereEqs ...func(*InputBuilder)) *ReactionBuilder

Input sets the input species and optional where conditions for the reaction. The whereEqs parameter allows chaining WhereEq calls to filter which molecules of the species can trigger this reaction.

func (*ReactionBuilder) Name

func (rb *ReactionBuilder) Name(name string) *ReactionBuilder

Name sets the human-readable name for the reaction. If not set, the name defaults to the reaction ID.

func (*ReactionBuilder) Notify

Notify configures notification settings for this reaction. When enabled, notifications are sent when the reaction fires, allowing external systems to react to events in real-time.

Example
package main

import (
	"github.com/daniacca/achemdb/pkg/client"
)

func main() {
	schema := client.NewSchema("security-alerts").
		Species("Event", "Raw events", nil).
		Species("Alert", "Alerts", nil).
		Reaction(client.NewReaction("event_to_alert").
			Input("Event").
			Rate(1.0).
			Effect(
				client.Consume(),
				client.Create("Alert").
					Payload("message", "Event processed"),
			).
			Notify(client.NewNotification().
				Enabled(true).
				Notifiers("webhook-1", "websocket-1"),
			),
		)

	_ = schema
}

func (*ReactionBuilder) Rate

func (rb *ReactionBuilder) Rate(rate float64) *ReactionBuilder

Rate sets the base reaction rate, a value between 0.0 and 1.0. This represents the probability that the reaction will fire when a matching molecule is available. The effective rate can be modified by catalysts.

type SchemaBuilder

type SchemaBuilder struct {
	// contains filtered or unexported fields
}

SchemaBuilder provides a fluent API for building schemas. Use it to define species and reactions that describe how molecules interact in an artificial chemistry system.

Example
package main

import (
	"fmt"

	"github.com/daniacca/achemdb/pkg/client"
)

func main() {
	schema := client.NewSchema("security-alerts").
		Species("Event", "Raw events", nil).
		Species("Suspicion", "Suspicious stuff", nil).
		Species("Alert", "Alerts", nil).
		Reaction(client.NewReaction("login_failure_to_suspicion").
			Input("Event", client.WhereEq("type", "login_failed")).
			Rate(1.0).
			Effect(
				client.Consume(),
				client.Create("Suspicion").
					Payload("ip", client.Ref("m.ip")).
					Payload("kind", "login_failed").
					Energy(1.0).
					Stability(1.0),
			),
		)

	cfg := schema.Build()
	fmt.Printf("Schema: %s\n", cfg.Name)
	fmt.Printf("Species: %d\n", len(cfg.Species))
	fmt.Printf("Reactions: %d\n", len(cfg.Reactions))

	// Example: Apply to server (commented out for test)
	// ctx := context.Background()
	// err := client.ApplySchema(ctx, "http://localhost:8080", "production", schema)
	// if err != nil {
	// 	log.Fatal(err)
	// }
	_ = schema
}

func NewSchema

func NewSchema(name string) *SchemaBuilder

NewSchema creates a new schema builder with the given name. The name identifies the schema and is used for organization purposes.

func (*SchemaBuilder) Build

func (sb *SchemaBuilder) Build() achem.SchemaConfig

Build converts the builder to a SchemaConfig that can be used with ApplySchema or other AChemDB APIs.

func (*SchemaBuilder) Reaction

func (sb *SchemaBuilder) Reaction(rb *ReactionBuilder) *SchemaBuilder

Reaction adds a reaction definition to the schema. Reactions define how molecules transform when they interact.

func (*SchemaBuilder) Species

func (sb *SchemaBuilder) Species(name, description string, meta map[string]any) *SchemaBuilder

Species adds a species definition to the schema. A species represents a type of molecule in the system. The meta parameter can be nil or contain additional metadata.

type UpdateEffectBuilder

type UpdateEffectBuilder struct {
	// contains filtered or unexported fields
}

UpdateEffectBuilder provides a fluent API for building update effects. Update effects modify existing molecules when a reaction fires.

func Update

func Update() *UpdateEffectBuilder

Update creates an effect builder for updating existing molecules when the reaction fires.

func (*UpdateEffectBuilder) Build

Build converts the builder to an UpdateEffectConfig.

func (*UpdateEffectBuilder) EnergyAdd

func (ueb *UpdateEffectBuilder) EnergyAdd(amount float64) *UpdateEffectBuilder

EnergyAdd sets the amount to add to the molecule's energy. The energy is modified in place when the reaction fires.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL