001/** 002 * Copyright (C) 2007, Jens Lehmann 003 * 004 * This file is part of DL-Learner. 005 * 006 * DL-Learner is free software; you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation; either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * DL-Learner is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 018 * 019 */ 020 021package org.dllearner.cli; 022 023import java.io.BufferedReader; 024import java.io.File; 025import java.io.FileWriter; 026import java.io.InputStreamReader; 027import java.io.RandomAccessFile; 028import java.util.ArrayList; 029import java.util.Arrays; 030import java.util.HashMap; 031import java.util.Iterator; 032import java.util.List; 033import java.util.Map; 034 035/** 036 * A tool to quickly start a learning example. It detects all conf files in the 037 * examples directory and offers the user to start one of them. 038 * 039 * @author Sebastian Hellmann 040 * @author Jens Lehmann 041 */ 042public class QuickStart { 043 044// static HashMap<String, ArrayList<String>> hm = null; 045 static String pm = "..";// pathmodifier 046 static List<String> conffiles = new ArrayList<>(); 047 048 public static void main(String[] args) { 049 050 051 052 String lastused = readit(); 053 String tab = " "; 054 int the_Number = 0; 055 ArrayList<String> finalSelection = new ArrayList<>(); 056 finalSelection.add("na"); 057 058 HashMap<String, ArrayList<String>> hm = new HashMap<>(); 059 String path = pm + File.separator + "examples"; 060 File f = new File(path); 061 getAllConfs(f, path, hm); 062 063 // System.out.println(hm.size()); 064 Iterator<String> i = hm.keySet().iterator(); 065 Object[] sort = new Object[hm.size()]; 066 int count = 0; 067 while (i.hasNext()) 068 sort[count++] = i.next(); 069 Arrays.sort(sort); 070 Object s; 071 String s1 = ""; 072 // String tmp=""; 073 for (Object aSort : sort) { 074 s = aSort; 075 s1 = (String) s; 076 if (s1.startsWith(pm + "\\examples\\") || s1.startsWith(pm + "/examples/")) 077 System.out.println(s1.substring(10).toUpperCase()); 078 else 079 System.out.println(s); 080 081 ArrayList<String> al = hm.get(s); 082 String[] files = new String[al.size()]; 083 for (int j = 0; j < al.size(); j++) { 084 files[j] = al.get(j); 085 } 086 Arrays.sort(files); 087 for (String file : files) { 088 the_Number++; 089 finalSelection.add(the_Number, s + file + ".conf");// tmp=the_Number+":"+tab+files[j]; 090 System.out.println(" " + the_Number + ":" + tab + file + ""); 091 092 } 093 // System.out.println(FinalSelection.get(1)); 094 095 }// end while 096 System.out.println("Last Used: " + lastused + "\n" 097 + "->press enter to use it again, else choose number:"); 098 boolean number = false; 099 try { 100 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 101 int target = 0; 102 String selected = ""; 103 boolean query=false; 104 while (true) { 105 String cmd = br.readLine(); 106 if(cmd.equalsIgnoreCase("q")|| cmd.equalsIgnoreCase("query")) { 107 query = !query; 108 System.out.println("Query mode switched. Now: "+query); 109 continue; 110 }else if(cmd.equalsIgnoreCase("exit") || cmd.equalsIgnoreCase("quit")) { 111 System.out.println("Bye..."); 112 System.exit(0); 113 } 114 try { 115 if (cmd.length() == 0) { 116 number = false; 117 break; 118 } 119 target = Integer.parseInt(cmd); 120 number = true; 121 break; 122 } catch (Exception e) { 123 124 for(String one:conffiles){ 125 126 if(one.contains(cmd)){ 127 System.out.println("Did you mean "+one+" ? (Press enter to confirm,\n" + 128 "any key+enter for another try)"); 129 cmd = br.readLine(); 130 if(cmd.length()==0){ 131 writeit(one); 132 if(!query) { 133 134 CLI.main(new String[] { one }); 135 }else { 136 CLI.main(new String[] {"-q",one}); 137 } 138 return; 139 }else {break;} 140 } 141 } 142 143 System.out.println("Not a number"); 144 continue; 145 } 146 147 }// end while 148 if (number) { 149 try { 150 selected = finalSelection.get(target); 151 } catch (Exception e) { 152 System.out.println("number does not exist"); 153 } 154 writeit(selected); 155 System.out.println(selected); 156 } else if (!number) { 157 selected = lastused; 158 } 159 160 // DLLearner.main(new String[] { Selected }); 161 if(!query) { 162 CLI.main(new String[] { selected }); 163 }else { 164 CLI.main(new String[] {"-q",selected}); 165 } 166 167 } catch (Exception e) { 168 e.printStackTrace(); 169 }// System.out.println(s+" : "+hm.get(s).get(0)); 170 171 // System.out.println(f.isDirectory()+f.getAbsolutePath()); 172 } 173 174 public static void getAllConfs(File f, String path, Map<String, ArrayList<String>> confs) { 175 path = path + File.separator; 176 // System.out.println(path); 177 String[] act = f.list();System.out.println(f); 178 for (String anAct : act) { 179 // System.out.println(act[i]); 180 181 if (new File(path + anAct).isDirectory()) { 182 183 getAllConfs(new File(path + anAct), path + anAct, confs); 184 // al.add(new File(act[i])); 185 } else if (anAct.endsWith(".conf")) { 186 if (confs.get(path) == null) { 187 confs.put(path, new ArrayList<>()); 188 } 189 confs.get(path).add(anAct.substring(0, anAct.length() - 5)); 190 conffiles.add(path + anAct); 191 // System.out.println(act[i].substring(0,act[i].length()-5)); 192 // System.out.println(hm.get(path).size()); 193 // hm.put(new 194 // File(act[i]).getAbsolutePath(),act[i].substring(0,act[i].length()-4)); 195 } 196 }// end for 197 198 } 199 200 static void writeit(String lastused) { 201 try { 202 FileWriter fw = new FileWriter(".lastUsedExample"); 203 fw.write(lastused); 204 fw.flush(); 205 } catch (Exception e) { 206 e.printStackTrace(); 207 } 208 } 209 210 static String readit() { 211 String lu = ""; 212 try { 213 RandomAccessFile raf = new RandomAccessFile(".lastUsedExample", "r"); 214 String line = ""; 215 while ((line = raf.readLine()) != null) { 216 lu = line; 217 } 218 } catch (Exception e) { 219 writeit("na"); 220 } 221 return lu; 222 } 223 224 static String readCP() { 225 String lu = ""; 226 try { 227 RandomAccessFile raf = new RandomAccessFile("classpath.start", "r"); 228 String line = ""; 229 while ((line = raf.readLine()) != null) { 230 lu += line; 231 } 232 } catch (Exception e) { 233 e.printStackTrace(); 234 } 235 return lu; 236 } 237 238}