Skip to main content

sgraph hierarchic graph data structure, format and algorithms

Project description

sgraph

sgraph contains data format, structures and algorithms to work with hierarchic graph structures. Typically it is suitable for representing software architectures.

Documentation: https://softagram.github.io/sgraph/

See also sgraph-mcp-server for enabling AI agents to utilize sgraph information.

Install

pip install sgraph

Contributions

The project is welcoming all contributions.

Core Ontology

A model, SGraph consists of a root SElement, which may have children of the same type (as in XML). Attribute information can be stored via key-value pairs into them. The SElement objects can be connected together via SElementAssociation objects.

Example model

nginx model has an nginx root element that represents the main directory. Inside it, there is a src element. And inside src, there is core.

https://github.com/nginx/nginx/tree/master/src inside core, there are several elements, e.g. nginx.c and nginx.h

https://github.com/nginx/nginx/blob/master/src/core/nginx.c

Because nginx.c contains #include directive to nginx.h, in the model it is formulated so that there is a relationship (also called as association) from nginx.c element to nginx.h

To make model more explicit, that particular relationship should be annotated with type "inc" to describe the dependency type.

It is also possible to have other attributes assigned to relationships other than type but typically this is rare.

XML format

In XML dataformat, minimalism is the goal to make it simple and clean. Integers are used as unique identifiers for the elements. In the example case, the nginx.h element is assigned with ID 2 and the relationship that is inside nginx.c refers this way to nginx.h

This integer reference system has been designed to make the data format highly performing even with 10 million element models.

Deps data format - line based simple format for easy scripting

In Deps data format (usually a .txt file), the above model can be described minimally this way:

/nginx/src/core/nginx.c:/nginx/src/core/nginx.h:inc

Although this might seem very compelling data format to use, it is not recommended for very large models, e.g. 10 million elements.

Using the API

Creating a simple model:

>>> from sgraph import SGraph
>>> from sgraph import SElement
>>> from sgraph import SElementAssociation
>>> x = SGraph(SElement(None, ''))
>>> x
<SGraph empty elements=0 id=0x7f2efae9ad30>

>>> x.to_deps(fname=None)

>>> e1 = x.createOrGetElementFromPath('/path/to/file.x')
>>> e2 = x.createOrGetElementFromPath('/path/to/file.y')
>>> x.to_deps(fname=None)
/path
/path/to
/path/to/file.x
/path/to/file.y

>>> x.to_xml(fname=None)
<model version="2.1">
  <elements>
  <e n="path" >
    <e n="to" >
      <e n="file.x" >
      </e>
      <e n="file.y" >
      </e>
    </e>
  </e>
</elements>
</model>

>>> ea = SElementAssociation(e1, e2, 'use')
>>> ea.initElems()  # ea is not connected to the model before this call.
>>> x.to_deps(fname=None)
/path/to/file.x:/path/to/file.y:use
/path
/path/to
>>>

>>> x.to_xml(fname=None)
<model version="2.1">
  <elements>
  <e n="path" >
    <e n="to" >
      <e n="file.x" >
        <r r="2" t="use" />
      </e>
      <e i="2" n="file.y" >
      </e>
    </e>
  </e>
 </elements>
</model>

Querying with Cypher

Models can be queried using the openCypher graph query language (requires optional dependency spycy-aneeshdurg):

from sgraph import SGraph
from sgraph.cypher import cypher_query

model = SGraph.parse_xml_or_zipped_xml('model.xml')
results = cypher_query(model, 'MATCH (a)-[r:inc]->(b) RETURN a.name, b.name')

A CLI with interactive REPL is also available:

pip install spycy-aneeshdurg
python -m sgraph.cypher model.xml.zip 'MATCH (n:file) RETURN n.name'   # single query
python -m sgraph.cypher model.xml.zip                                   # interactive REPL
python -m sgraph.cypher model.xml.zip -f dot 'MATCH (a)-[r]->(b) RETURN a, r, b' | dot -Tpng -o graph.png

See the Cypher documentation for full details and query examples.

Comparing models

Two models can be compared to see what was added, removed, or changed:

from sgraph.compare.modelcompare import ModelCompare

mc = ModelCompare()
compare_model = mc.compare('old_model.xml', 'new_model.xml')  # returns an SGraph
mc.printCompareInfos(compare_model)

A CLI is also available (exit codes follow git diff: 0 = no differences, 1 = differences, 2 = error):

python -m sgraph.cli.compare old_model.xml new_model.xml            # human-readable summary
python -m sgraph.cli.compare old_model.xml new_model.xml -f json    # machine-readable JSON
python -m sgraph.cli.compare old_model.xml new_model.xml --rename-detection

See the API reference for the full comparison API.

Current utilization

Softagram uses it for building up the information model about the analyzed software.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sgraph-1.6.1.tar.gz (104.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sgraph-1.6.1-py3-none-any.whl (116.1 kB view details)

Uploaded Python 3

File details

Details for the file sgraph-1.6.1.tar.gz.

File metadata

  • Download URL: sgraph-1.6.1.tar.gz
  • Upload date:
  • Size: 104.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.13

File hashes

Hashes for sgraph-1.6.1.tar.gz
Algorithm Hash digest
SHA256 b5312fb62e775c4fbf48b00542b168498e3019f452dac7c13d9c34acc9d8629d
MD5 ac39018aadb7a0169244c2860e2dcef3
BLAKE2b-256 d387906dec925bdd88d6dcfa4a4c47d0750ea9c64c68caf27c6a181f03dd05d3

See more details on using hashes here.

File details

Details for the file sgraph-1.6.1-py3-none-any.whl.

File metadata

  • Download URL: sgraph-1.6.1-py3-none-any.whl
  • Upload date:
  • Size: 116.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.13

File hashes

Hashes for sgraph-1.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8b48b687c9bab02790ff072c54029366432abe2a071c8dec9e0f99663903fdff
MD5 43c3e7568c9c40bb444bb8681646d202
BLAKE2b-256 7f65c24d850d06534c52a5b7918f1667b5ef4c83e3d7d9ac03716dcc7d996c32

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page