go-hep.org/x/hep@v0.38.1/fwk/core.go (about)

     1  // Copyright ©2017 The go-hep Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package fwk
     6  
     7  import (
     8  	"fmt"
     9  	"reflect"
    10  
    11  	"go-hep.org/x/hep/fwk/fsm"
    12  )
    13  
    14  // Context is the interface to access context-local data.
    15  type Context interface {
    16  	ID() int64      // id of this context (e.g. entry number or some kind of event number)
    17  	Slot() int      // slot number in the pool of event sequences
    18  	Store() Store   // data store corresponding to the id+slot
    19  	Msg() MsgStream // messaging for this context (id+slot)
    20  
    21  	Svc(n string) (Svc, error) // retrieve an already existing Svc by name
    22  }
    23  
    24  // Component is the interface satisfied by all values in fwk.
    25  //
    26  // A component can be asked for:
    27  // its Type() (ex: "go-hep.org/x/hep/fads.MomentumSmearing")
    28  // its Name() (ex: "MyPropagator")
    29  type Component interface {
    30  	Type() string // Type of the component (ex: "go-hep.org/x/hep/fads.MomentumSmearing")
    31  	Name() string // Name of the component (ex: "MyPropagator")
    32  }
    33  
    34  // ComponentMgr manages components.
    35  // ComponentMgr creates and provides access to all the components in a fwk App.
    36  type ComponentMgr interface {
    37  	Component(n string) Component
    38  	HasComponent(n string) bool
    39  	Components() []Component
    40  	New(t, n string) (Component, error)
    41  }
    42  
    43  // Task is a component processing event-level data.
    44  // Task.Process is called for every component and for every input event.
    45  type Task interface {
    46  	Component
    47  
    48  	StartTask(ctx Context) error
    49  	Process(ctx Context) error
    50  	StopTask(ctx Context) error
    51  }
    52  
    53  // TaskMgr manages tasks.
    54  type TaskMgr interface {
    55  	AddTask(tsk Task) error
    56  	DelTask(tsk Task) error
    57  	HasTask(n string) bool
    58  	GetTask(n string) Task
    59  	Tasks() []Task
    60  }
    61  
    62  // Configurer are components which can be configured via properties
    63  // declared or created by the job-options.
    64  type Configurer interface {
    65  	Component
    66  	Configure(ctx Context) error
    67  }
    68  
    69  // Svc is a component providing services or helper features.
    70  // Services are started before the main event loop processing and
    71  // stopped just after.
    72  type Svc interface {
    73  	Component
    74  
    75  	StartSvc(ctx Context) error
    76  	StopSvc(ctx Context) error
    77  }
    78  
    79  // SvcMgr manages services.
    80  type SvcMgr interface {
    81  	AddSvc(svc Svc) error
    82  	DelSvc(svc Svc) error
    83  	HasSvc(n string) bool
    84  	GetSvc(n string) Svc
    85  	Svcs() []Svc
    86  }
    87  
    88  // App is the component orchestrating all the other components
    89  // in a coherent application to process physics events.
    90  type App interface {
    91  	Component
    92  	ComponentMgr
    93  	SvcMgr
    94  	TaskMgr
    95  	PropMgr
    96  	PortMgr
    97  
    98  	FSMStater
    99  
   100  	Runner
   101  	Scripter() Scripter
   102  
   103  	Msg() MsgStream
   104  }
   105  
   106  // Runner runs a fwk App in a batch fashion:
   107  //   - Configure
   108  //   - Start
   109  //   - Run event loop
   110  //   - Stop
   111  //   - Shutdown
   112  type Runner interface {
   113  	Run() error
   114  }
   115  
   116  // Scripter gives finer control to running a fwk App
   117  type Scripter interface {
   118  	Configure() error
   119  	Start() error
   120  	Run(evtmax int64) error
   121  	Stop() error
   122  	Shutdown() error
   123  }
   124  
   125  // PropMgr manages properties attached to components.
   126  type PropMgr interface {
   127  	DeclProp(c Component, name string, ptr any) error
   128  	SetProp(c Component, name string, value any) error
   129  	GetProp(c Component, name string) (any, error)
   130  	HasProp(c Component, name string) bool
   131  }
   132  
   133  // Property is a pair key/value, associated to a component.
   134  // Properties of a given component can be modified
   135  // by a job-option or by other components.
   136  type Property interface {
   137  	DeclProp(name string, ptr any) error
   138  	SetProp(name string, value any) error
   139  	GetProp(name string) (any, error)
   140  }
   141  
   142  // Store provides access to a concurrent-safe map[string]any store.
   143  type Store interface {
   144  	Get(key string) (any, error)
   145  	Put(key string, value any) error
   146  	Has(key string) bool
   147  }
   148  
   149  // Port holds the name and type of a data item in a store
   150  type Port struct {
   151  	Name string
   152  	Type reflect.Type
   153  }
   154  
   155  // DeclPorter is the interface to declare input/output ports for the data flow.
   156  type DeclPorter interface {
   157  	DeclInPort(name string, t reflect.Type) error
   158  	DeclOutPort(name string, t reflect.Type) error
   159  }
   160  
   161  // PortMgr is the interface to manage input/output ports for the data flow
   162  type PortMgr interface {
   163  	DeclInPort(c Component, name string, t reflect.Type) error
   164  	DeclOutPort(c Component, name string, t reflect.Type) error
   165  }
   166  
   167  // FSMStater is the interface used to query the current state of the fwk application
   168  type FSMStater interface {
   169  	FSMState() fsm.State
   170  }
   171  
   172  // Level regulates the verbosity level of a component.
   173  type Level int
   174  
   175  // Default verbosity levels.
   176  const (
   177  	LvlDebug   Level = -10 // LvlDebug defines the DBG verbosity level
   178  	LvlInfo    Level = 0   // LvlInfo defines the INFO verbosity level
   179  	LvlWarning Level = 10  // LvlWarning defines the WARN verbosity level
   180  	LvlError   Level = 20  // LvlError defines the ERR verbosity level
   181  )
   182  
   183  func (lvl Level) msgstring() string {
   184  	switch lvl {
   185  	case LvlDebug:
   186  		return "DBG "
   187  	case LvlInfo:
   188  		return "INFO"
   189  	case LvlWarning:
   190  		return "WARN"
   191  	case LvlError:
   192  		return "ERR "
   193  	}
   194  	panic(fmt.Errorf("fwk.Level: invalid fwk.Level value [%d]", int(lvl)))
   195  }
   196  
   197  // String prints the human-readable representation of a Level value.
   198  func (lvl Level) String() string {
   199  	switch lvl {
   200  	case LvlDebug:
   201  		return "DEBUG"
   202  	case LvlInfo:
   203  		return "INFO"
   204  	case LvlWarning:
   205  		return "WARN"
   206  	case LvlError:
   207  		return "ERROR"
   208  	}
   209  	panic(fmt.Errorf("fwk.Level: invalid fwk.Level value [%d]", int(lvl)))
   210  }
   211  
   212  // MsgStream provides access to verbosity-defined formated messages, a la fmt.Printf.
   213  type MsgStream interface {
   214  	Debugf(format string, a ...any)
   215  	Infof(format string, a ...any)
   216  	Warnf(format string, a ...any)
   217  	Errorf(format string, a ...any)
   218  
   219  	Msg(lvl Level, format string, a ...any)
   220  }
   221  
   222  // Deleter prepares values to be GC-reclaimed
   223  type Deleter interface {
   224  	Delete() error
   225  }