Skip to content

Commit

Permalink
Fix NPE on dijkstra() and astar() functions
Browse files Browse the repository at this point in the history
Resolves: #7818
  • Loading branch information
luigidellaquila committed Oct 30, 2017
1 parent 566765c commit d31b0ee
Showing 1 changed file with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
import java.util.*;

/**
* A*'s algorithm describes how to find the cheapest path from one node to another node in a directed weighted graph with husrestic function.
* A*'s algorithm describes how to find the cheapest path from one node to another node in a directed weighted graph with husrestic
* function.
* <p>
* The first parameter is source record. The second parameter is destination record. The third parameter is a name of property that
* represents 'weight' and fourth represnts the map of options.
Expand Down Expand Up @@ -72,7 +73,8 @@ public LinkedList<OrientVertex> execute(final Object iThis, final OIdentifiable
final OSQLFunctionAstar context = this;
return OGraphCommandExecutorSQLFactory
.runWithAnyGraph(new OGraphCommandExecutorSQLFactory.GraphCallBack<LinkedList<OrientVertex>>() {
@Override public LinkedList<OrientVertex> call(final OrientBaseGraph graph) {
@Override
public LinkedList<OrientVertex> call(final OrientBaseGraph graph) {

final ORecord record = iCurrentRecord != null ? iCurrentRecord.getRecord() : null;

Expand All @@ -98,6 +100,9 @@ public LinkedList<OrientVertex> execute(final Object iThis, final OIdentifiable
bindAdditionalParams(iParams[3], context);
}
iContext.setVariable("getNeighbors", 0);
if (paramSourceVertex == null || paramDestinationVertex == null) {
return new LinkedList<OrientVertex>();
}
return internalExecute(iContext, graph);
}
});
Expand Down Expand Up @@ -173,10 +178,10 @@ private OrientVertex getNeighbor(OrientVertex current, OrientEdge neighborEdge,
}

private OrientVertex toVertex(OIdentifiable outVertex, OrientBaseGraph graph) {
if(outVertex==null){
if (outVertex == null) {
return null;
}
if (outVertex instanceof OrientVertex){
if (outVertex instanceof OrientVertex) {
return (OrientVertex) outVertex;
}
return graph.getVertex(outVertex);
Expand Down Expand Up @@ -226,8 +231,8 @@ private void bindAdditionalParams(Object additionalParams, OSQLFunctionAstar ctx
ctx.paramDFactor = doubleOrDefault(mapParams.get(OSQLFunctionAstar.PARAM_D_FACTOR), ctx.paramDFactor);
if (mapParams.get(OSQLFunctionAstar.PARAM_HEURISTIC_FORMULA) != null) {
if (mapParams.get(OSQLFunctionAstar.PARAM_HEURISTIC_FORMULA) instanceof String) {
ctx.paramHeuristicFormula = HeuristicFormula
.valueOf(stringOrDefault(mapParams.get(OSQLFunctionAstar.PARAM_HEURISTIC_FORMULA), "MANHATAN").toUpperCase(Locale.ENGLISH));
ctx.paramHeuristicFormula = HeuristicFormula.valueOf(
stringOrDefault(mapParams.get(OSQLFunctionAstar.PARAM_HEURISTIC_FORMULA), "MANHATAN").toUpperCase(Locale.ENGLISH));
} else {
ctx.paramHeuristicFormula = (HeuristicFormula) mapParams.get(OSQLFunctionAstar.PARAM_HEURISTIC_FORMULA);
}
Expand All @@ -241,11 +246,13 @@ public String getSyntax() {
return "astar(<sourceVertex>, <destinationVertex>, <weightEdgeFieldName>, [<options>]) \n // options : {direction:\"OUT\",edgeTypeNames:[] , vertexAxisNames:[] , parallel : false , tieBreaker:true,maxDepth:99999,dFactor:1.0,customHeuristicFormula:'custom_Function_Name_here' }";
}

@Override public Object getResult() {
@Override
public Object getResult() {
return getPath();
}

@Override protected double getDistance(final OrientVertex node, final OrientVertex parent, final OrientVertex target) {
@Override
protected double getDistance(final OrientVertex node, final OrientVertex parent, final OrientVertex target) {
final Iterator<Edge> edges = node.getEdges(target, paramDirection).iterator();
if (edges.hasNext()) {
final Edge e = edges.next();
Expand Down Expand Up @@ -274,11 +281,13 @@ else if (fieldValue instanceof Number)
return MIN;
}

@Override public boolean aggregateResults() {
@Override
public boolean aggregateResults() {
return false;
}

@Override protected double getHeuristicCost(final OrientVertex node, OrientVertex parent, final OrientVertex target) {
@Override
protected double getHeuristicCost(final OrientVertex node, OrientVertex parent, final OrientVertex target) {
double hresult = 0.0;

if (paramVertexAxisNames.length == 0) {
Expand Down Expand Up @@ -375,7 +384,8 @@ else if (fieldValue instanceof Number)

}

@Override protected boolean isVariableEdgeWeight() {
@Override
protected boolean isVariableEdgeWeight() {
return true;
}

Expand Down

0 comments on commit d31b0ee

Please sign in to comment.