diff --git a/java/src/main/java/org/softwareheritage/graph/utils/MPHTranslate.java b/java/src/main/java/org/softwareheritage/graph/utils/MPHTranslate.java new file mode 100644 index 0000000..a0fcec6 --- /dev/null +++ b/java/src/main/java/org/softwareheritage/graph/utils/MPHTranslate.java @@ -0,0 +1,65 @@ +package org.softwareheritage.graph.utils; + +import com.martiansoftware.jsap.*; +import it.unimi.dsi.fastutil.io.BinIO; +import it.unimi.dsi.fastutil.objects.Object2LongFunction; +import it.unimi.dsi.io.FastBufferedReader; +import it.unimi.dsi.io.LineIterator; +import org.softwareheritage.graph.experiments.multiplicationfactor.GenDistribution; + +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; + +public class MPHTranslate { + final static String SORT_BUFFER_SIZE = "40%"; + + private static JSAPResult parse_args(String[] args) { + JSAPResult config = null; + try { + SimpleJSAP jsap = new SimpleJSAP( + GenDistribution.class.getName(), + "", + new Parameter[]{ + new UnflaggedOption("function", JSAP.STRING_PARSER, JSAP.REQUIRED, + "Filename of the serialized MPH"), + } + ); + + config = jsap.parse(args); + if (jsap.messagePrinted()) { + System.exit(1); + } + } catch (JSAPException e) { + e.printStackTrace(); + } + return config; + } + + public static void main(String[] args) throws IOException { + JSAPResult config = parse_args(args); + String mphPath = config.getString("function"); + outputPermutation(mphPath); + } + + + @SuppressWarnings("unchecked") // Suppress warning for Object2LongFunction cast + static void outputPermutation(String mphPath) + throws IOException { + Object2LongFunction mphMap = null; + try { + mphMap = (Object2LongFunction) BinIO.loadObject(mphPath); + } catch (ClassNotFoundException e) { + System.exit(2); + } + + FastBufferedReader buffer = new FastBufferedReader(new InputStreamReader(System.in, + StandardCharsets.US_ASCII)); + LineIterator lineIterator = new LineIterator(buffer); + + while (lineIterator.hasNext()) { + String line = lineIterator.next().toString(); + System.out.println(mphMap.getLong(line)); + } + } +} diff --git a/java/src/main/java/org/softwareheritage/graph/utils/ReadGraph.java b/java/src/main/java/org/softwareheritage/graph/utils/ReadGraph.java new file mode 100644 index 0000000..333bddb --- /dev/null +++ b/java/src/main/java/org/softwareheritage/graph/utils/ReadGraph.java @@ -0,0 +1,32 @@ +package org.softwareheritage.graph.utils; + +import it.unimi.dsi.big.webgraph.ImmutableGraph; +import it.unimi.dsi.big.webgraph.NodeIterator; +import org.softwareheritage.graph.maps.NodeIdMap; + +import java.io.IOException; + +public class ReadGraph { + public static void main(String[] args) throws IOException { + String graphPath = args[0]; + + ImmutableGraph graph = ImmutableGraph.load(graphPath); + NodeIdMap nodeMap = new NodeIdMap(graphPath, graph.numNodes()); + + NodeIterator it = graph.nodeIterator(); + while (it.hasNext()) { + long srcNode = it.nextLong(); + + var s = it.successors(); + long dstNode; + while ((dstNode = s.nextLong()) >= 0) { + System.out.format( + "%s %s\n", + nodeMap.getSwhPID(srcNode), + nodeMap.getSwhPID(dstNode) + ); + } + } + } +} + diff --git a/java/src/main/java/org/softwareheritage/graph/utils/ReadLabelledGraph.java b/java/src/main/java/org/softwareheritage/graph/utils/ReadLabelledGraph.java new file mode 100644 index 0000000..f7abefe --- /dev/null +++ b/java/src/main/java/org/softwareheritage/graph/utils/ReadLabelledGraph.java @@ -0,0 +1,42 @@ +package org.softwareheritage.graph.utils; + +import it.unimi.dsi.big.webgraph.labelling.ArcLabelledImmutableGraph; +import it.unimi.dsi.big.webgraph.labelling.ArcLabelledNodeIterator; +import it.unimi.dsi.big.webgraph.labelling.BitStreamArcLabelledImmutableGraph; +import it.unimi.dsi.fastutil.io.BinIO; +import it.unimi.dsi.util.PermutedFrontCodedStringList; +import org.softwareheritage.graph.maps.NodeIdMap; + +import java.io.IOException; + +public class ReadLabelledGraph { + public static void main(String[] args) throws IOException, ClassNotFoundException { + String graphPath = args[0]; + + ArcLabelledImmutableGraph graph = BitStreamArcLabelledImmutableGraph.loadOffline(graphPath + "-labelled"); + NodeIdMap nodeMap = new NodeIdMap(graphPath, graph.numNodes()); + PermutedFrontCodedStringList labelMap = (PermutedFrontCodedStringList) BinIO.loadObject(graphPath + "-labels.fcl"); + + ArcLabelledNodeIterator it = graph.nodeIterator(); + while (it.hasNext()) { + long srcNode = it.nextLong(); + + ArcLabelledNodeIterator.LabelledArcIterator s = it.successors(); + long dstNode; + while ((dstNode = s.nextLong()) >= 0) { + int label = (int) s.label().get(); + int missing = (1 << s.label().fixedWidth()) - 1; + if (label == missing) + label = -1; + + System.out.format( + "%s %s %s\n", + nodeMap.getSwhPID(srcNode), + nodeMap.getSwhPID(dstNode), + ((label > -1) ? labelMap.get(label) : "") + ); + } + } + } +} +