-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFuzzyVariableBase.cpp
1482 lines (1206 loc) · 32.9 KB
/
FuzzyVariableBase.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
//
// File: FuzzyVariableBase.cpp
//
// Purpose: This file contains ancestor code for the FuzzyVariable classes
//
// Copyright � 1999-2001 Louder Than A Bomb! Software
//
// This file is part of the FFLL (Free Fuzzy Logic Library) project (http://ffll.sourceforge.net)
// It is released under the BSD license, see http://ffll.sourceforge.net/license.txt for the full text.
//
#include "FuzzyVariableBase.h"
#include "FuzzySetBase.h"
#include "FuzzyModelBase.h"
#include "MemberFuncBase.h"
#include <fstream>
#include <limits.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#include "debug.h"
#endif
// init static variables to a reasonable value. We add one to each array count to
// account for the fact that we start counting at zero.
// For example if we only had 5 elements in the array and the variable range was 0 to 5,
// we'd expect each element to be 1 'x' value, but then the 'x' value at values[4] (the
// fifth element) would be 4 and we EXPECT 5.The values array would look like:
//
// values[0] = 0
// values[1] = 1
// values[2] = 2
// values[3] = 3
// values[4] = 4
//
// if each "step" was 1. So we need to add 1 element to the array to get the results we want.
ValuesArrCountType FuzzyVariableBase::x_array_count = 201;
ValuesArrCountType FuzzyVariableBase::x_array_max_idx = FuzzyVariableBase::x_array_count - 1;
DOMType FuzzyVariableBase::dom_array_count = 101;
DOMType FuzzyVariableBase::dom_array_max_idx = FuzzyVariableBase::dom_array_count - 1;
//
// Function: FuzzyVariableBase()
//
// Purpose: default constructor
//
// Arguments:
//
// FuzzyModelBase* _parent - model that this variable is part of
//
// Returns:
//
// nothing
//
// Author: Michael Zarozinski
// Date: 6/10/99
//
// Modification History
// Author Date Modification
// ------ ---- ------------
//
//
FuzzyVariableBase::FuzzyVariableBase(FuzzyModelBase* _parent) : FFLLBase(_parent)
{
num_of_sets = 0;
rule_index = 0;
index = -1;
sets = NULL;
// set left/right shoulder
left_x = 0;
right_x = FuzzyVariableBase::x_array_max_idx;
set_id(L""); // init identifier
idx_multiplier = 0; // it should NEVER be 0 so by init'ing to 0 we'll know if there's a problem sooner
} // end FuzzyVariableBase::FuzzyVariableBase()
//
// Function: ~FuzzyVariableBase()
//
// Purpose: default destructor
//
// Arguments:
//
// none
//
// Returns:
//
// nothing
//
// Author: Michael Zarozinski
// Date: 6/10/99
//
// Modification History
// Author Date Modification
// ------ ---- ------------
//
//
FuzzyVariableBase::~FuzzyVariableBase()
{
delete_all_sets();
} // end FuzzyVariableBase::~FuzzyVariableBase()
//
// Function: set_ramp()
//
// Purpose: Change the ramp status of one of the variable's sets.
// A ramp is a membership function that has the 'y' axis
// as one of it's sides. For example a normal trapezoid memberhip
// function would look like this:
// ___
// Normal Trapezoid: / \
// ___
// Left Ramp Trapezoid: | \
// ___
// Right Ramp Trapezoid: / |
//
// Arguments:
//
// int left_right_ind - indicates if we're making a left (1) or right (0) ramp
// int hi_lo_ind - indicates if we're creating (1) or removing (0) ramp
// int set_idx - index of the set to change the ramp for
//
// Returns:
//
//
//
// Author: Michael Zarozinski
// Date: 7/99
//
// Modification History
// Author Date Modification
// ------ ---- ------------
//
//
void FuzzyVariableBase::set_ramp(int left_right_ind, int hi_lo_ind, int set_idx )
{
sets[set_idx]->set_ramp(hi_lo_ind, left_right_ind);
} // end FuzzyVariableBase::set_ramp()
//
// Function: is_set_id_unique
//
// Purpose: Checks if the set name passed in is unique for this variable
//
// Arguments:
//
// const wchar_t* set_id - set name to check
// int set_idx - index for the set the id belongs to
//
// Returns:
//
// true - id is unique
// false - id is NOT unique
//
// Author: Michael Zarozinski
// Date: 6/00
//
// Modification History
// Author Date Modification
// ------ ---- ------------
//
//
bool FuzzyVariableBase::is_set_id_unique(const wchar_t* set_id, int set_idx) const
{
FuzzySetBase* set; // set we're checking
// go through all the OTHER sets and make sure there is no match
for (int i = 0; i < get_num_of_sets(); i++)
{
if (i == set_idx)
continue; // skip this set
set = get_set(i);
assert(set != NULL);
// case insensitive comparison
if (wcsncmp(set->get_id(), set_id, wcslen(set_id)) == 0)
{
set_msg_text_int(ERR_SET_NON_UNIQUE_ID);
return false;
}
} // end loop through sets
// if we got here the id IS unique...
return true;
} // end is_set_id_unique()
//
// Function: set_left_x()
//
// Purpose: Set the left value for this variable. Note that the left value IS
// allowed to be greater than the right value. The only restriction is
// that they can NOT be the same.
//
// Arguments:
//
// RealType value - value to set left_x to
//
// Returns:
//
// 0 - set value
// -1 - problem setting value
//
// Author: Michael Zarozinski
// Date: 7/99
//
// Modification History
// Author Date Modification
// ------ ---- ------------
//
//
int FuzzyVariableBase::set_left_x(RealType value)
{
// make sure value != right_x
if (value == right_x)
{
set_msg_text_int(ERR_SAME_LEFT_RIGHT_VALS);
return -1;
}
left_x = value;
// re-calc multiplier cuz it's affected by this
calc_idx_multiplier();
return 0;
} // end FuzzyVariableBase::set_left_x()
//
// Function: set_right_x()
//
// Purpose: Set the right value for this variable. Note that the left value IS
// allowed to be greater than the right value. The only restriction is
// that they can NOT be the same.
//
// Arguments:
//
// RealType value - value to set right_x to
//
// Returns:
//
// 0 - set value
// -1 - problem setting value
//
// Author: Michael Zarozinski
// Date: 7/99
//
// Modification History
// Author Date Modification
// ------ ---- ------------
//
//
int FuzzyVariableBase::set_right_x(RealType value)
{
// make sure avlue != left_x
if (value == left_x)
{
set_msg_text_int(ERR_SAME_LEFT_RIGHT_VALS);
return -1;
}
right_x = value;
// re-calc multiplier cuz it's affected by this
calc_idx_multiplier();
return 0;
} // end FuzzyVariableBase::set_right_x()
//
// Function: calc_idx_multiplier()
//
// Purpose: Calculate the index multiplier value for this variable.
// NOTE: there is not 'set' for the multiplier - it's always calculated
//
// Arguments:
//
// none
//
// Returns:
//
// void
//
// Author: Michael Zarozinski
// Date: 8/99
//
// Modification History
// Author Date Modification
// ------ ---- ------------
//
//
//
void FuzzyVariableBase::calc_idx_multiplier()
{
idx_multiplier =(right_x - left_x) /((RealType)( FuzzyVariableBase::x_array_max_idx));
}; // end FuzzyVariableBase::calc_idx_multiplier()
//
// Function: delete_set()
//
// Purpose: Deletes a set from the variable and adjusts the sets[]
// array appropriately.
//
// Arguments:
//
// int _set_idx - index of the set to delete
//
// Returns:
//
// 0 - success
// non-zero - failure
//
// Author: Michael Zarozinski
// Date: 8/99
//
// Modification History
// Author Date Modification
// ------ ---- ------------
//
//
int FuzzyVariableBase::delete_set(int _set_idx)
{
// if we don't have any sets, just return
if (sets == NULL)
return 0; // nothing to do.
FuzzySetBase** tmp_sets; // temp array to hold sets
// if we're deleting the last set, don't alloc any memory
if (num_of_sets - 1 == 0)
tmp_sets = NULL;
else
tmp_sets = new FuzzySetBase*[num_of_sets - 1]; // create temp array
// copy the old memory to the temp mem...
int j = 0;
for (int i = 0; i < num_of_sets; i++)
{
// don't copy the set we're deleting
if (i == _set_idx)
{
// free the memory for the set we're deleting
delete sets[i] ;
sets[i] = NULL;
}
else
{
tmp_sets[j] = (FuzzySetBase*)sets[i];
tmp_sets[j]->set_index(j);
// set the index
j++;
}
} // end loop through sets
// decrement the set count
num_of_sets--;
// free old memory...
delete[] sets;
// assign the new array of sets to the sets[] member variable
sets = tmp_sets;
return 0;
} // end FuzzyVariableBase::delete_set()
//
// Function: delete_all_sets()
//
// Purpose: clean up the memory allocated for sets
//
// Arguments:
//
// none
//
// Returns:
//
// void
//
// Author: Michael Zarozinski
// Date: 6/10/99
//
// Modification History
// Author Date Modification
// ------ ---- ------------
//
//
void FuzzyVariableBase::delete_all_sets()
{
// if we don't have any sets, just return
if (sets == NULL)
return; // nothing to do.
// loop through all the sets and free memory
for (int i = 0; i < num_of_sets; i++)
{
delete sets[i] ;
sets[i] = NULL;
} // end loop through sets
num_of_sets = 0;
// free old memory...
delete[] sets;
sets = NULL;
} // end FuzzyVariableBase::delete_all_sets()
//
// Function: set_id()
//
// Purpose: Set the identifier for a variable or set. This version
// takes an ASCII string, converts it to wide characters and
// calls the wide character version of this function
//
// Arguments:
//
// const char* _id - identifier to set
// int set_idx - index of the set to change the id for, if -1 (default), change
// the id for the variable
//
// Returns:
//
// 0 - success
// non-zero - non-unique id
//
// Author: Michael Zarozinski
// Date: 8/99
//
// Modification History
// Author Date Modification
// ------ ---- ------------
// Michael Z 4/03 changed to use convert_to_wide_char() for compatiblity
//
int FuzzyVariableBase::set_id(const char* _id, int set_idx /* = -1 */)
{
// convert from char to wchar_t
wchar_t* wc = convert_to_wide_char(_id);
int ret_val = set_id(wc, set_idx);
// we MUST free mem convert_to_wide_char() created
delete[] wc;
return ret_val;
} // end FuzzyVariableBase::set_id()
//
// Function: set_id()
//
// Purpose: Set the identifier for a variable or set. If no set index
// is passed in set the variable's id.
// This function will only set the identifier if it's unique
//
// Arguments:
//
// const wchar_t* _id - identifier to set
// int set_idx - index of the set to change the id for, if -1 (default), change
// the id for the variable
//
// Returns:
//
// 0 - success
// non-zero - non-unique id
//
// Author: Michael Zarozinski
// Date: 8/99
//
// Modification History
// Author Date Modification
// ------ ---- ------------
//
//
int FuzzyVariableBase::set_id(const wchar_t* _id, int set_idx /* = -1 */)
{
if (_id == NULL || wcslen(_id) == 0)
{
id = L"";
return 0;
}
// if a set_idx is passed in, set the name for that set
if (set_idx >= 0)
{
int ret_val = sets[set_idx]->set_id(_id);
if (ret_val)
{
// read the error from the set and set it for 'this'
set_msg_text_wchar(sets[set_idx]->get_msg_text());
return ret_val;
}
return 0;
}
FuzzyModelBase* par = get_parent();
// make sure our id is UNIQUE within the rule block
if (par->is_var_id_unique(_id, get_index()) == false)
{
// read the error from the parent and set it for 'this'
set_msg_text_wchar(par->get_msg_text());
return -1;
}
// set the identifier
id = _id;
return 0;
}; // end FuzzyVariableBase::set_id()
//
// Function: add_set()
//
// Purpose: Insert a set into this variable. NOTE: that a COPY of the
// set passed in is inserted into the var so the caller must
// free the object if it dynamically allocated it.
//
// Arguments:
//
// const FuzzySetBase* _new_set - set to insert a copy of
//
// Returns:
//
// 0 - success
// non-zero - failure
//
// Author: Michael Zarozinski
// Date: 5/00
//
// Modification History
// Author Date Modification
// ------ ---- ------------
//
//
int FuzzyVariableBase::add_set(const FuzzySetBase* _new_set)
{
// allocate temp memory which is the current number of sets PLUS 1
FuzzySetBase** tmp_sets = new FuzzySetBase*[num_of_sets + 1];
// copy the old memory to the temp mem...
for (int i = 0; i < num_of_sets; i++)
{
tmp_sets[i] = (FuzzySetBase*)sets[i];
}
tmp_sets[num_of_sets] = new_set(); // insert empty set
tmp_sets[num_of_sets]->copy(*_new_set); // copy the set we passed in
// ensure that the set id is unique... if the user changed the id we
// performed a check PRIOR to calling this, so it'll be unique, if
// we get there through cut/copy/paste or something else we
// may need to change the name...
std::wstring tmp_name; // temp name
std::wstring set_name = tmp_sets[num_of_sets]->get_id();
while (!(is_set_id_unique(set_name.c_str(), get_num_of_sets())))
{
// save the name
tmp_name = set_name;
// just keep adding "Copy of" until we get a unique id
set_name = load_string(STR_COPY_OF);
set_name += L" " + tmp_name;
// clear the message text...
set_msg_text_wchar();
} // end wile set is NOT unique
tmp_sets[num_of_sets]->set_id(set_name.c_str());
// set the set index...
tmp_sets[num_of_sets]->set_index(num_of_sets);
tmp_sets[num_of_sets]->calc();
num_of_sets++; // increment the number of sets
// free old memory...
delete[] sets;
// copy new mem to old
sets = tmp_sets;
return 0;
} // end FuzzyVariableBase::add_set()
//
// Function: set_x_array_count()
//
// Purpose: Set the value for x_array_count. This also sets the
// x_array_max_idx value appropriately. Note: this is a static
// function so it can't call non-static member funcs which
// means the caller needs to make sure the idx_multiplier
// variables are re-calculated.
//
// Arguments:
//
// int _count - value to set x_array_count to.
//
// Returns:
//
// 0 - success
// -1 - invalid value passed in, set x_array_count to max allowed by datatype
//
// Author: Michael Zarozinski
// Date: 5/01
//
// Modification History
// Author Date Modification
// ------ ---- ------------
//
//
int FuzzyVariableBase::set_x_array_count(int _count)
{
// if they're trying to set over max, set to max and return -1
if (_count > USHRT_MAX)
{
x_array_count = USHRT_MAX;
x_array_max_idx = x_array_count - 1;
return -1;
}
x_array_count = _count;
x_array_max_idx = x_array_count - 1;
return 0;
}; // end FuzzyVariableBase::set_x_array_count()
//
// Function: set_dom_array_count()
//
// Purpose: Set the value for dom_array_count. This also sets the
// dom_array_max_idx value appropriately.
//
// Arguments:
//
// int _count - value to set dom_array_count to.
//
// Returns:
//
// 0 - success
// -1 - invalid value passed in, set dom_array_count to max allowed by datatype
//
// Author: Michael Zarozinski
// Date: 5/01
//
// Modification History
// Author Date Modification
// ------ ---- ------------
//
//
int FuzzyVariableBase::set_dom_array_count(int _count)
{
// if they're trying to set over max, set to max and return -1
if (_count > UCHAR_MAX)
{
dom_array_count = UCHAR_MAX;
dom_array_max_idx = x_array_count - 1;
return -1;
}
dom_array_count = _count;
dom_array_max_idx = dom_array_count - 1;
return 0;
}; // end FuzzyVariableBase::set_dom_array_count()
//
// Function: save_var_to_fcl_file()
//
// Purpose: Write out the FCL (Fuzzy Control Language) for this variable. This is written
// inside a FUNCTION_BLOCK section and has the format (as defined by the
// IEC 61131-7 International Standard):
//
// VAR_INPUT Input parameter declaration
// variable_name: data_type; (* RANGE *)
// ....
// END_VAR
//
// NOTE: we augment the standard by writing out the variable's range in a comment
// after the semi-colon. Without this we won't know the left/right values for the
// varaible when a FCL file is read in.
//
// Arguments:
//
// std::ofstream& file_contents - STL stream to write out to a file
//
/// Returns:
//
// void
//
// Author: Michael Zarozinski
// Date: 9/01
//
// Modification History
// Author Date Modification
// ------ ---- ------------
//
//
void FuzzyVariableBase::save_var_to_fcl_file(std::ofstream& file_contents)
{
// get the ascii version of the id...
char* aid = convert_to_ascii(get_id());
// store the ORIGINAL version BEFORE we replace any spaces so we can report on it
std::string tmp = aid;
delete[] aid;
// now get the version with spaces replaced by underscores
aid = convert_to_ascii(get_id(), '_');
// NOTE: the IEC 61131-7 does not specify a way to set the range of a varaible, so
// we write them out in a range comment
file_contents << "\t" << aid << "\tREAL; (* RANGE(" << get_left_x() << " .. " << get_right_x() << ") *) ";
delete[] aid;
FuzzyModelBase::validate_fcl_identifier(file_contents, tmp);
file_contents << "\n";
} // end FuzzyVariableBase::save_var_to_fcl_file()
//
// Function: save_sets_to_fcl_file()
//
// Purpose: Write out the FCL (Fuzzy Control Language) for the sets in this variable
// as defined by the IEC 61131-7 International Standard:
//
// FUZZIFY variable_name
// TERM term_name:= membership_function;
// ....
// END_FUZZIFY
//
// Arguments:
//
// std::ofstream& file_contents - STL stream to write out to a file
//
/// Returns:
//
// void
//
// Author: Michael Zarozinski
// Date: 9/01
//
// Modification History
// Author Date Modification
// ------ ---- ------------
//
//
void FuzzyVariableBase::save_sets_to_fcl_file(std::ofstream& file_contents)
{
FuzzySetBase* set; // set we're dealing with
// get the ascii version of the id and replace any spaces
// with an underscore...
char* aid = convert_to_ascii(get_id(), '_');
file_contents << "FUZZIFY " << aid << "\n";
delete[] aid;
// loop through the sets
for (int i = 0; i < num_of_sets; i++)
{
set = get_set(i);
set->save_to_fcl_file(file_contents);
char* aid_set = convert_to_ascii(set->get_id());
FuzzyModelBase::validate_fcl_identifier(file_contents, aid_set);
delete[] aid_set;
file_contents << "\n";
} // end loop through sets
file_contents << "END_FUZZIFY\n\n";
} // end FuzzyVariableBase::save_sets_to_file()
//
// Function: load_sets_from_fcl_file()
//
// Purpose: Read in an FCL file and create the sets that are associated
// with this variable.
// NOTE: This is a brain-dead parser, it can be made much more robust.
//
// Arguments:
//
// std::ifstream& file_contents - FCL file in stream form
//
// Returns:
//
// 0 - success
// non-zero - failure
//
// Author: Michael Zarozinski
// Date: 9/01
//
// Modification History
// Author Date Modification
// ------ ---- ------------
//
//
int FuzzyVariableBase::load_sets_from_fcl_file(std::ifstream& file_contents)
{
file_contents.seekg(0); // go to start of file
// get the ascii version of this variable's id...
char* aid = convert_to_ascii(get_id());
// search for keyword FUZZIFY followed by id of this var...
std::string stoken, var_name;
bool found = false;
while (!found)
{
// read until we get to the start of the variable declaration
do
{
file_contents >> stoken;
// check for EOF
if (file_contents.eof())
{
set_msg_text_int(ERR_EOF_READING_SETS);
delete[] aid;
return -1;
}
} while (strcmp(stoken.c_str(), "FUZZIFY") != 0 );
// if we got here we have a FUZZIFY block... check if it's the right var...
file_contents >> var_name;
if (strcmp(var_name.c_str(), aid) == 0)
found = true;
}; // end while !found
delete[] aid;
// we found "FUZZIFY <var name>", now loop until we find "END_FUZZIFY" reading
// the sets which are in the format:
//
// TERM term_name:= membership_function;
//
// Where membership_function ::= ( point i), ( point j), ...
// and point is either a single value (if singleton) or a list of x/y values (x, y)
// and the values MAY have a decimal point.
// read line-by-line to get the sets...
char line[300];
RealType x_point[7], y_point[7]; // x/y points for the set - NOTE: we assume a max of 7 points
char* set_name;
char* token;
// inlude ":=" in tokens so we skip the assignment operator
char seps[] = " ,()\t\n:=";
while(1)
{
// read in the next line
file_contents.getline(line, 300);
// check for EOF
if (file_contents.eof())
{
set_msg_text_int(ERR_EOF_READING_SETS);
return -1;
}
token = strtok(line, seps);
// if token is null... blank line
if (token == NULL)
continue;
if (strcmp(token, "END_FUZZIFY") == 0)
break; // we're done
// validate this is the "term" keyword
if (strncmp(token, "term", 4) != 0)
{
set_msg_text_int(ERR_INVALID_FILE_FORMAT); // could be a bit more "robuts" in our error reporting!
return -1;
}
// get the set name
set_name = strtok(NULL, seps);
token = strtok(NULL, seps);
if (token == NULL)
{
// ERROR - invalid format
set_msg_text_int(ERR_INVALID_FILE_FORMAT); // could be a bit more "robuts" in our error reporting!
return -1;
}
int num_of_points = 0;
// handle special case of singletons. They'll be of the form "<value>;"
// read points until we reach the semicolon
if (token[strlen(token) - 1] == ';')
{
// replace semi with null so we can convert
token[strlen(token) - 1] = '\0';
x_point[num_of_points] = atof(token);
// set 'y' value to max
y_point[num_of_points] = FuzzyVariableBase::get_dom_array_max_idx();
num_of_points = 1;
}
else
{
// not a singleton, points are of the form "(<value> , <value>)"
while (token[0] != ';')
{
x_point[num_of_points] = atof(token);
token = strtok(NULL, seps);
if (token[0] == ';')
{
if (num_of_points != 0)
{
// ERROR - invalid format
set_msg_text_int(ERR_INVALID_FILE_FORMAT); // could be a bit more "robuts" in our error reporting!
return -1;
}