Debian Codegraph @

FaCT++.Python/lib/Kernel/SaveLoad.cpp

raw symbols SCIP
1 /* This file is part of the FaCT++ DL reasoner
2 Copyright (C) 2008-2015 Dmitry Tsarkov and The University of Manchester
3 Copyright (C) 2015-2017 Dmitry Tsarkov
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 //-------------------------------------------------------
21 //-- Saving/restoring internal state of the FaCT++
22 //-------------------------------------------------------
23
24 #include "Kernel.h"
25 #include "ReasonerNom.h" // for initReasoner()
26 #include "SaveLoadManager.h"
27
28 const char* ReasoningKernel :: InternalStateFileHeader = "FaCT++InternalStateDump1.0";
29
30 const int bytesInInt = sizeof(int);
31
32 #if 0
33 // FIXME!! try to avoid recursion later on
34 static inline
35 void saveUIntAux ( ostream& o, unsigned int n, const int rest )
36 {
37 if ( rest > 1 )
38 saveUIntAux ( o, n/256, rest-1 );
39 m.o() << (unsigned char)n%256;
40 }
41
42 static inline
43 void saveUInt ( ostream& o, unsigned int n )
44 {
45 saveUIntAux ( o, n, bytesInInt );
46 }
47
48 static inline
49 void saveSInt ( ostream& o, int n ) { saveUInt(o,n); }
50
51 static inline
52 unsigned int loadUInt ( istream& i )
53 {
54 static unsigned int ret = 0;
55 unsigned char byte;
56 for ( int j = bytesInInt-1; j >= 0; --j )
57 {
58 i >> byte;
59 ret *= 256;
60 ret += byte;
61 }
62 return ret;
63 }
64
65 static inline
66 int loadSInt ( istream& i ) { return (int)loadUInt(i); }
67 #endif // 0
68
69 //----------------------------------------------------------
70 //-- Implementation of the Kernel methods (Kernel.h)
71 //----------------------------------------------------------
72
73 void
74 ReasoningKernel :: Save ( SaveLoadManager& m )
75 {
76 TsProcTimer t;
77 t.Start();
78 m.checkStream();
79 SaveHeader(m);
80 m.checkStream();
81 SaveOptions(m);
82 m.checkStream();
83 SaveKB(m);
84 m.checkStream();
85 SaveIncremental(m);
86 m.checkStream();
87 t.Stop();
88 std::cout << "Reasoner internal state saved in " << t << " sec" << std::endl;
89 }
90
91 void
92 ReasoningKernel :: Save ( void )
93 {
94 fpp_assert ( pSLManager != nullptr );
95 pSLManager->prepare(/*input=*/false);
96 Save(*pSLManager);
97 }
98
99 void
100 ReasoningKernel :: Load ( SaveLoadManager& m )
101 {
102 TsProcTimer t;
103 t.Start();
104 m.checkStream();
105 // releaseKB(); // we'll start a new one if necessary
106 LoadHeader(m);
107 m.checkStream();
108 LoadOptions(m);
109 m.checkStream();
110 LoadKB(m);
111 m.checkStream();
112 LoadIncremental(m);
113 m.checkStream();
114 t.Stop();
115 std::cout << "Reasoner internal state loaded in " << t << " sec" << std::endl;
116 }
117
118 void
119 ReasoningKernel :: Load ( void )
120 {
121 fpp_assert ( pSLManager != nullptr );
122 pSLManager->prepare(/*input=*/true);
123 Load(*pSLManager);
124 }
125
126 //-- save/load header (Kernel.h)
127
128 void
129 ReasoningKernel :: SaveHeader ( SaveLoadManager& m ) const
130 {
131 m.o() << InternalStateFileHeader << "\n" << Version << "\n" << bytesInInt << "\n";
132 }
133
134 void
135 ReasoningKernel :: LoadHeader ( SaveLoadManager& m )
136 {
137 std::string str;
138 m.i() >> str;
139 if ( str != InternalStateFileHeader )
140 throw EFPPSaveLoad("Incompatible save/load header");
141 m.i() >> str;
142 // FIXME!! we don't check version equivalence for now
143 // if ( str != Version )
144 // return true;
145 int n;
146 m.i() >> n;
147 if ( n != bytesInInt )
148 throw EFPPSaveLoad("Saved file differ in word size");
149 }
150
151 //-- save/load options (Kernel.h)
152
153 void
154 ReasoningKernel :: SaveOptions ( SaveLoadManager& m ) const
155 {
156 m.o() << "Options\n";
157 }
158
159 void
160 ReasoningKernel :: LoadOptions ( SaveLoadManager& m )
161 {
162 std::string options;
163 m.i() >> options;
164 }
165
166 //-- save/load KB (Kernel.h)
167
168 void
169 ReasoningKernel :: SaveKB ( SaveLoadManager& m )
170 {
171 m.saveUInt((unsigned int)getStatus());
172 switch ( getStatus() )
173 {
174 case kbEmpty: // nothing to do
175 return;
176 case kbLoading:
177 throw EFPPSaveLoad("Can't save internal state of the unclassified reasoner");
178 default:
179 getTBox()->Save(m);
180 break;
181 }
182 }
183
184 void
185 ReasoningKernel :: LoadKB ( SaveLoadManager& m )
186 {
187 KBStatus status = (KBStatus)m.loadUInt();
188 // initCacheAndFlags(); // will be done
189 // no classification => no need to monitor
190 pMonitor = nullptr;
191 if ( status == kbEmpty )
192 return;
193 // newKB();
194 getTBox()->Load(m,status);
195 }
196
197 //----------------------------------------------------------
198 //-- Helpers: Save/Load of class TNECollection
199 //----------------------------------------------------------
200
201 /// Save all the objects in the collection
202 template <typename T>
203 static void
204 SaveTNECollection ( const TNECollection<T>& collection, SaveLoadManager& m, const std::set<const TNamedEntry*>& excluded )
205 {
206 typename TNECollection<T>::const_iterator p, p_beg = collection.begin(), p_end = collection.end();
207 // get the max length of the identifier in the collection
208 size_t maxLength = 0, curLength, size = 0;
209
210 for ( p = p_beg; p < p_end; ++p )
211 {
212 if ( excluded.count(*p) == 0 )
213 {
214 ++size;
215 if ( maxLength < (curLength = strlen((*p)->getName())) )
216 maxLength = curLength;
217 }
218 }
219
220 // save number of entries and max length of the entry
221 m.saveUInt(size);
222 m.saveUInt(maxLength);
223
224 // save names of all entries
225 for ( p = p_beg; p < p_end; ++p )
226 {
227 // register all entries in the global map
228 m.registerE(*p);
229 if ( excluded.count(*p) == 0 )
230 m.o() << (*p)->getName() << "\n";
231 }
232
233 // save the entries itself
234 // for ( p = p_beg; p < p_end; ++p )
235 // (*p)->Save(o);
236 }
237 /// Load all the objects into the collection
238 template <typename T>
239 static void
240 LoadTNECollection ( TNECollection<T>& collection, SaveLoadManager& m )
241 {
242 // sanity check: Load shall be done for the empty collection and only once
243 // fpp_assert ( size() == 0 );
244
245 unsigned int collSize, maxLength;
246 collSize = m.loadUInt();
247 maxLength = m.loadUInt();
248 ++maxLength;
249 char* name = new char[maxLength];
250
251 // register all the named entries
252 for ( unsigned int j = 0; j < collSize; ++j )
253 {
254 m.i().getline ( name, maxLength, '\n' );
255 m.registerE(collection.get(name));
256 }
257
258 delete [] name;
259
260 // load all the named entries
261 // for ( iterator p = begin(); p < end(); ++p )
262 // (*p)->Load(i);
263 }
264
265 //----------------------------------------------------------
266 //-- Helpers: Save/Load of class RoleMaster
267 //----------------------------------------------------------
268
269 static void
270 SaveRoleMaster ( const RoleMaster& RM, SaveLoadManager& m )
271 {
272 RoleMaster::const_iterator p, p_beg = RM.begin(), p_end = RM.end();
273 // get the max length of the identifier in the collection
274 size_t maxLength = 0, curLength, size = 0;
275
276 for ( p = p_beg; p != p_end; p += 2, size++ )
277 if ( maxLength < (curLength = strlen((*p)->getName())) )
278 maxLength = curLength;
279
280 // save number of entries and max length of the entry
281 m.saveUInt(size);
282 m.saveUInt(maxLength);
283
284 // register const entries in the global map
285 m.registerE(RM.getBotRole());
286 m.registerE(RM.getTopRole());
287
288 // save names of all (non-inverse) entries
289 for ( p = p_beg; p != p_end; p += 2 )
290 {
291 TRole* R = *p;
292 m.registerE(R);
293 m.registerE(R->inverse());
294 m.o() << R->getName() << "\n";
295 }
296
297 // // save the entries itself
298 // for ( p = p_beg; p < p_end; ++p )
299 // (*p)->Save(o);
300 //
301 // // save the rest of the RM
302 // o << "\nRT";
303 // pTax->Save(o);
304 }
305
306 static void
307 LoadRoleMaster ( RoleMaster& RM, SaveLoadManager& m )
308 {
309 // sanity check: Load shall be done for the empty collection and only once
310 // fpp_assert ( size() == 0 );
311
312 unsigned int RMSize, maxLength;
313 RMSize = m.loadUInt();
314 maxLength = m.loadUInt();
315 ++maxLength;
316 char* name = new char[maxLength];
317
318 // register const entries in the global map
319 m.registerE(RM.getBotRole());
320 m.registerE(RM.getTopRole());
321
322 // register all the named entries
323 for ( unsigned int j = 0; j < RMSize; ++j )
324 {
325 m.i().getline ( name, maxLength, '\n' );
326 TRole* R = RM.ensureRoleName(name);
327 m.registerE(R);
328 m.registerE(R->inverse());
329 }
330
331 delete [] name;
332
333 // // load all the named entries
334 // for ( iterator p = begin(); p < end(); ++p )
335 // (*p)->Load(i);
336 //
337 // // load the rest of the RM
338 // expectChar(i,'R');
339 // expectChar(i,'T');
340 // pTax = new Taxonomy ( &universalRole, &emptyRole );
341 // pTax->Load(i);
342 // useUndefinedNames = false; // no names
343 }
344
345 //----------------------------------------------------------
346 //-- Helpers: Save/Load of class TDataType
347 //----------------------------------------------------------
348
349 static void
350 SaveDataType ( const TDataType* dt, SaveLoadManager& m )
351 {
352 // register the DT itself
353 m.registerE(dt->getType());
354 // register all the data values excluding expressions
355 typedef std::set<const TNamedEntry*> TNESet;
356 TNESet expressions;
357 TNECollection<TDataEntry>::const_iterator p, p_beg = dt->begin(), p_end = dt->end();
358 for ( p = p_beg; p != p_end; ++p )
359 if ( (*p)->isRestrictedDataType() )
360 expressions.insert(*p);
361 SaveTNECollection ( *dt, m, expressions );
362 // for expressions: register facets in the same order
363 for ( p = p_beg; p != p_end; ++p )
364 if ( (*p)->isRestrictedDataType() )
365 {
366 const TDataEntry* de = static_cast<const TDataEntry*>(*p);
367 m.registerE(de);
368 }
369 }
370
371 static void
372 LoadDataType ( TDataType* dt, SaveLoadManager& m )
373 {
374 // register the DT itself
375 m.registerE(dt->getType());
376 // register all the data values
377 LoadTNECollection ( *dt, m );
378 // for expressions: register facets
379 for ( TNECollection<TDataEntry>::const_iterator p = dt->begin(), p_end = dt->end(); p != p_end; ++p )
380 if ( (*p)->isRestrictedDataType() )
381 m.registerE(*p);
382 }
383
384
385 //----------------------------------------------------------
386 //-- Implementation of the DLDag methods (dlDag.h)
387 //----------------------------------------------------------
388
389 static void
390 SaveDLDag ( const DLDag& dag, SaveLoadManager& m )
391 {
392 m.saveUInt(dag.size());
393 m.o() << "\n";
394 // skip fake vertex and TOP
395 for ( unsigned int i = 2; i < dag.size(); ++i )
396 dag[i].Save(m);
397 }
398
399 void
400 LoadDLDag ( DLDag& dag, SaveLoadManager& m )
401 {
402 unsigned int j, size;
403 size = m.loadUInt();
404 for ( j = 2; j < size; ++j )
405 {
406 DagTag tag = static_cast<DagTag>(m.loadUInt());
407 DLVertex* v = new DLVertex(tag);
408 v->Load(m);
409 dag.directAdd(v);
410 }
411
412 // only reasoning now -- no cache
413 dag.setFinalSize();
414 }
415
416 /// @return true if the DAG in the SL structure is the same that is loaded
417 static bool
418 VerifyDag ( const DLDag& dag, SaveLoadManager& m )
419 {
420 unsigned int j, size;
421 size = m.loadUInt();
422
423 if ( size != dag.size() )
424 {
425 std::cout << "DAG verification fail: size " << size << ", expected " << dag.size() << "\n";
426 return false;
427 }
428
429 for ( j = 2; j < size; ++j )
430 {
431 DagTag tag = static_cast<DagTag>(m.loadUInt());
432 DLVertex* v = new DLVertex(tag);
433 v->Load(m);
434 if ( *v != dag[j] )
435 {
436 std::cout << "DAG verification fail: dag entry at " << j << " is ";
437 v->Print(std::cout);
438 std::cout << ", expected ";
439 dag[j].Print(std::cout);
440 std::cout << "\n";
441 delete v;
442 return false;
443 }
444 delete v;
445 }
446
447 return true;
448 }
449
450 // we need a tag to save/load appropriate cache
451 enum class ModelCacheType: unsigned int { mctConst, mctSingleton, mctIan };
452
453 static void
454 SaveSingleCache ( SaveLoadManager& m, BipolarPointer bp, const modelCacheInterface* cache )
455 {
456 if ( cache == nullptr )
457 return;
458 m.saveSInt(bp);
459 // check all alternatives, starting from the most possible ones
460 if ( auto cacheIan = dynamic_cast<const modelCacheIan*>(cache) )
461 {
462 m.saveUInt(static_cast<unsigned int>(ModelCacheType::mctIan));
463 cacheIan->Save(m);
464 }
465 else if ( auto cacheSingleton = dynamic_cast<const modelCacheSingleton*>(cache) )
466 {
467 m.saveUInt(static_cast<unsigned int>(ModelCacheType::mctSingleton));
468 m.saveSInt(cacheSingleton->getValue());
469 }
470 else if ( dynamic_cast<const modelCacheConst*>(cache) )
471 {
472 m.saveUInt(static_cast<unsigned int>(ModelCacheType::mctConst));
473 m.saveUInt(cache->getState() == csValid);
474 }
475 else
476 fpp_unreachable();
477
478 m.o() << "\n";
479 }
480
481 static const modelCacheInterface*
482 LoadSingleCache ( SaveLoadManager& m )
483 {
484 ModelCacheType state = static_cast<ModelCacheType>(m.loadUInt());
485 switch ( state )
486 {
487 case ModelCacheType::mctConst:
488 return new modelCacheConst ( m.loadUInt() != 0 );
489 case ModelCacheType::mctSingleton:
490 return new modelCacheSingleton(m.loadSInt());
491 case ModelCacheType::mctIan:
492 {
493 bool hasNominals = m.loadUInt() > 0;
494 unsigned int nC = m.loadUInt();
495 unsigned int nR = m.loadUInt();
496 modelCacheIan* cache = new modelCacheIan ( hasNominals, nC, nR );
497 cache->Load(m);
498 return cache;
499 }
500
501 default:
502 fpp_unreachable();
503 }
504 }
505
506 static void
507 SaveDagCache ( const DLDag& dag, SaveLoadManager& m )
508 {
509 m.o() << "\nDC"; // dag cache
510 for ( unsigned int i = 2; i < dag.size(); ++i )
511 {
512 const DLVertex& v = dag[(int)i];
513 SaveSingleCache ( m, createBiPointer(i,true), v.getCache(true) );
514 SaveSingleCache ( m, createBiPointer(i,false), v.getCache(false) );
515 }
516 m.saveUInt(0);
517 }
518
519 static void
520 LoadDagCache ( DLDag& dag, SaveLoadManager& m )
521 {
522 m.expectChar('D');
523 m.expectChar('C');
524 while ( BipolarPointer bp = m.loadSInt() )
525 dag.setCache ( bp, LoadSingleCache(m) );
526 }
527
528
529 //----------------------------------------------------------
530 //-- Implementation of the TBox methods (dlTBox.h)
531 //----------------------------------------------------------
532
533 void
534 TBox :: initPointerMaps ( SaveLoadManager& m ) const
535 {
536 m.clearPointerMaps();
537 m.registerE(pBottom);
538 m.registerE(pTop);
539 m.registerE(pTemp);
540 m.registerE(pQuery);
541 }
542
543 void
544 TBox :: Save ( SaveLoadManager& m )
545 {
546 initPointerMaps(m);
547 m.o() << "\nDT";
548 for ( DataTypeCenter::const_iterator p = DTCenter.begin(), p_end = DTCenter.end(); p != p_end; ++p )
549 SaveDataType(*p,m);
550 m.o() << "\nC";
551 std::set<const TNamedEntry*> empty;
552 SaveTNECollection(Concepts,m,empty);
553 m.o() << "\nI";
554 SaveTNECollection(Individuals,m,empty);
555 m.o() << "\nOR";
556 SaveRoleMaster(ORM,m);
557 m.o() << "\nDR";
558 SaveRoleMaster(DRM,m);
559 m.o() << "\nD";
560 DLHeap.removeQuery();
561 SaveDLDag(DLHeap,m);
562 if ( Status > kbCChecked )
563 {
564 m.o() << "\nCT";
565 pTax->Save(m,empty);
566 }
567 SaveDagCache(DLHeap,m);
568 }
569
570 void
571 TBox :: Load ( SaveLoadManager& m, KBStatus status )
572 {
573 Status = status;
574 initPointerMaps(m);
575 m.expectChar('D');
576 m.expectChar('T');
577 for ( DataTypeCenter::iterator p = DTCenter.begin(), p_end = DTCenter.end(); p != p_end; ++p )
578 LoadDataType(*p,m);
579 m.expectChar('C');
580 LoadTNECollection(Concepts,m);
581 m.expectChar('I');
582 LoadTNECollection(Individuals,m);
583 m.expectChar('O');
584 m.expectChar('R');
585 LoadRoleMaster(ORM,m);
586 m.expectChar('D');
587 m.expectChar('R');
588 LoadRoleMaster(DRM,m);
589 m.expectChar('D');
590 DLHeap.setSubOrder();
591 // LoadDLDag(DLHeap,m);
592 if ( !VerifyDag(DLHeap,m) )
593 throw EFPPSaveLoad("DAG verification failed");
594 // initReasoner();
595 if ( Status > kbCChecked )
596 {
597 initTaxonomy();
598 pTaxCreator->setBottomUp(GCIs);
599 m.expectChar('C');
600 m.expectChar('T');
601 pTax->Load(m);
602 }
603 LoadDagCache(DLHeap,m);
604 }
605
606 void
607 TBox :: SaveTaxonomy ( SaveLoadManager& m, const std::set<const TNamedEntry*>& excluded )
608 {
609 initPointerMaps(m);
610 m.o() << "\nC";
611 SaveTNECollection(Concepts,m,excluded);
612 m.o() << "\nI";
613 SaveTNECollection(Individuals,m,excluded);
614 m.o() << "\nCT";
615 pTax->Save(m,excluded);
616 }
617
618 void
619 TBox :: LoadTaxonomy ( SaveLoadManager& m )
620 {
621 initPointerMaps(m);
622 m.expectChar('C');
623 LoadTNECollection(Concepts,m);
624 m.expectChar('I');
625 LoadTNECollection(Individuals,m);
626 initTaxonomy();
627 pTaxCreator->setBottomUp(GCIs);
628 m.expectChar('C');
629 m.expectChar('T');
630 pTax->Load(m);
631 }
632
633 //----------------------------------------------------------
634 //-- Save/Load incremental structures (Kernel.h)
635 //----------------------------------------------------------
636
637 void
638 ReasoningKernel :: SaveIncremental ( SaveLoadManager& m ) const
639 {
640 if ( !useIncrementalReasoning )
641 return;
642 m.o() << "\nQ";
643 m.saveUInt(Name2Sig.size());
644 for ( NameSigMap::const_iterator p = Name2Sig.begin(), p_end = Name2Sig.end(); p != p_end; ++p )
645 {
646 m.savePointer(p->first);
647 m.saveUInt(p->second->size());
648
649 for ( const TNamedEntity* entity : *p->second )
650 m.savePointer(entity);
651 }
652 }
653
654 void
655 ReasoningKernel :: LoadIncremental ( SaveLoadManager& m )
656 {
657 if ( !useIncrementalReasoning )
658 return;
659 m.expectChar('Q');
660 Name2Sig.clear();
661 unsigned int size = m.loadUInt();
662 for ( unsigned int j = 0; j < size; j++ )
663 {
664 TNamedEntity* entity = m.loadEntity();
665 unsigned int sigSize = m.loadUInt();
666 TSignature* sig = new TSignature();
667 for ( unsigned int k = 0; k < sigSize; k++ )
668 sig->add(m.loadEntity());
669 Name2Sig[entity] = sig;
670 }
671 }
672
673 //----------------------------------------------------------
674 //-- Implementation of the TNamedEntry methods (tNamedEntry.h)
675 //----------------------------------------------------------
676
677 void
678 TNamedEntry :: Save ( SaveLoadManager& m ) const
679 {
680 m.saveUInt(getAllFlags());
681 }
682
683 void
684 TNamedEntry :: Load ( SaveLoadManager& m )
685 {
686 setAllFlags(m.loadUInt());
687 }
688
689 //----------------------------------------------------------
690 //-- Implementation of the TConcept methods (tConcept.h)
691 //----------------------------------------------------------
692
693 void
694 TConcept :: Save ( SaveLoadManager& m ) const
695 {
696 ClassifiableEntry::Save(m);
697 m.saveUInt((unsigned int)classTag);
698 m.saveUInt(tsDepth);
699 m.saveSInt(pName);
700 m.saveSInt(pBody);
701 m.saveUInt(posFeatures.getAllFlags());
702 m.saveUInt(negFeatures.getAllFlags());
703 // ERSet.Save(m);
704 }
705
706 void
707 TConcept :: Load ( SaveLoadManager& m )
708 {
709 ClassifiableEntry::Load(m);
710 classTag = CTTag(m.loadUInt());
711 tsDepth = m.loadUInt();
712 pName = m.loadSInt();
713 pBody = m.loadSInt();
714 posFeatures.setAllFlags(m.loadUInt());
715 negFeatures.setAllFlags(m.loadUInt());
716 // ERSet.Load(m);
717 }
718
719 //----------------------------------------------------------
720 //-- Implementation of the TIndividual methods (tIndividual.h)
721 //----------------------------------------------------------
722
723 void
724 TIndividual :: Save ( SaveLoadManager& m ) const
725 {
726 TConcept::Save(m);
727 // RelatedIndex.Save(m);
728 }
729
730 void
731 TIndividual :: Load ( SaveLoadManager& m )
732 {
733 TConcept::Load(m);
734 // RelatedIndex.Load(m);
735 }
736
737 //----------------------------------------------------------
738 //-- Implementation of the TRole methods (tRole.h)
739 //----------------------------------------------------------
740
741 void
742 TRole :: Save ( SaveLoadManager& m ) const
743 {
744 ClassifiableEntry::Save(m);
745 // FIXME!! think about automaton
746 }
747
748 void
749 TRole :: Load ( SaveLoadManager& m )
750 {
751 ClassifiableEntry::Load(m);
752 // FIXME!! think about automaton
753 }
754
755 //----------------------------------------------------------
756 //-- Implementation of the TaxonomyVertex methods (taxVertex.h)
757 //----------------------------------------------------------
758
759 void
760 TaxonomyVertex :: SaveLabel ( SaveLoadManager& m ) const
761 {
762 m.savePointer(sample);
763 m.saveUInt(Synonyms.size());
764 for ( const auto& synonym: synonyms() )
765 m.savePointer(synonym);
766 m.o() << "\n";
767 }
768
769 void
770 TaxonomyVertex :: LoadLabel ( SaveLoadManager& m )
771 {
772 // note that sample is already loaded
773 unsigned int size = m.loadUInt();
774 for ( unsigned int j = 0; j < size; ++j )
775 addSynonym(static_cast<ClassifiableEntry*>(m.loadEntry()));
776 }
777
778 void
779 TaxonomyVertex :: SaveNeighbours ( SaveLoadManager& m ) const
780 {
781 const_iterator p, p_end;
782 m.saveUInt(neigh(true).size());
783 for ( p = begin(true), p_end = end(true); p != p_end; ++p )
784 m.savePointer(*p);
785 m.saveUInt(neigh(false).size());
786 for ( p = begin(false), p_end = end(false); p != p_end; ++p )
787 m.savePointer(*p);
788 m.o() << "\n";
789 }
790
791 void
792 TaxonomyVertex :: LoadNeighbours ( SaveLoadManager& m )
793 {
794 unsigned int j, size;
795 size = m.loadUInt();
796 for ( j = 0; j < size; ++j )
797 addNeighbour ( true, m.loadVertex() );
798 size = m.loadUInt();
799 for ( j = 0; j < size; ++j )
800 addNeighbour ( false, m.loadVertex() );
801 }
802
803 //----------------------------------------------------------
804 //-- Implementation of the Taxonomy methods (Taxonomy.h)
805 //----------------------------------------------------------
806
807 void
808 Taxonomy :: Save ( SaveLoadManager& m, const std::set<const TNamedEntry*>& excluded ATTR_UNUSED ) const
809 {
810 TaxVertexVec::const_iterator p, p_beg = Graph.begin(), p_end = Graph.end();
811 for ( p = p_beg; p != p_end; ++p )
812 m.registerV(*p);
813
814 // save number of taxonomy elements
815 m.saveUInt(Graph.size()/*-excluded.size()*/);
816 m.o() << "\n";
817
818 // save labels for all vertices of the taxonomy
819 for ( p = p_beg; p != p_end; ++p )
820 // if ( excluded.count((*p)->getPrimer()) == 0 )
821 (*p)->SaveLabel(m);
822
823 // save the taxonomies hierarchy
824 for ( p = p_beg; p != p_end; ++p )
825 // if ( excluded.count((*p)->getPrimer()) == 0 )
826 (*p)->SaveNeighbours(m);
827 }
828
829 void
830 Taxonomy :: Load ( SaveLoadManager& m )
831 {
832 unsigned int size = m.loadUInt();
833 Graph.clear(); // both TOP and BOTTOM elements would be load;
834
835 // create all the vertices and load their labels
836 for ( unsigned int j = 0; j < size; ++j )
837 {
838 ClassifiableEntry* p = static_cast<ClassifiableEntry*>(m.loadEntry());
839 TaxonomyVertex* v = new TaxonomyVertex(p);
840 Graph.push_back(v);
841 v->LoadLabel(m);
842 m.registerV(v);
843 }
844
845 // load the hierarchy
846 for ( TaxVertexVec::iterator p = Graph.begin(), p_end = Graph.end(); p < p_end; ++p )
847 (*p)->LoadNeighbours(m);
848 }
849
850 //----------------------------------------------------------
851 //-- Implementation of the modelCacheIan methods (modelCacheIan.h)
852 //----------------------------------------------------------
853
854 static void
855 SaveIndexSet ( SaveLoadManager& m, const TSetAsTree& Set )
856 {
857 m.saveUInt(Set.size());
858 for ( TSetAsTree::const_iterator p = Set.begin(), p_end = Set.end(); p != p_end; ++p )
859 m.saveUInt(*p);
860 }
861
862 static void
863 LoadIndexSet ( SaveLoadManager& m, TSetAsTree& Set )
864 {
865 unsigned int n = m.loadUInt();
866 for ( unsigned int i = 0; i < n; i++ )
867 Set.insert(m.loadUInt());
868 }
869
870 void
871 modelCacheIan :: Save ( SaveLoadManager& m ) const
872 {
873 // header: hasNominals, nC, nR
874 m.saveUInt(hasNominalNode);
875 m.saveUInt(posDConcepts.maxSize());
876 m.saveUInt(existsRoles.maxSize());
877 // the body that will be loaded
878 SaveIndexSet(m,posDConcepts);
879 SaveIndexSet(m,posNConcepts);
880 SaveIndexSet(m,negDConcepts);
881 SaveIndexSet(m,negNConcepts);
882 #ifdef RKG_USE_SIMPLE_RULES
883 SaveIndexSet(o,extraDConcepts);
884 SaveIndexSet(o,extraNConcepts);
885 #endif
886 SaveIndexSet(m,existsRoles);
887 SaveIndexSet(m,forallRoles);
888 SaveIndexSet(m,funcRoles);
889 m.saveUInt(curState);
890 }
891
892 void
893 modelCacheIan :: Load ( SaveLoadManager& m )
894 {
895 // note that nominals, nC and nR already read, and all sets are created
896 LoadIndexSet(m,posDConcepts);
897 LoadIndexSet(m,posNConcepts);
898 LoadIndexSet(m,negDConcepts);
899 LoadIndexSet(m,negNConcepts);
900 #ifdef RKG_USE_SIMPLE_RULES
901 LoadIndexSet(m,extraDConcepts);
902 LoadIndexSet(m,extraNConcepts);
903 #endif
904 LoadIndexSet(m,existsRoles);
905 LoadIndexSet(m,forallRoles);
906 LoadIndexSet(m,funcRoles);
907 curState = (modelCacheState) m.loadUInt();
908 }
909
910 //----------------------------------------------------------
911 //-- Implementation of the DLVertex methods (dlVertex.h)
912 //----------------------------------------------------------
913
914 void
915 DLVertex :: Save ( SaveLoadManager& m ) const
916 {
917 m.saveUInt(static_cast<unsigned int>(Type()));
918
919 switch ( Type() )
920 {
921 case dtBad:
922 case dtTop: // can't be S/L
923 default:
924 fpp_unreachable();
925 break;
926
927 case dtAnd:
928 m.saveUInt(Child.size());
929 for ( const_iterator p = begin(); p != end(); ++p )
930 m.saveSInt(*p);
931 break;
932
933 case dtLE:
934 m.savePointer(Role);
935 m.saveSInt(getC());
936 m.saveUInt(getNumberLE());
937 break;
938
939 case dtForall: // n here is for the automaton state
940 m.savePointer(Role);
941 m.saveSInt(getC());
942 m.saveUInt(getNumberLE());
943 break;
944
945 case dtIrr:
946 m.savePointer(Role);
947 break;
948
949 case dtPConcept:
950 case dtNConcept:
951 case dtPSingleton:
952 case dtNSingleton:
953 m.savePointer(Concept);
954 m.saveSInt(getC());
955 break;
956
957 case dtProj:
958 m.saveSInt(getC());
959 m.savePointer(Role);
960 m.savePointer(ProjRole);
961 break;
962
963 case dtNN: // nothing to do
964 break;
965
966 case dtDataType:
967 case dtDataValue:
968 case dtDataExpr:
969 m.savePointer(Concept);
970 m.saveSInt(getC());
971 break;
972 }
973 m.o() << "\n";
974 }
975
976 void
977 DLVertex :: Load ( SaveLoadManager& m )
978 {
979 // now OP is already loaded
980 switch ( Type() )
981 {
982 case dtBad:
983 case dtTop: // can't be S/L
984 default:
985 fpp_unreachable();
986 break;
987
988 case dtAnd:
989 {
990 unsigned int size = m.loadUInt();
991 for ( unsigned int j = 0; j < size; ++j )
992 Child.push_back(m.loadSInt());
993 break;
994 }
995
996 case dtLE:
997 Role = static_cast<const TRole*>(m.loadEntry());
998 setChild(m.loadSInt());
999 n = m.loadUInt();
1000 break;
1001
1002 case dtForall:
1003 Role = static_cast<const TRole*>(m.loadEntry());
1004 setChild(m.loadSInt());
1005 n = m.loadUInt();
1006 break;
1007
1008 case dtIrr:
1009 Role = static_cast<const TRole*>(m.loadEntry());
1010 break;
1011
1012 case dtPConcept:
1013 case dtNConcept:
1014 case dtPSingleton:
1015 case dtNSingleton:
1016 setConcept(m.loadEntry());
1017 setChild(m.loadSInt());
1018 break;
1019
1020 case dtProj:
1021 setChild(m.loadSInt());
1022 Role = static_cast<const TRole*>(m.loadEntry());
1023 ProjRole = static_cast<const TRole*>(m.loadEntry());
1024 break;
1025
1026 case dtNN: // nothing to do
1027 break;
1028
1029 case dtDataType:
1030 case dtDataValue:
1031 case dtDataExpr:
1032 setConcept(m.loadEntry());
1033 setChild(m.loadSInt());
1034 break;
1035 }
1036 }