How to remove empty states from Activity Diagram in PlantUML? - plantuml

I am trying to diagram a game of blackjack in PlantUML to help document some code and I have empty Activity States that I would like deleted.
#startuml
skinparam defaultTextAlignment center
start
:Create Game \n(num_players, num_decks);
:Deal Cards\n(2 per player/dealer);
if (Dealer Shows Ace?) then (Yes)
:Offer Insurance;
if (Player takes insurance) then (Yes)
else (No)
endif
else (No)
endif
:Evaluate Hand;
:Next State;
#enduml

The diamond nodes you highlighted are not states, but merge nodes, which correspond to the decision (if) nodes. The syntax of UML basically requires they be there.
Perhaps by adding a separate action "Set Insurance" (to remember that the player accepted it), the two merge nodes will make more sense?
#startuml
skinparam defaultTextAlignment center
start
:Create Game \n(num_players, num_decks);
:Deal Cards\n(2 per player/dealer);
if (Dealer Shows Ace?) then (Yes)
:Offer Insurance;
if (Player takes insurance) then (Yes)
:Set Insurance;
else (No)
endif
else (No)
endif
:Evaluate Hand;
:Next State;
#enduml

Related

Plant UML Activity Diagram, Add a partition without changing width/formatting of elements

I have a large activity diagram with a lot of branches. I want to denote that part of this diagram is contained in one microservice, but when I add a partition it scrunches everything together and the lines get crossed, making it even more unreadable than it already was. Is there a way around this. I searched the docs and it looks like there is no way to set the width of a partition. While I can't post the actual code since it's confidential, here is a gibberish version
#startuml
skinparam backgroundColor Azure
"user clicks on link" --> "do something"
"do something" --> ===b1===
===b1=== --> "do somethin else"
===b1=== --> "yep"
"yep" --> "foobarbaz"
"do somethin else" --> "bizzfuzz"
"bizzfuzz" --> ===b2===
"foobarbaz" --> ===b2===
partition Conductor{
if "is it it" then
--> ["yes it's it"] "OK"
if "really?" then
-->"asdfasdfasdfasdfasdf"
else
-->"asdfasdffasdfasdf"
if "sure?" then
-->"zxcvzxcv"
else
-->"zxcv"
endif
endif
else
-->"do something different"
if "asdf?" then
-->"ghasdgf"
else
->"asdf"
if "reallysure?" then
-->"ireytiteryi"
else
-->"wertywert"
endif
endif
}
#enduml
Notice that if you remove the partition the elements spread out a bit. I want to keep them spread out that way but also have the partition. Is this possible? I've tried using swimlanes but it did not work for me (guess they do not work in activity diagrams)
I converted part of your diagram to swimlanes. The example with Conductor doesn't make any sense to me, so I reused something from the PlantUML documentation with many if statements. If you enable !pragma userVerticalIf on, the swimlane will become much narrower.
#startuml
skinparam backgroundColor Azure
!pragma useVerticalIf off
|Other|
start
:do something;
fork
:do somethin else;
:bizzfuzz;
fork again
:yep;
:foobarbaz;
end fork
|Conductor|
if (condition A) then (yes)
:Text 1;
elseif (condition B) then (yes)
:Text 2;
stop
elseif (condition C) then (yes)
:Text 3;
elseif (condition D) then (yes)
:Text 4;
else (nothing)
:Text else;
endif
#enduml

How to change the border color of loop group in plantuml?

See, I have a loop in plantuml
loop
etl -> kafka:
kafka -> linda:
linda --> kafka:
kafka --> etl:
end
Now I want to change the border color of loop group from Black to DodgerBlue, neither
skinparam sequence {
SequenceGroupBorderColor DodgerBlue
}
nor
skinparam loop {
SequenceGroupBorderColor DodgerBlue
}
works, the documentation work of PlantUML is so poor, I can hardly find any detail things, how to deal with this work?
The following does work for me:
skinparam Sequence {
GroupBorderColor #ff0000
}
and
skinparam {
SequenceGroupBorderColor #ff0000
}
To get all possible skin parameters use:
java -Djava.awt.headless=true -jar plantuml.jar -language
and browse through the result.
See http://plantuml.com/skinparam for a small (incomplete) description of the skin parameters.
Edit: My initial: "Looks a bit like the color mnemonics don't work (maybe raise an issue at https://forum.plantuml.net/ask)" is not correct. I must have made a typo somewhere. retried with the comment from OP and it did work with DodgerBlue.

How to make vertical switch-case like this?

I want to create an activity diagram in plantuml with some operators selection if elseif (switch - case), which lead to 2 decisions. I created such diagram, but it has horizontal layout.
What i get;
#startuml
start
:start;
if (some question) then (no)
elseif (some question) then (no)
elseif (some question) then (no)
else (yes)
: decision 2;
stop
endif
: decision 1;
stop
#enduml
What i want:
Try putting the yes following the then using nested ifs, like this:
start
:start;
if (some question) then (yes)
if (some question) then (yes)
if (some question) then (yes)
: decision 2;
stop
else (no)
endif
else (no)
endif
else (no)
endif
: decision 1;
stop
That will give you :
This is using the beta/new Activity Diagram Syntax, as your example uses it. I tried to use swimlanes and other features to move decision 1 to the right or left. But was not able to find anything that produced a better output.

Can we format the decision tree part of plantuml with a different color

I'm trying to put a part of the plantuml where there's an if else condition to be in a different color. I know that a plantUml like the following can be used to color the end states , but I'm trying to color the decision part (if block) in the plantUml. Is that possible?
#startuml
if (color it) then (yes)
#Orange: Colored;
else (no)
: Not Colored;
endif;
#enduml
Question is not 100% clear, a picture with what you want might be better.
From what I see a solution:
#startuml
if (color it) then (yes)
#Orange: Colored;
else (no)
#Transparent : Not Colored;
endif;
#enduml
Edit: after revisiting the question I think it is a bit cleared. Might be that you want to use a skinparamer like skinparam ActivityDiamondBackgroundColor green so getting:
#startuml
skinparam ActivityDiamondBackgroundColor green
if (color it) then (yes)
#Orange: Colored;
else (no)
#Transparent : Not Colored;
endif;
#enduml
Resulting in:
As suggested by albert, skinparam ActivityDiamondBackgroundColor can be a solution, but it has a drawback, it updates all if elements in the diagram:
#startuml
skinparam ActivityDiamondBackgroundColor green
if (color it) then (yes)
#Orange: Colored;
else (no)
#Transparent : Not Colored;
endif;
if (default color) then (yea)
:Ok;
endif
#enduml
There is another way to update a background color only under the text in the if diamond:
#startuml
if (<back:green>color it) then (yes)
#Orange: Colored;
else (no)
#Transparent : Not Colored;
endif;
if (default color) then (yea)
:Ok;
endif
#enduml
As for me, this looks sloppy. But this the only way to color the single if-diamond that I am aware of.

NetLogo: 2048 bot optimisation

I am trying to make a Netlogo simulation of a 2048 game. I have implemented three heuristic functions determined by weight parameters and want to use behaviour space to run simulations and check what is the best strategy for winning this game.
Procedure search uses export/import-world primitives to search over possible moves and chooses the move for which the heuristic function has the highest value.
The problem is that this procedure is very slow (due to the import-world function which is being called four times each turn). Do you have any ideas how to implement this without exporting and importing world so often?
This is a project for my Introduction to AI class. It is due in a couple of days and I can't seem to find any solutions.
The relevant part of the code is below. Procedures move-(direction) all work properly and variable moveable? is true if the square can move in said direction and false otherwise. It is checked in procedure moveable-check called by move-(direction).
I would very much appreciate your help. :)
to search
let x 0
let direction "down"
export-world "state.csv"
move-up
ifelse not any? squares with [moveable?]
[set h-value -5000]
[set x h-value
set direction "up"
import-world "state.csv"]
export-world "state.csv"
move-down
ifelse not any? squares with [moveable?]
[set h-value -5000]
[if h-value > x
[set x h-value
set direction "down"]
import-world "state.csv"]
export-world "state.csv"
move-left
ifelse not any? squares with [moveable?]
[set h-value -5000]
[if h-value > x
[set x h-value
set direction "left"]
import-world "state.csv"]
export-world "state.csv"
move-right
ifelse not any? squares with [moveable?]
[set h-value -5000]
[if h-value > x
[set x h-value
set direction "right"]
import-world "state.csv"]
ifelse direction = "up"
[move-up
print "up"]
[ifelse direction = "down"
[move-down
print "down"]
[ifelse direction = "right"
[move-right
print "right"]
[move-left
print "left"]]]
if not any? squares with [moveable?]
[
ask squares [set heading heading + 90]
moveable-check
if not any? squares with [moveable?]
[ask squares [set heading heading + 90]
moveable-check
if not any? squares with [moveable?]
[ask squares [set heading heading + 90]
moveable-check
if not any? squares with [moveable?]
[stop]]]
]
end
The most important, and difficult, information you need to be able to save and restore is the squares. This is pretty easy to do without import-world and export-world (note that the following uses NetLogo 6 syntax; if you're still on NetLogo 5, you'll need to use the old task syntax in the foreach):
to-report serialize-state
report [(list xcor ycor value)] of squares
end
to restore-state [ state ]
clear-squares
foreach state [ [sq] ->
create-squares 1 [
setxy (item 0 sq) (item 1 sq)
set heading 0 ;; or whatever
set value item 2 sq
]
]
end
value above just shows how to store arbitrary variables of your squares. I'm not sure what data you have associated with them or need to restore. The idea behind this code is that you're storing the information about the squares in a list of lists, where each inner list contains the data for one square. The way you use this then is:
let state serialize-state
;; make changes to state that you want to investigate
restore-state state
You may need to store some globals and such as well. Those can be stored in local variables or in the state list (which is more general, but more difficult to implement).
A few other ideas:
Right now it looks like you're only looking one state ahead, and at only one possible position for the new square that's going to be placed (make sure you're not cheating by know where exactly the new square is going to be). Eventually, you may want to do arbitrary look ahead using a kind of tree search. This tree gets really big really fast. If you do that, you'll want to use pruning strategies such as: https://en.wikipedia.org/wiki/Alpha%E2%80%93beta_pruning . Also, that makes the state restoration stuff more difficult, but still doable. You'll be storing a stack of states rather than a single state.
Instead of set heading heading + 90 you can just do right 90 or rt 90.

Resources