Java Code Examples for org.jgrapht.GraphPath#getEdgeList()

The following examples show how to use org.jgrapht.GraphPath#getEdgeList() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ShortestPathPointRouter.java    From openAGV with Apache License 2.0 6 votes vote down vote up
private List<Route.Step> translateToSteps(GraphPath<String, ModelEdge> graphPath) {
  List<ModelEdge> edges = graphPath.getEdgeList();
  List<Route.Step> result = new ArrayList<>(edges.size());

  int routeIndex = 0;
  for (ModelEdge edge : edges) {
    Point sourcePoint = points.get(graphPath.getGraph().getEdgeSource(edge));
    Point destPoint = points.get(graphPath.getGraph().getEdgeTarget(edge));

    result.add(new Route.Step(edge.getModelPath(),
                              sourcePoint,
                              destPoint,
                              orientation(edge, sourcePoint),
                              routeIndex));
    routeIndex++;
  }

  return result;
}
 
Example 2
Source File: DefaultTermGraph.java    From cocolian-nlp with Apache License 2.0 5 votes vote down vote up
@Override
public TermPath createPath(GraphPath<POSTerm, TermEdge> path) {
	TermPath termPath = this.createPath(path.getStartVertex());
	for (TermEdge edge : path.getEdgeList())
		termPath.extend(edge);
	return termPath;

}
 
Example 3
Source File: QbeQueryResource.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
private boolean hasAmbiguities(Query filteredQuery) {

		for (Map.Entry<IModelField, Set<IQueryField>> modelFields : getModelFieldsMap(filteredQuery).entrySet()) {

			List<GraphPath<IModelEntity, Relationship>> shortestPaths = getShortestPaths(filteredQuery, modelFields.getKey().getParent());
			if (shortestPaths.size() > 1) {
				logger.debug("Dump ambiguos shortest paths:");
				int i = 1;
				for (GraphPath<IModelEntity, Relationship> path : shortestPaths) {
					logger.debug("--- Ambiguos path #" + i);
					logger.debug("Start vertex: " + path.getStartVertex().getName());
					List<Relationship> edgeList = path.getEdgeList();
					logger.debug("\t   V   ");
					for (Relationship rel : edgeList) {
						logger.debug("\t   .   ");
						logger.debug("\t " + rel.getSourceEntity().getName() + " -> " + rel.getTargetEntity().getName());
						logger.debug("\t   .   ");
					}
					logger.debug("\t   X   ");
					logger.debug("End vertex: " + path.getEndVertex().getName());
					i++;
				}
				return true;
			}
			;
		}

		return false;
	}
 
Example 4
Source File: PathInspector.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 
 * @param path
 * @param vertex
 * @return
 */
private boolean containsVertex(GraphPath<IModelEntity,Relationship> path, IModelEntity vertex){
	List<Relationship> edges = path.getEdgeList();
	for (int i = 0; i < edges.size(); i++) {
		Relationship r =(Relationship) edges.get(i);
		if(r.getSourceEntity().equals(vertex) || r.getTargetEntity().equals(vertex) ){
			return true;
		}
	}
	return false;
}
 
Example 5
Source File: QueryGraphBuilder.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
private void addPathToGraph(Graph<IModelEntity, Relationship> graph, GraphPath<IModelEntity, Relationship> path ){
	logger.debug("IN");
	List<Relationship> edges = path.getEdgeList();
	if(edges!=null){
		for(int i=0; i<edges.size(); i++){
			Relationship edge = (Relationship)edges.get(i);
			addEdgeToGraph(graph, edge);
		}
	}
	logger.debug("OUT");
}
 
Example 6
Source File: RootEntitiesGraph.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
public Set<Relationship> getConnectingRelatiosnhips(Set<IModelEntity> entities) {

		Set<Relationship> connectingRelatiosnhips = new HashSet<Relationship>();

		Set<IModelEntity> connectedEntities = new HashSet<IModelEntity>();

		Iterator<IModelEntity> it = entities.iterator();
		connectedEntities.add(it.next());

		while (it.hasNext()) {
			IModelEntity entity = it.next();
			if (connectedEntities.contains(entity))
				continue;
			GraphPath minimumPath = null;
			double minPathLength = Double.MAX_VALUE;
			for (IModelEntity connectedEntity : connectedEntities) {
				DijkstraShortestPath dsp = new DijkstraShortestPath(rootEntitiesGraph, entity, connectedEntity);
				double pathLength = dsp.getPathLength();
				if (minPathLength > pathLength) {
					minPathLength = pathLength;
					minimumPath = dsp.getPath();
				}
			}
			List<Relationship> relationships = minimumPath.getEdgeList();
			connectingRelatiosnhips.addAll(relationships);
			for (Relationship relatioship : relationships) {
				connectedEntities.add(rootEntitiesGraph.getEdgeSource(relatioship));
				connectedEntities.add(rootEntitiesGraph.getEdgeTarget(relatioship));
			}
		}

		for (Relationship r : connectingRelatiosnhips) {
			IModelEntity source = rootEntitiesGraph.getEdgeSource(r);
			IModelEntity target = rootEntitiesGraph.getEdgeTarget(r);
			logger.error(source.getName() + " -> " + target.getName());
		}

		return connectingRelatiosnhips;
	}
 
Example 7
Source File: GraphUtilities.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
private static Set<String> getRelationsSet(GraphPath<IModelEntity, Relationship> path1) {
	Set<String> relations = new HashSet<String>();
	List<Relationship> edges = path1.getEdgeList();
	for (int i = 0; i < edges.size(); i++) {
		relations.add(edges.get(i).getId());
	}
	return relations;

}
 
Example 8
Source File: ShortestPathsCoverGraph.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
public Subgraph<IModelEntity, Relationship, Graph<IModelEntity, Relationship>> getCoverSubGraph(Graph<IModelEntity, Relationship> rootEntitiesGraph,
		Set<IModelEntity> entities) {
	Subgraph<IModelEntity, Relationship, Graph<IModelEntity, Relationship>> subGraph = null;
	Iterator<IModelEntity> it = entities.iterator();
	Set<Relationship> connectingRelatiosnhips = new HashSet<Relationship>();

	Set<IModelEntity> connectedEntities = new HashSet<IModelEntity>();
	if (it.hasNext())
		connectedEntities.add(it.next());

	// build the subgraph that contains all the nodes in entities
	logger.debug("Building the subgraph that contains only the entities involved in the query");
	while (it.hasNext()) {
		IModelEntity entity = it.next();
		for (IModelEntity otherEntity : entities) {

			GraphPath<IModelEntity, Relationship> minimumPath = null;
			if (otherEntity.getParent() == null) {
				// check the path from entity to connectedEntity
				DijkstraShortestPath<IModelEntity, Relationship> dsp = new DijkstraShortestPath(rootEntitiesGraph, entity, otherEntity);
				double pathLength = dsp.getPathLength();
				minimumPath = dsp.getPath();

				if (rootEntitiesGraph instanceof DirectedGraph) {
					// check the path from connectedEntity to entity
					DijkstraShortestPath dsp2 = new DijkstraShortestPath(rootEntitiesGraph, otherEntity, entity);
					double pathLength2 = dsp2.getPathLength();
					if (pathLength > pathLength2) {
						minimumPath = dsp2.getPath();
					}
				}

				if (minimumPath != null) {
					List<Relationship> pathRelations = minimumPath.getEdgeList();

					if (pathRelations != null) {
						for (int i = 0; i < pathRelations.size(); i++) {

							Relationship connectingRel = pathRelations.get(i);

							if (connectingRel != null && connectingRel.isConsidered() != false) {
								connectingRelatiosnhips.add(connectingRel);
								connectedEntities.add(connectingRel.getSourceEntity());
								connectedEntities.add(connectingRel.getTargetEntity());
							}

						}
					}
				}
			}
		}
	}

	if (rootEntitiesGraph instanceof DirectedGraph) {
		subGraph = (Subgraph) new DirectedSubgraph<IModelEntity, Relationship>((DirectedGraph) rootEntitiesGraph, connectedEntities,
				connectingRelatiosnhips);
	} else {
		subGraph = (Subgraph) new UndirectedSubgraph<IModelEntity, Relationship>((UndirectedGraph) rootEntitiesGraph, connectedEntities,
				connectingRelatiosnhips);
	}
	logger.debug("Subgraph built");

	return subGraph;
}
 
Example 9
Source File: RepresentationHandler.java    From DDF with Apache License 2.0 4 votes vote down vote up
/**
 * Converts from existing representation(s) to the desired representation, which has the specified dataType.
 * <p/>
 * The base representation returns only the default representation if the dataType matches the default type. Otherwise
 * it returns null.
 *
 * @param dataType
 * @return
 */
private Representation createRepresentation(Representation representation) throws DDFException {

  Collection<Representation> vertices = this.mReps.values();
  //List<GraphPath<Representation<?>, ConvertFunction<?, ?>>> pathList = new ArrayList<GraphPath<Representation<?>, ConvertFunction<?, ?>>>();
  double minWeight = Double.MAX_VALUE;
  GraphPath<Representation, ConvertFunction> minPath = null;
  for (Representation vertex : vertices) {
    mLog.info(">>>>>> start vertex = " + vertex.getTypeSpecsString());
    mLog.info(">>>>>> end Vertex = " + representation.getTypeSpecsString());
    GraphPath<Representation, ConvertFunction> shortestPath = this.mGraph.getShortestPath(vertex, representation);

    if (shortestPath != null) {
      mLog.info(">>>> shortestPath != null");
      if (shortestPath.getWeight() < minWeight) {
        minWeight = shortestPath.getWeight();
        minPath = shortestPath;
      }
    }
  }

  if (minPath == null) {
    return null;
  } else {
    Representation startVertex = minPath.getStartVertex();
    Representation startRepresentation = null;
    for (Representation rep : this.mReps.values()) {
      if (rep.equals(startVertex)) {
        startRepresentation = rep;
      }
    }
    mLog.info("minPath.getWeight = " + minPath.getWeight());
    mLog.info("minPath.lenth = " + minPath.getEdgeList().size());
    mLog.info("startVertext = " + minPath.getStartVertex().getTypeSpecsString());
    mLog.info("endVertex = " + minPath.getEndVertex().getTypeSpecsString());
    List<ConvertFunction> convertFunctions = minPath.getEdgeList();
    Representation objectRepresentation = startRepresentation;
    for (ConvertFunction func : convertFunctions) {
      objectRepresentation = func.apply(objectRepresentation);
    }
    return objectRepresentation;
  }
}