00001 package edu.tum.cs.srl.bayesnets.bln.py;
00002
00003 import java.util.Vector;
00004
00005 import org.python.core.PyObject;
00006 import org.python.core.PyObject.ConversionException;
00007
00008 import edu.tum.cs.tools.JythonInterpreter;
00009
00010 public class GroundFormula {
00011 protected String varName;
00012 protected JythonInterpreter jython;
00013 public int idxGF;
00014
00015 public GroundFormula(JythonInterpreter jython, int idxGF) throws ConversionException {
00016 this.jython = jython;
00017 varName = String.format("mln.gndFormulas[%d]", idxGF);
00018 this.idxGF = idxGF;
00019
00020 }
00021
00022 public Vector<String> getGroundAtoms() {
00023 PyObject list = jython.eval("%s.getGroundAtoms()", varName);
00024 Vector<String> v = new Vector<String>();
00025 for(int i = 0; i < list.__len__(); i++) {
00026 v.add(list.__getitem__(i).__str__().toString());
00027 }
00028 return v;
00029 }
00030
00031 public boolean isTrue(State state) throws ConversionException {
00032 return jython.evalBoolean("%s.isTrue(%s)", varName, state.varName);
00033 }
00034
00035 public void toCNF() throws ConversionException {
00036 jython.exec("%s = %s.toCNF()", varName, varName);
00037 if(jython.evalBoolean("type(%s)==MLN.FOL.Conjunction", varName)) {
00038 int numChildren = jython.evalInt("len(%s.children)", varName);
00039 for(int i = 0; i < numChildren; i++) {
00040 makeClause(String.format("%s.children[%d]", varName, i));
00041 }
00042 }
00043 else
00044 makeClause(varName);
00045 }
00046
00047 protected void makeClause(String cVar) throws ConversionException {
00048 System.out.println("clause: " + jython.evalString("str(%s)", cVar));
00049 }
00050
00051 public String toString() {
00052 try {
00053 return jython.evalString("str(%s)", varName);
00054 }
00055 catch (ConversionException e) {
00056 e.printStackTrace();
00057 return null;
00058 }
00059 }
00060 }