00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 package dfengine;
00025
00026 import java.util.*;
00027
00028 import utils.*;
00029 import theoremprover.*;
00030 import hittingsetalg.*;
00031
00032
00033 public class DiagnosisProblem {
00034
00035
00036 protected TreeMap components = new TreeMap();
00037
00038 protected FailureDepGraph fdg;
00039
00040
00041 protected LSentence sd;
00042
00043
00044 protected LSentence sdd;
00045
00046
00047 protected LSentence obs;
00048
00049 protected String assAB;
00050 protected String assNAB;
00051 protected String assIF;
00052 protected String assDF;
00053
00054
00055 public DiagnosisProblem(boolean useProb, String assAB, String assNAB, String assIF, String assDF) {
00056 fdg = new FailureDepGraph(useProb);
00057 this.assAB = assAB;
00058 this.assNAB = assNAB;
00059 this.assIF = assIF;
00060 this.assDF = assDF;
00061 }
00062
00063 public void clearFDGEdges() {
00064
00065
00066
00067 fdg = new FailureDepGraph(fdg.useProbabilities());
00068 Iterator itComp = components.values().iterator();
00069 while (itComp.hasNext()) {
00070 Component comp = (Component)itComp.next();
00071 fdg.addNode(comp);
00072 }
00073 }
00074
00078 public void addComponent(String compName) {
00079
00080 Component comp = new Component(compName, components.size());
00081 components.put(compName, comp);
00082 FailureDepNode fdNode = fdg.addNode(comp);
00083 }
00084
00085 public void addComponent(String compName, double prob_if) {
00086 Component comp = new Component(compName, components.size(), prob_if);
00087 components.put(compName, comp);
00088 FailureDepNode fdNode = fdg.addNode(comp);
00089 }
00090
00094 public void addFailureDep(String compFrom, String compTo) {
00095
00096 Object o1 = components.get(compFrom);
00097 Object o2 = components.get(compTo);
00098
00099
00100 assert((o1 != null) && (o2 != null));
00101
00102 Component c1 = (Component)o1;
00103 Component c2 = (Component)o2;
00104
00105 fdg.addEdge(c1, c2);
00106 }
00107
00108 public void addFailureDep(String compFrom, String compTo, double prob_df) {
00109
00110 Object o1 = components.get(compFrom);
00111 Object o2 = components.get(compTo);
00112
00113
00114 assert((o1 != null) && (o2 != null));
00115
00116 Component c1 = (Component)o1;
00117 Component c2 = (Component)o2;
00118
00119 fdg.addEdge(c1, c2, prob_df);
00120 }
00121
00122 public boolean hasComponent(String compName) {
00123 return (components.containsKey(compName));
00124 }
00125
00126 public void setSD(LSentence sd) {
00127 this.sd = sd;
00128 }
00129
00130 public void setOBS(LSentence obs) {
00131 this.obs = obs;
00132 }
00133
00134 public void setSDD(LSentence sdd) {
00135 this.sdd = sdd;
00136 }
00137
00138 public LSentence getSD() {
00139 if (sd == null) return new LSentence();
00140 else return sd;
00141 }
00142
00143 public LSentence getOBS() {
00144 if (obs == null) return new LSentence();
00145 else return obs;
00146 }
00147
00148 public LSentence getSDD() {
00149 if (sdd == null) return new LSentence();
00150 else return sdd;
00151 }
00152
00153 public FailureDepGraph getFDG() {
00154 return fdg;
00155 }
00156
00157 protected TreeMap getComponents() {
00158 return components;
00159 }
00160
00161 public String getAssAB() {
00162 return assAB;
00163 }
00164
00165 public String getAssNAB() {
00166 return assNAB;
00167 }
00168
00169 public String getAssIF() {
00170 return assIF;
00171 }
00172
00173 public String getAssDF() {
00174 return assDF;
00175 }
00176
00185 public ArrayList computeMinHittingSets(int maxHSSize, int maxNumMinHS,
00186 ArrayList resultingConflictSets)
00187 throws IllegalAssumption, ParseError {
00188
00189
00190
00191 LSentence sdUnionObs = new LSentence();
00192 sdUnionObs.addRules(sd);
00193 sdUnionObs.addRules(obs);
00194
00195 ABTheoremProver theoremProver = new ABTheoremProver();
00196 theoremProver = sdUnionObs.asABPropositionalSentence(theoremProver);
00197 if (theoremProver == null) {
00198 throw new ParseError("[unknown]");
00199 }
00200
00201
00202
00203 MinHittingSetsFM hs = new MinHittingSetsFM(false, theoremProver, assAB, assNAB);
00204 hs.compute(maxHSSize, maxNumMinHS);
00205 ArrayList minHS = hs.getMinHS();
00206
00207 assert(resultingConflictSets.size() == 0);
00208 ArrayList cs = hs.getConflictsAsAss();
00209 ArrayList splittedCS = splitAssumptions(cs);
00210 resultingConflictSets.addAll(splittedCS);
00211
00212 return convertAssListToCompList(minHS);
00213
00214 }
00215
00222 protected ArrayList convertAssListToCompList(ArrayList assList) throws ParseError {
00223
00224 ArrayList result = new ArrayList(assList.size());
00225 Iterator itAL = assList.iterator();
00226 while (itAL.hasNext()) {
00227 ArrayList expl = (ArrayList)itAL.next();
00228 ArrayList candidate = new ArrayList(expl.size());
00229 Iterator itExpl = expl.iterator();
00230
00231 while (itExpl.hasNext()) {
00232 Assumption a = (Assumption)itExpl.next();
00233 String ass = (String)a.getIdentifier();
00234 if (!ass.matches(assNAB + "\\(" + "[a-zA-Z_0-9]+" + "\\)"))
00235 throw new ParseError("[unknown]");
00236
00237 String compName = ass.substring(assNAB.length() + 1,
00238 ass.length() - 1);
00239 Object compObj = components.get(compName);
00240 if (compObj == null) throw new ParseError("[unknown]");
00241 Component comp = (Component)compObj;
00242
00243 candidate.add(comp);
00244 }
00245
00246 result.add(candidate);
00247 }
00248
00249 return result;
00250
00251 }
00252
00253
00254
00255
00256
00257
00258
00259
00260 public SplittedAssumption splitAssumption(Assumption ass) {
00261 String aStr = (String)ass.getIdentifier();
00262 String[] sl = aStr.split("[()]");
00263 assert(sl.length == 2);
00264
00265 String[] sl2 = sl[1].split(",");
00266 if (sl2.length == 1) return new SplittedAssumption(sl[0].trim(), null, sl[1].trim());
00267 else return new SplittedAssumption(sl[0].trim(), sl2[0].trim(), sl2[1].trim());
00268 }
00269
00270
00271
00272
00273
00274 protected ArrayList splitAssumptions(ArrayList assList) {
00275 ArrayList result = new ArrayList(assList.size());
00276
00277 Iterator itAL = assList.iterator();
00278 while (itAL.hasNext()) {
00279 ArrayList al = (ArrayList)itAL.next();
00280 ArrayList resItem = new ArrayList(al.size());
00281
00282 Iterator it = al.iterator();
00283 while (it.hasNext()) {
00284 Assumption a = (Assumption)it.next();
00285 SplittedAssumption splitA = splitAssumption(a);
00286 resItem.add(splitA);
00287 }
00288
00289 result.add(resItem);
00290 }
00291
00292 return result;
00293 }
00294
00302 public ArrayList computeDEs(ArrayList minHS, boolean computeBetaDE, int maxDFChainSize,
00303 ArrayList initialConflictSets)
00304 throws ParseError {
00305
00306 DiagnosisEnvironments diagEnvs = new DiagnosisEnvironments(this);
00307 ArrayList result = diagEnvs.computeDEs(minHS, computeBetaDE,
00308 maxDFChainSize, initialConflictSets);
00309
00310 return result;
00311 }
00312
00313
00314
00315
00316
00317 public RepairCandidates computeRepairCandidates(ArrayList minHS,
00318 boolean computeBetaDE, int maxDFChainSize,
00319 boolean discardOrderPerms,
00320 ArrayList initialConflictSets)
00321 throws ParseError {
00322
00323 DiagnosisEnvironments diagEnvs = new DiagnosisEnvironments(this);
00324 RepairCandidates result
00325 = diagEnvs.computeRepairCandidates(minHS, computeBetaDE,
00326 maxDFChainSize, discardOrderPerms,
00327 initialConflictSets);
00328
00329 return result;
00330 }
00331
00342 public ArrayList computeDERanking(ArrayList deList, boolean normalize) {
00343 DiagnosisEnvironments diagEnvs = new DiagnosisEnvironments(this);
00344
00345
00346
00347 double sum = 0.0;
00348
00349 ArrayList result = new ArrayList(deList.size());
00350 Iterator itDE = deList.iterator();
00351 while (itDE.hasNext()) {
00352 ModeAssignment de = (ModeAssignment)itDE.next();
00353 double prob = diagEnvs.computeProb(de);
00354 sum += prob;
00355
00356 ObjectPair op = new ObjectPair(de, new Double(prob));
00357 result.add(op);
00358 }
00359
00360
00361
00362 if (normalize && (sum > 0.0)) {
00363 Iterator itResult = result.iterator();
00364 while (itResult.hasNext()) {
00365 ObjectPair op = (ObjectPair)itResult.next();
00366 Double prob = (Double)op.last;
00367 double norm_value = prob.doubleValue() / sum;
00368 Double newProb = new Double(norm_value);
00369 op.last = newProb;
00370 }
00371 }
00372
00373
00374
00375 Comparator comp = new Comparator() {
00376 public int compare(Object o1, Object o2) {
00377 ObjectPair op1 = (ObjectPair)o1;
00378 ObjectPair op2 = (ObjectPair)o2;
00379
00380 double d1 = ((Double)op1.last).doubleValue();
00381 double d2 = ((Double)op2.last).doubleValue();
00382 if (d1 < d2) return +1;
00383 else if (d1 > d2) return -1;
00384 else return 0;
00385 }
00386
00387 public boolean equals(Object o1, Object o2) {
00388 return false;
00389 }
00390 };
00391
00392
00393 Collections.sort(result, comp);
00394
00395 return result;
00396
00397 }
00398
00399 }