ABTheoremProver.java
Go to the documentation of this file.
00001 /*
00002  * (c) copyright 2008, Technische Universitaet Graz and Technische Universitaet Wien
00003  *
00004  * This file is part of jdiagengine.
00005  *
00006  * jdiagengine is free software: you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation, either version 3 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * jdiagengine is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * You should have received a copy of the GNU General Public License
00016  * along with jdiagengine. If not, see <http://www.gnu.org/licenses/>.
00017  *
00018  * Authors: Joerg Weber, Franz Wotawa
00019  * Contact: jweber@ist.tugraz.at (preferred), or fwotawa@ist.tugraz.at
00020  *
00021  */
00022 
00023 
00037 package theoremprover;
00038 
00039 import java.lang.*;    // Java language classes 
00040 import java.awt.*;     // User Interface components
00041 import java.io.*;      // IO specific classes (File, Stream,..)
00042 import java.awt.event.*;   // Event handling
00043 import java.util.*;    // Java util
00044 
00045 import theoremprover.*;
00046 
00047 
00048 public class ABTheoremProver extends PropositionalTheoremProver
00049 {
00050 
00051   // Instance variables ...
00052 
00053   protected ArrayList assumptions;
00054 
00055   // Instance creation and initialization
00056 
00057   public ABTheoremProver ()
00058   {
00059     assumptions = new ArrayList();
00060     rules = new ArrayList();
00061     facts = new ArrayList();
00062     propositions = new ArrayList();
00063     contradiction = new Proposition();
00064     contradiction.setIdentifier("false");
00065   }
00066 
00067   // Accessing
00068 
00069   public ArrayList getAssumptions ()
00070   {
00071     return assumptions;
00072   }
00073 
00074   // Accessing and maintaining structure
00075 
00076   public boolean addAssumption(Assumption a)
00077   {
00078       //System.out.println("add assumption: " + a.toString());
00079       assumptions.add(a);
00080       return true;
00081   }
00082 
00083   // Printing...
00084 
00085   public String toString()
00086   {
00087     StringBuffer str = new StringBuffer();
00088     Iterator e = assumptions.iterator();
00089     str.append("Assumptions:\r\n");
00090     while (e.hasNext()) {
00091       str.append((e.next()).toString());
00092       str.append("\r\n");
00093     }
00094     e = facts.iterator();
00095     str.append("Facts:\r\n");
00096     while (e.hasNext()) {
00097       str.append((e.next()).toString());
00098       str.append("\r\n");
00099     }
00100     e = rules.iterator();
00101     str.append("Rules:\r\n");
00102     while (e.hasNext()) {
00103       str.append((e.next()).toString());
00104       str.append("\r\n");
00105     }
00106     return str.toString();
00107   }
00108 
00109   // Public accessing
00110 
00111   // Add a new rule or fact to the theorem prover and check consistency
00112   // It is assumed that all propositions used are added to my
00113   // list of propositions before calling the add method.
00114     public void add(ArrayList a, Proposition p) {
00115         PropositionalRule newRule = new PropositionalRule();
00116         int i = a.size();
00117         
00118         if(i > 0) {
00119             addRule(newRule);
00120         } else {
00121             addFact(newRule);
00122         }
00123         p.addSuccedence(newRule);
00124         newRule.succedence(p);
00125         Iterator e = a.iterator();
00126         while (e.hasNext()) {
00127             Assumption ap = (Assumption)e.next();
00128             ap.addAntecedence(newRule);
00129             newRule.addToAntecedence(ap);
00130             if (ap.getLabel() == true) {
00131                 i = i - 1;
00132             }
00133         }
00134         newRule.counter(i);
00135         if (i == 0) {
00136             if (p.getLabel() != true) {
00137                 p.setLabel(true);
00138                 p.activeRule(newRule);
00139                 // Propagate truth value
00140                 p.propagateTrue();
00141             }
00142         }
00143     }
00144 
00145     // Sets the assumption a to the passed value and propagates this value.
00146     public void setAssumption(Assumption a, boolean value) {
00147 
00148         if (value) {
00149             if (a.getLabel() != true) {
00150                 a.setLabel(true);
00151                 a.propagateTrue();
00152             } 
00153         } else {
00154             
00155             if(a.getLabel() != false) {
00156                 ArrayList v = new ArrayList();
00157                 v = a.propagateFalse(v); 
00158                 Iterator ve = v.iterator();                                
00159                 while (ve.hasNext()) {
00160                     Proposition p = (Proposition)ve.next();
00161                     p.correctLabels();
00162                 }
00163             }
00164 
00165         }
00166 
00167     }
00168 
00169   // Checks consistency assuming that all assumptions are true.
00170   public boolean checkConsistency ()
00171   {
00172     Iterator e = assumptions.iterator();
00173     while (e.hasNext()) {
00174       Assumption a = (Assumption)e.next();
00175       setAssumption(a, true);
00176     }
00177     return isConsistent();
00178   }
00179 
00180     /* 
00181      * Checks consistency; assumptions in posAssumptions are set to true,
00182      * ass. in negAssumptions to false. 
00183      * This method assumes that these two lists contain all used assumptions; if
00184      * there is any assumption x which is in neither list, then the label of x remains unchanged.
00185      */ 
00186     public boolean checkConsistency(ArrayList posAssumptions, ArrayList negAssumptions) {
00187 
00188         Iterator e = posAssumptions.iterator();
00189         while (e.hasNext()) {
00190             Assumption a = (Assumption)e.next();
00191             setAssumption(a, true);
00192         }
00193 
00194         e = negAssumptions.iterator();
00195         while (e.hasNext()) {
00196             Assumption a = (Assumption)e.next();
00197             setAssumption(a, false);
00198         }
00199         
00200         return isConsistent();
00201     }
00202 
00203     /*
00204      * Checks consistency. 
00205      * posAssPrefixes contains strings; each assumption which starts with
00206      * one of the prefixes is assumed true; the other assumptions are assumed
00207      * false.
00208      * E.g., posAssPrefixes could contain a string "NAB".
00209      */
00210     public boolean checkConsistency(ArrayList posAssPrefixes) {
00211 
00212         Iterator itAss = assumptions.iterator();
00213         while (itAss.hasNext()) {
00214             Assumption a = (Assumption)itAss.next();
00215             String aStr = (String)a.getIdentifier();
00216 
00217             Iterator itPre = posAssPrefixes.iterator();
00218             boolean positive = false;
00219 
00220             while(itPre.hasNext()) {
00221                 String prefix = (String)itPre.next();
00222                 if (aStr.startsWith(prefix)) {
00223                     positive = true;
00224                     break;
00225                 }
00226             }
00227 
00228             if (positive) setAssumption(a, true);
00229             else setAssumption(a, false);
00230         }
00231 
00232         return isConsistent();
00233     }
00234 
00235     /*
00236      * Returns a conflict set as an ArrayList of Assumption.
00237      */
00238     public ArrayList getConflictSet() {
00239         return contradiction.collectAssumptions();
00240     }
00241 }


tug_ist_diagnosis_engine
Author(s): Safdar Zaman, Gerald Steinbauer
autogenerated on Mon Jan 6 2014 11:51:16