Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 package ATPInterface;
00039
00040 import java.util.*;
00041
00042 import theoremprover.IllegalAssumption;
00043 import dfengine.DiagnosisProblem;
00044 import dfengine.FailureDepGraph;
00045 import dfengine.ModeAssignment;
00046 import dfengine.Component;
00047 import dfengine.RepairCandidates;
00048 import dfengine.RepairCandidate;
00049
00050 public class LogicalDB implements LogicalDBInterface {
00051
00052 protected boolean changed;
00053
00054 protected boolean lastUseFaultModes;
00055
00056 protected boolean lastMergeDEs;
00057
00058 protected int lastMaxExplSize = -1;
00059
00060 protected int lastMaxNumExpl = -1;
00061
00062 protected boolean lastComputeBetaDE;
00063
00064 protected int lastMaxDFChainSize = -1;
00065
00066
00067 protected boolean consistent;
00068
00069
00070 protected ArrayList explanations;
00071
00072
00073 protected ArrayList diagEnvs;
00074
00075 protected TreeMap subDBs;
00076
00077
00078 protected LogicalSubDB totalDB;
00079
00080 protected DiagnosisProblem diagProblem;
00081
00082
00083
00084
00085
00086 public LogicalDB() {
00087 changed = false;
00088 consistent = true;
00089 explanations = new ArrayList();
00090 diagEnvs = new ArrayList();
00091
00092 diagProblem = new DiagnosisProblem(false, ATPConstants.DEF_AB_ASSUMPTION, ATPConstants.DEF_NAB_ASSUMPTION,
00093 ATPConstants.DEF_IF_ASSUMPTION, ATPConstants.DEF_DF_ASSUMPTION);
00094
00095 subDBs = new TreeMap();
00096 subDBs.put(ATPConstants.SUBDB_SD, new LogicalSubDB(ATPConstants.SUBDB_SD));
00097 subDBs.put(ATPConstants.SUBDB_OBS, new LogicalSubDB(ATPConstants.SUBDB_OBS));
00098 subDBs.put(ATPConstants.SUBDB_SDD, new LogicalSubDB(ATPConstants.SUBDB_SDD));
00099 }
00100
00101 public int addRules(String subDB, boolean replace, ArrayList newRules)
00102 throws LogicParseException {
00103
00104 Object o = subDBs.get(subDB);
00105 assert(o != null);
00106 LogicalSubDB lsubDB = (LogicalSubDB)o;
00107
00108 if (replace) lsubDB.clear();
00109 lsubDB.addRules(newRules);
00110 changed = true;
00111
00112 return lsubDB.getNumRules();
00113 }
00114
00115 public int addFDGEdges(ArrayList edgeStrings, boolean replace) throws LogicParseException {
00116
00117 if (replace) diagProblem.clearFDGEdges();
00118
00119 for (int i = 0; i < edgeStrings.size(); ++i) {
00120
00121 String edgeStr = (String)edgeStrings.get(i);
00122 int pos = edgeStr.indexOf(ATPConstants.STR_FDG_EDGE);
00123
00124 if (pos < 0) {
00125 if (edgeStr.length() > 0) {
00126 String comp1Str = edgeStr;
00127 if (!diagProblem.hasComponent(comp1Str)) diagProblem.addComponent(comp1Str);
00128 }
00129 } else {
00130 String comp1Str = edgeStr.substring(0, pos).trim();
00131 String comp2Str = edgeStr.substring(pos + ATPConstants.STR_FDG_EDGE.length()).trim();
00132
00133 if ((comp1Str.length() == 0) || (comp2Str.length() == 0)) {
00134 throw new LogicParseException(i + 1);
00135 }
00136
00137 if (!diagProblem.hasComponent(comp1Str)) diagProblem.addComponent(comp1Str);
00138 if (!diagProblem.hasComponent(comp2Str)) diagProblem.addComponent(comp2Str);
00139 diagProblem.addFailureDep(comp1Str, comp2Str);
00140 }
00141 }
00142
00143 changed = true;
00144 return diagProblem.getFDG().getNumEdges();
00145 }
00146
00147 public int getNumSubDBs() {
00148 return subDBs.size();
00149 }
00150
00151
00152
00153
00154 public boolean checkConsistency(boolean useFaultModes) throws LogicParseException {
00155
00156 if (!changed && (useFaultModes == lastUseFaultModes)) return consistent;
00157 else {
00158
00159
00160
00161 totalDB = new LogicalSubDB("total");
00162 totalDB.append((LogicalSubDB)subDBs.get(ATPConstants.SUBDB_SD));
00163 totalDB.append((LogicalSubDB)subDBs.get(ATPConstants.SUBDB_OBS));
00164
00165
00166
00167 System.out.println("check consistency");
00168
00169 consistent = totalDB.checkConsistency(useFaultModes, ATPConstants.DEF_AB_ASSUMPTION,
00170 ATPConstants.DEF_NAB_ASSUMPTION);
00171
00172 if (consistent) {
00173 explanations = new ArrayList();
00174 diagEnvs = new ArrayList();
00175 } else {
00176 explanations = null;
00177 diagEnvs = null;
00178 }
00179
00180 changed = false;
00181 lastUseFaultModes = useFaultModes;
00182
00183 return consistent;
00184 }
00185 }
00186
00187 public void performConsistencyChecks(ArrayList queries, boolean useFaultModes, BitSet result)
00188 throws LogicParseException{
00189
00190 LogicalSubDB queryDB = new LogicalSubDB("QUERY");
00191 LogicalSubDB sd_obs = new LogicalSubDB("SD_OBS");
00192 sd_obs.append((LogicalSubDB)subDBs.get(ATPConstants.SUBDB_SD));
00193 sd_obs.append((LogicalSubDB)subDBs.get(ATPConstants.SUBDB_OBS));
00194
00195 System.out.println("perform inferences");
00196
00197 int index = 0;
00198 Iterator itQuery = queries.iterator();
00199 while (itQuery.hasNext())
00200 {
00201
00202
00203 String queryStr = (String)itQuery.next();
00204 queryDB.clear();
00205 ArrayList rules = new ArrayList();
00206 rules.add(queryStr);
00207 queryDB.addRules(rules);
00208
00209
00210
00211 LogicalSubDB totalDB = new LogicalSubDB("total");
00212 totalDB.append(sd_obs);
00213 totalDB.append(queryDB);
00214
00215
00216 consistent = totalDB.checkConsistency(useFaultModes, ATPConstants.DEF_AB_ASSUMPTION,
00217 ATPConstants.DEF_NAB_ASSUMPTION);
00218 assert(index < queries.size());
00219 result.set(index, consistent);
00220 assert(result.length() <= index + 1);
00221 ++index;
00222
00223 }
00224
00225 }
00226
00227 public ArrayList computeMinDiag(int maxExplSize, int maxNumExpl,
00228 boolean useFaultModes, boolean verboseOutput)
00229 throws LogicParseException, IllegalAssumption {
00230
00231 if (changed || (lastUseFaultModes != useFaultModes)) {
00232 checkConsistency(useFaultModes);
00233 }
00234
00235 if ((explanations == null) || (maxExplSize != lastMaxExplSize) || (maxNumExpl != lastMaxNumExpl)) {
00236 System.out.println("Compute explanations");
00237 explanations = totalDB.computeMinDiag(maxExplSize, maxNumExpl, useFaultModes,
00238 ATPConstants.DEF_AB_ASSUMPTION,
00239 ATPConstants.DEF_NAB_ASSUMPTION,
00240 verboseOutput);
00241 }
00242
00243 lastMaxExplSize = maxExplSize;
00244 lastMaxNumExpl = maxNumExpl;
00245
00246 return explanations;
00247 }
00248
00249 public ArrayList computeDEs(int maxExplSize, int maxNumExpl,
00250 boolean computeBetaDE, int maxDFChainSize, boolean mergeDEs,
00251 boolean discardOrderPerms, ArrayList minDiags)
00252 throws LogicParseException, IllegalAssumption {
00253
00254 if (changed || !lastUseFaultModes) {
00255 checkConsistency(true);
00256 }
00257
00258 System.out.println("Compute diagnosis environments");
00259
00260 LogicalSubDB subDB = (LogicalSubDB)subDBs.get(ATPConstants.SUBDB_SD);
00261 diagProblem.setSD(subDB.getRulesAsLSentence());
00262 subDB = (LogicalSubDB)subDBs.get(ATPConstants.SUBDB_OBS);
00263 diagProblem.setOBS(subDB.getRulesAsLSentence());
00264 subDB = (LogicalSubDB)subDBs.get(ATPConstants.SUBDB_SDD);
00265 diagProblem.setSDD(subDB.getRulesAsLSentence());
00266
00267 ArrayList minHS;
00268 ArrayList conflictSets = new ArrayList();
00269 try {
00270
00271
00272 minHS = diagProblem.computeMinHittingSets(maxExplSize, maxNumExpl, conflictSets);
00273
00274 if (!mergeDEs) {
00275
00276
00277
00278 ArrayList minDEs = diagProblem.computeDEs(minHS, computeBetaDE,
00279 maxDFChainSize, conflictSets);
00280
00281
00282 diagEnvs = new ArrayList(minDEs.size());
00283 Iterator itDE = minDEs.iterator();
00284 while (itDE.hasNext()) {
00285 ModeAssignment de = (ModeAssignment)itDE.next();
00286 diagEnvs.add(de.toStringShort());
00287 }
00288 } else {
00289
00290
00291
00292 RepairCandidates rcs
00293 = diagProblem.computeRepairCandidates(minHS, computeBetaDE,
00294 maxDFChainSize, discardOrderPerms, conflictSets);
00295
00296
00297 diagEnvs = new ArrayList(rcs.size());
00298 Iterator itRC = rcs.iterator();
00299 while (itRC.hasNext()) {
00300 RepairCandidate rc = (RepairCandidate)itRC.next();
00301 diagEnvs.add(rc.toString());
00302 }
00303
00304 }
00305
00306
00307
00308 Iterator itMinHS = minHS.iterator();
00309 while (itMinHS.hasNext())
00310 {
00311 String diagStr = "";
00312
00313 ArrayList diagnosis = (ArrayList)itMinHS.next();
00314 Iterator itComps = diagnosis.iterator();
00315 while (itComps.hasNext())
00316 {
00317 Component c = (Component)itComps.next();
00318 if (diagStr.length() > 0) diagStr = diagStr + "; ";
00319 diagStr = diagStr + ATPConstants.DEF_NAB_ASSUMPTION + "(" + c.getName() + ")";
00320 }
00321
00322 minDiags.add(diagStr);
00323 }
00324
00325 } catch (theoremprover.ParseError exc) {
00326 throw new LogicParseException();
00327 }
00328
00329
00330
00331 lastMaxExplSize = maxExplSize;
00332 lastMaxNumExpl = maxNumExpl;
00333 lastComputeBetaDE = computeBetaDE;
00334 lastMaxDFChainSize = maxDFChainSize;
00335 lastMergeDEs = mergeDEs;
00336
00337 return diagEnvs;
00338
00339 }
00340
00341 public int getTotalNumRules() {
00342
00343 int result = 0;
00344
00345 Iterator itSubDBs = subDBs.values().iterator();
00346
00347 while(itSubDBs.hasNext()) {
00348 LogicalSubDB sub = (LogicalSubDB)itSubDBs.next();
00349 result += sub.getNumRules();
00350 }
00351
00352 return result;
00353 }
00354
00355 public int getSubDBNumRules(String name) {
00356 Object o = subDBs.get(name);
00357 assert(o != null);
00358 LogicalSubDB subDB = (LogicalSubDB)o;
00359 return subDB.getNumRules();
00360 }
00361
00362 public ArrayList createSubDBStats() {
00363 ArrayList result = new ArrayList();
00364
00365 Iterator itSubDBs = subDBs.values().iterator();
00366
00367 while(itSubDBs.hasNext()) {
00368 LogicalSubDB sub = (LogicalSubDB)itSubDBs.next();
00369 SubDBStat stat = new SubDBStat();
00370 stat.name = sub.getName();
00371 stat.numRules = sub.getNumRules();
00372 result.add(stat);
00373 }
00374
00375 return result;
00376 }
00377
00378 public ArrayList getSubDBRules(String name) {
00379 Object o = subDBs.get(name);
00380 assert(o != null);
00381 LogicalSubDB subDB = (LogicalSubDB)o;
00382 return subDB.getRulesAsStrings();
00383 }
00384
00385 public FDGStat createFDGStats() {
00386 FailureDepGraph fdg = diagProblem.getFDG();
00387 FDGStat result = new FDGStat();
00388 result.numNodes = fdg.getNumNodes();
00389 result.numEdges = fdg.getNumEdges();
00390
00391 return result;
00392 }
00393 }