diff --git a/Java/src/cn/edu/xmut/configfile/FigFileUtil.java b/Java/src/cn/edu/xmut/configfile/FigFileUtil.java new file mode 100644 index 0000000..4dd70dc --- /dev/null +++ b/Java/src/cn/edu/xmut/configfile/FigFileUtil.java @@ -0,0 +1,188 @@ +/** + * + */ +package cn.edu.xmut.configfile; + +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import cn.edu.xmut.util.*; +import sun.tools.jar.resources.jar; + +import org.apache.commons.io.IOUtils; + +public class FigFileUtil { + + static Map lookuptableMap = new HashMap(); + static String path = "E:\\projects\\p17\\"; + static int simulatecount = 1; + + public static void main(String[] args) { + if (args.length < 2) { + path = "E:\\projects\\p17\\"; + simulatecount=4; + } else { + path = args[0]; + simulatecount = Integer.parseInt(args[1]); + } + + FileReader fig_filereader; + File fig_file = new File(path + "fig.fig"); + try { + fig_filereader = new FileReader(fig_file); + List configs = IOUtils.readLines(fig_filereader); + + for (int i = 0; i < configs.size(); i++) { + if (configs.get(i).startsWith("subbasin")) { + String line = configs.get(i); + String ww[] = line.split("\\s+"); + String line2 = configs.get(i + 1); + String old = line2.substring(line2.indexOf("0"), + 8 + line2.indexOf("0") + 1); + lookuptableMap.put(ww[3], old); + + } + } + System.out.println(lookuptableMap.toString()); + + } catch (IOException e1) { + e1.printStackTrace(); + } + + //-------------------------生成新旧子流域的映身表结束-------------------- + + Map> upstreamMap = new HashMap>(); + //prepare fig.fig files for all subbasin + + Map upstreamAreaMap = new HashMap(); + + FileReader filereader; + File file = new File(path + "routeset.txt"); + try { + filereader = new FileReader(file); + List routeinfos = IOUtils.readLines(filereader); + IOUtils.closeQuietly(filereader); + for (int i = 0; i < routeinfos.size(); i++) { + String[] tokens = routeinfos.get(i).split(","); + + + upstreamAreaMap.put(tokens[0],tokens[3]); + + if (!tokens[1].equals("-1")) { + + ArrayList rs = (ArrayList) upstreamMap + .get(tokens[1]); + if (rs == null) { + rs = new ArrayList(); + } + rs.add(tokens[0]); + upstreamMap.put(tokens[1], rs); + } + + } + + String subbasin_p1 = "subbasin 1 1 1"; + String subbasin_p2 = " xxxxxxxxx.sub"; + String rec_p1 = "recday 10hhhhhhffffff xxxxxx"; + String rec_p2 = " xxxxxxxxx.pot"; + String add = "add 5aaaaaabbbbbbcccccc"; + String route_p1 = "route 2aaaaaa 1bbbbbb"; + String route_p2 = " xxxxxxxxx.rtexxxxxxxxx.swq"; + String save_p1 = "save 9aaaaaa 1 0 0"; + String save_p2 = " xxxxxxxxx.pot "; + String finish = "finish 0"; + + + for (int z=0; z contents = new ArrayList(); + contents.add(subbasin_p1); + contents.add(subbasin_p2.replace("xxxxxxxxx", oldsub)); + int count = 0; + + if (upsubs != null) + count = upsubs.size(); + + int k = count + 1; + int g = k + 1; + + if (count > 0) { + for (int i = 0; i < count; i++) { + String tupstream = (String) upsubs.get(i); + String line1 = rec_p1 + .replace("hhhhhh", + UtilsFunctions.pad(" ", i + 2, 6)) + .replace("ffffff", + UtilsFunctions.pad(" ", i + 1, 6)) + .replace("xxxxxx", UtilsFunctions.pad(" ", upstreamAreaMap.get(tupstream), 6,"")) ; + + String line2 = rec_p2.replace("xxxxxxxxx", + UtilsFunctions.pad("0", tupstream, 5, simulation)); + contents.add(line1); + contents.add(line2); + } + + for (int i = 0; i < upsubs.size(); i++) { + String line1 = add + .replace("cccccc", + UtilsFunctions.pad(" ", i + 1, 6)) + .replace("bbbbbb", + UtilsFunctions.pad(" ", k, 6)) + .replace("aaaaaa", + UtilsFunctions.pad(" ", g, 6)); + contents.add(line1); + k++; + g++; + } + + } + //String route_p1="route 2aaaaaa 1bbbbbb"; + //String route_p2=" xxxxxxxxx.rtexxxxxxxxx.swq"; + + String l1 = route_p1 + .replace("aaaaaa", UtilsFunctions.pad(" ", g, 6)) + .replace("bbbbbb", UtilsFunctions.pad(" ", k, 6)); + String l2 = route_p2.replaceAll("xxxxxxxxx", oldsub); + contents.add(l1); + contents.add(l2); + + String line1 = save_p1.replace("aaaaaa", + UtilsFunctions.pad(" ", g, 6)); + String line2 = save_p2.replace("xxxxxxxxx", sub); + + contents.add(line1); + contents.add(line2); + contents.add(finish); + + FileWriter fileWriter; + File file4 = new File(path + "wconfigfiles/" + sub + ".fig"); + fileWriter = new FileWriter(file4); + IOUtils.writeLines(contents, null, fileWriter); + IOUtils.closeQuietly(fileWriter); + + } + } + + } catch ( + + IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/Java/src/cn/edu/xmut/model/Model.java b/Java/src/cn/edu/xmut/model/Model.java new file mode 100644 index 0000000..92d43ea --- /dev/null +++ b/Java/src/cn/edu/xmut/model/Model.java @@ -0,0 +1,262 @@ +package cn.edu.xmut.model; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FilenameFilter; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import cn.edu.xmut.util.LockList; +import cn.edu.xmut.util.UtilsFunctions; + +/** + * + */ + +public class Model { + + static boolean initiated = false; + + static String basepath = null; + + static int cores = 0; + + static String os="Linux"; + + static Map> upstreamMap = new HashMap>(); + + public static void init() { + + BufferedReader in = null; + try { + InputStream inputStream = Model.class.getClassLoader() + .getResourceAsStream("config.txt"); + in = new BufferedReader(new InputStreamReader(inputStream)); + String line; + while ((line = in.readLine()) != null) { + String[] tks = line.split("="); + if (tks[0].trim().startsWith("basepath")) { + basepath = tks[1].trim(); + } + if (tks[0].trim().startsWith("cores")) { + cores = Integer.parseInt(tks[1].trim()); + } + + if (tks[0].trim().startsWith("os")) { + os = tks[1].trim(); + } + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (in != null) + try { + in.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + FileReader filereader; + File file = new File(basepath + "routeset.txt"); + try { + + filereader = new FileReader(file); + List routeinfos = IOUtils.readLines(filereader); + IOUtils.closeQuietly(filereader); + for (int i = 0; i < routeinfos.size(); i++) { + String[] tokens = routeinfos.get(i).split(","); + + if (!tokens[1].equals("-1")) { + + ArrayList rs = (ArrayList) upstreamMap + .get(tokens[1]); + if (rs == null) { + rs = new ArrayList(); + } + rs.add(tokens[0]); + upstreamMap.put(tokens[1], rs); + } + + } + } catch (Exception e) { + e.printStackTrace(); + } + + File dir = new File(basepath + "repository"); + + FilenameFilter filter = new FilenameFilter() { + public boolean accept(File dir, String name) { + return !name.startsWith("."); + } + }; + String[] childrens = dir.list(filter); + + for (int i = 0; i < childrens.length; i++) { + + File file2 = new File(basepath + "repository/" + childrens[i]); + file2.delete(); + + } + + for (int k = 0; k < cores; k++) { + + dir = new File(basepath + "models/model" + k); + + filter = new FilenameFilter() { + public boolean accept(File dir, String name) { + return !name.startsWith(".") && name.endsWith("pot"); + } + }; + childrens = dir.list(filter); + + for (int i = 0; i < childrens.length; i++) { + + File file2 = new File( + basepath + "models/model" + k + "/" + childrens[i]); + file2.delete(); + + } + + } + + initiated = true; + + } + + /** + * @param args + * @throws IOException + */ + public static void call(long sub_simulation) throws IOException { + + if (!initiated) { + + init(); + } + + int lInfo = -1; + + //1.get a free model + while (true) { + + //TODO + //System.out.println("cores:" + cores); + //System.out.println("basepath:" + basepath); + + lInfo = LockList.getFreeModelNew(cores); + + if (lInfo != -1) { + + break; + + } else { + + try { + Thread.sleep(500); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + } + } + + //2.run model + + //2.1 prepare model configuration file + + int isub = (int) sub_simulation / 10000; + int isim = (int) sub_simulation % 10000; + //TODO + //System.out.println("isub:" + isub); + //System.out.println("isim:" + isim); + + String srcName = basepath + "wconfigfiles/" + + UtilsFunctions.pad("0", isub, 5) + + UtilsFunctions.pad("0", isim, 4) + ".fig"; + + String dstName = basepath + "models/model" + lInfo + "/fig.fig"; + //TODO + //System.out.println("dstName:=" + dstName); + //System.out.println("srcName:=" + srcName); + File myDelFile = new File(dstName); + try { + myDelFile.delete(); + } catch (Exception e) { + e.printStackTrace(); + } + + FileUtils.copyFile(new File(srcName), + new File(dstName)); + + + //2.2 prepare point source files + + ArrayList upstreams = upstreamMap.get(isub + ""); + //TODO + //System.out.println("isub:" + isub); + //System.out.println("2.2"); + + //has upstreams + if (upstreams != null) { + + for (int i = 0; i < upstreams.size(); i++) { + + String psName = UtilsFunctions.pad("0", upstreams.get(i), 5, "") + + UtilsFunctions.pad("0", isim, 4) + ".pot"; + + String srcPSName = basepath + "repository/" + psName; + String dstPSName = basepath + "models/model" + lInfo + "/" + + psName; + File psFile = new File(dstPSName); + if (!psFile.exists()) { + FileUtils.copyFile(new File(srcPSName), + new File(dstPSName)); + //TODO + //System.out.println("dstPSName:=" + dstPSName); + //System.out.println("srcPSName:=" + srcPSName); + } + } + + } + //2.3 invoke model + if(os.equals("Windows")) { + UtilsFunctions.exeComandWindows( + basepath + "models/model" + lInfo + "/invokeswat.bat>>" + + basepath + "models/model" + lInfo + "/log.txt"); + }else if(os.equals("Linux")){ + + UtilsFunctions.exeComand( + basepath + "models/model" + lInfo + "/invokeswat.sh>>" + + basepath + "models/model" + lInfo + "/log.txt"); + } + //2.4 put generated point source file to a shared repository + String genPSName = UtilsFunctions.pad("0", isub, 5) + + UtilsFunctions.pad("0", isim, 4) + ".pot"; + String gendstPSName = basepath + "repository/" + genPSName; + String gensrcPSName = basepath + "models/model" + lInfo + "/" + + genPSName; + + FileUtils.copyFile(new File(gensrcPSName), new File(gendstPSName)); + + //TODO + //System.out.println("gendstPSName:=" + gendstPSName); + //System.out.println("gensrcPSName:=" + gensrcPSName); + + //3. reLeaseModel + + LockList.releaseModelNew(lInfo); + + } + +} diff --git a/Java/src/cn/edu/xmut/routeinfo/OptimizedGraph.java b/Java/src/cn/edu/xmut/routeinfo/OptimizedGraph.java new file mode 100644 index 0000000..f0270f6 --- /dev/null +++ b/Java/src/cn/edu/xmut/routeinfo/OptimizedGraph.java @@ -0,0 +1,108 @@ +/** + * + */ +package cn.edu.xmut.routeinfo; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class OptimizedGraph { + + static String path = "E:\\projects\\p17\\"; + public static void main(String[] args) { + List originalList = new ArrayList<>(); + + BufferedReader in = null; + Map upstreamLookupTable = new HashMap(); + try { + in = new BufferedReader(new FileReader(path + "jjrouteset.txt")); + String line; + + while ((line = in.readLine()) != null) { + + System.out.println(line); + String[] tokens=line.split(","); + + int startPos=Integer.parseInt(tokens[2].trim()); + int endPos=Integer.parseInt(tokens[3].trim()); + Integer[] t=new Integer[(endPos-startPos+1)]; + + for (int i = 0; i < (endPos-startPos+1); i++) { + t[i]=startPos+i; + } + originalList.add(t); + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (in != null) + try { + in.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + System.out.println(""); + + List combineList = getCombineList(originalList); + + System.out.println("组成的新集合共有" + combineList.size() + "个数组"); + + combineList.forEach(s-> { + for (int i = 0; i < s.length; i++) { + System.out.print(s[i]+","); + } + System.out.println(); + }); + } + + private static List getCombineList(List originalList) { + int originalSize = originalList.size(); + int[] tempIndexArr = new int[originalSize]; + tempIndexArr[originalSize - 1] = -1; + int[] tempLengthArr = new int[originalSize]; + for (int i = 0; i < originalSize ; i++) { + tempLengthArr[i] = originalList.get(i).length; + } + + List combineList = new ArrayList<>(); + boolean completeFlag = false; + + while(!completeFlag) { + int changeIndex = originalList.size() - 1; + boolean isRightIndex = false; + while (!isRightIndex) { + tempIndexArr[changeIndex] += 1; + if(tempIndexArr[changeIndex] >= tempLengthArr[changeIndex]) { + if(changeIndex == 0) { + isRightIndex = true; + completeFlag = true; + } else { + tempIndexArr[changeIndex--] = 0; + } + } else { + isRightIndex = true; + } + } + if(isRightIndex && !completeFlag) { + Integer[] newItem = new Integer[originalList.size()]; + for (int i = 0; i < originalList.size() ; i++) { + newItem[i] = originalList.get(i)[tempIndexArr[i]]; + } + combineList.add(newItem); + } + } + + return combineList; + } + +} \ No newline at end of file diff --git a/Java/src/cn/edu/xmut/routeinfo/RouteSetUtil.java b/Java/src/cn/edu/xmut/routeinfo/RouteSetUtil.java new file mode 100644 index 0000000..85367aa --- /dev/null +++ b/Java/src/cn/edu/xmut/routeinfo/RouteSetUtil.java @@ -0,0 +1,347 @@ +package cn.edu.xmut.routeinfo; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.FilenameFilter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; + +import com.sun.crypto.provider.AESParameters; +import com.sun.org.apache.regexp.internal.recompile; + +import cn.edu.xmut.util.UtilsFunctions; + +/** + * + */ + +public class RouteSetUtil { + + static ArrayList lines = new ArrayList(); + static ArrayList rs = new ArrayList(); + static String path = "E:\\projects\\p17\\"; + static int simulatecount = 2; + + public static void main(String[] args) { + + if (args.length < 2) { + path = "E:\\projects\\p17\\"; + simulatecount = 4; + } else { + path = args[0]; + simulatecount = Integer.parseInt(args[1]); + } + + BufferedReader in = null; + Map upstreamLookupTable = new HashMap(); + try { + in = new BufferedReader(new FileReader(path + "fig.fig")); + String line; + + while ((line = in.readLine()) != null) { + //System.out.println(line); + lines.add(line); + + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (in != null) + try { + in.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + //从最下游子流域开始 + + for (int i = lines.size() - 1; i >= 0; i--) { + String line = lines.get(i); + if (line.startsWith("rout")) { + //System.out.println(line); + route(line, "-1", 0); + System.out.print("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); + break; + + } + } + + + + //generate edges + try { + File edgFile = new File(path + "stream.txt"); + FileWriter edgfileWriter = new FileWriter(edgFile); + for (int k = 0; k < simulatecount; k++) { + + for (int i = 0; i < rs.size(); i++) { + String[] tokens = rs.get(i).split(","); + if (!tokens[1].startsWith("-1")) + IOUtils.write(tokens[0] +UtilsFunctions.pad("0", k, 4)+ " " + tokens[1]+UtilsFunctions.pad("0", k, 4) + "\r\n", + edgfileWriter); + } + } + + IOUtils.closeQuietly(edgfileWriter); + } catch (IOException e) { + e.printStackTrace(); + } + + computeUpstreamArea(rs); + + for (int i = 0; i < rs.size(); i++) { + + String[] tokens = rs.get(i).split(","); + + Double area = upstreamAreaMap.get(tokens[0]); + if (area == null) { + + area = 0.0; + + } + + rs.set(i, rs.get(i) + "," + area); + + } + + //optimized graph + + for (int i = 0; i < rs.size(); i++) { + + String[] tokens = rs.get(i).split(","); + + for (int j = 0; j < rs.size(); j++) { + String[] tokens2 = rs.get(i).split(","); + + if(tokens[1].equals(tokens2[0])) { + + rs.set(i, rs.get(i) + "," + (Integer.parseInt(tokens2[2])-1)); + + } + + + } + + } + + + + + + try { + File file4 = new File(path + "routeset.txt"); + FileWriter fileWriter = new FileWriter(file4); + IOUtils.writeLines(rs, null, fileWriter); + IOUtils.closeQuietly(fileWriter); + } catch (IOException e) { + e.printStackTrace(); + } + + + //generate vertices + try { + File verFile = new File(path + "subbasin.txt"); + FileWriter verfileWriter = new FileWriter(verFile); + + for (int k = 0; k < simulatecount; k++) { + + for (int i = 0; i < rs.size(); i++) { + String[] tokens = rs.get(i).split(","); + int ustreamcount=0; + for (int g = 0; g= 3) { + if (tokens[2].equals(store)) { + return line; + } + } + + } + return ""; + + } + + public static int add(String line, String k, int lv) { + int z = lv; + int t1 = lv, t2 = lv, t3 = lv, t4 = lv; + String tokens[] = line.split("\\s+"); + if (tokens.length >= 3) { + String command1 = fineCommandByPosition(tokens[3]); + if (command1.startsWith("add")) { + t1 = add(command1, k, lv); + } else if (command1.startsWith("rout")) { + t3 = route(command1, k, lv); + + } + String command2 = fineCommandByPosition(tokens[4]); + if (command2.startsWith("add")) { + t2 = add(command2, k, lv); + } else if (command2.startsWith("rout")) { + t4 = route(command2, k, lv); + + } + } + + z = Math.max(Math.max(t1, t2), Math.max(t3, t4)); + + return z; + + } + + public static HashMap> upstreamMap = new HashMap>(); + public static HashMap upstreamAreaMap = new HashMap(); + + public static void computeUpstreamArea(ArrayList lines) { + int maxlvl = lines.size(); + int tmpmaxlvl = 2; + for (int lvl = 1; lvl < maxlvl; lvl++) { + for (int i = 0; i < lines.size(); i++) { + String[] tks = lines.get(i).split(","); + int sublvl = Integer.parseInt(tks[2]); + tmpmaxlvl = Math.max(tmpmaxlvl, sublvl); + if (sublvl == lvl) { + ArrayList upstreams = upstreamMap.get(tks[1]); + if (upstreams == null) { + upstreams = new ArrayList(); + } + upstreams.add(tks[0]); + ArrayList indirectupstreams = upstreamMap + .get(tks[0]); + if (indirectupstreams != null) + upstreams.addAll(indirectupstreams); + upstreamMap.put(tks[1], upstreams); + + if (i == lines.size() - 1) { + maxlvl = tmpmaxlvl; + } + } + + } + } + + for (String line : lines) { + + String[] tks = line.split(","); + + ArrayList upstreams = upstreamMap.get(tks[0]); + double totalArea = 0; + if (upstreams != null) { + + for (String upstream : upstreams) { + + String sub = UtilsFunctions.pad("0", upstream, 5, + "0000.sub"); + double area = getSubbasinArea(sub); + totalArea = totalArea + area; + + } + } + + String sub = UtilsFunctions.pad("0", tks[0], 5, "0000.sub"); + double area = getSubbasinArea(sub); + + upstreamAreaMap.put(tks[0], totalArea + area); + + } + + } + + public static double getSubbasinArea(String sub) { + ArrayList infos = new ArrayList(); + + BufferedReader in = null; + Map upstreamLookupTable = new HashMap(); + try { + in = new BufferedReader( + new FileReader(path + "modelbackup\\" + sub)); + String line; + + int k = 0; + + while ((line = in.readLine()) != null) { + //System.out.println(line); + infos.add(line); + k++; + if (k > 2) { + break; + } + + } + String tks[] = infos.get(1).split("\\|"); + return Double.parseDouble(tks[0].trim()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (in != null) + try { + in.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + return 0; + } + +} diff --git a/Java/src/cn/edu/xmut/util/LockInfo.java b/Java/src/cn/edu/xmut/util/LockInfo.java new file mode 100644 index 0000000..da73ad9 --- /dev/null +++ b/Java/src/cn/edu/xmut/util/LockInfo.java @@ -0,0 +1,15 @@ +/** + * + */ +package cn.edu.xmut.util; + +import java.nio.channels.FileLock; + +public class LockInfo { + + + public FileLock lock; + public int modelIndex; + + +} diff --git a/Java/src/cn/edu/xmut/util/LockList.java b/Java/src/cn/edu/xmut/util/LockList.java new file mode 100644 index 0000000..e6b6b0a --- /dev/null +++ b/Java/src/cn/edu/xmut/util/LockList.java @@ -0,0 +1,89 @@ +/** + * + */ +package cn.edu.xmut.util; + +import java.io.File; +import java.io.IOException; +import java.nio.channels.FileLock; +import java.util.ArrayList; +import java.util.concurrent.locks.LockSupport; + +import cn.edu.xmut.model.Model; + +public class LockList { + + static ArrayList locklist; + + + static boolean[] lockindicators={false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false}; + + + public static synchronized int getFreeModelNew(int cores) { + + for (int i = 0; i < cores; i++) { + if (!lockindicators[i]) { + lockindicators[i]=true; + System.out.println("model "+i+ " is locked!"); + return i; + } + + } + return -1; + } + + public static synchronized void releaseModelNew(int li) { + + System.out.println("model "+li+ " is unlocked!"); + lockindicators[li]=false; + + } + + public static synchronized LockInfo getFreeModel(int cores, String path) { + LockInfo li = null; + + if (locklist == null) { + locklist = new ArrayList(); + for (int i = 0; i < cores; i++) { + MultiProcessesLock mpLock = new MultiProcessesLock( + path + "\\lock." + i); + locklist.add(mpLock); + } + } + + for (int i = 0; i < locklist.size(); i++) { + + FileLock fLock = locklist.get(i).lock(); + + if (fLock != null) { + li = new LockInfo(); + li.lock = fLock; + li.modelIndex = i; + + System.out.println("model "+li.modelIndex+ " is locked!"); + + + return li; + } + + } + + return null; + + } + + public static void releaseModel(LockInfo li) { + + System.out.println("model "+li.modelIndex+ " is unlocked!"); + + if (li.lock != null) { + try { + li.lock.release(); + li.lock = null; + } catch (IOException e) { + e.printStackTrace(); + } + } + } + +} diff --git a/Java/src/cn/edu/xmut/util/MultiProcessesLock.java b/Java/src/cn/edu/xmut/util/MultiProcessesLock.java new file mode 100644 index 0000000..933101c --- /dev/null +++ b/Java/src/cn/edu/xmut/util/MultiProcessesLock.java @@ -0,0 +1,46 @@ +package cn.edu.xmut.util; + +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.channels.FileChannel; +import java.nio.channels.FileLock; + +public class MultiProcessesLock { + + private RandomAccessFile raf; + private FileChannel channel = null; + private FileLock lock = null; + + public MultiProcessesLock(String filename) { + RandomAccessFile raf; + try { + raf = new RandomAccessFile(filename, "rw"); + raf.seek(raf.length()); + channel = raf.getChannel(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public FileLock lock() { + try { + return channel.tryLock(); + } catch (Exception e) { + return null; + } + + } + + public void releaseLock() { + if (lock != null) { + try { + lock.release(); + lock = null; + } catch (IOException e) { + e.printStackTrace(); + } + } + + } + +} diff --git a/Java/src/cn/edu/xmut/util/UtilsFunctions.java b/Java/src/cn/edu/xmut/util/UtilsFunctions.java new file mode 100644 index 0000000..5ae23cb --- /dev/null +++ b/Java/src/cn/edu/xmut/util/UtilsFunctions.java @@ -0,0 +1,86 @@ +package cn.edu.xmut.util; + +import java.io.IOException; + +public class UtilsFunctions { + public static int model_index = 1; + + static synchronized int getFreeModelIndex(int cores) { + model_index = (model_index + 1) % cores; + return model_index; + } + + public static void println(String message) { + + try { + String[] commands = new String[] { "/bin/bash", "-c", + "echo '" + message + "'>>/home/application/debug1.out" }; + + Process pr = java.lang.Runtime.getRuntime().exec(commands); + pr.waitFor(); + + } catch (IOException e) { + + e.printStackTrace(); + + } catch (InterruptedException e) { + e.printStackTrace(); + } + + } + + public static void exeComand(String command) { + try { + String[] commands = new String[] { "/bin/bash", "-c", + command + "&>>/home/application/debug.out" }; + Process pr = java.lang.Runtime.getRuntime().exec(commands); + pr.waitFor(); + + } catch (IOException e) { + + e.printStackTrace(); + + } catch (InterruptedException e) { + e.printStackTrace(); + } + + } + + public static void exeComandWindows(String command) { + try { + + //System.out.println(command); + String[] commands = new String[] { "cmd", "/c", + command }; + Process pr = java.lang.Runtime.getRuntime().exec(commands); + pr.waitFor(); + + } catch (IOException e) { + + e.printStackTrace(); + + } catch (InterruptedException e) { + e.printStackTrace(); + } + + } + + public static String pad(String type, int input, int length) { + String tString = input + ""; + int len = tString.length(); + for (int i = 0; i < length - len; i++) { + tString = type + tString; + } + return tString; + } + + public static String pad(String type, String input, int length, + String posfix) { + String tString = input + ""; + int len = tString.length(); + for (int i = 0; i < length - len; i++) { + tString = type + tString; + } + return tString + posfix; + } +} diff --git a/Java/src/config.txt b/Java/src/config.txt new file mode 100644 index 0000000..643aa86 --- /dev/null +++ b/Java/src/config.txt @@ -0,0 +1,3 @@ +basepath=E:\\projects\\p17\\ +cores=4 +os=Linux \ No newline at end of file diff --git a/Scala/driver.scala b/Scala/driver.scala new file mode 100644 index 0000000..927dda9 --- /dev/null +++ b/Scala/driver.scala @@ -0,0 +1,49 @@ +:require E:\projects\p17\lib\commons-io-2.0.jar +:require E:\projects\p17\lib\graphswat.jar +import scala.reflect.ClassTag +import org.apache.spark._ +import org.apache.spark.rdd.RDD +import org.apache.spark.graphx._ +import cn.edu.xmut.model._ + +def parseSubbasin(str: String): (VertexId, (Int, Int)) = { + val token = str.split("\\s+") + (token(0).toLong, (token(1).toInt, 0)) +} + +def parseStream(str: String): Edge[Int] = { + val token = str.split("\\s+") + Edge(token(0).toLong, token(1).Int) +} + +val subbasins: RDD[(VertexId, (Int, Int))] = sc.textFile("E:/projects/p17/subbasin.txt").map(parseSubbasin(_)); + +val streams: RDD[Edge[Int]] = sc.textFile("E:/projects/p17/stream.txt").map(parseStream(_)); + +val graph = Graph(subbasins, streams).partitionBy(PartitionStrategy.RandomVertexCut,4) + +graph.pregel(0, Int.MaxValue, EdgeDirection.Out)( + (vid, usc, msg) => { + println("in vprog") + if (usc._1 == (msg + usc._2)) { + Model.call(vid) + //println("in vprog:"+vid) + } + var rs = msg + usc._2 + (usc._1, rs) + }, + triplet => { + if (triplet.srcAttr._1 == triplet.srcAttr._2) { + //println("in sendmsg1: " + triplet.srcId + " " + triplet.dstId + " " + 1) + Iterator((triplet.dstId, 1)) + } else { + //println("in sendmsg2: " + triplet.srcId + " " + triplet.dstId + " " + 0) + Iterator((triplet.dstId, 0)) + } + + }, + (msg1, msg2) => { + println("in merge: " + msg1 + " " + msg2 + " result:" + (msg1 + msg2)) + msg1 + msg2 + } +) \ No newline at end of file diff --git a/test-models.part01.rar b/test-models.part01.rar new file mode 100644 index 0000000..db13e59 Binary files /dev/null and b/test-models.part01.rar differ diff --git a/test-models.part02.rar b/test-models.part02.rar new file mode 100644 index 0000000..7517af0 Binary files /dev/null and b/test-models.part02.rar differ diff --git a/test-models.part03.rar b/test-models.part03.rar new file mode 100644 index 0000000..9949124 Binary files /dev/null and b/test-models.part03.rar differ diff --git a/test-models.part04.rar b/test-models.part04.rar new file mode 100644 index 0000000..cab2537 Binary files /dev/null and b/test-models.part04.rar differ diff --git a/test-models.part05.rar b/test-models.part05.rar new file mode 100644 index 0000000..a0b6564 Binary files /dev/null and b/test-models.part05.rar differ diff --git a/tutorial for GP-SWAT.docx b/tutorial for GP-SWAT.docx new file mode 100644 index 0000000..9ebe44c Binary files /dev/null and b/tutorial for GP-SWAT.docx differ diff --git a/tutorial.zip b/tutorial.zip new file mode 100644 index 0000000..7803860 Binary files /dev/null and b/tutorial.zip differ