1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.gwe.p2elv2;
18
19 import java.util.ArrayList;
20 import java.util.Comparator;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26 import java.util.TreeMap;
27
28 import org.gwe.p2elv2.model.PVariable;
29 import org.gwe.p2elv2.model.PVariableArray;
30 import org.gwe.utils.VelocityUtils;
31
32
33
34
35
36 public class PPermutation extends HashMap<PVariable, PVarValue> {
37
38 private List<PVariable> pendingSortedVars = new ArrayList<PVariable>();
39
40 public PPermutation() {}
41
42 public void addPendingVar(PVariable var) {
43 pendingSortedVars.add(var);
44 }
45
46 public PPermutation clone() {
47 PPermutation result = new PPermutation();
48 result.putAll(this);
49 result.pendingSortedVars.addAll(this.pendingSortedVars);
50 return result;
51 }
52
53 public PPermutation cloneAddingVar(PVariable var, PVarValue<?> value) {
54 PPermutation clone = new PPermutation();
55 clone.putAll(this);
56 clone.put(var, value);
57 return clone;
58 }
59
60 public PPermutation cloneOnlyWithVars(Set<String> varNames) {
61 PPermutation clone = new PPermutation();
62 for (Map.Entry<PVariable, PVarValue> entry : this.entrySet()) {
63 String varName = entry.getKey().getName();
64 if (varNames.contains(varName))
65 clone.put(entry.getKey(), entry.getValue());
66 }
67
68 return clone;
69 }
70
71 public void processSystemDependencies(PStatementContext ctx) throws P2ELDependentVariableNotResolvedException, P2ELFunctionNotSupported {
72 addVars(ctx.getSystemVars());
73
74 Set<PVariable> processedVars = new HashSet<PVariable>();
75 for (PVariable var : pendingSortedVars) {
76 if (var.isRuntime() || var.isDependentOnRuntimeVars()) break;
77 put(var, var.generateValueSpace(this, ctx).get(0));
78 }
79 pendingSortedVars.removeAll(processedVars);
80 }
81
82 public void processRuntimedependencies(PStatementContext ctx) throws P2ELDependentVariableNotResolvedException, P2ELFunctionNotSupported {
83 addVars(ctx.getRuntimeVars());
84
85 for (PVariable var : pendingSortedVars)
86 put(var, var.generateValueSpace(this, ctx).get(0));
87 pendingSortedVars.clear();
88 }
89
90 private void addVars(Map<String, String> vars) {
91 for (Map.Entry<String, String> var : vars.entrySet())
92 put(new PVariable(var.getKey(), var.getValue()), new PVarValue<String>(var.getValue()));
93 }
94
95 public List<PProcessor> getProcessors(PProcessorType procType) {
96 List<PProcessor> result = new ArrayList<PProcessor>();
97 for (PVarValue value : values())
98 result.addAll(value.getProcessors(procType));
99 return result;
100 }
101
102
103
104
105 public String merge(String template) {
106 return VelocityUtils.merge(asVTLModel(), template);
107 }
108
109 public Map<String, Object> asVTLModel() {
110 Map<String, Object> result = new HashMap<String, Object>();
111 for (Map.Entry<PVariable, PVarValue> entry : this.entrySet())
112 result.put(entry.getKey().getName(), entry.getValue().getVTLModel());
113 return result;
114 }
115
116 public List<String> getEntriesStr() {
117 List<String> result = new ArrayList<String>();
118 for (Map.Entry<PVariable, PVarValue> entry : getSortedCopy().entrySet())
119 result.add(entry.getKey().getFullName() + "=" + entry.getValue());
120 return result;
121 }
122
123 private Map<PVariable, PVarValue> getSortedCopy() {
124 Map<PVariable, PVarValue> sorted = new TreeMap<PVariable, PVarValue>(new Comparator<PVariable>() {
125 public int compare(PVariable var1, PVariable var2) {
126 return var1.getFullName().compareTo(var2.getFullName());
127 }
128 });
129 sorted.putAll(this);
130 return sorted;
131 }
132
133 public TreeMap<String, Object> asFriendlyTreeMap() {
134 TreeMap<String, Object> result = new TreeMap<String, Object>();
135 for (Map.Entry<PVariable, PVarValue> entry : entrySet()) {
136 PVariable var = entry.getKey();
137 PVarValue varValue = entry.getValue();
138 Object value = varValue.getVTLModel();
139 if (value instanceof Map && var instanceof PVariableArray) {
140 for (PVariable varChild : ((PVariableArray)var).getVars())
141 result.put(varChild.getFullName(), ((Map)value).get(varChild.getDimension()));
142 } else
143 result.put(var.getFullName(), value);
144 }
145 return result;
146 }
147
148 public String toString() {
149 String result = "(";
150 for (String entryStr : getEntriesStr()) {
151 if (!result.equals("(")) result += ",";
152 result += entryStr;
153 }
154 return result + ")";
155 }
156 }
157