Esempio n. 1
0
// GraphAsText is the standard implementation of Grapher
func (g *defaultGrapher) GraphAsText(dotText []byte) (string, error) {
	graphAst, err := parser.ParseBytes(dotText)
	if err != nil {
		return "", err
	}
	dag := ggv.NewGraph() // A directed acyclic graph
	ggv.Analyse(graphAst, dag)

	if len(dag.Edges.Edges) == 0 {
		return "", errNoActivity
	}
	nodeToOutEdges := g.collectionGetter.generateNodeToOutEdges(dag)
	inDegreeZeroNodes := g.collectionGetter.getInDegreeZeroNodes(dag)
	nameToNodes := dag.Nodes.Lookup

	buffer := new(bytes.Buffer)
	statusMap := make(map[string]discoveryStatus)

	for _, root := range inDegreeZeroNodes {
		g.searcher.dfs(searchArgs{
			root:           root,
			path:           nil,
			nodeToOutEdges: nodeToOutEdges,
			nameToNodes:    nameToNodes,
			buffer:         buffer,
			statusMap:      statusMap,
		})
	}

	return buffer.String(), nil
}
Esempio n. 2
0
func (this *DotSerializer) Deserialize(data []byte) (*DAG, error) {
	ast, err := dotparser.ParseBytes(data)
	if err != nil {
		return nil, err
	}
	graph := dot.NewGraph()
	dot.Analyse(ast, graph)

	p := NewDAG(graph.Name)

	for src, dests := range graph.Edges.SrcToDsts {
		for dest, _ := range dests {

			if orig, ok := p.Jobs[src]; !ok {
				n := dotNameToDAGJob(graph, src)
				n.Post = append(n.Post, dest)
				p.Jobs[src] = n
			} else {
				orig.Post = append(orig.Post, dest)
			}
			if orig, ok := p.Jobs[dest]; !ok {
				n := dotNameToDAGJob(graph, dest)
				n.Prev = append(n.Prev, src)
				p.Jobs[dest] = n
			} else {
				orig.Prev = append(orig.Prev, src)
			}

			if _, ok := p.InDegrees[src]; !ok {
				p.InDegrees[src] = 0
			}
			if orig, ok := p.InDegrees[dest]; !ok {
				p.InDegrees[dest] = 1
			} else {
				p.InDegrees[dest] = orig + 1
			}
		}
	}

	for _, edge := range graph.Edges.Edges {
		if v, ok := edge.Attrs["nonstrict"]; ok {
			nonstrict, err := util.StringToBool(strings.Trim(v, "\""))
			if err != nil {
				log.Error(err)
				return nil, err
			}
			if len(edge.Src) != 0 && len(edge.Dst) != 0 {
				r := NewRelation()
				r.NonStrict = nonstrict
				if _, ok := p.Relations[edge.Src]; !ok {
					p.Relations[edge.Src] = make(map[string]*Relation)
				}
				p.Relations[edge.Src][edge.Dst] = r
			}
		}
	}

	return p, nil
}
Esempio n. 3
0
func (n *Node) ToGraphViz() string {
	graphAst, _ := parser.ParseString(`digraph NestedSet {}`)
	graph := gographviz.NewGraph()
	gographviz.Analyse(graphAst, graph)

	n.addToGraphViz(graph, "NestedSet", "")

	output := graph.String()

	return output
}
Esempio n. 4
0
func ParseTasks(topologyDot []byte) *TaskGraphStructure {

	parsed, err := gographviz.Parse(topologyDot)
	if err != nil {
		panic(err)
	}
	// Display the graph
	var topology *TaskGraphStructure
	topology = NewTaskGraphStructure()
	gographviz.Analyse(parsed, topology)
	return topology
}
Esempio n. 5
0
func visualize(data *Data) {
	graphAst, _ := parser.ParseString(`digraph G {}`)
	g := gographviz.NewGraph()

	gographviz.Analyse(graphAst, g)

	// step1 : add nodes
	for node := range data.NodeMap {
		g.AddNode("G", node.ID, nil)
	}

	// step2 : make edge from source node  to target node
	for _, edge := range data.GetEdges() {
		g.AddEdge(edge.Src.ID, edge.Dst.ID, true, nil)
	}
	output := g.String()
	fmt.Println(output)
}
Esempio n. 6
0
func main() {
	pDoGame := flag.Bool("game", false, "start the game")
	flag.Parse()

	rcsv("data/neurons.csv", func(records []string) {
		newEmptyNeuron(records[0], records, 5, BODY)
	})

	rcsv("data/connectome.csv", func(records []string) {
		weight, err := strconv.ParseInt(records[3], 10, 32)
		if err != nil {
			log.Fatal(err)
		}

		ID_TO_NEURON[records[0]].connect(ID_TO_NEURON[records[1]], int32(weight))
	})

	rcsv("data/fake_motor.csv", func(records []string) {
		weight, err := strconv.ParseInt(records[3], 10, 32)
		if err != nil {
			log.Fatal(err)
		}

		if records[1] == "LEFT" {
			ID_TO_NEURON[records[0]].connect(BODY.left, int32(weight))
		} else if records[1] == "RIGHT" {
			ID_TO_NEURON[records[0]].connect(BODY.right, int32(weight))
		}
	})

	for _, v := range ID_TO_NEURON {
		v.ticker()
	}

	http.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
		q := r.URL.RawQuery
		if n, ok := ID_TO_NEURON[r.URL.RawQuery]; ok {
			n.ping(30)
			fmt.Fprintf(w, "nReceived: %d", n.nReceived)
		} else {
			http.Error(w, fmt.Sprintf("missing neuron: %s", q), 404)
		}
	})

	http.HandleFunc("/debug", func(w http.ResponseWriter, r *http.Request) {
		DEBUG = !DEBUG
		fmt.Fprintf(w, "current: %#v", DEBUG)
	})

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		graphAst, _ := parser.ParseString(`digraph G {}`)
		graph := gv.NewGraph()
		graph.SetDir(true)
		for _, v := range ID_TO_NEURON {

			if v.activity() > 0 {
				graph.AddNode("G", v.id(), nil)
				for _, c := range v.connections {
					graph.AddNode("G", c.to.id(), nil)
					graph.AddEdge(v.id(), c.to.id(), true, nil)
				}
			}
		}
		gv.Analyse(graphAst, graph)
		output := graph.String()
		fmt.Fprintf(w, output)
	})

	go func() {
		log.Fatal(http.ListenAndServe(":8080", nil))
	}()
	if *pDoGame {
		world()
	} else {
		BODY.ticker()
		for {
			time.Sleep(1 * time.Second)
		}
	}
}