1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.themis.solver.proj;
18
19 import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
20 import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
21 import io.github.tonywasher.joceanus.themis.parser.base.ThemisChar;
22 import io.github.tonywasher.joceanus.themis.parser.base.ThemisDataResource;
23 import io.github.tonywasher.joceanus.themis.parser.project.ThemisModule;
24 import io.github.tonywasher.joceanus.themis.parser.project.ThemisPackage;
25 import io.github.tonywasher.joceanus.themis.solver.proj.ThemisSolverDef.ThemisSolverModuleDef;
26 import io.github.tonywasher.joceanus.themis.solver.proj.ThemisSolverDef.ThemisSolverProjectDef;
27
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.LinkedHashMap;
31 import java.util.List;
32 import java.util.Map;
33
34
35
36
37 public class ThemisSolverModule
38 implements ThemisSolverModuleDef {
39
40
41
42 private static final MetisFieldSet<ThemisSolverModule> FIELD_DEFS = MetisFieldSet.newFieldSet(ThemisSolverModule.class);
43
44
45
46
47 static {
48 FIELD_DEFS.declareLocalField(ThemisDataResource.DATA_PROJECT, ThemisSolverModule::getOwningProject);
49 FIELD_DEFS.declareLocalField(ThemisDataResource.DATA_PACKAGES, ThemisSolverModule::getPackages);
50 FIELD_DEFS.declareLocalField(ThemisDataResource.DATA_UNDERLYING, ThemisSolverModule::getUnderlyingModule);
51 }
52
53
54
55
56 public static final String ROOT = "";
57
58
59
60
61 private final ThemisSolverProjectDef theProject;
62
63
64
65
66 private final ThemisModule theModule;
67
68
69
70
71 private final Map<String, ThemisSolverPackage> thePackages;
72
73
74
75
76
77
78
79 ThemisSolverModule(final ThemisSolverProjectDef pProject,
80 final ThemisModule pModule) {
81
82 theProject = pProject;
83 theModule = pModule;
84
85
86 thePackages = new LinkedHashMap<>();
87 for (ThemisPackage myPackage : theModule.getPackages()) {
88 final ThemisSolverPackage mySolverPackage = new ThemisSolverPackage(this, myPackage);
89 thePackages.put(mySolverPackage.getPackageName(), mySolverPackage);
90 }
91
92
93 createPlaceHolders();
94 }
95
96 @Override
97 public MetisFieldSet<ThemisSolverModule> getDataFieldSet() {
98 return FIELD_DEFS;
99 }
100
101 @Override
102 public String formatObject(final OceanusDataFormatter pFormatter) {
103 return toString();
104 }
105
106 @Override
107 public ThemisSolverProjectDef getOwningProject() {
108 return theProject;
109 }
110
111 @Override
112 public ThemisModule getUnderlyingModule() {
113 return theModule;
114 }
115
116
117
118
119
120
121 public Map<String, ThemisSolverPackage> getPackages() {
122 return thePackages;
123 }
124
125 @Override
126 public String toString() {
127 return theModule.toString();
128 }
129
130
131
132
133
134
135 public boolean isInError() {
136 final ThemisSolverPackage myRoot = getRoot();
137 return myRoot != null && myRoot.isInError();
138 }
139
140
141
142
143 private void createPlaceHolders() {
144
145 final HashMap<String, ThemisSolverPackage> myPackageMap = new HashMap<>(thePackages);
146 final List<ThemisSolverPackage> myPackages = new ArrayList<>(thePackages.values());
147
148
149 for (ThemisSolverPackage myPackage : myPackages) {
150
151 addParentLink(myPackageMap, myPackage);
152 }
153 }
154
155
156
157
158
159
160
161 private void addParentLink(final Map<String, ThemisSolverPackage> pPackageMap,
162 final ThemisSolverPackage pPackage) {
163
164 final String myName = pPackage.getPackageName();
165 final int iIndex = myName.lastIndexOf(ThemisChar.PERIOD);
166 final String myParentName = iIndex == -1 ? ROOT : myName.substring(0, iIndex);
167
168
169 ThemisSolverPackage myParent = pPackageMap.get(myParentName);
170
171
172 if (myParent == null) {
173
174 myParent = new ThemisSolverPackage(this, new ThemisPackage(myParentName));
175 pPackageMap.put(myParentName, myParent);
176 thePackages.put(myParentName, myParent);
177
178
179 if (!ROOT.equals(myParentName)) {
180 addParentLink(pPackageMap, myParent);
181 }
182 }
183
184
185 myParent.addChild(pPackage);
186 }
187
188
189
190
191
192
193 public ThemisSolverPackage getRoot() {
194
195 return thePackages.get(ROOT);
196 }
197 }