# Generated by ICCS SAN Editor
# 2012-01-25 Hot water example
				
		@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . 
		@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . 
		@prefix san: <http://imu.ntua.gr/ontologies/san/2011/06/01/san.rdf#> . 
		@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
		
# san:preserve-hot-water 
	  @prefix :	<http://play/sans/template-san-1/>	.

# e:hot-water-example
:hot-water-example	a	san:SANEntity ;
		san:name	"Hot water example"^^xsd:string;
		san:auto-start	"yes"^^xsd:string;
		san:hasRootGoal	:_preserve_hot_water.
	  
# g:_preserve_hot_water 
:_preserve_hot_water	a	san:RootGoal ;
		san:hasSituation	:_measurement_received ;
		san:hasContextCondition	:_check_if_cloudy ;
		san:hasAction	:_send_recommendation ;
		san:auto-start	"yes"^^xsd:string;
		san:name	"Preserve hot water in heater"^^xsd:string .

# s:_measurement_received 
:_measurement_received	a	san:Situation ;
		san:dialect	"default"^^xsd:string ;
		san:defined-by	"http://streams.event-processing.org/ids/ PachubeFeed s"^^xsd:string ;
		san:hasContextualizer :Sit_1_Ctxzr_RDF ;
		san:name	"Solar panel measurement received"^^xsd:string .

# Situation contextualizer - BEGIN
	:Sit_1_Ctxzr_RDF	a	san:Contextualizer ;
		san:type	"RDF"^^xsd:string ;
		san:contextualizer-query  :Sit_1_Ctxzr_Qry1 ;
		san:contextualizer-query  :Sit_1_Ctxzr_Qry2 ;
		san:contextualizer-query  :Sit_1_Ctxzr_Qry3 .
	
	:Sit_1_Ctxzr_Qry1	a san:ContextualizerQuery ;
		san:language	"SPARQL"^^xsd:string ;
		san:dialect		"default"^^xsd:string ;
		san:defined-by	"prefix ns1: <http://www.linkedopenservices.org/ns/temp-json#> prefix ns2: <http://events.event-processing.org/types/> SELECT ?uri ?tm WHERE { ?uri a ns2:PachubeEvent . ?uri ns2:endTime ?tm }"^^xsd:string .
#		san:defined-by	"SELECT ?uri ?tm WHERE { ?uri a ns2:PachubeEvent . ?uri ns2:endTime ?tm }"^^xsd:string .
	
	:Sit_1_Ctxzr_Qry2	a san:ContextualizerQuery ;
		san:language	"SPARQL"^^xsd:string ;
		san:dialect		"default"^^xsd:string ;
		san:defined-by	"prefix ns1: <http://www.linkedopenservices.org/ns/temp-json#> prefix ns2: <http://events.event-processing.org/types/> SELECT ?feed WHERE { ?uri ns1:feed ?feed }"^^xsd:string .
#		san:defined-by	"SELECT ?feed WHERE { ?uri ns1:feed ?feed }"^^xsd:string .
	
	:Sit_1_Ctxzr_Qry3	a san:ContextualizerQuery ;
		san:language	"SPARQL"^^xsd:string ;
		san:dialect		"default"^^xsd:string ;
		san:defined-by	"prefix ns1: <http://www.linkedopenservices.org/ns/temp-json#> prefix ns2: <http://events.event-processing.org/types/> SELECT ?min ?max ?avg WHERE { ?x ns1:max_value ?max . ?x ns1:min_value ?min . ?x ns1:value ?avg }"^^xsd:string .
#		san:defined-by	"SELECT ?min ?max ?avg WHERE { ?x ns1:max_value ?max . ?x ns1:min_value ?min . ?x ns1:value ?avg }"^^xsd:string .
# Situation contextualizer - END

# c:_check_if_cloudy 
:_check_if_cloudy	a	san:ContextCondition ;
		san:dialect	"JS"^^xsd:string ;
#		san:defined-by	"function check() { util.alert('Check if solar heater is not heating'); return true; } check();"^^xsd:string ;
		san:defined-by	"
				function check() {
					/* Get entity context */
					var entCtx = (ctx.isEntityContext()) ? ctx : ctx.getParentContext();
					
					/* Check if a recommendation has been sent in the last 10 minutes */
					var rdt = entCtx.getItem('recom');
					if (rdt!=null) {
						var now = new java.util.Date();
						var diff = (now.getTime()-rdt.getTime())/(60*1000);
						var RECOM_INTERVAL = util.getConfig('user.RECOM_INTERVAL');
						out.println('JS: CC: Last recommendation was sent '+diff+' minutes ago. [Last:'+rdt+', Now:'+now+']');
						if (diff<RECOM_INTERVAL) {
							out.println('JS: CC: A recommendation was send less than '+RECOM_INTERVAL+' minutes ago. Skipping the rest...');
							return false;
						}
					}
					
					/* Check if it is a peak hour (ie hours when enough sun-light is expected) */
					var tm = entCtx.getItem('tm');
					if (tm==null) return false;
					tm = tm[0];
					if (tm==null) return false;
					tm = util.parseDate(tm);
					out.println('JS: CC: Event timestamp: '+tm);
					var hr = util.formatDate(tm,'H');
					var START_HOUR = util.getConfig('user.START_HOUR');
					var END_HOUR = util.getConfig('user.END_HOUR');
					if (hr<START_HOUR || hr>END_HOUR) {
						out.println('JS: CC: Not in Peak hours ('+START_HOUR+':00-'+END_HOUR+':00)');
						return false;
					}
					
					/* Get last 10 events and average their average values */
					var avg = 0;
					var cnt = 0;
					var i = 0;
					while (cnt<10) {
						var v = entCtx.getItem('avg', i++);
						if (v==null) break;
						v = v[0];
						if (v==null) continue;
						v = java.lang.Double.parseDouble( v );
						if (v==null) break;
						avg += v;
						cnt++;
					}
					
					/* At least MIN_EVENTS events are required */
					var MIN_EVENTS = util.getConfig('user.MIN_EVENTS');
					if (cnt>=MIN_EVENTS) {
						avg = avg / cnt;
						out.println('JS: CC: Current Average='+avg);
					} else {
						out.println('JS: CC: Too few events ('+cnt+') to create a recommendation');
						return false;
					}
					
					/* If average is below AVERAGE_LIMIT continue to action */
					var AVERAGE_LIMIT=util.getConfig('user.AVERAGE_LIMIT');
					if (avg<AVERAGE_LIMIT) {
						out.println('JS: CC: A recommendation should be sent');
						var arr = new java.util.Date();
						entCtx.setItem('recom', arr);
						return true;
					}
					
					out.println('JS: CC: Nothing to do');
					return false;
				}
				check();"^^xsd:string ;
		san:name	"Check if solar heater is not heating"^^xsd:string .

# a:_send_recommendation 
:AAAA_send_recommendation	a	san:PrimitiveAction ;
		san:command	"EXPRESSION"^^xsd:string ;
		san:dialect "JS"^^xsd:string ;
		san:defined-by "
				function f() { 
					util.alert( 'RECOMMENDATION: Turn on the electric heater' ); 
					util.sendEvent('Moi', 'http://streams.event-processing.org/ids/', 'ESRRecom', 's', 'RECOMMENDATION: Turn on electric heater!');
					return true;
				}
				f();"^^xsd:string ;
		san:name	"Send a recommendation to turn on electric heater"^^xsd:string .

:_send_recommendation	a	san:PrimitiveAction ;
		san:command	"SEND EVENT http://streams.event-processing.org/ids/ ESRRecom s RECOMMENDATION: Turn on electric heater!"^^xsd:string ;
		san:dialect "JS"^^xsd:string ;
		san:defined-by "
				function f() {
					var recomText = 'Turn on the electric heater';
					var recomXml =	unescape('<rdf:RDF xmlns:rdf=%22http://www.w3.org/1999/02/22-rdf-syntax-ns#%22'+
							'         xmlns:rdfs=%22http://www.w3.org/2000/01/rdf-schema#%22'+
							'         xmlns:xsd=%22http://www.w3.org/2001/XMLSchema#%22'+
							'         xmlns:esr=%22http://play.eu/esr/v1/%22'+
							'         xmlns:xsi=%22http://www.w3.org/1999/XMLSchema-instance%22>'+
							'    <rdf:Description rdf:nodeID=%22A0%22>'+
							'        <esr:recommendation>'+recomText+'</esr:recommendation>'+
							'    </rdf:Description>'+
							'</rdf:RDF>');
					/*out.println(recomXml);*/
					return recomXml;
				}
				f();"^^xsd:string ;
		san:name	"Send a recommendation to turn on electric heater"^^xsd:string .

# EOF