package euler; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintStream; import java.net.URLDecoder; import java.util.ArrayList; import java.util.Arrays; import java.util.Enumeration; /** * Euler proof engine wrapper * * @author Jos De Roo */ public class ProofEngine { static boolean windows = System.getProperty("os.name").startsWith("Windows"); static boolean linux = System.getProperty("os.name").startsWith("Linux"); /** * runProofEngine method * * @param args * <options>* <data>* <query>* * @param ops * output print stream * @param eps * error print stream * * @return the process */ public static Process runProofEngine(String[] args, PrintStream ops, PrintStream eps) { Process pr = createProofEngine(args, ops, eps); executeProofEngine(pr); return pr; } /** * runProofEngine method * * @param args * <options>* <data>* <query>* * * @return the proof */ public static String runProofEngine(String[] args) { Process pr = runProofEngine(args, null, System.err); return pr.getOutput(); } /** * createProofEngine method * * @param args * <options>* <data>* <query>* * @param ops * output print stream * @param eps * error print stream * * @return the process */ public static Process createProofEngine(String[] args, PrintStream ops, PrintStream eps) { long ttl = Long.MAX_VALUE; String tmpdir = System.getProperty("java.io.tmpdir"); if (!windows) { tmpdir = "/tmp/"; } String sep = System.getProperty("file.separator"); if (!tmpdir.endsWith(sep)) { tmpdir += sep; } int j; String[] cmd; if (windows) { j = 6; cmd = new String[j + args.length]; cmd[0] = "swipl"; cmd[1] = "-x"; cmd[2] = "C:\\Program Files\\eye\\lib\\eye.pvm"; cmd[3] = "--"; cmd[4] = "--wget-path"; cmd[5] = tmpdir + "eye\\windows\\bin\\"; } else { j = 4; cmd = new String[j + args.length]; cmd[0] = "swipl"; cmd[1] = "-x"; cmd[2] = "/opt/eye/lib/eye.pvm"; cmd[3] = "--"; } ArrayList fl = new ArrayList(); for (int i = 0; i < args.length; i++) { if (args[i].startsWith("http://localhost/.context")) { try { File f = File.createTempFile("eye_", ".n3"); fl.add(f); FileOutputStream fos = new FileOutputStream(f); OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF8"); osw.write(Codd.urlDecode(args[i]).substring(26)); osw.flush(); osw.close(); cmd[j++] = f.getAbsolutePath(); } catch (Throwable t) { t.printStackTrace(); } } else { cmd[j++] = args[i]; } } Process pr = new Process(cmd, null, ops, eps); pr.setFileList(fl); return pr; } /** * executeProofEngine method * * @param pr * process */ public static void executeProofEngine(Process pr) { long ttl = Long.MAX_VALUE; ProcessHook ph = new ProcessHook(pr); Runtime rt = Runtime.getRuntime(); rt.addShutdownHook(ph); int ev = pr.execute(ttl); rt.removeShutdownHook(ph); ArrayList fl = pr.getFileList(); for (File f : fl) f.delete(); if (ev != 0) { throw new RuntimeException("Abnormal process termination"); } if (pr.err != null) { throw new RuntimeException(pr.err); } } /** * main method * * @param args * <options>* <data>* <query>* */ public static void main(String[] args) { try { PrintStream ps = new PrintStream(System.out, true, "UTF8"); runProofEngine(args, ps, System.err); } catch (Throwable e) { e.printStackTrace(); } } }