forked from trustmaster/goflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented IIP support trustmaster#15 and JSON graph format support t…
- Loading branch information
1 parent
f2e2835
commit 2428e5d
Showing
7 changed files
with
563 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package flow | ||
|
||
import ( | ||
"encoding/json" | ||
// "fmt" | ||
"io/ioutil" | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
// Internal representation of NoFlo JSON format | ||
type graphDescription struct { | ||
Processes map[string]struct { | ||
Component string | ||
Sync bool `json:",omitempty"` | ||
} | ||
Connections []struct { | ||
Data interface{} `json:",omitempty"` | ||
Src struct { | ||
Process string | ||
Port string | ||
} `json:",omitempty"` | ||
Tgt struct { | ||
Process string | ||
Port string | ||
} | ||
Buffer int `json:",omitempty"` | ||
} | ||
Exports []struct { | ||
Private string | ||
Public string | ||
} | ||
} | ||
|
||
// ParseJSON converts a JSON network definition string into | ||
// a flow.Graph object that can be run or used in other networks | ||
func ParseJSON(js []byte) *Graph { | ||
// Parse JSON into Go struct | ||
var descr graphDescription | ||
err := json.Unmarshal(js, &descr) | ||
if err != nil { | ||
return nil | ||
} | ||
// fmt.Printf("%+v\n", descr) | ||
|
||
// Create a new Graph | ||
net := new(Graph) | ||
net.InitGraphState() | ||
|
||
// Add processes to the network | ||
for procName, procValue := range descr.Processes { | ||
net.AddNew(procValue.Component, procName) | ||
// Support for Sync/Async process switch | ||
if procValue.Sync { | ||
proc := net.Get(procName).(*Component) | ||
proc.Mode = ComponentModeSync | ||
} | ||
} | ||
|
||
// Add connections | ||
for _, conn := range descr.Connections { | ||
// Check if it is an IIP or actual connection | ||
if conn.Data == nil { | ||
// Add a connection | ||
net.ConnectBuf(conn.Src.Process, conn.Src.Port, conn.Tgt.Process, conn.Tgt.Port, conn.Buffer) | ||
} else { | ||
// Add an IIP | ||
net.AddIIP(conn.Data, conn.Tgt.Process, conn.Tgt.Port) | ||
} | ||
} | ||
|
||
// Add port exports | ||
for _, export := range descr.Exports { | ||
// Split private into proc.port | ||
procName := export.Private[:strings.Index(export.Private, ".")] | ||
procPort := export.Private[strings.Index(export.Private, ".")+1:] | ||
// Try to detect port direction using reflection | ||
procType := reflect.TypeOf(net.Get(procName)).Elem() | ||
field, fieldFound := procType.FieldByName(procPort) | ||
if !fieldFound { | ||
panic("Private port '" + export.Private + "' not found") | ||
} | ||
if field.Type.Kind() == reflect.Chan && (field.Type.ChanDir()&reflect.RecvDir) != 0 { | ||
// It's an inport | ||
net.MapInPort(export.Public, procName, procPort) | ||
} else if field.Type.Kind() == reflect.Chan && (field.Type.ChanDir()&reflect.SendDir) != 0 { | ||
// It's an outport | ||
net.MapOutPort(export.Public, procName, procPort) | ||
} else { | ||
// It's not a proper port | ||
panic("Private port '" + export.Private + "' is not a valid channel") | ||
} | ||
// TODO add support for subgraphs | ||
} | ||
|
||
return net | ||
} | ||
|
||
// LoadJSON loads a JSON graph definition file into | ||
// a flow.Graph object that can be run or used in other networks | ||
func LoadJSON(filename string) *Graph { | ||
js, err := ioutil.ReadFile(filename) | ||
if err != nil { | ||
return nil | ||
} | ||
return ParseJSON(js) | ||
} |
Oops, something went wrong.