Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2017-2020 Crown Copyright
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package uk.gov.gchq.gaffer.data.graph;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import uk.gov.gchq.gaffer.commonutil.stream.Streams;
import uk.gov.gchq.gaffer.data.element.Edge;
import uk.gov.gchq.gaffer.data.element.Entity;
import uk.gov.gchq.gaffer.data.element.id.EntityId;
import java.util.AbstractMap;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import static uk.gov.gchq.gaffer.commonutil.CollectionUtil.distinct;
/**
* A {@code Walk} describes a graph traversal which begins at a vertex and
* comprises of:
a sequence of {@link Edge} objects, where the source of
* one edge must follow on from the destination of the previous
an
* optional sequence of {@link Entity} objects which are associated to each of
* the vertices visited on the Walk. Vertices with no associated entities are
* still represented, but only as empty collections.
*
* For example, a Walk through a simple graph could look like:
*
*
* {@code
* A --> B --> C
* \ \
* \ (Entity2, Entity3)
* \
* (Entity1)
*
* Edges: A -> B, B -> C
* Entities: [], [Entity1], [Entity2, Entity3]
* }
*
*/
public class Walk implements Iterable> {
private final List> edges;
private final List>> entities;
/**
* Private builder constructor.
*
* @param builder the builder
*/
private Walk(final Builder builder) {
this.entities = builder.entities;
this.edges = builder.edges;
}
/**
* Constructor used by Jackson.
*
* @param edges the edges to add to the walk
* @param entities the entities to add to the walk
*/
@JsonCreator
public Walk(@JsonProperty("edges") final List> edges,
@JsonProperty("entities") final List>> entities) {
this.edges = edges;
this.entities = entities;
}
/**
* Retrieve all of the entities associated with a particular vertex on the
* walk.
*
* @param vertex the vertex of interest
* @return a {@link Set} of all of the entities associated with the vertex
*/
@JsonIgnore
public Set getEntitiesForVertex(final Object vertex) {
return entities.stream()
.filter(e -> e.getKey().equals(vertex))
.map(Entry::getValue)
.flatMap(Set::stream)
.collect(toSet());
}
/**
* Get all of the entities at some distance from the start of the walk.
*
* @param n the distance from the start of the walk (in terms of the number
* of edges that would have to be traversed)
* @return the entities at the specified distance
*/
@JsonIgnore
public Set getEntitiesAtDistance(final int n) {
return entities.get(n).getValue();
}
public List> getEdges() {
return edges;
}
@JsonGetter("entities")
public List>> getEntitiesAsEntries() {
return entities;
}
@JsonIgnore
public List> getEntities() {
return entities.stream().map(Entry::getValue).collect(toList());
}
/**
* Get an ordered {@link List} of the vertices on the walk.
*
* This will include any repeated vertices.
*
* @return a list of the vertices on the walk
*/
@JsonIgnore
public List