forked from FLAME-HPC/xparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml.tmpl
1646 lines (1496 loc) · 47.4 KB
/
xml.tmpl
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
/**
* \file xml.c
* \brief Holds xml reading and writing functions.
*/
#include "header.h"
/** \fn void read_int_static_array(char * buffer, int * j, int ** int_static_array)
* \brief Reads integer static array.
*/
int read_int_static_array(char * buffer, int /*@unused@*/ buffer_size, int * j, int * int_static_array, int /*@unused@*/ size)
{
int arraycount = 0;
int array_k;
char arraydata[100000];
while(buffer[(*j)] != '{')
{
if(buffer[(*j)] != ' ') return -1;
else if(buffer[(*j)] == '\0') return -1;
else (*j)++;
}
(*j)++;
while(buffer[(*j)] != '}')
{
array_k = 0;
while(buffer[(*j)] != ',' && buffer[(*j)] != '}')
{
if(buffer[(*j)] == '\0') return -1;
arraydata[array_k] = buffer[(*j)];
array_k++;
(*j)++;
}
arraydata[array_k] = '\0';
if (arraycount < size) int_static_array[arraycount] = atoi(arraydata);
arraycount++;
if(buffer[(*j)] != '}') (*j)++;
}
if (arraycount > size)
{
fprintf(stderr, "WARNING: %d 'int' values provided for static array of size %d. Surplus values ignored.\n",
arraycount, size);
}
(*j)++;
return 0;
}
/** \fn void read_float_static_array(char * buffer, int * (*j), int ** float_static_array)
* \brief Reads float static array.
*/
int read_float_static_array(char * buffer, int /*@unused@*/ buffer_size, int * j, float * float_static_array, int /*@unused@*/ size)
{
int arraycount = 0;
int array_k;
char arraydata[100000];
while(buffer[(*j)] != '{')
{
if(buffer[(*j)] != ' ') return -1;
else if(buffer[(*j)] == '\0') return -1;
else (*j)++;
}
(*j)++;
while(buffer[(*j)] != '}')
{
array_k = 0;
while(buffer[(*j)] != ',' && buffer[(*j)] != '}')
{
if(buffer[(*j)] == '\0') return -1;
arraydata[array_k] = buffer[(*j)];
array_k++;
(*j)++;
}
arraydata[array_k] = '\0';
if (arraycount < size) float_static_array[arraycount] = (float)atof(arraydata);
arraycount++;
if(buffer[(*j)] != '}') (*j)++;
}
if (arraycount > size)
{
fprintf(stderr, "WARNING: %d 'float' values provided for static array of size %d. Surplus values ignored.\n",
arraycount, size);
}
(*j)++;
return 0;
}
/** \fn void read_double_static_array(char * buffer, int * (*j), int ** double_static_array)
* \brief Reads double static array.
*/
int read_double_static_array(char * buffer, int /*@unused@*/ buffer_size, int * j, double * double_static_array, int /*@unused@*/ size)
{
int arraycount = 0;
int array_k;
char arraydata[100000];
while(buffer[(*j)] != '{')
{
if(buffer[(*j)] != ' ') return -1;
else if(buffer[(*j)] == '\0') return -1;
else (*j)++;
}
(*j)++;
while(buffer[(*j)] != '}')
{
array_k = 0;
while(buffer[(*j)] != ',' && buffer[(*j)] != '}')
{
if(buffer[(*j)] == '\0') return -1;
arraydata[array_k] = buffer[(*j)];
array_k++;
(*j)++;
}
arraydata[array_k] = '\0';
if (arraycount < size) double_static_array[arraycount] = atof(arraydata);
arraycount++;
if(buffer[(*j)] != '}') (*j)++;
}
if (arraycount > size)
{
fprintf(stderr, "WARNING: %d 'double' values provided for static array of size %d. Surplus values ignored.\n",
arraycount, size);
}
(*j)++;
return 0;
}
/** \fn void read_char_static_array(char * buffer, int * (*j), int ** char_static_array)
* \brief Reads char static array.
*/
int read_char_static_array(char * buffer, int /*@unused@*/ buffer_size, int * j, char * char_static_array, int size)
{
int arraycount;
while(buffer[(*j)] == ' ')
{
(*j)++;
}
for(arraycount = 0; arraycount < size; arraycount++)
{
char_static_array[arraycount] = buffer[(*j)];
(*j)++;
}
//(*j)++;
return 0;
}
/** \fn void read_int_dynamic_array(char * buffer, int * (*j), int ** int_dynamic_array)
* \brief Reads integer dynamic array.
*/
int read_int_dynamic_array(char * buffer, int /*@unused@*/ buffer_size, int * j, int_array * int_dynamic_array)
{
int arraycount = 0;
int array_k;
char arraydata[100000];
while(buffer[(*j)] != '{')
{
if(buffer[(*j)] != ' ') return -1;
else if(buffer[(*j)] == '\0') return -1;
else (*j)++;
}
(*j)++;
while(buffer[(*j)] != '}')
{
array_k = 0;
while(buffer[(*j)] != ',' && buffer[(*j)] != '}')
{
if(buffer[(*j)] == '\0') return -1;
arraydata[array_k] = buffer[(*j)];
array_k++;
(*j)++;
}
arraydata[array_k] = '\0';
add_int(int_dynamic_array, atoi(arraydata));
arraycount++;
if(buffer[(*j)] != '}') (*j)++;
}
(*j)++;
return 0;
}
/** \fn void read_float_dynamic_array(char * buffer, int * (*j), int ** float_dynamic_array)
* \brief Reads float dynamic array.
*/
int read_float_dynamic_array(char * buffer, int /*@unused@*/ buffer_size, int * j, float_array * float_dynamic_array)
{
int arraycount = 0;
int array_k;
char arraydata[100000];
while(buffer[(*j)] != '{')
{
if(buffer[(*j)] != ' ') return -1;
else if(buffer[(*j)] == '\0') return -1;
else (*j)++;
}
(*j)++;
while(buffer[(*j)] != '}')
{
array_k = 0;
while(buffer[(*j)] != ',' && buffer[(*j)] != '}')
{
if(buffer[(*j)] == '\0') return -1;
arraydata[array_k] = buffer[(*j)];
array_k++;
(*j)++;
}
arraydata[array_k] = '\0';
add_float(float_dynamic_array, (float)atof(arraydata));
arraycount++;
if(buffer[(*j)] != '}') (*j)++;
}
(*j)++;
return 0;
}
/** \fn void read_double_dynamic_array(char * buffer, int * (*j), int ** double_dynamic_array)
* \brief Reads double dynamic array.
*/
int read_double_dynamic_array(char * buffer, int /*@unused@*/ buffer_size, int * j, double_array * double_dynamic_array)
{
int arraycount = 0;
int array_k;
char arraydata[100000];
while(buffer[(*j)] != '{')
{
if(buffer[(*j)] != ' ') return -1;
else if(buffer[(*j)] == '\0') return -1;
else (*j)++;
}
(*j)++;
while(buffer[(*j)] != '}')
{
array_k = 0;
while(buffer[(*j)] != ',' && buffer[(*j)] != '}')
{
if(buffer[(*j)] == '\0') return -1;
arraydata[array_k] = buffer[(*j)];
array_k++;
(*j)++;
}
arraydata[array_k] = '\0';
add_double(double_dynamic_array, atof(arraydata));
arraycount++;
if(buffer[(*j)] != '}') (*j)++;
}
(*j)++;
return 0;
}
/** \fn void read_char_dynamic_array(char * buffer, int * (*j), int ** char_dynamic_array)
* \brief Reads char dynamic array.
*/
int read_char_dynamic_array(char * buffer, int /*@unused@*/ buffer_size, int * j, char_array * char_dynamic_array)
{
if(*j > buffer_size) return -1;
while(buffer[(*j)] == ' ')
{
(*j)++;
}
while(buffer[(*j)] != '\0' && buffer[(*j)] != ',' && buffer[(*j)] != '}')
{
add_char(char_dynamic_array, buffer[(*j)]);
(*j)++;
}
return 0;
}
<?foreach datatype?>/** \fn void read_$name(char * buffer, int * j, $name * temp_datatype)
* \brief Reads $name datatype.
*/
int read_$name(char * buffer, int /*@unused@*/ buffer_size, int * j, $name * temp_datatype)
{
<?if single_vars?>int array_k;
char arraydata[100000];<?end if?>
<?if has_arrays_or_adts?>int rc;<?end if?>
while(buffer[(*j)] != '{')
{
if(buffer[(*j)] != ' ') return -1;
else if(buffer[(*j)] == '\0') return -1;
else (*j)++;
}
(*j)++;
<?foreach datatypevar?><?if modeldatatype?> while(buffer[*j] != '{') (*j)++;
<?if not_array?> rc = read_$type(buffer, buffer_size, j, &(*temp_datatype).$name);
if(rc != 0) return -1;<?end if?><?if array?><?if static_array?>
rc = read_$type_static_array(buffer, buffer_size, j, (*temp_datatype).$name, $arraylength);<?end if?><?if dynamic_array?>
rc = read_$notarraytype_dynamic_array(buffer, buffer_size, j, &(*temp_datatype).$name);<?end if?>
if(rc != 0) return -1;
<?end if?><?end if?><?if not_modeldatatype?><?if not_array?> (*temp_datatype).$name = $default_value;
array_k = 0;<?end if?>
<?if notlast?>while(buffer[*j] != ',')<?end if?><?if last?>while(buffer[*j] != '}')<?end if?>
{
if(buffer[(*j)] == '\0') return -1;
<?if not_array?>arraydata[array_k] = buffer[*j];
array_k++;
(*j)++;<?end if?><?if array?><?if notchar_array?>while(buffer[*j] != '{') (*j)++;<?end if?>
<?if static_array?>rc = read_$type_static_array(buffer, buffer_size, j, (*temp_datatype).$name, $arraylength);<?end if?><?if dynamic_array?>
rc = read_$notarraytype_dynamic_array(buffer, buffer_size, j, &(*temp_datatype).$name);<?end if?>
if(rc != 0) { printf("Error: reading variable '$name' of type '$type'\n"); return -1; }<?end if?>
}
<?if not_array?>arraydata[array_k] = '\0';
(*temp_datatype).$name = <?if not_char?>ato$c_type<?end if?>(arraydata<?if char?>[array_k-1]<?end if?>);<?end if?><?end if?>
(*j)++;
<?end foreach?>
return 0;
}
int read_$name_dynamic_array(char * buffer, int buffer_size, int * j, $name_array * temp_datatype_array)
{
int arraycount = 0;
int rc;
<?foreach datatypevar?><?if not_array?><?if modeldatatype?>$type $name;
# ifndef S_SPLINT_S
init_$type(&$name);
# endif<?end if?><?end if?>
<?if array?><?if static_array?>$type $name[$arraylength];
# ifndef S_SPLINT_S
init_$type_static_array($name, $arraylength);
# endif<?end if?>
<?if dynamic_array?>$type $name;
# ifndef S_SPLINT_S
init_$type(&$name);
# endif<?end if?><?end if?><?end foreach?>
while(buffer[(*j)] != '{')
{
if(buffer[(*j)] != ' ') return -1;
else if(buffer[(*j)] == '\0') return -1;
else (*j)++;
}
(*j)++;
while(buffer[(*j)] != '\0' && buffer[(*j)] != '}')
{
if(buffer[(*j)] == '{')
{
add_$name(temp_datatype_array<?foreach datatypevar?>, <?if not_array?><?if not_modeldatatype?>$default_value<?end if?><?if modeldatatype?>&$name<?end if?><?end if?><?if array?><?if dynamic_array?>&<?end if?>$name<?end if?><?end foreach?>);
rc = read_$name(buffer, buffer_size, j, &(*temp_datatype_array).array[arraycount]);
if(rc != 0) { printf("Error: reading variable '$name' of type '$type'\n"); return -1; }
arraycount++;
(*j)++;
}
while(buffer[(*j)] != '}' && buffer[(*j)] != '\0' && buffer[(*j)] != '{') { (*j)++; }
}
<?foreach datatypevar?><?if array?><?if dynamic_array?>free_$type(&$name);
<?end if?><?end if?><?end foreach?>
return 0;
}
int read_$name_static_array(char * buffer, int buffer_size, int * j, $name * temp_datatype_array, int size)
{
int arraycount;
int rc;
while(buffer[(*j)] != '{')
{
if(buffer[(*j)] != ' ') return -1;
else if(buffer[(*j)] == '\0') return -1;
else (*j)++;
}
(*j)++;
for(arraycount = 0; arraycount < size; arraycount++)
{
rc = read_$name(buffer, buffer_size, j, &temp_datatype_array[arraycount]);
if(rc != 0) { printf("Error: reading variable '$name' of type '$type'\n"); return -1; }
if(arraycount < (size-1)) while(buffer[(*j)] != '{') { (*j)++; }
}
(*j)++;
return 0;
}
<?end foreach?>
int readEnvironmentXML(char * location)
{
FILE * file;
char c = '\0';
char buffer[100000];
int index = 0;
int in_environment = 0;
<?foreach envvar?>int in_$name = 0;
<?end foreach?>
buffer[0] = '\0';
/* Open file */
if((file = fopen(location, "r"))==NULL)
{
printf("Error: cannot open import xml file '%s'\n", location);
exit(0);
}
printf("Reading environment data from: %s\n", location);
/* Keep reading file until EOF */
while(c != (char)EOF)
{
/* Get the next char from the file */
c = (char)fgetc(file);
if(c == '>')
{
buffer[index] = '\0';
if(strcmp(buffer, "environment") == 0) in_environment = 1;
if(strcmp(buffer, "/environment") == 0) in_environment = 0;
<?foreach envvar?>if(strcmp(buffer, "$name") == 0) in_$name = 1;
if(strcmp(buffer, "/$name") == 0) in_$name = 0;
<?end foreach?>
index = 0;
buffer[index] = '\0';
}
else if(c == '<')
{
buffer[index] = '\0';
if(in_environment == 1)
{
<?foreach envvar?>if(in_$name == 1) { FLAME_environment_variable_$name = <?if float?>(float)<?end if?><?if not_char?>ato$c_type<?end if?>(buffer<?if char?>[0]<?end if?>); }
<?end foreach?>
}
index = 0;
buffer[index] = '\0';
}
else
{
buffer[index] = c;
if(index < 999) index++;
}
}
/* Close file */
(void)fclose(file);
return 0;
}
int readAgentXML(char * location,
double cloud_data[],
int partition_method,
int flag,
int number_partitions,
int agent_count,
int * itno)
{
FILE * file;
char c = '\0';
char buffer[100000];
char agentname[10000];
int index = 0;
int j; /* Index for reading arrays */
int rc;
int in_itno = 0;
int FLAME_in_xagent = 0;
int FLAME_in_name = 0;
<?foreach xagent?>int in_$name_agent = 0;
<?end foreach?>
<?foreach allvar?>int in_$name = 0;
<?end foreach?>
<?foreach xagent?>xmachine_memory_$name * current_$name_agent = NULL;
<?end foreach?>
/* Things for round-robin partitioning */
int geometric = 1;
int other = 2;
double posx=0.0, posy=0.0, posz=0.0;
j = 0;
rc = 0;
/* Use j and rc to stop compiler warnings if not used */
if(j == 0 && rc == 0) {}
agentname[0] = '\0';
buffer[0] = '\0';
/* Open file */
if((file = fopen(location, "r"))==NULL)
{
printf("Error: cannot open import xml file '%s'\n", location);
exit(0);
}
printf("Reading agent data from: %s\n", location);
/* Keep reading file until EOF */
while(c != (char)EOF)
{
/* Get the next char from the file */
c = (char)fgetc(file);
if(c == '>')
{
buffer[index] = '\0';
if(strcmp(buffer, "itno") == 0) in_itno = 1;
if(strcmp(buffer, "/itno") == 0) in_itno = 0;
if(strcmp(buffer, "xagent") == 0)
{
FLAME_in_xagent = 1;
agentname[0] = '\0';
}
if(strcmp(buffer, "/xagent") == 0)
{
<?foreach xagent?><?if notfirst?>else <?end if?>if(strcmp(agentname, "$name") == 0)
{
if(current_$name_agent == NULL) { printf("Memory error reading $name agent\n"); exit(0); }
posx = (double)<?if use_xvar?>current_$name_agent->$xvar<?end if?><?if no_xvar?>0.0<?end if?>;
posy = (double)<?if use_yvar?>current_$name_agent->$yvar<?end if?><?if no_yvar?>0.0<?end if?>;
posz = (double)<?if use_zvar?>current_$name_agent->$zvar<?end if?><?if no_zvar?>0.0<?end if?>;
/* If flag is zero just read the data. We'll partition later.
* If flag is not zero we aleady have partition data so can read and distribute to the current node.*/
if( flag == 0 )
{
/* Next line should be commented out? */
add_$name_agent_internal(current_$name_agent, $name_$start_state_state);
/* Update the cloud data */
if ( posx < cloud_data[0] ) cloud_data[0] = posx;
if ( posx > cloud_data[1] ) cloud_data[1] = posx;
if ( posy < cloud_data[2] ) cloud_data[2] = posy;
if ( posy > cloud_data[3] ) cloud_data[3] = posy;
if ( posz < cloud_data[2] ) cloud_data[4] = posz;
if ( posz > cloud_data[3] ) cloud_data[5] = posz;
}
else
{
if(partition_method == geometric)
{
if (
((current_node->partition_data[0] == SPINF) || (current_node->partition_data[0] != SPINF && posx >= current_node->partition_data[0])) &&
((current_node->partition_data[1] == SPINF) || (current_node->partition_data[1] != SPINF && posx < current_node->partition_data[1])) &&
((current_node->partition_data[2] == SPINF) || (current_node->partition_data[2] != SPINF && posy >= current_node->partition_data[2])) &&
((current_node->partition_data[3] == SPINF) || (current_node->partition_data[3] != SPINF && posy < current_node->partition_data[3])) &&
((current_node->partition_data[4] == SPINF) || (current_node->partition_data[4] != SPINF && posz >= current_node->partition_data[4])) &&
((current_node->partition_data[5] == SPINF) || (current_node->partition_data[5] != SPINF && posz < current_node->partition_data[5]))
)
{
add_$name_agent_internal(current_$name_agent, $name_$start_state_state);
}
}
else if (partition_method == other)
{
if (agent_count % number_partitions == 0)
{
add_$name_agent_internal(current_$name_agent, $name_$start_state_state);
}
++agent_count;
}
}
}
<?end foreach?>else
{
printf("Warning: agent name undefined - '%s'\n", agentname);
exit(0);
}
agentname[0] = '\0';
FLAME_in_xagent = 0;
<?foreach xagent?>in_$name_agent = 0;
<?end foreach?>
}
if(strcmp(buffer, "name") == 0) FLAME_in_name = 1;
if(strcmp(buffer, "/name") == 0) FLAME_in_name = 0;
<?foreach allvar?>if(strcmp(buffer, "$name") == 0) { in_$name = 1; }
if(strcmp(buffer, "/$name") == 0) { in_$name = 0; }
<?end foreach?>
index = 0;
buffer[index] = '\0';
}
else if(c == '<')
{
buffer[index] = '\0';
if(in_itno && FLAME_in_xagent == 0) *itno = atoi(buffer);
if(FLAME_in_xagent == 1)
{
if(FLAME_in_name == 1)
{
strcpy(agentname, buffer);
<?foreach xagent?><?if notfirst?>else <?end if?>if(strcmp(agentname, "$name") == 0)
{
current_$name_agent = init_$name_agent();
in_$name_agent = 1;
}
<?end foreach?>else
{
printf("Warning: agent name undefined - '%s'\n", agentname);
exit(0);
}
}
<?foreach xagent?>else if(in_$name_agent == 1)
{
<?foreach xagentvar?> if(in_$name) { <?if not_array?><?if not_modeldatatype?>current_$agent_name_agent->$name = <?if not_char?>ato$c_type<?end if?>(buffer<?if char?>[0]<?end if?>);<?end if?><?if modeldatatype?>j = 0;
rc = read_$type(buffer, index, &j, ¤t_$agent_name_agent->$name);
if(rc != 0) { printf("Error: reading '$agent_name' agent variable '$name' of type '$type'\n"); exit(0); }<?end if?><?end if?><?if array?><?if static_array?><?if modeldatatype?><?end if?>j = 0;
rc = read_$type_static_array(buffer, index, &j, current_$agent_name_agent->$name, $arraylength);<?end if?><?if dynamic_array?>j = 0;
rc = read_$notarraytype_dynamic_array(buffer, index, &j, ¤t_$agent_name_agent->$name);<?end if?>
if(rc != 0) { printf("Error: reading '$agent_name' agent variable '$name' of type '$type'\n"); exit(0); }<?end if?> }
<?end foreach?> }<?end foreach?>
}
index = 0;
buffer[index] = '\0';
}
else
{
buffer[index] = c;
if(index < 99999) index++;
else
{
printf("Error: agent reading buffer too small\n");
printf("%s\n", buffer);
exit(0);
}
}
}
/* Close file */
(void)fclose(file);
return 0;
}
/** \fn void check_location_exists(char * location)
* \brief Check that a directory exists.
* \param location The directory location to check.
*/
int check_location_exists(char * location)
{
/* Save a dummy file to directory and remove.
* This checks directory exists and is writable */
FILE * file;
char * buffer;
buffer = (char *)malloc( (strlen(location) + strlen("dummy.temp") + 1) * sizeof(char));
strcpy(buffer, location);
strcat(buffer, "dummy.temp");
if((file = fopen(buffer, "w")) == NULL) return 0;
(void)fclose(file);
/* Remove dummy file */
remove(buffer);
return 1;
}
<?if parallel?>
/** \fn void readprepartitionedinitialstates(char * filename, int * itno, xmachine ** agent_list)
* \brief Read prepartitioned initial X-machine memory starting values from a set of files. The root for
* these files is filename and the actual filenames are formed as filename<node_id>-0.xml. NOTE that
* this must only be called for parallel applications.
* \param fileroot The path to the file with root for filenames.
* \param filelocation The directory of the location
* \param itno Pointer to the iteration number.
*/
void readprepartitionedinitialstates(char * fileroot, char * filelocation, int * itno)
{
/* Pointer to file */
FILE *file;
/* Char and char buffer for reading file to */
char c = '\0';
char buffer[100000];
char FLAME_location[10000];
char FLAME_format[10000];
char FLAME_type[10000];
FLAME_output * current_FLAME_output = NULL;
/* Temporary node and head of associated agent list to allow adding agents */
/*@unused@*/ //node_information temp_node;
/* Variables for checking tags */
int reading = 1;
int i = 0;
int rc;
int in_tag = 0;
int in_itno = 0;
int FLAME_in_imports = 0;
int FLAME_in_import = 0;
int FLAME_in_outputs = 0;
int FLAME_in_output = 0;
int FLAME_in_location = 0;
int FLAME_in_format = 0;
int FLAME_in_type = 0;
int FLAME_in_time = 0;
int FLAME_in_period = 0;
int FLAME_in_phase = 0;
int FLAME_in_name = 0;
int number_partitions=0;
int node_number=0;
int agent_count = 0;
/* Filename must be constructed from fileroot argument */
char filename[100000];
/* Following variables required by readAgentXML. */
double cloud_data[6];
int flag = 0; /* Set flag=0 so readAgentXML reads and adds agents and does nothing else. */
int partition_method = 0; /* This is irrelevant because flag = 0 */
/* Initialise environment vars */
<?foreach envvar?>FLAME_environment_variable_$name = $default_value;
<?end foreach?>
MPI_Comm_size (MPI_COMM_WORLD, &number_partitions);
MPI_Comm_rank (MPI_COMM_WORLD, &node_number);
agent_count = node_number;
sprintf(filename, "%s%d-0.xml", fileroot, node_number);
/* Open config file to read-only */
if((file = fopen(filename, "r"))==NULL)
{
printf("No input file '%s'\n", filename);
return ;
}
/* Initialise variables */
*itno = 0;
printf("Reading initial data file: %s\n", filename);
/* Read file until end of xml */
while(reading==1)
{
/* Get the next char from the file */
c = (char)fgetc(file);
/* If the end of a tag */
if(c == '>')
{
/* Place 0 at end of buffer to make chars a string */
buffer[i] = 0;
if(strcmp(buffer, "states") == 0) reading = 1;
if(strcmp(buffer, "/states") == 0) reading = 0;
if(strcmp(buffer, "itno") == 0) in_itno = 1;
if(strcmp(buffer, "/itno") == 0) in_itno = 0;
if(strcmp(buffer, "imports") == 0) FLAME_in_imports = 1;
if(strcmp(buffer, "/imports") == 0) FLAME_in_imports = 0;
if(strcmp(buffer, "import") == 0)
{
/*FLAME_location[0] = '\0';*/
strcpy(FLAME_location, filelocation);
FLAME_format[0] = '\0';
FLAME_type[0] = '\0';
FLAME_in_import = 1;
}
if(strcmp(buffer, "/import") == 0)
{
if(strcmp("agent", FLAME_type) == 0 || strcmp("environment", FLAME_type) == 0)
{
if(strcmp("xml", FLAME_format) == 0)
{
if(strcmp("agent", FLAME_type) == 0) readAgentXML(FLAME_location, cloud_data, partition_method, flag, number_partitions, agent_count, itno);
if(strcmp("environment", FLAME_type) == 0) readEnvironmentXML(FLAME_location);
}
else
{
printf("Error: import format '%s' is unsupported\n", FLAME_format);
exit(0);
}
}
else
{
printf("Error: import type '%s' is not agent or environment\n", FLAME_type);
exit(0);
}
FLAME_in_import = 0;
}
if(strcmp(buffer, "location") == 0) FLAME_in_location = 1;
if(strcmp(buffer, "/location") == 0) FLAME_in_location = 0;
if(strcmp(buffer, "format") == 0) FLAME_in_format = 1;
if(strcmp(buffer, "/format") == 0) FLAME_in_format = 0;
if(strcmp(buffer, "type") == 0) FLAME_in_type = 1;
if(strcmp(buffer, "/type") == 0) FLAME_in_type = 0;
if(strcmp(buffer, "outputs") == 0) FLAME_in_outputs = 1;
if(strcmp(buffer, "/outputs") == 0) FLAME_in_outputs = 0;
if(strcmp(buffer, "output") == 0)
{
if(FLAME_in_outputs == 1)
{
current_FLAME_output = add_FLAME_output(&FLAME_outputs);
FLAME_in_output = 1;
}
}
if(strcmp(buffer, "/output") == 0)
{
if(FLAME_in_outputs == 1)
{
if(current_FLAME_output->type == -1)
{
printf("Error: an output type has not been set\n");
exit(0);
}
if(current_FLAME_output->format == -1)
{
printf("Error: an output format has not been set\n");
exit(0);
}
if(current_FLAME_output->location == NULL)
{
printf("Error: an output location has not been set\n");
exit(0);
}
/* If type is xml check if location exists */
if(current_FLAME_output->type == 0)
{
rc = check_location_exists(current_FLAME_output->location);
if(rc == 0)
{
printf("Error: location directory '%s' does not exist\n", current_FLAME_output->location);
exit(0);
}
}
/* Period has to be larger than 0 */
if(current_FLAME_output->period < 1)
{
printf("Error: output period is less than 1: '%d'\n", current_FLAME_output->period);
exit(0);
}
/* Phase cannot be equal or larger than period */
if(current_FLAME_output->phase >= current_FLAME_output->period)
{
printf("Error: output phase is more or equal to period: '%d'\n", current_FLAME_output->phase);
exit(0);
}
FLAME_in_output = 0;
}
}
if(strcmp(buffer, "time") == 0) FLAME_in_time = 1;
if(strcmp(buffer, "/time") == 0) FLAME_in_time = 0;
if(strcmp(buffer, "period") == 0) FLAME_in_period = 1;
if(strcmp(buffer, "/period") == 0) FLAME_in_period = 0;
if(strcmp(buffer, "phase") == 0) FLAME_in_phase = 1;
if(strcmp(buffer, "/phase") == 0) FLAME_in_phase = 0;
if(strcmp(buffer, "name") == 0) FLAME_in_name = 1;
if(strcmp(buffer, "/name") == 0) FLAME_in_name = 0;
/* End of tag and reset buffer */
in_tag = 0;
i = 0;
}
/* If start of tag */
else if(c == '<')
{
/* Place /0 at end of buffer to end numbers */
buffer[i] = 0;
/* Flag in tag */
in_tag = 1;
if(in_itno) *itno = atoi(buffer);
if(FLAME_in_imports == 1)
{
if(FLAME_in_import == 1)
{
if(FLAME_in_location == 1) strcat(FLAME_location, buffer);
if(FLAME_in_format == 1) strcpy(FLAME_format, buffer);
if(FLAME_in_type == 1) strcpy(FLAME_type, buffer);
}
}
if(FLAME_in_outputs == 1)
{
if(FLAME_in_output == 1)
{
if(FLAME_in_type == 1)
{
if(strcmp("snapshot", buffer) == 0) current_FLAME_output->type = 0;
else if(strcmp("agent", buffer) != 0)
{
printf("Error: output type is not 'snapshot' or an 'agent': '%s'\n", buffer);
exit(0);
}
}
if(FLAME_in_name == 1)
{
<?foreach xagent?>if(strcmp("$name", buffer) == 0) current_FLAME_output->type = $xagentcountplusone;
else <?end foreach?>
{
printf("Error: output name is not an agent name: '%s'\n", buffer);
exit(0);
}
}
if(FLAME_in_format == 1)
{
if(strcmp("xml", buffer) == 0) current_FLAME_output->format = 0;
else
{
printf("Error: output format is unsupported: '%s'\n", buffer);
exit(0);
}
}
if(FLAME_in_location == 1)
{
current_FLAME_output->location = (char *)malloc( (strlen(filelocation) + strlen(buffer) + 1) * sizeof(char));
strcpy(current_FLAME_output->location, filelocation);
strcat(current_FLAME_output->location, buffer);
}
if(FLAME_in_time == 1)
{
if(FLAME_in_period == 1) current_FLAME_output->period = atoi(buffer);
if(FLAME_in_phase == 1) current_FLAME_output->phase = atoi(buffer);
}
}
}
/* Reset buffer */
i = 0;
}
/* If in tag put read char into buffer */
else if(in_tag)
{
buffer[i] = c;
i++;
}
/* If in data read char into buffer */
else
{
buffer[i] = c;
i++;
}
}
/* Close the file */
(void)fclose(file);
/* Also try and read environment and agents from 0.xml */
readEnvironmentXML(filename);
readAgentXML(filename, cloud_data, partition_method, flag, number_partitions, agent_count, itno);
/* If outputs is empty add default snapshot for every iteration */
if(FLAME_outputs == NULL)
{
current_FLAME_output = add_FLAME_output(&FLAME_outputs);
current_FLAME_output->type = 0; /* snapshot */
current_FLAME_output->format = 0; /* xml */
current_FLAME_output->location = (char *)malloc( (strlen(filelocation) + 1) * sizeof(char));
strcpy(current_FLAME_output->location, filelocation); /* location = 0.xml location */
current_FLAME_output->period = 1; /* every iteration */
current_FLAME_output->phase = 0; /* no phase */
}
/* Print output settings to CLI */
for(current_FLAME_output = FLAME_outputs; current_FLAME_output != NULL; current_FLAME_output = current_FLAME_output->next)
{
printf("output: type='");
if(current_FLAME_output->type == 0) printf("snapshot");
<?foreach xagent?>else if(current_FLAME_output->type == $xagentcountplusone) printf("agent' name='$name");
<?end foreach?>else printf("undefined");
printf("' format='");
if(current_FLAME_output->format == 0) printf("xml");
else printf("undefined");
printf("' location='%s'", current_FLAME_output->location);
printf(" period='%d' phase='%d'\n", current_FLAME_output->period, current_FLAME_output->phase);
}
}
<?end if?>
/** \fn void readinitialstates(char * filename, int * itno, xmachine ** agent_list, double cloud_data[6], int flag)
* \brief Read initial X-machine memory starting values from a file.
* \param filename The path to the file.
* \param filelocation The directory of the location
* \param itno Pointer to the iteration number.
* \param agent_list List of agents in the model to be constructed by this function.
* \param cloud_data Max and min x,y,z coordinates of agents.
* \param partition_method Identifies partitioning method: 1 = geometric, 2 = round-robin
* \param flag Flag for whether to check space partitions.
*/
void readinitialstates(char * filename, char * filelocation, int * itno, double cloud_data[],
int partition_method, int flag)
{
/* Pointer to file */
FILE *file;
/* Char and char buffer for reading file to */
char c = '\0';
char buffer[100000];
char FLAME_location[10000];
char FLAME_format[10000];
char FLAME_type[10000];
FLAME_output * current_FLAME_output = NULL;