Skip to content

Commit

Permalink
Added support for graph names and graph reuse via component registry/…
Browse files Browse the repository at this point in the history
…factory, refs trustmaster#7.
  • Loading branch information
trustmaster committed Oct 24, 2013
1 parent 1a4d32e commit 2d96215
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 44 deletions.
99 changes: 55 additions & 44 deletions loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package flow

import (
"encoding/json"
// "fmt"
"io/ioutil"
"reflect"
"strings"
)

// Internal representation of NoFlo JSON format
type graphDescription struct {
Properties struct {
Name string
}
Processes map[string]struct {
Component string
Sync bool `json:",omitempty"`
Expand Down Expand Up @@ -43,57 +45,66 @@ func ParseJSON(js []byte) *Graph {
}
// fmt.Printf("%+v\n", descr)

// Create a new Graph
net := new(Graph)
net.InitGraphState()
constructor := func() interface{} {
// 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 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 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")
// 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
}
// TODO add support for subgraphs

return net
}

// Register a component to be reused
if descr.Properties.Name != "" {
Register(descr.Properties.Name, constructor)
}

return net
return constructor().(*Graph)
}

// LoadJSON loads a JSON graph definition file into
Expand Down
3 changes: 3 additions & 0 deletions runtime_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ func (s *summarizer) Finish() {
}

var runtimeNetworkJSON = `{
"properties": {
"name": "runtimeNetwork"
},
"processes": {
"starter": {
"component": "starter"
Expand Down

0 comments on commit 2d96215

Please sign in to comment.