| Conditions | 8 |
| Total Lines | 117 |
| Code Lines | 64 |
| 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:
| 1 | package it.cnr.istc.pst.cognition.koala.reasoner.owl.jena; |
||
| 92 | @Override |
||
| 93 | protected void doGoalTriggering(List<ObservationReasonerUpdate> notifications) |
||
| 94 | { |
||
| 95 | // get current time |
||
| 96 | long timestamp = System.currentTimeMillis(); |
||
| 97 | // handle notifications |
||
| 98 | for (ObservationReasonerUpdate notification : notifications) |
||
| 99 | { |
||
| 100 | // current time |
||
| 101 | long now = System.currentTimeMillis(); |
||
| 102 | try |
||
| 103 | { |
||
| 104 | // event type |
||
| 105 | String typeUri = notification.getEventType(); |
||
| 106 | // propagate information into the knowledge base |
||
| 107 | Resource event = this.kb.createIndividual(typeUri); |
||
| 108 | // get associated observable feature |
||
| 109 | Resource observableFeature = this.kb.getResource(notification.getConcernedFeatureId()); |
||
| 110 | // get concerns property |
||
| 111 | Property concerns = this.kb.getProperty(OWLKoalaNameSpace.KOALA.getNs() + "concerns"); |
||
| 112 | // assert property |
||
| 113 | this.kb.assertProperty( |
||
| 114 | event.getURI(), |
||
| 115 | concerns.getURI(), |
||
| 116 | observableFeature.getURI()); |
||
| 117 | |||
| 118 | System.out.println("[GoalReasoner] Update received from observation reasoner\n" |
||
| 119 | + "\t- " + event + "\n" |
||
| 120 | + "\t- asserting property " + event.getURI() + " " + concerns.getURI() + " " + observableFeature.getURI()); |
||
| 121 | } |
||
| 122 | catch (Exception ex) { |
||
| 123 | System.err.println("[GoalReasoner] Error while handling notification\n" |
||
| 124 | + "- notification: " + notification + "\n" |
||
| 125 | + "- message: " + ex.getMessage()); |
||
| 126 | } |
||
| 127 | finally { |
||
| 128 | // compute inference time |
||
| 129 | long time = System.currentTimeMillis() - now; |
||
| 130 | // update counter |
||
| 131 | this.inferenceTime += time; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | try |
||
| 136 | { |
||
| 137 | // get the list of inferred events |
||
| 138 | List<Statement> list = this.kb.getStatements( |
||
| 139 | OWLKoalaNameSpace.SSN.getNs() + "isProxyFor"); |
||
| 140 | |||
| 141 | // check inferred events |
||
| 142 | for (Statement s : list) |
||
| 143 | { |
||
| 144 | // check if already notified |
||
| 145 | if (!this.cache.contains(s)) |
||
| 146 | { |
||
| 147 | // update inference counter |
||
| 148 | this.inferenceCounter++; |
||
| 149 | |||
| 150 | // get statement elements |
||
| 151 | Resource subject = s.getSubject(); |
||
| 152 | |||
| 153 | |||
| 154 | // get type property |
||
| 155 | Property pType = this.kb.getProperty(OWLKoalaNameSpace.RDF.getNs() + "type"); |
||
| 156 | // get event property statement |
||
| 157 | Statement pStatement = subject.getProperty(pType); |
||
| 158 | // get inferred event type |
||
| 159 | Resource taskType = pStatement.getResource(); |
||
| 160 | |||
| 161 | // get the observable feature associated |
||
| 162 | Property supports = this.kb.getProperty(OWLKoalaNameSpace.KOALA.getNs()+ "supports"); |
||
| 163 | // get concerns property statement |
||
| 164 | Statement sStatement = subject.getProperty(supports); |
||
| 165 | // get supported service |
||
| 166 | Resource serviceType = sStatement.getResource(); |
||
| 167 | |||
| 168 | // get "explanation" |
||
| 169 | Property precProperty = this.kb.getProperty(OWLKoalaNameSpace.KOALA.getNs() + "hasPrecondition"); |
||
| 170 | // get precondition |
||
| 171 | Resource precondition = subject.getProperty(precProperty).getResource(); |
||
| 172 | |||
| 173 | // events triggering goal |
||
| 174 | List<Resource> events = new ArrayList<Resource>(); |
||
| 175 | // get events enabling assistive task |
||
| 176 | List<Statement> eStatements = this.kb.getStatements( |
||
| 177 | precondition.getLocalName(), |
||
| 178 | OWLKoalaNameSpace.DUL.getNs()+ "includesEvent", |
||
| 179 | null); |
||
| 180 | |||
| 181 | for (Statement es : eStatements) { |
||
| 182 | // add resource |
||
| 183 | events.add(es.getResource()); |
||
| 184 | } |
||
| 185 | |||
| 186 | // print some data |
||
| 187 | System.out.println("[GoalReasoner] Assistive task detected:\n" |
||
| 188 | + "- task: " + taskType + "\n" |
||
| 189 | + "- supported-service: " + serviceType + "\n" |
||
| 190 | + "- triggering-event(s): " + events + "\n"); |
||
| 191 | |||
| 192 | // add statement to the cache |
||
| 193 | this.cache.add(s); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | } |
||
| 197 | catch (Exception ex) { |
||
| 198 | System.err.println("[GoalReasoner] Error while checking inferred assistive tasks\n- message= " + ex.getMessage()); |
||
| 199 | } |
||
| 200 | finally |
||
| 201 | { |
||
| 202 | // write data |
||
| 203 | if (this.writer != null) |
||
| 204 | { |
||
| 205 | // print inferred task type and service type |
||
| 206 | this.writer.print(timestamp + ";" + this.inferenceCounter + ";" + this.inferenceTime + "\n"); |
||
| 207 | // write record |
||
| 208 | this.writer.flush(); |
||
| 209 | } |
||
| 228 |