update API accodring to the Cyberiada-GraphML-1.0 standard + write to YED format

This commit is contained in:
Alexey Fedoseev
2024-03-23 20:29:34 +03:00
parent 65407577ff
commit 70bf382c73
3 changed files with 1297 additions and 277 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -33,27 +33,26 @@ extern "C" {
/* SM node types: */ /* SM node types: */
typedef enum { typedef enum {
cybNodeSM = 0, /* state machine */ cybNodeSM = 0, /* state machine */
cybNodeSimpleState, /* simple state */ cybNodeSimpleState = 1, /* simple state */
cybNodeCompositeState, /* composite state */ cybNodeCompositeState = 2, /* composite state */
cybNodeSubmachineState, /* submachine state */ cybNodeSubmachineState = 4, /* submachine state */
cybNodeComment, /* comment node */ cybNodeComment = 8, /* comment node */
cybNodeInitial, /* initial pseudostate */ cybNodeInitial = 16, /* initial pseudostate */
cybNodeFinal, /* final pseudostate */ cybNodeFinal = 32, /* final pseudostate */
cybNodeChoice, /* final pseudostate */ cybNodeChoice = 64, /* final pseudostate */
cybNodeJunction, /* junction pseudostate */ cybNodeEntry = 128, /* entry pseudostate */
cybNodeEntry, /* entry pseudostate */ cybNodeExit = 256, /* exit pseudostate */
cybNodeExit, /* exit pseudostate */ cybNodeShallowHistory = 512, /* shallow history pseudostate */
cybNodeHistory, /* shallow history pseudostate */ cybNodeTerminate = 1024, /* terminate pseudostate */
cybNodeDeepHistory, /* deep history pseudostate */
cybNodeFork, /* fork pseudostate */
cybNodeJoin, /* join pseudostate */
cybNodeTerminate, /* terminate pseudostate */
} CyberiadaNodeType; } CyberiadaNodeType;
typedef unsigned int CyberiadaNodeTypeMask;
/* SM node types: */ /* SM node types: */
typedef enum { typedef enum {
cybEdgeTransition = 0, cybEdgeTransition = 0,
cybEdgeComment = 1,
} CyberiadaEdgeType; } CyberiadaEdgeType;
/* SM behavior types: */ /* SM behavior types: */
@@ -61,7 +60,7 @@ typedef enum {
cybBehaviorTransition = 0, cybBehaviorTransition = 0,
cybBehaviorEntry = 1, cybBehaviorEntry = 1,
cybBehaviorExit = 2, cybBehaviorExit = 2,
cybBehaviorDo = 3, cybBehaviorDo = 4,
} CyberiadaBehaviorType; } CyberiadaBehaviorType;
/* SM node & transitions geometry */ /* SM node & transitions geometry */
@@ -126,34 +125,47 @@ typedef struct _CyberiadaEdge {
struct _CyberiadaEdge* next; struct _CyberiadaEdge* next;
} CyberiadaEdge; } CyberiadaEdge;
/* SM extentions /* SM metainformation */
typedef struct _CyberiadaExtension { typedef struct {
char* id; char* standard_version; /* HSM standard version (required parameter) */
size_t id_len; size_t standard_version_len;
char* title; char* platform_name; /* target platform name */
size_t title_len; size_t platform_name_len;
char* data; char* platform_version; /* target platform version */
size_t data_len; size_t platform_version_len;
struct _CyberiadaExtension* next; char* platform_language; /* target platform language */
} CyberiadaExtension;*/ size_t platform_language_len;
char* target_system; /* target system controlled by the SM */
size_t target_system_len;
char* name; /* document name */
size_t name_len;
char* author; /* document author */
size_t author_len;
char* contact; /* document author's contact */
size_t contact_len;
char* description; /* document description */
size_t description_len;
char* version; /* document version */
size_t version_len;
char* date; /* document date */
size_t date_len;
char actions_order_flag; /* actions order flag (0 = not set, 1 = transition first, 2 = exit first) */
char event_propagation_flag; /* event propagation flag (0 = not set, 1 = block events, 2 = propagate events) */
} CyberiadaMetainformation;
/* SM graph (state machine) */ /* SM graph (state machine) */
typedef struct { typedef struct {
char* name; char* format; /* SM graph format string */
size_t name_len; size_t format_len; /* SM graph format string length */
char* version; CyberiadaMetainformation* meta_info;
size_t version_len;
char* info;
size_t info_len;
CyberiadaNode* nodes; CyberiadaNode* nodes;
CyberiadaEdge* edges; CyberiadaEdge* edges;
/* CyberiadaExtension* extensions;*/
} CyberiadaSM; } CyberiadaSM;
/* SM GraphML supported formats */ /* SM GraphML supported formats */
typedef enum { typedef enum {
cybxmlYED = 0, cybxmlCyberiada = 0,
cybxmlCyberiada, cybxmlYED,
cybxmlUnknown cybxmlUnknown
} CyberiadaXMLFormat; } CyberiadaXMLFormat;
@@ -165,9 +177,10 @@ typedef enum {
#define CYBERIADA_XML_ERROR 1 #define CYBERIADA_XML_ERROR 1
#define CYBERIADA_FORMAT_ERROR 2 #define CYBERIADA_FORMAT_ERROR 2
#define CYBERIADA_BEHAVIOR_FORMAT_ERROR 3 #define CYBERIADA_BEHAVIOR_FORMAT_ERROR 3
#define CYBERIADA_NOT_FOUND 4 #define CYBERIADA_METADATA_FORMAT_ERROR 4
#define CYBERIADA_BAD_PARAMETER 5 #define CYBERIADA_NOT_FOUND 5
#define CYBERIADA_ASSERT 6 #define CYBERIADA_BAD_PARAMETER 6
#define CYBERIADA_ASSERT 7
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* The Cyberiada GraphML library functions * The Cyberiada GraphML library functions
@@ -176,16 +189,19 @@ typedef enum {
/* Allocate the SM structure in memory (for heap usage) */ /* Allocate the SM structure in memory (for heap usage) */
CyberiadaSM* cyberiada_create_sm(void); CyberiadaSM* cyberiada_create_sm(void);
/* Initialize the SM structure. Do not use the structure before the initialization! */ /* Initialize the SM structure. */
/* Do not use the structure before the initialization! */
int cyberiada_init_sm(CyberiadaSM* sm); int cyberiada_init_sm(CyberiadaSM* sm);
/* Cleanup the content of the SM structure, free the conents memory */ /* Cleanup the content of the SM structure */
/* Free the allocated memory of the structure content but not the structure itself */
int cyberiada_cleanup_sm(CyberiadaSM* sm); int cyberiada_cleanup_sm(CyberiadaSM* sm);
/* Free the allocated SM structure (for heap usage) */ /* Free the allocated SM structure with the content (for heap usage) */
int cyberiada_destroy_sm(CyberiadaSM* sm); int cyberiada_destroy_sm(CyberiadaSM* sm);
/* Read an XML file and decode the SM structure */ /* Read an XML file and decode the SM structure */
/* Allocate the SM structure first */
int cyberiada_read_sm(CyberiadaSM* sm, const char* filename, CyberiadaXMLFormat format); int cyberiada_read_sm(CyberiadaSM* sm, const char* filename, CyberiadaXMLFormat format);
/* Encode the SM structure and write the data to an XML file */ /* Encode the SM structure and write the data to an XML file */

154
test.c
View File

@@ -25,68 +25,168 @@
#include "cyberiadaml.h" #include "cyberiadaml.h"
const char* formats[] = { const char* formats[] = {
"yed", /* cybxmlYED */ "cyberiada", /* cybxmlCyberiada */
"cyberiada" /* cybxmlCyberiada */ "yed" /* cybxmlYED */
}; };
const char* format_names[] = { const char* format_descr[] = {
"yEd editor format used by Ostranna projects and the Orbita Simulator", "Cyberiada-GraphML format",
"Cyberiada-GraphML format" "yEd editor format used by Ostranna projects and the Orbita Simulator"
}; };
unsigned int format_count = sizeof(formats) / sizeof(char*); size_t format_count = sizeof(formats) / sizeof(char*);
const char* commands[] = {
"print",
"convert"
};
const char* command_descr[] = {
"print HSM diagram content to stdout; use -f key to set the scheme format (default - unknown)",
"convert HSM from <format-from> format to <to-format> into the file named <output-file>"
};
size_t command_count = sizeof(commands) / sizeof(char*);
static void print_usage(const char* name) static void print_usage(const char* name)
{ {
unsigned int i; unsigned int i;
fprintf(stderr, "%s [-t <format>] <path-to-graphml-file>\n\n", name); fprintf(stderr, "%s <command> [-f <format-from>] [-t <format-to> -o <dest-file>] <path-to-graphml-file>\n\n", name);
fprintf(stderr, "Supported commands:\n");
for (i = 0; i < command_count; i++) {
fprintf(stderr, " %-20s %s\n", commands[i], command_descr[i]);
}
fprintf(stderr, "Supported formats:\n"); fprintf(stderr, "Supported formats:\n");
for (i = 0; i < format_count; i++) { for (i = 0; i < format_count; i++) {
fprintf(stderr, " %-20s %s\n", formats[i], format_names[i]); fprintf(stderr, " %-20s %s\n", formats[i], format_descr[i]);
} }
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
char *filename; char *source_filename, *dest_filename;
char *format_str = ""; char *source_format_str = "", *dest_format_str = "";
CyberiadaXMLFormat format = cybxmlUnknown; CyberiadaXMLFormat source_format = cybxmlUnknown, dest_format = cybxmlUnknown;
unsigned int i; unsigned int i;
int res; int res;
CyberiadaSM sm; CyberiadaSM sm;
char print = 0;
if (argc == 4) { if (argc < 3) {
if (strcmp(argv[1], "-t") != 0) { print_usage(argv[0]);
return 1;
}
if (argc == 5 || argc == 9) {
if (strcmp(argv[2], "-f") != 0) {
print_usage(argv[0]); print_usage(argv[0]);
return 1; return 1;
} }
for(i = 0; i < format_count; i++) { for(i = 0; i < format_count; i++) {
if (strcmp(argv[2], formats[i]) == 0) { if (strcmp(argv[3], formats[i]) == 0) {
format = (CyberiadaXMLFormat)i; source_format = (CyberiadaXMLFormat)i;
format_str = argv[2]; source_format_str = argv[3];
break; break;
} }
} }
if(strlen(format_str) == 0) { if(strlen(source_format_str) == 0) {
fprintf(stderr, "unsupported graphml format %s\n", argv[2]); fprintf(stderr, "unsupported graphml format %s\n", argv[3]);
print_usage(argv[0]); print_usage(argv[0]);
return 2; return 2;
} }
filename = argv[3];
} else if (argc == 2) {
filename = argv[1];
} else {
print_usage(argv[0]);
return 1;
} }
if ((res = cyberiada_read_sm(&sm, filename, format)) != CYBERIADA_NO_ERROR) {
if (strcmp(argv[1], commands[0]) == 0) {
/* print */
if (argc == 5) {
source_filename = argv[4];
} else if (argc == 3) {
source_filename = argv[2];
} else {
print_usage(argv[0]);
return 1;
}
print = 1;
} else if (strcmp(argv[1], commands[1]) == 0) {
/* convert */
if (argc < 7) {
print_usage(argv[0]);
return 1;
}
if (argc == 9) {
if (strcmp(argv[4], "-t") != 0) {
print_usage(argv[0]);
return 1;
}
for(i = 0; i < format_count; i++) {
if (strcmp(argv[5], formats[i]) == 0) {
dest_format = (CyberiadaXMLFormat)i;
dest_format_str = argv[5];
break;
}
}
if(strlen(dest_format_str) == 0) {
fprintf(stderr, "unsupported graphml format %s\n", argv[5]);
print_usage(argv[0]);
return 2;
}
if (strcmp(argv[6], "-o") != 0) {
print_usage(argv[0]);
return 1;
}
dest_filename = argv[7];
source_filename = argv[8];
} else if (argc == 7) {
if (strcmp(argv[2], "-t") != 0) {
print_usage(argv[0]);
return 1;
}
for(i = 0; i < format_count; i++) {
if (strcmp(argv[3], formats[i]) == 0) {
dest_format = (CyberiadaXMLFormat)i;
dest_format_str = argv[3];
break;
}
}
if(strlen(dest_format_str) == 0) {
fprintf(stderr, "unsupported graphml format %s\n", argv[3]);
print_usage(argv[0]);
return 2;
}
if (strcmp(argv[4], "-o") != 0) {
print_usage(argv[0]);
return 1;
}
dest_filename = argv[5];
source_filename = argv[6];
} else {
print_usage(argv[0]);
return 1;
}
}
if ((res = cyberiada_read_sm(&sm, source_filename, source_format)) != CYBERIADA_NO_ERROR) {
fprintf(stderr, "error while reading %s file: %d\n", fprintf(stderr, "error while reading %s file: %d\n",
filename, res); source_filename, res);
return 2; return 2;
} }
cyberiada_print_sm(&sm); if (print) {
cyberiada_print_sm(&sm);
} else {
if ((res = cyberiada_write_sm(&sm, dest_filename, dest_format)) != CYBERIADA_NO_ERROR) {
fprintf(stderr, "error while writing %s file: %d\n",
dest_filename, res);
return 3;
}
}
cyberiada_cleanup_sm(&sm); cyberiada_cleanup_sm(&sm);