use of org.apache.rya.indexing.TemporalInterval in project incubator-rya by apache.
the class TemporalIntervalTest method constructorBadArgTest.
@Test
public void constructorBadArgTest() throws Exception {
// the end precedes the beginning:
try {
TemporalInterval ti = new //
TemporalInterval(//
new TemporalInstantRfc3339(2017, 12, 30, 12, 59, 59), // the invention of algebra.
new TemporalInstantRfc3339(820, 12, 30, 12, 59, 59));
Assert.assertFalse("Constructor should throw an error if the beginning is after the end, but no error for interval:" + ti, true);
} catch (IllegalArgumentException e) {
// expected to catch this error.
}
}
use of org.apache.rya.indexing.TemporalInterval in project incubator-rya by apache.
the class TemporalIntervalTest method relationsTest.
@Test
public void relationsTest() throws Exception {
TemporalInterval ti01 = new TemporalInterval(//
new TemporalInstantRfc3339(2015, 12, 30, 12, 59, 59), //
new TemporalInstantRfc3339(2016, 12, 30, 13, 00, 00));
TemporalInterval ti02 = new TemporalInterval(//
new TemporalInstantRfc3339(2015, 12, 30, 12, 59, 59), //
new TemporalInstantRfc3339(2016, 12, 30, 13, 00, 00));
Assert.assertTrue("same constructor parameters, should be equal.", ti01.equals(ti02));
Assert.assertTrue("same constructor parameters, should compare 0 equal.", 0 == ti01.compareTo(ti02));
}
use of org.apache.rya.indexing.TemporalInterval in project incubator-rya by apache.
the class TemporalIntervalTest method hashTest.
@Test
public void hashTest() throws Exception {
// Use MAX to see how it handles overflowing values. Should silently go negative.
int hashcode01Same = (new TemporalInterval(new TemporalInstantRfc3339(new DateTime(Integer.MAX_VALUE / 2)), new TemporalInstantRfc3339(new DateTime(Integer.MAX_VALUE)))).hashCode();
int hashcode02Same = (new TemporalInterval(new TemporalInstantRfc3339(new DateTime(Integer.MAX_VALUE / 2)), new TemporalInstantRfc3339(new DateTime(Integer.MAX_VALUE)))).hashCode();
int hashcode03Diff = (new TemporalInterval(new TemporalInstantRfc3339(new DateTime(Integer.MAX_VALUE / 2)), new TemporalInstantRfc3339(new DateTime(Integer.MAX_VALUE)))).hashCode();
int hashcode04Diff = (new TemporalInterval(new TemporalInstantRfc3339(new DateTime(Integer.MIN_VALUE)), new TemporalInstantRfc3339(new DateTime(Integer.MIN_VALUE)))).hashCode();
int hashcode05Diff = (new TemporalInterval(new TemporalInstantRfc3339(new DateTime(Integer.MAX_VALUE)), new TemporalInstantRfc3339(new DateTime(Integer.MAX_VALUE)))).hashCode();
int hashcode06Diff = (new TemporalInterval(new TemporalInstantRfc3339(new DateTime(0)), new TemporalInstantRfc3339(new DateTime(0)))).hashCode();
int hashcode07Diff = (new TemporalInterval(new TemporalInstantRfc3339(new DateTime(1000)), new TemporalInstantRfc3339(new DateTime(1000)))).hashCode();
int hashcode08Diff = (new TemporalInterval(new TemporalInstantRfc3339(new DateTime(0)), new TemporalInstantRfc3339(new DateTime(1000)))).hashCode();
int hashcode09Diff = (new TemporalInterval((TemporalInstantRfc3339.getMinimumInstance()), (TemporalInstantRfc3339.getMaximumInstance()))).hashCode();
int hashcode10Diff = (new TemporalInterval(new TemporalInstantRfc3339(new DateTime(0)), (TemporalInstantRfc3339.getMaximumInstance()))).hashCode();
Assert.assertEquals("Same input should produce same hashcode. (always!)", hashcode01Same, hashcode02Same);
Assert.assertTrue("Different small input should produce different hashcode. (usually!) hashcodes:" + hashcode03Diff + " " + hashcode04Diff + " " + hashcode03Diff + " " + hashcode05Diff, hashcode03Diff != hashcode04Diff && hashcode03Diff != hashcode05Diff);
Assert.assertTrue("Different large input should produce different hashcode. (usually!) hashcodes:" + hashcode06Diff + " " + hashcode07Diff + " " + hashcode06Diff + " " + hashcode08Diff + " key for date 0= " + (new TemporalInstantRfc3339(new DateTime(0))).getAsKeyString() + " key for date 1000= " + (new TemporalInstantRfc3339(new DateTime(1000))).getAsKeyString(), hashcode06Diff != hashcode07Diff && hashcode06Diff != hashcode08Diff);
Assert.assertTrue("Different max and min input should produce different hashcode. (usually!) hashcodes:" + hashcode09Diff + " != " + hashcode10Diff + "fyi: key for date max= " + (TemporalInstantRfc3339.getMaximumInstance()).getAsKeyString() + " key for date min= " + (TemporalInstantRfc3339.getMinimumInstance()).getAsKeyString(), hashcode09Diff != hashcode10Diff);
}
use of org.apache.rya.indexing.TemporalInterval in project incubator-rya by apache.
the class AccumuloTemporalIndexer method storeStatement.
/**
* Store a statement in the index if it meets the criterion: Object should be
* a literal and one of the validPredicates from the configuration.
* If it does not meet the criteria, it is silently ignored.
* logs a warning if the object is not parse-able.
* Attempts to parse with calendarValue = literalValue.calendarValue()
* if that fails, tries: org.joda.time.DateTime.parse() .
* T O D O parse an interval using multiple predicates for same subject -- ontology dependent.
*/
private void storeStatement(final Statement statement) throws IOException, IllegalArgumentException {
Objects.requireNonNull(temporalIndexBatchWriter, "This is not initialized for writing. Must call setMultiTableBatchWriter() and init().");
// if the predicate list is empty, accept all predicates.
// Otherwise, make sure the predicate is on the "valid" list
final boolean isValidPredicate = validPredicates == null || validPredicates.isEmpty() || validPredicates.contains(statement.getPredicate());
if (!isValidPredicate || !(statement.getObject() instanceof Literal)) {
return;
}
// 0 begin, 1 end of interval
final DateTime[] indexDateTimes = new DateTime[2];
extractDateTime(statement, indexDateTimes);
if (indexDateTimes[0] == null) {
return;
}
if (!this.isInit)
throw new RuntimeException("Method .init() was not called (or failed) before attempting to store statements.");
// Add this as an instant, or interval.
try {
if (indexDateTimes[1] != null) {
final TemporalInterval interval = new TemporalInterval(new TemporalInstantRfc3339(indexDateTimes[0]), new TemporalInstantRfc3339(indexDateTimes[1]));
addInterval(temporalIndexBatchWriter, interval, statement);
} else {
final TemporalInstant instant = new TemporalInstantRfc3339(indexDateTimes[0]);
addInstant(temporalIndexBatchWriter, instant, statement);
}
} catch (final MutationsRejectedException e) {
throw new IOException("While adding interval/instant for statement =" + statement, e);
}
}
use of org.apache.rya.indexing.TemporalInterval in project incubator-rya by apache.
the class EventDocumentConverter method fromDocument.
@Override
public Event fromDocument(final Document document) throws DocumentConverterException {
requireNonNull(document);
final boolean isInstant;
// Preconditions.
if (!document.containsKey(SUBJECT)) {
throw new DocumentConverterException("Could not convert document '" + document + "' because its '" + SUBJECT + "' field is missing.");
}
if (document.containsKey(INSTANT)) {
isInstant = true;
} else {
isInstant = false;
}
final String subject = document.getString(SUBJECT);
final Event.Builder builder = new Event.Builder().setSubject(new RyaURI(subject));
if (document.containsKey(GEO_KEY)) {
final Document geoObj = (Document) document.get(GEO_KEY);
final GeometryFactory geoFact = new GeometryFactory();
final String typeString = (String) geoObj.get("type");
final CoordinateList coords = new CoordinateList();
final Geometry geo;
if (typeString.equals("Point")) {
final List<Double> point = (List<Double>) geoObj.get("coordinates");
final Coordinate coord = new Coordinate(point.get(0), point.get(1));
geo = geoFact.createPoint(coord);
} else if (typeString.equals("LineString")) {
final List<List<Double>> pointsList = (List<List<Double>>) geoObj.get("coordinates");
for (final List<Double> point : pointsList) {
coords.add(new Coordinate(point.get(0), point.get(1)));
}
geo = geoFact.createLineString(coords.toCoordinateArray());
} else {
final List<List<List<Double>>> pointsList = (List<List<List<Double>>>) geoObj.get("coordinates");
if (pointsList.size() == 1) {
final List<List<Double>> poly = pointsList.get(0);
for (final List<Double> point : poly) {
coords.add(new Coordinate(point.get(0), point.get(1)));
}
geo = geoFact.createPolygon(coords.toCoordinateArray());
} else {
final List<List<Double>> first = pointsList.get(0);
final CoordinateList shellCoords = new CoordinateList();
for (final List<Double> point : pointsList.get(0)) {
shellCoords.add(new Coordinate(point.get(0), point.get(1)));
}
final LinearRing shell = geoFact.createLinearRing(shellCoords.toCoordinateArray());
final List<List<List<Double>>> holesPoints = pointsList.subList(1, pointsList.size() - 1);
final LinearRing[] holes = new LinearRing[holesPoints.size()];
for (int ii = 0; ii < holes.length; ii++) {
final List<List<Double>> holePoints = holesPoints.get(ii);
final CoordinateList shells = new CoordinateList();
for (final List<Double> point : pointsList.get(0)) {
shells.add(new Coordinate(point.get(0), point.get(1)));
}
holes[ii] = geoFact.createLinearRing(shells.toCoordinateArray());
}
geo = geoFact.createPolygon(shell, holes);
}
}
builder.setGeometry(geo);
}
if (isInstant) {
// we already know the key exists
final Date date = (Date) document.get(INSTANT);
final DateTime dt = new DateTime(date.getTime());
final TemporalInstant instant = new TemporalInstantRfc3339(dt);
builder.setTemporalInstant(instant);
} else if (document.containsKey(INTERVAL_START)) {
Date date = (Date) document.get(INTERVAL_START);
DateTime dt = new DateTime(date.getTime());
final TemporalInstant begining = new TemporalInstantRfc3339(dt);
date = (Date) document.get(INTERVAL_END);
dt = new DateTime(date.getTime());
final TemporalInstant end = new TemporalInstantRfc3339(dt);
final TemporalInterval interval = new TemporalInterval(begining, end);
builder.setTemporalInterval(interval);
}
return builder.build();
}
Aggregations