package uk.ac.abdn.foodsafety.csparql;

import java.text.ParseException;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;
import java.util.UUID;
import java.util.function.Consumer;

import uk.ac.abdn.foodsafety.FoodSafetyException;
import uk.ac.abdn.foodsafety.sensordata.TemperatureHumidityReading;
import eu.larkc.csparql.cep.api.RdfQuadruple;
import eu.larkc.csparql.cep.api.RdfStream;
import eu.larkc.csparql.core.engine.ConsoleFormatter;
import eu.larkc.csparql.core.engine.CsparqlEngineImpl;

/**
 * 
 * @author nhc
 *
 * A FoodSafetyEngine is a specific CsparqlEngine with methods
 * and queries for this project.
 */
public final class FoodSafetyEngine
    extends CsparqlEngineImpl 
    implements Consumer<TemperatureHumidityReading> {
    private final RdfStream wirelessStream = new RdfStream("http://foodsafety/wirelessTag");
    
    /**
     * Initializes this engine.
     */
    public FoodSafetyEngine() {
        this.initialize();
        this.registerStream(wirelessStream);
        String text = new Scanner(FoodSafetyEngine.class.getResourceAsStream("/wireless.sparql.txt"), "UTF-8").useDelimiter("\\A").next();
        try { //Register query and add a System.out observer
            this.registerQuery(text, false).addObserver(new ConsoleFormatter());
        } catch (final ParseException e) {
            throw FoodSafetyException.internalError(e);
        }
    }
    
    /**
     * Puts seven RdfQuadruples to this engine, based on one temperature/humidity reading from a wireless tag.
     */
    @Override
    public void accept(final TemperatureHumidityReading reading) {
        final String time = reading.time.format(DateTimeFormatter.ISO_LOCAL_TIME);
        final long timestamp = reading.time.toInstant().toEpochMilli();
        final String baseUri = "http://foodsafety-onto#";
        UUID random = UUID.randomUUID();
        wirelessStream.put(new RdfQuadruple(baseUri +  "wirelesstag", baseUri + "observes", baseUri + "temperatureObservation/" + random.toString(), timestamp));
        wirelessStream.put(new RdfQuadruple(baseUri +  "temperatureObservation/" + random.toString(), baseUri +  "type", baseUri + "temperatureObservation", timestamp));
        wirelessStream.put(new RdfQuadruple(baseUri + "temperatureObservation/" + random.toString(), baseUri + "value", Double.toString(reading.temperature), timestamp));
        wirelessStream.put(new RdfQuadruple(baseUri + "temperatureObservation/" + random.toString(), baseUri + "time", time, timestamp));
        UUID random2 = UUID.randomUUID();
        wirelessStream.put(new RdfQuadruple(baseUri +  "wirelesstag", baseUri + "observes", baseUri + "humidityObservation/" + random2.toString(), timestamp));
        wirelessStream.put(new RdfQuadruple(baseUri + "humidityObservation/" + random2.toString(), baseUri + "value", Double.toString(reading.humidity), timestamp));
        wirelessStream.put(new RdfQuadruple(baseUri +  "humidityObservation/" + random2.toString(), baseUri +  "type", baseUri + "humidityObservation", timestamp));
        wirelessStream.put(new RdfQuadruple(baseUri + "humidityObservation/" + random2.toString(), baseUri + "time", time, timestamp));
    }
}

@@ -33,16 +33,44 @@
 parql;%0A%0A
+import java.io.InputStream;%0A
 import j
@@ -90,24 +90,24 @@
 eException;%0A
-
 import java.
@@ -1049,110 +1049,784 @@
     
-String text = new Scanner(FoodSafetyEngine.class.getResourceAsStream(%22/wireless.sparql.txt%22), %22UTF-8%22)
+this.registerQueryFromResources(%22/wireless.sparql.txt%22);%0A    %7D%0A    %0A    /**%0A     * Registers a SPARQL query read from /src/main/resources%0A     * The query must be encoded in UTF-8.%0A     * A ConsoleFormatter will be added as observer to the query.%0A     * @param path The path to the query, relative to FoodSafety/src/main/resources,%0A     * example: %22/myquery.sparql.txt%22%0A     */%0A    private void registerQueryFromResources(final String path) %7B%0A        //Get InputStream for the file%0A        final InputStream resourceAsStream = FoodSafetyEngine.class.getResourceAsStream(path);%0A        Scanner scanner = null;%0A        try %7B%0A            //Read entire file as UTF-8 into String%0A            scanner = new Scanner(resourceAsStream, %22UTF-8%22);%0A            final String text = scanner
 .use
@@ -1858,21 +1858,19 @@
         
-try %7B
+   
  //Regis
@@ -1891,21 +1891,8 @@
 add 
-a System.out 
 obse
@@ -2076,16 +2076,16 @@
 ror(e);%0A
-
         
@@ -2081,24 +2081,151 @@
 );%0A        %7D
+ finally %7B //Close Scanner%0A            if (scanner != null) %7B%0A                scanner.close();%0A            %7D%0A        %7D%0A        
 %0A    %7D%0A    %0A

Make sure to close resource