1 9 10 16 17 package org.lsmp.djepExamples; 18 19 import org.lsmp.djep.groupJep.*; 20 import org.lsmp.djep.groupJep.groups.*; 21 import org.lsmp.djep.groupJep.interfaces.*; 22 import org.lsmp.djep.groupJep.values.*; 23 import org.nfunk.jep.*; 24 import java.io.*; 25 import java.math.*; 26 import java.util.*; 27 28 39 class GroupConsole { 40 41 42 private String prompt; 43 44 45 private BufferedReader br; 46 47 private GroupJep j; 48 49 String groupName="Q"; 50 51 boolean dumpTree = false; 52 53 boolean dumpSymbols=false; 54 55 56 public GroupConsole() { 57 prompt = "JEP > "; 58 br = new BufferedReader(new InputStreamReader(System.in)); 59 60 } 61 62 63 public static void main(String args[]) throws IOException { 64 GroupConsole c = new GroupConsole(); 65 c.run(args); 66 } 67 68 69 public void run(String args[]) throws IOException { 70 String command=""; 71 j = new GroupJep(new Rationals()); 72 j.setAllowAssignment(true); 74 j.setAllowUndeclared(true); 75 String temp=""; 76 for(int i=0;i<args.length;++i) 77 { 78 if(args[i].equals("--dumpTree")) 79 dumpTree = true; 80 else if(args[i].equals("--dumpSymbols")) 81 dumpSymbols = true; 82 else 83 temp += " " + args[i]; 84 } 85 if(temp.length()!=0) 86 { 87 j.parseExpression(temp); 88 if (j.hasError()) 89 System.out.println(j.getErrorInfo()); 90 else 91 { 92 Object val = j.getValueAsObject(); 93 if(j.hasError()) 94 System.out.println(j.getErrorInfo()); 95 else 96 System.out.println(val); 97 } 98 } 99 else 100 { 101 System.out.println("Using rationals. To change group type 'group g' where g is one of: "); 102 System.out.println("I - integers, Q - Rationals, Rn - Reals(n dp),"+ 103 " or Zn for Integers mod n"); 104 System.out.println("A n, Algebraic Intergers Z(sqrt(n))"); 105 System.out.println("JEP - Enter q to quit"); 106 108 System.out.println("Current Group: "+j.getGroup().toString()); 109 110 while ((command = getCommand()) != null) 111 { 112 try 113 { 114 Node node = j.parse(command); 115 Object value = j.evaluate(node); 116 if(value instanceof HasComplexValueI) 117 System.out.println(value.toString()+"=" 118 +((HasComplexValueI) value).getComplexValue()); 119 else 120 System.out.println(value.toString()); 121 } 122 catch(Exception e) 123 { 124 System.out.println(e.getClass().getName()); 125 System.out.println(e.getMessage()); 126 e.printStackTrace(); 127 } 128 } 129 } 130 131 } 132 String [] split(String s) 134 { 135 StringTokenizer st = new StringTokenizer(s); 136 int tokCount = st.countTokens(); 137 String res[] = new String [tokCount]; 138 int pos=0; 139 while (st.hasMoreTokens()) { 140 res[pos++]=st.nextToken(); 141 } 142 return res; 143 } 144 149 private String getCommand() throws IOException { 150 String s; 151 152 System.out.print(prompt); 153 154 if (br == null) 155 return null; 156 157 if ( (s = br.readLine()) == null) 158 return null; 159 160 if (s.equals("q") 161 || s.equals("quit") 162 || s.equals("exit")) 163 return null; 164 165 if(s.equals("group")) 166 { 167 System.out.println("Current Group: "+j.getGroup().toString()); 168 return getCommand(); 169 } 170 else if(s.startsWith("group")) 171 { 172 try 173 { 174 groupName = s.substring(6); 175 String splits[] = split(groupName); 177 System.out.println("Changing group to '"+groupName+"'"); 178 if(splits[0].equals("Z")) 179 { 180 j = new GroupJep(new Integers()); 181 } 182 else if(splits[0].equals("Q")) 183 { 184 j = new GroupJep(new Rationals()); 185 } 186 else if(splits[0].equals("R")) 187 { 188 j = new GroupJep(new BigReals( 189 Integer.parseInt(splits[1]), 190 BigDecimal.ROUND_HALF_EVEN )); 191 } 192 else if(splits[0].equals("P")) 193 { 194 j = new GroupJep(new PermutationGroup( 195 Integer.parseInt(splits[1])) 196 ); 197 } 198 else if(splits[0].equals("Zn")) 199 { 200 j = new GroupJep(new Zn(new BigInteger(splits[1]))); 201 } 202 else if(splits[0].equals("Qu")) 203 { 204 j = new GroupJep(new Quartonians()); 205 j.addStandardConstants(); 206 } 207 else if(splits[0].equals("extend") && splits.length == 2) 208 { 209 RingI ring = (RingI) j.getGroup(); 210 j = new GroupJep(new FreeGroup(ring, splits[1])); 211 j.addStandardConstants(); 212 } 213 else if(splits[0].equals("extend")) 214 { 215 RingI ring = (RingI) j.getGroup(); 216 217 int deg = splits.length-3; 218 Number coeffs[] = new Number [deg+1]; 219 for(int i=0;i<=deg;++i) 220 coeffs[i] = ring.valueOf(splits[splits.length-i-1]); 221 Polynomial p1 = new Polynomial(ring,splits[1],coeffs); 222 223 j = new GroupJep(new AlgebraicExtension(ring, p1)); 224 j.addStandardConstants(); 225 } 226 else 227 System.out.println("Invalid group: "+groupName); 228 229 System.out.println("Current Group: "+j.getGroup().toString()); 230 return getCommand(); 231 } 232 catch(Exception e) 233 { 234 e.printStackTrace(); 235 return getCommand(); 236 } 237 } 238 return s; 239 } 240 } 241 | Popular Tags |