Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

condition node alignment support #15

Merged
merged 5 commits into from
Feb 14, 2022
Merged

Conversation

cdfmlr
Copy link
Owner

@cdfmlr cdfmlr commented Feb 14, 2022

Support ConditionNode alignments, closes #14.

35487f9 v0.2.0 issue-14-align-conds

Support Node.set_param(key, value) to generate flowchart like this:

element(param1=value1,param2=value2)=>start: Start

And as a grammar sugar, support ConditionNode.no_align_next() to set a param align-next=no.

cond2(align-next=no)=>condition: Yes or No?

For convenience, you can also specify this operation when constructing a ConditionNode by:

ConditionNode("a cond node", align_next=False)

(Version 0.2.0 and 0.2.1 do not involve anything function of python code to flowchart, but are features regarding writing flowcharts in python.)

ddc4486 v0.2.1 connect_direction setting for human!

I find it's hard to make something like cond2(yes,right)->io5 when testing the v0.2.0. So I modified some interfaces to make it better.

To set a connection direction, Instead of calling the Node.set_connect_direction(), since v0.2.1 you can just pass a second parameter to connect(), which seems more human:

st = StartNode('a_pyflow_test')
op = OperationNode('do something')
cond1 = ConditionNode('Yes or No?', align_next=False)
cond2 = ConditionNode('True or False?', align_next=False)
cond3 = ConditionNode('1 or 0?', align_next=False)
io1 = InputOutputNode(InputOutputNode.OUTPUT, 'something...')
io2 = InputOutputNode(InputOutputNode.OUTPUT, 'anything...')
io3 = InputOutputNode(InputOutputNode.OUTPUT, 'nothing...')
sub = SubroutineNode('A Subroutine')
e = EndNode('a_pyflow_test')

st.connect(op, 'right')
op.connect(cond1)
cond1.connect_yes(io1, 'right')
cond2.connect_yes(io2, 'right')
cond3.connect_yes(io3, 'right')
cond1.connect_no(cond2)
cond2.connect_no(cond3)
io1.connect(sub, 'right')
io2.connect(sub, 'right')
io3.connect(sub, 'right')
sub.connect(e)

fc = Flowchart(st)
print(fc.flowchart())

Outputs:

st0=>start: start a_pyflow_test
op1=>operation: do something
cond2(align-next=no)=>condition: Yes or No?
io5=>inputoutput: output: something...
sub8=>subroutine: A Subroutine
e9=>end: end a_pyflow_test
cond3(align-next=no)=>condition: True or False?
io6=>inputoutput: output: anything...
cond4(align-next=no)=>condition: 1 or 0?
io7=>inputoutput: output: nothing...

st0(right)->op1
op1->cond2
cond2->
cond2->
cond2(yes, right)->io5
io5(right)->sub8
sub8->e9
cond2(no)->cond3
cond3->
cond3->
cond3(yes, right)->io6
io6(right)->sub8
cond3(no)->cond4
cond4->
cond4(yes, right)->io7
io7(right)->sub8

截屏2022-02-14 10.53.21

4577f9b v0.2.2 conds-align for py2flowchart

Improve the flowchart of consecutive If statements converted from python code with the new feature of v0.2.0:

# example-conds-align.py
if cond1:
	op1
if cond2:
	op2
if cond3:
	op3
op_end

It works fine in v0.1.4 with the default simplify feature:

 python3 -m pyflowchart example.py   
cond3=>operation: op1 if  cond1
cond14=>operation: op2 if  cond2
cond25=>operation: op3 if  cond3
op35=>operation: op_end

cond3->cond14
cond14->cond25
cond25->op35

截屏2022-02-14 10.53.42

But if we run it with --no-simplify:

python3 -m pyflowchart --no-simplify example-condsalign.py
cond3=>condition: if cond1
op7=>operation: op1
cond13=>condition: if cond2
op17=>operation: op2
cond23=>condition: if cond3
op27=>operation: op3
op32=>operation: op_end

cond3(yes)->op7
op7->cond13
cond13(yes)->op17
op17->cond23
cond23(yes)->op27
op27->op32
cond23(no)->op32
cond13(no)->cond23
cond3(no)->cond13

截屏2022-02-14 10.54.51

Ugly, right? No more~! This version offers a new --conds-align flag to beautify these consecutive If statements.

python3 -m pyflowchart --no-simplify --conds-align example.py

or in python:

import pyflowchart
with open('example.py') as f:
	code = f.read()

fc = pyflowchart.Flowchart.from_code(code, simplify=False, conds_align=True)
print(fc.flowchart())

Outputs:

cond3(align-next=no)=>condition: if cond1
op7=>operation: op1
cond13(align-next=no)=>condition: if cond2
op17=>operation: op2
cond23=>condition: if cond3
op27=>operation: op3
op32=>operation: op_end

cond3(yes, right)->op7
op7->cond13
cond13(yes, right)->op17
op17->cond23
cond23(yes, right)->op27
op27->op32
cond23(no)->op32
cond13(no)->cond23
cond3(no)->cond13

截屏2022-02-14 10.55.05

5d4d9e0 v0.2.3 fix a bug

v0.2.1 (ddc4486) does something wrong causing the old way Node.set_connect_direction() invalid. This version fixed it. So you can use both

sub.set_connect_direction("right")
sub.connect(op)

and

sub.connect(op, "right")

On a top level.

38b9828 docs updates

Updates README, docs new features above.

Implement ConditionNode alignment support (#14).
This comment follows bertrandmartel's steps (adrai/flowchart.js#115 (comment)):

1. add ability to put parameters for Node declaration such as:
        element(param1=value1,param2=value2)=>start: Start
    * add Node.params field, a dict for params;
    * add Node.set_param() method to set params;
    * modify Node.fc_definition to write params into generated flowchart.
2. add ability to set `align-next=no` param for ConditionNode:
    * add ConditionNode.no_align_next() to set the param.
    * ConditionNode.__init__() support passing a `align_next=False` to set it.

Closes #14
I find it's hard to make something like `cond2(yes,right)->io5` when testing
the `v0.2.0`. So I modified some interfaces to make it better.

To set a connection direction, Instead of calling the
`Node.set_connect_direction()`, since v0.2.1 you can just pass a second
parameter to `Node.connect()`, which seems more human:

Node.connect() and override methods in subclasses is modified to call
set_connect_direction() if param direction set.

Example:

    st = StartNode('a_pyflow_test')
    op = OperationNode('do something')
    cond1 = ConditionNode('Yes or No?', align_next=False)
    cond2 = ConditionNode('True or False?', align_next=False)
    cond3 = ConditionNode('1 or 0?', align_next=False)
    io1 = InputOutputNode(InputOutputNode.OUTPUT, 'something...')
    io2 = InputOutputNode(InputOutputNode.OUTPUT, 'anything...')
    io3 = InputOutputNode(InputOutputNode.OUTPUT, 'nothing...')
    sub = SubroutineNode('A Subroutine')
    e = EndNode('a_pyflow_test')

    st.connect(op, 'right')
    op.connect(cond1)
    cond1.connect_yes(io1, 'right')
    cond2.connect_yes(io2, 'right')
    cond3.connect_yes(io3, 'right')
    cond1.connect_no(cond2)
    cond2.connect_no(cond3)
    io1.connect(sub, 'right')
    io2.connect(sub, 'right')
    io3.connect(sub, 'right')
    sub.connect(e)

    fc = Flowchart(st)
    print(fc.flowchart())
Improve the flowchart of *consecutive If statements* converted from python
code with the new feature of v0.2.0.

- Flowchart.from_code(): add a `conds_align` param
- CLI: add a `--conds-align` flag
- pass a kwarg `conds_align` into parse() and AstNodes
- parse(): call ConditionNode.no_align_next() for consecutive If statements
  when `conds_align` is Ture.
- If.__init__(): make its yes connect_direction to "right" if there is no Else
  block.
fix a bug from ddc4486:

    connect(Node, direction='')

will overwrite connect_direction any way, makes the old `Node.set_connect_direction()` invalid.

This commit fix it:

- connect() checks direction before set it.
- ConditionNode: setr_param & no_align_next
- set_connect_direction() => connect(Node, direction='')
- Flowchart.from_code add param `conds_align`
- CLI add flag `--conds-align`
- add usage of conds-align
- typos fix
@cdfmlr cdfmlr changed the title #14 condition node alignment support condition node alignment support Feb 14, 2022
@cdfmlr cdfmlr merged commit 94a4150 into master Feb 14, 2022
@cdfmlr cdfmlr deleted the 14-ConditionNode_alignment_support branch October 23, 2023 01:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ConditionNode alignment support
1 participant