00001 package edu.tum.cs.srl.bayesnets;
00002
00003 import java.util.Collection;
00004 import java.util.HashMap;
00005 import java.util.Map;
00006
00007 import edu.tum.cs.logic.Formula;
00008 import edu.tum.cs.logic.GroundAtom;
00009 import edu.tum.cs.logic.IPossibleWorld;
00010 import edu.tum.cs.logic.PossibleWorldFromDatabase;
00011 import edu.tum.cs.logic.WorldVariables;
00012 import edu.tum.cs.logic.parser.FormulaParser;
00013 import edu.tum.cs.logic.parser.ParseException;
00014 import edu.tum.cs.srl.Database;
00015
00016 public class DecisionNode extends ExtendedNode {
00017 protected boolean isOperator;
00018 protected enum Operator {
00019 Negation;
00020 };
00021 protected Operator operator;
00022 protected Formula formula;
00023
00024 public DecisionNode(RelationalBeliefNetwork rbn, edu.ksu.cis.bnj.ver3.core.BeliefNode node) throws ParseException {
00025 super(rbn, node);
00026
00027 operator = null;
00028 if(node.getName().equals("neg")) {
00029 operator = Operator.Negation;
00030 }
00031
00032 if(operator == null) {
00033 formula = FormulaParser.parse(node.getName());
00034 }
00035 }
00036
00046 public boolean isTrue(Map<String, String> varBinding, IPossibleWorld w, WorldVariables worldVars, Database db) throws Exception {
00047 if(operator != null) {
00048 Collection<DecisionNode> parents = this.getDecisionParents();
00049 switch(operator) {
00050 case Negation:
00051 if(parents.size() != 1)
00052 throw new Exception("Operator neg must have exactly one child");
00053 return !parents.iterator().next().isTrue(varBinding, w, worldVars, db);
00054 default:
00055 throw new Exception("Operator not handled");
00056 }
00057 }
00058 else {
00059 try {
00060 Formula gf = formula.ground(varBinding, worldVars, db);
00061 return gf.isTrue(w);
00062 }
00063 catch(Exception e) {
00064 throw new Exception("Cannot evaluate precondition " + formula + ": " + e.getMessage());
00065 }
00066 }
00067 }
00068
00078 public boolean isTrue(String[] paramNames, String[] actualParams, Database db, boolean closedWorld) throws Exception {
00079
00080 HashMap<String, String> varBinding = new HashMap<String, String>();
00081 for(int i = 0; i < paramNames.length; i++)
00082 varBinding.put(paramNames[i], actualParams[i]);
00083
00084 WorldVariables worldVars = new WorldVariables() {
00085 @Override
00086 public GroundAtom get(String gndAtom) {
00087 return new GroundAtom(gndAtom);
00088 }
00089 };
00090
00091 return isTrue(varBinding, new PossibleWorldFromDatabase(this.bn, db, closedWorld), worldVars, db);
00092 }
00093
00094 @Override
00095 public String toString() {
00096 if(operator != null) {
00097 Collection<DecisionNode> parents = this.getDecisionParents();
00098 switch(operator) {
00099 case Negation:
00100 return "!(" + parents.iterator().next().toString() + ")";
00101 default:
00102 throw new RuntimeException("Operator not handled");
00103 }
00104 }
00105 else {
00106 return this.formula.toString();
00107 }
00108 }
00109 }