use of uk.gov.gchq.gaffer.operation.data.EdgeSeed in project Gaffer by gchq.
the class AbstractCoreKeyRangeFactory method getRange.
@SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST", justification = "If an element is not an Entity it must be an Edge")
@Override
public <T extends GetElementsOperation<?, ?>> List<Range> getRange(final ElementSeed elementSeed, final T operation) throws RangeFactoryException {
if (elementSeed instanceof EntitySeed) {
if (SeedMatchingType.EQUAL.equals(operation.getSeedMatching()) && !operation.isIncludeEntities()) {
throw new IllegalArgumentException("When doing querying by ID, you should only provide an EntitySeed seed if you also set includeEntities flag to true");
}
return getRange(((EntitySeed) elementSeed).getVertex(), operation, operation.getIncludeEdges());
} else {
if (!operation.isIncludeEntities() && IncludeEdgeType.NONE == operation.getIncludeEdges()) {
throw new IllegalArgumentException("Need to get either Entities and/or Edges when getting Elements");
}
final EdgeSeed edgeSeed = (EdgeSeed) elementSeed;
final List<Range> ranges = new ArrayList<>();
if (IncludeEdgeType.ALL == operation.getIncludeEdges() || (IncludeEdgeType.DIRECTED == operation.getIncludeEdges() && edgeSeed.isDirected()) || (IncludeEdgeType.UNDIRECTED == operation.getIncludeEdges() && !edgeSeed.isDirected())) {
// Get Edges with the given EdgeSeed - This is applicable for
// EQUALS and RELATED seed matching.
ranges.add(new Range(getKeyFromEdgeSeed(edgeSeed, operation, false), true, getKeyFromEdgeSeed(edgeSeed, operation, true), true));
}
if (SeedMatchingType.RELATED.equals(operation.getSeedMatching()) && operation.isIncludeEntities()) {
// Get Entities related to EdgeSeeds
ranges.addAll(getRange(edgeSeed.getSource(), operation, IncludeEdgeType.NONE));
ranges.addAll(getRange(edgeSeed.getDestination(), operation, IncludeEdgeType.NONE));
}
return ranges;
}
}
use of uk.gov.gchq.gaffer.operation.data.EdgeSeed in project Gaffer by gchq.
the class LoadAndQuery12 method run.
public Iterable<Entity> run() throws OperationException {
// [user] Create a user
// ---------------------------------------------------------
final User user = new User("user01");
// ---------------------------------------------------------
// [graph] create a graph using our schema and store properties
// ---------------------------------------------------------
final Graph graph = new Graph.Builder().addSchemas(getSchemas()).storeProperties(getStoreProperties()).build();
// ---------------------------------------------------------
// [add] add the edges to the graph
// ---------------------------------------------------------
final Set<String> dummyData = Collections.singleton("");
final OperationChain addOpChain = new OperationChain.Builder().first(new GenerateElements.Builder<String>().generator(new DataGenerator12()).objects(dummyData).build()).then(new AddElements()).build();
graph.execute(addOpChain, user);
// ---------------------------------------------------------
log("Added the edge A-B 1000 times each time with a ReservoirItemsUnion<String> containing a random string." + " Also added 500 edges X-Y0, X-Y1, ..., X-Y499 each and for each an Entity on X with a" + " ReservoirItemsUnion<String> containing the destination node.");
// [get red edge] Get the red edge
// ---------------------------------------------------------
final GetAllEdges getAllEdges = new GetAllEdges.Builder().view(new View.Builder().edge("red").build()).build();
final Iterable<Edge> allEdges = graph.execute(getAllEdges, user);
// ---------------------------------------------------------
log("\nThe red edge A-B:");
for (final Edge edge : allEdges) {
log("GET_A-B_EDGE_RESULT", edge.toString());
}
// [get strings sample from the red edge] Get the edge A-B and print out the sample of strings
// ---------------------------------------------------------
final GetEdges<EdgeSeed> query = new GetEdges.Builder<EdgeSeed>().addSeed(new EdgeSeed("A", "B", false)).build();
final Iterable<Edge> edges = graph.execute(query, user);
final Edge edge = edges.iterator().next();
final ReservoirItemsSketch<String> stringsSketch = ((ReservoirItemsUnion<String>) edge.getProperty("stringsSample")).getResult();
final String[] samples = stringsSketch.getSamples();
final StringBuilder sb = new StringBuilder("10 samples: ");
for (int i = 0; i < 10 && i < samples.length; i++) {
if (i > 0) {
sb.append(", ");
}
sb.append(samples[i]);
}
// ---------------------------------------------------------
log("\nEdge A-B with a sample of the strings");
log("GET_SAMPLE_FOR_RED_EDGE", sb.toString());
// [get sample from the blue entity] Get the entity Y and print a sample of the neighbours
// ---------------------------------------------------------
final GetEntities<EntitySeed> query2 = new GetEntities.Builder<EntitySeed>().addSeed(new EntitySeed("X")).build();
final Iterable<Entity> entities = graph.execute(query2, user);
final Entity entity = entities.iterator().next();
final ReservoirItemsSketch<String> neighboursSketch = ((ReservoirItemsUnion<String>) entity.getProperty("neighboursSample")).getResult();
final String[] neighboursSample = neighboursSketch.getSamples();
sb.setLength(0);
sb.append("10 samples: ");
for (int i = 0; i < 10 && i < neighboursSample.length; i++) {
if (i > 0) {
sb.append(", ");
}
sb.append(neighboursSample[i]);
}
// ---------------------------------------------------------
log("\nEntity for vertex X with a sample of its neighbouring vertices");
log("GET_SAMPLES_FOR_X_RESULT", sb.toString());
return null;
}
use of uk.gov.gchq.gaffer.operation.data.EdgeSeed in project Gaffer by gchq.
the class ExportIT method createEdges.
/**
* Adds edges dest[X] -> source[X+1]
*
* @return map of edges
*/
@Override
protected Map<EdgeSeed, Edge> createEdges() {
final Map<EdgeSeed, Edge> edges = super.createEdges();
for (int i = 0; i <= 10; i++) {
final Edge thirdEdge = new Edge(TestGroups.EDGE, DEST_DIR + i, SOURCE_DIR + (i + 1), true);
thirdEdge.putProperty(TestPropertyNames.INT, 1);
thirdEdge.putProperty(TestPropertyNames.COUNT, 1L);
addToMap(thirdEdge, edges);
}
return edges;
}
use of uk.gov.gchq.gaffer.operation.data.EdgeSeed in project Gaffer by gchq.
the class FilteringIT method testFilteringProperties.
@Test
@TraitRequirement(StoreTrait.PRE_AGGREGATION_FILTERING)
public void testFilteringProperties() throws OperationException {
// Given
final List<ElementSeed> seeds = Arrays.asList(new EntitySeed("A3"), new EdgeSeed("A5", "B5", false));
final GetElements<ElementSeed, Element> getElementsWithoutFiltering = new GetElements.Builder<>().seeds(seeds).build();
final GetElements<ElementSeed, Element> getElementsWithFiltering = new GetElements.Builder<>().seeds(seeds).view(new View.Builder().entity(TestGroups.ENTITY, new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select(IdentifierType.VERTEX.name()).execute(new IsEqual("A5")).build()).build()).edge(TestGroups.EDGE, new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select(TestPropertyNames.INT).execute(new IsLessThan(2)).build()).build()).build()).build();
// When - without filtering
final List<Element> resultsWithoutFiltering = Lists.newArrayList(graph.execute(getElementsWithoutFiltering, getUser()));
// When - with filtering
final List<Element> resultsWithFiltering = Lists.newArrayList(graph.execute(getElementsWithFiltering, getUser()));
// Then - without filtering
assertNotNull(resultsWithoutFiltering);
assertEquals(8, resultsWithoutFiltering.size());
assertThat(resultsWithoutFiltering, IsCollectionContaining.hasItems(getEdge("A3", "A3", false), getEdge("A3", "B3", false), getEdge("A3", "C3", false), getEdge("A3", "D3", false), getEdge("A5", "B5", false), getEntity("A5"), getEntity("B5")));
// Then - with filtering
assertNotNull(resultsWithFiltering);
assertEquals(6, resultsWithFiltering.size());
assertThat(resultsWithFiltering, IsCollectionContaining.hasItems(getEdge("A3", "A3", false), getEdge("A3", "B3", false), getEdge("A5", "B5", false), getEdge("A3", "D3", false), getEdge("A3", "C3", false), getEntity("A5")));
}
use of uk.gov.gchq.gaffer.operation.data.EdgeSeed in project Gaffer by gchq.
the class GeneratorsIT method shouldConvertFromDomainObjects.
@Test
public void shouldConvertFromDomainObjects() throws OperationException, UnsupportedEncodingException {
// Given
final OperationChain<Void> opChain = new OperationChain.Builder().first(new GenerateElements.Builder<DomainObject>().generator(new BasicGenerator()).objects(Arrays.asList(new EntityDomainObject(NEW_VERTEX, "1", null), new EdgeDomainObject(NEW_SOURCE, NEW_DEST, false, 1, 1L))).build()).then(new AddElements()).build();
// When - add
graph.execute(opChain, getUser());
// Then - check they were added correctly
final List<Element> results = Lists.newArrayList(graph.execute(new GetElements.Builder<>().addSeed(new EntitySeed(NEW_VERTEX)).addSeed(new EdgeSeed(NEW_SOURCE, NEW_DEST, false)).build(), getUser()));
final Edge expectedEdge = new Edge(TestGroups.EDGE, NEW_SOURCE, NEW_DEST, false);
expectedEdge.putProperty(TestPropertyNames.INT, 1);
expectedEdge.putProperty(TestPropertyNames.COUNT, 1L);
final Entity expectedEntity = new Entity(TestGroups.ENTITY, NEW_VERTEX);
expectedEntity.putProperty(TestPropertyNames.STRING, "1");
assertNotNull(results);
assertEquals(2, results.size());
assertThat(results, IsCollectionContaining.hasItems(expectedEntity, expectedEdge));
}
Aggregations