Ejemplo n.º 1
0
func (j *EditJob) EditObject(fts *models.FilesTreeStore, direction EditJobDirection) (state string, err error) {
	var detail, old *map[inputElement]string
	if direction == EditJobForward {
		detail, old = &j.detail, &j.old
	} else {
		old, detail = &j.detail, &j.old
	}
	var obj tr.TreeElementIf
	obj, err = fts.GetObjectById(j.objId)
	state = j.objId
	switch j.elemType {
	case eNode:
		n := obj.(bh.NodeIf)
		(*old)[iNodeName] = n.Name()
		n.SetName((*detail)[iNodeName])
		fts.SetValueById(j.objId, n.Name())
		for _, p := range n.InPorts() {
			updateConnections(p, fts)
		}
		for _, p := range n.OutPorts() {
			updateConnections(p, fts)
		}
	case eOutputNode:
		n := obj.(bh.NodeIf)
		(*old)[iOutputNodeName] = n.Name()
		n.SetName((*detail)[iOutputNodeName])
		fts.SetValueById(j.objId, n.Name())
		for _, p := range n.InPorts() {
			updateConnections(p, fts)
		}
	case eInputNode:
		n := obj.(bh.NodeIf)
		(*old)[iInputNodeName] = n.Name()
		n.SetName((*detail)[iInputNodeName])
		fts.SetValueById(j.objId, n.Name())
		for _, p := range n.OutPorts() {
			updateConnections(p, fts)
		}
	case eNodeType:
		nt := obj.(bh.NodeTypeIf)
		if len(nt.Instances()) > 0 {
			log.Printf("jobApplier.Apply(JobEdit): WARNING: NodeTypeIf %s has instances.\n", nt.TypeName())
			log.Printf("jobApplier.Apply(JobEdit): Editing is not implemented in this case.\n")
			return
		}
		(*old)[iTypeName] = nt.TypeName()
		nt.SetTypeName((*detail)[iTypeName])
		fts.SetValueById(j.objId, nt.TypeName())
	case ePortType:
		pt := obj.(bh.PortTypeIf)
		ptCursor := fts.Cursor(pt)
		ntCursor := fts.Parent(ptCursor)
		nt := fts.Object(ntCursor).(bh.NodeTypeIf)
		if len(nt.Instances()) > 0 {
			log.Printf("jobApplier.Apply(JobEdit): WARNING: NodeTypeIf %s has instances.\n", nt.TypeName())
			log.Printf("jobApplier.Apply(JobEdit): Editing is not implemented in this case.\n")
			return
		}
		(*old)[iPortName] = pt.Name()
		(*old)[iSignalTypeSelect] = pt.SignalType().TypeName()
		(*old)[iDirection] = direction2string[pt.Direction()]
		fts.DeleteObject(ptCursor.Path)
		fts.AddNewObject(ntCursor.Path, ntCursor.Position,
			behaviour.PortTypeNew((*detail)[iPortName],
				(*detail)[iSignalTypeSelect],
				string2direction[(*detail)[iDirection]]))
		state = ptCursor.Path
	case eSignalType:
		st := obj.(bh.SignalTypeIf)
		if (*detail)[iSignalTypeName] != st.TypeName() {
			log.Printf("jobApplier.Apply(JobEdit): Renaming SignalType is not implemented.\n")
		}
		(*old)[iCType] = st.CType()
		st.SetCType((*detail)[iCType])
		(*old)[iChannelId] = st.ChannelId()
		st.SetChannelId((*detail)[iChannelId])
		(*old)[iScope] = scope2string[st.Scope()]
		st.SetScope(string2scope[(*detail)[iScope]])
		(*old)[iSignalMode] = mode2string[st.Mode()]
		st.SetMode(string2mode[(*detail)[iSignalMode]])
	case eImplementation:
		impl := obj.(bh.ImplementationIf)
		(*old)[iImplName] = impl.ElementName()
		impl.SetElemName((*detail)[iImplName])
		fts.SetValueById(j.objId, (*detail)[iImplName])
	case eArch:
		a := obj.(pf.ArchIf)
		(*old)[iArchName] = a.Name()
		a.SetName((*detail)[iArchName])
		for _, p := range a.Processes() {
			for _, c := range p.InChannels() {
				link := c.Link()
				id := fts.Cursor(link)
				fts.SetValueById(id.Path, link.Name())
			}
			for _, c := range p.OutChannels() {
				link := c.Link()
				id := fts.Cursor(link)
				fts.SetValueById(id.Path, link.Name())
			}
		}
		fts.SetValueById(j.objId, a.Name())
	case eProcess:
		p := obj.(pf.ProcessIf)
		(*old)[iProcessName] = p.Name()
		p.SetName((*detail)[iProcessName])
		for _, c := range p.InChannels() {
			link := c.Link()
			id := fts.Cursor(link)
			fts.SetValueById(id.Path, link.Name())
		}
		for _, c := range p.OutChannels() {
			link := c.Link()
			id := fts.Cursor(link)
			fts.SetValueById(id.Path, link.Name())
		}
		fts.SetValueById(j.objId, p.Name())
	case eIOType:
		t := obj.(pf.IOTypeIf)
		(*old)[iIOTypeName] = t.Name()
		t.SetName((*detail)[iIOTypeName])
		for _, a := range t.Platform().Arch() {
			aCursor := fts.Cursor(a)
			for _, p := range a.Processes() {
				pCursor := fts.CursorAt(aCursor, p)
				for _, c := range p.InChannels() {
					if t == c.IOType() {
						cCursor := fts.CursorAt(pCursor, c)
						fts.SetValueById(cCursor.Path, c.Name())
					}
				}
				for _, c := range p.OutChannels() {
					if t == c.IOType() {
						cCursor := fts.CursorAt(pCursor, c)
						fts.SetValueById(cCursor.Path, c.Name())
					}
				}
			}
		}
		(*old)[iIOModeSelect] = string(t.IOMode())
		t.SetIOMode(gr.IOMode((*detail)[iIOModeSelect]))
		fts.SetValueById(j.objId, t.Name())
	case eChannel:
		c := obj.(pf.ChannelIf)
		//(*old)[iChannelDirection] = direction2string[c.Direction()]
		//c.SetDirection(string2direction[(*detail)[iChannelDirection]])
		(*old)[iIOTypeSelect] = c.IOType().Name()
		iot, ok := freesp.GetIOTypeByName((*detail)[iIOTypeSelect])
		if ok {
			c.SetIOType(iot)
		} else {
			log.Printf("jobApplier.Apply(JobEdit): ERROR: IOType %s not registered.\n", (*detail)[iIOTypeSelect])
		}
		fts.SetValueById(j.objId, c.Name())
		link := c.Link()
		id := fts.Cursor(link)
		fts.SetValueById(id.Path, link.Name())
	case eMapElement:
		c := obj.(mp.MappedElementIf)
		pr, ok := c.Process()
		if !ok {
			(*old)[iProcessSelect] = "<unmapped>"
		} else {
			(*old)[iProcessSelect] = fmt.Sprintf("%s/%s", pr.Arch().Name(), pr.Name())
		}
		for _, a := range c.Mapping().Platform().Arch() {
			for _, pr = range a.Processes() {
				if (*detail)[iProcessSelect] == fmt.Sprintf("%s/%s", a.Name(), pr.Name()) {
					c.SetProcess(pr)
					fts.SetValueById(j.objId, (*detail)[iProcessSelect])
					return
				}
			}
		}
		c.SetProcess(nil)
		fts.SetValueById(j.objId, (*detail)[iProcessSelect])
	default:
		log.Printf("jobApplier.Apply(JobEdit): error: invalid job description\n")
	}
	return
}
Ejemplo n.º 2
0
func (j *NewElementJob) CreateObject(fts *models.FilesTreeStore) (ret tr.TreeElementIf, err error) {
	var parentObject tr.TreeElementIf
	parentObject, err = fts.GetObjectById(j.parentId)
	if err != nil {
		log.Fatal("NewElementJob.CreateObject error: referenced parentObject run away...")
	}
	switch j.elemType {
	case eNode, eInputNode, eOutputNode:
		var context bh.SignalGraphTypeIf
		switch parentObject.(type) {
		case bh.NodeIf:
			context = parentObject.(bh.NodeIf).Context()
			j.parentId = getParentId(j.parentId)
		case bh.SignalGraphIf:
			context = parentObject.(bh.SignalGraphIf).ItsType()
		case bh.SignalGraphTypeIf:
			context = parentObject.(bh.SignalGraphTypeIf)
		case bh.ImplementationIf:
			if parentObject.(bh.ImplementationIf).ImplementationType() == bh.NodeTypeGraph {
				context = parentObject.(bh.ImplementationIf).Graph()
			} else {
				log.Fatal("NewElementJob.CreateObject(eNode) error: parent implementation is no graph...")
			}
		default:
			log.Fatal("NewElementJob.CreateObject(eNode) error: referenced parentObject wrong type...")
		}
		if j.elemType == eNode {
			ntype, ok := freesp.GetNodeTypeByName(j.input[iNodeTypeSelect])
			if !ok {
				log.Fatal("NewElementJob.CreateObject(eNode) error: referenced parentObject type wrong...")
			}
			ret, err = behaviour.NodeNew(j.input[iNodeName], ntype, context)
		} else if j.elemType == eInputNode {
			ret, err = behaviour.InputNodeNew(j.input[iInputNodeName], j.input[iInputTypeSelect], context)
		} else {
			ret, err = behaviour.OutputNodeNew(j.input[iOutputNodeName], j.input[iOutputTypeSelect], context)
		}
		if len(j.extra) > 0 {
			coords := strings.Split(j.extra, "|")
			var x, y int
			fmt.Sscanf(coords[0], "%d", &x)
			fmt.Sscanf(coords[1], "%d", &y)
			//pos := image.Point{x, y}
			//log.Printf("NewElementJob.CreateObject(eNode) setting position %s: %v\n", j.extra, pos)
			//ret.(bh.NodeIf).SetPosition(pos)
			// TODO: SetModePosition...
		}

	case eNodeType:
		var context string
		switch parentObject.(type) {
		case bh.NodeTypeIf:
			context = parentObject.(bh.NodeTypeIf).DefinedAt()
			j.parentId = getParentId(j.parentId)
		case bh.SignalTypeIf:
			j.parentId = getParentId(j.parentId)
			parentObject, err = fts.GetObjectById(j.parentId)
			context = parentObject.(bh.LibraryIf).Filename()
		case bh.LibraryIf:
			context = parentObject.(bh.LibraryIf).Filename()
		default:
			log.Fatal("NewElementJob.CreateObject(eNodeType) error: referenced parentObject wrong type...")
		}
		ret = behaviour.NodeTypeNew(j.input[iTypeName], context)

	case eConnection:
		switch parentObject.(type) {
		case bh.PortIf:
		case bh.SignalGraphTypeIf:
			fromTo := strings.Split(j.extra, "/")
			var n bh.NodeIf
			for _, n = range parentObject.(bh.SignalGraphTypeIf).Nodes() {
				if n.Name() == fromTo[0] {
					for _, parentObject = range n.OutPorts() {
						if parentObject.(bh.PortIf).Name() == fromTo[1] {
							break
						}
					}
					break
				}
			}
			if parentObject == nil {
				log.Fatalf("NewElementJob.CreateObject(eNodeType) error: no valid FROM port for edge job %v\n", j)
			}
			_ = parentObject.(bh.PortIf)
		case bh.ImplementationIf:
			fromTo := strings.Split(j.extra, "/")
			var n bh.NodeIf
			for _, n = range parentObject.(bh.ImplementationIf).Graph().Nodes() {
				if n.Name() == fromTo[0] {
					for _, parentObject = range n.OutPorts() {
						if parentObject.(bh.PortIf).Name() == fromTo[1] {
							break
						}
					}
					break
				}
			}
			if parentObject == nil {
				log.Fatalf("NewElementJob.CreateObject(eNodeType) error: no valid FROM port for edge job %v\n", j)
			}
			_ = parentObject.(bh.PortIf)
		default:
			log.Fatalf("NewElementJob.CreateObject(eConnection) error: referenced parentObject wrong type %T\n", parentObject)
		}
		ports := getMatchingPorts(fts, parentObject)
		for _, p := range ports {
			s := fmt.Sprintf("%s/%s", p.Node().Name(), p.Name())
			if j.input[iPortSelect] == s {
				var from, to bh.PortIf
				if p.Direction() == gr.InPort {
					from = parentObject.(bh.PortIf)
					to = p
				} else {
					from = p
					to = parentObject.(bh.PortIf)
				}
				ret = behaviour.ConnectionNew(from, to)
				break
			}
		}

	case ePortType:
		switch parentObject.(type) {
		case bh.PortTypeIf:
			j.parentId = getParentId(j.parentId)
		case bh.NodeTypeIf:
		default:
			log.Fatal("NewElementJob.CreateObject(ePortType) error: referenced parentObject wrong type...")
		}
		_, ok := freesp.GetSignalTypeByName(j.input[iSignalTypeSelect])
		if !ok {
			err = fmt.Errorf("NewElementJob.CreateObject(ePortType) error: referenced signal type wrong...")
			return
		}
		ret = behaviour.PortTypeNew(j.input[iPortName], j.input[iSignalTypeSelect], string2direction[j.input[iDirection]])

	case eSignalType:
		switch parentObject.(type) {
		case bh.SignalTypeIf:
			j.parentId = getParentId(j.parentId)
		case bh.NodeTypeIf:
			j.parentId = getParentId(j.parentId)
		case bh.LibraryIf:
		default:
			log.Fatalf("NewElementJob.CreateObject(eSignalType) error: referenced parentObject wrong type %T\n", parentObject)
		}
		parentObject, err = fts.GetObjectById(j.parentId)
		if err != nil {
			log.Fatalf("NewElementJob.CreateObject(eSignalType) error: parent library object not found\n")
		}
		name := j.input[iSignalTypeName]
		cType := j.input[iCType]
		channelId := j.input[iChannelId]
		scope := string2scope[j.input[iScope]]
		mode := string2mode[j.input[iSignalMode]]
		ret, err = behaviour.SignalTypeNew(name, cType, channelId, scope, mode, parentObject.(bh.LibraryIf).Filename())
		if err != nil {
			log.Printf("NewElementJob.CreateObject(eSignalType) error: SignalTypeNew failed: %s\n", err)
			return
		}

	case eImplementation:
		switch parentObject.(type) {
		case bh.ImplementationIf:
			j.parentId = getParentId(j.parentId)
		case bh.NodeTypeIf:
		default:
			log.Fatalf("NewElementJob.CreateObject(eSignalType) error: referenced parentObject wrong type %T\n", parentObject)
		}
		implType := string2implType[j.input[iImplementationType]]
		ret = behaviour.ImplementationNew(j.input[iImplName], implType, &global)

	case eArch:
		switch parentObject.(type) {
		case pf.ArchIf:
			j.parentId = getParentId(j.parentId)
			parentObject = parentObject.(pf.ArchIf).Platform()
		case pf.PlatformIf:
		default:
			log.Fatalf("NewElementJob.CreateObject(eArch) error: referenced parentObject wrong type %T\n", parentObject)
		}
		ret = platform.ArchNew(j.input[iArchName], parentObject.(pf.PlatformIf))

	case eIOType:
		var p pf.PlatformIf
		switch parentObject.(type) {
		case pf.IOTypeIf:
			j.parentId = getParentId(j.parentId)
			p = parentObject.(pf.IOTypeIf).Platform()
		case pf.ArchIf:
			p = parentObject.(pf.ArchIf).Platform()
		default:
			log.Fatalf("NewElementJob.CreateObject(eIOType) error: referenced parentObject wrong type %T\n", parentObject)
		}
		ret, err = platform.IOTypeNew(j.input[iIOTypeName], gr.IOMode(j.input[iIOModeSelect]), p)

	case eProcess:
		switch parentObject.(type) {
		case pf.ProcessIf:
			j.parentId = getParentId(j.parentId)
			parentObject = parentObject.(pf.ProcessIf).Arch()
		case pf.ArchIf:
		default:
			log.Fatalf("NewElementJob.CreateObject(eProcess) error: referenced parentObject wrong type %T\n", parentObject)
		}
		ret = platform.ProcessNew(j.input[iProcessName], parentObject.(pf.ArchIf))

	case eChannel:
		switch parentObject.(type) {
		case pf.ChannelIf:
			j.parentId = getParentId(j.parentId)
			parentObject = parentObject.(pf.ChannelIf).Process()
		case pf.ProcessIf:
		default:
			log.Fatalf("NewElementJob.CreateObject(eChannel) error: referenced parentObject wrong type %T\n", parentObject)
		}
		processes := getOtherProcesses(fts, parentObject)
		var p pf.ProcessIf
		for _, p = range processes {
			s := fmt.Sprintf("%s/%s", p.Arch().Name(), p.Name())
			if s == j.input[iChannelLinkSelect] {
				break
			}
		}
		if p == nil {
			log.Fatalf("NewElementJob.CreateObject(eChannel) error: can't find chosen process\n", j.input[iChannelLinkSelect])
		}
		ioType, ok := freesp.GetIOTypeByName(j.input[iIOTypeSelect])
		if !ok {
			log.Fatalf("NewElementJob.CreateObject(eChannel) error: can't find chosen ioType\n", j.input[iIOTypeSelect])
		}
		ret = platform.ChannelNew(string2direction[j.input[iChannelDirection]], ioType, parentObject.(pf.ProcessIf), j.input[iChannelLinkSelect])

	default:
		log.Fatal("NewElementJob.CreateObject error: invalid elemType ", j.elemType)
	}
	return
}