|
| 1 | +package com.neuronrobotics.bowlerstudio.scripting.cadoodle; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.Map.Entry; |
| 7 | + |
| 8 | +import com.google.gson.annotations.Expose; |
| 9 | +import com.neuronrobotics.bowlerstudio.scripting.ScriptingEngine; |
| 10 | + |
| 11 | +import eu.mihosoft.vrl.v3d.parametrics.CSGDatabaseInstance; |
| 12 | + |
| 13 | +public class CaDoodleParameters { |
| 14 | + @Expose(serialize = true, deserialize = true) |
| 15 | + private ArrayList<Map.Entry<String, String>> params; |
| 16 | + |
| 17 | + private HashMap<String, Double> values = null; |
| 18 | + |
| 19 | + private CSGDatabaseInstance db; |
| 20 | + |
| 21 | + public String getString(String key) { |
| 22 | + for (Map.Entry<String, String> m : getParams()) { |
| 23 | + if (m.getKey().contentEquals(key)) |
| 24 | + return m.getValue(); |
| 25 | + } |
| 26 | + throw new NumberFormatException(); |
| 27 | + } |
| 28 | + public void delete(String key) { |
| 29 | + Map.Entry<String, String> set = null; |
| 30 | + for (Map.Entry<String, String> m : getParams()) { |
| 31 | + if (m.getKey().contentEquals(key)) { |
| 32 | + set = m; |
| 33 | + break; |
| 34 | + } |
| 35 | + } |
| 36 | + if(set!=null) |
| 37 | + params.remove(set); |
| 38 | + } |
| 39 | + public void set(String key, Object value) { |
| 40 | + Map.Entry<String, String> set = null; |
| 41 | + for (Map.Entry<String, String> m : getParams()) { |
| 42 | + if (m.getKey().contentEquals(key)) { |
| 43 | + set = m; |
| 44 | + break; |
| 45 | + } |
| 46 | + } |
| 47 | + if (set == null) { |
| 48 | + set =Map.entry(key,value.toString()); |
| 49 | + getParams().add(set); |
| 50 | + } |
| 51 | + set.setValue(value.toString()); |
| 52 | + values=null; |
| 53 | + } |
| 54 | + public ArrayList<String> keys(){ |
| 55 | + ArrayList<String> keys=new ArrayList<String>(); |
| 56 | + for(Entry<String, String> e:getParams()) { |
| 57 | + keys.add(e.getKey()); |
| 58 | + } |
| 59 | + return keys; |
| 60 | + } |
| 61 | + private ArrayList<Map.Entry<String, String>> getParams() { |
| 62 | + if (params == null) { |
| 63 | + params = new ArrayList<Map.Entry<String, String>>(); |
| 64 | + } |
| 65 | + return params; |
| 66 | + } |
| 67 | + |
| 68 | + public double getValue(String key) throws Exception { |
| 69 | + return getValues().get(key).doubleValue(); |
| 70 | + } |
| 71 | + |
| 72 | + private HashMap<String, Double> getValues() throws Exception { |
| 73 | + if (values == null) { |
| 74 | + String code = "HashMap<String,Double> numbers = new HashMap<>()\n"; |
| 75 | + String vars = ""; |
| 76 | + String equs = ""; |
| 77 | + |
| 78 | + for (Map.Entry<String, String> m : getParams()) { |
| 79 | + // System.out.println(line); |
| 80 | + String value = m.getValue(); |
| 81 | + String key =m.getKey(); |
| 82 | + String reconstructed = key + "=" + value; |
| 83 | + try { |
| 84 | + Double.parseDouble(value); |
| 85 | + vars += reconstructed + "\n"; |
| 86 | + vars += "numbers.put(\"" + key + "\"," + key + ");\n"; |
| 87 | + } catch (NumberFormatException ex) { |
| 88 | + equs += reconstructed + "\n"; |
| 89 | + equs += "numbers.put(\"" + key + "\"," + key + ");\n"; |
| 90 | + } |
| 91 | + } |
| 92 | + code += vars; |
| 93 | + code += equs; |
| 94 | + code += "return numbers"; |
| 95 | + // println code |
| 96 | + values = (HashMap<String, Double>) ScriptingEngine.inlineScriptStringRun(getDb(), code, null, "Groovy"); |
| 97 | + } |
| 98 | + return values; |
| 99 | + } |
| 100 | + |
| 101 | + public CSGDatabaseInstance getDb() { |
| 102 | + return db; |
| 103 | + } |
| 104 | + |
| 105 | + public void setDb(CSGDatabaseInstance db) { |
| 106 | + this.db = db; |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments