00001
00002
00003
00004
00005 package edu.tum.cs.srl.mln.inference;
00006
00007 import java.util.ArrayList;
00008 import java.util.Vector;
00009 import java.util.regex.Pattern;
00010
00011 import edu.tum.cs.inference.IParameterHandler;
00012 import edu.tum.cs.inference.ParameterHandler;
00013 import edu.tum.cs.logic.GroundAtom;
00014 import edu.tum.cs.srl.mln.MarkovRandomField;
00015
00021 public abstract class InferenceAlgorithm implements IParameterHandler {
00022
00023 protected MarkovRandomField mrf;
00024 protected ParameterHandler paramHandler;
00025 protected boolean debug = false;
00026 protected boolean verbose = true;
00027
00028 public InferenceAlgorithm(MarkovRandomField mrf) throws Exception {
00029 this.mrf = mrf;
00030 paramHandler = new ParameterHandler(this);
00031 paramHandler.add("debug", "setDebugMode");
00032 paramHandler.add("verbose", "setVerbose");
00033 }
00034
00035 public void setDebugMode(boolean active) {
00036 debug = active;
00037 }
00038
00039 public void setVerbose(boolean verbose) {
00040 this.verbose = verbose;
00041 }
00042
00043 public abstract double getResult(GroundAtom ga);
00044
00045 public ArrayList<InferenceResult> getResults(Iterable<String> queries) {
00046
00047 Vector<Pattern> patterns = new Vector<Pattern>();
00048 for(String query : queries) {
00049 String p = query;
00050 p = Pattern.compile("([,\\(])([a-z][^,\\)]*)").matcher(p).replaceAll("$1.*?");
00051 p = p.replace("(", "\\(").replace(")", "\\)") + ".*";
00052 patterns.add(Pattern.compile(p));
00053
00054 }
00055
00056
00057 ArrayList<InferenceResult> results = new ArrayList<InferenceResult>();
00058 int numRes = 0;
00059 for(GroundAtom ga : mrf.getWorldVariables())
00060 for(Pattern pattern : patterns)
00061 if(pattern.matcher(ga.toString()).matches()) {
00062 results.add(new InferenceResult(ga, getResult(ga)));
00063 numRes++;
00064 break;
00065 }
00066 if(numRes == 0)
00067 System.err.println("Warning: None of the queries could be matched to a variable.");
00068 return results;
00069 }
00070
00071 public abstract ArrayList<InferenceResult> infer(Iterable<String> queries, int maxSteps) throws Exception;
00072
00073 public String getAlgorithmName() {
00074 return this.getClass().getSimpleName();
00075 }
00076
00077 public ParameterHandler getParameterHandler() {
00078 return paramHandler;
00079 }
00080 }