graph

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2017 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package graph manages programs stored as graphs.

Index

Constants

This section is empty.

Variables

View Source
var PartFactories = map[string]PartFactory{
	"Aggregator":     func() Part { return new(parts.Aggregator) },
	"Broadcast":      func() Part { return new(parts.Broadcast) },
	"Code":           func() Part { return new(parts.Code) },
	"Filter":         func() Part { return new(parts.Filter) },
	"HTTPServer":     func() Part { return new(parts.HTTPServer) },
	"StaticSend":     func() Part { return new(parts.StaticSend) },
	"TextFileReader": func() Part { return new(parts.TextFileReader) },
	"Unslicer":       func() Part { return new(parts.Unslicer) },
}

PartFactories translates part type strings into part factories.

Functions

This section is empty.

Types

type Channel

type Channel struct {
	Name string `json:"name"`
	Type string `json:"type"`
	Cap  int    `json:"cap"`
	// contains filtered or unexported fields
}

Channel models a channel. It can be marshalled and unmarshalled to JSON sensibly.

func (*Channel) IsSimple

func (c *Channel) IsSimple() bool

IsSimple returns true if its in-degree and out-degree are both 1. This causes the channel to appear as a single arrow instead of an intermediate node. Only correct after calling ComputeDegrees.

func (*Channel) Readers

func (c *Channel) Readers() []*Node

Readers returns nodes that read from this channel. Only correct after calling ComputeDegrees.

func (*Channel) Writers

func (c *Channel) Writers() []*Node

Writers returns nodes that write to or close this channel. Only correct after calling ComputeDegrees.

type Graph

type Graph struct {
	SourcePath  string              `json:"-"` // path to the JSON source.
	Name        string              `json:"name"`
	PackagePath string              `json:"package_path"`
	Imports     []string            `json:"imports"`
	IsCommand   bool                `json:"is_command"`
	Nodes       map[string]*Node    `json:"nodes"`
	Channels    map[string]*Channel `json:"channels"`
}

Graph describes a Go program as a graph. It can be marshalled and unmarshalled to JSON sensibly.

func LoadJSON

func LoadJSON(r io.Reader, sourcePath string) (*Graph, error)

LoadJSON loads a JSON-encoded Graph from an io.Reader.

func LoadJSONFile

func LoadJSONFile(path string) (*Graph, error)

LoadJSONFile loads a JSON-encoded Graph from a file at a given path.

func New

func New(srcPath string) *Graph

New returns a new empty graph associated with a file path.

func (*Graph) AllImports

func (g *Graph) AllImports() []string

AllImports combines all desired imports into one slice. It doesn't fix conflicting names, but dedupes any whole lines. TODO: Put nodes in separate files to solve all import issues.

func (*Graph) Build

func (g *Graph) Build() error

Build saves the graph as Go source code and tries to "go build" it.

func (*Graph) DeclaredChannels

func (g *Graph) DeclaredChannels(chans []string) []*Channel

DeclaredChannels returns the given channels which exist in g.Channels.

func (*Graph) Definitions

func (g *Graph) Definitions() string

Definitions returns the imports and channel var blocks from the Go program. This is useful for advanced parsing and typechecking.

func (*Graph) GeneratePackage

func (g *Graph) GeneratePackage() (string, error)

GeneratePackage writes the Go view of the graph to a file called generated.go in ${GOPATH}/src/${g.PackagePath}/, returning the full path.

func (*Graph) Install

func (g *Graph) Install() error

Install saves the graph as Go source code and tries to "go install" it.

func (*Graph) PackageName

func (g *Graph) PackageName() string

PackageName extracts the name of the package from the package path ("full" package name).

func (*Graph) RecomputeDegrees

func (g *Graph) RecomputeDegrees()

RecomputeDegrees analyses how nodes and channels are related.

func (*Graph) RecomputeNode

func (g *Graph) RecomputeNode(n *Node) error

RecomputeNode gets the node to figure out what channels it uses.

func (*Graph) Run

func (g *Graph) Run(stdout, stderr io.Writer) error

Run saves the graph as Go source code, creates a temporary runner, and tries to run it. The stdout and stderr pipes are copied to the given io.Writers.

func (*Graph) SaveJSONFile

func (g *Graph) SaveJSONFile() error

SaveJSONFile saves the JSON-encoded Graph to the SourcePath.

func (*Graph) WriteDotTo

func (g *Graph) WriteDotTo(dst io.Writer) error

WriteDotTo writes the Dot language view of the graph to the io.Writer.

func (*Graph) WriteGoTo

func (g *Graph) WriteGoTo(w io.Writer) error

WriteGoTo writes the Go language view of the graph to the io.Writer.

func (*Graph) WriteJSONTo

func (g *Graph) WriteJSONTo(w io.Writer) error

WriteJSONTo writes nicely-formatted JSON to the given Writer.

func (*Graph) WriteRawGoTo

func (g *Graph) WriteRawGoTo(w io.Writer) error

WriteRawGoTo writes the Go language view of the graph to the io.Writer, without formatting.

type Node

type Node struct {
	Part

	Name         string
	Multiplicity uint
	Wait         bool
	// contains filtered or unexported fields
}

Node models a goroutine. It can be marshalled and unmarshalled to JSON sensibly.

func (*Node) Channels

func (n *Node) Channels() (read, written source.StringSet)

Channels returns all the channels this node uses.

func (*Node) ChannelsRead

func (n *Node) ChannelsRead() []string

ChannelsRead returns the channels read from by this node. It is a convenience function for the templates, which can't do multiple returns.

func (*Node) ChannelsWritten

func (n *Node) ChannelsWritten() []string

ChannelsWritten returns the channels written to by this node. It is a convenience function for the templates, which can't do multiple returns.

func (*Node) Copy

func (n *Node) Copy() *Node

Copy returns a copy of this node, but with an empty name and a clone of the Part.

func (*Node) ImplBody

func (n *Node) ImplBody() string

ImplBody returns the Body part of the implementation.

func (*Node) ImplHead

func (n *Node) ImplHead() string

ImplHead returns the Head part of the implementation.

func (*Node) ImplTail

func (n *Node) ImplTail() string

ImplTail returns the Tail part of the implementation.

func (*Node) MarshalJSON

func (n *Node) MarshalJSON() ([]byte, error)

MarshalJSON encodes the node and part as JSON.

func (*Node) RenameChannel

func (n *Node) RenameChannel(from, to string)

RenameChannel renames any uses of channel "from" to channel "to".

func (*Node) String

func (n *Node) String() string

func (*Node) UnmarshalJSON

func (n *Node) UnmarshalJSON(j []byte) error

UnmarshalJSON decodes the node and part as JSON.

type Part

type Part interface {
	// AssociateEditor associates a template called "part_view" with the given template.
	AssociateEditor(*template.Template) error

	// Channels returns any additional channels the part thinks it uses.
	// This should be unnecessary.
	Channels() (read, written source.StringSet)

	// Clone returns a copy of this part.
	Clone() interface{}

	// Help returns a helpful description of what this part can do.
	Help() template.HTML

	// Impl returns Go source code implementing the part.
	// The head is executed, then the body is executed (# Multiplicity
	// instances of the body concurrently), then the tail (once the body/bodies
	// are finished).
	//
	// This allows cleanly closing channels for nodes with Multiplicity > 1.
	// The tail is deferred so that the body can use "return" and it is still
	// executed.
	Impl() (head, body, tail string)

	// Imports returns any extra import lines needed for the Part.
	Imports() []string

	// RenameChannel informs the part that a channel has been renamed.
	RenameChannel(from, to string)

	// TypeKey returns the "type" of part.
	TypeKey() string

	// Update sets fields in the part based on info in the given Request.
	Update(*http.Request) error
}

Part abstracts the implementation of a node. Concrete implementations should be able to be marshalled to and unmarshalled from JSON sensibly.

type PartFactory

type PartFactory func() Part

PartFactory creates a part.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL