-
Notifications
You must be signed in to change notification settings - Fork 0
/
cyberiadaml.c
4653 lines (4205 loc) · 149 KB
/
cyberiadaml.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -----------------------------------------------------------------------------
* The Cyberiada GraphML library implemention
*
* The C library implementation
*
* Copyright (C) 2024 Alexey Fedoseev <aleksey@fedoseev.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/
*
* ----------------------------------------------------------------------------- */
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/encoding.h>
#include <libxml/xmlwriter.h>
#include <regex.h>
#include <stddef.h>
#include <math.h>
#include "cyberiadaml.h"
#include "utf8enc.h"
#include "cyb_types.h"
#include "cyb_string.h"
#include "cyb_error.h"
#include "geometry.h"
/* -----------------------------------------------------------------------------
* Cyberiada parser constants
* ----------------------------------------------------------------------------- */
/* Core GraphML constants */
#define GRAPHML_XML_ENCODING "utf-8"
#define GRAPHML_NAMESPACE_URI "http://graphml.graphdrawing.org/xmlns"
#define GRAPHML_GRAPHML_ELEMENT "graphml"
#define GRAPHML_GRAPH_ELEMENT "graph"
#define GRAPHML_NODE_ELEMENT "node"
#define GRAPHML_EDGE_ELEMENT "edge"
#define GRAPHML_DATA_ELEMENT "data"
#define GRAPHML_KEY_ELEMENT "key"
#define GRAPHML_PORT_ELEMENT "port"
#define GRAPHML_POINT_ELEMENT "point"
#define GRAPHML_RECT_ELEMENT "rect"
#define GRAPHML_ID_ATTRIBUTE "id"
#define GRAPHML_KEY_ATTRIBUTE "key"
#define GRAPHML_FOR_ATTRIBUTE "for"
#define GRAPHML_NAME_ATTRIBUTE "name"
#define GRAPHML_EDGEDEFAULT_ATTRIBUTE "edgedefault"
#define GRAPHML_ATTR_NAME_ATTRIBUTE "attr.name"
#define GRAPHML_ATTR_TYPE_ATTRIBUTE "attr.type"
#define GRAPHML_EDGEDEFAULT_ATTRIBUTE_VALUE "directed"
#define GRAPHML_SOURCE_ATTRIBUTE "source"
#define GRAPHML_TARGET_ATTRIBUTE "target"
/* Common GraphML constants */
#define GRAPHML_GEOM_X_ATTRIBUTE "x"
#define GRAPHML_GEOM_Y_ATTRIBUTE "y"
#define GRAPHML_GEOM_WIDTH_ATTRIBUTE "width"
#define GRAPHML_GEOM_HEIGHT_ATTRIBUTE "height"
/* Cyberiada-GraphML format constants */
#define GRAPHML_CYB_GRAPH_VERTEX_INITIAL "initial"
#define GRAPHML_CYB_GRAPH_VERTEX_FINAL "final"
#define GRAPHML_CYB_GRAPH_VERTEX_CHOICE "choice"
#define GRAPHML_CYB_GRAPH_VERTEX_TERMINATE "terminate"
#define GRAPHML_CYB_GRAPH_VERTEX_SHALLOW_HISTORY "shallowHistory"
#define GRAPHML_CYB_GRAPH_VERTEX_DEEP_HISTORY "deepHistory"
#define GRAPHML_CYB_GRAPH_VERTEX_ENTRY_POINT "entryPoint"
#define GRAPHML_CYB_GRAPH_VERTEX_EXIT_POINT "exitPoint"
#define GRAPHML_CYB_GRAPH_VERTEX_FORK "fork"
#define GRAPHML_CYB_GRAPH_VERTEX_JOIN "join"
#define GRAPHML_CYB_COMMENT_FORMAL "formal"
#define GRAPHML_CYB_COMMENT_INFORMAL "informal"
/* YED format constants */
#define GRAPHML_NAMESPACE_URI_YED "http://www.yworks.com/xml/graphml"
#define GRAPHML_BERLOGA_SCHEMENAME_ATTR "SchemeName"
#define GRAPHML_YED_YFILES_TYPE_ATTR "yfiles.type"
#define GRAPHML_YED_GEOMETRYNODE "Geometry"
#define GRAPHML_YED_BORDERSTYLENODE "BorderStyle"
#define GRAPHML_YED_LINESTYLENODE "LineStyle"
#define GRAPHML_YED_FILLNODE "Fill"
#define GRAPHML_YED_PATHNODE "Path"
#define GRAPHML_YED_POINTNODE "Point"
#define GRAPHML_YED_GEOM_SOURCE_X_ATTRIBUTE "sx"
#define GRAPHML_YED_GEOM_SOURCE_Y_ATTRIBUTE "sy"
#define GRAPHML_YED_GEOM_TARGET_X_ATTRIBUTE "tx"
#define GRAPHML_YED_GEOM_TARGET_Y_ATTRIBUTE "ty"
#define GRAPHML_YED_COMMENTNODE "UMLNoteNode"
#define GRAPHML_YED_GROUPNODE "GroupNode"
#define GRAPHML_YED_GENERICNODE "GenericNode"
#define GRAPHML_YED_LABELNODE "NodeLabel"
#define GRAPHML_YED_NODE_CONFIG_ATTRIBUTE "configuration"
#define GRAPHML_YED_NODE_CONFIG_START "com.yworks.bpmn.Event"
#define GRAPHML_YED_NODE_CONFIG_START2 "com.yworks.bpmn.Event.withShadow"
#define GRAPHML_YED_PROPNODE "Property"
#define GRAPHML_YED_PROP_VALUE_ATTRIBUTE "value"
#define GRAPHML_YED_PROP_VALUE_START "EVENT_CHARACTERISTIC_START"
#define GRAPHML_YED_EDGELABEL "EdgeLabel"
#define GRAPHML_YED_POLYLINEEDGE "PolyLineEdge"
/* HSM format / standard constants */
#define CYBERIADA_FORMAT_CYBERIADAML "Cyberiada-GraphML-1.0"
#define CYBERIADA_FORMAT_BERLOGA "yEd Berloga"
#define CYBERIADA_FORMAT_OSTRANNA "yEd Ostranna"
#define CYBERIADA_STANDARD_VERSION_CYBERIADAML "1.0"
/* HSM action constants */
#define CYBERIADA_SINGLE_NEWLINE "\n"
#define CYBERIADA_NEWLINE "\n\n"
#define CYBERIADA_NEWLINE_RN "\r\n\r\n"
#define CYBERIADA_ACTION_TRIGGER_ENTRY "entry"
#define CYBERIADA_ACTION_TRIGGER_EXIT "exit"
#define CYBERIADA_ACTION_TRIGGER_DO "do"
#define CYBERIADA_ACTION_EDGE_REGEXP "^\\s*(\\w((\\w| |\\.)*\\w)?(\\(\\w+\\))?)?\\s*(\\[([^]]+)\\])?\\s*(propagate|block)?\\s*(/\\s*(.*))?\\s*$"
#define CYBERIADA_ACTION_NODE_REGEXP "^\\s*(\\w((\\w| |\\.)*\\w)?(\\(\\w+\\))?)\\s*(\\[([^]]+)\\])?\\s*(propagate|block)?\\s*(/\\s*(.*)?)\\s*$"
#define CYBERIADA_ACTION_REGEXP_MATCHES 10
#define CYBERIADA_ACTION_REGEXP_MATCH_TRIGGER 1
#define CYBERIADA_ACTION_REGEXP_MATCH_GUARD 6
#define CYBERIADA_ACTION_REGEXP_MATCH_PROP 7
#define CYBERIADA_ACTION_REGEXP_MATCH_ACTION 9
#define CYBERIADA_ACTION_SPACES_REGEXP "^\\s*$"
/*#define CYBERIADA_ACTION_NEWLINE_REGEXP "^([^\n]*(\n[ \t\r]*[^\\s])?)*\n\\s*\n(.*)?$"
#define CYBERIADA_ACTION_NL_REGEXP_MATCHES 4*/
#define CYBERIADA_ACTION_LEGACY_REGEXP "^\\s*(\\w((\\w| |\\.)*\\w)?(\\(\\w+\\))?)\\s*(\\[([^]]+)\\])?\\s*/"
#define CYBERIADA_ACTION_LEGACY_MATCHES 7
#define CYBERIADA_ACTION_LEGACY_MATCH_TRIGGER 1
/* CybediadaML metadata constants */
#define CYBERIADA_META_NODE_DEFAULT_ID "nMeta"
#define CYBERIADA_META_NODE_TITLE "CGML_META"
#define CYBERIADA_META_SEPARATOR_CHR '/'
#define CYBERIADA_META_NEW_LINE_CHR '\n'
#define CYBERIADA_META_NEW_LINE_STR "\n"
#define CYBERIADA_META_STANDARD_VERSION "standardVersion"
#define CYBERIADA_META_PLATFORM_NAME "platform"
#define CYBERIADA_META_PLATFORM_VERSION "platformVersion"
#define CYBERIADA_META_PLATFORM_LANGUAGE "platformLanguage"
#define CYBERIADA_META_TARGET_SYSTEM "target"
#define CYBERIADA_META_NAME "name"
#define CYBERIADA_META_AUTHOR "author"
#define CYBERIADA_META_CONTACT "contact"
#define CYBERIADA_META_DESCRIPTION "description"
#define CYBERIADA_META_VERSION "version"
#define CYBERIADA_META_DATE "date"
#define CYBERIADA_META_TRANSITION_ORDER "transitionOrder"
#define CYBERIADA_META_AO_TRANSITION "transitionFirst"
#define CYBERIADA_META_AO_EXIT "exitFirst"
#define CYBERIADA_META_EVENT_PROPAGATION "eventPropagation"
#define CYBERIADA_META_EP_PROPAGATE "propagate"
#define CYBERIADA_META_EP_BLOCK "block"
#define CYBERIADA_META_MARKUP_LANGUAGE "markupLanguage"
/* Misc. constants */
#define PSEUDO_NODE_SIZE 20
#define DEFAULT_NODE_SIZE 100
#define EMPTY_TITLE ""
/* Types & macros for XML/GraphML processing */
#define INDENT_STR " "
#define XML_WRITE_OPEN_E(w, e) if ((res = xmlTextWriterStartElement(w, (const xmlChar *)e)) < 0) { \
fprintf(stderr, "xml open element error %d at %s:%d", res, __FILE__, __LINE__); \
return CYBERIADA_XML_ERROR; \
}
#define XML_WRITE_OPEN_E_I(w, e, indent) xmlTextWriterWriteRaw(w, (const xmlChar *)"\n"); \
for (size_t _i = 0; _i < indent; _i++) { \
xmlTextWriterWriteRaw(w, (const xmlChar *)INDENT_STR); \
} \
if ((res = xmlTextWriterStartElement(w, (const xmlChar *)e)) < 0) { \
fprintf(stderr, "xml open element error %d at %s:%d", res, __FILE__, __LINE__); \
return CYBERIADA_XML_ERROR; \
}
#define XML_WRITE_OPEN_E_NS_I(w, e, ns, indent) xmlTextWriterWriteRaw(w, (const xmlChar *)"\n"); \
for (size_t _i = 0; _i < indent; _i++) { \
xmlTextWriterWriteRaw(w, (const xmlChar *)INDENT_STR); \
} \
if ((res = xmlTextWriterStartElementNS(w, (const xmlChar *)ns, \
(const xmlChar *)e, NULL)) < 0) { \
fprintf(stderr, "xml open element error %d at %s:%d", res, __FILE__, __LINE__); \
return CYBERIADA_XML_ERROR; \
}
#define XML_WRITE_ATTR(w, a, v) if ((res = xmlTextWriterWriteAttribute(w, (const xmlChar *)a, \
(const xmlChar *)v)) < 0) { \
fprintf(stderr, "xml attribute error %d at %s:%d", res, __FILE__, __LINE__); \
return CYBERIADA_XML_ERROR; \
}
#define XML_WRITE_TEXT(w, txt) if ((res = xmlTextWriterWriteString(w, (const xmlChar *)txt)) < 0) { \
fprintf(stderr, "xml text error %d at %s:%d", res, __FILE__, __LINE__); \
return CYBERIADA_XML_ERROR; \
}
#define XML_WRITE_CLOSE_E(w) if ((res = xmlTextWriterEndElement(w)) < 0) { \
fprintf(stderr, "xml close element error %d at %s:%d", res, __FILE__, __LINE__); \
return CYBERIADA_XML_ERROR; \
}
#define XML_WRITE_CLOSE_E_I(w, indent) xmlTextWriterWriteRaw(w, (const xmlChar *)"\n"); \
for (size_t _i = 0; _i < indent; _i++) { \
xmlTextWriterWriteRaw(w, (const xmlChar *)INDENT_STR); \
} \
if ((res = xmlTextWriterEndElement(w)) < 0) { \
fprintf(stderr, "xml close element error %d at %s:%d", res, __FILE__, __LINE__); \
return CYBERIADA_XML_ERROR; \
}
# define XML_READMEMORY_BASENAME "noname.xml"
typedef struct {
char* attr_id;
const char* attr_for;
const char* attr_name;
const char* attr_type;
char* extra;
} GraphMLKey;
#define GRAPHML_CYB_KEY_FORMAT "gFormat"
#define GRAPHML_CYB_KEY_NAME "dName"
#define GRAPHML_CYB_KEY_STATE_MACHINE "dStateMachine"
#define GRAPHML_CYB_KEY_SUBMACHINE "dSubmachineState"
#define GRAPHML_CYB_KEY_DATA "dData"
#define GRAPHML_CYB_KEY_COMMENT "dNote"
#define GRAPHML_CYB_KEY_COMMENT_SUBJECT "dPivot"
#define GRAPHML_CYB_KEY_COMMENT_CHUNK "dChunk"
#define GRAPHML_CYB_KEY_GEOMETRY "dGeometry"
#define GRAPHML_CYB_KEY_SOURCE_POINT "dSourcePoint"
#define GRAPHML_CYB_KEY_TARGET_POINT "dTargetPoint"
#define GRAPHML_CYB_KEY_LABEL_GEOMETRY "dLabelGeometry"
#define GRAPHML_CYB_KEY_VERTEX "dVertex"
#define GRAPHML_CYB_KEY_MARKUP "dMarkup"
#define GRAPHML_CYB_KEY_COLOR "dColor"
#define GRAPHML_CYB_KEY_FORMAT_NAME "format"
#define GRAPHML_CYB_KEY_NAME_NAME "name"
#define GRAPHML_CYB_KEY_STATE_MACHINE_NAME "stateMachine"
#define GRAPHML_CYB_KEY_SUBMACHINE_NAME "submachineState"
#define GRAPHML_CYB_KEY_DATA_NAME "data"
#define GRAPHML_CYB_KEY_COMMENT_NAME "note"
#define GRAPHML_CYB_KEY_COMMENT_SUBJECT_NAME "pivot"
#define GRAPHML_CYB_KEY_COMMENT_CHUNK_NAME "chunk"
#define GRAPHML_CYB_KEY_GEOMETRY_NAME "geometry"
#define GRAPHML_CYB_KEY_SOURCE_POINT_NAME "sourcePoint"
#define GRAPHML_CYB_KEY_TARGET_POINT_NAME "targetPoint"
#define GRAPHML_CYB_KEY_LABEL_GEOMETRY_NAME "labelGeometry"
#define GRAPHML_CYB_KEY_VERTEX_NAME "vertex"
#define GRAPHML_CYB_KEY_MARKUP_NAME "markup"
#define GRAPHML_CYB_KEY_COLOR_NAME "color"
static GraphMLKey cyberiada_graphml_keys[] = {
{ GRAPHML_CYB_KEY_FORMAT, GRAPHML_GRAPHML_ELEMENT, GRAPHML_CYB_KEY_FORMAT_NAME, "string", NULL },
{ GRAPHML_CYB_KEY_NAME, GRAPHML_GRAPH_ELEMENT, GRAPHML_CYB_KEY_NAME_NAME, "string", NULL },
{ GRAPHML_CYB_KEY_NAME, GRAPHML_NODE_ELEMENT, GRAPHML_CYB_KEY_NAME_NAME, "string", NULL },
{ GRAPHML_CYB_KEY_STATE_MACHINE, GRAPHML_GRAPH_ELEMENT, GRAPHML_CYB_KEY_STATE_MACHINE_NAME, "string", NULL },
{ GRAPHML_CYB_KEY_SUBMACHINE, GRAPHML_NODE_ELEMENT, GRAPHML_CYB_KEY_SUBMACHINE_NAME, "string", NULL },
{ GRAPHML_CYB_KEY_GEOMETRY, GRAPHML_GRAPH_ELEMENT, GRAPHML_CYB_KEY_GEOMETRY_NAME, NULL, NULL },
{ GRAPHML_CYB_KEY_GEOMETRY, GRAPHML_NODE_ELEMENT, GRAPHML_CYB_KEY_GEOMETRY_NAME, NULL, NULL },
{ GRAPHML_CYB_KEY_GEOMETRY, GRAPHML_EDGE_ELEMENT, GRAPHML_CYB_KEY_GEOMETRY_NAME, NULL, NULL },
{ GRAPHML_CYB_KEY_SOURCE_POINT, GRAPHML_EDGE_ELEMENT, GRAPHML_CYB_KEY_SOURCE_POINT_NAME, NULL, NULL },
{ GRAPHML_CYB_KEY_TARGET_POINT, GRAPHML_EDGE_ELEMENT, GRAPHML_CYB_KEY_TARGET_POINT_NAME, NULL, NULL },
{ GRAPHML_CYB_KEY_LABEL_GEOMETRY, GRAPHML_EDGE_ELEMENT, GRAPHML_CYB_KEY_LABEL_GEOMETRY_NAME, NULL, NULL },
{ GRAPHML_CYB_KEY_COMMENT, GRAPHML_NODE_ELEMENT, GRAPHML_CYB_KEY_COMMENT_NAME, "string", NULL },
{ GRAPHML_CYB_KEY_VERTEX, GRAPHML_NODE_ELEMENT, GRAPHML_CYB_KEY_VERTEX_NAME, "string", NULL },
{ GRAPHML_CYB_KEY_DATA, GRAPHML_NODE_ELEMENT, GRAPHML_CYB_KEY_DATA_NAME, "string", NULL },
{ GRAPHML_CYB_KEY_DATA, GRAPHML_EDGE_ELEMENT, GRAPHML_CYB_KEY_DATA_NAME, "string", NULL },
{ GRAPHML_CYB_KEY_MARKUP, GRAPHML_NODE_ELEMENT, GRAPHML_CYB_KEY_MARKUP_NAME, "string", NULL },
{ GRAPHML_CYB_KEY_COLOR, GRAPHML_NODE_ELEMENT, GRAPHML_CYB_KEY_COLOR_NAME, "string", NULL },
{ GRAPHML_CYB_KEY_COLOR, GRAPHML_EDGE_ELEMENT, GRAPHML_CYB_KEY_COLOR_NAME, "string", NULL },
{ GRAPHML_CYB_KEY_COMMENT_SUBJECT, GRAPHML_EDGE_ELEMENT, GRAPHML_CYB_KEY_COMMENT_SUBJECT_NAME, "string", NULL },
{ GRAPHML_CYB_KEY_COMMENT_CHUNK, GRAPHML_EDGE_ELEMENT, GRAPHML_CYB_KEY_COMMENT_CHUNK_NAME, "string", NULL }
};
static const size_t cyberiada_graphml_keys_count = sizeof(cyberiada_graphml_keys) / sizeof(GraphMLKey);
#define GRAPHML_YED_KEY_GRAPH_DESCR "d0"
#define GRAPHML_YED_KEY_PORT_GRAPHICS "d1"
#define GRAPHML_YED_KEY_PORT_GEOMETRY "d2"
#define GRAPHML_YED_KEY_PORT_USER_DATA "d3"
#define GRAPHML_YED_KEY_NODE_URL "d4"
#define GRAPHML_YED_KEY_NODE_DESCR "d5"
#define GRAPHML_YED_KEY_NODE_GRAPHICS "d6"
#define GRAPHML_YED_KEY_GRAPHML_RES "d7"
#define GRAPHML_YED_KEY_EDGE_URL "d8"
#define GRAPHML_YED_KEY_EDGE_DESCR "d9"
#define GRAPHML_YED_KEY_EDGE_GRAPHICS "d10"
static GraphMLKey yed_graphml_keys[] = {
{ GRAPHML_YED_KEY_GRAPH_DESCR, GRAPHML_GRAPH_ELEMENT, "description", "string", NULL },
{ GRAPHML_YED_KEY_PORT_GRAPHICS, GRAPHML_PORT_ELEMENT, NULL, NULL, "portgraphics" },
{ GRAPHML_YED_KEY_PORT_GEOMETRY, GRAPHML_PORT_ELEMENT, NULL, NULL, "portgeometry" },
{ GRAPHML_YED_KEY_PORT_USER_DATA, GRAPHML_PORT_ELEMENT, NULL, NULL, "portuserdata" },
{ GRAPHML_YED_KEY_NODE_URL, GRAPHML_NODE_ELEMENT, "url", "string", NULL },
{ GRAPHML_YED_KEY_NODE_DESCR, GRAPHML_NODE_ELEMENT, "description", "string", NULL },
{ GRAPHML_YED_KEY_NODE_GRAPHICS, GRAPHML_NODE_ELEMENT, NULL, NULL, "nodegraphics" },
{ GRAPHML_YED_KEY_GRAPHML_RES, GRAPHML_GRAPHML_ELEMENT, NULL, NULL, "resources" },
{ GRAPHML_YED_KEY_EDGE_URL, GRAPHML_EDGE_ELEMENT, "url", "string", NULL },
{ GRAPHML_YED_KEY_EDGE_DESCR, GRAPHML_EDGE_ELEMENT, "description", "string", NULL },
{ GRAPHML_YED_KEY_EDGE_GRAPHICS, GRAPHML_EDGE_ELEMENT, NULL, NULL, "edgegraphics" }
};
static const size_t yed_graphml_keys_count = sizeof(yed_graphml_keys) / sizeof(GraphMLKey);
/* -----------------------------------------------------------------------------
* Graph manipulation functions
* ----------------------------------------------------------------------------- */
typedef int (*CyberiadaGraphHandler)(CyberiadaNode* node,
CyberiadaNode* parent);
static int cyberiada_process_graph(CyberiadaNode* root,
CyberiadaNode* parent,
CyberiadaGraphHandler handler)
{
int res;
CyberiadaNode *node;
for (node = root; node; node = node->next) {
if ((res = handler(node, parent)) != CYBERIADA_NO_ERROR) {
return res;
}
if (node->children) {
if ((res = cyberiada_process_graph(node->children,
node, handler)) != CYBERIADA_NO_ERROR) {
return res;
}
}
}
return CYBERIADA_NO_ERROR;
}
CyberiadaNode* cyberiada_graph_find_node_by_id(CyberiadaNode* root, const char* id)
{
CyberiadaNode* node;
CyberiadaNode* found;
for (node = root; node; node = node->next) {
if (strcmp(node->id, id) == 0) {
return node;
}
if (node->children) {
found = cyberiada_graph_find_node_by_id(node->children, id);
if (found) return found;
}
}
return NULL;
}
CyberiadaNode* cyberiada_graph_find_node_by_type(CyberiadaNode* root, CyberiadaNodeTypeMask mask)
{
CyberiadaNode* node;
CyberiadaNode* found;
if (root->type & mask) {
return root;
}
for (node = root->next; node; node = node->next) {
found = cyberiada_graph_find_node_by_type(node, mask);
if (found)
return found;
}
if (root->children) {
return cyberiada_graph_find_node_by_type(root->children, mask);
}
return NULL;
}
static CyberiadaEdge* cyberiada_graph_find_edge_by_id(CyberiadaEdge* root, const char* id)
{
CyberiadaEdge* edge;
for (edge = root; edge; edge = edge->next) {
if (edge->id && strcmp(edge->id, id) == 0) {
return edge;
}
}
return NULL;
}
/* -----------------------------------------------------------------------------
* The Cyberiada GraphML library fucntions declarations
* ----------------------------------------------------------------------------- */
CyberiadaCommentData* cyberiada_new_comment_data(void)
{
CyberiadaCommentData* cd = (CyberiadaCommentData*)malloc(sizeof(CyberiadaCommentData));
memset(cd, 0, sizeof(CyberiadaCommentData));
return cd;
}
static CyberiadaCommentData* cyberiada_copy_comment_data(CyberiadaCommentData* src)
{
CyberiadaCommentData* cd;
if (!src) {
return NULL;
}
cd = cyberiada_new_comment_data();
if (src->body) {
cyberiada_copy_string(&(cd->body), &(cd->body_len), src->body);
}
if (src->markup) {
cyberiada_copy_string(&(cd->markup), &(cd->markup_len), src->markup);
}
return cd;
}
CyberiadaLink* cyberiada_new_link(const char* ref)
{
CyberiadaLink* link = (CyberiadaLink*)malloc(sizeof(CyberiadaLink));
memset(link, 0, sizeof(CyberiadaLink));
cyberiada_copy_string(&(link->ref), &(link->ref_len), ref);
return link;
}
static CyberiadaLink* cyberiada_copy_link(CyberiadaLink* src)
{
if (!src || !(src->ref)) {
return NULL;
}
return cyberiada_new_link(src->ref);
}
CyberiadaAction* cyberiada_new_action(CyberiadaActionType type,
const char* trigger,
const char* guard,
const char* behavior)
{
CyberiadaAction* action = (CyberiadaAction*)malloc(sizeof(CyberiadaAction));
memset(action, 0, sizeof(CyberiadaAction));
action->type = type;
cyberiada_copy_string(&(action->trigger), &(action->trigger_len), trigger);
cyberiada_copy_string(&(action->guard), &(action->guard_len), guard);
cyberiada_copy_string(&(action->behavior), &(action->behavior_len), behavior);
return action;
}
static CyberiadaAction* cyberiada_copy_action(CyberiadaAction* src)
{
if (!src) {
return NULL;
}
return cyberiada_new_action(src->type, src->trigger, src->guard, src->behavior);
}
CyberiadaNode* cyberiada_new_node(const char* id)
{
CyberiadaNode* new_node = (CyberiadaNode*)malloc(sizeof(CyberiadaNode));
memset(new_node, 0, sizeof(CyberiadaNode));
cyberiada_copy_string(&(new_node->id), &(new_node->id_len), id);
new_node->type = cybNodeSimpleState;
return new_node;
}
static CyberiadaNode* cyberiada_copy_node(CyberiadaNode* src)
{
CyberiadaNode *dst, *n, *dst_child, *src_child;
CyberiadaAction *a, *dst_action, *src_action;
if (!src || !(src->id)) {
return NULL;
}
dst = cyberiada_new_node(src->id);
dst->type = src->type;
if (src->title) {
cyberiada_copy_string(&(dst->title), &(dst->title_len), src->title);
}
if (src->geometry_point) {
dst->geometry_point = htree_copy_point(src->geometry_point);
}
if (src->geometry_rect) {
dst->geometry_rect = htree_copy_rect(src->geometry_rect);
}
if (src->color) {
cyberiada_copy_string(&(dst->color), &(dst->color_len), src->color);
}
if (src->link) {
dst->link = cyberiada_copy_link(src->link);
}
if (src->comment_data) {
dst->comment_data = cyberiada_copy_comment_data(src->comment_data);
}
if (src->actions) {
for (src_action = src->actions; src_action; src_action = src_action->next) {
dst_action = cyberiada_copy_action(src_action);
if (dst->actions) {
a = dst->actions;
while (a->next) a = a->next;
a->next = dst_action;
} else {
dst->actions = dst_action;
}
}
}
if (src->children) {
for (src_child = src->children; src_child; src_child = src_child->next) {
dst_child = cyberiada_copy_node(src_child);
dst_child->parent = dst;
if (dst->children) {
n = dst->children;
while (n->next) n = n->next;
n->next = dst_child;
} else {
dst->children = dst_child;
}
}
}
return dst;
}
static int cyberiada_destroy_action(CyberiadaAction* action)
{
CyberiadaAction* a;
if (action != NULL) {
do {
a = action;
if (a->trigger) free(a->trigger);
if (a->guard) free(a->guard);
if (a->behavior) free(a->behavior);
action = a->next;
free(a);
} while (action);
}
return CYBERIADA_NO_ERROR;
}
static int cyberiada_destroy_all_nodes(CyberiadaNode* node);
static int cyberiada_destroy_node(CyberiadaNode* node)
{
if(node != NULL) {
if (node->id) free(node->id);
if (node->title) free(node->title);
if (node->children) {
cyberiada_destroy_all_nodes(node->children);
}
if (node->actions) cyberiada_destroy_action(node->actions);
if (node->geometry_point) htree_destroy_point(node->geometry_point);
if (node->geometry_rect) htree_destroy_rect(node->geometry_rect);
if (node->color) free(node->color);
if (node->link) {
if (node->link->ref) free(node->link->ref);
free(node->link);
}
if (node->comment_data) {
if (node->comment_data->body) free(node->comment_data->body);
if (node->comment_data->markup) free(node->comment_data->markup);
free(node->comment_data);
}
free(node);
}
return CYBERIADA_NO_ERROR;
}
static int cyberiada_destroy_all_nodes(CyberiadaNode* node)
{
CyberiadaNode* n;
if(node != NULL) {
do {
n = node;
node = node->next;
cyberiada_destroy_node(n);
} while (node);
}
return CYBERIADA_NO_ERROR;
}
CyberiadaCommentSubject* cyberiada_new_comment_subject(CyberiadaCommentSubjectType type)
{
CyberiadaCommentSubject* cs = (CyberiadaCommentSubject*)malloc(sizeof(CyberiadaCommentSubject));
memset(cs, 0, sizeof(CyberiadaCommentSubject));
cs->type = type;
return cs;
}
static CyberiadaCommentSubject* cyberiada_copy_comment_subject(CyberiadaCommentSubject* src)
{
CyberiadaCommentSubject* dst;
if (!src) {
return NULL;
}
dst = cyberiada_new_comment_subject(src->type);
if (src->fragment) {
cyberiada_copy_string(&(dst->fragment), &(dst->fragment_len), src->fragment);
}
return dst;
}
CyberiadaEdge* cyberiada_new_edge(const char* id, const char* source, const char* target)
{
CyberiadaEdge* new_edge;
if (!source || !target) {
return NULL;
}
new_edge = (CyberiadaEdge*)malloc(sizeof(CyberiadaEdge));
memset(new_edge, 0, sizeof(CyberiadaEdge));
new_edge->type = cybEdgeTransition;
cyberiada_copy_string(&(new_edge->id), &(new_edge->id_len), id);
cyberiada_copy_string(&(new_edge->source_id), &(new_edge->source_id_len), source);
cyberiada_copy_string(&(new_edge->target_id), &(new_edge->target_id_len), target);
return new_edge;
}
static CyberiadaEdge* cyberiada_copy_edge(CyberiadaEdge* src)
{
CyberiadaEdge* dst;
if (!src) {
return NULL;
}
dst = cyberiada_new_edge(src->id, src->source_id, src->target_id);
dst->type = src->type;
if (src->action) {
dst->action = cyberiada_copy_action(src->action);
}
if (src->comment_subject) {
dst->comment_subject = cyberiada_copy_comment_subject(src->comment_subject);
}
if (src->geometry_polyline) {
dst->geometry_polyline = htree_copy_polyline(src->geometry_polyline);
}
if (src->geometry_source_point) {
dst->geometry_source_point = htree_copy_point(src->geometry_source_point);
}
if (src->geometry_target_point) {
dst->geometry_target_point = htree_copy_point(src->geometry_target_point);
}
if (src->geometry_label_point) {
dst->geometry_label_point = htree_copy_point(src->geometry_label_point);
}
if (src->geometry_label_rect) {
dst->geometry_label_rect = htree_copy_rect(src->geometry_label_rect);
}
if (src->color) {
cyberiada_copy_string(&(dst->color), &(dst->color_len), src->color);
}
return dst;
}
static int cyberiada_destroy_edge(CyberiadaEdge* e)
{
if (!e) {
return CYBERIADA_BAD_PARAMETER;
}
if (e->id) free(e->id);
if (e->source_id) free(e->source_id);
if (e->target_id) free(e->target_id);
if (e->action) cyberiada_destroy_action(e->action);
if (e->comment_subject) {
if (e->comment_subject->fragment) free(e->comment_subject->fragment);
free(e->comment_subject);
}
if (e->geometry_polyline) {
htree_destroy_polyline(e->geometry_polyline);
}
if (e->geometry_source_point) htree_destroy_point(e->geometry_source_point);
if (e->geometry_target_point) htree_destroy_point(e->geometry_target_point);
if (e->geometry_label_point) htree_destroy_point(e->geometry_label_point);
if (e->geometry_label_rect) htree_destroy_rect(e->geometry_label_rect);
if (e->color) free(e->color);
free(e);
return CYBERIADA_NO_ERROR;
}
static regex_t cyberiada_edge_action_regexp;
static regex_t cyberiada_node_action_regexp;
static regex_t cyberiada_node_legacy_action_regexp;
/*static regex_t cyberiada_newline_regexp;*/
static regex_t cyberiada_spaces_regexp;
static int cyberiada_init_action_regexps(void)
{
if (regcomp(&cyberiada_edge_action_regexp, CYBERIADA_ACTION_EDGE_REGEXP, REG_EXTENDED)) {
ERROR("cannot compile edge action regexp\n");
return CYBERIADA_ASSERT;
}
if (regcomp(&cyberiada_node_action_regexp, CYBERIADA_ACTION_NODE_REGEXP, REG_EXTENDED)) {
ERROR("cannot compile edge action regexp\n");
return CYBERIADA_ASSERT;
}
if (regcomp(&cyberiada_node_legacy_action_regexp, CYBERIADA_ACTION_LEGACY_REGEXP, REG_EXTENDED)) {
ERROR("cannot compile edge action regexp\n");
return CYBERIADA_ASSERT;
}
/* if (regcomp(&cyberiada_newline_regexp, CYBERIADA_ACTION_NEWLINE_REGEXP, REG_EXTENDED)) {
ERROR("cannot compile new line regexp\n");
return CYBERIADA_ASSERT;
}*/
if (regcomp(&cyberiada_spaces_regexp, CYBERIADA_ACTION_SPACES_REGEXP, REG_EXTENDED)) {
ERROR("cannot compile new line regexp\n");
return CYBERIADA_ASSERT;
}
return CYBERIADA_NO_ERROR;
}
static int cyberiada_free_action_regexps(void)
{
regfree(&cyberiada_edge_action_regexp);
regfree(&cyberiada_node_action_regexp);
regfree(&cyberiada_node_legacy_action_regexp);
/* regfree(&cyberiada_newline_regexp);*/
regfree(&cyberiada_spaces_regexp);
return CYBERIADA_NO_ERROR;
}
static int cyberiaga_matchres_action_regexps(const char* text,
const regmatch_t* pmatch, size_t pmatch_size,
char** trigger, char** guard, char** behavior)
{
size_t i;
char* part;
int start, end;
if (pmatch_size != CYBERIADA_ACTION_REGEXP_MATCHES) {
ERROR("bad action regexp match array size\n");
return CYBERIADA_ASSERT;
}
for (i = 0; i < pmatch_size; i++) {
if (i != CYBERIADA_ACTION_REGEXP_MATCH_TRIGGER &&
i != CYBERIADA_ACTION_REGEXP_MATCH_GUARD &&
i != CYBERIADA_ACTION_REGEXP_MATCH_ACTION) {
continue;
}
start = pmatch[i].rm_so;
end = pmatch[i].rm_eo;
if (end > start && text[start] != 0) {
part = (char*)malloc((size_t)(end - start + 1));
strncpy(part, &text[start], (size_t)(end - start));
part[(size_t)(end - start)] = 0;
} else {
part = "";
}
if (i == CYBERIADA_ACTION_REGEXP_MATCH_TRIGGER) {
*trigger = part;
} else if (i == CYBERIADA_ACTION_REGEXP_MATCH_GUARD) {
*guard = part;
} else {
/* i == ACTION_REGEXP_MATCH_ACTION */
*behavior= part;
}
}
return CYBERIADA_NO_ERROR;
}
/*static int cyberiaga_matchres_newline(const regmatch_t* pmatch, size_t pmatch_size,
size_t* next_block)
{
if (pmatch_size != ACTION_NL_REGEXP_MATCHES) {
ERROR("bad new line regexp match array size\n");
return CYBERIADA_ASSERT;
}
if (next_block) {
*next_block = (size_t)pmatch[pmatch_size - 1].rm_so;
}
return CYBERIADA_NO_ERROR;
}*/
static int decode_utf8_strings(char** trigger, char** guard, char** behavior)
{
char* oldptr;
size_t len;
if (*trigger && **trigger) {
oldptr = *trigger;
*trigger = utf8_decode(*trigger, strlen(*trigger), &len);
free(oldptr);
}
if (*guard && **guard) {
oldptr = *guard;
*guard = utf8_decode(*guard, strlen(*guard), &len);
free(oldptr);
}
if (*behavior && **behavior) {
oldptr = *behavior;
*behavior = utf8_decode(*behavior, strlen(*behavior), &len);
free(oldptr);
}
return CYBERIADA_NO_ERROR;
}
static int cyberiada_decode_edge_action(const char* text, CyberiadaAction** action)
{
int res;
size_t buffer_len;
char *trigger = "", *guard = "", *behavior = "";
char *buffer;
regmatch_t pmatch[CYBERIADA_ACTION_REGEXP_MATCHES];
buffer = utf8_encode(text, strlen(text), &buffer_len);
if (!buffer) {
*action = NULL;
return CYBERIADA_NO_ERROR;
}
if ((res = regexec(&cyberiada_edge_action_regexp, buffer,
CYBERIADA_ACTION_REGEXP_MATCHES, pmatch, 0)) != 0) {
if (res == REG_NOMATCH) {
ERROR("edge action text didn't match the regexp\n");
return CYBERIADA_ACTION_FORMAT_ERROR;
} else {
ERROR("edge action regexp error %d\n", res);
return CYBERIADA_ASSERT;
}
}
if (cyberiaga_matchres_action_regexps(buffer,
pmatch, CYBERIADA_ACTION_REGEXP_MATCHES,
&trigger, &guard, &behavior) != CYBERIADA_NO_ERROR) {
return CYBERIADA_ASSERT;
}
decode_utf8_strings(&trigger, &guard, &behavior);
/* DEBUG("\n");
DEBUG("edge action:\n");
DEBUG("trigger: %s\n", trigger);
DEBUG("guard: %s\n", guard);
DEBUG("action: %s\n", action);*/
if (*trigger || *guard || *behavior) {
cyberiada_string_trim(trigger);
cyberiada_string_trim(guard);
cyberiada_string_trim(behavior);
*action = cyberiada_new_action(cybActionTransition, trigger, guard, behavior);
} else {
*action = NULL;
}
if (*trigger) free(trigger);
if (*guard) free(guard);
if (*behavior) free(behavior);
free(buffer);
return CYBERIADA_NO_ERROR;
}
static int cyberiada_add_action(const char* trigger, const char* guard, const char* behavior,
CyberiadaAction** action)
{
CyberiadaAction* new_action;
CyberiadaActionType type;
if (trigger) {
if (strcmp(trigger, CYBERIADA_ACTION_TRIGGER_ENTRY) == 0) {
type = cybActionEntry;
} else if (strcmp(trigger, CYBERIADA_ACTION_TRIGGER_EXIT) == 0) {
type = cybActionExit;
} else if (strcmp(trigger, CYBERIADA_ACTION_TRIGGER_DO) == 0) {
type = cybActionDo;
} else {
type = cybActionTransition;
}
}
/*DEBUG("\n");
DEBUG("node action:\n");
DEBUG("trigger: %s\n", trigger);
DEBUG("guard: %s\n", guard);
DEBUG("behavior: %s\n", behavior);
DEBUG("type: %d\n", type);*/
new_action = cyberiada_new_action(type, trigger, guard, behavior);
if (*action) {
CyberiadaAction* b = *action;
while (b->next) b = b->next;
b->next = new_action;
} else {
*action = new_action;
}
return CYBERIADA_NO_ERROR;
}
static int cyberiada_decode_state_block_action(const char* text, CyberiadaAction** action)
{
int res;
char *trigger = "", *guard = "", *behavior = "";
regmatch_t pmatch[CYBERIADA_ACTION_REGEXP_MATCHES];
if ((res = regexec(&cyberiada_node_action_regexp, text,
CYBERIADA_ACTION_REGEXP_MATCHES, pmatch, 0)) != 0) {
if (res == REG_NOMATCH) {
ERROR("node block action text didn't match the regexp\n");
return CYBERIADA_ACTION_FORMAT_ERROR;
} else {
ERROR("node block action regexp error %d\n", res);
return CYBERIADA_ASSERT;
}
}
if (cyberiaga_matchres_action_regexps(text,
pmatch, CYBERIADA_ACTION_REGEXP_MATCHES,
&trigger, &guard, &behavior) != CYBERIADA_NO_ERROR) {
return CYBERIADA_ASSERT;
}
decode_utf8_strings(&trigger, &guard, &behavior);
cyberiada_string_trim(trigger);
cyberiada_string_trim(guard);
cyberiada_string_trim(behavior);
cyberiada_add_action(trigger, guard, behavior, action);
if (*trigger) free(trigger);
if (*guard) free(guard);
if (*behavior) free(behavior);
return CYBERIADA_NO_ERROR;
}
static int cyberiada_decode_state_actions(const char* text, CyberiadaAction** actions)
{
int res;
size_t buffer_len;
char *buffer, *start, *block, *block2, *next;
/* regmatch_t pmatch[ACTION_NL_REGEXP_MATCHES];*/
buffer = utf8_encode(text, strlen(text), &buffer_len);
next = buffer;
*actions = NULL;
if (!buffer) {
return CYBERIADA_NO_ERROR;
}
while (*next) {
start = next;
block = strstr(start, CYBERIADA_NEWLINE);
block2 = strstr(start, CYBERIADA_NEWLINE_RN);
if (block2 && ((block && (block > block2)) || !block)) {
block = block2;
*block2 = 0;
next = block2 + 4;
} else if (block) {
*block = 0;
next = block + 2;
} else {
block = start;
next = start + strlen(block);
}
/* if ((res = regexec(&cyberiada_newline_regexp, start,
ACTION_NL_REGEXP_MATCHES, pmatch, 0)) != 0) {
if (res != REG_NOMATCH) {
ERROR("newline regexp error %d\n", res);
return CYBERIADA_ASSERT;
}
}
if (res == REG_NOMATCH) {
block = start;
start = start + strlen(block);
} else {
if (cyberiaga_matchres_newline(pmatch, ACTION_NL_REGEXP_MATCHES,
&next_block) != CYBERIADA_NO_ERROR) {
return CYBERIADA_ASSERT;
}
block = start;
start[next_block - 1] = 0;
start = start + next_block;
DEBUG("first part: '%s'\n", block);
DEBUG("second part: '%s'\n", start);
}*/
if (regexec(&cyberiada_spaces_regexp, start, 0, NULL, 0) == 0) {
continue ;
}
if ((res = cyberiada_decode_state_block_action(start, actions)) != CYBERIADA_NO_ERROR) {
ERROR("error while decoding state block %s: %d\n", start, res);
return res;
}
}
free(buffer);
return CYBERIADA_NO_ERROR;
}
static int cyberiada_decode_state_actions_yed(const char* text, CyberiadaAction** actions)
{
int res;
char *buffer, *next, *start, *block;
size_t buffer_len;
CyberiadaList *sections_list = NULL, *list;
regmatch_t pmatch[CYBERIADA_ACTION_LEGACY_MATCHES];
buffer = utf8_encode(text, strlen(text), &buffer_len);
next = buffer;
*actions = NULL;
if (!buffer) {