示例#1
0
func parseSignalGraph(text, context string) (job *PasteJob, ok bool) {
	xmlsg := backend.XmlSignalGraph{}
	_, xmlerr := xmlsg.Read([]byte(text))
	if xmlerr != nil {
		return
	}
	for _, l := range xmlsg.Libraries {
		_, err := global.LibraryMgr().Access(l.Name)
		if err != nil {
			fmt.Printf("parseSignalGraph error: referenced library %s not accessible.\n", l.Name)
			return
		}
	}
	var njob *NewElementJob
	njob = NewElementJobNew(context, eSignalGraph)
	job = PasteJobNew()
	job.context = context
	job.newElements = append(job.newElements, njob)
	for _, xmln := range xmlsg.ProcessingNodes {
		_, validNodeType := freesp.GetNodeTypeByName(xmln.NType)
		if !validNodeType {
			fmt.Printf("parseSignalGraph error: node type %s not registered.\n", xmln.NType)
			return
		}
		njob = NewElementJobNew(context, eNode)
		njob.input[iNodeName] = xmln.NName
		njob.input[iNodeTypeSelect] = xmln.NType
		pj := PasteJobNew()
		pj.newElements = append(pj.newElements, njob)
		job.children = append(job.children, pj)
	}
	for _, xmln := range xmlsg.InputNodes {
		if len(xmln.OutPort) == 0 {
			fmt.Printf("parseSignalGraph error: input node has no outports.\n")
			return
		}
		njob = NewElementJobNew(context, eInputNode)
		njob.input[iInputNodeName] = xmln.NName
		njob.input[iInputTypeSelect] = xmln.OutPort[0].PType // TODO
		pj := PasteJobNew()
		pj.newElements = append(pj.newElements, njob)
		job.children = append(job.children, pj)
	}
	for _, xmln := range xmlsg.OutputNodes {
		if len(xmln.InPort) == 0 {
			fmt.Printf("parseSignalGraph error: output node has no inports.\n")
			return
		}
		njob = NewElementJobNew(context, eOutputNode)
		njob.input[iOutputNodeName] = xmln.NName
		njob.input[iOutputTypeSelect] = xmln.InPort[0].PType // TODO
		pj := PasteJobNew()
		pj.newElements = append(pj.newElements, njob)
		job.children = append(job.children, pj)
	}
	ok = true
	return
}
示例#2
0
func createSignalGraphTypeFromXml(g *backend.XmlSignalGraph, name string, context mod.ModelContextIf,
	resolvePort func(portname string, dir gr.PortDirection) *portType) (t *signalGraphType, err error) {
	t = SignalGraphTypeNew(context)
	for _, ref := range g.Libraries {
		l, ok := freesp.GetLibraryByName(ref.Name)
		if !ok {
			var f fd.FileDataIf
			f, err = t.context.LibraryMgr().Access(ref.Name)
			if err != nil {
				err = fmt.Errorf("createSignalGraphTypeFromXml error: referenced library file %s not found", ref.Name)
				return
			}
			l = f.(bh.LibraryIf)
			freesp.RegisterLibrary(l)
			fmt.Println("createSignalGraphTypeFromXml: library", ref.Name, "successfully loaded")
		}
		t.libraries = append(t.libraries, l)
	}
	for _, n := range g.InputNodes {
		var nnode *node
		nnode, err = t.createInputNodeFromXml(n, resolvePort)
		if err != nil {
			return
		}
		t.inputNodes = append(t.inputNodes, nnode)
		t.nodes.Append(nnode)
	}
	for _, n := range g.OutputNodes {
		var nnode *node
		nnode, err = t.createOutputNodeFromXml(n, resolvePort)
		if err != nil {
			return
		}
		t.outputNodes = append(t.outputNodes, nnode)
		t.nodes.Append(nnode)
	}
	for _, n := range g.ProcessingNodes {
		nnode := t.createNodeFromXml(n.XmlNode)
		t.processingNodes = append(t.processingNodes, nnode)
		t.nodes.Append(nnode)
	}
	for i, c := range g.Connections {
		n1, ok := t.NodeByName(c.From)
		if !ok {
			dump, _ := g.Write()
			log.Println("createSignalGraphTypeFromXml error:")
			log.Fatal(fmt.Sprintf("invalid edge %d: node %s not found\n%s", i, c.From, dump))
		}
		n2, ok := t.NodeByName(c.To)
		if !ok {
			dump, _ := g.Write()
			log.Println("createSignalGraphTypeFromXml error:")
			log.Fatal(fmt.Sprintf("invalid edge %d: node %s not found\n%s", i, c.To, dump))
		}
		p1, err := n1.(*node).outPortFromName(c.FromPort)
		if err != nil {
			dump, _ := g.Write()
			log.Println("createSignalGraphTypeFromXml error:")
			log.Printf("edge = %v\n", c)
			log.Printf("node = %v, missing port = %s\n", n1, c.FromPort)
			log.Fatal(fmt.Sprintf("invalid edge %d outPortFromName failed: %s\n%s", i, err, dump))
		}
		p2, err := n2.(*node).inPortFromName(c.ToPort)
		if err != nil {
			dump, _ := g.Write()
			log.Println("createSignalGraphTypeFromXml error:")
			log.Fatal(fmt.Sprintf("invalid edge %d inPortFromName failed: %s\n%s", i, err, dump))
		}
		err = p1.AddConnection(ConnectionNew(p1, p2))
		if err != nil {
			dump, _ := g.Write()
			log.Println("createSignalGraphTypeFromXml error:")
			log.Fatal(fmt.Sprintf("invalid edge %d PortConnect failed: %s\n%s", i, err, dump))
		}
	}
	return
}