Пример #1
0
// Compiles a tree returning a gp.Primitive, resulting
// from the execution of the Run method
func CompileTree(root *Node) gp.Primitive {
	if root.value.IsFunctional() {
		// If it's a functional, compile each children and return
		terms := make([]gp.Primitive, len(root.children))
		for i := 0; i < len(root.children); i++ {
			terms[i] = CompileTree(root.children[i])
		}
		if root.value.Arity() != len(terms) {
			fmt.Println("ERROR! Trying to call a Functional with Arity", root.value.Arity(), "passing", len(terms), "arguments")
			fmt.Println("Tree is", root)
			panic(fmt.Sprint("ERROR! Trying to call a Functional with Arity ", root.value.Arity(), " passing ", len(terms), " arguments: ", gp.FuncName(root.value)))
		}
		return root.value.Run(terms...)
	} else {
		if root.value.IsEphemeral() {
			panic("Ephemerals should never be in a compilable tree!")
		}
		return root.value.Run()
	}
}
Пример #2
0
func (self Functional2) Name() string                       { return gp.FuncName(self) }
Пример #3
0
// Produces a graph for the tree, using graphviz dot, saving the graph
// in the file <name>.png. If a color map (generated with Colorize) is
// provided, the nodes will be colored
func (root *Node) GraphvizDot(name string, hsvColors map[*Node][]float32) string {
	// Function to build edges
	var ind func(parent, r *Node, tabStr string) string

	// Store the identifiers for tree node
	ids := make(map[*Node]string)
	// Count how many times the name has been used
	counts := make(map[string]int)

	ind = func(parent, r *Node, tabStr string) string {
		// Get the identifier for the node, and see if it's present
		_, ok := ids[r]
		if !ok {
			// This is the first time we use this node, give it a name
			name := gp.FuncName(r.value)
			// Increase the sequential numbet for this node type
			counts[name] += 1
			ids[r] = fmt.Sprintf("\"%v-%v\"", name, counts[name])
		}

		//
		str := ""

		if parent != nil {
			// We print the "parent -> children;" line
			str += fmt.Sprintf("%v%v -> %v;\n", tabStr, ids[parent], ids[r])
		}
		for i := 0; i < len(r.children); i++ {
			str += ind(r, r.children[i], tabStr)
		}
		return str
	}

	// Write edges, and also modify the identifiers map
	edges := ind(nil, root, "    ")

	// If a color is present, we set the fill color

	style := ""
	colors := ""
	if hsvColors != nil {
		style = "node [style=filled,margin=0,shape=box];\n"
		for k, v := range ids {
			colors += fmt.Sprintf("%v [fillcolor=\"%0.3f %0.3f %0.3f\", label=\"%p\"]\n", v, hsvColors[k][0], hsvColors[k][1], hsvColors[k][2], k)
		}
	}

	src := fmt.Sprintf("digraph %v {rankdir=LR;%v\n%v\n%v\n label=\"%v\"}\n", name, style, edges, colors, name)

	cmd := exec.Command("dot", "-Tpng", "-o", name+".png")
	cmd.Stdin = strings.NewReader(src)
	var out, stderr bytes.Buffer
	cmd.Stdout = &out
	cmd.Stderr = &stderr
	err := cmd.Run()
	if err != nil {
		fmt.Printf("Cannot run dot (%v)\nOutput:\n%v\nError:\n%v\n", err, out.String(), stderr.String())
		return src
	}
	return "" //src
}
Пример #4
0
func (self Terminal2) Name() string                       { return gp.FuncName(self) }