Skip to content

Commit

Permalink
Merge pull request #53 from hovsep/v.0.1.0
Browse files Browse the repository at this point in the history
V.0.1.0
  • Loading branch information
hovsep authored Sep 24, 2024
2 parents b3349b0 + 46bb386 commit 9cb3981
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/qodana_code_quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ jobs:
with:
fetch-depth: 0
- name: 'Qodana Scan'
uses: JetBrains/qodana-action@v2024.3
uses: JetBrains/qodana-action@v2024.2
env:
QODANA_TOKEN: ${{ secrets.QODANA_TOKEN }}
12 changes: 6 additions & 6 deletions component/activation_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ type ActivationResult struct {
type ActivationResultCode int

const (
// ActivationCodeOK ...: component is activated and did not return any errors
// ActivationCodeOK : component is activated and did not return any errors
ActivationCodeOK ActivationResultCode = iota

// ActivationCodeNoInput ...: component is not activated because it has no input set
// ActivationCodeNoInput : component is not activated because it has no input set
ActivationCodeNoInput

// ActivationCodeNoFunction ...: component activation function is not set, so we can not activate it
// ActivationCodeNoFunction : component activation function is not set, so we can not activate it
ActivationCodeNoFunction

// ActivationCodeWaitingForInput ...: component is waiting for more inputs on some ports
// ActivationCodeWaitingForInput : component is waiting for more inputs on some ports
ActivationCodeWaitingForInput

// ActivationCodeReturnedError ...: component is activated, but returned an error
// ActivationCodeReturnedError : component is activated, but returned an error
ActivationCodeReturnedError

// ActivationCodePanicked ...: component is activated, but panicked
// ActivationCodePanicked : component is activated, but panicked
ActivationCodePanicked
)

Expand Down
8 changes: 4 additions & 4 deletions component/collection.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package component

// ComponentCollection is a collection of components with useful methods
// Collection is a collection of components with useful methods
type Collection map[string]*Component

// NewComponentCollection creates empty collection
Expand All @@ -13,9 +13,9 @@ func (collection Collection) ByName(name string) *Component {
return collection[name]
}

// Add adds new components to existing collection
func (collection Collection) Add(newComponents ...*Component) Collection {
for _, component := range newComponents {
// Add adds components to existing collection
func (collection Collection) Add(components ...*Component) Collection {
for _, component := range components {
collection[component.Name()] = component
}
return collection
Expand Down
4 changes: 2 additions & 2 deletions component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (c *Component) MaybeActivate() (activationResult *ActivationResult) {
return
}

// FlushInputs ...
// FlushInputs flushes and clears (when needed) input ports
// @TODO: hide this method from user
func (c *Component) FlushInputs(activationResult *ActivationResult, keepInputSignals bool) {
c.Inputs().Flush()
Expand All @@ -157,7 +157,7 @@ func (c *Component) FlushInputs(activationResult *ActivationResult, keepInputSig
}
}

// FlushOutputs ...
// FlushOutputs flushes output ports and disposes processed signals
// @TODO: hide this method from user
func (c *Component) FlushOutputs(activationResult *ActivationResult) {
for portName, p := range c.Outputs() {
Expand Down
4 changes: 2 additions & 2 deletions component/state_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ func NewStateSnapshot() *StateSnapshot {
}
}

// InputPortsMetadata ... getter
// InputPortsMetadata getter
func (s *StateSnapshot) InputPortsMetadata() port.MetadataMap {
return s.inputPortsMetadata
}

// OutputPortsMetadata ... getter
// OutputPortsMetadata getter
func (s *StateSnapshot) OutputPortsMetadata() port.MetadataMap {
return s.outputPortsMetadata
}
Expand Down
12 changes: 7 additions & 5 deletions cycle/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ func NewCollection() Collection {
return make(Collection, 0)
}

// Add adds cycle results to existing collection
func (collection Collection) Add(newCycleResults ...*Cycle) Collection {
for _, cycleResult := range newCycleResults {
collection = append(collection, cycleResult)
// With adds cycle results to existing collection
func (collection Collection) With(cycleResults ...*Cycle) Collection {
newCollection := make(Collection, len(collection)+len(cycleResults))
copy(newCollection, collection)
for i, cycleResult := range cycleResults {
newCollection[len(collection)+i] = cycleResult
}
return collection
return newCollection
}
2 changes: 1 addition & 1 deletion cycle/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestCollection_Add(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, tt.cycleResults.Add(tt.args.cycleResults...))
assert.Equal(t, tt.want, tt.cycleResults.With(tt.args.cycleResults...))
})
}
}
2 changes: 1 addition & 1 deletion fmesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (fm *FMesh) Run() (cycle.Collection, error) {
allCycles := cycle.NewCollection()
for {
cycleResult := fm.runCycle()
allCycles = allCycles.Add(cycleResult)
allCycles = allCycles.With(cycleResult)

mustStop, err := fm.mustStop(cycleResult, len(allCycles))
if mustStop {
Expand Down
10 changes: 5 additions & 5 deletions fmesh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func TestFMesh_Run(t *testing.T) {
{
name: "empty mesh stops after first cycle",
fm: New("fm"),
want: cycle.NewCollection().Add(cycle.New()),
want: cycle.NewCollection().With(cycle.New()),
wantErr: false,
},
{
Expand All @@ -294,7 +294,7 @@ func TestFMesh_Run(t *testing.T) {
//Fire the mesh
fm.Components().ByName("c1").Inputs().ByName("i1").PutSignals(signal.New("start c1"))
},
want: cycle.NewCollection().Add(
want: cycle.NewCollection().With(
cycle.New().
WithActivationResults(component.NewActivationResult("c1").
SetActivated(true).
Expand All @@ -316,7 +316,7 @@ func TestFMesh_Run(t *testing.T) {
initFM: func(fm *FMesh) {
fm.Components().ByName("c1").Inputs().ByName("i1").PutSignals(signal.New("start"))
},
want: cycle.NewCollection().Add(
want: cycle.NewCollection().With(
cycle.New().
WithActivationResults(
component.NewActivationResult("c1").
Expand Down Expand Up @@ -374,7 +374,7 @@ func TestFMesh_Run(t *testing.T) {
c1.Inputs().ByName("i1").PutSignals(signal.New("start c1"))
c3.Inputs().ByName("i1").PutSignals(signal.New("start c3"))
},
want: cycle.NewCollection().Add(
want: cycle.NewCollection().With(
cycle.New().
WithActivationResults(
component.NewActivationResult("c1").
Expand Down Expand Up @@ -485,7 +485,7 @@ func TestFMesh_Run(t *testing.T) {
c1.Inputs().ByName("i1").PutSignals(signal.New("start c1"))
c3.Inputs().ByName("i1").PutSignals(signal.New("start c3"))
},
want: cycle.NewCollection().Add(
want: cycle.NewCollection().With(
//c1 and c3 activated, c3 finishes with error
cycle.New().
WithActivationResults(
Expand Down
10 changes: 5 additions & 5 deletions integration_tests/piping/piping_from_inputs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func Test_PipingFromInput(t *testing.T) {
WithOutputsIndexed("o", 1, 2).
WithActivationFunc(func(inputs port.Collection, outputs port.Collection) error {
//Activate downstream components
outputs.PutSignals(inputs.AllSignals()[0])
outputs.PutSignals(inputs.ByName("start").Signals().First())
return nil
})

Expand All @@ -97,7 +97,7 @@ func Test_PipingFromInput(t *testing.T) {
WithInputs("i1").
WithOutputs("o1").
WithActivationFunc(func(inputs port.Collection, outputs port.Collection) error {
outputs.PutSignals(signal.New(1 + inputs.AllSignals().FirstPayload().(int)))
outputs.PutSignals(signal.New(1 + inputs.ByName("i1").Signals().FirstPayload().(int)))
return nil
})

Expand All @@ -106,7 +106,7 @@ func Test_PipingFromInput(t *testing.T) {
WithInputs("i1").
WithOutputs("o1").
WithActivationFunc(func(inputs port.Collection, outputs port.Collection) error {
outputs.PutSignals(signal.New(1 + inputs.AllSignals().FirstPayload().(int)))
outputs.PutSignals(signal.New(1 + inputs.ByName("i1").Signals().FirstPayload().(int)))
return nil
})

Expand All @@ -115,7 +115,7 @@ func Test_PipingFromInput(t *testing.T) {
WithInputs("i1").
WithOutputs("o1").
WithActivationFunc(func(inputs port.Collection, outputs port.Collection) error {
outputs.PutSignals(signal.New(2 * inputs.AllSignals().FirstPayload().(int)))
outputs.PutSignals(signal.New(2 * inputs.ByName("i1").Signals().FirstPayload().(int)))
return nil
})

Expand All @@ -138,7 +138,7 @@ func Test_PipingFromInput(t *testing.T) {
WithInputsIndexed("i", 1, 2).
WithOutputs("log").
WithActivationFunc(func(inputs port.Collection, outputs port.Collection) error {
outputs.ByName("log").PutSignals(inputs.AllSignals()...)
outputs.ByName("log").PutSignals(inputs.ByNames("i1", "i2").Signals()...)
return nil
})

Expand Down
3 changes: 2 additions & 1 deletion port/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ func (collection Collection) AddIndexed(prefix string, startIndex int, endIndex
return collection.Add(NewIndexedGroup(prefix, startIndex, endIndex)...)
}

func (collection Collection) AllSignals() signal.Group {
// Signals returns all signals of all ports in the group
func (collection Collection) Signals() signal.Group {
group := signal.NewGroup()
for _, p := range collection {
group = append(group, p.Signals()...)
Expand Down
11 changes: 5 additions & 6 deletions port/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@ func NewIndexedGroup(prefix string, startIndex int, endIndex int) Group {

// With adds ports to group
func (group Group) With(ports ...*Port) Group {
for _, port := range ports {
if port == nil {
continue
}
group = append(group, port)
newGroup := make(Group, len(group)+len(ports))
copy(newGroup, group)
for i, port := range ports {
newGroup[len(group)+i] = port
}

return group
return newGroup
}
28 changes: 14 additions & 14 deletions port/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ func (p *Port) FlushAndDispose(n int) {
p.DisposeSignals(n)
}

// Flush pushes signals to pipes and returns true when flushed
// @TODO: hide this method from user
func (p *Port) Flush() bool {
if !p.HasSignals() || !p.HasPipes() {
return false
}

for _, outboundPort := range p.pipes {
//Fan-Out
ForwardSignals(p, outboundPort)
}
return true
}

// HasSignals says whether port signals is set or not
func (p *Port) HasSignals() bool {
return len(p.Signals()) > 0
Expand All @@ -98,20 +112,6 @@ func (p *Port) PipeTo(toPorts ...*Port) {
}
}

// Flush pushes signals to pipes, clears the port if needed and returns true when flushed
// @TODO: hide this method from user
func (p *Port) Flush() bool {
if !p.HasSignals() || !p.HasPipes() {
return false
}

for _, outboundPort := range p.pipes {
//Fan-Out
ForwardSignals(p, outboundPort)
}
return true
}

// ForwardSignals copies all signals from source port to destination port, without clearing the source port
func ForwardSignals(source *Port, dest *Port) {
dest.PutSignals(source.Signals()...)
Expand Down
37 changes: 22 additions & 15 deletions signal/group.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package signal

// Group represents a list of signals
type Group []*Signal

// NewGroup creates empty group
Expand All @@ -11,39 +12,45 @@ func NewGroup(payloads ...any) Group {
return group
}

// First returns the first signal in the group
func (group Group) First() *Signal {
return group[0]
}

// FirstPayload returns the first signal payload
func (group Group) FirstPayload() any {
//Intentionally not checking the group len
//as the method does not have returning error (api is simpler)
//and we can not just return nil, as nil may be a valid payload.
//Just letting the runtime panic
return group[0].Payload()
return group.First().Payload()
}

// AllPayloads returns a slice with all payloads of the all signals in the group
func (group Group) AllPayloads() []any {
all := make([]any, 0, len(group))
for _, s := range group {
all = append(all, s.Payload())
all := make([]any, len(group), len(group))
for i, sig := range group {
all[i] = sig.Payload()
}
return all
}

// With adds signals to group
// With returns the group with added signals
func (group Group) With(signals ...*Signal) Group {
for _, sig := range signals {
newGroup := make(Group, len(group)+len(signals))
copy(newGroup, group)
for i, sig := range signals {
if sig == nil {
continue
}
group = append(group, sig)
newGroup[len(group)+i] = sig
}

return group
return newGroup
}

// WithPayloads returns a group with added signals created from provided payloads
func (group Group) WithPayloads(payloads ...any) Group {
for _, p := range payloads {
group = append(group, New(p))
newGroup := make(Group, len(group)+len(payloads))
copy(newGroup, group)
for i, p := range payloads {
newGroup[len(group)+i] = New(p)
}
return group
return newGroup
}

0 comments on commit 9cb3981

Please sign in to comment.