go-hep.org/x/hep@v0.38.1/fwk/internal/fwktest/task4.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 fwktest
     6  
     7  import (
     8  	"reflect"
     9  
    10  	"go-hep.org/x/hep/fwk"
    11  )
    12  
    13  // task4 is like task2, except it works on float64s
    14  type task4 struct {
    15  	fwk.TaskBase
    16  
    17  	input  string
    18  	output string
    19  	fct    func(f float64) float64
    20  }
    21  
    22  func (tsk *task4) Configure(ctx fwk.Context) error {
    23  	var err error
    24  	msg := ctx.Msg()
    25  	msg.Infof("configure...\n")
    26  
    27  	err = tsk.DeclInPort(tsk.input, reflect.TypeOf(float64(1)))
    28  	if err != nil {
    29  		return err
    30  	}
    31  
    32  	err = tsk.DeclOutPort(tsk.output, reflect.TypeOf(float64(1)))
    33  	if err != nil {
    34  		return err
    35  	}
    36  
    37  	msg.Infof("configure... [done]\n")
    38  	return err
    39  }
    40  
    41  func (tsk *task4) StartTask(ctx fwk.Context) error {
    42  	msg := ctx.Msg()
    43  	msg.Infof("start...\n")
    44  	return nil
    45  }
    46  
    47  func (tsk *task4) StopTask(ctx fwk.Context) error {
    48  	msg := ctx.Msg()
    49  	msg.Infof("stop...\n")
    50  	return nil
    51  }
    52  
    53  func (tsk *task4) Process(ctx fwk.Context) error {
    54  	store := ctx.Store()
    55  	msg := ctx.Msg()
    56  	v, err := store.Get(tsk.input)
    57  	if err != nil {
    58  		return err
    59  	}
    60  	i := v.(float64)
    61  	o := tsk.fct(i)
    62  	err = store.Put(tsk.output, o)
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	msg.Infof("proc... (id=%d|%d) => [%d -> %d]\n", ctx.ID(), ctx.Slot(), i, o)
    68  	return nil
    69  }
    70  
    71  func init() {
    72  	fwk.Register(reflect.TypeOf(task4{}),
    73  		func(typ, name string, mgr fwk.App) (fwk.Component, error) {
    74  			var err error
    75  			tsk := &task4{
    76  				TaskBase: fwk.NewTask(typ, name, mgr),
    77  				input:    "floats1",
    78  				output:   "massaged_floats1",
    79  			}
    80  			tsk.fct = func(f float64) float64 {
    81  				return f * f
    82  			}
    83  
    84  			err = tsk.DeclProp("Input", &tsk.input)
    85  			if err != nil {
    86  				return nil, err
    87  			}
    88  
    89  			err = tsk.DeclProp("Output", &tsk.output)
    90  			if err != nil {
    91  				return nil, err
    92  			}
    93  
    94  			err = tsk.DeclProp("Fct", &tsk.fct)
    95  			if err != nil {
    96  				return nil, err
    97  			}
    98  
    99  			return tsk, err
   100  		},
   101  	)
   102  }