DEGraph.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 
00024 package dfengine;
00025 
00026 import java.util.*;
00027 
00028 import utils.*;
00029 
00030 
00031 public class DEGraph extends DoubleLinkedDAG {
00032 
00033     ArrayList rootNodes = new ArrayList();
00034 
00035     TreeSet unexpandedAlphaNodes = new TreeSet();
00036 
00037 
00038     protected String PRINT_OFFSET = "    ";
00039     
00040 
00041     public Object clone() {
00042         throw new UnsupportedOperationException("class DEGraph does not support clone()");
00043     }
00044     
00045     public String toStringShort() {
00046         
00047         StringBuffer result = new StringBuffer();
00048 
00049         Iterator itRootNodes = rootNodes.iterator();
00050         while (itRootNodes.hasNext()) {
00051             DENode rootNode = (DENode)itRootNodes.next();
00052             System.out.println("DEGraph.toStringShort(): root node: " + rootNode.toStringShort()
00053                 + ";  num children: " + rootNode.getChildren().size());
00054             printSubgraph(result, rootNode, 0);
00055         }
00056 
00057         return result.toString();
00058     }
00059 
00060     // util method for toStringShort()
00061     protected void printSubgraph(StringBuffer buf, DENode n, int depth) {
00062         
00063         for (int i = 0; i < depth; ++i) {
00064             buf.append(PRINT_OFFSET);
00065         }
00066 
00067         buf.append(n.toStringShort());
00068         buf.append('\n');
00069 
00070         Iterator itChildren = n.getChildrenIterator();
00071         while (itChildren.hasNext()) {
00072             DENode child = (DENode)itChildren.next();
00073             printSubgraph(buf, child, depth + 1);
00074         }
00075     }
00076 
00077     public void addRootNode(DENode newRootNode) {
00078 
00079         assert(invariant());
00080         
00081         addNode(newRootNode);
00082         rootNodes.add(newRootNode);
00083 
00084         assert(invariant());
00085     }
00086 
00087     public void addChildNode(DENode n, DENode child) {
00088         assert(invariant());  // inv
00089         assert((n.getID() >= 0) && hasNode(n.getID()));  // precond.
00090 
00091         addNode(child);
00092         addEdge(n, child);
00093 
00094         assert(invariant());
00095     }
00096 
00097     public TreeSet getUnexpandedAlphaNodes() {
00098         return unexpandedAlphaNodes;
00099     }
00100 
00101     /*
00102      * If a node with a mode assignment equal to ma already exists: return this node.
00103      *
00104      * Return null otherwise. 
00105      * Children of exclRootNode are NOT considered in the search!
00106      */
00107     public DENode alreadyExists(ModeAssignment ma, DENode exclRootNode) {
00108         
00109         Iterator itRootNodes = rootNodes.iterator();
00110         while (itRootNodes.hasNext()) {
00111             DENode rootN = (DENode)itRootNodes.next();
00112             if (rootN != exclRootNode) {
00113                 SearchMAVisitor visitor = new SearchMAVisitor(ma);
00114                 visitChildrenPreOrder(rootN, visitor, true);
00115                 
00116                 if (visitor.foundNode != null) {
00117                     return visitor.foundNode;
00118                 }
00119             }
00120         }
00121         
00122         return null;
00123     }
00124 
00125     /*
00126      * Returns true iff all the MAs in the DEG are unique.
00127      */
00128     protected boolean checkUniqueModesHypothesis() {
00129         
00130         for (int i = nodes.size() - 1; i >= 0; --i) {
00131             DENode n = (DENode)nodes.get(i);
00132             
00133             for (int j = i - 1; j >= 0; --j) {
00134                 DENode n1 = (DENode)nodes.get(j);
00135                 if (n.getModeAssignment().equals(n1.getModeAssignment())) {
00136                     
00137                     System.out.println("WARNING: mode assignments not unique: ["
00138                                        + n.getModeAssignment().toStringShort()
00139                                        + "]  [" + n1.getModeAssignment().toStringShort()
00140                                        + "]");
00141                     return false;
00142                 }
00143             }
00144         }
00145 
00146         return true;
00147     }
00148 
00149     protected boolean invariant() {
00150 
00151         // for all nodes n in unexpandedAlphaNodes: n.hasChildren() must be false,
00152         // and n.expanded(alpha) must also be false.
00153 
00154         Iterator unIt = unexpandedAlphaNodes.iterator();
00155         while (unIt.hasNext()) {
00156             DENode n = (DENode)unIt.next();
00157             if (!n.expanded(DENode.TYPE_BETA) && n.hasChildren()) return false;
00158 
00159             if (n.expanded(DENode.TYPE_ALPHA)) return false;
00160         }
00161 
00162         // check invariant of all nodes
00163 
00164         Iterator itNodes = iterator();
00165         while (itNodes.hasNext()) {
00166             DENode n = (DENode)itNodes.next();
00167             if (!n.invariant()) return false;
00168         }
00169 
00170         // ensure that mode assignments are unique in the graph
00171         if (!checkUniqueModesHypothesis()) return false;
00172 
00173         return true;
00174     }
00175 
00176     /*
00177      * Marks n and all of its descendants as inconsistent
00178      */
00179     public void propagateInconsToDesc(DENode n) {
00180         assert(invariant());
00181         
00182         MarkInconsistentVisitor visitor = new MarkInconsistentVisitor();
00183         visitChildrenPreOrder(n, visitor, true);
00184         assert((n.consistent() == DENode.STATUS_FALSE) && n.descInconsistent());
00185 
00186         assert(invariant());
00187     }
00188 
00189 
00190 }
00191 
00192 /*
00193  * Searches a mode assignment. If found: returned in "foundNode".
00194  */
00195 class SearchMAVisitor extends DoubleLinkedDAGVisitor {
00196 
00197     ModeAssignment ma;
00198 
00199     DENode foundNode = null;
00200 
00201     public SearchMAVisitor(ModeAssignment ma) {
00202         this.ma = ma;
00203     }
00204 
00205     public boolean wantMoreNodes() {
00206         return (foundNode == null);
00207     }
00208 
00209     public void visit(DoubleLinkedDAGNode n) {
00210 
00211         DENode den = (DENode)n;
00212         if (ma.equals(den.getModeAssignment())) {
00213             foundNode = den;
00214         }
00215     }
00216 
00217 }
00218 
00219 /*
00220  * Marks nodes as "inconsistent".
00221  */
00222 class MarkInconsistentVisitor extends DoubleLinkedDAGVisitor {
00223 
00224     public boolean wantMoreNodes() {
00225         return true;
00226     }
00227 
00228     public void visit(DoubleLinkedDAGNode n) {
00229         DENode den = (DENode)n;
00230         den.setDescInconsistent();
00231         den.setConsistent(DENode.STATUS_FALSE);
00232     }
00233 }


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