https://github.com/softagram/sgraph
graph library for sgraph, hierarchic graphs
https://github.com/softagram/sgraph
algorithms graph graph-algorithms hacktoberfest json reverse-engineering-source-code xml
Last synced: 10 days ago
JSON representation
graph library for sgraph, hierarchic graphs
- Host: GitHub
- URL: https://github.com/softagram/sgraph
- Owner: softagram
- License: mit
- Created: 2021-05-06T18:24:01.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2025-12-16T20:43:19.000Z (2 months ago)
- Last Synced: 2025-12-20T09:56:15.190Z (about 2 months ago)
- Topics: algorithms, graph, graph-algorithms, hacktoberfest, json, reverse-engineering-source-code, xml
- Language: Python
- Homepage:
- Size: 445 KB
- Stars: 0
- Watchers: 2
- Forks: 6
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sgraph
sgraph contains data format, structures and algorithms to work with hierachic graph structures.
Typically it is suitable for representing software structures.
**Documentation**: https://softagram.github.io/sgraph/
See also [sgraph-mcp-server](https://github.com/softagram/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
>>> 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)
>>> 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)
```
## Current utilization
[Softagram](https://github.com/softagram) uses it for building up the information model about the
analyzed software.