File: SaveLoad.cpp

package info (click to toggle)
fact%2B%2B 1.6.5~dfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 4,496 kB
  • sloc: cpp: 28,000; java: 22,674; xml: 3,268; makefile: 102; ansic: 61; sh: 3
file content (1032 lines) | stat: -rw-r--r-- 24,735 bytes parent folder | download | duplicates (3)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
/* This file is part of the FaCT++ DL reasoner
Copyright (C) 2008-2015 Dmitry Tsarkov and The University of Manchester
Copyright (C) 2015-2016 Dmitry Tsarkov

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

//-------------------------------------------------------
//-- Saving/restoring internal state of the FaCT++
//-------------------------------------------------------

#include "Kernel.h"
#include "ReasonerNom.h"	// for initReasoner()
#include "SaveLoadManager.h"

const char* ReasoningKernel :: InternalStateFileHeader = "FaCT++InternalStateDump1.0";

const int bytesInInt = sizeof(int);

#if 0
// FIXME!! try to avoid recursion later on
static inline
void saveUIntAux ( ostream& o, unsigned int n, const int rest )
{
	if ( rest > 1 )
		saveUIntAux ( o, n/256, rest-1 );
	m.o() << (unsigned char)n%256;
}

static inline
void saveUInt ( ostream& o, unsigned int n )
{
	saveUIntAux ( o, n, bytesInInt );
}

static inline
void saveSInt ( ostream& o, int n ) { saveUInt(o,n); }

static inline
unsigned int loadUInt ( istream& i )
{
	static unsigned int ret = 0;
	unsigned char byte;
	for ( int j = bytesInInt-1; j >= 0; --j )
	{
		i >> byte;
		ret *= 256;
		ret += byte;
	}
	return ret;
}

static inline
int loadSInt ( istream& i ) { return (int)loadUInt(i); }
#endif	// 0

//----------------------------------------------------------
//-- Implementation of the Kernel methods (Kernel.h)
//----------------------------------------------------------

void
ReasoningKernel :: Save ( SaveLoadManager& m )
{
	TsProcTimer t;
	t.Start();
	m.checkStream();
	SaveHeader(m);
	m.checkStream();
	SaveOptions(m);
	m.checkStream();
	SaveKB(m);
	m.checkStream();
	SaveIncremental(m);
	m.checkStream();
	t.Stop();
	std::cout << "Reasoner internal state saved in " << t << " sec" << std::endl;
}

void
ReasoningKernel :: Save ( void )
{
	fpp_assert ( pSLManager != NULL );
	pSLManager->prepare(/*input=*/false);
	Save(*pSLManager);
}

void
ReasoningKernel :: Load ( SaveLoadManager& m )
{
	TsProcTimer t;
	t.Start();
	m.checkStream();
//	releaseKB();	// we'll start a new one if necessary
	LoadHeader(m);
	m.checkStream();
	LoadOptions(m);
	m.checkStream();
	LoadKB(m);
	m.checkStream();
	LoadIncremental(m);
	m.checkStream();
	t.Stop();
	std::cout << "Reasoner internal state loaded in " << t << " sec" << std::endl;
}

void
ReasoningKernel :: Load ( void )
{
	fpp_assert ( pSLManager != NULL );
	pSLManager->prepare(/*input=*/true);
	Load(*pSLManager);
}

//-- save/load header (Kernel.h)

void
ReasoningKernel :: SaveHeader ( SaveLoadManager& m ) const
{
	m.o() << InternalStateFileHeader << "\n" << Version << "\n" << bytesInInt << "\n";
}

void
ReasoningKernel :: LoadHeader ( SaveLoadManager& m )
{
	std::string str;
	m.i() >> str;
	if ( str != InternalStateFileHeader )
		throw EFPPSaveLoad("Incompatible save/load header");
	m.i() >> str;
	// FIXME!! we don't check version equivalence for now
//	if ( str != Version )
//		return true;
	int n;
	m.i() >> n;
	if ( n != bytesInInt )
		throw EFPPSaveLoad("Saved file differ in word size");
}

//-- save/load options (Kernel.h)

void
ReasoningKernel :: SaveOptions ( SaveLoadManager& m ) const
{
	m.o() << "Options\n";
}

void
ReasoningKernel :: LoadOptions ( SaveLoadManager& m )
{
	std::string options;
	m.i() >> options;
}

//-- save/load KB (Kernel.h)

void
ReasoningKernel :: SaveKB ( SaveLoadManager& m )
{
	m.saveUInt((unsigned int)getStatus());
	switch ( getStatus() )
	{
	case kbEmpty:	// nothing to do
		return;
	case kbLoading:
		throw EFPPSaveLoad("Can't save internal state of the unclassified reasoner");
	default:
		getTBox()->Save(m);
		break;
	}
}

void
ReasoningKernel :: LoadKB ( SaveLoadManager& m )
{
	KBStatus status = (KBStatus)m.loadUInt();
//	initCacheAndFlags();	// will be done
	// no classification => no need to monitor
	pMonitor = NULL;
	if ( status == kbEmpty )
		return;
//	newKB();
	getTBox()->Load(m,status);
}

//----------------------------------------------------------
//-- Helpers: Save/Load of class TNECollection
//----------------------------------------------------------

/// Save all the objects in the collection
template<class T>
static void
SaveTNECollection ( const TNECollection<T>& collection, SaveLoadManager& m, const std::set<const TNamedEntry*>& excluded )
{
	typename TNECollection<T>::const_iterator p, p_beg = collection.begin(), p_end = collection.end();
	// get the max length of the identifier in the collection
	size_t maxLength = 0, curLength, size = 0;

	for ( p = p_beg; p < p_end; ++p )
	{
		if ( excluded.count(*p) == 0 )
		{
			++size;
			if ( maxLength < (curLength = strlen((*p)->getName())) )
				maxLength = curLength;
		}
	}

	// save number of entries and max length of the entry
	m.saveUInt(size);
	m.saveUInt(maxLength);

	// save names of all entries
	for ( p = p_beg; p < p_end; ++p )
	{
		// register all entries in the global map
		m.registerE(*p);
		if ( excluded.count(*p) == 0 )
			m.o() << (*p)->getName() << "\n";
	}

	// save the entries itself
//	for ( p = p_beg; p < p_end; ++p )
//		(*p)->Save(o);
}
/// Load all the objects into the collection
template<class T>
static void
LoadTNECollection ( TNECollection<T>& collection, SaveLoadManager& m )
{
	// sanity check: Load shall be done for the empty collection and only once
//	fpp_assert ( size() == 0 );

	unsigned int collSize, maxLength;
	collSize = m.loadUInt();
	maxLength = m.loadUInt();
	++maxLength;
	char* name = new char[maxLength];

	// register all the named entries
	for ( unsigned int j = 0; j < collSize; ++j )
	{
		m.i().getline ( name, maxLength, '\n' );
		m.registerE(collection.get(name));
	}

	delete [] name;

	// load all the named entries
//	for ( iterator p = begin(); p < end(); ++p )
//		(*p)->Load(i);
}

//----------------------------------------------------------
//-- Helpers: Save/Load of class RoleMaster
//----------------------------------------------------------

static void
SaveRoleMaster ( const RoleMaster& RM, SaveLoadManager& m )
{
	RoleMaster::const_iterator p, p_beg = RM.begin(), p_end = RM.end();
	// get the max length of the identifier in the collection
	size_t maxLength = 0, curLength, size = 0;

	for ( p = p_beg; p != p_end; p += 2, size++ )
		if ( maxLength < (curLength = strlen((*p)->getName())) )
			maxLength = curLength;

	// save number of entries and max length of the entry
	m.saveUInt(size);
	m.saveUInt(maxLength);

	// register const entries in the global map
	m.registerE(RM.getBotRole());
	m.registerE(RM.getTopRole());

	// save names of all (non-inverse) entries
	for ( p = p_beg; p != p_end; p += 2 )
	{
		TRole* R = *p;
		m.registerE(R);
		m.registerE(R->inverse());
		m.o() << R->getName() << "\n";
	}

//	// save the entries itself
//	for ( p = p_beg; p < p_end; ++p )
//		(*p)->Save(o);
//
//	// save the rest of the RM
//	o << "\nRT";
//	pTax->Save(o);
}

static void
LoadRoleMaster ( RoleMaster& RM, SaveLoadManager& m )
{
	// sanity check: Load shall be done for the empty collection and only once
//	fpp_assert ( size() == 0 );

	unsigned int RMSize, maxLength;
	RMSize = m.loadUInt();
	maxLength = m.loadUInt();
	++maxLength;
	char* name = new char[maxLength];

	// register const entries in the global map
	m.registerE(RM.getBotRole());
	m.registerE(RM.getTopRole());

	// register all the named entries
	for ( unsigned int j = 0; j < RMSize; ++j )
	{
		m.i().getline ( name, maxLength, '\n' );
		TRole* R = RM.ensureRoleName(name);
		m.registerE(R);
		m.registerE(R->inverse());
	}

	delete [] name;

//	// load all the named entries
//	for ( iterator p = begin(); p < end(); ++p )
//		(*p)->Load(i);
//
//	// load the rest of the RM
//	expectChar(i,'R');
//	expectChar(i,'T');
//	pTax = new Taxonomy ( &universalRole, &emptyRole );
//	pTax->Load(i);
//	useUndefinedNames = false;	// no names
}

//----------------------------------------------------------
//-- Helpers: Save/Load of class TDataType
//----------------------------------------------------------

static void
SaveDataType ( const TDataType* dt, SaveLoadManager& m )
{
	// register the DT itself
	m.registerE(dt->getType());
	// register all the data values excluding expressions
	typedef std::set<const TNamedEntry*> TNESet;
	TNESet expressions;
	TNECollection<TDataEntry>::const_iterator p, p_beg = dt->begin(), p_end = dt->end();
	for ( p = p_beg; p != p_end; ++p )
		if ( (*p)->isRestrictedDataType() )
			expressions.insert(*p);
	SaveTNECollection ( *dt, m, expressions );
	// for expressions: register facets in the same order
	for ( p = p_beg; p != p_end; ++p )
		if ( (*p)->isRestrictedDataType() )
		{
			const TDataEntry* de = static_cast<const TDataEntry*>(*p);
			m.registerE(de);
		}
}

static void
LoadDataType ( TDataType* dt, SaveLoadManager& m )
{
	// register the DT itself
	m.registerE(dt->getType());
	// register all the data values
	LoadTNECollection ( *dt, m );
	// for expressions: register facets
	for ( TNECollection<TDataEntry>::const_iterator p = dt->begin(), p_end = dt->end(); p != p_end; ++p )
		if ( (*p)->isRestrictedDataType() )
			m.registerE(*p);
}


//----------------------------------------------------------
//-- Implementation of the DLDag methods (dlDag.h)
//----------------------------------------------------------

static void
SaveDLDag ( const DLDag& dag, SaveLoadManager& m )
{
	m.saveUInt(dag.size());
	m.o() << "\n";
	// skip fake vertex and TOP
	for ( unsigned int i = 2; i < dag.size(); ++i )
		dag[i].Save(m);
}

void
LoadDLDag ( DLDag& dag, SaveLoadManager& m )
{
	unsigned int j, size;
	size = m.loadUInt();
	for ( j = 2; j < size; ++j )
	{
		DagTag tag = static_cast<DagTag>(m.loadUInt());
		DLVertex* v = new DLVertex(tag);
		v->Load(m);
		dag.directAdd(v);
	}

	// only reasoning now -- no cache
	dag.setFinalSize();
}

/// @return true if the DAG in the SL structure is the same that is loaded
static bool
VerifyDag ( const DLDag& dag, SaveLoadManager& m )
{
	unsigned int j, size;
	size = m.loadUInt();

	if ( size != dag.size() )
	{
		std::cout << "DAG verification fail: size " << size << ", expected " << dag.size() << "\n";
		return false;
	}

	for ( j = 2; j < size; ++j )
	{
		DagTag tag = static_cast<DagTag>(m.loadUInt());
		DLVertex* v = new DLVertex(tag);
		v->Load(m);
		if ( *v != dag[j] )
		{
			std::cout << "DAG verification fail: dag entry at " << j << " is ";
			v->Print(std::cout);
			std::cout << ", expected ";
			dag[j].Print(std::cout);
			std::cout << "\n";
			delete v;
			return false;
		}
		delete v;
	}

	return true;
}

static void
SaveSingleCache ( SaveLoadManager& m, BipolarPointer bp, const modelCacheInterface* cache )
{
	if ( cache == NULL )
		return;
	m.saveSInt(bp);
	m.saveUInt(cache->getCacheType());
	switch ( cache->getCacheType() )
	{
	case modelCacheInterface::mctConst:
		m.saveUInt(cache->getState() == csValid);
		break;

	case modelCacheInterface::mctSingleton:
		m.saveSInt(dynamic_cast<const modelCacheSingleton*>(cache)->getValue());
		break;

	case modelCacheInterface::mctIan:
		dynamic_cast<const modelCacheIan*>(cache)->Save(m);
		break;

	default:
		fpp_unreachable();
	}
	m.o() << "\n";
}

static const modelCacheInterface*
LoadSingleCache ( SaveLoadManager& m )
{
	modelCacheState state = (modelCacheState)m.loadUInt();
	switch ( state )
	{
	case modelCacheInterface::mctConst:
		return new modelCacheConst ( m.loadUInt() != 0 );
	case modelCacheInterface::mctSingleton:
		return new modelCacheSingleton(m.loadSInt());
	case modelCacheInterface::mctIan:
	{
		bool hasNominals = m.loadUInt() > 0;
		unsigned int nC = m.loadUInt();
		unsigned int nR = m.loadUInt();
		modelCacheIan* cache = new modelCacheIan ( hasNominals, nC, nR );
		cache->Load(m);
		return cache;
	}

	default:
		fpp_unreachable();
	}
}

static void
SaveDagCache ( const DLDag& dag, SaveLoadManager& m )
{
	m.o() << "\nDC";	// dag cache
	for ( unsigned int i = 2; i < dag.size(); ++i )
	{
		const DLVertex& v = dag[(int)i];
		SaveSingleCache ( m, createBiPointer(i,true), v.getCache(true) );
		SaveSingleCache ( m, createBiPointer(i,false), v.getCache(false) );
	}
	m.saveUInt(0);
}

static void
LoadDagCache ( DLDag& dag, SaveLoadManager& m )
{
	m.expectChar('D');
	m.expectChar('C');
	while ( BipolarPointer bp = m.loadSInt() )
		dag.setCache ( bp, LoadSingleCache(m) );
}


//----------------------------------------------------------
//-- Implementation of the TBox methods (dlTBox.h)
//----------------------------------------------------------

void
TBox :: initPointerMaps ( SaveLoadManager& m ) const
{
	m.clearPointerMaps();
	m.registerE(pBottom);
	m.registerE(pTop);
	m.registerE(pTemp);
	m.registerE(pQuery);
}

void
TBox :: Save ( SaveLoadManager& m )
{
	initPointerMaps(m);
	m.o() << "\nDT";
	for ( DataTypeCenter::const_iterator p = DTCenter.begin(), p_end = DTCenter.end(); p != p_end; ++p )
		SaveDataType(*p,m);
	m.o() << "\nC";
	std::set<const TNamedEntry*> empty;
	SaveTNECollection(Concepts,m,empty);
	m.o() << "\nI";
	SaveTNECollection(Individuals,m,empty);
	m.o() << "\nOR";
	SaveRoleMaster(ORM,m);
	m.o() << "\nDR";
	SaveRoleMaster(DRM,m);
	m.o() << "\nD";
	DLHeap.removeQuery();
	SaveDLDag(DLHeap,m);
	if ( Status > kbCChecked )
	{
		m.o() << "\nCT";
		pTax->Save(m,empty);
	}
	SaveDagCache(DLHeap,m);
}

void
TBox :: Load ( SaveLoadManager& m, KBStatus status )
{
	Status = status;
	initPointerMaps(m);
	m.expectChar('D');
	m.expectChar('T');
	for ( DataTypeCenter::iterator p = DTCenter.begin(), p_end = DTCenter.end(); p != p_end; ++p )
		LoadDataType(*p,m);
	m.expectChar('C');
	LoadTNECollection(Concepts,m);
	m.expectChar('I');
	LoadTNECollection(Individuals,m);
	m.expectChar('O');
	m.expectChar('R');
	LoadRoleMaster(ORM,m);
	m.expectChar('D');
	m.expectChar('R');
	LoadRoleMaster(DRM,m);
	m.expectChar('D');
	DLHeap.setSubOrder();
//	LoadDLDag(DLHeap,m);
	if ( !VerifyDag(DLHeap,m) )
		throw EFPPSaveLoad("DAG verification failed");
//	initReasoner();
	if ( Status > kbCChecked )
	{
		initTaxonomy();
		pTaxCreator->setBottomUp(GCIs);
		m.expectChar('C');
		m.expectChar('T');
		pTax->Load(m);
	}
	LoadDagCache(DLHeap,m);
}

void
TBox :: SaveTaxonomy ( SaveLoadManager& m, const std::set<const TNamedEntry*>& excluded )
{
	initPointerMaps(m);
	m.o() << "\nC";
	SaveTNECollection(Concepts,m,excluded);
	m.o() << "\nI";
	SaveTNECollection(Individuals,m,excluded);
	m.o() << "\nCT";
	pTax->Save(m,excluded);
}

void
TBox :: LoadTaxonomy ( SaveLoadManager& m )
{
	initPointerMaps(m);
	m.expectChar('C');
	LoadTNECollection(Concepts,m);
	m.expectChar('I');
	LoadTNECollection(Individuals,m);
	initTaxonomy();
	pTaxCreator->setBottomUp(GCIs);
	m.expectChar('C');
	m.expectChar('T');
	pTax->Load(m);
}

//----------------------------------------------------------
//-- Save/Load incremental structures (Kernel.h)
//----------------------------------------------------------

void
ReasoningKernel :: SaveIncremental ( SaveLoadManager& m ) const
{
	if ( !useIncrementalReasoning )
		return;
	m.o() << "\nQ";
	m.saveUInt(Name2Sig.size());
	for ( NameSigMap::const_iterator p = Name2Sig.begin(), p_end = Name2Sig.end(); p != p_end; ++p )
	{
		m.savePointer(p->first);
		m.saveUInt(p->second->size());

		for ( TSignature::iterator q = p->second->begin(), q_end = p->second->end(); q != q_end; ++q )
			m.savePointer(*q);
	}
}

void
ReasoningKernel :: LoadIncremental ( SaveLoadManager& m )
{
	if ( !useIncrementalReasoning )
		return;
	m.expectChar('Q');
	Name2Sig.clear();
	unsigned int size = m.loadUInt();
	for ( unsigned int j = 0; j < size; j++ )
	{
		TNamedEntity* entity = m.loadEntity();
		unsigned int sigSize = m.loadUInt();
		TSignature* sig = new TSignature();
		for ( unsigned int k = 0; k < sigSize; k++ )
			sig->add(m.loadEntity());
		Name2Sig[entity] = sig;
	}
}

//----------------------------------------------------------
//-- Implementation of the TNamedEntry methods (tNamedEntry.h)
//----------------------------------------------------------

void
TNamedEntry :: Save ( SaveLoadManager& m ) const
{
	m.saveUInt(getAllFlags());
}

void
TNamedEntry :: Load ( SaveLoadManager& m )
{
	setAllFlags(m.loadUInt());
}

//----------------------------------------------------------
//-- Implementation of the TConcept methods (tConcept.h)
//----------------------------------------------------------

void
TConcept :: Save ( SaveLoadManager& m ) const
{
	ClassifiableEntry::Save(m);
	m.saveUInt((unsigned int)classTag);
	m.saveUInt(tsDepth);
	m.saveSInt(pName);
	m.saveSInt(pBody);
	m.saveUInt(posFeatures.getAllFlags());
	m.saveUInt(negFeatures.getAllFlags());
//	ERSet.Save(m);
}

void
TConcept :: Load ( SaveLoadManager& m )
{
	ClassifiableEntry::Load(m);
	classTag = CTTag(m.loadUInt());
	tsDepth = m.loadUInt();
	pName = m.loadSInt();
	pBody = m.loadSInt();
	posFeatures.setAllFlags(m.loadUInt());
	negFeatures.setAllFlags(m.loadUInt());
//	ERSet.Load(m);
}

//----------------------------------------------------------
//-- Implementation of the TIndividual methods (tIndividual.h)
//----------------------------------------------------------

void
TIndividual :: Save ( SaveLoadManager& m ) const
{
	TConcept::Save(m);
//	RelatedIndex.Save(m);
}

void
TIndividual :: Load ( SaveLoadManager& m )
{
	TConcept::Load(m);
//	RelatedIndex.Load(m);
}

//----------------------------------------------------------
//-- Implementation of the TRole methods (tRole.h)
//----------------------------------------------------------

void
TRole :: Save ( SaveLoadManager& m ) const
{
	ClassifiableEntry::Save(m);
	// FIXME!! think about automaton
}

void
TRole :: Load ( SaveLoadManager& m )
{
	ClassifiableEntry::Load(m);
	// FIXME!! think about automaton
}

//----------------------------------------------------------
//-- Implementation of the TaxonomyVertex methods (taxVertex.h)
//----------------------------------------------------------

void
TaxonomyVertex :: SaveLabel ( SaveLoadManager& m ) const
{
	m.savePointer(sample);
	m.saveUInt(synonyms.size());
	for ( syn_iterator p = begin_syn(), p_end = end_syn(); p < p_end; ++p )
		m.savePointer(*p);
	m.o() << "\n";
}

void
TaxonomyVertex :: LoadLabel ( SaveLoadManager& m )
{
	// note that sample is already loaded
	unsigned int size = m.loadUInt();
	for ( unsigned int j = 0; j < size; ++j )
		addSynonym(static_cast<ClassifiableEntry*>(m.loadEntry()));
}

void
TaxonomyVertex :: SaveNeighbours ( SaveLoadManager& m ) const
{
	const_iterator p, p_end;
	m.saveUInt(neigh(true).size());
	for ( p = begin(true), p_end = end(true); p != p_end; ++p )
		m.savePointer(*p);
	m.saveUInt(neigh(false).size());
	for ( p = begin(false), p_end = end(false); p != p_end; ++p )
		m.savePointer(*p);
	m.o() << "\n";
}

void
TaxonomyVertex :: LoadNeighbours ( SaveLoadManager& m )
{
	unsigned int j, size;
	size = m.loadUInt();
	for ( j = 0; j < size; ++j )
		addNeighbour ( true, m.loadVertex() );
	size = m.loadUInt();
	for ( j = 0; j < size; ++j )
		addNeighbour ( false, m.loadVertex() );
}

//----------------------------------------------------------
//-- Implementation of the Taxonomy methods (Taxonomy.h)
//----------------------------------------------------------

void
Taxonomy :: Save ( SaveLoadManager& m, const std::set<const TNamedEntry*>& excluded ) const
{
	TaxVertexVec::const_iterator p, p_beg = Graph.begin(), p_end = Graph.end();
	for ( p = p_beg; p != p_end; ++p )
		m.registerV(*p);

	// save number of taxonomy elements
	m.saveUInt(Graph.size()/*-excluded.size()*/);
	m.o() << "\n";

	// save labels for all verteces of the taxonomy
	for ( p = p_beg; p != p_end; ++p )
//		if ( excluded.count((*p)->getPrimer()) == 0 )
			(*p)->SaveLabel(m);

	// save the taxonomys hierarchy
	for ( p = p_beg; p != p_end; ++p )
//		if ( excluded.count((*p)->getPrimer()) == 0 )
			(*p)->SaveNeighbours(m);
}

void
Taxonomy :: Load ( SaveLoadManager& m )
{
	unsigned int size = m.loadUInt();
	Graph.clear();	// both TOP and BOTTOM elements would be load;

	// create all the verteces and load their labels
	for ( unsigned int j = 0; j < size; ++j )
	{
		ClassifiableEntry* p = static_cast<ClassifiableEntry*>(m.loadEntry());
		TaxonomyVertex* v = new TaxonomyVertex(p);
		Graph.push_back(v);
		v->LoadLabel(m);
		m.registerV(v);
	}

	// load the hierarchy
	for ( TaxVertexVec::iterator p = Graph.begin(), p_end = Graph.end(); p < p_end; ++p )
		(*p)->LoadNeighbours(m);
}

//----------------------------------------------------------
//-- Implementation of the modelCacheIan methods (modelCacheIan.h)
//----------------------------------------------------------

static void
SaveIndexSet ( SaveLoadManager& m, const TSetAsTree& Set )
{
	m.saveUInt(Set.size());
	for ( TSetAsTree::const_iterator p = Set.begin(), p_end = Set.end(); p != p_end; ++p )
		m.saveUInt(*p);
}

static void
LoadIndexSet ( SaveLoadManager& m, TSetAsTree& Set )
{
	unsigned int n = m.loadUInt();
	for ( unsigned int i = 0; i < n; i++ )
		Set.insert(m.loadUInt());
}

void
modelCacheIan :: Save ( SaveLoadManager& m ) const
{
	// header: hasNominals, nC, nR
	m.saveUInt(hasNominalNode);
	m.saveUInt(posDConcepts.maxSize());
	m.saveUInt(existsRoles.maxSize());
	// the body that will be loaded
	SaveIndexSet(m,posDConcepts);
	SaveIndexSet(m,posNConcepts);
	SaveIndexSet(m,negDConcepts);
	SaveIndexSet(m,negNConcepts);
#ifdef RKG_USE_SIMPLE_RULES
	SaveIndexSet(o,extraDConcepts);
	SaveIndexSet(o,extraNConcepts);
#endif
	SaveIndexSet(m,existsRoles);
	SaveIndexSet(m,forallRoles);
	SaveIndexSet(m,funcRoles);
	m.saveUInt(curState);
}

void
modelCacheIan :: Load ( SaveLoadManager& m )
{
	// note that nominals, nC and nR already read, and all sets are created
	LoadIndexSet(m,posDConcepts);
	LoadIndexSet(m,posNConcepts);
	LoadIndexSet(m,negDConcepts);
	LoadIndexSet(m,negNConcepts);
#ifdef RKG_USE_SIMPLE_RULES
	LoadIndexSet(m,extraDConcepts);
	LoadIndexSet(m,extraNConcepts);
#endif
	LoadIndexSet(m,existsRoles);
	LoadIndexSet(m,forallRoles);
	LoadIndexSet(m,funcRoles);
	curState = (modelCacheState) m.loadUInt();
}

//----------------------------------------------------------
//-- Implementation of the DLVertex methods (dlVertex.h)
//----------------------------------------------------------

void
DLVertex :: Save ( SaveLoadManager& m ) const
{
	m.saveUInt(static_cast<unsigned int>(Type()));

	switch ( Type() )
	{
	case dtBad:
	case dtTop:		// can't be S/L
	default:
		fpp_unreachable();
		break;

	case dtAnd:
		m.saveUInt(Child.size());
		for ( const_iterator p = begin(); p != end(); ++p )
			m.saveSInt(*p);
		break;

	case dtLE:
		m.savePointer(Role);
		m.saveSInt(getC());
		m.saveUInt(getNumberLE());
		break;

	case dtForall:	// n here is for the automaton state
		m.savePointer(Role);
		m.saveSInt(getC());
		m.saveUInt(getNumberLE());
		break;

	case dtIrr:
		m.savePointer(Role);
		break;

	case dtPConcept:
	case dtNConcept:
	case dtPSingleton:
	case dtNSingleton:
		m.savePointer(Concept);
		m.saveSInt(getC());
		break;

	case dtProj:
		m.saveSInt(getC());
		m.savePointer(Role);
		m.savePointer(ProjRole);
		break;

	case dtNN:	// nothing to do
		break;

	case dtDataType:
	case dtDataValue:
	case dtDataExpr:
		m.savePointer(Concept);
		m.saveSInt(getC());
		break;
	}
	m.o() << "\n";
}

void
DLVertex :: Load ( SaveLoadManager& m )
{
	// now OP is already loaded
	switch ( Type() )
	{
	case dtBad:
	case dtTop:		// can't be S/L
	default:
		fpp_unreachable();
		break;

	case dtAnd:
	{
		unsigned int size = m.loadUInt();
		for ( unsigned int j = 0; j < size; ++j )
			Child.push_back(m.loadSInt());
		break;
	}

	case dtLE:
		Role = static_cast<const TRole*>(m.loadEntry());
		setChild(m.loadSInt());
		n = m.loadUInt();
		break;

	case dtForall:
		Role = static_cast<const TRole*>(m.loadEntry());
		setChild(m.loadSInt());
		n = m.loadUInt();
		break;

	case dtIrr:
		Role = static_cast<const TRole*>(m.loadEntry());
		break;

	case dtPConcept:
	case dtNConcept:
	case dtPSingleton:
	case dtNSingleton:
		setConcept(m.loadEntry());
		setChild(m.loadSInt());
		break;

	case dtProj:
		setChild(m.loadSInt());
		Role = static_cast<const TRole*>(m.loadEntry());
		ProjRole = static_cast<const TRole*>(m.loadEntry());
		break;

	case dtNN:	// nothing to do
		break;

	case dtDataType:
	case dtDataValue:
	case dtDataExpr:
		setConcept(m.loadEntry());
		setChild(m.loadSInt());
		break;
	}
}