!!! Loading a Knowledge Base
The central class of the knowledge base persistence is the {{PersistenceManager}} that organizes the reading and writing of a knowledge base by a collection of plugins (KnowledgeReader, KnowledgeWriter, FragmentHandler).
A d3web knowledge base is a jar file consisting of several files (mostly XML), and different plugins handle the different files contained in the jar.
We refer to [Knowledge Persistence] for a detailed description of how to implement your own PersistenceHandler plugin.
!! Initializing and Loading
Usually, the main application take care of loading all required plugins at start-up.
When writing your own small demo program, you need to initialize the plugins yourself by
%%prettify
{{{
InitPluginManager.init();
}}}
/%
After initialization, you are able to load the knowledge base into a KnowledgeBase instance by the following call
%%prettify
{{{
KnowledgeBase knowledgeBase = persistenceManager.load(new File(knowledgeFilename));
}}}
/%
where {{knowledgeFilename}} is a simple file path, such as "/temp/carDiagnosis.jar".
!! Play a little bit around
You are able to return the list of all solutions ({{Solution}} instances) by
%%prettify
{{{
List<Solution> solutions = knowledgeBase.getSolutions();
}}}
/%
Analogously, you retrieve a flat list of all questions of the knowledge base by
%%prettify
{{{
List<Question> questions = knowledgeBase.getQuestions();
}}}
/%
!! The Complete Code Example
%%prettify
{{{
// Initializing the plugins for the persistence
InitPluginManager.init();
PersistenceManager persistenceManager = PersistenceManager.getInstance();
// Load knowledge base
KnowledgeBase knowledgeBase = persistenceManager.load(new File(knowledgeFilename));
List<Solution> solutions = knowledgeBase.getSolutions();
System.out.println(solutions);
List<Question> questions = knowledgeBase.getQuestions();
System.out.println(questions);
}}}
/%