MaterialXLab API  0.0.1
APIs For MaterialXLab Libraries
Loading...
Searching...
No Matches
MxMermaidGraphExporter Class Reference

Class which will export a graph to Mermaid format. More...

Public Member Functions

 constructor (graphDictionary, connections)
 Constructor.
 
 setOrientation (orientation)
 Set the orientation of the graph.
 
 setEmitCategory (emitCategory)
 Set the emit the category versus the node name.
 
 setEmitType (emitType)
 Emit the type of each node in the graph.
 
 sanitizeString (path)
 Sanitize the a node path to be safe to use with Mermaid.
 
 execute ()
 Build the graph.
 
 write (filename)
 Write the graph to a file.
 
 getGraph (wrap=true)
 Get the graph wrapped in a code block if desired.
 
 export ()
 

Detailed Description

Class which will export a graph to Mermaid format.

Definition at line 471 of file JsMaterialXGraph.js.

Member Function Documentation

◆ constructor()

MxMermaidGraphExporter::constructor ( graphDictionary,
connections )

Constructor.

Parameters
graphDictionarySet of graph elements organized by node graph
connectionsSet of connections between graph elements
Returns
{void}

Definition at line 479 of file JsMaterialXGraph.js.

479 {
480 this.graphDictionary = graphDictionary;
481 this.connections = connections;
482 this.mermaid = [];
483 this.orientation = 'LR';
484 this.emitCategory = false;
485 this.emitType = false;
486 }

◆ execute()

MxMermaidGraphExporter::execute ( )

Build the graph.

Scans the graph dictionary to create the list of nodes per graph and then scans the connections to create the list of connections between nodes. Specific shapes and colours are assigned for different node types.

Returns
Mermaid graph

Definition at line 537 of file JsMaterialXGraph.js.

537 {
538 let mermaid = [];
539 mermaid.push(`graph ${this.orientation}`);
540
541 for (let graphPath in this.graphDictionary) {
542 let isSubgraph = graphPath !== '';
543 if (isSubgraph) {
544 mermaid.push(` subgraph ${graphPath}`);
545 }
546
547 for (let item of this.graphDictionary[graphPath]) {
548 let path = item[0];
549 let label = path.split('/').pop();
550 // Sanitize the path name
551 path = this.sanitizeString(path)
552
553 if (this.emitCategory) {
554 label = item[1];
555 }
556
557 if (this.emitType) {
558 label += `:${item[2]}`;
559 }
560
561 if (item[3]) {
562 label += `:${item[3]}`;
563 }
564
565 if (['input', 'output'].includes(item[1])) {
566 mermaid.push(` ${path}([${label}])`);
567 mermaid.push(` style ${path} fill:#09D, color:#FFF`);
568 } else if (item[1] === 'surfacematerial') {
569 mermaid.push(` ${path}([${label}])`);
570 mermaid.push(` style ${path} fill:#090, color:#FFF`);
571 } else if (item[1] === 'nodedef') {
572 mermaid.push(` ${path}[[${label}]]`);
573 //mermaid.push(` style ${path} fill:#02F, color:#FFF`);
574 } else if (['ifequal', 'ifgreatereq', 'switch'].includes(item[1])) {
575 mermaid.push(` ${path}{${label}}`);
576 mermaid.push(` style ${path} fill:#C72, color:#FFF`);
577 } else if (item[1] === 'token') {
578 mermaid.push(` ${path}{{${label}}}`);
579 mermaid.push(` style ${path} fill:#222, color:#FFF`);
580 } else if (item[1] === 'constant') {
581 mermaid.push(` ${path}([${label}])`);
582 mermaid.push(` style ${path} fill:#888, color:#000`);
583 } else {
584 mermaid.push(` ${path}[${label}]`);
585 }
586 }
587
588 if (isSubgraph) {
589 mermaid.push(' end');
590 }
591 }
592
593 this.mermaid = mermaid;
594
595 for (let connection of this.connections)
596 {
597 // Sanitize path names
598 connection[0] = this.sanitizeString(connection[0])
599 connection[2] = this.sanitizeString(connection[2])
600
601 let source = connection[0];
602 let dest = connection[2];
603 let edge = '';
604
605 if (connection[1].length > 0) {
606 if (connection[3].length > 0) {
607 edge = `${connection[1]}-->${connection[3]}`;
608 } else {
609 edge = connection[1];
610 }
611 } else {
612 edge = connection[3];
613 }
614
615 let connectString = '';
616
617 if (connection[4] === 'value') {
618 let sourceNode = source.split('/').pop();
619 connectString = edge.length > 0
620 ? ` ${sourceNode}["${source}"] --${edge}--> ${dest}`
621 : ` ${sourceNode}["${source}"] --> ${dest}`;
622 } else {
623 connectString = edge.length > 0
624 ? ` ${source} --"${edge}"--> ${dest}`
625 : ` ${source} --> ${dest}`;
626 }
627
628 mermaid.push(connectString);
629 }
630
631 return mermaid;
632 }
sanitizeString(path)
Sanitize the a node path to be safe to use with Mermaid.

◆ export()

MxMermaidGraphExporter::export ( )

Definition at line 657 of file JsMaterialXGraph.js.

657 {
658 return this.getGraph();
659 }
getGraph(wrap=true)
Get the graph wrapped in a code block if desired.

◆ getGraph()

MxMermaidGraphExporter::getGraph ( wrap = true)

Get the graph wrapped in a code block if desired.

Parameters
wrapWrap the graph in a Markdown code block

Definition at line 648 of file JsMaterialXGraph.js.

648 {
649 let result = wrap
650 ? '```mermaid\n' + this.mermaid.join('\n') + '\n```'
651 : this.mermaid.join('\n');
652
653 result = result.replace('/default', '/default1');
654 return result;
655 }

◆ sanitizeString()

MxMermaidGraphExporter::sanitizeString ( path)

Sanitize the a node path to be safe to use with Mermaid.

Parameters
pathOriginal path
Returns
sanitized path

Definition at line 520 of file JsMaterialXGraph.js.

521 {
522 path = path.replace('/default', '/default1');
523 path = path.replace('/', '_');
524 path = path.replace(' ', '_');
525 return path;
526 }

◆ setEmitCategory()

MxMermaidGraphExporter::setEmitCategory ( emitCategory)

Set the emit the category versus the node name.

Parameters
emitCategory
Returns
{void}

Definition at line 502 of file JsMaterialXGraph.js.

502 {
503 this.emitCategory = emitCategory;
504 }

◆ setEmitType()

MxMermaidGraphExporter::setEmitType ( emitType)

Emit the type of each node in the graph.

Parameters
emitType
Returns
{void}

Definition at line 511 of file JsMaterialXGraph.js.

511 {
512 this.emitType = emitType;
513 }

◆ setOrientation()

MxMermaidGraphExporter::setOrientation ( orientation)

Set the orientation of the graph.

Parameters
orientation
Returns
{void}

Definition at line 493 of file JsMaterialXGraph.js.

493 {
494 this.orientation = orientation;
495 }

◆ write()

MxMermaidGraphExporter::write ( filename)

Write the graph to a file.

Parameters
filename
Returns
{void}

Definition at line 639 of file JsMaterialXGraph.js.

639 {
640 let fs = require('fs');
641 fs.writeFileSync(filename, this.export());
642 }

The documentation for this class was generated from the following file: