From a144c8fed9799e7a4ab5e789d5ba3e9c450960a7 Mon Sep 17 00:00:00 2001 From: hovsep Date: Sun, 10 Nov 2024 02:23:34 +0200 Subject: [PATCH] Add readme example --- README.md | 97 +++++++++++++---------------- examples/strings_processing/main.go | 61 ++++++++++++++++++ 2 files changed, 106 insertions(+), 52 deletions(-) create mode 100644 examples/strings_processing/main.go diff --git a/README.md b/README.md index 2dfd03b..217ccf3 100644 --- a/README.md +++ b/README.md @@ -6,78 +6,71 @@

What is it?

-

F-Mesh is a simplistic FBP-inspired framework in Go. -It allows you to express your program as a mesh of interconnected components (or more formally as computational graph). -You can think of it as a simple functions orchestrator. +

F-Mesh is a functions orchestrator inspired by FBP. +It allows you to express your program as a mesh of interconnected components (or more formally as a computational graph).

Main concepts:

What it is not?

-

F-mesh is not a classical FBP implementation, and it is not fully async. It does not support long-running components or wall-time events (like timers and tickers)

+

F-mesh is not a classical FBP implementation, it does not support long-running components or wall-time events (like timers and tickers)

Example:

```go 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) + 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(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, - }) + 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"), - ) + fm.Components().ByName("concat").Outputs().ByName("res").PipeTo( + fm.Components().ByName("case").Inputs().ByName("i1"), + ) - // Init inputs - fm.Components().ByName("concat").Inputs().ByName("i1").PutSignals(signal.New("hello ")) - fm.Components().ByName("concat").Inputs().ByName("i2").PutSignals(signal.New("world !")) + // 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() + // Run the mesh + _, err := fm.Run() - // Check for errors - if err != nil { - fmt.Println("F-Mesh returned an error") - os.Exit(1) - } + // Check for errors + if err != nil { + fmt.Println("F-Mesh returned an error") + os.Exit(1) + } - //Extract results - results := fm.Components().ByName("case").Outputs().ByName("res").FirstSignalPayloadOrNil() - fmt.Printf("Result is :%v", results) + //Extract results + results := fm.Components().ByName("case").OutputByName("res").FirstSignalPayloadOrNil() + fmt.Printf("Result is : %v", results) ``` - +See more in ```examples``` directory.

Version 0.1.0 coming soon

\ No newline at end of file diff --git a/examples/strings_processing/main.go b/examples/strings_processing/main.go new file mode 100644 index 0000000..539a112 --- /dev/null +++ b/examples/strings_processing/main.go @@ -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) +}