00001 package edu.tum.cs.logic; 00002 00003 import java.io.PrintStream; 00004 00005 import edu.tum.cs.srl.Database; 00006 import edu.tum.cs.srl.RelationalModel; 00007 import edu.tum.cs.srl.AbstractVariable; 00008 00009 public class PossibleWorld implements IPossibleWorld { 00010 00011 protected WorldVariables worldVars; 00012 protected boolean[] state; 00013 00014 public PossibleWorld(WorldVariables worldVars) { 00015 this.worldVars = worldVars; 00016 this.state = new boolean[worldVars.size()]; 00017 } 00018 00019 public PossibleWorld(WorldVariables worldVars, boolean[] state) { 00020 if(state.length != worldVars.size()) 00021 throw new IllegalArgumentException("Size of state array does not much number of variables"); 00022 this.worldVars = worldVars; 00023 this.state = state; 00024 } 00025 00026 public boolean isTrue(GroundAtom ga) { 00027 return state[ga.index]; 00028 } 00029 00030 public void set(String gndAtom, boolean value) { 00031 state[worldVars.get(gndAtom).index] = value; 00032 } 00033 00034 public void set(GroundAtom gndAtom, boolean value) { 00035 state[gndAtom.index] = value; 00036 } 00037 00038 public void set(int idxGndAtom, boolean value) { 00039 state[idxGndAtom] = value; 00040 } 00041 00042 public boolean get(int idxGndAtom) { 00043 return state[idxGndAtom]; 00044 } 00045 00046 public PossibleWorld clone() { 00047 return new PossibleWorld(worldVars, state.clone()); 00048 } 00049 00050 public boolean[] getState() { 00051 return state; 00052 } 00053 00054 public WorldVariables getVariables() { 00055 return worldVars; 00056 } 00057 00058 public void print() { 00059 print(System.out); 00060 } 00061 00062 public void print(PrintStream out) { 00063 out.println("world size: " + worldVars.size()); 00064 for(int i = 0; i < worldVars.size(); i++) { 00065 GroundAtom ga = worldVars.get(i); 00066 out.println(ga.index + " " + ga + " " + ga.isTrue(this)); 00067 } 00068 } 00069 00070 public void setState(boolean[] state){ 00071 if (state.length == this.state.length) 00072 this.state = state; 00073 else 00074 throw new IllegalArgumentException("Size of state array does not match number of variables!"); 00075 } 00076 00077 public void setEvidence(Database db) throws Exception { 00078 for(AbstractVariable var : db.getEntries()) { 00079 this.set(var.getPredicate(), var.isTrue()); 00080 } 00081 } 00082 }