use of uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView in project Gaffer by gchq.
the class NamedViewResolver method resolveView.
private View resolveView(final String namedViewName, final Map<String, Object> parameters, final Context context) {
final NamedViewDetail cachedNamedView;
try {
cachedNamedView = cache.getNamedView(namedViewName, context.getUser());
} catch (final CacheOperationFailedException e) {
throw new RuntimeException(e);
}
View resolvedView;
if (null == cachedNamedView) {
resolvedView = new View();
} else {
resolvedView = cachedNamedView.getView(parameters);
if (resolvedView instanceof NamedView) {
((NamedView) resolvedView).setName(null);
if (CollectionUtils.isNotEmpty(((NamedView) resolvedView).getMergedNamedViewNames())) {
final View.Builder viewBuilder = new View.Builder();
viewBuilder.merge(resolvedView);
for (final String name : ((NamedView) resolvedView).getMergedNamedViewNames()) {
viewBuilder.merge(resolveView(name, parameters, context));
}
resolvedView = viewBuilder.build();
}
}
}
return resolvedView;
}
use of uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView in project Gaffer by gchq.
the class NamedViewResolver method resolveView.
private View resolveView(final NamedView namedView, final Context context) {
View resolvedView = resolveView(namedView.getName(), namedView.getParameters(), context);
if (CollectionUtils.isNotEmpty(namedView.getMergedNamedViewNames())) {
final View.Builder viewBuilder = new View.Builder();
viewBuilder.merge(resolvedView);
for (final String name : namedView.getMergedNamedViewNames()) {
viewBuilder.merge(resolveView(name, namedView.getParameters(), context));
}
resolvedView = viewBuilder.build();
}
namedView.setName(null);
return new View.Builder().merge(resolvedView).merge(namedView).build();
}
use of uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView in project gaffer-doc by gchq.
the class NamedViews method run.
@Override
public CloseableIterable<? extends Element> run() throws OperationException, IOException {
// / [graph] create a graph using our schema and store properties
// ---------------------------------------------------------
final Graph graph = new Graph.Builder().config(getDefaultGraphConfig()).addSchemas(StreamUtil.openStreams(getClass(), schemaPath)).storeProperties(getDefaultStoreProperties()).build();
// ---------------------------------------------------------
// [user] Create a user
// ---------------------------------------------------------
final User user = new User("user01");
// ---------------------------------------------------------
// [add] Create a data generator and add the edges to the graph using an operation chain consisting of:
// generateElements - generating edges from the data (note these are directed edges)
// addElements - add the edges to the graph
// ---------------------------------------------------------
final OperationChain<Void> addOpChain = new OperationChain.Builder().first(new GenerateElements.Builder<String>().generator(new RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator()).input(IOUtils.readLines(StreamUtil.openStream(getClass(), "RoadAndRoadUseWithTimesAndCardinalities/data.txt"))).build()).then(new AddElements()).build();
graph.execute(addOpChain, user);
// ---------------------------------------------------------
// [add named view] add a NamedView to be reused
// ---------------------------------------------------------
final AddNamedView addNamedView = new AddNamedView.Builder().name("RoadUse edges").description("NamedView to get only RoadUse edges").view(new View.Builder().edge("RoadUse").build()).overwrite(false).build();
// ---------------------------------------------------------
printJsonAndPython("ADD_NAMED_VIEW", addNamedView);
graph.execute(addNamedView, user);
// [get all named views] Get all named views
// ---------------------------------------------------------
final GetAllNamedViews getAllNamedViews = new GetAllNamedViews();
// ---------------------------------------------------------
printJsonAndPython("GET_ALL_NAMED_VIEWS", new GetAllNamedViews());
final CloseableIterable<NamedViewDetail> namedViewDetails = graph.execute(getAllNamedViews, user);
for (final NamedViewDetail detail : namedViewDetails) {
print("ALL_NAMED_VIEW", detail.toString());
}
// [get elements with named view] create the operation using the NamedView
// ---------------------------------------------------------
final GetElements operation = new GetElements.Builder().view(new NamedView.Builder().name("RoadUse edges").build()).input(new EntitySeed("10")).build();
// ---------------------------------------------------------
printJsonAndPython("GET_ELEMENTS_WITH_NAMED_VIEW", operation);
final CloseableIterable<? extends Element> namedViewResults = graph.execute(operation, user);
for (final Object result : namedViewResults) {
print("GET_ELEMENTS_WITH_NAMED_VIEW_RESULTS", result.toString());
}
// [add named view with parameters] create an operation chain to be executed as a named operation
// with parameters
// ---------------------------------------------------------
final String viewJson = "{" + " \"edges\" : {" + " \"RoadUse\" : { " + " \"preAggregationFilterFunctions\" : [ {" + " \"predicate\" : {" + " \"class\" : \"uk.gov.gchq.koryphe.impl.predicate.IsMoreThan\"," + " \"orEqualTo\" : false," + " \"value\": {" + " \"java.lang.Long\" : \"${isMoreThan}\"" + " }" + " }," + " \"selection\" : [ \"${property}\" ]" + " } ] }" + " }," + " \"entities\" : { }" + "}";
final ViewParameterDetail propertyParam = new ViewParameterDetail.Builder().description("Property to select").valueClass(String.class).required(true).build();
final ViewParameterDetail valueParam = new ViewParameterDetail.Builder().description("Value for the is more than predicate").defaultValue(0L).valueClass(Long.class).build();
final Map<String, ViewParameterDetail> paramDetailMap = Maps.newHashMap();
paramDetailMap.put("property", propertyParam);
paramDetailMap.put("isMoreThan", valueParam);
final AddNamedView addNamedViewWithParams = new AddNamedView.Builder().name("customCountView").description("named View with count param").view(viewJson).parameters(paramDetailMap).overwrite(true).build();
// ---------------------------------------------------------
printJsonAndPython("ADD_NAMED_VIEW_WITH_PARAMETERS", addNamedViewWithParams);
graph.execute(addNamedViewWithParams, user);
// [get elements with named view with parameters] create the named operation with a parameter
// ---------------------------------------------------------
final Map<String, Object> paramMap = Maps.newHashMap();
paramMap.put("property", "count");
paramMap.put("isMoreThan", 1L);
final GetElements operationUsingParams = new GetElements.Builder().view(new NamedView.Builder().name("customCountView").parameters(paramMap).build()).input(new EntitySeed("10")).build();
// ---------------------------------------------------------
printJsonAndPython("GET_ELEMENTS_WITH_NAMED_VIEW_WITH_PARAMETERS", operationUsingParams);
final CloseableIterable<? extends Element> namedViewResultsWithParams = graph.execute(operationUsingParams, user);
for (final Object result : namedViewResultsWithParams) {
print("GET_ELEMENTS_WITH_NAMED_VIEW_WITH_PARAMETERS_RESULTS", result.toString());
}
return namedViewResultsWithParams;
}
use of uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView in project Gaffer by gchq.
the class AddNamedViewHandlerTest method shouldNotAddNestedNamedView.
@Test
public void shouldNotAddNestedNamedView() throws OperationException {
final NamedView nestedNamedView = new NamedView.Builder().name(testNamedViewName + 1).edge(TestGroups.EDGE).build();
addNamedView = new AddNamedView.Builder().name(testNamedViewName).view(nestedNamedView).overwrite(false).build();
try {
handler.doOperation(addNamedView, context, store);
} catch (final OperationException e) {
assertTrue(e.getMessage().equals("NamedView can not be nested within NamedView"));
}
}
use of uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView in project Gaffer by gchq.
the class OperationView method setViews.
/**
* Merges a list of {@link View}s, including the current if set, and sets the {@link OperationView} to the new merged {@link View}.
*
* @param views the list of views to merge
*/
default void setViews(final List<View> views) {
if (null != views) {
boolean isNamedView = null != getView() && getView() instanceof NamedView;
if (!isNamedView) {
for (final View view : views) {
if (null != view && view instanceof NamedView) {
isNamedView = true;
break;
}
}
}
final View.BaseBuilder builder = isNamedView ? new NamedView.Builder() : new View.Builder();
if (null != getView()) {
builder.merge(getView());
}
views.forEach(view -> builder.merge(view));
setView(builder.build());
}
}
Aggregations