init(EnvironmentReasoner)   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 12
c 0
b 0
f 0
cc 1
rs 10
1
package it.cnr.istc.pst.cognition.koala.reasoner.observation;
2
3
import java.util.ArrayList;
4
import java.util.List;
5
6
import it.cnr.istc.pst.cognition.koala.reasoner.environment.EnvironmentListener;
7
import it.cnr.istc.pst.cognition.koala.reasoner.environment.EnvironmentReasoner;
8
import it.cnr.istc.pst.cognition.koala.reasoner.goal.ObservationListener;
9
import it.cnr.istc.pst.cognition.koala.reasoner.owl.jena.OWLKoalaModel;
10
11
/**
12
 * 
13
 * @author anacleto
14
 *
15
 */
16
public abstract class ObservationReasoner implements EnvironmentListener
17
{
18
	private final List<ObservationListener> listeners;			// observation listeners
19
	protected EnvironmentReasoner environment;					// environment model
20
	
21
	/**
22
	 * 
23
	 */
24
	public ObservationReasoner() {
25
		this.listeners = new ArrayList<ObservationListener>();
26
	}
27
	
28
	/**
29
	 * 
30
	 * @param listener
31
	 */
32
	public void subscribe(ObservationListener listener) {
33
		synchronized (this.listeners) {
34
			// add subscriber
35
			this.listeners.add(listener);
36
		}
37
	}
38
	
39
	/**
40
	 * 
41
	 * @param env
42
	 */
43
	public void init(EnvironmentReasoner env) {
44
		// set environment
45
		this.environment = env;
46
		this.environment.subscribe(this);
47
		
48
		// clear list of subscribers
49
		synchronized (this.listeners) {
50
			this.listeners.clear();
51
		}
52
		
53
		// complete initialization
54
		this.doInitialize(this.environment);
55
	}
56
	
57
	/**
58
	 * 
59
	 */
60
	public abstract void close();
61
	
62
	/**
63
	 * 
64
	 */
65
	public void update() 
66
	{
67
		// TODO Auto-generated method stub
68
	}
69
	
70
	/**
71
	 * 
72
	 * @param env
73
	 */
74
	protected abstract void doInitialize(EnvironmentReasoner env);
75
	
76
	/**
77
	 * 
78
	 * @param sensorId
79
	 * @param value
80
	 * @param propertyUri
81
	 * @throws Exception
82
	 */
83
	public void observation(String sensorId, Object value, String propertyUri) 
84
			throws Exception
85
	{
86
		// do handle observation
87
		this.doHandleObservation(sensorId, value, propertyUri);
88
		
89
		// extract information about inferred events and/or activities
90
		List<ObservationReasonerUpdate> updates = this.doPrepareObservationNotifications();
91
		// check observed events
92
		if (!updates.isEmpty()) 
93
		{
94
			// notify observation listeners
95
			synchronized (this.listeners) {
96
				// send update signal
97
				for (ObservationListener listener : this.listeners) {
98
					// send update signal
99
					listener.update(updates);
100
				}
101
			}
102
		}
103
	}
104
	
105
	/**
106
	 * 
107
	 * @param sensorIds
108
	 * @param values
109
	 * @param propertyUris
110
	 * @throws Exception
111
	 */
112
	public void observation(String[] sensorIds, Object[] values, String[] propertyUris) 
113
			throws Exception 
114
	{
115
		// handle all observations
116
		for (int index = 0; index < sensorIds.length; index++) {
117
			// get observation information
118
			String sensorId = sensorIds[index];
119
			Object value = values[index];
120
			String propertyUri = propertyUris[index];
121
			
122
			// handle observation
123
			this.doHandleObservation(sensorId, value, propertyUri);
124
		}
125
		
126
		
127
		// extract information about inferred events and/or activities
128
		List<ObservationReasonerUpdate> updates = this.doPrepareObservationNotifications();
129
		// check observed events
130
		if (!updates.isEmpty()) 
131
		{
132
			// notify observation listeners
133
			synchronized (this.listeners) {
134
				// send update signal
135
				for (ObservationListener listener : this.listeners) {
136
					// send update signal
137
					listener.update(updates);
138
				}
139
			}
140
		}
141
	}
142
	
143
	/**
144
	 * 
145
	 * @param eventId
146
	 * @param eventType
147
	 * @param concernedFeatureId
148
	 * @return
149
	 */
150
	protected ObservationReasonerUpdate createEventUpdate(String eventId, String eventType, String concernedFeatureId) {
151
		// create update object
152
		return new ObservationReasonerUpdate(
153
				eventId, 
154
				eventType,
155
				concernedFeatureId,
156
				ObservationUpdateCategory.OBSERVED_EVENT);
157
		
158
	}
159
	
160
	/**
161
	 * 
162
	 * @param eventId
163
	 * @param eventType
164
	 * @param concernedFeatureId
165
	 * @return
166
	 */
167
	protected ObservationReasonerUpdate createActivityUpdate(String eventId, String eventType, String concernedFeatureId) {
168
		// create update object
169
		return new ObservationReasonerUpdate(
170
				eventId, 
171
				eventType, 
172
				concernedFeatureId,
173
				ObservationUpdateCategory.OBSERVED_ACTIVITY);
174
		
175
	}
176
	
177
	
178
	/**
179
	 * 
180
	 * @param sensorId
181
	 * @param value
182
	 * @param propertyUri
183
	 * @throws Exception
184
	 */
185
	protected abstract void doHandleObservation(String sensorId, Object value, String propertyUri) 
186
			throws Exception;
0 ignored issues
show
Best Practice introduced by
Dedicated exceptions should be preferred over throwing the generic Exception.
Loading history...
187
	
188
	/**
189
	 * 
190
	 * @return
191
	 */
192
	protected abstract List<ObservationReasonerUpdate> doPrepareObservationNotifications();
193
	
194
	/**
195
	 * 
196
	 * @return
197
	 */
198
	public abstract OWLKoalaModel getModel();
199
}
200