00001 package edu.tum.cs.logic;
00002
00003 import java.util.HashMap;
00004 import java.util.Iterator;
00005 import java.util.Set;
00006 import java.util.Vector;
00007
00008 import edu.tum.cs.util.StringTool;
00009
00017 public class WorldVariables implements Iterable<GroundAtom> {
00018 protected HashMap<String, GroundAtom> vars;
00022 protected HashMap<Integer, Block> var2block;
00023 protected HashMap<Integer, GroundAtom> varsByIndex;
00024
00028 public WorldVariables() {
00029 vars = new HashMap<String, GroundAtom>();
00030 var2block = new HashMap<Integer, Block>();
00031 varsByIndex = new HashMap<Integer, GroundAtom>();
00032 }
00033
00038 public void add(GroundAtom gndAtom) {
00039 gndAtom.setIndex(vars.size());
00040 vars.put(gndAtom.toString(), gndAtom);
00041 varsByIndex.put(gndAtom.index, gndAtom);
00042 }
00043
00049 public Block addBlock(Vector<GroundAtom> block) {
00050 Block b = new Block(block);
00051 for(GroundAtom ga : block) {
00052 if(!vars.containsKey(ga.toString()))
00053 add(ga);
00054 var2block.put(ga.index, b);
00055 }
00056 return b;
00057 }
00058
00064 public GroundAtom get(String gndAtom) {
00065 return vars.get(gndAtom);
00066 }
00067
00068 public GroundAtom get(Integer index) {
00069 return varsByIndex.get(index);
00070 }
00071
00072 public Block getBlock(Integer idxGA) {
00073 return var2block.get(idxGA);
00074 }
00075
00076 public int size() {
00077 return vars.size();
00078 }
00079
00080 public Set<String> getVariableStrings() {
00081 return vars.keySet();
00082 }
00083
00084 public String toString() {
00085 return "<" + StringTool.join(" \n", vars.keySet()) + ">";
00086 }
00087
00088 public static class Block implements Iterable<GroundAtom> {
00089 protected Vector<GroundAtom> gndAtoms;
00090 protected GroundAtom trueOne;
00091
00092 public Block(Vector<GroundAtom> list) {
00093 gndAtoms = list;
00094 trueOne = null;
00095 }
00096
00097 public Iterator<GroundAtom> iterator() {
00098 return gndAtoms.iterator();
00099 }
00100
00101 public GroundAtom getTrueOne(IPossibleWorld w) {
00102
00103 for(GroundAtom ga : gndAtoms)
00104 if(ga.isTrue(w)) {
00105 trueOne = ga;
00106 break;
00107 }
00108
00109 return trueOne;
00110 }
00111
00112
00113
00114
00115
00116
00117 public int size() {
00118 return gndAtoms.size();
00119 }
00120
00121 public GroundAtom get(int index) {
00122 return gndAtoms.get(index);
00123 }
00124
00125 public int indexOf(GroundAtom gndAtom) {
00126 return gndAtoms.indexOf(gndAtom);
00127 }
00128 }
00129
00130 public Iterator<GroundAtom> iterator() {
00131 return this.vars.values().iterator();
00132 }
00133 }