observer

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2024 License: MIT Imports: 1 Imported by: 1

Documentation

Overview

Package observer provides interfaces for observers that extract data from the ECS world. Observers are required by reporter systems (see github.com/mlange-42/arche-model/reporter). The observer extracts the data, while the reporter handles it by e.g. showing a plot or writing to a file.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Grid

type Grid interface {
	Matrix           // Methods from Matrix observer.
	X(c int) float64 // X axis coordinates.
	Y(r int) float64 // Y axis coordinates.
}

Grid observer interface. Provides dimensionality, axis information, and a matrix of values per call.

See also Matrix and GridLayers. See package github.com/mlange-42/arche-model/reporter for usage examples.

Example
package main

import (
	"github.com/mlange-42/arche-model/observer"
	"github.com/mlange-42/arche/ecs"
)

// Example observer, reporting a grid with z = x^2 + y.
type GridObserver struct {
	cols     int
	rows     int
	cellsize float64
	xOrigin  float64
	yOrigin  float64
	values   []float64
}

func (o *GridObserver) Initialize(w *ecs.World) {
	// In a real example, we would get these values from the world,
	// e.g. from a resource representing a global grid.
	o.cols = 24
	o.rows = 16
	o.cellsize = 1000
	o.xOrigin = 123_000
	o.yOrigin = 234_000

	o.values = make([]float64, o.cols*o.rows)
}

func (o *GridObserver) Update(w *ecs.World) {}

func (o *GridObserver) Dims() (int, int) {
	return o.cols, o.rows
}

func (o *GridObserver) X(c int) float64 {
	return o.xOrigin + o.cellsize*float64(c)
}

func (o *GridObserver) Y(r int) float64 {
	return o.yOrigin + o.cellsize*float64(r)
}

func (o *GridObserver) Values(w *ecs.World) []float64 {
	for idx := 0; idx < len(o.values); idx++ {
		x := o.X(idx % o.cols)
		y := o.Y(idx / o.cols)
		o.values[idx] = float64(x*x + y)
	}
	return o.values
}

func main() {
	var _ observer.Grid = &GridObserver{}
}
Output:

func MatrixToGrid added in v0.3.0

func MatrixToGrid(obs Matrix, origin *[2]float64, cellsize *[2]float64) Grid

MatrixToGrid creates an observer that serves as adapter from a Matrix observer to a Grid observer.

Example
// A Matrix observer
var matrix observer.Matrix = &MatrixObserver{}

// A MatrixToGrid observer, wrapping the Matrix observer
var _ observer.Grid = observer.MatrixToGrid(
	matrix,
	&[...]float64{100, 200},
	&[...]float64{1000, 1000},
)
Output:

type GridLayers added in v0.3.0

type GridLayers interface {
	MatrixLayers     // Methods from Matrix observer.
	X(c int) float64 // X axis coordinates.
	Y(r int) float64 // Y axis coordinates.
}

GridLayers observer interface. Provides dimensionality, axis information, and multiple matrices of values per call.

See also Grid, Matrix and GridLayers. See package github.com/mlange-42/arche-model/reporter for usage examples.

func GridToLayers added in v0.3.0

func GridToLayers(obs ...Grid) GridLayers

GridToLayers creates an observer that serves as adapter from multiple Grid observers to a GridLayers observer.

Example
// Multiple Grid observers
var grid1 observer.Grid = &GridObserver{}
var grid2 observer.Grid = &GridObserver{}
var grid3 observer.Grid = &GridObserver{}

// A MatrixToGrid observer, wrapping the Grid observers
var _ observer.GridLayers = observer.GridToLayers(grid1, grid2, grid3)
Output:

func LayersToLayers added in v0.3.0

func LayersToLayers(obs MatrixLayers, origin *[2]float64, cellsize *[2]float64) GridLayers

LayersToLayers creates an observer that serves as adapter from a MatrixLayers observer to a GridLayers observer.

Example
// Multiple Matrix observers
var matrix1 observer.Matrix = &MatrixObserver{}
var matrix2 observer.Matrix = &MatrixObserver{}
var matrix3 observer.Matrix = &MatrixObserver{}

// A MatrixToGrid observer, wrapping the Matrix observers
var layers observer.MatrixLayers = observer.MatrixToLayers(matrix1, matrix2, matrix3)

// A GridLayers observer, wrapping the MatrixLayers observer
var _ observer.GridLayers = observer.LayersToLayers(layers, nil, nil)
Output:

type Matrix

type Matrix interface {
	Initialize(w *ecs.World)       // Initialize the observer. No other methods are called before this.
	Update(w *ecs.World)           // Update the observer.
	Dims() (int, int)              // Matrix dimensions.
	Values(w *ecs.World) []float64 // Values for the current model tick, in row-major order (i.e. idx = row*ncols + col).
}

Matrix observer interface. Provides dimensionality, and a matrix of values per call.

See also Grid. See package github.com/mlange-42/arche-model/reporter for usage examples.

Example
package main

import (
	"github.com/mlange-42/arche-model/observer"
	"github.com/mlange-42/arche/ecs"
)

// Example observer, reporting a matrix with z = i^2 + j.
type MatrixObserver struct {
	cols   int
	rows   int
	values []float64
}

func (o *MatrixObserver) Initialize(w *ecs.World) {
	o.cols = 24
	o.rows = 16
	o.values = make([]float64, o.cols*o.rows)
}

func (o *MatrixObserver) Update(w *ecs.World) {}

func (o *MatrixObserver) Dims() (int, int) {
	return o.cols, o.rows
}

func (o *MatrixObserver) Values(w *ecs.World) []float64 {
	for idx := 0; idx < len(o.values); idx++ {
		i := idx % o.cols
		j := idx / o.cols
		o.values[idx] = float64(i*i + j)
	}
	return o.values
}

func main() {
	var _ observer.Matrix = &MatrixObserver{}
}
Output:

type MatrixLayers added in v0.3.0

type MatrixLayers interface {
	Initialize(w *ecs.World)         // Initialize the observer. No other methods are called before this.
	Update(w *ecs.World)             // Update the observer.
	Layers() int                     // Number of layers.
	Dims() (int, int)                // Matrix dimensions.
	Values(w *ecs.World) [][]float64 // Values for the current model tick, in row-major order (i.e. idx = row*ncols + col). First index is the layer.
}

MatrixLayers observer interface. Provides dimensionality, and multiple matrices of values per call.

See also Matrix and GridLayers. See package github.com/mlange-42/arche-model/reporter for usage examples.

func MatrixToLayers added in v0.3.0

func MatrixToLayers(obs ...Matrix) MatrixLayers

MatrixToLayers creates an observer that serves as adapter from multiple Matrix observers to a MatrixLayers observer.

Example
// Multiple Matrix observers
var matrix1 observer.Matrix = &MatrixObserver{}
var matrix2 observer.Matrix = &MatrixObserver{}
var matrix3 observer.Matrix = &MatrixObserver{}

// A MatrixToGrid observer, wrapping the Matrix observers
var _ observer.MatrixLayers = observer.MatrixToLayers(matrix1, matrix2, matrix3)
Output:

type Row

type Row interface {
	Initialize(w *ecs.World)       // Initialize the observer. No other methods are called before this.
	Update(w *ecs.World)           // Update the observer.
	Header() []string              // Header/column names in the same order as data values.
	Values(w *ecs.World) []float64 // Values for the current model tick.
}

Row observer interface. Provides column headers, and a single data row per call.

See also Table. See package github.com/mlange-42/arche-model/reporter for usage examples.

Example
package main

import (
	"github.com/mlange-42/arche-model/observer"
	"github.com/mlange-42/arche/ecs"
)

// Example observer, reporting the number of entities.
type RowObserver struct{}

func (o *RowObserver) Initialize(w *ecs.World) {}

func (o *RowObserver) Update(w *ecs.World) {}

func (o *RowObserver) Header() []string {
	return []string{"TotalEntities"}
}

func (o *RowObserver) Values(w *ecs.World) []float64 {
	query := w.Query(ecs.All())
	cnt := query.Count()
	query.Close()

	return []float64{float64(cnt)}
}

func main() {
	var _ observer.Row = &RowObserver{}
}
Output:

type Table

type Table interface {
	Initialize(w *ecs.World)         // Initialize the observer. No other methods are called before this.
	Update(w *ecs.World)             // Update the observer.
	Header() []string                // Header/column names in the same order as data values.
	Values(w *ecs.World) [][]float64 // Values for the current model tick.
}

Table observer interface. Provides column headers, and multiple data rows per call.

See also Row. See package github.com/mlange-42/arche-model/reporter for usage examples.

Example
package main

import (
	"github.com/mlange-42/arche-model/observer"
	"github.com/mlange-42/arche/ecs"
)

// Example observer, reporting a nonsense table.
type TableObserver struct{}

func (o *TableObserver) Initialize(w *ecs.World) {}

func (o *TableObserver) Update(w *ecs.World) {}

func (o *TableObserver) Header() []string {
	return []string{"TotalEntities"}
}

func (o *TableObserver) Values(w *ecs.World) [][]float64 {
	return [][]float64{
		{1, 2, 3},
		{1, 2, 3},
		{1, 2, 3},
		{1, 2, 3},
	}
}

func main() {
	var _ observer.Table = &TableObserver{}
}
Output:

func RowToTable added in v0.3.0

func RowToTable(row Row) Table

RowToTable creates an observer that serves as adapter from a Row observer to a Table observer.

Example
// A Row observer
var row observer.Row = &RowObserver{}

// A RowToTable observer, wrapping the Row observer
var _ observer.Table = observer.RowToTable(row)
Output:

Jump to

Keyboard shortcuts

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