File

packages/ast-to-mmd/src/app/graph-blocks/if-else-declaration.graph-block.ts

Extends

BlockDeclarationGraphBlock

Index

Properties
Methods
Accessors

Constructor

constructor(id: string, condition: string, thanBlock: GraphBlock[], elseBlock?: GraphBlock[])
Parameters :
Name Type Optional
id string No
condition string No
thanBlock GraphBlock[] No
elseBlock GraphBlock[] Yes

Properties

Private Static CONDITION_COUNTER
Type : number
Default value : 0
Private conditionID
Default value : IfElseDeclarationGraphBlock.CONDITION_COUNTER++
Private _lazyDependencies
Type : string[]
Default value : []
Inherited from GraphBlock
Defined in GraphBlock:30

Lazy dependencies ready to render when this node is rendered.

Private _parent
Type : GraphBlock | undefined
Default value : undefined
Inherited from GraphBlock
Defined in GraphBlock:25

Parent of current node in graph. Can be undefined.

Private _parentSiblingCondition
Type : SiblingCondition
Inherited from GraphBlock
Defined in GraphBlock:39

SiblingCondition for finding correct parent.

Private _siblingCondition
Type : SiblingCondition
Inherited from GraphBlock
Defined in GraphBlock:35
Private Static Readonly EMPTY_LINE_RENDERER_MODIFIER
Type : LineRendererModifier
Default value : () => {...}
Inherited from GraphBlock
Defined in GraphBlock:20

Empty implementation of LineRendererModifier.

Public Readonly id
Type : string
Inherited from GraphBlock
Defined in GraphBlock:46
Unique ID in the node graph.
Private Static Readonly POSITIVE_SIBLING_CONDITION
Type : SiblingCondition
Default value : new PositiveSiblingCondition()
Inherited from GraphBlock
Defined in GraphBlock:16

Positive sibling condition default for all GraphBlock.

Private Static Readonly SHAPE_RENDERER
Type : ShapeRenderer
Default value : new ShapeRenderer()
Inherited from GraphBlock
Defined in GraphBlock:12

Shape renderer for generating different shapes in graph.

Methods

Private __renderDirectDependencies
__renderDirectDependencies(indent: number, children: GraphBlock, builderModifier: LineRendererModifier)
Parameters :
Name Type Optional
indent number No
children GraphBlock No
builderModifier LineRendererModifier No
Returns : string
Protected negativeBuilderModifier
negativeBuilderModifier(builder: LineRenderer)
Parameters :
Name Type Optional
builder LineRenderer No
Returns : LineRenderer
Protected positiveBuilderModifier
positiveBuilderModifier(builder: LineRenderer)
Parameters :
Name Type Optional
builder LineRenderer No
Returns : LineRenderer
Public render
render(_indent: number)
Inherited from GraphBlock
Defined in GraphBlock:21
Parameters :
Name Type Optional
_indent number No
Returns : string
Protected renderDirectDependencies
renderDirectDependencies(indent: number)
Parameters :
Name Type Optional
indent number No
Returns : string
Protected renderOuterBody
renderOuterBody(indent: number)
Parameters :
Name Type Optional
indent number No
Returns : string
Protected createLineBuilder
createLineBuilder(lhsId: string, rhsId: string)
Inherited from GraphBlock
Defined in GraphBlock:71
Parameters :
Name Type Optional
lhsId string No
rhsId string No
Returns : LineRenderer
Protected renderChildren
renderChildren(indent: number, children: GraphBlock[])
Parameters :
Name Type Optional
indent number No
children GraphBlock[] No
Returns : string
Protected renderDependencies
renderDependencies(_indent: number)
Parameters :
Name Type Optional
_indent number No
Returns : string
Private __multiFindChildIndex
__multiFindChildIndex(children: GraphBlock[][], child: GraphBlock)
Inherited from GraphBlock
Defined in GraphBlock:575

Private method for finding a child index of defined block in a multidimensional array of children.

Parameters :
Name Type Optional Description
children GraphBlock[][] No

[] Multidimensional array of children including the child.

child GraphBlock No

GraphBlock Wanted child.

Returns : | undefined

Coordinates of child block or undefined when block is not found.

Private __renderLines
__renderLines(indent: number, id: string, ids: string[], direction: "left" | "right", builderModifier: LineRendererModifier[])
Inherited from GraphBlock
Defined in GraphBlock:546

Private method for rendering lines between nodes in defined directions.

Parameters :
Name Type Optional Description
indent number No

Indentation from start of the line.

id string No

ID of one side of connection.

ids string[] No

Array of IDs of end of a connection.

direction "left" | "right" No

Direction of a line.

builderModifier LineRendererModifier[] No

Optional modifier of current line.

Returns : string

Rendered line.

Protected _assignParent
_assignParent(children: GraphBlock[])
Inherited from GraphBlock
Defined in GraphBlock:289

Helper method for assigning this parent into each children.

Parameters :
Name Type Optional Description
children GraphBlock[] No

[]GraphBlock Children where this parent is going to be assigned.

Returns : void
Protected _filterChildren
_filterChildren(children: GraphBlock[])
Inherited from GraphBlock
Defined in GraphBlock:466

Helper method for filtering array of children.

Parameters :
Name Type Optional Description
children GraphBlock[] No

[]GraphBlock Array of children.

Returns : GraphBlock[]
Protected _findParentBySiblingCondition
_findParentBySiblingCondition(child: GraphBlock, siblingCondition: SiblingCondition)
Inherited from GraphBlock
Defined in GraphBlock:521

Helper method for finding an indirect parent of defined child by SiblingCondition.

Parameters :
Name Type Optional Description
child GraphBlock No

GraphBlock Child whose parent is wanted.

siblingCondition SiblingCondition No

SiblingCondition.

Protected _findSiblingChild
_findSiblingChild(child: GraphBlock, parentSiblingCondition: SiblingCondition, siblingCondition: SiblingCondition)
Inherited from GraphBlock
Defined in GraphBlock:478

Helper method for finding a sibling of defined child in parents children.

Parameters :
Name Type Optional Default value Description
child GraphBlock No

GraphBlock Node which for sibling is looked for.

parentSiblingCondition SiblingCondition No this.siblingCondition

SiblingCondition.

siblingCondition SiblingCondition No this.siblingCondition

SiblingCondition.

Protected _generateSpace
_generateSpace(count: number)
Inherited from GraphBlock
Defined in GraphBlock:301

Helper method for generating space.

Parameters :
Name Type Optional Description
count number No

Count of space.

Returns : string

Space of defined length.

Protected _multiFilterChildren
_multiFilterChildren(children: GraphBlock[][])
Inherited from GraphBlock
Defined in GraphBlock:456

Helper method for filtering children in multidimensional array of children.

Parameters :
Name Type Optional Description
children GraphBlock[][] No

[] Array of children.

Returns : [][]
Protected _renderConnectionWithParent
_renderConnectionWithParent(indent: number, children: GraphBlock, lastIds: string[], priorityBuilderModifier: LineRendererModifier[])
Inherited from GraphBlock
Defined in GraphBlock:410
Parameters :
Name Type Optional
indent number No
children GraphBlock No
lastIds string[] No
priorityBuilderModifier LineRendererModifier[] No
Returns : string
Protected _renderDependencies
_renderDependencies(indent: number, blocks: GraphBlock[])
Inherited from GraphBlock
Defined in GraphBlock:346

Helper method for rendering dependencies with defined indent between blocks.

Parameters :
Name Type Optional Description
indent number No

Indentation from start of the line.

blocks GraphBlock[] No

[]GraphBlock Blocks to render dependencies between each node.

Returns : string

Dependencies of blocks.

Protected _renderLine
_renderLine(indent: number, lhsId: string, rhsId: string, builderModifier: LineRendererModifier[])
Inherited from GraphBlock
Defined in GraphBlock:314

Helper method for generating line between two nodes.

Parameters :
Name Type Optional Description
indent number No

Indentation from start of the line.

lhsId string No

ID of start node.

rhsId string No

ID of end node.

builderModifier LineRendererModifier[] No
Returns : string

Line definition between two nodes.

Protected _renderLinesL2R
_renderLinesL2R(indent: number, toId: string, fromIds: string[], builderModifier: LineRendererModifier[])
Inherited from GraphBlock
Defined in GraphBlock:383

Helper method for rendering line starting in one node and ending in multiple nodes.

Parameters :
Name Type Optional Description
indent number No

Indentation from start of the line.

toId string No

ID of start node.

fromIds string[] No

Array of end nodes.

builderModifier LineRendererModifier[] No

Optional modifier of current line.

Returns : string

Rendered line.

Protected _renderLinesR2L
_renderLinesR2L(indent: number, fromId: string, toIds: string[], builderModifier: LineRendererModifier[])
Inherited from GraphBlock
Defined in GraphBlock:401

Helper method for rendering line starting in multiple nodes and ending in one node.

Parameters :
Name Type Optional Description
indent number No

Indentation from start of the line.

fromId string No

Array of start nodes.

toIds string[] No

ID of end node.

builderModifier LineRendererModifier[] No

Optional modifier of current line.

Returns : string

Rendered line.

Protected _renderShape
_renderShape(text: string, shape: Shape)
Inherited from GraphBlock
Defined in GraphBlock:335

Helper method for rendering defined shape with text.

Parameters :
Name Type Optional Description
text string No

Text in shape.

shape Shape No

Shape Type of shape.

Returns : string

Rendered shape with text.

Protected renderInSubgraph
renderInSubgraph(_indent: number, id: string, text: string, bodyRenderer: (indent: number) => void, direction: "TB" | "TD" | "BT" | "LR" | "RL")
Inherited from GraphBlock
Defined in GraphBlock:86

Renders current node wrapped in a subgraph.

Direction options:

  • TB - top to bottom (default)
  • TD - top-down / same as top to bottom
  • BT - bottom to top
  • LR - left to rignt
  • RL - right to left
Parameters :
Name Type Optional Default value Description
_indent number No

Indentation from start of the line.

id string No

ID of a subgraph.

text string No

Name of a subgraph.

bodyRenderer function No

Callback responsible for rendering real node content.

direction "TB" | "TD" | "BT" | "LR" | "RL" No 'TB'

Direction of a subgraph.

Returns : string

Graph wrapped in a subgraph.

Public renderLazyDependencies
renderLazyDependencies(_indent: number)
Inherited from GraphBlock
Defined in GraphBlock:63

Renders lazy dependencies of current node.

Parameters :
Name Type Optional Description
_indent number No

Indentiation from start of the line.

Returns : string
Public toString
toString()
Inherited from GraphBlock
Defined in GraphBlock:589
Returns : string

Accessors

blockKind
getblockKind()
firstBlock
getfirstBlock()
lastBlocks
getlastBlocks()
hasChildren
gethasChildren()
children
getchildren()
allowUnwrapChildren
getallowUnwrapChildren()

Conditions

Example of rendered simple condition with functions before and after

Conditions

Example of rendered condition without else

Conditions

Example of rendered nested conditions with functions before and after

Conditions

Example of rendered nested conditions with functions before and after and return statements

Conditions

Example of rendered condition with return when positive

Conditions

Example of rendered condition with return when negative

Conditions

Example of rendered condition with throw when positive

Conditions

Example of rendered condition with throw when positive without continue

Conditions

Example of rendered condition with throw when negative

Conditions

import { BlockKind } from '../block.kind';
import { BlockDeclarationGraphBlock } from './block-declaration.graph-block';
import { GraphBlock } from './graph-block';
import { LineRenderer, LineRendererModifier, Shape } from './renderer';

export class IfElseDeclarationGraphBlock extends BlockDeclarationGraphBlock {
  private static CONDITION_COUNTER = 0;

  private conditionID = IfElseDeclarationGraphBlock.CONDITION_COUNTER++;

  constructor(
    id: string,
    private readonly condition: string,
    private readonly thanBlock: GraphBlock[],
    private readonly elseBlock?: GraphBlock[]
  ) {
    super(id, thanBlock);
    this._assignParent(elseBlock || []);
  }

  public override render(_indent: number): string {
    return this.renderInSubgraph(
      _indent,
      `${this.id}_${this.conditionID}`,
      `CONDITION_${this.conditionID}`,
      (bodyIndent) => this.renderOuterBody(bodyIndent)
    );
  }

  public override get blockKind(): BlockKind {
    return BlockKind.IF_ELSE_DECLARATION;
  }

  override get firstBlock(): GraphBlock {
    return this;
  }

  public override get lastBlocks(): string[] {
    const thanBlockId = super.lastBlocks;
    const visibleElseBlocks = this.elseBlock ? this._filterChildren(this.elseBlock) : [];
    let elseBlockId: string[];
    if (visibleElseBlocks.length === 0) {
      elseBlockId = [];
    } else {
      elseBlockId = visibleElseBlocks[visibleElseBlocks.length - 1].lastBlocks;
    }
    return [...thanBlockId, ...elseBlockId];
  }

  public override get hasChildren(): boolean {
    return this.thanBlock.length !== 0;
  }

  public override get children(): GraphBlock[][] {
    const filteredThanBlock = this._filterChildren(this.thanBlock);
    const filteredElseBlock = this.elseBlock ? this._filterChildren(this.elseBlock) : [];
    const result: GraphBlock[][] = [];

    /**
     * Local helper function for unified pushing direct children into a result
     *
     * @param child {@link GraphBlock}
     * @param result {@link GraphBlock[][]}
     */
    function pushDirectChildren(child: GraphBlock, result: GraphBlock[][]): void {
      const indirectChildren = child.children;
      if (indirectChildren.length !== 0 && child.allowUnwrapChildren) {
        result.push(...indirectChildren);
      } else {
        result.push([child]);
      }
    }

    if (filteredThanBlock.length !== 0) {
      const lastDirectChild = filteredThanBlock[filteredThanBlock.length - 1];
      pushDirectChildren(lastDirectChild, result);
    }

    if (filteredElseBlock.length !== 0) {
      const lastDirectChild = filteredElseBlock[filteredElseBlock.length - 1];
      pushDirectChildren(lastDirectChild, result);
    }

    return result;
  }

  override get allowUnwrapChildren(): boolean {
    return false;
  }

  protected renderOuterBody(indent: number): string {
    return `
${this._generateSpace(indent + 1)}${this.id}${this._renderShape(this.condition, Shape.RHOMBUS)}
${this.renderChildren(indent, this.thanBlock)}
${this.renderChildren(indent, this.elseBlock || [])}
${this.renderDependencies(indent)}
${this._renderDependencies(indent, this.elseBlock || [])}
${this.renderDirectDependencies(indent + 1)}
`;
  }

  protected renderDirectDependencies(indent: number): string {
    let dependencies = '';
    const filteredThanBlock = this._filterChildren(this.thanBlock);
    if (filteredThanBlock.length !== 0) {
      const firstChild: GraphBlock = filteredThanBlock[0];
      dependencies += this.__renderDirectDependencies(indent, firstChild, this.positiveBuilderModifier);

      dependencies += '\n';
    }
    if (this.elseBlock) {
      const filteredElseBlock = this._filterChildren(this.elseBlock);
      if (filteredElseBlock.length !== 0) {
        const firstChild: GraphBlock = filteredElseBlock[0];
        dependencies += this.__renderDirectDependencies(indent, firstChild, this.negativeBuilderModifier);
      } else {
        if (this.parent) {
          const siblingChild: GraphBlock | undefined = this._findSiblingChild(this);
          if (siblingChild) {
            siblingChild.lazyDependency = this._renderLine(0, this.id, siblingChild.id, this.negativeBuilderModifier);
          }
        }
      }
    } else {
      if (this.parent) {
        const siblingChild: GraphBlock | undefined = this._findSiblingChild(this);
        if (siblingChild) {
          siblingChild.lazyDependency = this._renderLine(0, this.id, siblingChild.id, this.negativeBuilderModifier);
        }
      }
    }

    return dependencies;
  }

  protected positiveBuilderModifier(builder: LineRenderer): LineRenderer {
    return builder.setConnectionDescription('Yes');
  }

  protected negativeBuilderModifier(builder: LineRenderer): LineRenderer {
    return builder.setConnectionDescription('No');
  }

  private __renderDirectDependencies(indent: number, children: GraphBlock, builderModifier: LineRendererModifier): string {
    let dependencies = '';
    if (!children.isDependencyBridge) {
      dependencies += this._renderLine(indent, this.id, children.firstBlock.id, builderModifier);
    }
    if (children.skipRenderRestDependencies) {
      dependencies += this._renderConnectionWithParent(indent, children, [this.id], builderModifier);
    }

    return dependencies;
  }

}

results matching ""

    No results matching ""