1 17 18 package SOFA.SOFAnode.Util.DFSRChecker.optimizer; 19 20 21 import java.util.Stack ; 22 import java.util.TreeMap ; 23 import java.util.TreeSet ; 24 25 26 import SOFA.SOFAnode.Util.DFSRChecker.DFSR.CheckingException; 27 import SOFA.SOFAnode.Util.DFSRChecker.DFSR.Options; 28 import SOFA.SOFAnode.Util.DFSRChecker.node.AdjustmentNode; 29 import SOFA.SOFAnode.Util.DFSRChecker.node.AndParallelNode; 30 import SOFA.SOFAnode.Util.DFSRChecker.node.CompositionNode; 31 import SOFA.SOFAnode.Util.DFSRChecker.node.ExplicitNode; 32 import SOFA.SOFAnode.Util.DFSRChecker.node.InvalidParameterException; 33 import SOFA.SOFAnode.Util.DFSRChecker.node.TreeNode; 34 import SOFA.SOFAnode.Util.DFSRChecker.state.Signature; 35 import SOFA.SOFAnode.Util.DFSRChecker.state.State; 36 import SOFA.SOFAnode.Util.DFSRChecker.state.TransitionPair; 37 38 45 public class OptExplicit { 46 47 48 public OptExplicit(TreeNode node, int threshold) { 49 this.node = node; 50 this.threshold = threshold; 51 } 52 53 58 public TreeNode perform() throws InvalidParameterException { 59 60 Stack shouldbuild = new Stack (); boolean buildflag = false; 63 Stack stack = new Stack (); 64 int memoryleft = Options.totalmemory - Options.statememory; 65 memoryleft *= 3000; 66 67 73 stack.push(node); 74 shouldbuild.push(new Boolean (buildflag)); 75 76 while (stack.size() > 0) { 77 TreeNode actual = (TreeNode) stack.pop(); 78 buildflag = ((Boolean ) shouldbuild.pop()).booleanValue(); 79 80 TreeNode[] children = actual.getChildren(); 81 82 if ((actual instanceof AndParallelNode) || (actual instanceof AdjustmentNode) || (actual instanceof CompositionNode)) 83 buildflag = true; 84 85 for (int i = 0; i < children.length; ++i) { 86 long weight = children[i].getWeight(); 87 if ((weight < threshold) && (buildflag)) { 88 children[i] = buildExplicit(children[i]); 89 memoryleft -= weight; 90 threshold = memoryleft < threshold ? memoryleft : threshold; 91 } else { 92 stack.push(children[i]); 93 shouldbuild.push(new Boolean (buildflag)); 94 } 95 96 } 97 } 98 99 Options.statememory += (Signature.getSize() * memoryleft / 300000); 101 102 return node; 103 } 104 105 112 private TreeNode buildExplicit(TreeNode node) throws InvalidParameterException { 113 TreeMap restrans = new TreeMap (); 114 State resinit; 115 TreeSet resaccept = new TreeSet (); 116 TreeSet visited = new TreeSet (); 117 118 Stack stack = new Stack (); 119 120 State actual = node.getInitial(); 121 123 resinit = actual; 124 if (node.isAccepting(actual)) 125 resaccept.add(actual); 126 127 stack.push(actual); 128 129 while (stack.size() > 0) { 130 actual = (State) stack.pop(); 131 actual.getSignature(); 132 133 if (!visited.contains(actual)) { 134 135 if (node.isAccepting(actual)) 136 resaccept.add(actual); 137 138 TransitionPair[] trans; 139 try { 140 trans = (TransitionPair[]) node.getTransitions(actual).transitions; 141 } 142 catch (CheckingException e) { 145 throw new RuntimeException ("Internal checker error"); 146 } 147 148 restrans.put(actual, trans); 149 150 for (int i = 0; i < trans.length; ++i) { 151 stack.push(trans[i].state); 152 } 153 154 visited.add(actual); 155 } 156 } 157 158 return new ExplicitNode(resinit, restrans, resaccept, node.protocol, node); 159 } 160 161 165 private TreeNode node; 166 167 171 private int threshold; 172 173 } | Popular Tags |