use of datawave.query.attributes.ValueTuple in project datawave by NationalSecurityAgency.
the class EvaluationPhaseFilterFunctionsTest method toNonTypedValueTuple.
private static ValueTuple toNonTypedValueTuple(String csv) {
String[] tokens = csv.split(",");
String field = tokens[0];
String normalized = tokens[2];
PreNormalizedAttribute attribute = new PreNormalizedAttribute(normalized, null, false);
return new ValueTuple(field, "second", normalized, attribute);
}
use of datawave.query.attributes.ValueTuple in project datawave by NationalSecurityAgency.
the class GroupingRequiredFilterFunctions method getGroupsForMatchesInGroup.
/**
* <pre>
* 'args' will be either a matched set of field/regex pairs, or a matched set of field/regex pairs followed by an index integer,
* in which case the integer is a zero based value to determine where to split off the 'group'.
* FOO.1 with a index of '0' will split off '1'
* FOO.BLAH.ZIP.0 with an index of '2' will split off 'BLAH.ZIP.0'
* If no index is supplied, the default is 0
*
* The return is a collection of 'groups' that matched: [0, 1, 5] for example.
* This collection of groups may be passed as the argument to the method getValuesForGroups.
* For example: AGE.getValuesForGroups(getGroupsForMatchesInGroup(NAME, 'MEADOW', GENDER, 'FEMALE')
* The return value from that combination will be a collection of ages for the same groups where NAME and GENDER matched
* the supplied parameters
* If the return of the getGroupsForMatchesInGroup call was [0, 1, 5] then the values for AGE.0, AGE.1, AGE.5] will be returned
* </pre>
*
* @param args
* @return a collection of the groups that matched.
*/
public static Collection<?> getGroupsForMatchesInGroup(Object... args) {
// this is either '0', or it is the integer value of the last argument
// when the argumentCount is odd
Set<String> groups = new HashSet<>();
int positionFromRight = 0;
if (args.length % 2 != 0) {
// it's odd
Object lastArgument = args[args.length - 1];
positionFromRight = Integer.parseInt(lastArgument.toString());
}
Collection<ValueTuple> leftSideMatches;
Collection<ValueTuple> allMatches = new HashSet<>();
Object fieldValue1 = args[0];
String regex = args[1].toString();
if (fieldValue1 instanceof Iterable) {
// cast as Iterable in order to call the right getAllMatches method
leftSideMatches = EvaluationPhaseFilterFunctions.getAllMatches((Iterable) fieldValue1, regex);
} else {
leftSideMatches = EvaluationPhaseFilterFunctions.getAllMatches(fieldValue1, regex);
}
for (ValueTuple currentMatch : leftSideMatches) {
String matchFieldName = ValueTuple.getFieldName(currentMatch);
// my fieldValue2 will be a collection that looks like [ AGE.FOO.7.1:1, GENDER.BAZ.7.2:2, NAME.FO.7.3:1 ]
// I am only interested in a match on the one that ends with the 'context' (.2) that I found above
String context = EvaluationPhaseFilterFunctions.getMatchToRightOfPeriod(matchFieldName, positionFromRight);
if (context != null && !context.isEmpty()) {
groups.add(context);
}
for (int i = 2; i < args.length; i++) {
if (args[i] instanceof Iterable) {
boolean contextHasMatch = false;
for (Object fieldValue : (Iterable) args[i]) {
// do not change the value of contextHasMatch from true back to false.
if (manageGroupsForMatchesInGroupRemainingArgs(fieldValue, args[i + 1].toString(), context, allMatches, currentMatch)) {
contextHasMatch = true;
}
}
if (!contextHasMatch) {
groups.remove(context);
}
} else if (args[i] instanceof ValueTuple) {
if (!manageGroupsForMatchesInGroupRemainingArgs(args[i], args[i + 1].toString(), context, allMatches, currentMatch)) {
groups.remove(context);
}
}
}
}
// number of field/regex pairs
if (args.length == 2) {
// maybe they passed in only 2 args, in that case, get the groups for whatever was in the first (only) arg pair
allMatches.addAll(leftSideMatches);
} else if (allMatches.size() < args.length / 2) {
// truncated in case args.length was odd
allMatches.clear();
groups.clear();
}
if (log.isTraceEnabled()) {
log.trace("getGroupsForMatchesInGroup(" + Arrays.toString(args) + ") returning " + groups);
}
return groups;
}
use of datawave.query.attributes.ValueTuple in project datawave by NationalSecurityAgency.
the class GroupingRequiredFilterFunctions method matchesInGroupLeft.
/**
* <pre>
* 'args' will be either a matched set of field/regex pairs, or a matched set of field/regex pairs followed by an index integer,
* in which case the integer is a zero based value to determine where to split off the 'group'.
* FOO.1 with a index of '0' will split off '1'
* FOO.BLAH.ZIP.0 with an index of '2' will split off 'BLAH.ZIP.0'
* If no index is supplied, the default is 0
* </pre>
*
* @param args
* @return a collection of matches
*/
public static Collection<?> matchesInGroupLeft(Object... args) {
if (log.isTraceEnabled()) {
log.trace("matchesInGroupLeft(" + Arrays.asList(args) + ")");
}
// positionFrom is either '0', or it is the integer value of the last argument
// when the argumentCount is odd
int positionFromLeft = 0;
if (args.length % 2 != 0) {
// it's odd
Object lastArgument = args[args.length - 1];
positionFromLeft = Integer.parseInt(lastArgument.toString());
}
Collection<ValueTuple> firstMatches;
Collection<ValueTuple> allMatches = new HashSet<>();
Object fieldValue1 = args[0];
String regex = args[1].toString();
if (fieldValue1 instanceof Iterable) {
// cast as Iterable in order to call the right getAllMatches method
firstMatches = EvaluationPhaseFilterFunctions.getAllMatches((Iterable) fieldValue1, regex);
} else {
firstMatches = EvaluationPhaseFilterFunctions.getAllMatches(fieldValue1, regex);
}
if (log.isTraceEnabled()) {
log.trace("firstMatches = " + firstMatches);
}
for (ValueTuple currentMatch : firstMatches) {
String matchFieldName = ValueTuple.getFieldName(currentMatch);
// my firstMatches will be a collection that looks like [NAME.grandparent_0.parent_0.child_0:SANTINO]
String theFirstMatch = EvaluationPhaseFilterFunctions.getMatchToLeftOfPeriod(matchFieldName, positionFromLeft);
for (int i = 2; i < args.length; i += 2) {
if (args[i] instanceof Iterable) {
// look for a match on FREDO
for (Object fieldValue : (Iterable) args[i]) {
String fieldName = ValueTuple.getFieldName(fieldValue);
// @formatter:off
manageMatchesInGroupLeftRemainingArgs(fieldValue, // regex
args[i + 1].toString(), allMatches, theFirstMatch, // the next match
EvaluationPhaseFilterFunctions.getMatchToLeftOfPeriod(fieldName, positionFromLeft), currentMatch);
// @formatter:on
}
} else if (args[i] instanceof ValueTuple) {
// args[i] is a ValueTuple that looks like:
// [NAME.grandparent_0.parent_0.child_1,FREDO,fredo],
// let's say that regex is 'FREDO'
// Assuming that positionFromLeft is 0, then for each of these, I will consider only the ones that have a match on
// grandparent_0.parent_0, then I will see if the name matches my regex (FREDO)
// If the positionFromLeft were 1, I would consider all of the above that include grandparent.0, and then
// look for a match on FREDO
Object fieldValue = args[i];
String fieldName = ValueTuple.getFieldName(fieldValue);
// @formatter:off
manageMatchesInGroupLeftRemainingArgs(fieldValue, // regex
args[i + 1].toString(), allMatches, theFirstMatch, // the next match
EvaluationPhaseFilterFunctions.getMatchToLeftOfPeriod(fieldName, positionFromLeft), currentMatch);
// @formatter:on
}
}
}
// number of field/regex pairs
if (allMatches.size() < args.length / 2) {
// truncated in case args.length was odd
allMatches.clear();
}
if (log.isTraceEnabled()) {
log.trace("returning matches:" + allMatches);
}
return Collections.unmodifiableCollection(allMatches);
}
use of datawave.query.attributes.ValueTuple in project datawave by NationalSecurityAgency.
the class GeoWaveFunctions method getGeometryFromFieldValue.
private static Geometry getGeometryFromFieldValue(Object fieldValue) {
Geometry geometry = null;
if (fieldValue instanceof Geometry) {
geometry = (Geometry) fieldValue;
} else if (fieldValue instanceof GeoNormalizer.GeoPoint) {
geometry = geoPointToGeometry((GeoNormalizer.GeoPoint) fieldValue);
} else if (fieldValue instanceof String) {
geometry = parseGeometry((String) fieldValue);
} else if (fieldValue instanceof ValueTuple) {
ValueTuple t = (ValueTuple) fieldValue;
Object o = t.second();
if (o instanceof AbstractGeometryType) {
AbstractGeometryType<?> gt = (AbstractGeometryType<?>) o;
geometry = ((AbstractGeometry<?>) gt.getDelegate()).getJTSGeometry();
} else if (o instanceof GeoType) {
geometry = parseGeometryFromGeoType(ValueTuple.getNormalizedStringValue(fieldValue));
}
} else if (fieldValue instanceof FunctionalSet) {
FunctionalSet<?> funcSet = (FunctionalSet<?>) fieldValue;
Geometry[] geometries = funcSet.stream().map(GeoWaveFunctions::getGeometryFromFieldValue).toArray(Geometry[]::new);
geometry = new GeometryCollection(geometries, geometryFactory);
}
if (geometry == null) {
throw new IllegalArgumentException("Field Value:" + fieldValue + " cannot be recognized as a geometry");
}
return geometry;
}
use of datawave.query.attributes.ValueTuple in project datawave by NationalSecurityAgency.
the class EvaluationPhaseFilterFunctions method betweenDates.
/**
* Searches for a date between start and end (inclusively)
*
* @param fieldValue
* An iterable of field values in one of the formats above
* @param start
* A start date in the supplied rangePattern format
* @param end
* An end date in the supplied rangePattern format
* @param rangePattern
* A date format to be supplied to java.text.SimpleDateFormat
* @return True if the datetime occurs between the provided datetime values
*/
public static FunctionalSet<ValueTuple> betweenDates(Iterable<?> fieldValue, String start, String end, String rangePattern) {
FunctionalSet<ValueTuple> matches = FunctionalSet.emptySet();
if (fieldValue != null) {
try {
DateFormat rangeFormat = newSimpleDateFormat(rangePattern);
int granularity = getGranularity(rangePattern);
long lStart = getTime(start, rangeFormat);
long lEnd = getNextTime(end, rangeFormat, granularity) - 1;
for (Object o : fieldValue) {
if (betweenInclusive(getTime(o), lStart, lEnd)) {
matches = FunctionalSet.singleton(getHitTerm(o));
break;
}
}
} catch (ParseException pe) {
log.error(pe.getMessage());
} catch (NumberFormatException nfe) {
log.error("Unable to numeric argument " + start + " or " + end + ": " + nfe.getMessage());
}
}
return matches;
}
Aggregations