How to make vertical switch-case like this? - plantuml

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.

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 remove empty states from Activity Diagram in 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

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.

How to break while-loop in VFP (FoxPro) by button click?

I'm trying to make simple FoxPro application with two buttons. First button starts while-loop and works correctly. Second button must break this loop. But I can't find any correctly working solution.
There are loop code:
DO WHILE !thisForm.cancelpressed
IF thisForm.cancelpressed
EXIT
ENDIF
randNum=VAL(TRIM(STR(ROUND( 3*RAND(), 1))))
&&oneVar=VAL("1")
IF randNum = VAL("1")
ThisForm.shape1.BackColor = greenColor
ThisForm.shape2.BackColor = orangeColor
ThisForm.shape3.BackColor = redColor
ENDIF
cs = seconds()
IF abs(cs - seconds()) >= 15
doevents
cs = seconds()
ENDIF
IF thisForm.cancelpressed
EXIT
ENDIF
DOEVENTS
WAIT TIMEOUT 1
ENDDO
This code is in the first button Click method. Form object have user-defined 'cancelpressed' property. In the second button click method I put this:
thisForm.cancelpressed = .T.
Thisform.Release()
Clear Events
But it not work. When loop started it dont want to listen any commands and clicks, and it dont let to close the program. Please help if you can
You could try setting
Application.AutoYield = .T.
as per https://msdn.microsoft.com/en-us/library/aa977324(v=vs.71).aspx
But if that doesn't work then I think you are stuck as VFP is not a true multithreaded application. You might be able to get around the problem using a timer object as per http://fox.wikis.com/wc.dll?Wiki~TimerExample or using a multi threading workaround from https://blogs.msdn.microsoft.com/calvin_hsia/2006/05/16/create-multiple-threads-from-within-your-application/ but you would be very much on your own.
I have to say that starting a new application in VFP today probably isn't the best idea. There are plenty of current alternatives.
The problem is with your wait window. Remove it.
Local greenColor, orangeColor, redColor
m.greenColor = 0x00FF00
m.orangeColor = 0x3060FF
m.redColor = 0xFF0000
Do While !Thisform.cancelpressed
randNum=Val(Trim(Str(Round( 3*Rand(), 1))))
If m.randNum = Val("1")
Thisform.shape1.BackColor = m.greenColor
Thisform.shape2.BackColor = m.orangeColor
Thisform.shape3.BackColor = m.redColor
Endif
Doevents
Enddo
(I don't understand why you are doing such a weird randNum setting and check nor the purpose of setting the same colours again and again but it is not the point in this question)
Normally you should prefer using a Timer with a code like this.
And BTW this has nothing to do with multithreading neither in VFP nor in another language. You are simply doing a loop with a cancel flag check. Updating the UI, say in C#, with the same approach wouldn't be any easier.

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