use of com.baidu.hugegraph.type.define.Directions in project incubator-hugegraph by apache.
the class RamTable method matched.
@Watched
public boolean matched(Query query) {
if (this.edgesSize() == 0L || this.loading) {
return false;
}
if (!query.resultType().isEdge() || !(query instanceof ConditionQuery)) {
return false;
}
ConditionQuery cq = (ConditionQuery) query;
int conditionsSize = cq.conditionsSize();
Object owner = cq.condition(HugeKeys.OWNER_VERTEX);
Directions direction = cq.condition(HugeKeys.DIRECTION);
Id label = cq.condition(HugeKeys.LABEL);
if (direction == null && conditionsSize > 1) {
for (Condition cond : cq.conditions()) {
if (cond.equals(BOTH_COND)) {
direction = Directions.BOTH;
break;
}
}
}
int matchedConds = 0;
if (owner != null) {
matchedConds++;
} else {
return false;
}
if (direction != null) {
matchedConds++;
}
if (label != null) {
matchedConds++;
}
return matchedConds == cq.conditionsSize();
}
use of com.baidu.hugegraph.type.define.Directions in project incubator-hugegraph by apache.
the class FusiformSimilarityTraverser method fusiformSimilarityForVertex.
private Set<Similar> fusiformSimilarityForVertex(HugeVertex vertex, Directions direction, String label, int minNeighbors, double alpha, int minSimilars, int top, String groupProperty, int minGroups, long degree, long capacity, boolean withIntermediary) {
boolean matched = this.matchMinNeighborCount(vertex, direction, label, minNeighbors, degree);
if (!matched) {
// Ignore current vertex if its neighbors number is not enough
return ImmutableSet.of();
}
Id labelId = this.getEdgeLabelId(label);
// Get similar nodes and counts
Iterator<Edge> edges = this.edgesOfVertex(vertex.id(), direction, labelId, degree);
Map<Id, MutableInt> similars = newMap();
MultivaluedMap<Id, Id> intermediaries = new MultivaluedHashMap<>();
Set<Id> neighbors = newIdSet();
while (edges.hasNext()) {
Id target = ((HugeEdge) edges.next()).id().otherVertexId();
if (neighbors.contains(target)) {
continue;
}
neighbors.add(target);
checkCapacity(capacity, ++this.accessed, "fusiform similarity");
Directions backDir = direction.opposite();
Iterator<Edge> backEdges = this.edgesOfVertex(target, backDir, labelId, degree);
Set<Id> currentSimilars = newIdSet();
while (backEdges.hasNext()) {
Id node = ((HugeEdge) backEdges.next()).id().otherVertexId();
if (currentSimilars.contains(node)) {
continue;
}
currentSimilars.add(node);
if (withIntermediary) {
intermediaries.add(node, target);
}
MutableInt count = similars.get(node);
if (count == null) {
count = new MutableInt(0);
similars.put(node, count);
checkCapacity(capacity, ++this.accessed, "fusiform similarity");
}
count.increment();
}
}
// Delete source vertex
assert similars.containsKey(vertex.id());
similars.remove(vertex.id());
if (similars.isEmpty()) {
return ImmutableSet.of();
}
// Match alpha
double neighborNum = neighbors.size();
Map<Id, Double> matchedAlpha = newMap();
for (Map.Entry<Id, MutableInt> entry : similars.entrySet()) {
double score = entry.getValue().intValue() / neighborNum;
if (score >= alpha) {
matchedAlpha.put(entry.getKey(), score);
}
}
if (matchedAlpha.size() < minSimilars) {
return ImmutableSet.of();
}
// Sorted and topN if needed
Map<Id, Double> topN;
if (top > 0) {
topN = topN(matchedAlpha, true, top);
} else {
topN = matchedAlpha;
}
// Filter by groupCount by property
if (groupProperty != null) {
Set<Object> values = newSet();
// Add groupProperty value of source vertex
values.add(vertex.value(groupProperty));
for (Id id : topN.keySet()) {
Vertex v = graph().vertices(id).next();
values.add(v.value(groupProperty));
}
if (values.size() < minGroups) {
return ImmutableSet.of();
}
}
// Construct result
Set<Similar> result = InsertionOrderUtil.newSet();
for (Map.Entry<Id, Double> entry : topN.entrySet()) {
Id similar = entry.getKey();
double score = entry.getValue();
List<Id> inters = withIntermediary ? intermediaries.get(similar) : ImmutableList.of();
result.add(new Similar(similar, score, inters));
}
return result;
}
use of com.baidu.hugegraph.type.define.Directions in project incubator-hugegraph by apache.
the class HugeVertexStep method constructEdgesQuery.
protected ConditionQuery constructEdgesQuery(Traverser.Admin<Vertex> traverser) {
HugeGraph graph = TraversalUtil.getGraph(this);
// Query for edge with conditions(else conditions for vertex)
boolean withEdgeCond = this.withEdgeCondition();
boolean withVertexCond = this.withVertexCondition();
Id vertex = (Id) traverser.get().id();
Directions direction = Directions.convert(this.getDirection());
Id[] edgeLabels = graph.mapElName2Id(this.getEdgeLabels());
LOG.debug("HugeVertexStep.edges(): vertex={}, direction={}, " + "edgeLabels={}, has={}", vertex, direction, edgeLabels, this.hasContainers);
ConditionQuery query = GraphTransaction.constructEdgesQuery(vertex, direction, edgeLabels);
// Query by sort-keys
if (withEdgeCond && edgeLabels.length == 1) {
TraversalUtil.fillConditionQuery(query, this.hasContainers, graph);
if (!GraphTransaction.matchPartialEdgeSortKeys(query, graph)) {
// Can't query by sysprop and by index (HugeGraph-749)
query.resetUserpropConditions();
} else if (GraphTransaction.matchFullEdgeSortKeys(query, graph)) {
// All sysprop conditions are in sort-keys
withEdgeCond = false;
} else {
// Partial sysprop conditions are in sort-keys
assert query.userpropKeys().size() > 0;
}
}
// Query by has(id)
if (query.idsSize() > 0) {
// Ignore conditions if query by edge id in has-containers
// FIXME: should check that the edge id matches the `vertex`
query.resetConditions();
LOG.warn("It's not recommended to query by has(id)");
}
/*
* Unset limit when needed to filter property after store query
* like query: outE().has(k,v).limit(n)
* NOTE: outE().limit(m).has(k,v).limit(n) will also be unset limit,
* Can't unset limit if query by paging due to page position will be
* exceeded when reaching the limit in tinkerpop layer
*/
if (withEdgeCond || withVertexCond) {
com.baidu.hugegraph.util.E.checkArgument(!this.queryInfo().paging(), "Can't query by paging " + "and filtering");
this.queryInfo().limit(Query.NO_LIMIT);
}
query = this.injectQueryInfo(query);
return query;
}
use of com.baidu.hugegraph.type.define.Directions in project incubator-hugegraph by apache.
the class PersonalRankTraverser method personalRank.
public Map<Id, Double> personalRank(Id source, String label, WithLabel withLabel) {
E.checkNotNull(source, "source vertex id");
this.checkVertexExist(source, "source vertex");
E.checkArgumentNotNull(label, "The edge label can't be null");
Map<Id, Double> ranks = newMap();
ranks.put(source, 1.0);
Id labelId = this.graph().edgeLabel(label).id();
Directions dir = this.getStartDirection(source, label);
Set<Id> outSeeds = newIdSet();
Set<Id> inSeeds = newIdSet();
if (dir == Directions.OUT) {
outSeeds.add(source);
} else {
inSeeds.add(source);
}
Set<Id> rootAdjacencies = newIdSet();
for (long i = 0; i < this.maxDepth; i++) {
Map<Id, Double> newRanks = this.calcNewRanks(outSeeds, inSeeds, labelId, ranks);
ranks = this.compensateRoot(source, newRanks);
if (i == 0) {
rootAdjacencies.addAll(ranks.keySet());
}
}
// Remove directly connected neighbors
removeAll(ranks, rootAdjacencies);
// Remove unnecessary label
if (withLabel == WithLabel.SAME_LABEL) {
removeAll(ranks, dir == Directions.OUT ? inSeeds : outSeeds);
} else if (withLabel == WithLabel.OTHER_LABEL) {
removeAll(ranks, dir == Directions.OUT ? outSeeds : inSeeds);
}
return ranks;
}
Aggregations