func parseNode(text, context string, graph bh.SignalGraphTypeIf) (job *PasteJob, ok bool) { xmln := backend.XmlNode{} _, xmlerr := xmln.Read([]byte(text)) if xmlerr != nil { return } nodes := graph.Nodes() for true { valid := true for _, reg := range nodes { if reg.Name() == xmln.NName { valid = false } } if valid { break } xmln.NName = createNextNameCandidate(xmln.NName) } _, validType := freesp.GetNodeTypeByName(xmln.NType) var njob *NewElementJob switch { case validType: njob = NewElementJobNew(context, eNode) njob.input[iNodeName] = xmln.NName njob.input[iNodeTypeSelect] = xmln.NType case len(xmln.InPort) == 0: if len(xmln.OutPort) == 0 { fmt.Printf("parseNode error: no ports.\n") return } njob = NewElementJobNew(context, eInputNode) njob.input[iInputNodeName] = xmln.NName njob.input[iInputTypeSelect] = xmln.OutPort[0].PType // TODO case len(xmln.OutPort) == 0: njob = NewElementJobNew(context, eOutputNode) njob.input[iOutputNodeName] = xmln.NName njob.input[iOutputTypeSelect] = xmln.InPort[0].PType // TODO default: if !validType { fmt.Printf("parseNode error: node type %s not registered.\n", xmln.NType) return } // TODO: Create node type?? njob = NewElementJobNew(context, eNode) njob.input[iNodeName] = xmln.NName njob.input[iNodeTypeSelect] = xmln.NType } job = PasteJobNew() job.context = context job.newElements = append(job.newElements, njob) ok = true return }
func TestGraph(t *testing.T) { case1 := []struct { library, graph string nodes, connections int }{ {`<library xmlns="http://www.freesp.de/xml/freeSP" version="1.0"> <signal-type name="s1" scope="" mode="" c-type="" message-id=""></signal-type> <node-type name="Test"> <intype port="" type="s1"></intype> <outtype port="" type="s1"></outtype> </node-type> </library> `, `<?xml version="1.0" encoding="UTF-8"?> <signal-graph xmlns="http://www.freesp.de/xml/freeSP" version="1.0"> <nodes> <input name="sensor"> <outtype type="s1"/> </input> <output name="actuator"> <intype type="s1"/> </output> <processing-node name="test" type="Test"></processing-node> </nodes> <connections> <connect from="sensor" to="test"/> <connect from="test" to="actuator"/> </connections> </signal-graph> `, 3, 2}, } for i, c := range case1 { freesp.Init() var l bh.LibraryIf = LibraryNew("test.alml", nil) buf := copyBuf(c.library) _, err := l.Read(buf) if err != nil { t.Errorf("Testcase %d: Failed to read from buffer: %v", i, err) return } var sg bh.SignalGraphIf = SignalGraphNew("test.sml", nil) buf = copyBuf(c.graph) _, err = sg.Read(buf) if err != nil { t.Errorf("Testcase %d: Failed to read from buffer: %v", i, err) return } var st bh.SignalGraphTypeIf = sg.ItsType() if len(st.Nodes()) != c.nodes { t.Errorf("Testcase %d: NodeIf count mismatch", i) return } } }
func SignalGraphTypeUsesNodeType(t bh.SignalGraphTypeIf, nt bh.NodeTypeIf) bool { for _, n := range t.Nodes() { if n.ItsType().TypeName() == nt.TypeName() { return true } for _, impl := range n.ItsType().Implementation() { if impl.ImplementationType() == bh.NodeTypeGraph { if SignalGraphTypeUsesNodeType(impl.Graph(), nt) { return true } } } } return false }
func SignalGraphTypeUsesSignalType(t bh.SignalGraphTypeIf, st bh.SignalTypeIf) bool { for _, n := range t.Nodes() { for _, p := range n.InPorts() { if p.SignalType() == st { return true } } for _, p := range n.OutPorts() { if p.SignalType() == st { return true } } for _, impl := range n.ItsType().Implementation() { if impl.ImplementationType() == bh.NodeTypeGraph { if SignalGraphTypeUsesSignalType(impl.Graph(), st) { return true } } } } return false }
func NodeNew(name string, ntype bh.NodeTypeIf, context bh.SignalGraphTypeIf) (ret *node, err error) { for _, n := range context.Nodes() { if n.Name() == name { err = fmt.Errorf("NodeNew error: node '%s' already exists in context.", name) return } } if len(ntype.InPorts())+len(ntype.OutPorts()) == 0 { err = fmt.Errorf("NodeNew error: type '%s' has no ports.", ntype.TypeName()) return } ret = &node{*gr.PathModePositionerObjectNew(), context, name, ntype, portListInit(), portListInit(), nil, false} for _, p := range ntype.InPorts() { ret.addInPort(p) } for _, p := range ntype.OutPorts() { ret.addOutPort(p) } ntype.(*nodeType).addInstance(ret) return }
func CreateXmlSignalGraphType(t bh.SignalGraphTypeIf) *backend.XmlSignalGraph { ret := backend.XmlSignalGraphNew() for _, l := range t.Libraries() { ret.Libraries = append(ret.Libraries, *CreateXmlLibraryRef(l.Filename())) } for _, n := range t.InputNodes() { ret.InputNodes = append(ret.InputNodes, *CreateXmlInputNode(n)) } for _, n := range t.OutputNodes() { ret.OutputNodes = append(ret.OutputNodes, *CreateXmlOutputNode(n)) } for _, n := range t.ProcessingNodes() { ret.ProcessingNodes = append(ret.ProcessingNodes, *CreateXmlProcessingNode(n)) } for _, n := range t.Nodes() { for _, p := range n.OutPorts() { for _, c := range p.Connections() { conn := p.Connection(c) ret.Connections = append(ret.Connections, *CreateXmlConnection(conn)) } } } return ret }