Skip to content

Commit

Permalink
Merge pull request #50 from hovsep/v.0.1.0
Browse files Browse the repository at this point in the history
Various refactorings
  • Loading branch information
hovsep authored Sep 23, 2024
2 parents 1edb7e9 + f7b2a01 commit 74e226e
Show file tree
Hide file tree
Showing 17 changed files with 488 additions and 519 deletions.
12 changes: 6 additions & 6 deletions component/activation_result_collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func TestActivationResultCollection_Add(t *testing.T) {
collection: NewActivationResultCollection(),
args: args{
activationResults: []*ActivationResult{
NewComponent("c1").newActivationResultOK(),
NewComponent("c2").newActivationResultReturnedError(errors.New("oops")),
New("c1").newActivationResultOK(),
New("c2").newActivationResultReturnedError(errors.New("oops")),
},
},
assertions: func(t *testing.T, collection ActivationResultCollection) {
Expand All @@ -48,13 +48,13 @@ func TestActivationResultCollection_Add(t *testing.T) {
{
name: "adding to existing collection",
collection: NewActivationResultCollection().Add(
NewComponent("c1").newActivationResultOK(),
NewComponent("c2").newActivationResultOK(),
New("c1").newActivationResultOK(),
New("c2").newActivationResultOK(),
),
args: args{
activationResults: []*ActivationResult{
NewComponent("c4").newActivationResultNoInput(),
NewComponent("c5").newActivationResultPanicked(errors.New("panic")),
New("c4").newActivationResultNoInput(),
New("c5").newActivationResultPanicked(errors.New("panic")),
},
},
assertions: func(t *testing.T, collection ActivationResultCollection) {
Expand Down
10 changes: 5 additions & 5 deletions component/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestCollection_ByName(t *testing.T) {
}{
{
name: "component found",
components: NewComponentCollection().Add(NewComponent("c1"), NewComponent("c2")),
components: NewComponentCollection().Add(New("c1"), New("c2")),
args: args{
name: "c2",
},
Expand All @@ -32,7 +32,7 @@ func TestCollection_ByName(t *testing.T) {
},
{
name: "component not found",
components: NewComponentCollection().Add(NewComponent("c1"), NewComponent("c2")),
components: NewComponentCollection().Add(New("c1"), New("c2")),
args: args{
name: "c3",
},
Expand Down Expand Up @@ -70,7 +70,7 @@ func TestCollection_Add(t *testing.T) {
name: "adding to empty collection",
collection: NewComponentCollection(),
args: args{
components: []*Component{NewComponent("c1"), NewComponent("c2")},
components: []*Component{New("c1"), New("c2")},
},
assertions: func(t *testing.T, collection Collection) {
assert.Len(t, collection, 2)
Expand All @@ -81,9 +81,9 @@ func TestCollection_Add(t *testing.T) {
},
{
name: "adding to existing collection",
collection: NewComponentCollection().Add(NewComponent("c1"), NewComponent("c2")),
collection: NewComponentCollection().Add(New("c1"), New("c2")),
args: args{
components: []*Component{NewComponent("c3"), NewComponent("c4")},
components: []*Component{New("c3"), New("c4")},
},
assertions: func(t *testing.T, collection Collection) {
assert.Len(t, collection, 4)
Expand Down
33 changes: 15 additions & 18 deletions component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ import (
"github.com/hovsep/fmesh/port"
)

// @TODO add getter\setter and constructor
type StateSnapshot struct {
InputPorts port.MetadataMap
OutputPorts port.MetadataMap
}

type ActivationFunc func(inputs port.Collection, outputs port.Collection) error

// Component defines a main building block of FMesh
Expand All @@ -23,8 +17,8 @@ type Component struct {
f ActivationFunc
}

// NewComponent creates a new empty component
func NewComponent(name string) *Component {
// New creates initialized component
func New(name string) *Component {
return &Component{
name: name,
inputs: port.NewCollection(),
Expand Down Expand Up @@ -93,13 +87,6 @@ func (c *Component) hasActivationFunction() bool {
return c.f != nil
}

func (c *Component) getStateSnapshot() *StateSnapshot {
return &StateSnapshot{
InputPorts: c.Inputs().GetPortsMetadata(),
OutputPorts: c.Outputs().GetPortsMetadata(),
}
}

// MaybeActivate tries to run the activation function if all required conditions are met
// @TODO: hide this method from user
func (c *Component) MaybeActivate() (activationResult *ActivationResult) {
Expand Down Expand Up @@ -158,12 +145,22 @@ func (c *Component) MaybeActivate() (activationResult *ActivationResult) {

// FlushInputs ...
// @TODO: hide this method from user
func (c *Component) FlushInputs() {
c.Inputs().Flush(false)
func (c *Component) FlushInputs(activationResult *ActivationResult, keepInputSignals bool) {
c.Inputs().Flush()
if !keepInputSignals {
// Inputs can not be just cleared, instead we remove signals which
// have been used (been set on inputs) during the last activation cycle
// thus not affecting ones the component could have been received from i2i pipes
for portName, p := range c.Inputs() {
p.DisposeSignals(activationResult.StateBefore().InputPortsMetadata()[portName].SignalBufferLen)
}
}
}

// FlushOutputs ...
// @TODO: hide this method from user
func (c *Component) FlushOutputs(activationResult *ActivationResult) {
c.Outputs().FlushProcessedSignals(activationResult.StateAfter().OutputPorts)
for portName, p := range c.Outputs() {
p.FlushAndDispose(activationResult.StateAfter().OutputPortsMetadata()[portName].SignalBufferLen)
}
}
Loading

0 comments on commit 74e226e

Please sign in to comment.