00001 package edu.tum.cs.srl.mln; 00002 00003 import java.io.PrintStream; 00004 00005 import edu.tum.cs.srl.Signature; 00006 import edu.tum.cs.util.StringTool; 00007 00008 public class MLNWriter { 00009 protected java.io.PrintStream out; 00010 00011 public MLNWriter(PrintStream out) { 00012 this.out = out; 00013 } 00014 00015 public void writeDomainDecl(String domName, String[] elems) { 00016 out.printf("%s = {%s}\n", formatAsTypeName(domName), StringTool.join(", ", elems)); 00017 } 00018 00019 public void writePredicateDecl(String predName, String[] types, Integer functionallyDeterminedArg) { 00020 out.print(formatAsPredName(predName)); 00021 out.print('('); 00022 for(int i = 0; i < types.length; i++) { 00023 if(i > 0) 00024 out.print(", "); 00025 out.print(formatAsTypeName(types[i])); 00026 if(functionallyDeterminedArg != null && i == functionallyDeterminedArg) 00027 out.print("!"); 00028 } 00029 out.println(')'); 00030 } 00031 00032 public void writePredicateDecl(Signature sig, Integer functionallyDeterminedArg) { 00033 writePredicateDecl(sig.functionName, sig.argTypes, functionallyDeterminedArg); 00034 } 00035 00042 public void writeMutexDecl(String predName, String[] params, String[] detParams) { 00043 out.print(formatAsPredName(predName)); 00044 out.print('('); 00045 for(int i = 0; i < params.length; i++) { 00046 if(i > 0) out.print(", "); 00047 out.print("a" + i); 00048 for(int j = 0; j < detParams.length; j++) 00049 if(params[i].equals(detParams[j])) { 00050 out.print("!"); 00051 break; 00052 } 00053 } 00054 out.println(')'); 00055 } 00056 00057 public static String formatAsPredName(String predName) { 00058 return lowerCaseString(predName); 00059 } 00060 00061 public static String formatAsTypeName(String typeName) { 00062 return lowerCaseString(typeName); 00063 } 00064 00065 public static String formatAsAtom(String atom) { 00066 int pos = atom.indexOf('('); 00067 String predName = atom.substring(0, pos); 00068 String params = atom.substring(pos); 00069 return formatAsPredName(predName) + params; 00070 } 00071 00077 public static String lowerCaseString(String s) { 00078 char[] c = s.toCharArray(); 00079 c[0] = Character.toLowerCase(c[0]); 00080 return new String(c); 00081 } 00082 00088 public static String upperCaseString(String s) { 00089 char[] c = s.toCharArray(); 00090 c[0] = Character.toUpperCase(c[0]); 00091 return new String(c); 00092 } 00093 00094 }