All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jgrapht.nio.graphml.SimpleGraphMLEdgeListImporter Maven / Gradle / Ivy

/*
 * (C) Copyright 2016-2021, by Dimitrios Michail and Contributors.
 *
 * JGraphT : a free Java graph-theory library
 *
 * See the CONTRIBUTORS.md file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0, or the
 * GNU Lesser General Public License v2.1 or later
 * which is available at
 * http://www.gnu.org/licenses/old-licenses/lgpl-2.1-standalone.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR LGPL-2.1-or-later
 */
package org.jgrapht.nio.graphml;

import org.jgrapht.alg.util.*;
import org.jgrapht.nio.*;

import java.io.*;
import java.util.*;
import java.util.function.*;

/**
 * Imports a GraphML file as an edge list. Vertices are numbered from $0$ to $n-1$ in the order they
 * are first encountered in the input file.
 * 
 * 

* This is a simple implementation with supports only a limited set of features of the GraphML * specification. For a more rigorous parser use {@link GraphMLImporter}. This version is oriented * towards parsing speed. Default attribute values are completely ignored. * *

* For a description of the format see * http://en.wikipedia.org/wiki/ GraphML or the * GraphML Primer. *

* *

* Below is small example of a graph in GraphML format. * *

 * {@code
 * 
 * 
 *   
 *   
 *   
 *     
 *       green
 *     
 *     
 *       black
 *          
 *     
 *       blue
 *     
 *     
 *       red
 *     
 *     
 *       white
 *     
 *     
 *       turquoise
 *     
 *     
 *       1.0
 *     
 *     
 *       1.0
 *     
 *     
 *       2.0
 *     
 *     
 *     
 *     
 *     
 *       1.1
 *     
 *   
 * 
 * }
 * 
* *

* The importer by default validates the input using the 1.0 * GraphML Schema. The user can * (not recommended) disable the validation by calling {@link #setSchemaValidation(boolean)}. * * @author Dimitrios Michail */ public class SimpleGraphMLEdgeListImporter extends BaseEventDrivenImporter> implements EventDrivenImporter> { private static final String EDGE_WEIGHT_DEFAULT_ATTRIBUTE_NAME = "weight"; private boolean schemaValidation; private String edgeWeightAttributeName = EDGE_WEIGHT_DEFAULT_ATTRIBUTE_NAME; /** * Constructs a new importer. */ public SimpleGraphMLEdgeListImporter() { super(); this.schemaValidation = true; } /** * Get the attribute name for edge weights * * @return the attribute name */ public String getEdgeWeightAttributeName() { return edgeWeightAttributeName; } /** * Set the attribute name to use for edge weights. * * @param edgeWeightAttributeName the attribute name */ public void setEdgeWeightAttributeName(String edgeWeightAttributeName) { this.edgeWeightAttributeName = Objects .requireNonNull(edgeWeightAttributeName, "Edge weight attribute name cannot be null"); } /** * Whether the importer validates the input * * @return true if the importer validates the input */ public boolean isSchemaValidation() { return schemaValidation; } /** * Set whether the importer should validate the input * * @param schemaValidation value for schema validation */ public void setSchemaValidation(boolean schemaValidation) { this.schemaValidation = schemaValidation; } @Override public void importInput(Reader input) throws ImportException { SimpleGraphMLEventDrivenImporter genericImporter = new SimpleGraphMLEventDrivenImporter(); genericImporter.setEdgeWeightAttributeName(edgeWeightAttributeName); genericImporter.setSchemaValidation(schemaValidation); Consumers consumers = new Consumers(); genericImporter.addImportEventConsumer(consumers.eventConsumer); genericImporter.addVertexConsumer(consumers.vertexConsumer); genericImporter.addEdgeConsumer(consumers.edgeConsumer); genericImporter.addEdgeAttributeConsumer(consumers.edgeAttributeConsumer); genericImporter.importInput(input); } private class Consumers { private int nodeCount; private Map vertexMap; private Triple lastIntegerTriple; private Triple lastTriple; public Consumers() { this.nodeCount = 0; this.vertexMap = new HashMap<>(); } public final Consumer eventConsumer = (e) -> { if (ImportEvent.END.equals(e)) { if (lastTriple != null) { notifyEdge(lastIntegerTriple); lastTriple = null; lastIntegerTriple = null; } } }; public final Consumer vertexConsumer = (v) -> { vertexMap.computeIfAbsent(v, k -> nodeCount++); }; public final BiConsumer, String>, Attribute> edgeAttributeConsumer = (edgeAndKey, a) -> { Triple q = edgeAndKey.getFirst(); String keyName = edgeAndKey.getSecond(); if (lastTriple == q && edgeWeightAttributeName.equals(keyName)) { lastTriple.setThird(q.getThird()); lastIntegerTriple.setThird(q.getThird()); } }; public final Consumer> edgeConsumer = (q) -> { if (q != lastTriple) { if (lastTriple != null) { notifyEdge(lastIntegerTriple); } lastTriple = q; lastIntegerTriple = createIntegerTriple(q); } }; private Triple createIntegerTriple( Triple e) { int source = vertexMap.computeIfAbsent(e.getFirst(), k -> { return Integer.valueOf(nodeCount++); }); int target = vertexMap.computeIfAbsent(e.getSecond(), k -> { return Integer.valueOf(nodeCount++); }); Double weight = e.getThird(); return Triple.of(source, target, weight); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy