com.clarkparsia.pellint.util.OptimizedDirectedMultigraph Maven / Gradle / Ivy
The newest version!
// Copyright (c) 2006 - 2008, Clark & Parsia, LLC.
// This source code is available under the terms of the Affero General Public License v3.
//
// Please see LICENSE.txt for full license terms, including the availability of proprietary exceptions.
// Questions, comments, or requests for clarification: [email protected]
package com.clarkparsia.pellint.util;
import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.SimpleDirectedWeightedGraph;
/**
*
* Title: Optimized Directed Multigraph
*
*
* Description: A directed multigraph where the edge's multiplicity is implemented
* as the weight of the edge as opposed to many instances of edges - has better
* performances under graph traversal and connectivity algorithms.
*
*
* Copyright: Copyright (c) 2008
*
*
* Company: Clark & Parsia, LLC.
*
*
* @author Harris Lin
*/
public class OptimizedDirectedMultigraph extends SimpleDirectedWeightedGraph {
private static final long serialVersionUID = 1L;
private static final String NON_POSITIVE_MULTIPLICITY = "Non-positive multiplicity is not allowed";
public OptimizedDirectedMultigraph() {
super(DefaultWeightedEdge.class);
}
public DefaultWeightedEdge addEdge(V sourceVertex, V targetVertex) {
return addEdge(sourceVertex, targetVertex, 1);
}
public DefaultWeightedEdge addEdge(V sourceVertex, V targetVertex, int multiplicity) {
if (multiplicity <= 0) {
throw new IllegalArgumentException(NON_POSITIVE_MULTIPLICITY);
}
DefaultWeightedEdge edge = getEdge(sourceVertex, targetVertex);
if (edge == null) {
edge = super.addEdge(sourceVertex, targetVertex);
setEdgeWeight(edge, multiplicity);
} else {
double oldWeight = getEdgeWeight(edge);
setEdgeWeight(edge, oldWeight + multiplicity);
}
return edge;
}
public int getEdgeMultiplicity(DefaultWeightedEdge edge) {
return (int) getEdgeWeight(edge);
}
}