This code is assumed to exist at the top of the Java source file:
import java.io.*;
import java.util.*;
import com.oracle.determinations.engine.*;
The following shows the question search loop:
// This is where the user's input will come from:
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Engine eng = EngineFactory.localEngine();
Rulebase rb = eng.getRulebase("blah");
Session sess = eng.createSession(rb, SessionManagementMode.UNMANAGED,
SessionSynchronizationMode.UNSYNCHRONIZED);
sess.setThinkMode(ThinkMode.IMMEDIATE);
EntityInstance globalEI = sess.getGlobalEntityInstance();
Entity globalEnt = globalEI.getEntity();
Attribute attr = globalEnt.getAttribute("a5");
// Perform question search until we have a result.
while(attr.isUnknown(globalEI))
{
Question question = attr.getNextQuestion(globalEI);
// Prompt the user with the text of the question:
System.out.println(question.getQuestionText());
// If this is the defining attribute of an entity,
// we must collect instances of the entity:
Attribute questionAttr = question.getAttribute();
if (questionAttr == null)
{
// We're being asked for instances of a relationship, but we
// need to work out which entity is requested.
// The relationship has a source and target
// for the entity it links, so we just use that.
Relationship rel = question.getRelationship();
Entity questionEntity = rel.getTargetEntity();
List targets = new ArrayList();
System.out.println("Enter a series of values, and type \"done\" to finish.");
String questionAnswer = in.readLine();
while(!questionAnswer.equalsIgnoreCase("done"))
{
// Create an instance of the entity within the
// entity instance that the question was asked.
targets.add( sess.createEntityInstance(questionEntity));
// Read the next response from the user:
questionAnswer = in.readLine();
}
// Finished collecting values, so set up the relationship
rel.setInstance(question.getEntityInstance(), targets);
}
else
{
// We're being asked for the value of an attribute.
// Get the value from the user:
String questionAnswer = in.readLine();
// Set the user's response within the entity instance
// that the question was asked. Assume the attributes
// are always text values.
questionAttr.setValue(question.getEntityInstance(), questionAnswer);
}
}
// Question search is over.
// See whether it was successful or not:
if(attr.isUncertain(globalEI)) {
System.out.println("The value is uncertain!");
} else {
String goalValue = (String) attr.getValue(globalEI);
System.out.println("The value of attribute a5 is " + goalValue);
}
// The session was created as an unmanaged session, so the garbage
// collector will free the session object and related objects.
This is the implementation of the displayControl() function used in the above code.
private static void displayControl(EntityInstance controlEntityInstance,
Control currentControl,
BufferedReader inputReader)
throws java.io.IOException
(
// Controls can be hidden:
if (!currentControl.isVisible(controlEntityInstance))
(
// Control is hidden, so get out now while you can!
return;
)
// Not all controls will have a caption:
String caption = currentControl.getCaption(controlEntityInstance);
if (caption.length() > 0)
(
System.out.println(caption);
)
// A fixed-value control has a specific value that must always be set:
if (currentControl.getControlType() == ControlType.FIXED_VALUE)
(
FixedValueControl fixedValControl = (FixedValueControl)currentControl;
fixedValControl.getAttribute().setValue(controlEntityInstance,
fixedValControl.getValueToSet());
)
// An attribute control is handled by just asking the user the answer to the question:
if (currentControl.getControlType().isAttributeControl())
{
AttributeControl attrControl = (AttributeControl)currentControl;
Attribute attr = attrControl.getAttribute();
// There are a number of sub-types of attribute control - but in this example
// we just ask for a textual response:
// If there was no caption, we still want to prompt the user for something.
// So use the question text of the attribute:
if (caption.length() == 0)
String questionText = attr.getText(controlEntityInstance,SentenceForm.QUESTION);
System.out.println(questionText);
String answer = inputReader.readLine();
// Set the answer - a blank string is considered uncertain:
if (answer.length() == 0)
(
attr.setValue(controlEntityInstance, HaleyUncertain.INSTANCE);
)
else
(
attr.setValue(controlEntityInstance, answer);
)
)
)
//Entity controls require asking the user for instances of each entity:
if (currentControl.getControlType() == ControlType.ENTITY)
(
EntityControl entityControl = (EntityControl)currentControl;
Entity collectedEntity = entityControl.getEntity();
Relationship collectedRel = entityControl.getRelationship();
// New instances need to be created on the Session object:
Session sess = controlEntityInstance.getSession();
List childTargets = new ArrayList();
// Keep prompting the user for instances of this entity until they give up:
while(true)
(
System.out.println("Enter the value for a new instance of " +
collectedRel.getName());
System.out.println("(or just press enter to finish creating instances)");
System.out.print("Value for " + collectedRel.getName() + "? ");
String name = inputReader.readLine();
// Set the answer - a blank string breaks the loop
if (name.length() == 0)
(
break;
)
// Create a new instance of the entity as requested:
EntityInstance newEntityInstance = sess.createEntityInstance(collectedEntity);
newEntityInstance.setName(name);
childTargets.add(newEntityInstance);
// Display each of the child controls, which will collect specific
//data about the new entity instance:
List childControls = entityControl.getControls();
Iterator iChildControl = childControls.iterator();
while(iChildControl.hasNext())
(
Control currentChildControl = (Control) iChildControl.next();
displayControl(newEntityInstance, currentChildControl, inputReader);
)
)
collectedRel.setInstance(controlEntityInstance, childTargets);
)
)