Skip to content

Commit

Permalink
Add readme example
Browse files Browse the repository at this point in the history
  • Loading branch information
hovsep committed Nov 10, 2024
1 parent 7963777 commit 8814563
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions examples/strings_processing/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"fmt"
"github.com/hovsep/fmesh"
"github.com/hovsep/fmesh/component"
"github.com/hovsep/fmesh/port"
"github.com/hovsep/fmesh/signal"
"os"
"strings"
)

// This example is used in readme.md
func main() {
fm := fmesh.New("hello world").
WithComponents(
component.New("concat").
WithInputs("i1", "i2").
WithOutputs("res").
WithActivationFunc(func(inputs *port.Collection, outputs *port.Collection) error {
word1 := inputs.ByName("i1").FirstSignalPayloadOrDefault("").(string)
word2 := inputs.ByName("i2").FirstSignalPayloadOrDefault("").(string)

outputs.ByName("res").PutSignals(signal.New(word1 + word2))
return nil
}),
component.New("case").
WithInputs("i1").
WithOutputs("res").
WithActivationFunc(func(inputs *port.Collection, outputs *port.Collection) error {
inputString := inputs.ByName("i1").FirstSignalPayloadOrDefault("").(string)

outputs.ByName("res").PutSignals(signal.New(strings.ToTitle(inputString)))
return nil
})).
WithConfig(fmesh.Config{
ErrorHandlingStrategy: fmesh.StopOnFirstErrorOrPanic,
CyclesLimit: 10,
})

fm.Components().ByName("concat").Outputs().ByName("res").PipeTo(
fm.Components().ByName("case").Inputs().ByName("i1"),
)

// Init inputs
fm.Components().ByName("concat").InputByName("i1").PutSignals(signal.New("hello "))
fm.Components().ByName("concat").InputByName("i2").PutSignals(signal.New("world !"))

// Run the mesh
_, err := fm.Run()

// Check for errors
if err != nil {
fmt.Println("F-Mesh returned an error")
os.Exit(1)
}

//Extract results
results := fm.Components().ByName("case").OutputByName("res").FirstSignalPayloadOrNil()
fmt.Printf("Result is : %v", results)
}

0 comments on commit 8814563

Please sign in to comment.