Skip to content

Add Cypher query language support#147

Merged
villelaitila merged 3 commits intosoftagram:mainfrom
villelaitila:feature/cypher-query-support
Mar 17, 2026
Merged

Add Cypher query language support#147
villelaitila merged 3 commits intosoftagram:mainfrom
villelaitila:feature/cypher-query-support

Conversation

@villelaitila
Copy link
Contributor

Summary

  • Add openCypher query support for sgraph models via a pluggable sPyCy backend (src/sgraph/cypher.py)
  • Document graph conventions for software architecture and genealogy domains (docs/graph-conventions.md)
  • Full documentation with examples, CLI reference, and feature matrix (docs/cypher.md)

What it does

Enables querying sgraph models with Cypher pattern matching:

-- Find transitive dependencies
MATCH (a:file)-[:imports|function_ref*1..3]->(b)
RETURN DISTINCT a.name, b.name

-- Trace ancestors in a genealogy model
MATCH (p)-[:parent*1..3]->(ancestor)
WHERE p.name CONTAINS 'Pekka'
RETURN ancestor.name

Python API:

from sgraph.cypher import cypher_query
results = cypher_query(model, 'MATCH (n:file) RETURN n.name')

CLI with interactive REPL:

python -m sgraph.cypher model.xml.zip                          # REPL
python -m sgraph.cypher model.xml.zip 'MATCH ...' -f csv       # single query
python -m sgraph.cypher model.xml.zip -f dot 'MATCH ... RETURN a, r, b' | dot -Tpng -o out.png

11 output formats: table, csv, tsv, json, jsonl, xml, deps, dot, plantuml, graphml, cytoscape. Graph formats extract a subgraph from query results and export via existing sgraph converters.

Design

The SGraphCypherBackend maps sgraph's dual-layer model (element tree + association edges) to Cypher's labeled property graph:

sgraph Cypher
Element type attr Node label (:file, :class)
Association deptype Relationship type (:imports, :inc)
Parent-child tree :CONTAINS relationships (optional)
Element attrs + name/path Node properties

Optional dependency: pip install spycy-aneeshdurg

Test plan

  • Tabular queries: MATCH, WHERE, RETURN, ORDER BY, LIMIT, aggregations
  • Variable-length paths (*1..N), OPTIONAL MATCH, WITH, UNION
  • Label and property filtering
  • All 5 tabular output formats (table, csv, tsv, json, jsonl)
  • All 6 graph output formats (xml, deps, dot, plantuml, graphml, cytoscape)
  • Subgraph extraction preserves element types, attributes, and associations
  • CLI error handling (bad query, missing file)
  • Interactive REPL with \format switching
  • Both software architecture and genealogy conventions

Introduce openCypher query support via a pluggable sPyCy backend, enabling
graph pattern matching directly against sgraph's hierarchical data model.

The implementation bridges sgraph's dual-layer structure (element tree +
association edges) to Cypher's labeled property graph model:
- SElement type attributes become node labels (:file, :class, :function)
- SElementAssociation deptype becomes relationship types (:imports, :inc)
- Parent-child hierarchy is optionally exposed as :CONTAINS relationships
- Element attrs and computed name/path are exposed as node properties

Key components:

  src/sgraph/cypher.py
  - SGraphCypherBackend: read-only sPyCy Graph ABC implementation
  - cypher_query(): one-call API returning pandas DataFrames
  - Subgraph extraction from query results back to SGraph models
  - CLI with interactive REPL (python -m sgraph.cypher)

  CLI output formats:
  - Tabular: table, csv, tsv, json, jsonl
  - Graph: xml, deps, dot, plantuml, graphml, cytoscape
    (graph formats reconstruct an SGraph subgraph from matched
    Node/Edge objects and export via existing converters)

  docs/graph-conventions.md
  - Documents the software architecture convention (project element,
    External subtree, element types, association types, hierarchy
    semantics, path format)
  - Documents the genealogy convention (flat structure, person naming,
    parent associations)
  - Defines what a convention must specify for meaningful queries

  docs/cypher.md
  - Full documentation with Python API, CLI usage, query examples
    for both software and genealogy conventions, output format
    reference, and supported Cypher feature matrix

Optional dependency: pip install spycy-aneeshdurg
Optional dependency for the Cypher query module (python -m sgraph.cypher).
Audited: no CVEs, no postinstall hooks, no network/subprocess calls,
MIT license. Its deps (numpy, pandas) already overlap with sgraph's own.
Document the Cypher module in developer docs (CLAUDE.md) and user-facing
README. Includes Python API example, CLI usage, and links to full docs.
@villelaitila villelaitila merged commit 2d398f4 into softagram:main Mar 17, 2026
1 check passed
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.

1 participant