MCPcopy Create free account
hub / github.com/codemix/graph / convertEdgePattern

Function convertEdgePattern

packages/graph/src/astToSteps.ts:1642–1694  ·  view source on GitHub ↗

* Convert an edge pattern to steps, handling quantifiers for variable-length paths.

(edgePattern: EdgePattern, destNode?: NodePattern)

Source from the content-addressed store, hash-verified

1640 * Convert an edge pattern to steps, handling quantifiers for variable-length paths.
1641 */
1642function convertEdgePattern(edgePattern: EdgePattern, destNode?: NodePattern): Step<any>[] {
1643 // No quantifier - single edge traversal + destination vertex
1644 if (!edgePattern.quantifier) {
1645 const baseStep = new EdgeStep({
1646 direction: edgePattern.direction,
1647 edgeLabels: edgePattern.labels,
1648 stepLabels: edgePattern.variable ? [edgePattern.variable] : undefined,
1649 });
1650
1651 const steps: Step<any>[] = [baseStep];
1652
1653 // Add filter for edge properties if specified
1654 if (edgePattern.properties && Object.keys(edgePattern.properties).length > 0) {
1655 const propertyConditions = Object.entries(edgePattern.properties).map(
1656 ([key, value]) => ["=", key, value] as StepCondition,
1657 );
1658 const condition: StepCondition =
1659 propertyConditions.length === 1
1660 ? propertyConditions[0]!
1661 : (["and", ...propertyConditions] as StepCondition);
1662 steps.push(new FilterElementsStep({ condition }));
1663 }
1664
1665 // Add VertexStep with "other" direction to traverse to the destination vertex
1666 if (destNode) {
1667 steps.push(
1668 new VertexStep({
1669 direction: "other",
1670 edgeLabels: [],
1671 stepLabels: destNode.variable ? [destNode.variable] : undefined,
1672 }),
1673 );
1674 }
1675
1676 return steps;
1677 }
1678
1679 // With quantifier - wrap in RepeatStep with destination node label
1680 const baseStep = new EdgeStep({
1681 direction: edgePattern.direction,
1682 edgeLabels: edgePattern.labels,
1683 stepLabels: edgePattern.variable ? [edgePattern.variable] : undefined,
1684 });
1685
1686 const repeatStep = convertQuantifiedEdge(
1687 baseStep,
1688 edgePattern.quantifier,
1689 destNode?.variable,
1690 edgePattern.properties,
1691 );
1692
1693 return [repeatStep];
1694}
1695
1696/**
1697 * Convert a quantified edge pattern to a RepeatStep.

Callers 1

convertPatternFunction · 0.85

Calls 3

convertQuantifiedEdgeFunction · 0.85
mapMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected