-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpdp_psystem_source_binary.cpp
More file actions
1271 lines (1039 loc) · 36.1 KB
/
pdp_psystem_source_binary.cpp
File metadata and controls
1271 lines (1039 loc) · 36.1 KB
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
/*
ABCD-GPU: Simulating Population Dynamics P systems on the GPU, by DCBA
ABCD-GPU is a subproject of PMCGPU (Parallel simulators for Membrane
Computing on the GPU)
Copyright (c) 2015 Research Group on Natural Computing, Universidad de Sevilla
Dpto. Ciencias de la Computación e Inteligencia Artificial
Escuela Técnica Superior de Ingeniería Informática,
Avda. Reina Mercedes s/n, 41012 Sevilla (Spain)
Author: Miguel Ángel Martínez-del-Amor
This file is part of ABCD-GPU.
ABCD-GPU is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ABCD-GPU 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with ABCD-GPU. If not, see <http://www.gnu.org/licenses/>. */
#include "pdp_psystem_source_binary.h"
#include <string.h>
using namespace std;
/**********************************************************/
/* CONSTRUCTORS: */
/* */
/* Binary reader of a PDP System: maravillosos procedures */
/**********************************************************/
PDP_Psystem_source_binary::PDP_Psystem_source_binary(const char * filename, Options options) {
this->options=options;
// Next is not used yet
order[0]=GENERAL_INFO;
order[1]=BLOCKS_PI;
order[2]=BLOCKS_ENV;
order[3]=CONFIGURATION;
// INITIALIZE POINTERS
delete_id_objects=delete_id_environments=delete_id_membranes=true;
id_objects=NULL;
id_environments=NULL;
id_membranes=NULL;
membrane_hierarchy=NULL;
lengthU=NULL;
lengthV=NULL;
active_membrane=NULL;
rules=NULL;
lengthUp=NULL;
lengthVp=NULL;
obj_lhs=obj_lhs_m=obj_rhs=obj_rhs_m=NULL;
prob=NULL;
block_precision=charge=NULL;
env_lengthU=NULL;
ini_multiset=NULL;
ini_info=NULL;
ini_charge=NULL;
if (options->verbose>0)
cout << "[1] STARTING READING THE PDP SYSTEM FROM " << filename << endl;
/************************************/
/* Open the input file as binary */
is.open (filename, ios::binary);
/* If couldn't open it, error*/
check_file_error(is.fail(), string("Couldn't open binary file ") + filename, is);
}
PDP_Psystem_source_binary::PDP_Psystem_source_binary(Options options) {
this->options=options;
}
PDP_Psystem_source_binary::~PDP_Psystem_source_binary() {
if (!is.fail() && is.good() && is.is_open())
is.close();
if (delete_id_objects&&id_objects) {
for (int i=0;i<options->num_objects;i++) {
if (id_objects[i]) delete [] id_objects[i];
}
delete[]id_objects;
}
if (delete_id_environments&&id_environments) {
for (int i=0;i<options->num_environments;i++) {
if (id_environments[i]) delete [] id_environments[i];
}
delete[]id_environments;
}
if (delete_id_membranes&&id_membranes) {
for (int i=0;i<options->num_membranes;i++) {
if (id_membranes[i]) delete [] id_membranes[i];
}
delete[]id_membranes;
}
if (membrane_hierarchy) delete[]membrane_hierarchy;
if (lengthU) delete []lengthU;
if (lengthV) delete []lengthV;
if (lengthUp) delete []lengthUp;
if (lengthVp) delete []lengthVp;
if (active_membrane) delete []active_membrane;
if (rules) delete []rules;
if (prob) delete []prob;
if (obj_lhs) delete []obj_lhs;
if (obj_rhs) delete []obj_rhs;
if (obj_lhs_m) delete []obj_lhs_m;
if (obj_rhs_m) delete []obj_rhs_m;
if (block_precision) delete []block_precision;
if (charge) delete []charge;
if (env_lengthU) delete []env_lengthU;
if (ini_info) delete []ini_info;
if (ini_multiset) {
for (int i=0;i<options->num_membranes*options->num_environments;i++) {
if (ini_multiset[i]) delete [] ini_multiset[i];
}
delete [] ini_multiset;
}
if (ini_charge) delete []ini_charge;
}
/********************************************/
/* AUXILIARY FUNCTIONS: */
/* */
/* Read each section of a binary file */
/********************************************/
/* Auxiliary function:
If condition is true, there is an error: close the binary file,
print the message and throws an exception. */
void PDP_Psystem_source_binary::check_file_exception(bool condition, string message) {
if (condition) {
//is.close();
throw FileException("Error while reading file: "+message);
//cerr << "Error while reading file: " << message << endl;
}
}
bool PDP_Psystem_source_binary::read_header() {
#ifdef BIN_DEBUG
cout << "READING HEAD:" << endl;
#endif
/************************/
/* Read the file header */
unsigned int recbytes = read_bytes(is,HEADER_SIZE);
if ((recbytes & HEADER_BITMASK) != HEADER) {
cout << "Error, the file is not a good binary file" << endl;
return false;
}
else if ((recbytes & (HEADER_VERSION_BITMASK|HEADER_VARIANT_BITMASK)) != 0X21) {
cout << "Error, the file version is not compatible" << endl;
return false;
}
#ifdef BIN_DEBUG
cout << "Using binary file version " << (recbytes & HEADER_VARIANT_BITMASK) << endl;
#endif
/************************/
/***************************/
/* Read the file subheader */
recbytes = read_bytes(is,SUBHEADER_SIZE);
#ifdef BIN_DEBUG
cout << "recbytes=" << recbytes << endl;
#endif
precision.objects = (1 << ((recbytes&0xC00000)>>22));
precision.environments = (1 << ((recbytes&0x300000)>>20));
precision.membranes = (1 << ((recbytes&0xC0000)>>18));
precision.pi_rules = (1 << ((recbytes&0x30000)>>16));
precision.env_rules = (1 << ((recbytes&0xC000)>>14));
precision.multisets = (1 << ((recbytes&0x3000)>>12));
precision.ini_num_objects = (1 << ((recbytes&0xC00)>>10));
precision.ini_mult = (1 << ((recbytes&0x300)>>8));
id_strings.objects = ((recbytes & 0x4)>>2);
id_strings.environments = ((recbytes & 0x2)>>1);
id_strings.membranes = (recbytes & 0x1);
#ifdef BIN_DEBUG
cout << "#objects="<<precision.objects << endl;
cout << "#environments="<<precision.environments << endl;
cout << "#membranes="<<precision.membranes << endl;
cout << "#pi_rules="<<precision.pi_rules << endl;
cout << "#env_rules="<<precision.env_rules << endl;
cout << "#multisets="<<precision.multisets << endl;
cout << "#mult_rules="<<precision.ini_num_objects << endl;
cout << "#mult_multisets="<<precision.ini_mult << endl;
cout << "#showobjects="<< id_strings.objects << endl;
cout << "#showenvironments="<< id_strings.environments << endl;
cout << "#showmembranes="<< id_strings.membranes << endl;
/***************************/
#endif
return true;
}
bool PDP_Psystem_source_binary::read_global_sizes() {
#ifdef BIN_DEBUG
cout << "READING GLOBAL SIZES:" <<endl;
#endif
char text_buffer [MAX_TEXT_SIZE];
/*********************/
/* Read global sizes */
number_objects=read_bytes(is,precision.objects);
check_file_exception(number_objects==0,"No objects defined");
if (id_strings.objects) {
id_objects=new char*[number_objects];
for (int i=0;i<number_objects;i++){
int len=read_text(is,text_buffer,MAX_TEXT_SIZE,END_OF_TEXT);
check_file_exception(len<=0, "Text size is not correct.");
id_objects[i]=new char[len+1];
strcpy(id_objects[i],text_buffer);
}
}
else id_objects=NULL;
number_environments=read_bytes(is,precision.environments);
check_file_exception(number_environments==0,"No environments defined");
if (id_strings.environments) {
id_environments=new char*[number_environments];
for (int i=0;i<number_environments;i++){
int len=read_text(is,text_buffer,MAX_TEXT_SIZE,END_OF_TEXT);
check_file_exception(len<=0, "Text size is not correct.");
id_environments[i]=new char[len+1];
strcpy(id_environments[i],text_buffer);
}
}
else id_environments=NULL;
number_membranes=read_bytes(is,precision.membranes);
check_file_exception(number_membranes==0,"No membranes defined");
membrane_hierarchy=new unsigned int[number_membranes];
if (id_strings.membranes)
id_membranes=new char*[number_membranes];
else id_membranes=NULL;
for (int i=0;i<number_membranes;i++){
unsigned int parent=read_bytes(is,precision.membranes);
membrane_hierarchy[i]=parent;
if (id_strings.membranes) {
int len=read_text(is,text_buffer,MAX_TEXT_SIZE,END_OF_TEXT);
check_file_exception(len<=0, "Text size is not correct.");
id_membranes[i]=new char[len+1];
strcpy(id_membranes[i],text_buffer);
}
}
number_rule_blocks_pi=read_bytes(is,precision.pi_rules);
number_rule_blocks_env=read_bytes(is,precision.env_rules);
#ifdef BIN_DEBUG
cout << "number objects=" << number_objects << endl;
if (id_strings.objects && id_objects!=NULL) {
cout << "Object IDs:" << endl;
for (int i=0;i<number_objects;i++) cout << id_objects[i] << ", ";
cout << endl;
}
cout << "number membranes=" << number_membranes << endl << "Membrane hierarchy:" << endl;
for (int i=0;i<number_membranes;i++) cout << "parent[" << i << "]=" << membrane_hierarchy[i] << endl;
if (id_strings.membranes && id_membranes!=NULL) {
cout << "Memb IDs:" << endl;
for (int i=0;i<number_membranes;i++) cout << id_membranes[i] << ", ";
cout << endl;
}
cout << "number environments=" << number_environments << endl;
if (id_strings.environments && id_environments!=NULL) {
cout << "Env IDs:" << endl;
for (int i=0;i<number_environments;i++) cout << id_environments[i] << ", ";
cout << endl;
}
cout << "number pi rules=" << number_rule_blocks_pi << endl;
cout << "number env rules=" << number_rule_blocks_env << endl;
#endif
options->num_blocks_env=number_rule_blocks_env;
options->num_rule_blocks=number_rule_blocks_pi;
options->num_objects=number_objects;
options->num_membranes=number_membranes;
options->num_environments=number_environments;
/*********************/
/**********************************/
/* Initialize aux data structures */
lengthU= new short int[options->num_rule_blocks];
lengthV= new short int[options->num_rule_blocks];
active_membrane= new unsigned int[options->num_rule_blocks+options->num_blocks_env];
block_precision= new char[options->num_rule_blocks+options->num_blocks_env];
rules = new unsigned int[options->num_rule_blocks+options->num_blocks_env+1];
charge = new char[options->num_rule_blocks];
/**********************************/
return true;
}
/* Required macros for extracting information from rule blocks information byte
*/
#define PREC_MULTIPLICITY(byte) (1<<(((byte)&0xC0)>>6))
#define PREC_NUM_OBJS_LHS(byte) (1<<(((byte)&0x20)>>5))
#define PREC_NUM_OBJS_RHS(byte) (1<<(((byte)&0x10)>>4))
#define PREC_NUM_RULES(byte) (1<<(((byte)&0xC)>>2))
#define DIF_PROB_ENV(byte) !(((byte)&0x2)>>1)
#define SHOW_PARENT_MEMBRANE(byte) ((byte)&0x1)
#define CHARGE_ALPHA(charge) ((charge&0xC0)>>6)
#define CHARGE_BETA(charge) ((charge&0x30)>>4)
bool PDP_Psystem_source_binary::read_inf_1() {
#ifdef BIN_DEBUG
cout<< "ENTERING FOR INFORMATION 1"<<endl;
#endif
unsigned int objects_in_lhs=0;
number_rules_pi=number_rules_env=number_rules=0;
prob_length=0;
rules[0]=0;
options->max_lhs=0;
options->max_num_rules=0;
for (unsigned int i=0;i<options->num_rule_blocks;i++) {
int nr=0;
block_precision[i]=read_bytes(is,BLOCK_INF_BYTE_SIZE);
nr=read_bytes(is,PREC_NUM_RULES(block_precision[i]));
rules[i+1]=rules[i]+nr;
lengthU[i]=read_bytes(is,PREC_NUM_OBJS_LHS(block_precision[i]));
active_membrane[i]=read_bytes(is,precision.membranes);
if (SHOW_PARENT_MEMBRANE(block_precision[i]))
read_bytes(is,precision.membranes);
charge[i]=read_bytes(is,CHARGE_SIZE);
objects_in_lhs+=lengthU[i];
number_rules_pi+=nr;
//prob_length+=(DIF_PROB_ENV(block_precision[i]))?nr:nr*options->num_environments;
prob_length+=nr*options->num_environments;
// Update max lhs
if (options->max_lhs<lengthU[i]) options->max_lhs=lengthU[i];
if (options->max_num_rules<nr) options->max_num_rules=nr;
#ifdef BIN_DEBUG
cout << "PI Block " << i << ":"<< endl;
cout << "\tPrecision byte=" << hex << (short int) block_precision[i] << dec << endl;
cout << "\tNo Rules="<<rules[i+1]-rules[i]<<endl;
cout << "\tLength LHS="<<lengthU[i]<<endl;
cout << "\tAMembrane="<<active_membrane[i]<<endl;
cout << "\tCharges Alf="<<CHARGE_ALPHA(charge[i])<<endl;
cout << "\tCharges Bet="<<CHARGE_BETA(charge[i])<<endl;
#endif
}
for (unsigned int i=0;i<options->num_blocks_env;i++) {
int nr=0;
unsigned int bid=i+options->num_rule_blocks;
block_precision[bid]=read_bytes(is,BLOCK_INF_BYTE_SIZE);
nr=read_bytes(is,PREC_NUM_RULES(block_precision[bid]));
rules[bid+1]=nr+rules[bid];
active_membrane[bid]=read_bytes(is,precision.environments);
number_rules_env+=nr;
//prob_length+=(DIF_PROB_ENV(block_precision[i]))?nr:nr*options->num_environments;
prob_length+=nr;
if (options->max_num_rules<nr) options->max_num_rules=nr;
#ifdef BIN_DEBUG
cout << "ENV Block " << i << " (" << bid <<"):"<< endl;
cout << "\tNo Rules="<<nr<<endl;
cout << "\tEnv="<<active_membrane[i]<<endl;
#endif
}
number_rules=number_rules_pi+number_rules_env;
#ifdef BIN_DEBUG
cout << "Total rules Pi="<<number_rules_pi<<endl;
cout << "Total rules Env="<<number_rules_env<<endl;
cout << "Total rules="<<number_rules<<endl;
cout << "Total length probs="<<prob_length<<endl;
#endif
obj_lhs=new unsigned int[objects_in_lhs+options->num_blocks_env];
obj_lhs_m=new unsigned int[objects_in_lhs];
lengthUp=new short int[number_rules];
lengthVp=new short int[number_rules];
prob=new float[prob_length];
num_obj_lhs_blocks_pi=objects_in_lhs;
return true;
}
bool PDP_Psystem_source_binary::read_inf_2() {
#ifdef BIN_DEBUG
cout<< "ENTERING FOR INFORMATION 2"<<endl;
#endif
unsigned int objects_in_rhs=0;
unsigned int object_pointer=0; // global pointer for objects
options->max_rhs=0;
for (unsigned int i=0;i<options->num_rule_blocks;i++) {
for (int r=rules[i]; r<rules[i+1];r++) {
lengthUp[r]=read_bytes(is,PREC_NUM_OBJS_RHS(block_precision[i]));
objects_in_rhs+=lengthUp[r];
if (DIF_PROB_ENV(block_precision[i])) {
for (int e=0;e<options->num_environments;e++) {
int pr=read_bytes(is,PROB_SIZE);
prob[r*options->num_environments+e]=pr/PROB_PREC;
}
}
else {
int pr=read_bytes(is,PROB_SIZE);
for (int e=0;e<options->num_environments;e++)
prob[r*options->num_environments+e]=pr/PROB_PREC;
}
if (options->max_rhs<lengthUp[r]) options->max_rhs=lengthUp[r];
}
short int lhsl=lengthU[i];
lengthU[i]=read_bytes(is,PREC_NUM_OBJS_LHS(block_precision[i]));
for (int o=0; o<lengthU[i]; o++) {
obj_lhs[object_pointer]=read_bytes(is,precision.objects);
obj_lhs_m[object_pointer++]=read_bytes(is,PREC_MULTIPLICITY(block_precision[i]));//,precision.multisets);
}
lengthV[i]=read_bytes(is,PREC_NUM_OBJS_LHS(block_precision[i]));
//cout << lengthU[i] << "+" << lengthV[i] <<"="<< lengthU[i]+lengthV[i] << "!=" << lhsl << endl;
check_file_exception((lengthU[i]+lengthV[i])!=lhsl,"Different lengths of LHS in info 1 vs 2 ");
for (int o=0; o<lengthV[i]; o++) {
obj_lhs[object_pointer]=read_bytes(is,precision.objects);
obj_lhs_m[object_pointer++]=read_bytes(is,PREC_MULTIPLICITY(block_precision[i]));//,precision.multisets);
}
#ifdef BIN_DEBUG
cout << "PI Block " << i << ":"<< endl;
for (int r=rules[i]; r<rules[i+1];r++)
cout << "\tLength RHS="<<lengthUp[r]<<endl <<
"\tProb=" << prob[r*options->num_environments]<<endl;
cout << "\tLHS=";
int o_it=object_pointer-lhsl;
for (int o=0; o<lengthU[i]; o++) {
cout << id_objects[obj_lhs[o_it]]/*obj_lhs[o_it]*/ << "*" << obj_lhs_m[o_it] << " ";o_it++;
}
cout <<"[ ";
for (int o=0; o<lengthV[i]; o++) {
cout << id_objects[obj_lhs[o_it]]/*obj_lhs[o_it]*/ << "*" << obj_lhs_m[o_it] << " ";o_it++;
}
cout <<"]^" << CHARGE_ALPHA(charge[i]) << "_" << active_membrane[i] << endl;
#endif
}
num_obj_rhs_rules_pi=objects_in_rhs;
for (unsigned int i=0;i<options->num_blocks_env;i++) {
obj_lhs[object_pointer++]=read_bytes(is,precision.objects);
unsigned int bid=i+options->num_rule_blocks;
for (int r=rules[bid]; r<rules[bid+1];r++) {
lengthUp[r]=read_bytes(is,PREC_NUM_OBJS_RHS(block_precision[bid]));
objects_in_rhs+=lengthUp[r];
int prob_offset=rules[options->num_rule_blocks]*options->num_environments+
r-rules[options->num_rule_blocks];
int pr=read_bytes(is,PROB_SIZE);
prob[prob_offset]=pr/PROB_PREC;
if (options->max_rhs<lengthUp[r]) options->max_rhs=lengthUp[r];
}
#ifdef BIN_DEBUG
cout << "ENV Block " << i << ":"<< endl;
for (int r=rules[bid]; r<rules[bid+1];r++)
cout << "\tLength RHS="<<lengthUp[r]<<endl <<
"\tProb=" << prob[r]<<endl;
cout << "\tLHS=(";
cout << id_objects[obj_lhs[object_pointer-1]] << ")" << endl;
#endif
}
obj_rhs=new unsigned int[objects_in_rhs];
obj_rhs_m=new unsigned int[objects_in_rhs];
return true;
}
bool PDP_Psystem_source_binary::read_inf_3() {
#ifdef BIN_DEBUG
cout<< "ENTERING FOR INFORMATION 3"<<endl;
#endif
unsigned int object_pointer=0; // global pointer for objects
for (unsigned int i=0;i<options->num_rule_blocks;i++) {
#ifdef BIN_DEBUG
cout << "PI Block " << i << ":"<< endl;
#endif
for (int r=rules[i]; r<rules[i+1];r++) {
int rhsl=lengthUp[r];
lengthUp[r]=read_bytes(is,PREC_NUM_OBJS_RHS(block_precision[i]));
for (int o=0; o<lengthUp[r]; o++) {
obj_rhs[object_pointer]=read_bytes(is,precision.objects);
obj_rhs_m[object_pointer++]=read_bytes(is,PREC_MULTIPLICITY(block_precision[i]));//,precision.multisets);
}
lengthVp[r]=read_bytes(is,PREC_NUM_OBJS_LHS(block_precision[i]));
check_file_exception((lengthUp[r]+lengthVp[r])!=rhsl,"Different lengths of RHS in rule "+(r-rules[i]));
for (int o=0; o<lengthVp[r]; o++) {
obj_rhs[object_pointer]=read_bytes(is,precision.objects);
obj_rhs_m[object_pointer++]=read_bytes(is,PREC_MULTIPLICITY(block_precision[i]));//,precision.multisets);
}
#ifdef BIN_DEBUG
cout << "\tRule " << r-rules[i] << ", RHS=";
int o_it=object_pointer-rhsl;
for (int o=0; o<lengthUp[r]; o++) {
cout << id_objects[obj_rhs[o_it]]/*obj_lhs[o_it]*/ << "*" << obj_rhs_m[o_it] << " ";o_it++;
}
cout <<"[ ";
for (int o=0; o<lengthVp[r]; o++) {
cout << id_objects[obj_rhs[o_it]]/*obj_lhs[o_it]*/ << "*" << obj_rhs_m[o_it] << " ";o_it++;
}
cout <<"]^" << CHARGE_BETA(charge[i]) << "_" << active_membrane[i] << endl;
#endif
}
}
for (unsigned int i=0;i<options->num_blocks_env;i++) {
unsigned int bid=i+options->num_rule_blocks;
#ifdef BIN_DEBUG
cout << "Env Block " << bid << ":"<< endl;
#endif
for (int r=rules[bid]; r<rules[bid+1];r++) {
for (int o=0; o<lengthUp[r]; o++) {
obj_rhs[object_pointer]=read_bytes(is,precision.objects);
obj_rhs_m[object_pointer++]=read_bytes(is,precision.environments);
}
#ifdef BIN_DEBUG
cout << "\tRule " << r-rules[i] << ", RHS=";
int o_it=object_pointer-lengthUp[r];
for (int o=0; o<lengthUp[r]; o++) {
cout << "(" << id_objects[obj_rhs[o_it]]/*obj_lhs[o_it]*/ << ")_" << obj_rhs_m[o_it] << " ";o_it++;
}
#endif
}
}
return true;
}
// Not necessary for version 34
//#define INITIAL_CHARGE(info) ((info>>(6+(precision.ini_num_objects-1)*8))&0x3)
//#define INITIAL_NUMBER_OBJECTS(info) (info&(0xFFFFFFFF^(0xC0<<(precision.ini_num_objects-1)*8)))
#define INI_OFFSET(e,q) (e*options->num_membranes+q)
bool PDP_Psystem_source_binary::read_multisets() {
ini_multiset = new unsigned int* [options->num_environments*options->num_membranes];
ini_info = new unsigned int [options->num_environments*options->num_membranes];
ini_charge = new char [options->num_environments*options->num_membranes];
for (int e=0; e<options->num_environments; e++) {
for (int q=0; q<options->num_membranes; q++) {
ini_charge[INI_OFFSET(e,q)] = read_bytes(is,1);
unsigned int num_objs = ini_info[INI_OFFSET(e,q)] = read_bytes(is,precision.ini_num_objects);
//int num_objs=INITIAL_NUMBER_OBJECTS(ini_info[INI_OFFSET(e,q)]);
if (num_objs<=0) ini_multiset[INI_OFFSET(e,q)]=NULL;
else ini_multiset[INI_OFFSET(e,q)]=new unsigned int [num_objs*2];
for (int o=0;o<num_objs;o++) {
int obj=read_bytes(is,precision.objects);
int mult=read_bytes(is,precision.ini_mult);
ini_multiset[INI_OFFSET(e,q)][o*2]=obj;
ini_multiset[INI_OFFSET(e,q)][o*2+1]=mult;
}
}
}
#ifdef BIN_DEBUG
for (int e=0; e<options->num_environments; e++) {
cout << "Env "<<e<<endl;
for (int q=0; q<options->num_membranes; q++) {
//cout << "\tMembr "<<q<<" ("<<INITIAL_NUMBER_OBJECTS(ini_info[INI_OFFSET(e,q)])<<" #objects):";
cout << "\tMembr "<<q<<" (charge: " << CHARGE_ALPHA(ini_charge[INI_OFFSET(e,q)]) << ", "<<ini_info[INI_OFFSET(e,q)]<<" #objects):";
//for (int o=0;o<(INITIAL_NUMBER_OBJECTS(ini_info[INI_OFFSET(e,q)]))*2;o+=2) {
for (int o=0;o<(ini_info[INI_OFFSET(e,q)])*2;o+=2) {
cout << id_objects[ini_multiset[INI_OFFSET(e,q)][o]] <<"*"
<< ini_multiset[INI_OFFSET(e,q)][o+1]<< " ";
}
cout << endl;
}
}
#endif
return true;
}
/*******************************************************/
/* Public methods inherited from pdp_system_source */
/*******************************************************/
bool PDP_Psystem_source_binary::start() {
try {
read_header();
read_global_sizes();
read_inf_1();
read_inf_2();
read_inf_3();
read_multisets();
} catch (FileException fe) {
cerr << fe.getMessage() << endl;
return false;
}
return true;
}
/*********************/
/* Procedures for Pi */
unsigned int PDP_Psystem_source_binary::pi_loop_rule_blocks(){
rule_block_it=0;
lhs_it=rhs_it=0;
return options->num_rule_blocks;
}
bool PDP_Psystem_source_binary::pi_next_rule_block() {
if (rule_block_it < options->num_rule_blocks-1) {
lhs_it+=lengthU[rule_block_it]+lengthV[rule_block_it];
for (int r=rules[rule_block_it];r<rules[rule_block_it+1];r++)
rhs_it+=lengthUp[r]+lengthVp[r];
rule_block_it++;
return true;
}
return false;
}
unsigned int PDP_Psystem_source_binary::pi_loop_lhs() {
return lengthV[rule_block_it]+lengthU[rule_block_it];
}
char PDP_Psystem_source_binary::pi_lhs_charge() {
return CHARGE_ALPHA(this->charge[rule_block_it]);
}
unsigned int PDP_Psystem_source_binary::pi_lhs_membrane() {
return active_membrane[rule_block_it];
}
unsigned int PDP_Psystem_source_binary::pi_lhs_parent_membrane() {
return this->membrane_hierarchy[active_membrane[rule_block_it]];
}
unsigned int PDP_Psystem_source_binary::pi_lhs_loop_U() {
U_it=0; V_it=lengthV[rule_block_it]+2;
return lengthU[rule_block_it];
}
unsigned int PDP_Psystem_source_binary::pi_lhs_loop_V() {
V_it=0; U_it=lengthU[rule_block_it]+2;
return lengthV[rule_block_it];
}
bool PDP_Psystem_source_binary::pi_lhs_next_object(unsigned int & object, unsigned int & multiplicity) {
int oidx=0;
if (U_it<lengthU[rule_block_it]) {
oidx=lhs_it+U_it++;
}
else if (V_it<lengthV[rule_block_it]) {
oidx=lhs_it+lengthU[rule_block_it]+V_it++;
}
else {
return false;
}
unsigned int obj=random() % options->num_objects;
object = obj_lhs[oidx];
multiplicity = obj_lhs_m[oidx];
return true;
}
unsigned int PDP_Psystem_source_binary::pi_loop_rules() {
rule_it=rules[rule_block_it];
rhs_it_internal=rhs_it;
return rules[rule_block_it+1]-rules[rule_block_it];
}
bool PDP_Psystem_source_binary::pi_next_rule() {
if (rule_it<rules[rule_block_it+1]-1) {
rhs_it_internal+=lengthUp[rule_it]+lengthVp[rule_it];
rule_it++;
return true;
}
return false;
}
float* PDP_Psystem_source_binary::pi_rule_probabilities() {
return &(prob[rule_it*options->num_environments]);
}
unsigned int PDP_Psystem_source_binary::pi_loop_rhs() {
return lengthVp[rule_it]+lengthUp[rule_it];
}
char PDP_Psystem_source_binary::pi_rhs_charge() {
return CHARGE_BETA(this->charge[rule_block_it]);
}
unsigned int PDP_Psystem_source_binary::pi_rhs_membrane() {
return active_membrane[rule_block_it];
}
unsigned int PDP_Psystem_source_binary::pi_rhs_loop_U() {
Up_it=0; Vp_it=lengthVp[rule_it]+2;
return lengthUp[rule_it];
}
unsigned int PDP_Psystem_source_binary::pi_rhs_loop_V() {
Vp_it=0; Up_it=lengthUp[rule_it]+2;
return lengthVp[rule_it];
}
bool PDP_Psystem_source_binary::pi_rhs_next_object(unsigned int & object, unsigned int & multiplicity) {
int oidx=0;
if (Up_it<lengthUp[rule_it]) {
oidx=rhs_it_internal+Up_it++;
}
else if (Vp_it<lengthVp[rule_it]) {
oidx=rhs_it_internal+lengthUp[rule_it]+Vp_it++;
}
else {
return false;
}
object = obj_rhs[oidx];
multiplicity = obj_rhs_m[oidx];
return true;
}
/****************************************************/
/* Corresponding to rule blocks of the environments */
unsigned int PDP_Psystem_source_binary::env_loop_rule_blocks() {
block_env_it=0;
rule_env_it=rules[options->num_rule_blocks];
rhs_it=num_obj_rhs_rules_pi;
return options->num_blocks_env;
}
bool PDP_Psystem_source_binary::env_next_rule_block() {
if (block_env_it < options->num_blocks_env-1) {
for (int r=rules[block_env_it];r<rules[block_env_it+1];r++)
rhs_it+=lengthUp[r];
block_env_it++;
return true;
}
return false;
}
unsigned int PDP_Psystem_source_binary::env_get_object_lhs() {
return obj_lhs[num_obj_lhs_blocks_pi+block_env_it];
}
unsigned int PDP_Psystem_source_binary::env_get_environment() {
return this->active_membrane[options->num_rule_blocks+block_env_it];
}
unsigned int PDP_Psystem_source_binary::env_loop_rules() {
int benv=options->num_rule_blocks+block_env_it;
rule_env_it=rules[benv];
rhs_it_internal=rhs_it;
return rules[benv+1]-rules[benv];
}
bool PDP_Psystem_source_binary::env_next_rule() {
int benv=options->num_rule_blocks+block_env_it;
if (rule_env_it < rules[benv+1] -1) {
rhs_it_internal+=lengthUp[rule_env_it];
rule_env_it++;
return true;
}
return false;
}
float PDP_Psystem_source_binary::env_get_probability() {
int prob_offset=rules[options->num_rule_blocks]*options->num_environments+
rule_env_it-rules[options->num_rule_blocks];
return prob[prob_offset];
}
unsigned int PDP_Psystem_source_binary::env_loop_rhs() {
Up_it=0;
return lengthUp[rule_env_it];
}
bool PDP_Psystem_source_binary::env_next_object(unsigned int & object, unsigned int & environment) {
int oidx=0;
if (Up_it<lengthUp[rule_env_it]) {
oidx=rhs_it_internal+Up_it++;
}
else {
return false;
}
object = obj_rhs[oidx];
environment = obj_rhs_m[oidx];
return true;
}
/*************************************/
/* Corresponding to the configuration*/
bool PDP_Psystem_source_binary::conf_loop() {
return true;
}
unsigned int PDP_Psystem_source_binary::conf_loop_environments() {
env_it=0;
return options->num_environments;
}
bool PDP_Psystem_source_binary::conf_next_environment() {
if (env_it<options->num_environments-1) {
env_it++;
return true;
}
return false;
}
unsigned int PDP_Psystem_source_binary::conf_loop_membranes() {
memb_it=0;
return options->num_membranes;
}
bool PDP_Psystem_source_binary::conf_next_membrane() {
if (memb_it<options->num_membranes-1) {
memb_it++;
return true;
}
return false;
}
unsigned int PDP_Psystem_source_binary::conf_parent_membrane() {
return this->membrane_hierarchy[memb_it];
}
char PDP_Psystem_source_binary::conf_charge_membrane() {
//return INITIAL_CHARGE(ini_info[INI_OFFSET(env_it,memb_it)]);
return CHARGE_ALPHA(ini_charge[INI_OFFSET(env_it,memb_it)]);
}
unsigned int PDP_Psystem_source_binary::conf_loop_objects() {
obj_it=0;
return options->num_objects;
}
bool PDP_Psystem_source_binary::conf_next_object(unsigned int & object, unsigned int & multiplicity) {
//if (obj_it<INITIAL_NUMBER_OBJECTS(ini_info[INI_OFFSET(env_it,memb_it)])*2-1) {
if (ini_info[INI_OFFSET(env_it,memb_it)]==NULL) return false;
if (obj_it<ini_info[INI_OFFSET(env_it,memb_it)]*2-1) {
object=ini_multiset[INI_OFFSET(env_it,memb_it)][obj_it];
multiplicity=ini_multiset[INI_OFFSET(env_it,memb_it)][obj_it+1];
obj_it+=2;
return true;
}
return false;
}
char ** PDP_Psystem_source_binary::get_objects_ids() {
delete_id_objects=false;
return id_objects;
}
char ** PDP_Psystem_source_binary::get_environments_ids() {
delete_id_environments=false;
return id_environments;
}
char ** PDP_Psystem_source_binary::get_membranes_ids() {
delete_id_membranes=false;
return id_membranes;
}
/*****************************************************************/
/* Writing the debug file from P-Lingua testing examples (no. 2) */
void PDP_Psystem_source_binary::write_test_binary() {
ofstream outfile ("small.bin",ofstream::binary);
char buffer [8];
// Header
buffer[0]=0xAF;
buffer[1]=0x12;
buffer[2]=0xFA;
buffer[3]=0x21; // 2 = PDP systems
outfile.write(buffer,4);
// Subheader
buffer[0]=0;
buffer[1]=0;
buffer[2]=0x04; // Show object labels
outfile.write(buffer,3);
// Number of objects
buffer[0]=0x09; // 8 objects plus the empty object #
outfile.write(buffer,1);
// Objects ids
buffer[0]='#';
buffer[1]='\0';
outfile.write(buffer,2);
buffer[0]='a';
outfile.write(buffer,2);
buffer[0]='b';
outfile.write(buffer,2);
buffer[0]='c';
outfile.write(buffer,2);
buffer[0]='d';
outfile.write(buffer,2);
buffer[0]='e';
outfile.write(buffer,2);
buffer[0]='f';
outfile.write(buffer,2);
buffer[0]='g';
outfile.write(buffer,2);
buffer[0]='h';
outfile.write(buffer,2);
// Number of Environments
buffer[0]=0x01;
outfile.write(buffer,1);
// Number of membranes
buffer[0]=0x03; // 2 membranes plus the environment as a region
outfile.write(buffer,1);