/
AttachedAssociation.java
335 lines (259 loc) · 11.8 KB
/
AttachedAssociation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package de.deepamehta.core.impl.service;
import de.deepamehta.core.Association;
import de.deepamehta.core.AssociationType;
import de.deepamehta.core.DeepaMehtaTransaction;
import de.deepamehta.core.RelatedAssociation;
import de.deepamehta.core.RelatedTopic;
import de.deepamehta.core.ResultSet;
import de.deepamehta.core.Role;
import de.deepamehta.core.Topic;
import de.deepamehta.core.TopicRole;
import de.deepamehta.core.Type;
import de.deepamehta.core.model.AssociationModel;
import de.deepamehta.core.model.AssociationRoleModel;
import de.deepamehta.core.model.IndexMode;
import de.deepamehta.core.model.RelatedAssociationModel;
import de.deepamehta.core.model.RelatedTopicModel;
import de.deepamehta.core.model.RoleModel;
import de.deepamehta.core.model.SimpleValue;
import de.deepamehta.core.model.TopicRoleModel;
import de.deepamehta.core.service.ClientState;
import de.deepamehta.core.service.Directive;
import de.deepamehta.core.service.Directives;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;
/**
* An association that is attached to the {@link DeepaMehtaService}.
*/
class AttachedAssociation extends AttachedDeepaMehtaObject implements Association {
// ---------------------------------------------------------------------------------------------- Instance Variables
private Role role1; // Attached object cache
private Role role2; // Attached object cache
private Logger logger = Logger.getLogger(getClass().getName());
// ---------------------------------------------------------------------------------------------------- Constructors
AttachedAssociation(AssociationModel model, EmbeddedService dms) {
super(model, dms);
// init attached object cache
this.role1 = createAttachedRole(model.getRoleModel1());
this.role2 = createAttachedRole(model.getRoleModel2());
}
// -------------------------------------------------------------------------------------------------- Public Methods
// ******************************************
// *** AttachedDeepaMehtaObject Overrides ***
// ******************************************
// === Implementation of the abstract methods ===
@Override
protected String className() {
return "association";
}
@Override
protected void storeUri() {
dms.storage.setAssociationUri(getId(), getUri());
}
@Override
protected void storeTypeUri() {
// remove current assignment
long assocId = dms.objectFactory.fetchAssociationTypeTopic(getId()).getAssociationModel().getId();
dms.deleteAssociation(assocId, null); // clientState=null
// create new assignment
dms.objectFactory.associateWithAssociationType(getId(), getTypeUri());
}
@Override
protected SimpleValue storeSimpleValue() {
return dms.storage.setAssociationValue(getId(), getSimpleValue());
}
@Override
protected void indexValue(IndexMode indexMode, String indexKey, SimpleValue value, SimpleValue oldValue) {
// Note: we do not index association values
// dms.storage.indexAssociationValue(getId(), indexMode, indexKey, value, oldValue);
}
@Override
protected Type getType() {
return dms.getAssociationType(getTypeUri(), null); // FIXME: clientState=null
}
@Override
protected RoleModel createRoleModel(String roleTypeUri) {
return new AssociationRoleModel(getId(), roleTypeUri);
}
// === Deletion ===
@Override
public void delete(Directives directives) {
DeepaMehtaTransaction tx = dms.beginTx();
try {
// delete sub-topics and associations
super.delete(directives);
// delete association itself
logger.info("Deleting " + this);
dms.storage.deleteAssociation(getId());
directives.add(Directive.DELETE_ASSOCIATION, this);
//
tx.success();
} catch (Exception e) {
logger.warning("ROLLBACK!");
throw new RuntimeException("Deleting association failed (" + this + ")", e);
} finally {
tx.finish();
}
}
// **********************************
// *** Association Implementation ***
// **********************************
@Override
public Role getRole1() {
return role1;
}
@Override
public Role getRole2() {
return role2;
}
// ---
@Override
public Role getRole(RoleModel model) {
if (getRole1().getModel().refsSameObject(model)) {
return getRole1();
} else if (getRole2().getModel().refsSameObject(model)) {
return getRole2();
}
throw new RuntimeException("Role is not part of association (role=" + model + ", association=" + this);
}
// ---
public AssociationModel getModel() {
return (AssociationModel) super.getModel();
}
// === Updating ===
/**
* @param model The data to update.
* If the type URI is <code>null</code> it is not updated.
* If role 1 is <code>null</code> it is not updated.
* If role 2 is <code>null</code> it is not updated.
*/
@Override
public void update(AssociationModel model, ClientState clientState, Directives directives) {
logger.info("Updating association " + getId() + " (new " + model + ")");
//
dms.fireEvent(CoreEvent.PRE_UPDATE_ASSOCIATION, this, model, directives);
//
AssociationModel oldModel = getModel().clone();
super.update(model, clientState, directives);
updateRole(model.getRoleModel1(), 1);
updateRole(model.getRoleModel2(), 2);
//
directives.add(Directive.UPDATE_ASSOCIATION, this);
//
dms.fireEvent(CoreEvent.POST_UPDATE_ASSOCIATION, this, oldModel, clientState, directives);
}
// === Traversal ===
@Override
public Topic getTopic(String roleTypeUri) {
Set<Topic> topics = getTopics(roleTypeUri);
switch (topics.size()) {
case 0:
return null;
case 1:
return topics.iterator().next();
default:
throw new RuntimeException("Ambiguity in association: " + topics.size() + " topics have role type \"" +
roleTypeUri + "\" (" + this + ")");
}
}
@Override
public Set<Topic> getTopics(String roleTypeUri) {
Set<Topic> topics = new HashSet();
filterTopic(getRole1(), roleTypeUri, topics);
filterTopic(getRole2(), roleTypeUri, topics);
return topics;
}
// ---
@Override
public RelatedAssociation getRelatedAssociation(String assocTypeUri, String myRoleTypeUri,
String othersRoleTypeUri) {
RelatedAssociationModel assoc = dms.storage.getAssociationRelatedAssociation(getId(),
assocTypeUri, myRoleTypeUri, othersRoleTypeUri, null); // othersAssocTypeUri=null
return assoc != null ? dms.attach(assoc, false, false) : null; // fetchComposite=false,
// fetchRelatingComposite=false
}
// ***************************************
// *** DeepaMehtaObject Implementation ***
// ***************************************
// === Traversal ===
// --- Topic Retrieval ---
@Override
public AttachedRelatedTopic getRelatedTopic(String assocTypeUri, String myRoleTypeUri, String othersRoleTypeUri,
String othersTopicTypeUri, boolean fetchComposite,
boolean fetchRelatingComposite, ClientState clientState) {
RelatedTopicModel topic = dms.storage.getAssociationRelatedTopic(getId(), assocTypeUri, myRoleTypeUri,
othersRoleTypeUri, othersTopicTypeUri);
return topic != null ? dms.attach(topic, fetchComposite, fetchRelatingComposite, clientState) : null;
}
@Override
public ResultSet<RelatedTopic> getRelatedTopics(String assocTypeUri, String myRoleTypeUri, String othersRoleTypeUri,
String othersTopicTypeUri, boolean fetchComposite, boolean fetchRelatingComposite,
int maxResultSize, ClientState clientState) {
ResultSet<RelatedTopicModel> topics = dms.storage.getAssociationRelatedTopics(getId(), assocTypeUri,
myRoleTypeUri, othersRoleTypeUri, othersTopicTypeUri, maxResultSize);
return dms.attach(topics, fetchComposite, fetchRelatingComposite, clientState);
}
@Override
public ResultSet<RelatedTopic> getRelatedTopics(List assocTypeUris, String myRoleTypeUri, String othersRoleTypeUri,
String othersTopicTypeUri, boolean fetchComposite, boolean fetchRelatingComposite,
int maxResultSize, ClientState clientState) {
ResultSet<RelatedTopicModel> topics = dms.storage.getAssociationRelatedTopics(getId(), assocTypeUris,
myRoleTypeUri, othersRoleTypeUri, othersTopicTypeUri, maxResultSize);
return dms.attach(topics, fetchComposite, fetchRelatingComposite, clientState);
}
// --- Association Retrieval ---
@Override
public Association getAssociation(String assocTypeUri, String myRoleTypeUri, String othersRoleTypeUri,
long othersTopicId) {
AssociationModel assoc = dms.storage.getAssociationBetweenTopicAndAssociation(assocTypeUri,
othersTopicId, getId(), othersRoleTypeUri, myRoleTypeUri);
return assoc != null ? dms.attach(assoc, false) : null; // fetchComposite=false
}
@Override
public Set<Association> getAssociations() {
return dms.attach(dms.storage.getAssociationAssociations(getId()), false); // fetchComposite=false
}
// ----------------------------------------------------------------------------------------- Package Private Methods
/**
* Convenience method.
*/
AssociationType getAssociationType() {
return (AssociationType) getType();
}
// ------------------------------------------------------------------------------------------------- Private Methods
// --- Update ---
/**
* @param nr used only for logging
*/
private void updateRole(RoleModel newModel, int nr) {
if (newModel != null) {
// Note: We must lookup the roles individually.
// The role order (getRole1(), getRole2()) is undeterministic and not fix.
Role role = getRole(newModel);
String newRoleTypeUri = newModel.getRoleTypeUri(); // new value
String roleTypeUri = role.getRoleTypeUri(); // current value
if (!roleTypeUri.equals(newRoleTypeUri)) { // has changed?
logger.info("### Changing role type " + nr + " from \"" + roleTypeUri + "\" -> \"" + newRoleTypeUri +
"\"");
role.setRoleTypeUri(newRoleTypeUri);
}
}
}
// --- Helper ---
private Role createAttachedRole(RoleModel model) {
if (model instanceof TopicRoleModel) {
return new AttachedTopicRole((TopicRoleModel) model, this, dms);
} else if (model instanceof AssociationRoleModel) {
return new AttachedAssociationRole((AssociationRoleModel) model, this, dms);
} else {
throw new RuntimeException("Unexpected RoleModel object (" + model + ")");
}
}
private void filterTopic(Role role, String roleTypeUri, Set<Topic> topics) {
if (role instanceof TopicRole && role.getRoleTypeUri().equals(roleTypeUri)) {
topics.add(((TopicRole) role).getTopic());
}
}
}