Skip to content

Commit

Permalink
Merge pull request #28 from sprotest/feature/placeholder-node
Browse files Browse the repository at this point in the history
Feature/placeholder node
  • Loading branch information
slavas490 authored Mar 29, 2024
2 parents 5893ef1 + eff6928 commit 12d61f0
Show file tree
Hide file tree
Showing 5 changed files with 302 additions and 41 deletions.
59 changes: 58 additions & 1 deletion sys/workflow/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,9 @@ func (me *Engine) setupNodes(ctx *context) error {
if len(me.nodeImpls) == 0 {
return ErrNodeImplementationNotProvided
}

for _, item := range ctx.flow.Nodes {
item.internalNode = item.isCondition() || item.isWorkflow()
item.internalNode = item.isCondition() || item.isWorkflow() || item.isPlaceholder()
if !item.internalNode {
if s, ok := me.nodeImpls[item.Type]; ok && s.InitImplFunc != nil {
item.new = s.InitImplFunc
Expand All @@ -319,6 +320,62 @@ func (me *Engine) setupNodes(ctx *context) error {
return nil
}

func (me *Engine) removeUselessNodes(ctx *context) error {
uselessConnections := make(map[string]*Connection, 0)

for name, item := range ctx.flow.Nodes {
if !item.isPlaceholder() {
continue
}

for _, conn := range item.Connections {
correctConnection := conn
currentNode := ctx.flow.Nodes[correctConnection.NodeID]

for currentNode.isPlaceholder() {
if len(currentNode.Connections) > 0 {
correctConnection = currentNode.Connections[0]
currentNode = ctx.flow.Nodes[correctConnection.NodeID]
} else {
break
}
}

uselessConnections[name] = correctConnection
}
}

for _, item := range ctx.flow.Nodes {
if item.isPlaceholder() {
continue
}

for i := 0; i < len(item.Connections); i++ {
conn := item.Connections[i]

if val, ok := uselessConnections[conn.NodeID]; ok {
item.Connections[i] = val
}
}
}

if val, ok := uselessConnections[ctx.flow.Start.NodeID]; ok {
ctx.flow.Start.NodeID = val.NodeID
}

filteredNodes := make(map[string]*Node, 0)

for name, item := range ctx.flow.Nodes {
if !item.isPlaceholder() {
filteredNodes[name] = item
}
}

ctx.flow.Nodes = filteredNodes

return nil
}

func (me *Engine) execute(nn *Node, considerSteps bool) (proceed bool, err error) {
nn.focus()
if nn.internalNode {
Expand Down
4 changes: 4 additions & 0 deletions sys/workflow/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func (n *Node) isWorkflow() bool {
return n.Type == "workflow"
}

func (n *Node) isPlaceholder() bool {
return n.Type == "placeholder"
}

func (me *Node) getImpl() (NodeIF, error) {
//instance is still valid for reuse
if me.impl != nil {
Expand Down
15 changes: 13 additions & 2 deletions sys/workflow/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ func newWorkflow(wf *Workflow, engine *Engine, parent *context) (*context, error
if wf == nil {
return nil, ErrWorkflowMissing
}

if wf.Flow == nil || wf.Flow.Start == nil || wf.Flow.Start.NodeID == "" {
return nil, ErrStartNodeMissing
}

me := context{
id: "root",
flow: wf.Flow,
Expand All @@ -84,11 +86,18 @@ func newWorkflow(wf *Workflow, engine *Engine, parent *context) (*context, error
engine: engine,
}

err := engine.setupNodes(&me)
err := engine.removeUselessNodes(&me)
if err != nil {
return nil, err
}

err = engine.setupNodes(&me)
if err != nil {
return nil, err
}

me.start()

return &me, nil
}

Expand Down Expand Up @@ -122,7 +131,9 @@ func (me *context) resolve(n *Node) error {
return err
}
return me.resolve(nn)
} else if n.isWorkflow() {
} else if n.isPlaceholder() {
return nil
}else if n.isWorkflow() {
return me.stepIntoWorkflow(n)
}
} else {
Expand Down
Loading

0 comments on commit 12d61f0

Please sign in to comment.