Is there a better way to implement for/while loop in perprocess function?

0 votes
asked Feb 19, 2020 in Question / help by CamNemo (120 points)

Is there a better way to implement for/while loop in perprocess function?

When I try to generate several numbered task in plantUML, I wroted a function to generate task.

Then I need to call it many times.

With out native loop, I was forceed to implement it like this:

!function loop($time)

!if %not($time == 1)

!$step = 1

!$last = $time - $step

loop2($last)

!endif

round($time)

!endfunction

!function loop2($time)

loop($time)

!endfunction

I really hope loop can be native supported.

Like:

!function wh($time)

!while %not($time == 0)

round($time)

!$time = $time - 1

!endwhile

!endfunction

3 Answers

0 votes
answered Feb 19, 2020 by kjw

This is something that has been wanted for awhile now. See Below

http://wiki.plantuml.net/site/preprocessing-json

0 votes
answered Feb 19, 2020 by plantuml (257,940 points)
Congratulation for your workaround ! Very clever...

While loop is indeed a good idea : we are going to implement it.

We'll post a message here when a beta will be available.
0 votes
answered Feb 25, 2020 by plantuml (257,940 points)

So with last beta http://beta.plantuml.net/plantuml.jar

You can now have :

@startuml
!function $foo($arg)
:function start;
!while $arg!=0
  !$i=3
  !while $i!=0
    :i=$i arg=$arg;
    !$i = $i - 1
  !endwhile
  !$arg = $arg - 1
!endwhile
:function end;
!endfunction
:a;
$foo(3)
:b;
@enduml

This has not been widely tested, so any feedback is welcome.

We are also concerned by the risk of infinite loop now...

commented Mar 2, 2020 by kjw

Adding another twist :

@startuml

!$partlen=2

!$data={"participants" :[

{"name": "XYZ" ,"as": "xyz"},

{"name": "RST" ,"as": "rst"},

{"name": "UVW" ,"as": "uvw"}

]

}

!$i=0

:A;

!while $i<=$partlen

'!foreach $item in $data.participants 

        :$data.participants[$i].name as $data.participants[$i].as;

':$item[$i].name as $item[$i].name.as;

!$i = $i + 1

'!endfor 

!endwhile

:B; 

@enduml

...