use of uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition in project Gaffer by gchq.
the class ExamplesService method generateViewBuilder.
public View.Builder generateViewBuilder() {
final View.Builder viewBuilder = new View.Builder();
if (hasEntities()) {
final ViewElementDefinition viewElement;
if (null == getAnEntityPropertyName()) {
viewElement = new ViewElementDefinition();
} else {
viewElement = new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select(getAnEntityPropertyName()).execute(new ExampleFilterFunction()).build()).build();
}
viewBuilder.entity(getAnEntityGroup(), viewElement);
}
if (hasEdges()) {
final ViewElementDefinition viewElement;
if (null == getAnEdgePropertyName()) {
viewElement = new ViewElementDefinition();
} else {
viewElement = new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select(getAnEdgePropertyName()).execute(new ExampleFilterFunction()).build()).build();
}
viewBuilder.edge(getAnEdgeGroup(), viewElement);
}
viewBuilder.globalElements(new GlobalViewElementDefinition.Builder().groupBy().build());
return viewBuilder;
}
use of uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition in project Gaffer by gchq.
the class GetAdjacentIdsTest method shouldPassValidationOnEntitiesWithoutFilters.
@Test
public void shouldPassValidationOnEntitiesWithoutFilters() throws OperationException {
// Given
final Graph graph = GetAllElementsHandlerTest.getGraph();
final AddElements addElements = new AddElements.Builder().input(GetAllElementsHandlerTest.getElements()).build();
graph.execute(addElements, new User());
// When
final GetAdjacentIds getAdjacentIds = new GetAdjacentIds.Builder().input(new EntitySeed("A"), new EntitySeed("Y2")).view(new View.Builder().edge(GetAllElementsHandlerTest.BASIC_EDGE1, new ViewElementDefinition.Builder().postAggregationFilter(new ElementFilter.Builder().select(GetAllElementsHandlerTest.COUNT).execute(new IsMoreThan(5)).build()).build()).entity(GetAllElementsHandlerTest.BASIC_ENTITY, new ViewElementDefinition()).build()).build();
final CloseableIterable<? extends EntityId> results = graph.execute(getAdjacentIds, new User());
// Then
final Set<EntityId> resultsSet = new HashSet<>();
Streams.toStream(results).forEach(resultsSet::add);
final Set<EntityId> expectedResults = new HashSet<>();
GetAllElementsHandlerTest.getElements().stream().filter(element -> element instanceof Edge).filter(element -> element.getGroup().equals(GetAllElementsHandlerTest.BASIC_EDGE1)).filter(element -> {
final Edge edge = (Edge) element;
return edge.getSource().equals("A") || edge.getDestination().equals("A") || edge.getSource().equals("Y2") || edge.getDestination().equals("Y2");
}).filter(element -> ((Integer) element.getProperty(GetAllElementsHandlerTest.COUNT)) > 5).map(element -> {
final Edge edge = (Edge) element;
final Set<EntityId> nodes = new HashSet<>();
nodes.add(new EntitySeed(edge.getSource()));
nodes.add(new EntitySeed(edge.getDestination()));
return nodes;
}).flatMap(nodes -> nodes.stream()).forEach(expectedResults::add);
expectedResults.remove(new EntitySeed("A"));
expectedResults.remove(new EntitySeed("Y2"));
assertEquals(expectedResults, resultsSet);
}
use of uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition in project Gaffer by gchq.
the class AbstractElementFilter method init.
@Override
public void init(final SortedKeyValueIterator<Key, Value> source, final Map<String, String> options, final IteratorEnvironment env) throws IOException {
super.init(source, options, env);
schema = Schema.fromJson(StringUtil.toBytes(options.get(AccumuloStoreConstants.SCHEMA)));
LOGGER.debug("Initialising AbstractElementFilter with Schema {}", schema);
final String elementConverterClass = options.get(AccumuloStoreConstants.ACCUMULO_ELEMENT_CONVERTER_CLASS);
try {
elementConverter = Class.forName(elementConverterClass).asSubclass(AccumuloElementConverter.class).getConstructor(Schema.class).newInstance(schema);
LOGGER.debug("Creating AccumuloElementConverter of class {}", elementConverterClass);
} catch (final ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
throw new ElementFilterException("Failed to create element converter of the class name provided (" + elementConverterClass + ")", e);
}
if (filterType == ElementValidator.FilterType.SCHEMA_VALIDATION) {
updateSchemaGroupsWithoutFilters();
elementPredicate = new ElementValidator(schema, false)::validateWithSchema;
} else {
final String viewJson = options.get(AccumuloStoreConstants.VIEW);
if (null == viewJson) {
throw new IllegalArgumentException("Must specify the " + AccumuloStoreConstants.VIEW);
}
final View view = View.fromJson(StringUtil.toBytes(viewJson));
LOGGER.debug("Determining groups that don't need to be filtered based on view {}", view);
if (filterType == ElementValidator.FilterType.PRE_AGGREGATION_FILTER) {
updateViewGroupsWithoutFilters(view, ViewElementDefinition::hasPreAggregationFilters);
elementPredicate = new ElementValidator(view)::validateInput;
} else {
updateViewGroupsWithoutFilters(view, ViewElementDefinition::hasPostAggregationFilters);
elementPredicate = new ElementValidator(view)::validateAggregation;
}
}
}
use of uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition in project Gaffer by gchq.
the class FiltersToOperationConverter method applyPropertyFilters.
private Output<RDD<Element>> applyPropertyFilters(final View derivedView, final Output<RDD<Element>> operation) {
final List<Set<String>> groupsRelatedToFilters = new ArrayList<>();
for (final Filter filter : filters) {
final Set<String> groupsRelatedToFilter = getGroupsFromFilter(filter);
if (null != groupsRelatedToFilter && !groupsRelatedToFilter.isEmpty()) {
groupsRelatedToFilters.add(groupsRelatedToFilter);
}
LOGGER.info("Groups {} are related to filter {}", StringUtils.join(groupsRelatedToFilter, ','), filter);
}
LOGGER.info("Groups related to filters are: {}", StringUtils.join(groupsRelatedToFilters, ','));
// Take the intersection of this list of groups - only these groups can be related to the query
final Set<String> intersection = new HashSet<>(derivedView.getEntityGroups());
intersection.addAll(derivedView.getEdgeGroups());
for (final Set<String> groupsRelatedToFilter : groupsRelatedToFilters) {
intersection.retainAll(groupsRelatedToFilter);
}
LOGGER.info("Groups that can be returned are: {}", StringUtils.join(intersection, ','));
// Update view with filters and add to operation
final Map<String, List<TupleAdaptedPredicate<String, ?>>> groupToFunctions = new HashMap<>();
for (final Filter filter : filters) {
final Map<String, List<TupleAdaptedPredicate<String, ?>>> map = getFunctionsFromFilter(filter);
for (final Entry<String, List<TupleAdaptedPredicate<String, ?>>> entry : map.entrySet()) {
if (!groupToFunctions.containsKey(entry.getKey())) {
groupToFunctions.put(entry.getKey(), new ArrayList<>());
}
groupToFunctions.get(entry.getKey()).addAll(entry.getValue());
}
}
LOGGER.info("The following functions will be applied for the given group:");
for (final Entry<String, List<TupleAdaptedPredicate<String, ?>>> entry : groupToFunctions.entrySet()) {
LOGGER.info("Group = {}: ", entry.getKey());
for (final TupleAdaptedPredicate<String, ?> cfc : entry.getValue()) {
if (null != cfc.getSelection()) {
LOGGER.info("\t{} {}", cfc.getSelection(), cfc.getPredicate());
} else {
LOGGER.info("\t{} {}", StringUtils.join(cfc.getSelection(), ','), cfc.getPredicate());
}
}
}
boolean updated = false;
View.Builder builder = new View.Builder();
for (final String group : derivedView.getEntityGroups()) {
if (intersection.contains(group)) {
if (null != groupToFunctions.get(group)) {
final ViewElementDefinition ved = new ViewElementDefinition.Builder().merge(derivedView.getEntity(group)).postAggregationFilterFunctions(groupToFunctions.get(group)).build();
LOGGER.info("Adding the following filter functions to the view for group {}:", group);
for (final TupleAdaptedPredicate<String, ?> cfc : groupToFunctions.get(group)) {
if (null != cfc.getSelection()) {
LOGGER.info("\t{} {}", cfc.getSelection(), cfc.getPredicate());
} else {
LOGGER.info("\t{} {}", StringUtils.join(cfc.getSelection(), ','), cfc.getPredicate());
}
}
builder = builder.entity(group, ved);
updated = true;
} else {
LOGGER.info("Not adding any filter functions to the view for group {}", group);
}
}
}
for (final String group : derivedView.getEdgeGroups()) {
if (intersection.contains(group)) {
if (null != groupToFunctions.get(group)) {
final ViewElementDefinition ved = new ViewElementDefinition.Builder().merge(derivedView.getEdge(group)).postAggregationFilterFunctions(groupToFunctions.get(group)).build();
LOGGER.info("Adding the following filter functions to the view for group {}:", group);
for (final TupleAdaptedPredicate<String, ?> cfc : groupToFunctions.get(group)) {
if (null != cfc.getSelection()) {
LOGGER.info("\t{} {}", cfc.getSelection(), cfc.getPredicate());
} else {
LOGGER.info("\t{} {}", StringUtils.join(cfc.getSelection(), ','), cfc.getPredicate());
}
}
builder = builder.edge(group, ved);
updated = true;
} else {
LOGGER.info("Not adding any filter functions to the view for group {}", group);
}
}
}
if (updated) {
((GraphFilters) operation).setView(builder.build());
} else {
((GraphFilters) operation).setView(derivedView);
}
return operation;
}
use of uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition in project Gaffer by gchq.
the class HBaseRetriever method deserialiseAndTransform.
private Element deserialiseAndTransform(final Cell cell) {
try {
Element element = serialisation.getElement(cell, includeMatchedVertex);
final ViewElementDefinition viewDef = operation.getView().getElement(element.getGroup());
if (null != viewDef) {
final ElementTransformer transformer = viewDef.getTransformer();
if (null != transformer) {
element = transformer.apply(element);
}
}
return element;
} catch (final SerialisationException e) {
throw new RuntimeException(e);
}
}
Aggregations