/
FileManager.cpp
1107 lines (1015 loc) · 27.3 KB
/
FileManager.cpp
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
// FileManager.cpp
#include "BoolModel.h"
// Extract the next token or delimiter from the begining of string s
String FileManager::next_token( String& s, const String& delim )
{
// Find the index of first delimiter
size_t tp = s.find_first_of( delim );
// If none found, return all of s
if( tp == String::npos )
{
tp = s.length();
}
// If the first character is a delimiter, return it
if( tp == 0 && s.length() > 0 ) tp = 1;
// Return the substring between the beginning and the delimiter
String token = s.substr(0,tp);
// Remove the same substring from the beginning of s
s = s.substr(tp);
return token;
}
String FileManager::GetLine( std::ifstream& in_file )
{
String l;
char ch = (char)in_file.get();
while( !(iscntrl(ch) && ch != '\t') && !in_file.eof() )
{
l.push_back( ch );
ch = (char) in_file.get();
}
return l;
}
// Read in and parse the text file that controls a run of the G.A.
bool FileManager::ReadControlFile( String& control_file_path )
{
// Extract path from control file, if any
size_t pe = control_file_path.rfind( '\\' );
if( pe != String::npos )
{
mPath = control_file_path.substr(0, pe+1);
}
std::ifstream cf;
cf.open( control_file_path.c_str() );
if( !cf.is_open() )
{
std::cout << "ERROR - Control File Failed to Open" << std::endl;
return false;
}
String block;
String element;
String token;
String delims = "=(){}\",";
while( !cf.eof() )
{
// Read in a block by reading and contatinating elements until a ';' is found
// NOTE: this blocking removes all white space by use of operator>>
element = ""; block = "";
while( element.find(';') == String::npos && cf.good() )
{
cf >> element;
block += element;
}
// If we really have a block
if( element.find(';') != String::npos )
{
// Get the keyword
token = next_token( block, delims );
if( token == "N" )
{
// =
token = next_token( block, delims );
if( token != "=" ) continue;
// num_vars
token = next_token( block, delims );
mNumVars = atoi( token.c_str() );
}
if( token == "WT" )
{
// Check for '='
token = next_token( block, delims );
if( token != "=" ) continue;
// Check for '{'
token = next_token( block, delims );
if( token != "{" ) continue;
// Until we reach '}'
while( token != "}" )
{
// "
token = next_token( block, delims );
if( token != "\"" ) break;
// filename
token = next_token( block, delims );
if( token.length() > 1 )
{
wt_file_list.push_back( token );
}
// eat the next two tokens " and , or " and ;
token = next_token( block, delims );
token = next_token( block, delims );
}
}
else if( token == "KO" )
{
// (1, "m_out.txt"),
// Check for '='
token = next_token( block, delims );
if( token != "=" ) continue;
// Check for '{'
token = next_token( block, delims );
if( token != "{" ) continue;
// Until we reach '}'
while( token != "}" )
{
// (
token = next_token( block, delims );
if( token != "(" ) break;
// knockout variable number
token = next_token( block, delims );
int k = atoi(token.c_str());
// , "
token = next_token( block, delims );
if( token != "," ) break;
token = next_token( block, delims );
if( token != "\"" ) break;
// filename
token = next_token( block, delims );
if( token.length() > 1 )
{
ko_file_list.push_back( KOFileSpec(k,token) );
}
// eat the next three tokens "), or ")};
token = next_token( block, delims );
token = next_token( block, delims );
token = next_token( block, delims );
}
}
else if( token == "BIO" )
{
// Check for '='
token = next_token( block, delims );
if( token != "=" ) break;
// Check for {
token = next_token( block, delims );
if( token != "{" ) break;
// Check for "
token = next_token( block, delims );
if( token == "}" ) continue;
if( token != "\"" ) break;
// Filename
token = next_token( block, delims );
if( token.length() > 1 )
{
mBioFile = token;
}
// eat the next two tokens "}
token = next_token( block, delims );
token = next_token( block, delims );
}
else if( token == "CMPLX" )
{
// Check for '='
token = next_token( block, delims );
if( token != "=" ) break;
// Check for {
token = next_token( block, delims );
if( token != "{" ) break;
// Check for "
token = next_token( block, delims );
if( token == "}" ) continue;
if( token != "\"" ) break;
// Filename
token = next_token( block, delims );
if( token.length() > 1 )
{
mDepGraphFile = token;
}
// eat the next two tokens "}
token = next_token( block, delims );
token = next_token( block, delims );
}
else if( token == "REV" )
{
// Check for '='
token = next_token( block, delims );
if( token != "=" ) break;
// Check for {
token = next_token( block, delims );
if( token != "{" ) break;
// Check for "
token = next_token( block, delims );
if( token == "}" ) continue;
if( token != "\"" ) break;
// Filename
token = next_token( block, delims );
if( token.length() > 1 )
{
mRevFile = token;
}
// eat the next two tokens "}
token = next_token( block, delims );
token = next_token( block, delims );
}
else if( token == "MODEL" )
{
// Check for '='
token = next_token( block, delims );
if( token != "=" ) continue;
// Check for '{'
token = next_token( block, delims );
if( token != "{" ) continue;
// Until we reach '}'
while( token != "}" )
{
// "
token = next_token( block, delims );
if( token != "\"" ) break;
// filename
token = next_token( block, delims );
if( token.length() > 1 )
{
model_file_list.push_back( token );
}
// eat the next two tokens " and , or " and ;
token = next_token( block, delims );
token = next_token( block, delims );
}
}
else if( token == "PARAMS" )
{
// Check for '='
token = next_token( block, delims );
if( token != "=" ) break;
// Check for {
token = next_token( block, delims );
if( token != "{" ) break;
// Check for "
token = next_token( block, delims );
if( token == "}" ) continue;
if( token != "\"" ) break;
// Filename
token = next_token( block, delims );
if( token.length() > 1 )
{
mParamFile = token;
}
// eat the next two tokens "}
token = next_token( block, delims );
token = next_token( block, delims );
}
}
}
cf.close();
return true;
}
// Read in a time series from the given file, optionally verify knockout variable kov
bool FileManager::ReadTimeSeries( String& file_path, TimeSeries& ts, size_t kov )
{
// std::cout << "file: " << file_path << std::endl;
std::ifstream in_file;
in_file.open( file_path.c_str() );
if( !in_file.good() )
{
throw( String("FileManager::ReadTimeSeries - unable to open file ") + file_path);
}
String in_line;
char ch;
size_t num_digits = 0;
size_t prev_num_digits = 0;
while( !in_file.eof() )
{
in_file.get( ch );
if( ch == '\n' )
{
if( num_digits > 0 )
{
// std::cout << "line: " << in_line << std::endl;
NTuple t( in_line );
// std::cout << "tuple: " << t << std::endl;
if( t.Size() != GAParams::NumVars() )
{
throw( "FileManager::ReadTimeSeries - time series n-tuple length not equal to number of variables" );
}
if( kov != 0 )
{
if( t[kov] )
{
// std::cout << t[kov] << std::endl;
// std::cout << kov << std::endl;
t.Reset( kov );
std::cout << "FileManager::ReadTimeSeries - knockout variable not zero in time series" << std::endl;
std::cout << " data corrected - processing will continue" << std::endl;
}
}
// Save the n-tuple in the time series
ts.push_back( t );
// Clear for next line
num_digits = 0;
in_line = "";
}
}
else
{
in_line.push_back( ch );
// Count only digits in the field
if( isdigit( ch ) )
{
if( mFieldDigits.find( ch ) == String::npos )
{
throw( "FileManager::ReadTimeSeries - invalid digit in time series" );
}
++num_digits;
}
}
}
in_file.close();
return true;
}
// Read in all of the wild-type time-series, one for each file in wt_file_list
bool FileManager::ReadWildTimeSeries( TimeSeriesVector& wt_series )
{
wt_series.clear();
mMaxWtTsLength = 0;
// For each file in wt_file_list
StringVectorIter iter = wt_file_list.begin();
while( iter != wt_file_list.end() )
{
String fullpath = mPath + *iter;
TimeSeries ts;
if( ReadTimeSeries( fullpath, ts ) )
{
if( ts.size() > mMaxWtTsLength ) mMaxWtTsLength = ts.size();
wt_series.push_back( ts );
}
++iter;
}
return true;
}
// Read in all of the knockout time series, one set for each variable
bool FileManager::ReadKnockOutTimeSeries( TimeSeriesVectorSet& ko_series_set )
{
mMaxKoTsLength = 0;
// ko_series_set is a multimap with key = knockout variable number
// and data = a time series for that variable
KOFileSpecListIter iter = ko_file_list.begin();
while( iter != ko_file_list.end() )
{
// Build the path from the filename
String fullpath = mPath + iter->mFilename;
// Read in the time series from that file
TimeSeries ts;
if( ReadTimeSeries( fullpath, ts, iter->mK ) )
{
if( ts.size() > mMaxKoTsLength ) mMaxKoTsLength = ts.size();
// Insert the time series into the map, keyed by knockout variable number
ko_series_set.insert(TimeSeriesVectorSet::value_type( iter->mK, ts ));
}
++iter;
}
return true;
}
// Process a single row of the Reverse Engineering Matrix
void FileManager::ProcessRow( WeightMatrixRow& row )
{
// Divide thru by the last entry and then drop it
int n = (int) floor(row.back());
WeightMatrixRowIter row_iter = row.begin();
while( row_iter != row.end() )
{
if( n > 0 && *row_iter <= n )
{
*row_iter /= n;
}
else
{
if( n == 0 && *row_iter != 0 )
{
throw( "FileManager::ReadReverseEngWeightMatrix - nonzero entry with total count = 0" );
}
else if ( n < *row_iter )
{
throw( "FileManager::ReadReverseEngWeightMatrix - entry greater than total count" );
}
}
++row_iter;
}
// remove the last entry and then save the row
row.pop_back( );
}
// Read in a file containing frequence of use counts for each variable in each function
bool FileManager::ReadReverseEngWeightMatrix( WeightMatrix& weight_matrix )
{
// If no file name do not complain
if( mRevFile.length() == 0 ) return true;
String file_path = mPath + mRevFile;
std::ifstream in_file;
in_file.open( file_path.c_str() );
if( !in_file.good() )
{
throw( String("FileManager::ReadReverseEngWeightMatrix - unable to open file") + file_path);
}
char ch;
int n;
double x;
int var_num;
String ns; // to collect a string of digits representing a number
// fill the weight matrix with mNumVar rows
WeightMatrixRow row;
for( n = (int) GAParams::NumVars(); n > 0; --n )
{
weight_matrix.push_back( row );
}
bool in_num = false;
bool in_row = false;
bool still_blank = true;
bool first_num = true;
while( !in_file.eof() )
{
in_file.get( ch );
if( in_row )
{
if( in_num )
{
if( isdigit( ch ) || ch == '.' )
{
// accumulate digits
ns.push_back( ch );
}
else
{
if( ns.length() > 0 )
{
if( first_num )
{
// The first number after 'F' identifies the variable
var_num = atoi( ns.c_str());
if( var_num > (int)weight_matrix.size() )
{
throw( "FileManager::ReadReverseEngWeightMatrix - invalid variable number after F" );
}
first_num = false;
}
else
{
x = atof( ns.c_str() );
// Append the number to the current row
row.push_back( x );
}
}
if( ch == '\n' )
{
if( row.size() != GAParams::NumVars() + 1 )
{
throw( "FileManager::ReadReverseEngWeightMatrix - row length does not match number of variables");
}
// Normalize the values using the last column
ProcessRow( row );
weight_matrix[ var_num - 1 ] = row;
row.clear();
still_blank = true; first_num = true;
}
// Leave the 'in number' state
in_num = false;
}
}
else // we are outside of a number
{
if( isdigit( ch ) || ch == '.' )
{
// accumulate the first digit
ns.clear();
if( ch == '.' ) ns.push_back( '0' );
ns.push_back( ch );
in_num = true;
}
else
{
if( ch == '\n' )
{
if( row.size() > 0 )
{
// Normalize the values using the last column
if( row.size() != GAParams::NumVars() + 1 )
{
throw( "FileManager::ReadReverseEngWeightMatrix - row length does not match number of variables");
}
ProcessRow( row );
weight_matrix[ var_num - 1 ] = row;
}
row.clear();
still_blank = true; first_num = true;
}
}
}
}
else
{
if( ch == 'F' && still_blank )
{
in_row = true;
}
if( isalnum( ch )) still_blank = false;
if( ch == '\n' )
{
still_blank = true; first_num = true;
}
}
}
in_file.close();
return true;
}
bool FileManager::ReadBioDependencyMatrix( WeightMatrix& prob_matrix )
{
// If no file name do not complain
if( mBioFile.length() == 0 ) return true;
String file_path = mPath + mBioFile;
std::ifstream in_file;
in_file.open( file_path.c_str() );
if( !in_file.good() )
{
throw( String("FileManager::ReadBioDependencyMatrix - unable to open file") + file_path);
}
char ch;
int n;
int var_num;
String ns; // to collect a string of digits representing a number
// fill the weight matrix with mNumVar rows
WeightMatrixRow row;
for( n = (int) GAParams::NumVars(); n > 0; --n )
{
prob_matrix.push_back( row );
}
bool in_num = false;
bool in_row = false;
bool still_blank = true;
bool first_num = true;
double x;
while( !in_file.eof() )
{
in_file.get( ch );
if( in_row )
{
if( in_num )
{
if( isdigit( ch ) || ch == '.' )
{
// accumulate digits
ns.push_back( ch );
}
else
{
if( ns.length() > 0 )
{
if( first_num )
{
// The first number after 'F' identifies the variable
var_num = atoi( ns.c_str() );
if( var_num > (int)prob_matrix.size() )
{
throw( "FileManager::ReadBioDependencyMatrix - invalid variable number after F" );
}
first_num = false;
}
else
{
x = atof( ns.c_str() );
if( 0.0 <= x && x <= 1.0 )
{
// Append the number to the current row
row.push_back( x );
}
else
{
throw( "FileManager::ReadBioDependencyMatrix - probability value not in [0.0, 1.0]" );
}
}
}
if( ch == '\n' )
{
if( row.size() != GAParams::NumVars() )
{
throw( "FileManager::ReadBioDependencyMatrix - row length does not match number of variables");
}
prob_matrix[ var_num - 1 ] = row;
row.clear();
still_blank = true; first_num = true;
}
// Leave the 'in number' state
in_num = false;
}
}
else // we are outside of a number
{
if( isdigit( ch ) || ch == '.' )
{
// accumulate the first digit
ns.clear();
if( ch == '.' ) ns.push_back( '0' );
ns.push_back( ch );
in_num = true;
}
else
{
if( ch == '\n' )
{
if( row.size() > 0 )
{
// Normalize the values using the last column
if( row.size() != GAParams::NumVars() )
{
throw( "FileManager::ReadBioDependencyMatrix - row length does not match number of variables");
}
prob_matrix[ var_num - 1 ] = row;
}
row.clear();
still_blank = true; first_num = true;
}
}
}
}
else
{
if( ch == 'F' && still_blank )
{
in_row = true;
}
if( isalnum( ch )) still_blank = false;
if( ch == '\n' )
{
still_blank = true; first_num = true;
}
}
}
in_file.close();
return true;
}
// Enter this function after a '(' encountered
// it will return a ComplexityMatrixEntry when the ')' is encountered
ComplexityMatrixEntry FileManager::ReadCmplxEntry( std::ifstream& in_file )
{
int p,q;
char ch;
String num_str;
ComplexityMatrixEntry c;
ComplexityQuotient x;
bool in_numerator = true;
while( !in_file.eof() )
{
in_file.get( ch );
if( in_numerator )
{
if( isdigit( ch ) )
{
num_str.push_back( ch );
}
else if( ch == '/' )
{
in_numerator = false;
p = atoi( num_str.c_str() );
num_str.clear();
}
}
else // in denominator
{
if( isdigit( ch ) )
{
num_str.push_back( ch );
}
else if( ch == '+' || ch == ')' )
{
in_numerator = true;
q = atoi( num_str.c_str() );
num_str.clear();
if( q == 0 && p != 0 )
{
throw( "illegal zero denominator" );
}
// Form the complexity quotient and append it to the entry
x.p = p; x.q = q;
c.push_back( x );
if( ch == ')' ) return c;
}
}
}
throw( "closing ) not found" );
return c;
}
bool FileManager::ReadComplexityMatrix( ComplexityMatrix& cmplx_mat )
{
// Open the file
// For each non-blank line
// extract Fnn
// for mNumVars entries
// read each entry as ( p1/q1 + p2/q2 + ... )
//
// If no file name do not complain
if( mDepGraphFile.length() == 0 ) return true;
String file_path = mPath + mDepGraphFile;
std::ifstream in_file;
in_file.open( file_path.c_str() );
if( !in_file.good() )
{
throw( String("FileManager::ReadComplexityMatrix - unable to open file") + file_path);
}
char ch;
int n;
int var_num;
String ns; // to collect a string of digits representing a number
// fill the complexity matrix with mNumVar rows
ComplexityMatrixRow row;
for( n = (int) GAParams::NumVars(); n > 0; --n )
{
cmplx_mat.push_back( row );
}
bool in_num = false;
bool in_row = false;
bool still_blank = true;
bool first_num = true;
while( !in_file.eof() )
{
in_file.get( ch );
if( in_row )
{
if( in_num )
{
if( isdigit( ch ) ) // accumulate the digit as a number
{
ns.push_back( ch );
}
else
{ // non-digit found to end the number
if( ns.length() > 0 )
{
if( first_num )
{
// The first number after 'F' identifies the variable
var_num = atoi( ns.c_str() );
if( var_num > (int)GAParams::NumVars() )
{
throw( "FileManager::ReadComplexityMatrix - invalid variable number after F" );
}
first_num = false;
}
else
{
n = atoi( ns.c_str() );
if( n == 0 || row.size() == GAParams::NumVars() )
{
// Dummy up ( (0,0 ) ) for a single 0 entry
// Or ( (n,n) ) for the last entry which is total appearances
ComplexityQuotient x;
x.p = n; x.q = n;
ComplexityMatrixEntry cp;
cp.push_back( x );
row.push_back( cp );
}
else
{
throw( "FileManager::ReadComplexityMatrix - invalid complexity entry" );
}
}
}
if( ch == '\n' )
{
if( row.size() != GAParams::NumVars() + 1 )
{
throw( "FileManager::ReadBioDependencyMatrix - row length does not match number of variables");
}
cmplx_mat[ var_num - 1 ] = row;
row.clear();
still_blank = true; first_num = true;
}
// Leave the 'in number' state
in_num = false;
}
}
else // we are outside of a number
{
if( isdigit( ch ) )
{
// accumulate the first digit
ns.clear();
ns.push_back( ch );
in_num = true;
}
else if( ch == '(' ) // Start of a Complexity entry ( p1/q1 + p2/q2 + ... )
{
ComplexityMatrixEntry cp = ReadCmplxEntry( in_file );
row.push_back( cp );
}
else if( ch == '\n' )
{
if( row.size() > 0 )
{
if( row.size() != GAParams::NumVars() + 1 )
{
throw( "FileManager::ReadBioDependencyMatrix - row length does not match number of variables");
}
cmplx_mat[ var_num - 1 ] = row;
}
row.clear();
still_blank = true; first_num = true;
}
}
}
else
{
if( ch == 'F' && still_blank )
{
in_row = true;
}
if( isalnum( ch )) still_blank = false;
if( ch == '\n' )
{
still_blank = true; first_num = true;
}
}
}
in_file.close();
return true;
}
bool FileManager::ReadDiscreteModels( DynSysModelVector& models )
{
StringVectorIter mf_iter = model_file_list.begin();
while( mf_iter != model_file_list.end() )
{
String file_path = mPath + *mf_iter;
std::ifstream in_file;
in_file.open( file_path.c_str() );
if( !in_file.good() )
{
throw( String("FileManager::ReadDiscreteModels - unable to open file") + file_path );
}
DynSysModel m;
while( !in_file.eof() )
{
String in_line = GetLine( in_file );
// Ignore lines that are too short to hold a function definition
if( in_line.length() < 4 ) continue;
Polynomial p;
if( ParseFunction( in_line, p ) )
{
// Set function number k
if( m.IsDefaultPolyQ( p.mK ) )
{
m.SetFunction( p.mK, p );
}
else
{
throw( "FileManager::ReadDiscreteModels - duplicate function number");
}
}
}
// Check that we have one polynomial for each variable in the model
if( !m.IsCompleteModelQ() )
{
throw( "FileManager::ReadDiscreteModels - insufficient number of functions to construct a complete model" );
}
models.push_back( m );
in_file.close();
++mf_iter;
}
return true;
}
// Parse a string of the form: f1 = x1*x2 + x3 + 1
// to fill in the Polynomial object poly
// return true if sucessful, false if the string is invalid
bool FileManager::ParseFunction( String& s, Polynomial& poly )
{
String digits( "0123456789" );
Monomial m;
bool got_something = false;
// function starts with Fnn
size_t i = s.find_first_of( String("fF") );
if( i == s.npos ) return false; // throw( "ParseFunction - missing 'F' for function" );
// get the function number
size_t j = s.find_first_not_of( digits, i+1 );
if( j == s.npos ) return false; // throw( "ParseFunction - missing function number" );
if( j <= i+1 ) return false; // throw( "ParseFunction - missing function number" );
// the function number is found in s[i+1] to s[j-1]
String fn = s.substr( i+1, j-i-1 );
// Save the function number in the polynomial
poly.mK = atoi( fn.c_str() );
// Make sure the equal sign is there
i = s.find( '=', j);
if( i == s.npos ) throw( "ParseFunction - missing equal sign" );
// TBD - for general case must read optional coefficient first
// Try to find the first element Xnn
j = s.find_first_of( String("-+01xX"), i+1 );
if( j == s.npos )
{
throw( "ParseFunction - no terms after equal sign" );
}
else
{
if( s[j] == '+' || s[j] == '-' )
{
j = s.find_first_of( String("01xX"), j+1 );
if( j == s.npos ) throw( "ParseFunction - missing term after +/-" );
}
}
size_t k;
// Now capture each monomial in turn
while( j != s.npos )
{
i = j;
// Handle the constant term
if( s[j] == '0' || s[j] == '1' )
{
m.Reset();
if( s[j] == '0' ) m.mCoeff = false;
// poly.AddTerm( m ) will be done after +/- or end
got_something = true;
k = s.find_first_not_of( digits, j+1 );
}
else
{
String xn;
k = s.find_first_not_of( digits, j+1 );
if( k == j+1 ) throw( "ParseFunction - missing variable number" );
// If there were no non-digits after the variable number
if( k == s.npos )
{
// the digits are the remaining elements of the string
xn = s.substr( j+1 );
}
else
{
// the variable number runs from s[j+1] to s[k-1]
xn = s.substr( j+1, k-j-1 );
}