001 // GraphLab Project: http://graphlab.sharif.edu 002 // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology 003 // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/ 004 005 package graphlab.plugins.commandline.commands; 006 007 /** 008 * @author Mohammad Ali Rostami 009 * @email ma.rostami@yahoo.com 010 */ 011 012 import graphlab.graph.graph.EdgeModel; 013 import graphlab.graph.graph.GraphModel; 014 import graphlab.graph.graph.VertexModel; 015 import graphlab.platform.core.BlackBoard; 016 import graphlab.platform.lang.CommandAttitude; 017 import graphlab.platform.parameter.Parameter; 018 import graphlab.plugins.main.GraphData; 019 020 import java.util.HashMap; 021 import java.util.Iterator; 022 import java.util.StringTokenizer; 023 024 public class NativeCommands { 025 private BlackBoard bb; 026 private GraphData datas; 027 028 public NativeCommands(BlackBoard bb) { 029 this.bb = bb; 030 datas = new GraphData(bb); 031 } 032 033 static { 034 try { 035 //System.loadLibrary("graphlab_gui_plugins_commandline_commands_NativeCommands"); 036 } catch (Exception e) { 037 } 038 } 039 040 private native String homomorph(String graph_format1, String graph_format2); 041 042 @CommandAttitude(name = "ghomomorph", abbreviation = "_ghom", description = "check homomorphism") 043 public String ghomomorph(@Parameter(name = "first_graph")GraphModel g1, 044 @Parameter(name = "second_graph")GraphModel g2) { 045 String graph1 = ""; 046 String graph2 = ""; 047 graph1 += g1.getVerticesCount() + "\n"; 048 graph2 += g2.getVerticesCount() + "\n"; 049 Iterator<EdgeModel> it1 = g1.edgeIterator(); 050 Iterator<EdgeModel> it2 = g2.edgeIterator(); 051 052 HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); 053 HashMap<Integer, Integer> nmap = new HashMap<Integer, Integer>(); 054 Iterator<VertexModel> vm = g1.iterator(); 055 int counter = 0; 056 while (vm.hasNext()) { 057 VertexModel vv = vm.next(); 058 map.put(vv.getId(), counter); 059 nmap.put(counter++, vv.getId()); 060 } 061 062 while (it1.hasNext()) { 063 EdgeModel e = it1.next(); 064 graph1 += map.get(e.source.getId()) + " " + map.get(e.target.getId()) + "\n"; 065 } 066 067 map = new HashMap<Integer, Integer>(); 068 vm = g2.iterator(); 069 counter = 0; 070 071 while (vm.hasNext()) 072 map.put(vm.next().getId(), counter++); 073 074 while (it2.hasNext()) { 075 EdgeModel e = it2.next(); 076 graph2 += map.get(e.source.getId()) + " " + map.get(e.target.getId()) + "\n"; 077 } 078 079 String s = homomorph(graph1, graph2); 080 double time = Double.parseDouble(s.substring(0, s.indexOf("\n"))); 081 s = s.substring(s.indexOf("\n") + 1); 082 if (s.equals("false")) return "time : " + time + ".\nNo homomorphism exists."; 083 String result = ""; 084 StringTokenizer stk2 = new StringTokenizer(s); 085 while (stk2.hasMoreElements()) { 086 String temp = (String) stk2.nextElement(); 087 result = result + nmap.get(Integer.parseInt(temp.substring(0, temp.indexOf("-")))) + "->" 088 + nmap.get(Integer.parseInt(temp.substring(temp.indexOf(">") + 1))) + "\n"; 089 } 090 091 return "time : " + time + ".\nFounded Homomorphism is:\n" + result; 092 } 093 }