Skip to content

Package: AttributeGenerationTask

AttributeGenerationTask

nameinstructionbranchcomplexitylinemethod
AttributeGenerationTask(Model, TaskExecutor, InitialGenerator)
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
beginTask()
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
finalizeTask()
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
handleAttribute(Attribute, ClassType)
M: 20 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
handleClass(ClassType)
M: 51 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
handleConstructorOrOperation(ConstructorOrOperation, ClassType)
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
handleGroup(Group)
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: package de.fhdw.wtf.generator.database.tasks;
2:
3: import de.fhdw.wtf.common.ast.Attribute;
4: import de.fhdw.wtf.common.ast.ConstructorOrOperation;
5: import de.fhdw.wtf.common.ast.DatabaseIDSetState;
6: import de.fhdw.wtf.common.ast.Group;
7: import de.fhdw.wtf.common.ast.Model;
8: import de.fhdw.wtf.common.ast.type.AtomicType;
9: import de.fhdw.wtf.common.ast.type.ClassType;
10: import de.fhdw.wtf.common.ast.type.CompositeType;
11: import de.fhdw.wtf.common.ast.type.ListType;
12: import de.fhdw.wtf.common.ast.type.MapType;
13: import de.fhdw.wtf.common.ast.type.ProductType;
14: import de.fhdw.wtf.common.ast.type.SumType;
15: import de.fhdw.wtf.common.ast.type.ThrownType;
16: import de.fhdw.wtf.common.ast.type.TypeProxy;
17: import de.fhdw.wtf.common.ast.visitor.CompositeTypeVisitorException;
18: import de.fhdw.wtf.common.ast.visitor.TypeVisitorException;
19: import de.fhdw.wtf.common.exception.generation.NoTypeIdSetException;
20: import de.fhdw.wtf.common.exception.walker.TaskException;
21: import de.fhdw.wtf.common.task.TaskExecutor;
22: import de.fhdw.wtf.context.model.collections.PersistentList;
23: import de.fhdw.wtf.context.model.collections.PersistentMap;
24: import de.fhdw.wtf.generator.database.generation.InitialGenerator;
25: import de.fhdw.wtf.persistence.exception.PersistenceException;
26: import de.fhdw.wtf.persistence.facade.TypeManager;
27: import de.fhdw.wtf.persistence.meta.Type;
28: import de.fhdw.wtf.persistence.meta.UnidirectionalAssociation;
29: import de.fhdw.wtf.walker.walker.HelperUtils;
30: import de.fhdw.wtf.walker.walker.SimpleWalkerTask;
31:
32: /**
33: * The {@link AttributeGenerationTask} puts all classes of the Model (AST) into the database. It also adds Type-Ids to
34: * the AST.
35: *
36: */
37: public class AttributeGenerationTask extends SimpleWalkerTask {
38:         
39:         /**
40:          * The name of the default attribute 'This'.
41:          */
42:         private static final String THIS_ATTRIBUTE_NAME = "This";
43:         /**
44:          * The name of the default attribute '$generatedObjects'.
45:          */
46:         private static final String GENERATED_OBJECTS_ATTRIBUTE_NAME = "$generatedObjects";
47:         
48:         /**
49:          * Creates an {@link AttributeGenerationTask}.
50:          *
51:          * @param m
52:          * The associated model.
53:          * @param man
54:          * The task manager to use.
55:          * @param generator
56:          * The generator to use.
57:          */
58:         public AttributeGenerationTask(final Model m, final TaskExecutor man, final InitialGenerator generator) {
59:                 super(m, man);
60:                 this.generator = generator;
61:                 this.persistentList = null;
62:                 this.persistentMap = null;
63:         }
64:         
65:         /**
66:          * The generator to use.
67:          */
68:         private final InitialGenerator generator;
69:         /**
70:          * The type used for persistent lists.
71:          */
72:         private Type persistentList;
73:         /**
74:          * The type used for persistent maps.
75:          */
76:         private Type persistentMap;
77:         
78:         @Override
79:         public void handleAttribute(final Attribute a, final ClassType owner) throws TaskException {
80:                 try {
81:                         a.getAttrType().accept(new AttributeTypeVisitor(a, owner));
82:                 } catch (final TaskException e) {
83:                         throw e;
84:                 } catch (final Exception e) {
85:                         throw new TaskException(e);
86:                 }
87:                 
88:         }
89:         
90:         @Override
91:         public void finalizeTask() {
92:                 // Not involved in this task
93:         }
94:         
95:         @Override
96:         public void handleConstructorOrOperation(final ConstructorOrOperation coo, final ClassType owner)
97:                         throws TaskException {
98:                 // Not involved in this task
99:         }
100:         
101:         @Override
102:         public void handleGroup(final Group g) throws TaskException {
103:                 // Not involved in this task
104:         }
105:         
106:         /**
107:          * Creates the fields 'This' and '$generatedObjects' as attributes of the class for classId in the persistence.
108:          *
109:          * @param c
110:          * The class to create the fields for.
111:          */
112:         @Override
113:         public void handleClass(final ClassType c) throws TaskException {
114:                 long classId;
115:                 try {
116:                         classId = c.getTypeId().getId();
117:                         final TypeManager typeManager = this.generator.getClassFacade().getTypeManager();
118:•                        if (!typeManager.existsType("de.fhdw.wtf.context.model.collections.MutableMap")) {
119:                                 this.generator.createClass("de.fhdw.wtf.context.model.collections.MutableMap", false, false);
120:                         }
121:                         this.generator.createUnidirectionalAssociation(THIS_ATTRIBUTE_NAME, false, false, classId, classId);
122:                         this.generator.createUnidirectionalAssociation(
123:                                         GENERATED_OBJECTS_ATTRIBUTE_NAME,
124:                                         false,
125:                                         false,
126:                                         classId,
127:                                         this.generator.mapNameToType(PersistentMap.class.getName()).getId());
128:                 } catch (final NoTypeIdSetException | PersistenceException e) {
129:                         throw new TaskException(e);
130:                 }
131:         }
132:         
133:         @Override
134:         public void beginTask() throws TaskException {
135:                 this.persistentList = this.generator.mapNameToType(PersistentList.class.getName());
136:                 this.persistentMap = this.generator.mapNameToType(PersistentMap.class.getName());
137:         }
138:         
139:         /**
140:          * Handles an attribute's type.
141:          */
142:         private class AttributeTypeVisitor implements TypeVisitorException<Exception> {
143:                 
144:                 /**
145:                  * The attribute to operate on.
146:                  */
147:                 private final Attribute a;
148:                 /**
149:                  * The owner type of the attribute.
150:                  */
151:                 private final ClassType owner;
152:                 
153:                 /**
154:                  * Creates an {@link AttributeTypeVisitor}.
155:                  *
156:                  * @param a
157:                  * The attribute to operate on.
158:                  * @param owner
159:                  * The owner type of the attribute.
160:                  */
161:                 AttributeTypeVisitor(final Attribute a, final ClassType owner) {
162:                         this.a = a;
163:                         this.owner = owner;
164:                 }
165:                 
166:                 @Override
167:                 public void handle(final AtomicType s) throws Exception {
168:                         final UnidirectionalAssociation newAsso =
169:                                         AttributeGenerationTask.this.generator.createUnidirectionalAssociation(
170:                                                         this.a.getName(),
171:                                                         true,
172:                                                         true,
173:                                                         this.owner.getTypeId().getId(),
174:                                                         s.getTypeId().getId());
175:                         this.a.setAttributeId(new DatabaseIDSetState(newAsso.getId()));
176:                 }
177:                 
178:                 @Override
179:                 public void handle(final CompositeType c) throws Exception {
180:                         c.accept(new CompositeAttributeTypeVisitor(this.a, this.owner));
181:                 }
182:                 
183:                 @Override
184:                 public void handle(final TypeProxy s) throws Exception {
185:                         HelperUtils.getReferencedType(s).accept(this);
186:                 }
187:         }
188:         
189:         /**
190:          * Handles an attribute's composite type.
191:          */
192:         private class CompositeAttributeTypeVisitor implements CompositeTypeVisitorException<Exception> {
193:                 /**
194:                  * The attribute to operate on.
195:                  */
196:                 private final Attribute a;
197:                 /**
198:                  * The owner type of the attribute.
199:                  */
200:                 private final ClassType owner;
201:                 
202:                 /**
203:                  * Creates an {@link AttributeTypeVisitor}.
204:                  *
205:                  * @param a
206:                  * The attribute to operate on.
207:                  * @param owner
208:                  * The owner type of the attribute.
209:                  */
210:                 CompositeAttributeTypeVisitor(final Attribute a, final ClassType owner) {
211:                         this.a = a;
212:                         this.owner = owner;
213:                 }
214:                 
215:                 @Override
216:                 public void handle(final ListType list) throws Exception {
217:                         final UnidirectionalAssociation newAsso =
218:                                         AttributeGenerationTask.this.generator.createUnidirectionalAssociation(
219:                                                         CompositeAttributeTypeVisitor.this.a.getName(),
220:                                                         true,
221:                                                         true,
222:                                                         CompositeAttributeTypeVisitor.this.owner.getTypeId().getId(),
223:                                                         AttributeGenerationTask.this.persistentList.getId());
224:                         CompositeAttributeTypeVisitor.this.a.setAttributeId(new DatabaseIDSetState(newAsso.getId()));
225:                 }
226:                 
227:                 @Override
228:                 public void handle(final MapType map) throws Exception {
229:                         final UnidirectionalAssociation newAsso =
230:                                         AttributeGenerationTask.this.generator.createUnidirectionalAssociation(
231:                                                         CompositeAttributeTypeVisitor.this.a.getName(),
232:                                                         true,
233:                                                         true,
234:                                                         CompositeAttributeTypeVisitor.this.owner.getTypeId().getId(),
235:                                                         AttributeGenerationTask.this.persistentMap.getId());
236:                         CompositeAttributeTypeVisitor.this.a.setAttributeId(new DatabaseIDSetState(newAsso.getId()));
237:                 }
238:                 
239:                 @Override
240:                 public void handle(final ProductType product) throws Exception {
241:                         final UnidirectionalAssociation newAsso =
242:                                         AttributeGenerationTask.this.generator.createUnidirectionalAssociation(
243:                                                         CompositeAttributeTypeVisitor.this.a.getName(),
244:                                                         true,
245:                                                         true,
246:                                                         CompositeAttributeTypeVisitor.this.owner.getTypeId().getId(),
247:                                                         product.getTypeId().getId());
248:                         CompositeAttributeTypeVisitor.this.a.setAttributeId(new DatabaseIDSetState(newAsso.getId()));
249:                 }
250:                 
251:                 @Override
252:                 public void handle(final SumType sum) throws Exception {
253:                         final UnidirectionalAssociation newAsso =
254:                                         AttributeGenerationTask.this.generator.createUnidirectionalAssociation(
255:                                                         CompositeAttributeTypeVisitor.this.a.getName(),
256:                                                         true,
257:                                                         true,
258:                                                         CompositeAttributeTypeVisitor.this.owner.getTypeId().getId(),
259:                                                         sum.getTypeId().getId());
260:                         CompositeAttributeTypeVisitor.this.a.setAttributeId(new DatabaseIDSetState(newAsso.getId()));
261:                 }
262:                 
263:                 @Override
264:                 public void handle(final ThrownType thrownType) throws Exception {
265:                         throw new TaskException("A type that should be thrown is not possible in an attribute.");
266:                 }
267:         }
268: }