Commit 34a43248 authored by Christian Willms's avatar Christian Willms
Browse files

cleanup and comments

parent 7666c581
......@@ -9,13 +9,29 @@ import java.util.*;
import java.util.stream.Collectors;
/**
* TODO add usage description
* The aggregational operator GetEventsLastNDays returns a table of all events
* that occurred during the last n days starting from today 00:00.
* It is supposed to be given a table of several columns of the following form
* arg1 ... argN time n
* where the tuples <arg1, ..., argN> with timestamps during the last n days are returned.
*
* Note that time is required to be filled with XSD long integers
*
* example query:
*
* // return all labvalues for children collected in the last 3 days
* SELECT ?child ?prop ?val ?t
* WHERE ?child <rdf:type> <dom:Child> ?t1
* & ?child <dom:hasLabValue> ?lv ?t2
* & ?lv ?prop ?val ?t
* AGGREGATE ?measurement ?result ?patient ?time = GetEventsLastNDays ?prop ?val ?child ?t ?t "3"^^<xsd:int>
*
*
* @author (C) Christian Willms
* @since JDK 1.8
* @version Tue Feb 13 10:41:55 CET 2018
*/
public class GetLastDaysEvents extends AggregationalOperator {
public class GetEventsLastNDays extends AggregationalOperator {
//////////////////////////////////////////////////
// Some useful constants
......@@ -24,9 +40,7 @@ public class GetLastDaysEvents extends AggregationalOperator {
* One day (24 hours) represented as milliseconds
*/
static final long DAY = 86400000;
//TODO compute the last sunday using currentime % Week
static final long WEEK = 604800000;
static final long MONTH = 0;
/**
* general form of the aggregate call: ?arg1' ... ?argN' = LGetLatest ?arg1 ... ?argN ?time ?n
......
package de.dfki.lt.hfc.aggregates;
import de.dfki.lt.hfc.types.XsdInt;
import de.dfki.lt.hfc.types.XsdLong;
import java.util.HashSet;
/**
* TODO add usage description
* The aggregational operator GetEventsLastTwoWeeks returns a table of all events
* that occurred during the last two weeks starting from last monday 00:00.
* It is supposed to be given a table of several columns of the following form
* arg1 ... argN time
* where the tuples <arg1, ..., argN> with timestamps during the last two weeks are returned.
*
* Note that time is required to be filled with XSD long integers
*
* example query:
*
* // return all labvalues for children collected in the last 2 weeks
* SELECT ?child ?prop ?val ?t
* WHERE ?child <rdf:type> <dom:Child> ?t1
* & ?child <dom:hasLabValue> ?lv ?t2
* & ?lv ?prop ?val ?t
* AGGREGATE ?measurement ?result ?patient ?time = GetEventsLastTwoWeeks ?prop ?val ?child ?t ?t
*
* @author (C) Christian Willms
* @since JDK 1.8
* @version Tue Feb 13 10:46:38 CET 2018
*/
public final class GetLastTwoWeeksEvents extends GetLastDaysEvents {
public final class GetEventsLastTwoWeeks extends GetEventsLastNDays {
......
package de.dfki.lt.hfc.aggregates;
import de.dfki.lt.hfc.types.XsdInt;
import de.dfki.lt.hfc.types.XsdLong;
import java.util.HashSet;
/**
* TODO add usage description
/**
* The aggregational operator GetEventsLastWeek returns a table of all events
* that occurred during the last week starting from last monday 00:00.
* It is supposed to be given a table of several columns of the following form
* arg1 ... argN time
* where the tuples <arg1, ..., argN> with timestamps during the last week are returned.
*
* Note that time is required to be filled with XSD long integers
*
* example query:
*
* // return all labvalues for children collected in the last week
* SELECT ?child ?prop ?val ?t
* WHERE ?child <rdf:type> <dom:Child> ?t1
* & ?child <dom:hasLabValue> ?lv ?t2
* & ?lv ?prop ?val ?t
* AGGREGATE ?measurement ?result ?patient ?time = GetEventsLastWeek ?prop ?val ?child ?t ?t
*
* @author (C) Christian Willms
* @since JDK 1.8
* @version Tue Feb 13 10:48:22 CET 2018
*/
public final class GetLastWeeksEvents extends GetLastDaysEvents {
public final class GetEventsLastWeeks extends GetEventsLastNDays {
......
package de.dfki.lt.hfc.aggregates;
import de.dfki.lt.hfc.types.XsdInt;
import de.dfki.lt.hfc.types.XsdLong;
import java.util.HashSet;
/**
* TODO add usage description
* The aggregational operator GetEventsThisWeek returns a table of all events
* that occurred during the this week starting at current time and going back to Monday 00:00.
* It is supposed to be given a table of several columns of the following form
* arg1 ... argN time
* where the tuples <arg1, ..., argN> with timestamps during the current week are returned.
*
* Note that time is required to be filled with XSD long integers
*
* example query:
*
* // return all labvalues for children collected in the this week
* SELECT ?child ?prop ?val ?t
* WHERE ?child <rdf:type> <dom:Child> ?t1
* & ?child <dom:hasLabValue> ?lv ?t2
* & ?lv ?prop ?val ?t
* AGGREGATE ?measurement ?result ?patient ?time = GetEventsThisWeek ?prop ?val ?child ?t ?t
*
* @author (C) Christian Willms
* @since JDK 1.8
* @version Tue Feb 13 10:52:05 CET 2018
*/
public final class GetThisWeeksEvents extends GetLastDaysEvents {
public final class GetEventsThisWeek extends GetEventsLastNDays {
......
package de.dfki.lt.hfc.aggregates;
import de.dfki.lt.hfc.types.XsdInt;
import de.dfki.lt.hfc.types.XsdLong;
import java.util.*;
/**
* TODO add usage description
* The aggregational operator GetEventsToday returns a table of all events
* that occurred during the current day starting at current time and going back to 00:00.
* It is supposed to be given a table of several columns of the following form
* arg1 ... argN time
* where the tuples <arg1, ..., argN> with timestamps during the duration specified above are returned.
*
* Note that time is required to be filled with XSD long integers
*
* example query:
*
* // return all labvalues for children collected today
* SELECT ?child ?prop ?val ?t
* WHERE ?child <rdf:type> <dom:Child> ?t1
* & ?child <dom:hasLabValue> ?lv ?t2
* & ?lv ?prop ?val ?t
* AGGREGATE ?measurement ?result ?patient ?time = GetEventsToday ?prop ?val ?child ?t ?t
*
* @author (C) Christian Willms
* @since JDK 1.8
* @version Tue Feb 13 10:58:29 CET 2018
*/
public final class GetTodaysEvents extends GetLastDaysEvents {
public final class GetEventsToday extends GetEventsLastNDays {
......
package de.dfki.lt.hfc.aggregates;
import de.dfki.lt.hfc.types.XsdInt;
import de.dfki.lt.hfc.types.XsdLong;
import java.util.HashSet;
/**
* TODO add usage description
* The aggregational operator GetEventsToday returns a table of all events
* that occurred during the current day starting at current time and going back to 00:00.
* It is supposed to be given a table of several columns of the following form
* arg1 ... argN time
* where the tuples <arg1, ..., argN> with timestamps during the duration specified above are returned.
*
* Note that time is required to be filled with XSD long integers
*
* example query:
*
* // return all labvalues for children collected today
* SELECT ?child ?prop ?val ?t
* WHERE ?child <rdf:type> <dom:Child> ?t1
* & ?child <dom:hasLabValue> ?lv ?t2
* & ?lv ?prop ?val ?t
* AGGREGATE ?measurement ?result ?patient ?time = GetEventsToday ?prop ?val ?child ?t ?t
*
* @author (C) Christian Willms
* @since JDK 1.8
* @version Tue Feb 13 10:45:55 CET 2018
*/
public final class GetEventsOfYesterday extends GetLastDaysEvents {
public final class GetEventsYesterday extends GetEventsLastNDays {
......
......@@ -45,7 +45,7 @@ public class NoValue {
};
Query q = new Query(fc.tupleStore);
BindingTable bt = q.query("SELECT ?s ?p ?o WHERE ?s ?p ?o");
//printExpected(bt, fc.tupleStore); // TODO: THIS SHOULD BE REMOVED WHEN FINISHED
printExpected(bt, fc.tupleStore); // TODO: THIS SHOULD BE REMOVED WHEN FINISHED
checkResult(expected, bt, bt.getVars());
}
......
......@@ -6,7 +6,6 @@ import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static de.dfki.lt.hfc.TestUtils.checkResult;
import static junit.framework.TestCase.fail;
/**
* Created by christian on 29/05/17.
......@@ -22,7 +21,7 @@ public class QueryTest_LookupPointInTime {
@BeforeAll
public static void init() throws Exception {
fc = new ForwardChainer(4, // #cores
fc = new ForwardChainer(4, // #cores
true, // verbose
false, // RDF Check
false, // EQ reduction disabled
......@@ -36,117 +35,88 @@ public class QueryTest_LookupPointInTime {
getResource("lookupAtom.idx")
);
// compute deductive closure
// TODO move this into extra tests -> fcInterval.computeClosure();
}
@AfterAll
public static void finish() {
fc.shutdownNoExit();
}
@Test
public void testWhereNoInterval(){
public void testWhereNoInterval() throws QueryParseException {
TupleStore tupleStore = fc.tupleStore;
Query query = new Query(tupleStore);
String[][] expected = {{"<test:Sensor1>","\"1\"^^<xsd:int>"}};
try {
BindingTable bt = query.query("SELECT ?s ?o WHERE \"1\"^^<xsd:long> ?s <test:hasValue> ?o ");
checkResult(fc, bt, expected, "?s" ,"?o");
} catch (QueryParseException e) {
e.printStackTrace();
fail();
}
}
String[][] expected = {{"<test:Sensor1>", "\"1\"^^<xsd:int>"}};
BindingTable bt = query.query("SELECT ?s ?o WHERE \"1\"^^<xsd:long> ?s <test:hasValue> ?o ");
checkResult(fc, bt, expected, "?s", "?o");
}
@Test
public void testSelectWhere(){
public void testSelectWhere() throws QueryParseException {
TupleStore tupleStore = fc.tupleStore;
Query query = new Query(tupleStore);
String[][] expected = {{"<test:Sensor2>","\"2\"^^<xsd:int>"},
{"<test:Sensor1>","\"2\"^^<xsd:int>"},
{"<test:Sensor2>","\"4\"^^<xsd:int>"},
{"<test:Sensor1>","\"3\"^^<xsd:int>"}};
try { // F
BindingTable bt = query.query("SELECT ?s ?o WHERE [\"1\"^^<xsd:long>, \"5\"^^<xsd:long>] ?s <test:hasValue> ?o ");
System.out.println(bt.toString(true));
checkResult(fc, bt, expected, "?s" ,"?o");
} catch (QueryParseException e) {
e.printStackTrace();
fail();
}
String[][] expected = {{"<test:Sensor2>", "\"2\"^^<xsd:int>"},
{"<test:Sensor1>", "\"2\"^^<xsd:int>"},
{"<test:Sensor2>", "\"4\"^^<xsd:int>"},
{"<test:Sensor1>", "\"3\"^^<xsd:int>"}};
BindingTable bt = query.query("SELECT ?s ?o WHERE [\"1\"^^<xsd:long>, \"5\"^^<xsd:long>] ?s <test:hasValue> ?o ");
checkResult(fc, bt, expected, "?s", "?o");
}
@Test
public void testSelectDistinctWhere(){
public void testSelectDistinctWhere() throws QueryParseException {
TupleStore tupleStore = fc.tupleStore;
Query query = new Query(tupleStore);
String[][] expected = {{"<test:Sensor2>","\"2\"^^<xsd:int>"},
{"<test:Sensor1>","\"2\"^^<xsd:int>"},
{"<test:Sensor2>","\"4\"^^<xsd:int>"},
{"<test:Sensor1>","\"3\"^^<xsd:int>"}};
try { // F
BindingTable bt = query.query("SELECT DISTINCT ?s ?o WHERE [\"1\"^^<xsd:long>, \"5\"^^<xsd:long>] ?s <test:hasValue> ?o");
System.out.println(bt.toString());
checkResult(fc, bt, expected, "?s" ,"?o");
} catch (QueryParseException e) {
e.printStackTrace();
fail();
}
String[][] expected = {{"<test:Sensor2>", "\"2\"^^<xsd:int>"},
{"<test:Sensor1>", "\"2\"^^<xsd:int>"},
{"<test:Sensor2>", "\"4\"^^<xsd:int>"},
{"<test:Sensor1>", "\"3\"^^<xsd:int>"}};
BindingTable bt = query.query("SELECT DISTINCT ?s ?o WHERE [\"1\"^^<xsd:long>, \"5\"^^<xsd:long>] ?s <test:hasValue> ?o");
System.out.println(bt.toString());
checkResult(fc, bt, expected, "?s", "?o");
}
@Test
public void testSelectWhereFilter(){
public void testSelectWhereFilter() throws QueryParseException {
TupleStore tupleStore = fc.tupleStore;
Query query = new Query(tupleStore);
String[][] expected = {{"<test:Sensor2>","\"2\"^^<xsd:int>"},
{"<test:Sensor2>","\"4\"^^<xsd:int>"}};
try { // F
BindingTable bt = query.query("SELECT DISTINCT ?s ?o WHERE [\"1\"^^<xsd:long>, \"5\"^^<xsd:long>] ?s <test:hasValue> ?o FILTER ?s != <test:Sensor1>");
System.out.println(bt.toString());
checkResult(fc, bt, expected, "?s" ,"?o");
} catch (QueryParseException e) {
e.printStackTrace();
fail();
}
}
String[][] expected = {{"<test:Sensor2>", "\"2\"^^<xsd:int>"},
{"<test:Sensor2>", "\"4\"^^<xsd:int>"}};
BindingTable bt = query.query("SELECT DISTINCT ?s ?o WHERE [\"1\"^^<xsd:long>, \"5\"^^<xsd:long>] ?s <test:hasValue> ?o FILTER ?s != <test:Sensor1>");
System.out.println(bt.toString());
checkResult(fc, bt, expected, "?s", "?o");
}
@Test
public void testSelectDistinctWhereFilter(){
public void testSelectDistinctWhereFilter() throws QueryParseException {
TupleStore tupleStore = fc.tupleStore;
Query query = new Query(tupleStore);
String[][] expected = {
{"<test:Sensor1>", "\"3\"^^<xsd:int>"},{ "<test:Sensor2>", "\"4\"^^<xsd:int>"}};
try { // F
BindingTable bt = query.query("SELECT DISTINCT ?s ?o WHERE [\"1\"^^<xsd:long>, \"5\"^^<xsd:long>] ?s <test:hasValue> ?o FILTER IGreater ?o \"2\"^^<xsd:int>");
System.out.println(bt.toString());
checkResult(fc, bt, expected, "?s" ,"?o");
} catch (QueryParseException e) {
e.printStackTrace();
fail();
}
}
String[][] expected = {
{"<test:Sensor1>", "\"3\"^^<xsd:int>"}, {"<test:Sensor2>", "\"4\"^^<xsd:int>"}};
BindingTable bt = query.query("SELECT DISTINCT ?s ?o WHERE [\"1\"^^<xsd:long>, \"5\"^^<xsd:long>] ?s <test:hasValue> ?o FILTER IGreater ?o \"2\"^^<xsd:int>");
System.out.println(bt.toString());
checkResult(fc, bt, expected, "?s", "?o");
}
@Test
public void testSelectWhereAggregate(){
public void testSelectWhereAggregate() throws QueryParseException {
TupleStore tupleStore = fc.tupleStore;
Query query = new Query(tupleStore);
String[][] expected = {{"\"4\"^^<xsd:int>"}};
try { // F
BindingTable bt = query.query("SELECT DISTINCT ?s ?o WHERE [\"1\"^^<xsd:long>, \"5\"^^<xsd:long>] ?s <test:hasValue> ?o AGGREGATE ?number = Count ?o");
System.out.println(bt.toString());
checkResult(fc, bt, expected, "?number" );
} catch (QueryParseException e) {
e.printStackTrace();
fail();
}
}
String[][] expected = {{"\"4\"^^<xsd:int>"}};
BindingTable bt = query.query("SELECT DISTINCT ?s ?o WHERE [\"1\"^^<xsd:long>, \"5\"^^<xsd:long>] ?s <test:hasValue> ?o AGGREGATE ?number = Count ?o");
System.out.println(bt.toString());
checkResult(fc, bt, expected, "?number");
@AfterAll
public static void finish() {
fc.shutdownNoExit();
}
}
......@@ -8,6 +8,7 @@ import static org.junit.jupiter.api.Assertions.*;
/**
* TODO change tests to use checkResult !!!
* Created by christian on 08/06/17.
*/
public class QueryTest_LookupTimeCombi {
......@@ -35,8 +36,7 @@ public class QueryTest_LookupTimeCombi {
getResource("lookupCombi.idx")
);
// compute deductive closure
// TODO move this into extra tests -> fcInterval.computeClosure();
}
/**
......@@ -120,17 +120,12 @@ public class QueryTest_LookupTimeCombi {
TupleStore tupleStore = fc.tupleStore;
Query query = new Query(tupleStore);
long startTime = System.currentTimeMillis();
BindingTable bt = query.query(
"SELECT DISTINCT ?s ?o WHERE [\"1\"^^<xsd:long>, \"6\"^^<xsd:long>] ?s <test:hasValue> ?o D \"200\"^^<xsd:long> \"1550\"^^<xsd:long> AGGREGATE ?number = Count ?o");
assertNotNull(bt);
assertFalse(bt.isEmpty());
assertEquals(1, bt.size());
assertEquals(1, bt.getVars().length);
System.out.println(bt.toString());
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println(totalTime);
}
......
......@@ -5,9 +5,10 @@ import de.dfki.lt.hfc.ForwardChainer;
import de.dfki.lt.hfc.Query;
import de.dfki.lt.hfc.QueryParseException;
import de.dfki.lt.hfc.runnable.Utils;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
......@@ -17,31 +18,36 @@ import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
import static de.dfki.lt.hfc.TestUtils.printExpected;
import static de.dfki.lt.hfc.runnable.Utils.checkResult;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* TODO add usage description
* This is a collection of test for the newly introduced aggregtates
* - {@link GetEventsLastNDays},
* - {@link GetEventsLastTwoWeeks},
* - {@link GetEventsLastWeeks},
* - {@link GetEventsThisWeek},
* - {@link GetEventsToday},
* - {@link GetEventsYesterday}
*
* @author (C) Christian Willms
* @since JDK 1.8
* @version Tue Feb 13 16:41:55 CET 2018
* @since JDK 1.8
*/
public class TestGetEvents {
static final long DAY = 86400000;
ForwardChainer fc;
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
public static String getResource(String name) {
return Utils.getTestResource("LGetLatestValues" , name);
return Utils.getTestResource("LGetLatestValues", name);
}
@BeforeEach
public void init() throws Exception {
public void init() throws Exception {
fc = new ForwardChainer(4, // #cores
fc = new ForwardChainer(4, // #cores
false, // verbose
false, // RDF Check
false, // EQ reduction disabled
......@@ -59,25 +65,24 @@ public class TestGetEvents {
@AfterEach
public void cleanup() {
public void cleanup() {
fc.shutdownNoExit();
}
/**
* This method tests the basic logic used for the GetLastDaysEvents aggregate
* TODO (write down proper description and method name)
* This method tests the basic logic used for the GetEventsLastNDays aggregate
*/
@Test
public void testBasicLogic(){
public void testBasicLogic() {
// create a set of tuple where the transaction times depend on the current time and load those into the ontology
long currentTime = System.currentTimeMillis();
long midnight = currentTime - (currentTime % DAY) ;
long midnight = currentTime - (currentTime % DAY);
// check whether this is really 00:00
LocalDateTime midnightDate = Instant.ofEpochMilli(midnight).atZone(ZoneId.of("UTC")).toLocalDateTime();
assertTrue(midnightDate.toString().endsWith("00:00"));
// check whether the computation of sundays work
long sundayMidnight = currentTime - (currentTime % (7 * DAY)) - 3* DAY;
long sundayMidnight = currentTime - (currentTime % (7 * DAY)) - 3 * DAY;
LocalDateTime lastSundayDateTime = Instant.ofEpochMilli(sundayMidnight).atZone(ZoneId.of("UTC")).toLocalDateTime();
LocalDate lastSundayDate = Instant.ofEpochMilli(sundayMidnight).atZone(ZoneId.of("UTC")).toLocalDate();
System.out.println(lastSundayDate);
......@@ -87,10 +92,11 @@ public class TestGetEvents {
/**
* This method uses random generated timestamps to test whether the GetLastDaysEvents aggregate works correctly.
* It adds 20 tuples with random timestamps for the transaction time to the ontology.
* This method uses randomly generated timestamps to test whether the GetEventsLastNDays aggregate works correctly.
* It adds 20 tuples with random timestamps to the ontology.
* All timestamps lay in the last 4 days. Therefore we can test whether the aggregate correctly handles out of range
* values, i.e. values that are older than 3 days.
*
* @throws QueryParseException
*/
@Test
......@@ -98,29 +104,27 @@ public class TestGetEvents {
Query q = new Query(fc.tupleStore);
long currentTime = System.currentTimeMillis();
long midnight = currentTime - (currentTime % DAY) ;
long midnight = currentTime - (currentTime % DAY);
long start = midnight - 3 * DAY;
// populate ontology and expected values
Set<String[]> validTuples = new HashSet<String[]>();
populateTestSetup(validTuples, currentTime, start, midnight, 1);
populateTestSetup(validTuples, currentTime, start, midnight, 1);
String[][] expected = validTuples.toArray(new String[validTuples.size()][]);
BindingTable bt = q.query("SELECT ?child ?prop ?val ?t "
+ "WHERE ?child <rdf:type> <dom:Child> ?t1 "
+ "& ?child <dom:hasLabValue> ?lv ?t2 "
+ "& ?lv ?prop ?val ?t "
+ "AGGREGATE ?measurement ?result ?patient ?time = GetLastDaysEvents ?prop ?val ?child ?t ?t \"3\"^^<xsd:int>");
+ "AGGREGATE ?measurement ?result ?patient ?time = GetEventsLastNDays ?prop ?val ?child ?t ?t \"3\"^^<xsd:int>");
checkResult(fc, bt, expected, "?measurement", "?result", "?patient", "?time");
}
/**
* This method uses random generated timestamps to test whether the GetTodaysEvents aggregate works correctly.
* This method uses randomly generated timestamps to test whether the GetEventsToday aggregate works correctly.
* It adds 20 tuples with random timestamps for the transaction time to the ontology.
* All timestamps lay in the last 4 days. Therefore we can test whether the aggregate correctly handles out of range
* values, i.e. values that are older than 3 days.
*
* @throws QueryParseException
*/
@Test
......@@ -128,7 +132,7 @@ public class TestGetEvents {
Query q = new Query(fc.tupleStore);
long currentTime = System.currentTimeMillis();
long start = currentTime - (currentTime % DAY) ;
long start = currentTime - (currentTime % DAY);