Skip to content

Instantly share code, notes, and snippets.

@szktty
Last active January 29, 2025 04:52
Show Gist options
  • Save szktty/d20f83967e1a71a22ceb1612e32edb1f to your computer and use it in GitHub Desktop.
Save szktty/d20f83967e1a71a22ceb1612e32edb1f to your computer and use it in GitHub Desktop.
Plough circular references sample

https://pub.dev/packages/plough

circular references

Graph createGraph() {
  final graph = Graph();

  // Create state nodes
  final init = GraphNode(
    properties: {
      'label': 'Initialize',
      'description': 'Program initialization state',
    },
  );
  final process = GraphNode(
    properties: {
      'label': 'Process Data',
      'description': 'Main data processing state',
    },
  );
  final validate = GraphNode(
    properties: {
      'label': 'Validate',
      'description': 'Data validation state',
    },
  );
  final error = GraphNode(
    properties: {
      'label': 'Error',
      'description': 'Error handling state',
    },
  );
  final complete = GraphNode(
    properties: {
      'label': 'Complete',
      'description': 'Process completion state',
    },
  );

  // Multiple edges between same nodes (different conditions)
  final initToProcess = GraphLink(
    source: init,
    target: process,
    direction: GraphLinkDirection.outgoing,
    properties: {
      'label': 'Start',
      'description': 'Begin normal processing',
    },
  );

  final processToValidate = GraphLink(
    source: process,
    target: validate,
    direction: GraphLinkDirection.outgoing,
    properties: {
      'label': 'Check',
      'description': 'Validate processed data',
    },
  );

  // Alternative path between same nodes
  final processToValidateUrgent = GraphLink(
    source: process,
    target: validate,
    direction: GraphLinkDirection.outgoing,
    properties: {
      'label': 'Priority Check',
      'description': 'Immediate validation for priority data',
    },
  );

  // Circular reference: validate can return to process
  final validateToProcess = GraphLink(
    source: validate,
    target: process,
    direction: GraphLinkDirection.outgoing,
    properties: {
      'label': 'Retry',
      'description': 'Return to processing on validation failure',
    },
  );

  final validateToComplete = GraphLink(
    source: validate,
    target: complete,
    direction: GraphLinkDirection.outgoing,
    properties: {
      'label': 'Success',
      'description': 'Validation successful',
    },
  );

  // Error handling paths
  final processToError = GraphLink(
    source: process,
    target: error,
    direction: GraphLinkDirection.outgoing,
    properties: {
      'label': 'Fail',
      'description': 'Processing error occurred',
    },
  );

  final errorToProcess = GraphLink(
    source: error,
    target: process,
    direction: GraphLinkDirection.outgoing,
    properties: {
      'label': 'Retry',
      'description': 'Retry after error handling',
    },
  );

  // Alternative error recovery path
  final errorToInit = GraphLink(
    source: error,
    target: init,
    direction: GraphLinkDirection.outgoing,
    properties: {
      'label': 'Reset',
      'description': 'Complete reset after critical error',
    },
  );

  // Add nodes
  graph
    ..addNode(init)
    ..addNode(process)
    ..addNode(validate)
    ..addNode(error)
    ..addNode(complete);

  // Add links
  graph
    ..addLink(initToProcess)
    ..addLink(processToValidate)
    ..addLink(processToValidateUrgent)  // Multiple edge example
    ..addLink(validateToProcess)        // Circular reference example
    ..addLink(validateToComplete)
    ..addLink(processToError)
    ..addLink(errorToProcess)           // Another circular reference
    ..addLink(errorToInit);             // Alternative path

  return graph;
}

GraphDefaultNodeRenderer createNodeRenderer(
  BuildContext context,
  GraphNode node,
  Widget? child,
) {
  return GraphDefaultNodeRenderer(
    style: GraphDefaultNodeRendererStyle(shape: GraphDefaultNodeRendererShape.rectangle),
    node: node,
    child: SizedBox(
      child: Center(
        child: Text(
          '${node['label']}',
          style: const TextStyle(fontSize: 14),
        ),
      ),
    ),
  );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment