コード例 #1
0
  /**
   * Perform a query.
   *
   * @param line The query.
   * @return The probability.
   */
  public double performQuery(String line) {
    if (this.query == null) {
      throw new BayesianError("This Bayesian network does not have a query to define.");
    }

    ParseProbability parse = new ParseProbability(this);
    ParsedProbability parsedProbability = parse.parse(line);

    // create a temp query
    BayesianQuery q = this.query.clone();

    // first, mark all events as hidden
    q.reset();

    // deal with evidence (input)
    for (ParsedEvent parsedEvent : parsedProbability.getGivenEvents()) {
      BayesianEvent event = this.requireEvent(parsedEvent.getLabel());
      q.defineEventType(event, EventType.Evidence);
      q.setEventValue(event, parsedEvent.resolveValue(event));
    }

    // deal with outcome (output)
    for (ParsedEvent parsedEvent : parsedProbability.getBaseEvents()) {
      BayesianEvent event = requireEvent(parsedEvent.getLabel());
      q.defineEventType(event, EventType.Outcome);
      q.setEventValue(event, parsedEvent.resolveValue(event));
    }

    q.locateEventTypes();

    q.execute();
    return q.getProbability();
  }
コード例 #2
0
 /**
  * Define a relationship.
  *
  * @param line The relationship to define.
  */
 public void defineRelationship(String line) {
   ParseProbability parse = new ParseProbability(this);
   ParsedProbability parsedProbability = parse.parse(line);
   parsedProbability.defineRelationships(this);
 }
コード例 #3
0
 /**
  * Define the probability for an event.
  *
  * @param line The event.
  * @param probability The probability.
  */
 public void defineProbability(String line, double probability) {
   ParseProbability parse = new ParseProbability(this);
   ParsedProbability parsedProbability = parse.parse(line);
   parsedProbability.defineTruthTable(this, probability);
 }