From e8d3a88ecde5b6ca0ff45f57bb21b0218220636f Mon Sep 17 00:00:00 2001
From: Philipp Spohr <spohr.philipp@web.de>
Date: Sun, 13 Aug 2017 14:16:00 +0200
Subject: [PATCH] implemented very very basic functionality

---
 .../de/hhu/ba/yoshikoWrapper/CyActivator.java |  4 +-
 .../de/hhu/ba/yoshikoWrapper/core/Core.java   | 63 +++++++++++++++++++
 .../ba/yoshikoWrapper/core/GraphParser.java   | 52 ---------------
 .../ba/yoshikoWrapper/core/NetworkParser.java | 54 ++++++++++++++++
 .../hhu/ba/yoshikoWrapper/core/NodeMap.java   | 38 +++++++++++
 .../hhu/ba/yoshikoWrapper/gui/MainPanel.java  | 10 ++-
 .../yoshikoWrapper/swig/LibraryInterface.java | 46 ++++++++++++++
 .../swig/LibraryInterfaceJNI.java             | 11 ++++
 8 files changed, 221 insertions(+), 57 deletions(-)
 create mode 100644 src/main/java/de/hhu/ba/yoshikoWrapper/core/Core.java
 delete mode 100644 src/main/java/de/hhu/ba/yoshikoWrapper/core/GraphParser.java
 create mode 100644 src/main/java/de/hhu/ba/yoshikoWrapper/core/NetworkParser.java
 create mode 100644 src/main/java/de/hhu/ba/yoshikoWrapper/core/NodeMap.java

diff --git a/src/main/java/de/hhu/ba/yoshikoWrapper/CyActivator.java b/src/main/java/de/hhu/ba/yoshikoWrapper/CyActivator.java
index 1dd4db5..bcc6679 100644
--- a/src/main/java/de/hhu/ba/yoshikoWrapper/CyActivator.java
+++ b/src/main/java/de/hhu/ba/yoshikoWrapper/CyActivator.java
@@ -9,7 +9,7 @@ import org.cytoscape.application.swing.CytoPanelComponent;
 import org.cytoscape.service.util.AbstractCyActivator;
 import org.osgi.framework.BundleContext;
 
-import de.hhu.ba.yoshikoWrapper.core.GraphParser;
+import de.hhu.ba.yoshikoWrapper.core.Core;
 import de.hhu.ba.yoshikoWrapper.gui.MainPanel;
 import de.hhu.ba.yoshikoWrapper.gui.MainPanelAction;
 
@@ -24,7 +24,7 @@ public class CyActivator extends AbstractCyActivator {
 	public void start(BundleContext context) throws Exception {
 		//TODO: Load shared library if installed on startup
 		CyApplicationManager cyApplicationManager = getService(context, CyApplicationManager.class);
-		GraphParser.registerApplicationManager(cyApplicationManager);
+		Core.registerApplicationManager(cyApplicationManager);
 		
 		CySwingApplication cytoscapeDesktopService = getService(context,CySwingApplication.class);
 		
diff --git a/src/main/java/de/hhu/ba/yoshikoWrapper/core/Core.java b/src/main/java/de/hhu/ba/yoshikoWrapper/core/Core.java
new file mode 100644
index 0000000..04a3c68
--- /dev/null
+++ b/src/main/java/de/hhu/ba/yoshikoWrapper/core/Core.java
@@ -0,0 +1,63 @@
+package de.hhu.ba.yoshikoWrapper.core;
+
+import java.util.HashMap;
+
+//TODO: ADD LOGGER SYSTEM
+
+import org.cytoscape.application.CyApplicationManager;
+import org.cytoscape.model.CyNetwork;
+import org.cytoscape.model.CyNode;
+
+import de.hhu.ba.yoshikoWrapper.swig.LibraryInterface;
+import de.hhu.ba.yoshikoWrapper.swig.SWIGTYPE_p_std__vectorT_int_t;
+import de.hhu.ba.yoshikoWrapper.swig.SWIGTYPE_p_yskInput__LibraryInput;
+import de.hhu.ba.yoshikoWrapper.swig.SWIGTYPE_p_ysk__ClusterEditingSolutions;
+
+public class Core {
+	
+	private static CyApplicationManager cy;
+	
+	public static void registerApplicationManager(CyApplicationManager cyApplicationManager) {
+		cy = cyApplicationManager;
+	}
+
+	public static void performYoshiko() {
+		CyNetwork currentNetwork = cy.getCurrentNetwork();
+		if (currentNetwork == null) {
+			//TODO
+		}
+		
+		NodeMap nodeMap  = new NodeMap(currentNetwork);
+
+		SWIGTYPE_p_yskInput__LibraryInput input = NetworkParser.parseNetwork(currentNetwork,nodeMap);
+		SWIGTYPE_p_ysk__ClusterEditingSolutions solutions = LibraryInterface.processLibraryInput(input);
+		long numberOfSolutions = LibraryInterface.ClusterEditingSolutions_getNumberOfSolutions(solutions);
+		System.out.println("Found: "+numberOfSolutions+" solutions!");
+		double modificationCost = LibraryInterface.ClusterEditingSolutions_getTotalCost(solutions);
+		System.out.println("Paid a total modification cost of: "+modificationCost);
+		for (long i=0;i<numberOfSolutions;i++) {
+			System.out.println("Processing solution "+(i+1)+" of "+numberOfSolutions);
+			String columnName = "YOSHIKO_SOLUTION_"+(i+1);
+			currentNetwork.getDefaultNodeTable().createColumn(columnName, String.class, false);
+			long numberOfClusters = LibraryInterface.ClusterEditingSolutions_getNumberOfClusters(solutions, i);
+			for (long k=0;k<numberOfClusters;k++) {
+				System.out.println("Processing cluster "+(k+1)+" of "+numberOfClusters);
+				SWIGTYPE_p_std__vectorT_int_t cluster = LibraryInterface.ClusterEditingSolutions_getCluster(solutions, i, k);
+				long sizeOfCluster = LibraryInterface.IntVector_size(cluster);
+				for (int l=0;l<sizeOfCluster;l++) { //Unsafe mismatch int long
+					System.out.println("Processing entry "+(l+1)+ " of "+sizeOfCluster);
+					int nodeID = LibraryInterface.IntVector_get(cluster, l);
+					CyNode node = nodeMap.indexOf(nodeID); //<<< Another int/long conversion
+					if (node == null) {
+						System.out.println("FATAL ERROR: Node was not previously mapped!");
+						return;
+					}
+					currentNetwork.getRow(node).set(columnName, ""+(k+1));
+				}
+			}
+		}
+		
+		LibraryInterface.delete_LibraryInput(input);		
+	}
+
+}
diff --git a/src/main/java/de/hhu/ba/yoshikoWrapper/core/GraphParser.java b/src/main/java/de/hhu/ba/yoshikoWrapper/core/GraphParser.java
deleted file mode 100644
index 83a007c..0000000
--- a/src/main/java/de/hhu/ba/yoshikoWrapper/core/GraphParser.java
+++ /dev/null
@@ -1,52 +0,0 @@
-package de.hhu.ba.yoshikoWrapper.core;
-
-import java.util.List;
-
-import org.cytoscape.application.CyApplicationManager;
-import org.cytoscape.model.CyEdge;
-import org.cytoscape.model.CyNetwork;
-import org.cytoscape.model.CyNode;
-import org.cytoscape.model.CyTable;
-import org.cytoscape.model.CyTableUtil;
-
-import de.hhu.ba.yoshikoWrapper.swig.LibraryInterface;
-import de.hhu.ba.yoshikoWrapper.swig.SWIGTYPE_p_yskInput__LibraryInput;
-
-public class GraphParser {
-	
-	private static CyApplicationManager cy;
-	//**TODO MAKE DYNAMIC
-	private static String weightIndex = "weight";
-	
-	public static SWIGTYPE_p_yskInput__LibraryInput parseGraph() {
-		if (cy == null) {
-			//TODO:
-		}
-		SWIGTYPE_p_yskInput__LibraryInput generatedInput = LibraryInterface.new_LibraryInput();
-		CyNetwork net = cy.getCurrentNetwork();
-		if (net != null){			
-			LibraryInterface.LibraryInput_setSize(generatedInput,  net.getNodeCount());
-			List<CyEdge> edges = net.getEdgeList();
-			CyTable edgeTable = net.getDefaultEdgeTable();
-			for (CyEdge e : edges) {
-				LibraryInterface.LibraryInput_addEdge(
-						generatedInput,
-						(e.getSource().getSUID()),
-						(e.getTarget().getSUID()),
-						0.0
-						);
-				
-			}
-		}
-		return generatedInput;
-	}
-
-	public static void registerApplicationManager(CyApplicationManager cyApplicationManager) {
-		cy = cyApplicationManager;
-	}
-
-
-	
-
-
-}
diff --git a/src/main/java/de/hhu/ba/yoshikoWrapper/core/NetworkParser.java b/src/main/java/de/hhu/ba/yoshikoWrapper/core/NetworkParser.java
new file mode 100644
index 0000000..632f109
--- /dev/null
+++ b/src/main/java/de/hhu/ba/yoshikoWrapper/core/NetworkParser.java
@@ -0,0 +1,54 @@
+package de.hhu.ba.yoshikoWrapper.core;
+
+import java.util.HashMap;
+import java.util.List;
+
+import org.cytoscape.application.CyApplicationManager;
+import org.cytoscape.model.CyColumn;
+import org.cytoscape.model.CyEdge;
+import org.cytoscape.model.CyNetwork;
+import org.cytoscape.model.CyNode;
+import org.cytoscape.model.CyRow;
+
+import de.hhu.ba.yoshikoWrapper.swig.LibraryInterface;
+import de.hhu.ba.yoshikoWrapper.swig.SWIGTYPE_p_yskInput__LibraryInput;
+
+public class NetworkParser {
+	
+	//**TODO MAKE DYNAMIC
+	private static String weightIndex = "Weight";
+	
+
+	
+	
+	public static SWIGTYPE_p_yskInput__LibraryInput parseNetwork(CyNetwork net,NodeMap nodeMap) {
+		SWIGTYPE_p_yskInput__LibraryInput generatedInput = LibraryInterface.new_LibraryInput();
+		if (net != null){			
+			LibraryInterface.LibraryInput_setSize(generatedInput,  net.getNodeCount());
+			List<CyEdge> edges = net.getEdgeList();
+			Class<?> weightType = net.getDefaultEdgeTable().getColumn(weightIndex).getType();
+			System.out.println("Column has type:"+weightType.getName());
+			for (CyEdge e : edges) {
+				CyRow edgeEntry = net.getRow(e);
+				double weight = 0.0;
+				if (weightType == Integer.class) {
+					weight = (double)edgeEntry.get(weightIndex, Integer.class);
+				}
+				System.out.println("Found Edge: "+edgeEntry.get("name", String.class)+ "with weight:"+weight);
+				
+				LibraryInterface.LibraryInput_addEdge(generatedInput,
+						nodeMap.get(e.getSource()),
+						nodeMap.get(e.getTarget())
+						, weight);
+				
+			}
+		}
+		return generatedInput;
+	}
+
+
+
+	
+
+
+}
diff --git a/src/main/java/de/hhu/ba/yoshikoWrapper/core/NodeMap.java b/src/main/java/de/hhu/ba/yoshikoWrapper/core/NodeMap.java
new file mode 100644
index 0000000..821b51a
--- /dev/null
+++ b/src/main/java/de/hhu/ba/yoshikoWrapper/core/NodeMap.java
@@ -0,0 +1,38 @@
+package de.hhu.ba.yoshikoWrapper.core;
+
+import java.util.HashMap;
+import java.util.Map.Entry;
+
+import org.cytoscape.model.CyNetwork;
+import org.cytoscape.model.CyNode;
+
+public class NodeMap {
+	
+	private HashMap<CyNode,Long> nodeMap;
+	
+	
+	public NodeMap(CyNetwork net){
+		nodeMap = new HashMap<CyNode,Long> ();
+		long index = 0;
+		for (CyNode n :net.getNodeList()) {
+			nodeMap.put(n, index);
+			index++;
+		}
+		
+	}
+
+
+	public long get(CyNode key) {
+		return nodeMap.get(key);
+	}
+
+
+	public CyNode indexOf(long nodeID) {
+	    for (Entry<CyNode, Long> entry : nodeMap.entrySet()) {
+	        if (nodeID ==  entry.getValue()) {
+	            return entry.getKey();
+	        }
+	    }
+	    return null;
+	}
+}
diff --git a/src/main/java/de/hhu/ba/yoshikoWrapper/gui/MainPanel.java b/src/main/java/de/hhu/ba/yoshikoWrapper/gui/MainPanel.java
index 71c6de5..5aa2e73 100644
--- a/src/main/java/de/hhu/ba/yoshikoWrapper/gui/MainPanel.java
+++ b/src/main/java/de/hhu/ba/yoshikoWrapper/gui/MainPanel.java
@@ -3,6 +3,7 @@ package de.hhu.ba.yoshikoWrapper.gui;
 import java.awt.Component;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.util.HashMap;
 
 import javax.swing.Icon;
 import javax.swing.JButton;
@@ -12,8 +13,11 @@ import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 import org.cytoscape.application.swing.CytoPanelComponent;
 import org.cytoscape.application.swing.CytoPanelName;
+import org.cytoscape.model.CyNode;
 
-import de.hhu.ba.yoshikoWrapper.core.GraphParser;
+import de.hhu.ba.yoshikoWrapper.core.Core;
+import de.hhu.ba.yoshikoWrapper.core.NetworkParser;
+import de.hhu.ba.yoshikoWrapper.core.NodeMap;
 import de.hhu.ba.yoshikoWrapper.core.YoshikoLoader;
 import de.hhu.ba.yoshikoWrapper.swig.LibraryInterface;
 import de.hhu.ba.yoshikoWrapper.swig.SWIGTYPE_p_yskInput__LibraryInput;
@@ -77,8 +81,8 @@ public class MainPanel extends JPanel implements CytoPanelComponent {
 			@Override
 			public void actionPerformed(ActionEvent e) {
 				if (yoshikoLoader.isLibraryLoaded()){
-					SWIGTYPE_p_yskInput__LibraryInput input = GraphParser.parseGraph();
-					LibraryInterface.delete_LibraryInput(input);
+					Core.performYoshiko();
+
 				}
 			}
 			
diff --git a/src/main/java/de/hhu/ba/yoshikoWrapper/swig/LibraryInterface.java b/src/main/java/de/hhu/ba/yoshikoWrapper/swig/LibraryInterface.java
index b12ec2d..d06fa11 100644
--- a/src/main/java/de/hhu/ba/yoshikoWrapper/swig/LibraryInterface.java
+++ b/src/main/java/de/hhu/ba/yoshikoWrapper/swig/LibraryInterface.java
@@ -9,6 +9,52 @@
 package de.hhu.ba.yoshikoWrapper.swig;
 
 public class LibraryInterface {
+  public static SWIGTYPE_p_std__vectorT_int_t new_IntVector() {
+    long cPtr = LibraryInterfaceJNI.new_IntVector__SWIG_0();
+    return (cPtr == 0) ? null : new SWIGTYPE_p_std__vectorT_int_t(cPtr, true);
+  }
+
+  public static SWIGTYPE_p_std__vectorT_int_t new_IntVector(long n) {
+    long cPtr = LibraryInterfaceJNI.new_IntVector__SWIG_1(n);
+    return (cPtr == 0) ? null : new SWIGTYPE_p_std__vectorT_int_t(cPtr, true);
+  }
+
+  public static long IntVector_size(SWIGTYPE_p_std__vectorT_int_t self) {
+    return LibraryInterfaceJNI.IntVector_size(SWIGTYPE_p_std__vectorT_int_t.getCPtr(self));
+  }
+
+  public static long IntVector_capacity(SWIGTYPE_p_std__vectorT_int_t self) {
+    return LibraryInterfaceJNI.IntVector_capacity(SWIGTYPE_p_std__vectorT_int_t.getCPtr(self));
+  }
+
+  public static void IntVector_reserve(SWIGTYPE_p_std__vectorT_int_t self, long n) {
+    LibraryInterfaceJNI.IntVector_reserve(SWIGTYPE_p_std__vectorT_int_t.getCPtr(self), n);
+  }
+
+  public static boolean IntVector_isEmpty(SWIGTYPE_p_std__vectorT_int_t self) {
+    return LibraryInterfaceJNI.IntVector_isEmpty(SWIGTYPE_p_std__vectorT_int_t.getCPtr(self));
+  }
+
+  public static void IntVector_clear(SWIGTYPE_p_std__vectorT_int_t self) {
+    LibraryInterfaceJNI.IntVector_clear(SWIGTYPE_p_std__vectorT_int_t.getCPtr(self));
+  }
+
+  public static void IntVector_add(SWIGTYPE_p_std__vectorT_int_t self, int x) {
+    LibraryInterfaceJNI.IntVector_add(SWIGTYPE_p_std__vectorT_int_t.getCPtr(self), x);
+  }
+
+  public static int IntVector_get(SWIGTYPE_p_std__vectorT_int_t self, int i) {
+    return LibraryInterfaceJNI.IntVector_get(SWIGTYPE_p_std__vectorT_int_t.getCPtr(self), i);
+  }
+
+  public static void IntVector_set(SWIGTYPE_p_std__vectorT_int_t self, int i, int val) {
+    LibraryInterfaceJNI.IntVector_set(SWIGTYPE_p_std__vectorT_int_t.getCPtr(self), i, val);
+  }
+
+  public static void delete_IntVector(SWIGTYPE_p_std__vectorT_int_t self) {
+    LibraryInterfaceJNI.delete_IntVector(SWIGTYPE_p_std__vectorT_int_t.getCPtr(self));
+  }
+
   public static SWIGTYPE_p_ysk__ClusterEditingSolutions new_ClusterEditingSolutions() {
     long cPtr = LibraryInterfaceJNI.new_ClusterEditingSolutions();
     return (cPtr == 0) ? null : new SWIGTYPE_p_ysk__ClusterEditingSolutions(cPtr, true);
diff --git a/src/main/java/de/hhu/ba/yoshikoWrapper/swig/LibraryInterfaceJNI.java b/src/main/java/de/hhu/ba/yoshikoWrapper/swig/LibraryInterfaceJNI.java
index f2e0d84..aa49472 100644
--- a/src/main/java/de/hhu/ba/yoshikoWrapper/swig/LibraryInterfaceJNI.java
+++ b/src/main/java/de/hhu/ba/yoshikoWrapper/swig/LibraryInterfaceJNI.java
@@ -9,6 +9,17 @@
 package de.hhu.ba.yoshikoWrapper.swig;
 
 public class LibraryInterfaceJNI {
+  public final static native long new_IntVector__SWIG_0();
+  public final static native long new_IntVector__SWIG_1(long jarg1);
+  public final static native long IntVector_size(long jarg1);
+  public final static native long IntVector_capacity(long jarg1);
+  public final static native void IntVector_reserve(long jarg1, long jarg2);
+  public final static native boolean IntVector_isEmpty(long jarg1);
+  public final static native void IntVector_clear(long jarg1);
+  public final static native void IntVector_add(long jarg1, int jarg2);
+  public final static native int IntVector_get(long jarg1, int jarg2);
+  public final static native void IntVector_set(long jarg1, int jarg2, int jarg3);
+  public final static native void delete_IntVector(long jarg1);
   public final static native long new_ClusterEditingSolutions();
   public final static native long ClusterEditingSolutions_getNumberOfClusters(long jarg1, long jarg2);
   public final static native long ClusterEditingSolutions_getCluster(long jarg1, long jarg2, long jarg3);
-- 
GitLab