Conditions | 14 |
Total Lines | 146 |
Code Lines | 79 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like it.cnr.istc.pst.platinum.ai.deliberative.solver.PseudoControllabilityAwareSolver.solve() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | package it.cnr.istc.pst.platinum.ai.deliberative.solver; |
||
45 | @Override |
||
46 | public SearchSpaceNode solve() |
||
47 | throws NoSolutionFoundException |
||
48 | { |
||
49 | // set solving start time |
||
50 | long start = System.currentTimeMillis(); |
||
51 | // set solving step counter |
||
52 | this.stepCounter = 0; |
||
53 | // last extracted node |
||
54 | SearchSpaceNode last = null, node = null; |
||
55 | // search condition |
||
56 | boolean search = true; |
||
57 | // search a solution |
||
58 | while (search) |
||
59 | { |
||
60 | try |
||
61 | { |
||
62 | |||
63 | // update step counter |
||
64 | this.stepCounter++; |
||
65 | // get time passed from the start |
||
66 | long now = System.currentTimeMillis() - start; |
||
67 | // check timeout |
||
68 | if (this.timeout > 0 && now > this.timeout) |
||
69 | { |
||
70 | // no solution found stop search |
||
71 | search = false; |
||
72 | // set solving time |
||
73 | this.time = System.currentTimeMillis() - start; |
||
74 | // backtrack from the last propagated node |
||
75 | this.backtrack(last); |
||
76 | // timeout exception |
||
77 | throw new NoSolutionFoundException("Timeout: no solution found after " + this.time + " msecs and " + this.stepCounter + " solving steps"); |
||
78 | } |
||
79 | |||
80 | |||
81 | // extract a node from the fringe |
||
82 | node = this.fringe.dequeue(); |
||
83 | |||
84 | // info message |
||
85 | String info = "Extracted node [step = " + this.stepCounter + "]:\n" |
||
86 | + "node: " + node + "\n"; |
||
87 | // check operators |
||
88 | if (last != null) { |
||
89 | info += "Operators:\n"; |
||
|
|||
90 | // print last node operations |
||
91 | for (Operator op : node.getOperators()) { |
||
92 | info += "op: " + op + "\n"; |
||
93 | } |
||
94 | } |
||
95 | // info log |
||
96 | info(info); |
||
97 | |||
98 | // propagate extracted node |
||
99 | this.contextSwitch(last, node); |
||
100 | // updated last propagated node |
||
101 | last = node; |
||
102 | // check consistency of the resulting partial plan |
||
103 | this.pdb.verify(); |
||
104 | |||
105 | // context switch done |
||
106 | info("Context switch successfully done [step = " + this.stepCounter + "]:\n" |
||
107 | + "Plan: " + last.getPartialPlan() + "\n"); |
||
108 | |||
109 | // print information concerning current partial plan |
||
110 | debug("Detailed plan after propagation: " + node.getGenerator() + "\n" |
||
111 | + "\tplan:\n" |
||
112 | + "\t\tdecisions= " + this.pdb.getPlan().getDecisions() + "\n" |
||
113 | + "\t\trelations= " + this.pdb.getPlan().getRelations() + "\n\n" |
||
114 | + "\tpending plan (agenda):\n" |
||
115 | + "\t\tdecisions= " + this.pdb.getPlan(PlanElementStatus.PENDING).getDecisions() + "\n" |
||
116 | + "\t\trelations= " + this.pdb.getPlan(PlanElementStatus.PENDING).getRelations() + "\n\n" |
||
117 | + "\tsilent plan:\n" |
||
118 | + "\t\tdecisions= " + this.pdb.getPlan(PlanElementStatus.SILENT).getDecisions() + "\n" |
||
119 | + "\t\trelations= " + this.pdb.getPlan(PlanElementStatus.SILENT).getRelations() + "\n\n"); |
||
120 | |||
121 | // choose the best flaws to solve |
||
122 | List<Flaw> flaws = new ArrayList<>(this.heuristic.choose()); |
||
123 | // create a branch for each "equivalent" flaw to solve next |
||
124 | for (Flaw flaw : flaws) |
||
125 | { |
||
126 | // expand the search space with the available solutions of the flaw |
||
127 | for (SearchSpaceNode child : this.expand(node, flaw)) { |
||
128 | // add the node to the fringe |
||
129 | this.fringe.enqueue(child); |
||
130 | // expand the search space |
||
131 | info("Search tree expansion:\n" |
||
132 | + "node: " + child + "\n" |
||
133 | + "generator: " + child.getGenerator() + "\n"); |
||
134 | } |
||
135 | } |
||
136 | } |
||
137 | catch (PlanRefinementException ex) { |
||
138 | // refinement error |
||
139 | warning("Refinement error [step = " + this.stepCounter + "]:\n" |
||
140 | + "message: " + ex.getMessage() + "\n" |
||
141 | + "Plan:\n" + last.getPartialPlan() + "\n"); |
||
142 | } |
||
143 | catch (UnsolvableFlawException ex) { |
||
144 | // refinement error |
||
145 | warning("Unsolvable flaw found [step = " + this.stepCounter + "]:\n" |
||
146 | + "message: " + ex.getMessage() + "\n" |
||
147 | + "Plan:\n" + last.getPartialPlan() + "\n"); |
||
148 | } |
||
149 | catch (ConsistencyCheckException ex) |
||
150 | { |
||
151 | // context switch failure |
||
152 | warning("Context switch failure [step = " + this.stepCounter + "]:\n" |
||
153 | + "message: " + ex.getMessage() + "\n" |
||
154 | + "Plan:\n" + last.getPartialPlan() + "\n"); |
||
155 | } |
||
156 | catch (NoFlawFoundException ex) |
||
157 | { |
||
158 | // solution found stop search |
||
159 | search = false; |
||
160 | // set solving time |
||
161 | this.time = System.currentTimeMillis() - start; |
||
162 | // pseudo-controllable solution found |
||
163 | info("Pseudo-controllable solution found after " + (this.time / 1000) + " (secs) and " + this.stepCounter + " solving steps\n"); |
||
164 | } |
||
165 | catch (EmptyFringeException ex) |
||
166 | { |
||
167 | // no solution found stop search |
||
168 | search = false; |
||
169 | // set solving time |
||
170 | this.time = System.currentTimeMillis() - start; |
||
171 | // backtrack from the last propagated node |
||
172 | this.backtrack(last); |
||
173 | // throw exception |
||
174 | throw new NoSolutionFoundException("No pseudo-controllable solution found after " + (this.time / 1000) + " (secs) and " + this.stepCounter + " solving steps\n"); |
||
175 | } |
||
176 | // close connection |
||
177 | finally |
||
178 | { |
||
179 | // check if stop |
||
180 | if (!search) { |
||
181 | // clear data structures |
||
182 | this.clear(); |
||
183 | } |
||
184 | } |
||
185 | |||
186 | } // end while |
||
187 | |||
188 | |||
189 | // get last expanded node |
||
190 | return last; |
||
191 | } |
||
204 |