c
This commit is contained in:
75
slowgraph/dgtxt.typ
Normal file
75
slowgraph/dgtxt.typ
Normal file
@@ -0,0 +1,75 @@
|
||||
= dgtxt file format
|
||||
Human readable file format for directed graphs
|
||||
|
||||
== Design goals
|
||||
- any valid input to `tsort`, not containing `#`, is always valid dgtxt.
|
||||
- removing every occureance of the RegEx `#.*?\n` should produce a valid input to `tsort` (if it doesn't contain cycles of course)
|
||||
- easy to generate
|
||||
- human readable
|
||||
- easy to parse fully
|
||||
|
||||
== Concepts
|
||||
=== Node
|
||||
Has:
|
||||
- incoming edges
|
||||
- outgoing edges
|
||||
- name
|
||||
- key-value attributes
|
||||
|
||||
=== Edge
|
||||
Has:
|
||||
- source node
|
||||
- destination node
|
||||
- key-value attributes
|
||||
|
||||
== Syntax
|
||||
Note that `{x}` means none or more times.
|
||||
|
||||
```ebnf
|
||||
ident char = anything - '\n' - '\r' - ' ' - '#';
|
||||
ident = ident char, { ident char };
|
||||
|
||||
node attribute = '##', {spaces},
|
||||
ident, {spaces}, (* referred node *)
|
||||
ident, {spaces}, (* attribute key *)
|
||||
{ not new line } (* attribute value *)
|
||||
;
|
||||
|
||||
comment = '#', { note new line };
|
||||
|
||||
edge attribute = '#:', {spaces}, ident, {spaces}, {not new line} ;
|
||||
|
||||
edge = ident, {spaces}, (* source node *)
|
||||
ident, {spaces}, (* destination node *)
|
||||
{ {spaces}, edge attribute, {spaces} }
|
||||
;
|
||||
|
||||
element = edge
|
||||
| node attribute
|
||||
| comment
|
||||
;
|
||||
|
||||
|
||||
grammar = { spaces, element, spaces };
|
||||
```
|
||||
|
||||
== Example
|
||||
```
|
||||
# comment line (ignored)
|
||||
DownloadLinux FlashDrive
|
||||
# double hashtass start node attributes:
|
||||
## DownloadLinux describtion Attrbiute key/value formats are not standardized her
|
||||
FindFlashDrive FlashDrive #: cost 200
|
||||
#: another-attr-key another attr value (until end of line)
|
||||
DownloadDriveFlashSoftware FlashDrive
|
||||
FlashDrive ShutdownWindows
|
||||
ShutdownWindows RestartPc
|
||||
RestartPc InstallLinux
|
||||
```
|
||||
|
||||
== Recommendations
|
||||
- tools should prefix attribute names with the tool names, unless they think that other tools might adopt that attribute too.
|
||||
- tools that modify graphs should preserve all attributes
|
||||
- tools should not error on unknown attributes (or invalid attribute values), as they might be valid in a different tool
|
||||
- actual comments should have a space after the `#`, for future proofing
|
||||
- node attributes be listed directly after node definitions
|
63
slowgraph/sgraph-adj.c
Normal file
63
slowgraph/sgraph-adj.c
Normal file
@@ -0,0 +1,63 @@
|
||||
#define SLOWGRAPH_IMPL
|
||||
#include "../slowgraph.h"
|
||||
|
||||
static void write_csv_field(char const *s) {
|
||||
char const *y;
|
||||
int quote = 0;
|
||||
for (y = s; *y; y++) {
|
||||
if (*y == '\n' || *y == '"') {
|
||||
quote = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (quote)
|
||||
putchar('"');
|
||||
|
||||
for (; *s; s++) {
|
||||
if (*s == '"')
|
||||
putchar('"');
|
||||
putchar(*s);
|
||||
}
|
||||
|
||||
if (quote)
|
||||
putchar('"');
|
||||
}
|
||||
|
||||
static void dump_adj_matrix_row(SlowGraph *g, SlowGraphNode *row) {
|
||||
SlowGraphNode *col;
|
||||
write_csv_field(row->name);
|
||||
printf(",");
|
||||
for (col = g->first; col; col = col->next) {
|
||||
if (SlowGraphNode_findConnection(row, col)) {
|
||||
printf("1,");
|
||||
} else {
|
||||
printf("0,");
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
static void dump_adj_matrix(SlowGraph *g) {
|
||||
SlowGraphNode *n;
|
||||
printf(",");
|
||||
for (n = g->first; n; n = n->next) {
|
||||
if (n != g->first)
|
||||
printf(",");
|
||||
write_csv_field(n->name);
|
||||
}
|
||||
printf("\n");
|
||||
for (n = g->first; n; n = n->next) {
|
||||
dump_adj_matrix_row(g, n);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
SlowGraph graph = {0};
|
||||
|
||||
if (SlowGraph_readDGTXT(&graph, stdin))
|
||||
return 1;
|
||||
|
||||
dump_adj_matrix(&graph);
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user