All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.articulate.sigma.Vampire Maven / Gradle / Ivy

Go to download

Sigma knowledge engineering system is an system for developing, viewing and debugging theories in first order logic. It works with Knowledge Interchange Format (KIF) and is optimized for the Suggested Upper Merged Ontology (SUMO) www.ontologyportal.org.

There is a newer version: 2.10
Show newest version
/** This code is copyright Articulate Software (c) 2003.  Some
portions copyright Teknowledge (c) 2003 and reused under the terms of the GNU license.
This software is released under the GNU Public License .
Users of this code also consent, by use of this code, to credit Articulate Software
and Teknowledge in any writings, briefings, publications, presentations, or 
other representations of any software which incorporates, builds on, or uses this 
code.  Please cite the following article in any publication with references:

Pease, A., (2003). The Sigma Ontology Development Environment, 
in Working Notes of the IJCAI-2003 Workshop on Ontology and Distributed Systems,
August 9, Acapulco, Mexico.  See also sigmakee.sourceforge.net
*/

package com.articulate.sigma;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Class for invoking the KIF version of Vampire from Java. Vampire takes XML input
 * and returns XML output.  Input forms are either KIF formula
 * or KIF formula.  Results of queries have the following form:
 * 
 *   
 *     
 *       
 *         
 *       
 *     
 *     
 *       
 *         
 *           
 *             
 *               KIF formula
 *             
 *           
 *         
 *          
 *           
 *             KIF formula
 *           
 *         
 *       
 *     
 *   
 *   
 * 
 * 
 * Note that if the result of a query is not a variable binding, then the 
 * element will be omitted.
 *
 
 * @author Andrei Voronkov
 * @since 14/08/2003, Acapulco 
 */

public class Vampire extends InferenceEngine {

    private Process _vampire;
    private BufferedReader _reader; 
    private BufferedWriter _writer; 
    private BufferedReader _error; 
    
    public static class VampireFactory extends EngineFactory {

        @Override
        public InferenceEngine createWithFormulas(Iterable formulaSource) {
                return Vampire.getNewInstanceWithFormulas(formulaSource);
        }

        @Override
        public InferenceEngine createFromKBFile(String kbFileName) {
                return Vampire.getNewInstance(kbFileName);
        }
    }

    public static EngineFactory getFactory() {
            return new VampireFactory();
    }

    /** *************************************************************
     * This static factory method returns a new Vampire instance.
     *
     * @param kbFileName The complete (absolute) pathname of the KB
     * file that will be used to populate the inference engine
     * instance with assertions.
     *
     * @throws IOException should not normally be thrown unless either
     *         Vampire executable or database file name are incorrect
     */
    public static Vampire getNewInstance (String kbFileName) {

        Vampire vpr = null;
        String error = null;

        try {
            File kbFile = null;
            if ( error == null ) {
                kbFile = new File( kbFileName );
                if ( ! kbFile.exists() ) {
                    error = ( "The file " + kbFileName + " does not exist" );
                    System.out.println( "INFO in Vampire.getNewInstance(): " + error );
                    KBmanager.getMgr().setError( KBmanager.getMgr().getError()
                                                 + "\n
" + error + "\n
" ); } } if ( error == null ) { KIF kif = new KIF(); kif.setParseMode( KIF.RELAXED_PARSE_MODE ); kif.readFile( kbFile.getCanonicalPath() ); Iterable formulaSource = kif.formulaMap.keySet(); vpr = getNewInstanceWithFormulas(formulaSource); } } catch (Exception ex) { System.out.println(ex.getMessage()); ex.printStackTrace(); } return vpr; } /** ************************************************************* * */ public static Vampire getNewInstanceWithFormulas(Iterable formulaSource) { Vampire vpr = null; String error = null; try { String execPathname = KBmanager.getMgr().getPref("inferenceEngine"); if (!StringUtil.isNonEmptyString(execPathname)) { error = "No pathname has been set for \"inferenceEngine\""; System.out.println("Error in Vampire.getNewInstanceWithFormulas(): " + error); KBmanager.getMgr().setError(KBmanager.getMgr().getError() + "\n
" + error + "\n
"); } File vampireExecutable = null; if (error == null) { vampireExecutable = new File(execPathname); if (!vampireExecutable.exists()) { error = ("The executable file " + vampireExecutable.getCanonicalPath() + " does not exist"); System.out.println("Error in Vampire.getNewInstanceWithFormulas(): " + error); KBmanager.getMgr().setError(KBmanager.getMgr().getError() + "\n
" + error + "\n
"); } } if (error == null) { File vampireDirectory = vampireExecutable.getParentFile(); System.out.println("INFO in Vampire.getNewInstanceWithFormulas(): executable == " + vampireExecutable.getCanonicalPath()); System.out.println("INFO in Vampire.getNewInstanceWithFormulas(): directory == " + vampireDirectory.getCanonicalPath()); // It should only ever be necessary to write this file once. File initFile = new File(vampireDirectory, "init-v.kif"); if (!initFile.exists()) { PrintWriter pw = new PrintWriter(initFile); pw.println("(instance Process Entity)"); pw.flush(); try { pw.close(); } catch (Exception e1) { } } System.out.println("INFO in Vampire.getNewInstanceWithFormulas(): " + "Starting vampire as " + vampireExecutable.getCanonicalPath() + " " + initFile.getCanonicalPath()); Vampire vprInst = new Vampire(vampireExecutable, initFile); if (vprInst instanceof Vampire) { Iterator it = formulaSource.iterator(); if (it.hasNext()) { List badFormulas = new ArrayList(); String formStr = null; String response = null; int goodCount = 0; long start = System.currentTimeMillis(); while (it.hasNext()) { formStr = (String) it.next(); response = vprInst.assertFormula(formStr); if (!(response.indexOf("Formula has been added") >= 0)) badFormulas.add(formStr); else goodCount++ ; } long duration = (System.currentTimeMillis() - start); System.out.println(goodCount + " formulas asserted to " + vprInst + " in " + (duration / 1000.0) + " seconds"); if (!badFormulas.isEmpty()) { int bc = badFormulas.size(); Iterator it2 = badFormulas.iterator(); System.out.println("INFO in Vampire.getNewInstanceWithFormulas(): " + bc + " FORMULA" + ((bc == 1) ? "" : "S") + " REJECTED "); int badCount = 1; String badStr = null; String mgrErrStr = KBmanager.getMgr().getError(); while (it2.hasNext()) { badStr = ("[" + badCount++ + "] " + ((String)it2.next())); System.out.println(badStr); mgrErrStr += ("\n
" + "Bad formula: " + badStr + "\n
"); } KBmanager.getMgr().setError(mgrErrStr); } if (goodCount > 0) vpr = vprInst; // If we've made it this far, we have a usable Vampire instance. } } } } catch (Exception ex) { System.out.println(ex.getMessage()); ex.printStackTrace(); } return vpr; } /** ************************************************************* * To obtain a new instance of Vampire, use the static factory * method Vampire.getNewInstance(). */ private Vampire () { } /** ************************************************************* * Creates a running instance of Vampire. To obtain a new * instance of Vampire, use the static factory method * Vampire.getNewInstance(). * * @param executable A File object denoting the platform-specific * Vampire executable. * * @param kbFile A File object denoting the initial knowledge base * to be loaded by the Vampire executable. * * @throws IOException should not normally be thrown unless either * Vampire executable or database file name are incorrect */ private Vampire (File executable, File kbFile) throws IOException { _vampire = Runtime.getRuntime().exec(executable.getCanonicalPath() + " " + kbFile.getCanonicalPath()); _reader = new BufferedReader(new InputStreamReader(_vampire.getInputStream())); _error = new BufferedReader(new InputStreamReader(_vampire.getErrorStream())); String line = null; while (_reader.ready() || _error.ready()) { if (_reader.ready()) line = _reader.readLine(); else if (_error.ready()) line = _error.readLine(); System.out.println("INFO in Vampire(): Return string: " + line); if (line.indexOf("Error:") != -1) throw new IOException(line); } _writer = new BufferedWriter(new OutputStreamWriter(_vampire.getOutputStream())); } /** ************************************************************* * Add an assertion. * * @param formula asserted formula in the KIF syntax * @return answer to the assertion (in the XML syntax) * @throws IOException should not normally be thrown */ public String assertFormula(String formula) throws IOException { String result = ""; try { StringBuilder assertion = new StringBuilder(); String safe = StringUtil.replaceUnsafeNamespaceDelimiters(formula); safe = StringUtil.replaceNonAsciiChars(safe); assertion.append(" "); assertion.append(safe); assertion.append(" \n"); _writer.write(assertion.toString()); _writer.flush(); for (;;) { String line = _reader.readLine(); if (line.indexOf("Error:") != -1) throw new IOException(line); // System.out.println("INFO Vampire.assertFormula(): Response: " + line); result += line + "\n"; if (line.indexOf("") != -1) break; } } catch (Exception ex) { System.out.println("Error in Vampire.assertFormula(" + formula + ")"); System.out.println(ex.getMessage()); ex.printStackTrace(); } return result; } /** ************************************************************* * Terminate this instance of Vampire. * Warning:After calling this function * no further assertions or queries can be done. * * @throws IOException should not normally be thrown */ public void terminate () throws IOException { System.out.println(); System.out.println("TERMINATING " + this); try { _writer.write("\n"); _writer.close(); _reader.close(); System.out.println("DESTROYING the Process " + _vampire); System.out.println(); _vampire.destroy(); } catch (Exception ex) { ex.printStackTrace(); } } /** ************************************************************* * Submit a query. * * @param formula query in the KIF syntax * @param timeLimit time limit for answering the query (in seconds) * @param bindingsLimit limit on the number of bindings * @return answer to the query (in the XML syntax) * @throws IOException should not normally be thrown */ public String submitQuery (String formula, int timeLimit, int bindingsLimit) throws IOException { String result = ""; String query = (" " + formula + " \n"); System.out.println("INFO in Vampire.submitQuery(): " + query); try { _writer.write(query); _writer.flush(); } catch (Exception ex) { System.out.println("Error in Vampire.submitQuery(): " + ex.getMessage()); ex.printStackTrace(); } for (;;) { String line = _reader.readLine(); if (line.indexOf("Error:") != -1) throw new IOException(line); result += line + "\n"; if ((line.indexOf("") != -1) || // result is ok. (line.indexOf("") != -1)) { // result is syntax error. System.out.println("INFO in Vampire.submitQuery(): ==================================="); System.out.println(result); result = result.replaceAll("<","<"); result = result.replaceAll(">",">"); return result; } } } /** ************************************************************* * A simple test. Works as follows: *
    *
  1. start Vampire;
  2. *
  3. make an assertion;
  4. *
  5. submit a query;
  6. *
  7. terminate Vampire.
  8. *
*/ public static void main (String[] args) throws Exception { String initialDatabase = "SUMO-v.kif"; Vampire vampire = Vampire.getNewInstance(initialDatabase); System.out.print(vampire.submitQuery("(holds instance ?X Relation)",5,2)); // System.out.print(vampire.assertFormula("(human Socrates)")); // System.out.print(vampire.assertFormula("(holds instance Adam Human)")); // System.out.print(vampire.submitQuery("(human ?X)", 1, 2)); // System.out.print(vampire.submitQuery("(holds instance ?X Human)", 5, 2)); vampire.terminate(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy