Running a d3web SessionContent unavailable! (broken link)https://www.d3web.de/images/out.png#
Once, you have created a KnowledgeBaseContent unavailable! (broken link)https://www.d3web.de/images/out.png instance (for example, by Loading a knowledge base), you can run a problem-solving session. During a problem-solving session you enter Question-Value pairs (FactContent unavailable! (broken link)https://www.d3web.de/images/out.png) and the system will derive appropriate solutions for the entered facts.
Creating a new Session#
Sessions are represented by instances of Session. Use the SessionFactory to quickly create a new Session instance for the KnowledgeBase instance:
1
Session session = SessionFactory.createSession(knowledgeBase);
Retrieve the next reasonable question#
The InterviewContent unavailable! (broken link)https://www.d3web.de/images/out.png, which can be accessed from the session by using the interview problem solver (PSMethod), determines the next question to be presented to the user. More precisely, a FormStrategy computes the next From that is presented in the dialog. A Form consists of a title and a list of active questions:
1 2 3
Interview interview = session.getSessionObject(session.getPSMethodInstance(PSMethodInterview.class));
Form form = interview.nextForm();
List<Question> activeQuestions = form.getActiveQuestions();
To retrieve a single question contained in the Form you may use the NextUnansweredQuestionFormStrategy. This can be explicitly set by
1
interview.setFormStrategy(new NextUnansweredQuestionFormStrategy());
Entering Facts to the Session#
Next, we subsequently add facts to the session. When already having the appropriate instances of Question and Value, respectively, we are able to simply set the value by
1 2 3 4 5 6 7
Question question = kb.getManager().searchQuestion("QuestionName");
ChoiceValue value = new ChoiceValue("ChoiceName");
Fact fact = FactFactory.createFact(session, question, value,
PSMethodUserSelected.getInstance(),
PSMethodUserSelected.getInstance());
session.getBlackboard().addValueFact(fact);
Please note, that we create a new Fact having the question and value. Additionally, the source of the fact is the User (PSMethodUserSelected) and the corresponding problem-solver is also the user. In different scenarios, the source may be the instance of a Rule used in the context of a particular PSMethod instance.
Check the Derived Solutions#
We retrieve the list of derived solutions together with their state by iterating over all solutions in the knowledge base and by asking for a diagnosis Rating that is different from UNCLEAR. This way, we return solution instances with states ESTABLISHED, SUGGESTED, and EXCLUDED.
1 2 3 4 5
for (Solution solution : knowledgeBase.getSolutions()) {
Rating state = session.getBlackboard().getState(solution);
if (!state.hasState(Rating.State.UNCLEAR))
out.println(" " + solution + " (" + state + ")");
}
Sometimes it is easier (and a bit faster) to just get all solutions in a particular state, e.g. all established solutions:
1 2 3 4
out.println("All established solutions:");
for (Solution solution : session.getBlackboard().getSolutions(Rating.State.ESTABLISHED)) {
out.println(solution);
}
Revisit the entered Facts#
You can simply retrieve all entered facts by the following code block:
1 2 3
for (ProtocolEntry entry : session.getProtocol().getProtocolHistory()) {
out.println(entry);
}
Please note, that the session manages a Protocol, where entered findings (question/value tuples) are stored in a chronological order.
The Complete Example#
Note: The current version of the full example should be found in d3web-GlobalTests as the class de.d3web.demos.RunningACase.
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
public void runDemoCase() throws IOException {
KnowledgeBase knowledgeBase = buildKnowledgeBase();
PrintStream out = System.out;
// Create a case (problem-solving session and set
// all specified question/answers
out.println("+++ Setting values +++");
Session session = SessionFactory.createSession(knowledgeBase);
// set: pregnant = yes
Fact fact1 = FactFactory.createUserEnteredFact(
pregnant, yes);
session.getBlackboard().addValueFact(fact1);
out.println(fact1);
// set: weight = 80
Fact fact2 = FactFactory.createUserEnteredFact(
weight, new NumValue(80));
session.getBlackboard().addValueFact(fact2);
out.println(fact2);
// Print all solutions with a state != UNCLEAR
out.println("+++ Solutions +++");
for (Solution solution : knowledgeBase.getManager().getSolutions()) {
Rating state = session.getBlackboard().getRating(solution);
if (!state.hasState(Rating.State.UNCLEAR)) {
out.println(" " + solution + " (" + state + ")");
}
}
// Show all entered findings
out.println("+++ Entered Questions +++");
for (ProtocolEntry entry : session.getProtocol().getProtocolHistory()) {
out.println(" " + entry);
}
}
private KnowledgeBase buildKnowledgeBase() throws IOException {
// root {container}
// - demoQuestion {containers}
// -- pregnant [oc]
// -- weight [num]
// Solution: dangerousMood
InitPluginManager.init();
KnowledgeBase kb = KnowledgeBaseUtils.createKnowledgeBase();
QASet root = kb.getRootQASet();
demoQuestions = new QContainer(root, "demoQuestions");
pregnant = new QuestionOC(demoQuestions, "pregnant", "yes", "no");
yes = new ChoiceValue(KnowledgeBaseUtils.findChoice(pregnant, "yes"));
weight = new QuestionNum(demoQuestions, "weight");
dangerousMood = new Solution(kb.getRootSolution(), "dangerousMood");
// Define the init questionnaire
kb.setInitQuestions(Arrays.asList(demoQuestions));
// Define the magic rule: preganant=yes AND weight > 70 => dangerousMood
// (P7)
List<Condition> terms = new ArrayList<Condition>();
terms.add(new CondEqual(pregnant, yes));
terms.add(new CondNumGreater(weight, (double) 70));
RuleFactory.createHeuristicPSRule(dangerousMood, Score.P7, new CondAnd(terms));
return kb;
}