...
 
Commits (2)
    https://gitcode.net/loovejava/gephi/-/commit/c877d9ace23a1f5f8eb36a002d9dce40e439ad0f Fix #2783 dynamic edge weight for slice 2023-03-25T21:49:38+01:00 Mathieu Bastian mathieu.bastian@gmail.com https://gitcode.net/loovejava/gephi/-/commit/4b9be0f985d5a96209d97456689e3fbe0291da25 Add processor tests 2023-03-25T22:01:38+01:00 Mathieu Bastian mathieu.bastian@gmail.com
......@@ -141,4 +141,9 @@ public class ColumnDraftImpl implements ColumnDraft {
public boolean isDynamic() {
return dynamic;
}
@Override
public String toString() {
return title + " (" + typeClass.toString() + ")";
}
}
......@@ -92,6 +92,18 @@ public class EdgeDraftImpl extends ElementDraftImpl implements EdgeDraft {
//SETTERS
@Override
public void setWeight(double weight) {
if (getGraphInterval() != null || getGraphTimestamp() != null) {
// Slice mode
ColumnDraft draftColumn = container.getEdgeColumn("weight");
if (draftColumn != null && draftColumn.isDynamic()) {
if (getGraphInterval() != null) {
setValue("weight", weight, getGraphInterval().getLow(), getGraphInterval().getHigh());
} else {
setValue("weight", weight, getGraphTimestamp());
}
return;
}
}
this.weight = weight;
}
......
......@@ -77,6 +77,12 @@
<artifactId>org-netbeans-modules-masterfs</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.gephi</groupId>
<artifactId>graph-api</artifactId>
<scope>test</scope>
<type>test-jar</type>
</dependency>
<dependency>
<groupId>org.gephi</groupId>
<artifactId>io-exporter-plugin</artifactId>
......
package org.gephi.io.importer.plugin.file;
import org.gephi.graph.api.AttributeUtils;
import org.gephi.graph.api.types.TimestampDoubleMap;
import org.gephi.graph.api.types.TimestampIntegerMap;
import org.gephi.io.importer.api.Container;
import org.gephi.io.importer.api.EdgeDraft;
import org.gephi.io.importer.api.MetadataDraft;
......@@ -84,4 +86,37 @@ public class GEXFTest {
Assert.assertEquals("TITLE", meta.getTitle());
Assert.assertEquals("DESCRIPTION", meta.getDescription());
}
@Test
public void testDynamicWeight() {
ImporterGEXF importer = new ImporterGEXF();
importer.setReader(Utils.getReader("gexf/dynamicedgeweight.gexf"));
Container container = new ImportContainerImpl();
importer.execute(container.getLoader());
EdgeDraft edge = Utils.getEdge(container, "0");
Assert.assertEquals(new TimestampDoubleMap(new double[] {2004, 2005}, new double[] {1, 2}),
edge.getValue("weight"));
}
@Test
public void testSlice() {
ImporterGEXF importer = new ImporterGEXF();
importer.setReader(Utils.getReader("gexf/slice.gexf"));
Container container = new ImportContainerImpl();
importer.execute(container.getLoader());
Assert.assertEquals(2007, container.getUnloader().getTimestamp(), 0.0);
Assert.assertTrue(container.getUnloader().getNodeColumn("price").isDynamic());
Assert.assertTrue(container.getUnloader().getEdgeColumn("weight").isDynamic());
NodeDraft node = Utils.getNode(container, "1");
Assert.assertEquals(new TimestampIntegerMap(new double[] {2007}, new int[] {12}), node.getValue("price"));
EdgeDraft edge = Utils.getEdge(container, "0");
Assert.assertEquals(new TimestampDoubleMap(new double[] {2007}, new double[] {2}),
edge.getValue("weight"));
}
}
......@@ -39,6 +39,11 @@ public class Utils {
return Arrays.stream(nodes).filter(n -> n.getId().equals(id)).findFirst().orElse(null);
}
public static EdgeDraft getEdge(Container container, String id) {
EdgeDraft[] edges = toEdgesArray(container);
return Arrays.stream(edges).filter(n -> n.getId().equals(id)).findFirst().orElse(null);
}
public static EdgeDraft[] toEdgesArray(Container container) {
List<EdgeDraft> result = new ArrayList<>();
container.getUnloader().getEdges().iterator().forEachRemaining(result::add);
......
package org.gephi.io.importer.plugin;
package org.gephi.io.processor.plugin;
import org.gephi.graph.api.GraphModel;
import org.gephi.graph.api.Node;
......
package org.gephi.io.processor.plugin;
import java.util.Collections;
import org.gephi.graph.GraphGenerator;
import org.gephi.graph.api.Configuration;
import org.gephi.graph.api.Edge;
import org.gephi.graph.api.TimeRepresentation;
import org.gephi.graph.api.types.TimestampDoubleMap;
import org.gephi.io.importer.impl.EdgeDraftImpl;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class DynamicEdgeWeightTest {
private Utils.TestProcessor processor;
private EdgeDraftImpl edgeDraft;
private Edge edge;
@Before
public void setUp() {
Configuration configuration = new Configuration();
configuration.setEdgeWeightType(TimestampDoubleMap.class);
configuration.setTimeRepresentation(TimeRepresentation.TIMESTAMP);
processor = new Utils.TestProcessor(
GraphGenerator.build(configuration).generateTinyGraph().getGraphModel()
);
edgeDraft = new EdgeDraftImpl(processor.getContainer(), GraphGenerator.FIRST_EDGE);
processor.getContainer().setTimeRepresentation(TimeRepresentation.TIMESTAMP);
edge = processor.graphModel.getGraph().getEdge(GraphGenerator.FIRST_EDGE);
}
@Test
public void testNewEdge() {
processor.getContainer().addEdgeColumn("weight", Double.class, true);
edgeDraft.setValue("weight", new TimestampDoubleMap(new double[] {2.0}, new double[] {4.0}));
processor.flushEdgeWeight(processor.getContainer(), edgeDraft, edge, true);
Assert.assertEquals(Collections.EMPTY_LIST, processor.getReport().getIssuesList(100));
Assert.assertEquals(4.0, edge.getWeight(2.0), 0.0);
}
@Test
public void testMergeWeight() {
edge.setWeight(5.0, 1.0);
processor.getContainer().addEdgeColumn("weight", Double.class, true);
edgeDraft.setValue("weight", new TimestampDoubleMap(new double[] {2.0}, new double[] {4.0}));
processor.flushEdgeWeight(processor.getContainer(), edgeDraft, edge, false);
Assert.assertEquals(Collections.EMPTY_LIST, processor.getReport().getIssuesList(100));
Assert.assertEquals(4.0, edge.getWeight(2.0), 0.0);
Assert.assertEquals(5.0, edge.getWeight(1.0), 0.0);
}
@Test
public void testPreserveEdgeWeight() {
processor.getContainer().addEdgeColumn("weight", Double.class, true);
edge.setWeight(5.0, 1.0);
processor.flushEdgeWeight(processor.getContainer(), edgeDraft, edge, false);
Assert.assertEquals(Collections.EMPTY_LIST, processor.getReport().getIssuesList(100));
Assert.assertEquals(5.0, edge.getWeight(1.0), 0.0);
}
}
package org.gephi.io.processor.plugin;
import org.gephi.graph.GraphGenerator;
import org.gephi.graph.api.Edge;
import org.gephi.io.importer.api.EdgeMergeStrategy;
import org.gephi.io.importer.impl.EdgeDraftImpl;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class EdgeWeightTest {
private Utils.TestProcessor processor;
private EdgeDraftImpl edgeDraft;
private Edge edge;
@Before
public void setUp() {
processor = new Utils.TestProcessor(
GraphGenerator.build().generateTinyGraph().getGraphModel()
);
edgeDraft = new EdgeDraftImpl(processor.getContainer(), GraphGenerator.FIRST_EDGE);
edge = processor.graphModel.getGraph().getEdge(GraphGenerator.FIRST_EDGE);
}
@Test
public void testSumMergeStrategy() {
edgeDraft.setWeight(42.0);
processor.flushEdgeWeight(processor.getContainer(), edgeDraft, edge, false);
Assert.assertEquals(edgeDraft.getWeight() + 1, edge.getWeight(), 0.0);
}
@Test
public void testFirstMergeStrategy() {
edgeDraft.setWeight(42.0);
processor.getContainer().setEdgesMergeStrategy(EdgeMergeStrategy.FIRST);
processor.flushEdgeWeight(processor.getContainer(), edgeDraft, edge, false);
Assert.assertEquals(1, edge.getWeight(), 0.0);
}
@Test
public void testLastMergeStrategy() {
edgeDraft.setWeight(42.0);
processor.getContainer().setEdgesMergeStrategy(EdgeMergeStrategy.LAST);
processor.flushEdgeWeight(processor.getContainer(), edgeDraft, edge, false);
Assert.assertEquals(edgeDraft.getWeight(), edge.getWeight(), 0.0);
}
@Test
public void testNoMergeStrategy() {
edgeDraft.setWeight(42.0);
processor.getContainer().setEdgesMergeStrategy(EdgeMergeStrategy.NO_MERGE);
processor.flushEdgeWeight(processor.getContainer(), edgeDraft, edge, false);
Assert.assertEquals(1, edge.getWeight(), 0.0);
}
@Test
public void testAvgMergeStrategy() {
edgeDraft.setWeight(42.0);
processor.getContainer().setEdgesMergeStrategy(EdgeMergeStrategy.AVG);
processor.flushEdgeWeight(processor.getContainer(), edgeDraft, edge, false);
Assert.assertEquals((edgeDraft.getWeight() + 1) / 2, edge.getWeight(), 0.0);
}
@Test
public void testAvgMergeStrategyWithThree() {
processor.getContainer().setEdgesMergeStrategy(EdgeMergeStrategy.AVG);
edge.setWeight(4);
edgeDraft = new EdgeDraftImpl(processor.getContainer(), GraphGenerator.FIRST_EDGE);
edgeDraft.setWeight(10.0);
processor.flushEdgeWeight(processor.getContainer(), edgeDraft, edge, false);
processor.flushEdgeWeight(processor.getContainer(), edgeDraft, edge, false);
Assert.assertEquals((24.0) / 3, edge.getWeight(), 0.0);
}
@Test
public void testMinMergeStrategy() {
edgeDraft.setWeight(42.0);
processor.getContainer().setEdgesMergeStrategy(EdgeMergeStrategy.MIN);
processor.flushEdgeWeight(processor.getContainer(), edgeDraft, edge, false);
Assert.assertEquals(1, edge.getWeight(), 0.0);
}
@Test
public void testMaxMergeStrategy() {
edgeDraft.setWeight(42.0);
processor.getContainer().setEdgesMergeStrategy(EdgeMergeStrategy.MAX);
processor.flushEdgeWeight(processor.getContainer(), edgeDraft, edge, false);
Assert.assertEquals(edgeDraft.getWeight(), edge.getWeight(), 0.0);
}
}
package org.gephi.io.processor.plugin;
import org.gephi.graph.api.GraphModel;
import org.gephi.io.importer.impl.ImportContainerImpl;
public class Utils {
protected static class TestProcessor extends AbstractProcessor {
public TestProcessor(GraphModel graphModel) {
this.graphModel = graphModel;
this.containers = new ImportContainerImpl[] {new ImportContainerImpl()};
}
public ImportContainerImpl getContainer() {
return (ImportContainerImpl) containers[0];
}
@Override
public void process() {
}
@Override
public String getDisplayName() {
return null;
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<gexf xmlns="http://www.gexf.net/1.3" version="1.3">
<graph mode="dynamic" defaultedgetype="directed" timeformat="double" timerepresentation="timestamp">
<attributes class="edge" mode="dynamic">
<attribute id="weight" title="Weight" type="double"/>
</attributes>
<nodes>
<node id="0" label="Hello" />
<node id="1" label="Word" />
</nodes>
<edges>
<edge id="0" source="0" target="1" >
<attvalues>
<attvalue for="weight" value="1" timestamp="2004"/>
<attvalue for="weight" value="2" timestamp="2005"/>
</attvalues>
</edge>
</edges>
</graph>
</gexf>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<gexf xmlns="http://www.gexf.net/1.3" version="1.3">
<graph mode="slice" defaultedgetype="directed" timerepresentation="timestamp" timestamp="2007">
<attributes class="node" type="dynamic">
<attribute id="price" title="Price" type="int"/>
</attributes>
<attributes class="edge" type="dynamic">
<attribute id="weight" title="Weight" type="double"/>
</attributes>
<nodes>
<node id="1" label="Node 1">
<attvalue for="price" value="12"/>
</node>
<node id="2" label="Node 2">
<attvalue for="price" value="8"/>
</node>
</nodes>
<edges>
<edge id ="0" source="1" target="2" weight="2" />
</edges>
</graph>
</gexf>
\ No newline at end of file