Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *
   *  *  Copyright 2014 Orient Technologies LTD (info(at)orientechnologies.com)
   *  *
   *  *  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.
  *  *
  *  * For more information: http://www.orientechnologies.com
  *
  */
 package com.orientechnologies.orient.graph.sql.functions;
 
 
Dijkstra's algorithm describes how to find the cheapest path from one node to another node in a directed weighted graph. The first parameter is source record. The second parameter is destination record. The third parameter is a name of property that represents 'weight'. If property is not defined in edge or is null, distance between vertexes are 0.

Author(s):
Luca Garulli (l.garulli--at--orientechnologies.com)
 
 public class OSQLFunctionDijkstra extends OSQLFunctionPathFinder {
   public static final String NAME = "dijkstra";
 
   private String             paramWeightFieldName;
 
   public OSQLFunctionDijkstra() {
     super(, 3, 4);
   }
 
   public LinkedList<OrientVertexexecute(Object iThisOIdentifiable iCurrentRecordObject iCurrentResult,
       final Object[] iParamsOCommandContext iContext) {
     final OModifiableBoolean shutdownFlag = new OModifiableBoolean();
     final OrientBaseGraph graph = OGraphCommandExecutorSQLFactory.getGraph(falseshutdownFlag);
     try {
 
       final ORecord record = (ORecord) (iCurrentRecord != null ? iCurrentRecord.getRecord() : null);
 
       Object source = iParams[0];
       if (OMultiValue.isMultiValue(source)) {
         if (OMultiValue.getSize(source) > 1)
           throw new IllegalArgumentException("Only one sourceVertex is allowed");
         source = OMultiValue.getFirstValue(source);
       }
        = graph.getVertex(OSQLHelper.getValue(sourcerecordiContext));
 
       Object dest = iParams[1];
       if (OMultiValue.isMultiValue(dest)) {
         if (OMultiValue.getSize(dest) > 1)
           throw new IllegalArgumentException("Only one destinationVertex is allowed");
         dest = OMultiValue.getFirstValue(dest);
       }
        = graph.getVertex(OSQLHelper.getValue(destrecordiContext));
 
        = OStringSerializerHelper.getStringContent(iParams[2]);
       if (iParams.length > 3)
          = Direction.valueOf(iParams[3].toString().toUpperCase());
 
       return super.execute(iContext);
     } finally {
       if (shutdownFlag.getValue())
         graph.shutdown(false);
     }
   }
 
   public String getSyntax() {
     return "dijkstra(<sourceVertex>, <destinationVertex>, <weightEdgeFieldName>, [<direction>])";
   }
  protected float getDistance(final OrientVertex nodefinal OrientVertex target) {
    final Iterator<Edgeedges = node.getEdges(target).iterator();
    if (edges.hasNext()) {
      final Edge e = edges.next();
      if (e != null) {
        final Object fieldValue = e.getProperty();
        if (fieldValue != null)
          if (fieldValue instanceof Float)
            return (FloatfieldValue;
          else if (fieldValue instanceof Number)
            return ((NumberfieldValue).floatValue();
      }
    }
    return ;
  }
  protected boolean isVariableEdgeWeight() {
    return true;
  }
New to GrepCode? Check out our FAQ X