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

com.articulate.sigma.semRewrite.RHS 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.

The newest version!
package com.articulate.sigma.semRewrite;

/*
Copyright 2014-2015 IPsoft

Author: Adam Pease [email protected]

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program ; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston,
MA  02111-1307 USA 
*/

import java.text.ParseException;
import java.util.*;

import com.articulate.sigma.*;

public class RHS {

    public Formula form = null;
    public CNF cnf = null;
    boolean stop;
    
    /** ***************************************************************
     */
    public String toString() {
        
        StringBuffer sb = new StringBuffer();
        if (stop)
            sb.append("stop");
        else if (cnf != null)
            sb.append("(" + cnf + ")");
        else if (form == null)
            sb.append("!");
        else 
            sb.append("{" + form.toString() + "}");
        return sb.toString();
    }
    
    /** ***************************************************************
     */
    public RHS deepCopy() {
        
        RHS rhs = new RHS();
        if (form != null)
            rhs.form = form.deepCopy();
        if (cnf != null)
            rhs.cnf = cnf.deepCopy();
        rhs.stop = stop;
        return rhs;
    }
    
    /** ***************************************************************
     * The predicate must already have been read
     */
    public static RHS parse(Lexer lex, int startLine) {

        String errStart = "Parsing error in " + RuleSet.filename;
        String errStr;
        RHS rhs = new RHS();
        try {
            if (lex.testTok(Lexer.Stop)) {
                rhs.stop = true;
                lex.next();
                //System.out.println("Info in RHS.parse(): 1 " + lex.look());
                if (!lex.testTok(Lexer.Stop)) {
                    errStr = (errStart + ": Invalid end token '" + lex.next() + "' near line " + startLine);
                    throw new ParseException(errStr, startLine);
                }
            }
            else if (lex.testTok(Lexer.Zero)) {
                lex.next();
            }
            else if (lex.testTok(Lexer.OpenBracket)) {
                StringBuffer sb = new StringBuffer();
                String st = lex.nextUnfiltered();
                while (!st.equals("}")) {
                    st = lex.nextUnfiltered();
                    if (!st.equals("}"))
                        sb.append(st);
                }
                rhs.form = new Formula(sb.toString()); 
                //System.out.println("Info in RHS.parse(): SUMO: " + sb.toString());
            }
            else if (lex.testTok(Lexer.OpenPar)) {
                lex.next();
                rhs.cnf = CNF.parseSimple(lex);
                //lex.next();
            }
            //System.out.println("Info in RHS.parse(): 2 " + lex.look());
        }
        catch (Exception ex) {
            String message = ex.getMessage();
            System.out.println("Error in RHS.parse() " + message);
            ex.printStackTrace();
        }
        return rhs;
    }
 
    /** ***************************************************************
     * Apply variable substitutions to this set of clauses  
     * TODO: note that a replace for ?A will erroneously match ?AB
     */
    public RHS applyBindings(HashMap bindings) {
        
        RHS rhs = new RHS();
        if (cnf != null) {
            rhs.cnf = cnf.applyBindings(bindings);
            return rhs;
        }
        else {
            Iterator it = bindings.keySet().iterator();
            while (it.hasNext()) {
                String key = it.next();
                String value = bindings.get(key);
                if (form != null)
                    form.theFormula = form.theFormula.replaceAll("\\"+key,value);
                //System.out.println("INFO in RHS.applyBindings(): " + form.theFormula);
            }
            rhs.form = form;
        }
        return rhs;
    }
    
    /** *************************************************************
     * A test method
     */
    public static void main (String args[]) {
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy