Skip to content

Commit

Permalink
JSON rules initial version complete
Browse files Browse the repository at this point in the history
  • Loading branch information
richturner committed Jan 3, 2019
1 parent c0e485c commit 5289d9b
Show file tree
Hide file tree
Showing 14 changed files with 673 additions and 354 deletions.
Expand Up @@ -806,7 +806,7 @@ protected String buildFromString(BaseAssetQuery query, int level) {
}

if (level == 1) {
if (query.parent != null && !query.parent.noParent && (query.parent.id != null || query.parent.type != null)) {
if (query.parent != null && !query.parent.noParent && (query.parent.id != null || query.parent.type != null || query.parent.name != null)) {
sb.append("cross join ASSET P ");
} else {
sb.append("left outer join ASSET P on A.PARENT_ID = P.ID ");
Expand Down Expand Up @@ -890,19 +890,27 @@ protected String buildWhereClause(BaseAssetQuery query, int level, List<Paramete
}

if (query.parent != null) {
// Can only restrict recursive query parent by asset type
if (level == 1 && query.parent.id != null) {
sb.append(" and p.ID = a.PARENT_ID");
sb.append(" and A.PARENT_ID = ?");
final int pos = binders.size() + 1;
binders.add(st -> st.setString(pos, query.parent.id));
} else if (query.parent.type != null) {
sb.append(" and p.ID = a.PARENT_ID");
sb.append(" and P.ASSET_TYPE = ?");
final int pos = binders.size() + 1;
binders.add(st -> st.setString(pos, query.parent.type));
} else if (level == 1 && query.parent.noParent) {
sb.append(" and A.PARENT_ID is null");
} else if (query.parent.type != null || query.parent.name != null) {

sb.append(" and p.ID = a.PARENT_ID");

if (query.parent.type != null) {
sb.append(" and P.ASSET_TYPE = ?");
final int pos = binders.size() + 1;
binders.add(st -> st.setString(pos, query.parent.type));
}
if (query.parent.name != null) {
sb.append(" and P.NAME = ?");
final int pos = binders.size() + 1;
binders.add(st -> st.setString(pos, query.parent.name));
}
}
}

Expand Down
Expand Up @@ -235,6 +235,7 @@ public static Predicate<AssetState> asPredicate(ParentPredicate predicate) {
return assetState ->
(predicate.id == null || predicate.id.equals(assetState.getParentId()))
&& (predicate.type == null || predicate.type.equals(assetState.getParentTypeString()))
&& (predicate.name == null || predicate.name.equals(assetState.getParentName()))
&& (!predicate.noParent || assetState.getParentId() == null);
}

Expand All @@ -260,14 +261,20 @@ public static Predicate<Coordinate> asPredicate(GeofencePredicate predicate) {
GeodeticCalculator calculator = new GeodeticCalculator();
calculator.setStartingGeographicPoint(radialLocationPredicate.lng, radialLocationPredicate.lat);
calculator.setDestinationGeographicPoint(coordinate.y, coordinate.x);
return calculator.getOrthodromicDistance() < radialLocationPredicate.radius;
if (predicate.negated) {
return calculator.getOrthodromicDistance() > radialLocationPredicate.radius;
}
return calculator.getOrthodromicDistance() <= radialLocationPredicate.radius;
} else if (predicate instanceof RectangularGeofencePredicate) {
// Again this is a euclidean plane so doesn't work perfectly for WGS lat/lng - the bigger the rectangle to less accurate it is)
RectangularGeofencePredicate rectangularLocationPredicate = (RectangularGeofencePredicate) predicate;
Envelope envelope = new Envelope(rectangularLocationPredicate.latMin,
rectangularLocationPredicate.lngMin,
rectangularLocationPredicate.latMax,
rectangularLocationPredicate.lngMax);
if (predicate.negated) {
return !envelope.contains(coordinate);
}
return envelope.contains(coordinate);
} else {
throw new UnsupportedOperationException("Location predicate '" + predicate.getClass().getSimpleName() + "' not supported in rules matching");
Expand Down

0 comments on commit 5289d9b

Please sign in to comment.