RepairOrderDAG.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 public class RepairOrderDAG extends DoubleLinkedDAG {
00031 
00032     /*
00033      * Map from Component to RepairOrderNode.
00034      */
00035     protected TreeMap components = new TreeMap();
00036 
00037     protected GraphMatrix transClosure = null;
00038 
00039 
00040     public RepairOrderDAG(ModeAssignment ma) {
00041         createFromModeAssignment(ma);
00042 
00043         assert(invariant());
00044     }
00045 
00046     /*
00047      * Util method, internally used.
00048      */
00049     protected void addNode(Component comp) {
00050         assert(invariant());
00051 
00052         RepairOrderDAGNode newNode = new RepairOrderDAGNode(comp);
00053         super.addNode(newNode);
00054         components.put(comp, newNode);
00055 
00056         assert(invariant());
00057     }
00058 
00059     public RepairOrderDAGNode getNode(Component comp) {
00060         Object o = components.get(comp);
00061         if (o == null) return null;
00062         else return (RepairOrderDAGNode)o;
00063     }
00064 
00065     public void addEdge(Component from, Component to) {
00066 
00067         assert(invariant());
00068 
00069         RepairOrderDAGNode fromNode = getNode(from);
00070         RepairOrderDAGNode toNode = getNode(to);
00071         
00072         addEdge(fromNode, toNode);
00073         updateTransClosureForNewEdge(getTransitiveClosure(), fromNode, toNode, false);
00074         
00075         // postcondition: assert that updated transClosure conforms to DAG
00076         assert(hasEdge(from, to));
00077         assert(correctTransitiveClosure(getTransitiveClosure(), false));
00078         // invariant
00079         assert(invariant());
00080     }
00081 
00082     public boolean hasEdge(Component from, Component to) {
00083         return hasEdge(getNode(from), getNode(to));
00084     }
00085 
00086     protected void createFromModeAssignment(ModeAssignment ma) {
00087         
00088         System.out.println("createFromModeAssignment(): ma = " + ma.toStringShort());
00089 
00090         // create nodes
00091 
00092         Iterator itModes = ma.iterator();
00093         while (itModes.hasNext()) {
00094             Mode mode = (Mode)itModes.next();
00095             addNode(mode.component);           
00096         }
00097 
00098         // create edges
00099 
00100         itModes = ma.iterator();
00101         while (itModes.hasNext()) {
00102             Mode mode = (Mode)itModes.next();
00103             if (mode.type == Mode.MODE_DF) {
00104                 RepairOrderDAGNode node = getNode(mode.component);
00105                 RepairOrderDAGNode parentNode = getNode(mode.parent);
00106                 addEdge(parentNode, node);
00107             }
00108         }
00109 
00110         transClosure = null;
00111 
00112         System.out.println("createFromModeAssignment() results in: " + this);
00113     }
00114 
00115     protected boolean invariant() {
00116 
00117         // is transitive closure correct?
00118         if ((transClosure != null) && !correctTransitiveClosure(transClosure, false)) {
00119             System.err.println("ERROR: invariant of RepairOrderDAG violated: incorrect transitive closure!");
00120             return false;
00121         }
00122 
00123         // check if invariants of all nodes hold
00124         Iterator itNodes = iterator();
00125         while (itNodes.hasNext()) {
00126             RepairOrderDAGNode n = (RepairOrderDAGNode)itNodes.next();
00127             if (!n.invariant()) {
00128                 System.err.println("ERROR: invariant of RepairOrderDAG violated: invariant of a node does not hold!");
00129                 return false;
00130             }
00131         }
00132 
00133         // check if there is more than one node for a specific component (must not happen)
00134         if (!uniqueComponents()) {
00135             System.err.println("ERROR: invariant of RepairOrderDAG violated: components are not unique!");
00136             return false;
00137         }
00138 
00139         return true;
00140     }
00141 
00142     public GraphMatrix getTransitiveClosure() {
00143         if (transClosure == null) transClosure = computeTransitiveClosure(false);
00144         return transClosure;
00145     }
00146 
00147     public boolean equalComponents(Set comps) {
00148         
00149         return (components.keySet().equals(comps));
00150 
00151     }
00152 
00153     // Returns true iff there is at most one node for each component. For test purposes.
00154     protected boolean uniqueComponents() {
00155         for (int i = 0; i < nodes.size(); ++i) {
00156             for (int j = i + 1; j < nodes.size(); ++j) {
00157                 if (nodes.get(i).equals(nodes.get(j))) return false;
00158             }
00159         }
00160         return true;
00161     }
00162 
00163     public String toString() {
00164         if (!uniqueComponents()) {
00165             System.err.println("ERROR: invariant of RepairOrderDAG violated: components are not unique!");
00166         }
00167 
00168         StringBuffer result = new StringBuffer();
00169         ToStringVisitor visitor = new ToStringVisitor(result);
00170         visitRoots(visitor, true);
00171         return result.toString();
00172     }
00173 
00174 }
00175 
00176 
00177 /*
00178  * For each visited node n: iterate through parents n_p and
00179  * add strings of format " " to the StringBuffer which is passed
00180  * to the constructor.
00181  */
00182 class ToStringVisitor extends DoubleLinkedDAGVisitor {
00183 
00184     StringBuffer str;
00185 
00186     public ToStringVisitor(StringBuffer str) {
00187         this.str = str;
00188     }
00189 
00190     public boolean wantMoreNodes() {
00191         return true;
00192     }
00193 
00194     public void visit(DoubleLinkedDAGNode node) {
00195         Iterator itParents = node.getParentsIterator();
00196         if (!itParents.hasNext()) {
00197             if (str.length() > 0) str.append("; ");
00198             str.append("IF(" + node.toString() + ")");
00199         }
00200         while (itParents.hasNext()) {
00201             DoubleLinkedDAGNode parentNode = (DoubleLinkedDAGNode)itParents.next();
00202             if (str.length() > 0) str.append("; ");
00203             str.append("DF(" + parentNode.toString() + ", " + node.toString() + ")");
00204         }
00205     }
00206 
00207     public String getStr() {
00208         return str.toString();
00209     }
00210 
00211 }


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