1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krms.impl.repository.mock;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.LinkedHashMap;
21 import java.util.LinkedHashSet;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25 import java.util.UUID;
26 import org.kuali.rice.core.api.criteria.QueryByCriteria;
27 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
28 import org.kuali.rice.core.api.exception.RiceIllegalStateException;
29 import org.kuali.rice.krms.api.repository.NaturalLanguageTree;
30 import org.kuali.rice.krms.api.repository.RuleManagementService;
31 import org.kuali.rice.krms.api.repository.action.ActionDefinition;
32 import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
33 import org.kuali.rice.krms.api.repository.agenda.AgendaItemDefinition;
34 import org.kuali.rice.krms.api.repository.agenda.AgendaTreeDefinition;
35 import org.kuali.rice.krms.api.repository.context.ContextDefinition;
36 import org.kuali.rice.krms.api.repository.context.ContextSelectionCriteria;
37 import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplate;
38 import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplaterContract;
39 import org.kuali.rice.krms.api.repository.language.NaturalLanguageUsage;
40 import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
41 import org.kuali.rice.krms.api.repository.proposition.PropositionParameter;
42 import org.kuali.rice.krms.api.repository.reference.ReferenceObjectBinding;
43 import org.kuali.rice.krms.api.repository.rule.RuleDefinition;
44 import org.kuali.rice.krms.api.repository.term.TermDefinition;
45 import org.kuali.rice.krms.api.repository.term.TermRepositoryService;
46 import org.kuali.rice.krms.impl.repository.TranslationUtility;
47 import org.kuali.rice.krms.impl.repository.language.SimpleNaturalLanguageTemplater;
48
49 public class RuleManagementServiceMockImpl implements RuleManagementService {
50
51
52
53
54
55 private Map<String, ReferenceObjectBinding> referenceObjectBindingMap = new LinkedHashMap<String, ReferenceObjectBinding>();
56 private Map<String, ContextDefinition> contextMap = new LinkedHashMap<String, ContextDefinition>();
57 private Map<String, AgendaDefinition> agendaMap = new LinkedHashMap<String, AgendaDefinition>();
58 private Map<String, AgendaItemDefinition> agendaItemMap = new LinkedHashMap<String, AgendaItemDefinition>();
59 private Map<String, RuleDefinition> ruleMap = new LinkedHashMap<String, RuleDefinition>();
60 private Map<String, ActionDefinition> actionMap = new LinkedHashMap<String, ActionDefinition>();
61 private Map<String, PropositionDefinition> propositionMap = new LinkedHashMap<String, PropositionDefinition>();
62 private Map<String, NaturalLanguageUsage> naturalLanguageUsageMap = new LinkedHashMap<String, NaturalLanguageUsage>();
63 private Map<String, NaturalLanguageTemplate> naturalLanguageTemplateMap = new LinkedHashMap<String, NaturalLanguageTemplate>();
64
65
66 private NaturalLanguageTemplaterContract templater = new SimpleNaturalLanguageTemplater();
67 private TermRepositoryService termRepositoryService;
68
69 public NaturalLanguageTemplaterContract getTemplater() {
70 return templater;
71 }
72
73 public void setTemplater(NaturalLanguageTemplaterContract templater) {
74 this.templater = templater;
75 }
76
77 public TermRepositoryService getTermRepositoryService() {
78 return termRepositoryService;
79 }
80
81 public void setTermRepositoryService(TermRepositoryService termRepositoryService) {
82 this.termRepositoryService = termRepositoryService;
83 }
84
85
86
87 public void clear() {
88 this.referenceObjectBindingMap.clear();
89 this.contextMap.clear();
90 this.agendaMap.clear();
91 this.agendaItemMap.clear();
92 this.ruleMap.clear();
93 this.actionMap.clear();
94 this.propositionMap.clear();
95 this.naturalLanguageUsageMap.clear();
96 this.naturalLanguageTemplateMap.clear();
97 }
98
99 @Override
100 public ReferenceObjectBinding createReferenceObjectBinding(ReferenceObjectBinding referenceObjectDefinition)
101 throws RiceIllegalArgumentException {
102
103 ReferenceObjectBinding orig = this.getReferenceObjectBinding(referenceObjectDefinition.getId());
104 if (orig != null) {
105 throw new RiceIllegalArgumentException(referenceObjectDefinition.getId());
106 }
107 ReferenceObjectBinding.Builder copy = ReferenceObjectBinding.Builder.create(referenceObjectDefinition);
108 if (copy.getId() == null) {
109 copy.setId(UUID.randomUUID().toString());
110 }
111 referenceObjectDefinition = copy.build();
112 referenceObjectBindingMap.put(referenceObjectDefinition.getId(), referenceObjectDefinition);
113 return referenceObjectDefinition;
114 }
115
116 @Override
117 public ReferenceObjectBinding getReferenceObjectBinding(String id)
118 throws RiceIllegalArgumentException {
119
120 if (!this.referenceObjectBindingMap.containsKey(id)) {
121 throw new RiceIllegalArgumentException(id);
122 }
123 return this.referenceObjectBindingMap.get(id);
124 }
125
126 @Override
127 public List<ReferenceObjectBinding> getReferenceObjectBindings(List<String> ids)
128 throws RiceIllegalArgumentException {
129
130 List<ReferenceObjectBinding> list = new ArrayList<ReferenceObjectBinding>();
131 for (String id : ids) {
132 list.add(this.getReferenceObjectBinding(id));
133 }
134 return list;
135 }
136
137 @Override
138 public List<ReferenceObjectBinding> findReferenceObjectBindingsByReferenceDiscriminatorType(String referenceObjectReferenceDiscriminatorType)
139 throws RiceIllegalArgumentException {
140 List<ReferenceObjectBinding> list = new ArrayList<ReferenceObjectBinding>();
141 for (ReferenceObjectBinding info : this.referenceObjectBindingMap.values()) {
142 if (info.getReferenceDiscriminatorType().equals(referenceObjectReferenceDiscriminatorType)) {
143 list.add(info);
144 }
145 }
146 return list;
147 }
148
149 @Override
150 public List<ReferenceObjectBinding> findReferenceObjectBindingsByReferenceObject(String referenceObjectReferenceDiscriminatorType, String referenceObjectId) throws RiceIllegalArgumentException {
151
152 List<ReferenceObjectBinding> list = new ArrayList<ReferenceObjectBinding>();
153 for (ReferenceObjectBinding info : this.referenceObjectBindingMap.values()) {
154 if (info.getReferenceDiscriminatorType().equals(referenceObjectReferenceDiscriminatorType)) {
155 list.add(info);
156 }
157 }
158 return list;
159 }
160
161 @Override
162 public List<ReferenceObjectBinding> findReferenceObjectBindingsByKrmsDiscriminatorType(String referenceObjectKrmsDiscriminatorType)
163 throws RiceIllegalArgumentException {
164 List<ReferenceObjectBinding> list = new ArrayList<ReferenceObjectBinding>();
165 for (ReferenceObjectBinding info : this.referenceObjectBindingMap.values()) {
166 if (info.getKrmsDiscriminatorType().equals(referenceObjectKrmsDiscriminatorType)) {
167 list.add(info);
168 }
169 }
170 return list;
171 }
172
173 @Override
174 public List<ReferenceObjectBinding> findReferenceObjectBindingsByKrmsObject(String krmsObjectId)
175 throws RiceIllegalArgumentException {
176 List<ReferenceObjectBinding> list = new ArrayList<ReferenceObjectBinding>();
177 for (ReferenceObjectBinding info : this.referenceObjectBindingMap.values()) {
178 if (info.getKrmsObjectId().equals(krmsObjectId)) {
179 list.add(info);
180 }
181 }
182 return list;
183 }
184
185 @Override
186 public void updateReferenceObjectBinding(ReferenceObjectBinding referenceObjectBindingDefinition)
187 throws RiceIllegalArgumentException {
188
189 ReferenceObjectBinding.Builder copy = ReferenceObjectBinding.Builder.create(referenceObjectBindingDefinition);
190 ReferenceObjectBinding old = this.getReferenceObjectBinding(referenceObjectBindingDefinition.getId());
191 if (!old.getVersionNumber().equals(copy.getVersionNumber())) {
192 throw new RiceIllegalStateException("" + old.getVersionNumber());
193 }
194 copy.setVersionNumber(copy.getVersionNumber() + 1);
195 referenceObjectBindingDefinition = copy.build();
196 this.referenceObjectBindingMap.put(referenceObjectBindingDefinition.getId(), referenceObjectBindingDefinition);
197 return;
198 }
199
200 @Override
201 public void deleteReferenceObjectBinding(String id)
202 throws RiceIllegalArgumentException {
203
204 if (this.referenceObjectBindingMap.remove(id) == null) {
205 throw new RiceIllegalArgumentException(id);
206 }
207 return;
208 }
209
210 @Override
211 public List<String> findReferenceObjectBindingIds(QueryByCriteria queryByCriteria)
212 throws RiceIllegalArgumentException {
213 CriteriaMatcherInMemory<ReferenceObjectBinding> instance = new CriteriaMatcherInMemory<ReferenceObjectBinding>();
214 instance.setCriteria(queryByCriteria);
215 Collection<ReferenceObjectBinding> selected = instance.findMatching(this.referenceObjectBindingMap.values());
216 List<String> list = new ArrayList<String>();
217 for (ReferenceObjectBinding sel : selected) {
218 list.add(sel.getId());
219 }
220 return list;
221 }
222
223 @Override
224 public AgendaDefinition createAgenda(AgendaDefinition agendaDefinition)
225 throws RiceIllegalArgumentException {
226 if (agendaDefinition.getId() != null) {
227 AgendaDefinition orig = this.getAgenda(agendaDefinition.getId());
228 if (orig != null) {
229 throw new RiceIllegalArgumentException(agendaDefinition.getId() + "." + agendaDefinition.getName());
230 }
231 }
232 AgendaDefinition existing = this.getAgendaByNameAndContextId (agendaDefinition.getName(), agendaDefinition.getContextId());
233 if (existing == null) {
234 throw new RiceIllegalArgumentException (agendaDefinition.getName() + " " + agendaDefinition.getContextId() + " already exists");
235 }
236 AgendaDefinition.Builder copy = AgendaDefinition.Builder.create(agendaDefinition);
237 if (copy.getId() == null) {
238 copy.setId(UUID.randomUUID().toString());
239 }
240 copy.setVersionNumber(0l);
241 agendaDefinition = copy.build();
242 agendaMap.put(agendaDefinition.getId(), agendaDefinition);
243 return agendaDefinition;
244 }
245
246 @Override
247 public AgendaDefinition findCreateAgenda(AgendaDefinition agendaDefinition) throws RiceIllegalArgumentException {
248 AgendaDefinition agenda = this.getAgendaByNameAndContextId(agendaDefinition.getName(), agendaDefinition.getContextId());
249 if (agenda != null) {
250 return agenda;
251 }
252 return this.createAgenda(agendaDefinition);
253 }
254
255 @Override
256 public AgendaDefinition getAgendaByNameAndContextId(String name, String contextId) {
257 for (AgendaDefinition info : this.agendaMap.values()) {
258 if (info.getContextId().equals(contextId)) {
259 if (info.getName().equals(name)) {
260 return info;
261 }
262 }
263 }
264 return null;
265 }
266
267
268
269 @Override
270 public AgendaDefinition getAgenda(String id)
271 throws RiceIllegalArgumentException {
272
273 if (!this.agendaMap.containsKey(id)) {
274 throw new RiceIllegalArgumentException(id);
275 }
276 return this.agendaMap.get(id);
277 }
278
279 @Override
280 public List<AgendaDefinition> getAgendasByType(String typeId)
281 throws RiceIllegalArgumentException {
282
283 List<AgendaDefinition> list = new ArrayList<AgendaDefinition>();
284 for (AgendaDefinition info : agendaMap.values()) {
285 if (typeId.equals(info.getTypeId())) {
286 list.add(info);
287 }
288 }
289 return list;
290 }
291
292 @Override
293 public List<AgendaDefinition> getAgendasByContext(String contextId)
294 throws RiceIllegalArgumentException {
295 List<AgendaDefinition> list = new ArrayList<AgendaDefinition>();
296 for (AgendaDefinition info : this.agendaMap.values()) {
297 if (info.getContextId().equals(contextId)) {
298 list.add(info);
299 }
300 }
301 return list;
302 }
303
304 @Override
305 public List<AgendaDefinition> getAgendasByTypeAndContext(String typeId, String contextId)
306 throws RiceIllegalArgumentException {
307 List<AgendaDefinition> list = new ArrayList<AgendaDefinition>();
308 for (AgendaDefinition info : this.agendaMap.values()) {
309 if (info.getContextId().equals(contextId)) {
310 if (info.getTypeId().equals(typeId)) {
311 list.add(info);
312 }
313 }
314 }
315 return list;
316 }
317
318 @Override
319 public void updateAgenda(AgendaDefinition agendaDefinition)
320 throws RiceIllegalArgumentException {
321
322 AgendaDefinition.Builder copy = AgendaDefinition.Builder.create(agendaDefinition);
323 AgendaDefinition old = this.getAgenda(agendaDefinition.getId());
324 if (!old.getVersionNumber().equals(copy.getVersionNumber())) {
325 throw new RiceIllegalStateException("" + old.getVersionNumber());
326 }
327 copy.setVersionNumber(copy.getVersionNumber() + 1);
328 agendaDefinition = copy.build();
329 this.agendaMap.put(agendaDefinition.getId(), agendaDefinition);
330 return;
331 }
332
333 @Override
334 public void deleteAgenda(String id)
335 throws RiceIllegalArgumentException {
336
337 if (this.agendaMap.remove(id) == null) {
338 throw new RiceIllegalArgumentException(id);
339 }
340 return;
341 }
342
343 @Override
344 public AgendaItemDefinition createAgendaItem(AgendaItemDefinition agendaItemDefinition)
345 throws RiceIllegalArgumentException {
346
347 AgendaItemDefinition.Builder copy = AgendaItemDefinition.Builder.create(agendaItemDefinition);
348 if (copy.getId() == null) {
349 copy.setId(UUID.randomUUID().toString());
350 }
351 agendaItemDefinition = copy.build();
352 agendaItemMap.put(agendaItemDefinition.getId(), agendaItemDefinition);
353 return agendaItemDefinition;
354 }
355
356 @Override
357 public AgendaItemDefinition getAgendaItem(String id)
358 throws RiceIllegalArgumentException {
359
360 if (!this.agendaItemMap.containsKey(id)) {
361 throw new RiceIllegalArgumentException(id);
362 }
363 return this.agendaItemMap.get(id);
364 }
365
366 @Override
367 public List<AgendaItemDefinition> getAgendaItemsByType(String typeId)
368 throws RiceIllegalArgumentException {
369
370 List<AgendaDefinition> agendas = this.getAgendasByType(typeId);
371 List<AgendaItemDefinition> list = new ArrayList<AgendaItemDefinition>();
372 for (AgendaDefinition agenda : agendas) {
373 for (AgendaItemDefinition info : agendaItemMap.values()) {
374 if (agenda.getId().equals(info.getAgendaId())) {
375 list.add(info);
376 }
377 }
378 }
379 return list;
380 }
381
382 @Override
383 public List<AgendaItemDefinition> getAgendaItemsByContext(String contextId)
384 throws RiceIllegalArgumentException {
385 List<AgendaDefinition> agendas = this.getAgendasByContext(contextId);
386 List<AgendaItemDefinition> list = new ArrayList<AgendaItemDefinition>();
387 for (AgendaDefinition agenda : agendas) {
388 for (AgendaItemDefinition info : agendaItemMap.values()) {
389 if (agenda.getId().equals(info.getAgendaId())) {
390 list.add(info);
391 }
392 }
393 }
394 return list;
395 }
396
397 @Override
398 public List<AgendaItemDefinition> getAgendaItemsByTypeAndContext(String typeId, String contextId)
399 throws RiceIllegalArgumentException {
400 List<AgendaDefinition> agendas = this.getAgendasByContext(contextId);
401 List<AgendaItemDefinition> list = new ArrayList<AgendaItemDefinition>();
402 for (AgendaDefinition agenda : agendas) {
403 if (agenda.getTypeId().equals(typeId)) {
404 for (AgendaItemDefinition info : agendaItemMap.values()) {
405 if (agenda.getId().equals(info.getAgendaId())) {
406 list.add(info);
407 }
408 }
409 }
410 }
411 return list;
412 }
413
414 @Override
415 public void updateAgendaItem(AgendaItemDefinition agendaItemDefinition)
416 throws RiceIllegalArgumentException {
417
418 AgendaItemDefinition.Builder copy = AgendaItemDefinition.Builder.create(agendaItemDefinition);
419 AgendaItemDefinition old = this.getAgendaItem(agendaItemDefinition.getId());
420 if (!old.getVersionNumber().equals(copy.getVersionNumber())) {
421 throw new RiceIllegalStateException("" + old.getVersionNumber());
422 }
423 copy.setVersionNumber(copy.getVersionNumber() + 1);
424 agendaItemDefinition = copy.build();
425 this.agendaItemMap.put(agendaItemDefinition.getId(), agendaItemDefinition);
426 return;
427 }
428
429 @Override
430 public void deleteAgendaItem(String id)
431 throws RiceIllegalArgumentException {
432
433 if (this.agendaItemMap.remove(id) == null) {
434 throw new RiceIllegalArgumentException(id);
435 }
436 return;
437 }
438
439 @Override
440 public RuleDefinition getRuleByNameAndNamespace(String name, String namespace) {
441 for (RuleDefinition rule : this.ruleMap.values()) {
442 if (rule.getName().equals(name)) {
443 if (rule.getNamespace().equals(namespace)) {
444 return rule;
445 }
446 }
447 }
448 return null;
449 }
450
451
452
453 @Override
454 public RuleDefinition createRule(RuleDefinition ruleDefinition)
455 throws RiceIllegalArgumentException {
456
457 if (ruleDefinition.getId() != null) {
458 RuleDefinition orig = this.getRule(ruleDefinition.getId());
459 if (orig != null) {
460 throw new RiceIllegalArgumentException(ruleDefinition.getId());
461 }
462 }
463 RuleDefinition.Builder copy = RuleDefinition.Builder.create(ruleDefinition);
464 if (copy.getId() == null) {
465 copy.setId(UUID.randomUUID().toString());
466 }
467 ruleDefinition = copy.build();
468 ruleMap.put(ruleDefinition.getId(), ruleDefinition);
469 return ruleDefinition;
470 }
471
472 @Override
473 public RuleDefinition getRule(String ruleId) {
474
475 if (!this.ruleMap.containsKey(ruleId)) {
476 throw new RiceIllegalArgumentException(ruleId);
477 }
478 return this.ruleMap.get(ruleId);
479 }
480
481 @Override
482 public List<RuleDefinition> getRules(List<String> ruleIds) {
483 List<RuleDefinition> list = new ArrayList<RuleDefinition>();
484 for (String id : ruleIds) {
485 list.add(this.getRule(id));
486 }
487 return list;
488 }
489
490 @Override
491 public void updateRule(RuleDefinition ruleDefinition)
492 throws RiceIllegalArgumentException {
493
494 RuleDefinition.Builder copy = RuleDefinition.Builder.create(ruleDefinition);
495 RuleDefinition old = this.getRule(ruleDefinition.getId());
496 if (!old.getVersionNumber().equals(copy.getVersionNumber())) {
497 throw new RiceIllegalStateException("" + old.getVersionNumber());
498 }
499 copy.setVersionNumber(copy.getVersionNumber() + 1);
500 ruleDefinition = copy.build();
501 this.ruleMap.put(ruleDefinition.getId(), ruleDefinition);
502 return;
503 }
504
505 @Override
506 public void deleteRule(String id)
507 throws RiceIllegalArgumentException {
508
509 if (this.ruleMap.remove(id) == null) {
510 throw new RiceIllegalArgumentException(id);
511 }
512 return;
513 }
514
515 @Override
516 public ActionDefinition createAction(ActionDefinition actionDefinition)
517 throws RiceIllegalArgumentException {
518
519 ActionDefinition orig = this.getAction(actionDefinition.getId());
520 if (orig != null) {
521 throw new RiceIllegalArgumentException(actionDefinition.getId());
522 }
523 ActionDefinition.Builder copy = ActionDefinition.Builder.create(actionDefinition);
524 if (copy.getId() == null) {
525 copy.setId(UUID.randomUUID().toString());
526 }
527 actionDefinition = copy.build();
528 actionMap.put(actionDefinition.getId(), actionDefinition);
529 return actionDefinition;
530 }
531
532 @Override
533 public ActionDefinition getAction(String actionId) {
534
535 if (!this.actionMap.containsKey(actionId)) {
536 throw new RiceIllegalArgumentException(actionId);
537 }
538 return this.actionMap.get(actionId);
539 }
540
541 @Override
542 public List<ActionDefinition> getActions(List<String> actionIds) {
543 List<ActionDefinition> list = new ArrayList<ActionDefinition>();
544 for (String id : actionIds) {
545 list.add(this.getAction(id));
546 }
547 return list;
548 }
549
550 @Override
551 public void updateAction(ActionDefinition actionDefinition)
552 throws RiceIllegalArgumentException {
553
554 ActionDefinition.Builder copy = ActionDefinition.Builder.create(actionDefinition);
555 ActionDefinition old = this.getAction(actionDefinition.getId());
556 if (!old.getVersionNumber().equals(copy.getVersionNumber())) {
557 throw new RiceIllegalStateException("" + old.getVersionNumber());
558 }
559 copy.setVersionNumber(copy.getVersionNumber() + 1);
560 actionDefinition = copy.build();
561 this.actionMap.put(actionDefinition.getId(), actionDefinition);
562 return;
563 }
564
565 @Override
566 public void deleteAction(String id)
567 throws RiceIllegalArgumentException {
568
569 if (this.actionMap.remove(id) == null) {
570 throw new RiceIllegalArgumentException(id);
571 }
572 return;
573 }
574
575 @Override
576 public PropositionDefinition createProposition(PropositionDefinition propositionDefinition)
577 throws RiceIllegalArgumentException {
578
579 if (propositionDefinition.getId() != null) {
580 PropositionDefinition orig = this.getProposition(propositionDefinition.getId());
581 if (orig != null) {
582 throw new RiceIllegalArgumentException(propositionDefinition.getId());
583 }
584 }
585 PropositionDefinition.Builder copy = PropositionDefinition.Builder.create(propositionDefinition);
586 if (copy.getId() == null) {
587 copy.setId(UUID.randomUUID().toString());
588 }
589 for (PropositionParameter.Builder paramBldr : copy.getParameters()) {
590 if (paramBldr.getId() == null) {
591 paramBldr.setId(UUID.randomUUID().toString());
592 }
593 if (paramBldr.getPropId() == null) {
594 paramBldr.setPropId(copy.getId());
595 }
596 if (paramBldr.getTermValue() != null) {
597 TermDefinition termValue = paramBldr.getTermValue();
598
599 if (termValue.getId() == null) {
600 termValue = this.termRepositoryService.createTerm(termValue);
601 paramBldr.setTermValue(termValue);
602 }
603 if (paramBldr.getValue() == null) {
604 paramBldr.setValue(termValue.getId());
605 }
606 }
607 }
608 propositionDefinition = copy.build();
609 propositionMap.put(propositionDefinition.getId(), propositionDefinition);
610 return propositionDefinition;
611 }
612
613 @Override
614 public PropositionDefinition getProposition(String id)
615 throws RiceIllegalArgumentException {
616
617 if (!this.propositionMap.containsKey(id)) {
618 throw new RiceIllegalArgumentException(id);
619 }
620 return this.propositionMap.get(id);
621 }
622
623 @Override
624 public Set<PropositionDefinition> getPropositionsByType(String typeId)
625 throws RiceIllegalArgumentException {
626
627 Set<PropositionDefinition> set = new LinkedHashSet<PropositionDefinition>();
628 for (PropositionDefinition info : propositionMap.values()) {
629 if (typeId.equals(info.getTypeId())) {
630 set.add(info);
631 }
632 }
633 return set;
634 }
635
636 @Override
637 public Set<PropositionDefinition> getPropositionsByRule(String ruleId)
638 throws RiceIllegalArgumentException {
639 Set<PropositionDefinition> set = new LinkedHashSet<PropositionDefinition>();
640 for (PropositionDefinition info : this.propositionMap.values()) {
641 if (info.getRuleId().equals(ruleId)) {
642 set.add(info);
643 }
644 }
645 return set;
646 }
647
648 @Override
649 public void updateProposition(PropositionDefinition propositionDefinition)
650 throws RiceIllegalArgumentException {
651 if (this.propositionMap.containsKey(propositionDefinition.getId())) {
652 throw new RiceIllegalArgumentException (propositionDefinition.getId() + "not found");
653 }
654
655 PropositionDefinition.Builder copy = PropositionDefinition.Builder.create(propositionDefinition);
656 PropositionDefinition old = this.getProposition(propositionDefinition.getId());
657 if (!old.getVersionNumber().equals(copy.getVersionNumber())) {
658 throw new RiceIllegalStateException("" + old.getVersionNumber());
659 }
660 copy.setVersionNumber(copy.getVersionNumber() + 1);
661 for (PropositionParameter.Builder paramBldr : copy.getParameters()) {
662 if (paramBldr.getId() == null) {
663 paramBldr.setId(UUID.randomUUID().toString());
664 }
665 if (paramBldr.getPropId() == null) {
666 paramBldr.setPropId(copy.getId());
667 }
668 if (paramBldr.getTermValue() != null) {
669 TermDefinition termValue = paramBldr.getTermValue();
670
671 if (termValue.getId() == null) {
672 termValue = this.termRepositoryService.createTerm(termValue);
673 paramBldr.setTermValue(termValue);
674 }
675 if (paramBldr.getValue() == null) {
676 paramBldr.setValue(termValue.getId());
677 }
678 }
679 }
680 propositionDefinition = copy.build();
681 this.propositionMap.put(propositionDefinition.getId(), propositionDefinition);
682 return;
683 }
684
685 @Override
686 public void deleteProposition(String id)
687 throws RiceIllegalArgumentException {
688
689 if (this.propositionMap.remove(id) == null) {
690 throw new RiceIllegalArgumentException(id);
691 }
692 return;
693 }
694
695 @Override
696 public NaturalLanguageUsage createNaturalLanguageUsage(NaturalLanguageUsage naturalLanguageUsage)
697 throws RiceIllegalArgumentException {
698
699 try {
700 NaturalLanguageUsage orig = this.getNaturalLanguageUsage(naturalLanguageUsage.getId());
701 if (orig != null) {
702 throw new RiceIllegalArgumentException(naturalLanguageUsage.getId());
703 }
704 } catch (RiceIllegalArgumentException ex) {
705
706 }
707 NaturalLanguageUsage.Builder copy = NaturalLanguageUsage.Builder.create(naturalLanguageUsage);
708 if (copy.getId() == null) {
709 copy.setId(UUID.randomUUID().toString());
710 }
711 naturalLanguageUsage = copy.build();
712 naturalLanguageUsageMap.put(naturalLanguageUsage.getId(), naturalLanguageUsage);
713 return naturalLanguageUsage;
714 }
715
716 @Override
717 public NaturalLanguageUsage getNaturalLanguageUsage(String id)
718 throws RiceIllegalArgumentException {
719
720 if (!this.naturalLanguageUsageMap.containsKey(id)) {
721 throw new RiceIllegalArgumentException(id);
722 }
723 return this.naturalLanguageUsageMap.get(id);
724 }
725
726 @Override
727 public void updateNaturalLanguageUsage(NaturalLanguageUsage naturalLanguageUsage)
728 throws RiceIllegalArgumentException {
729
730 NaturalLanguageUsage.Builder copy = NaturalLanguageUsage.Builder.create(naturalLanguageUsage);
731 NaturalLanguageUsage old = this.getNaturalLanguageUsage(naturalLanguageUsage.getId());
732 if (!old.getVersionNumber().equals(copy.getVersionNumber())) {
733 throw new RiceIllegalStateException("" + old.getVersionNumber());
734 }
735 copy.setVersionNumber(copy.getVersionNumber() + 1);
736 naturalLanguageUsage = copy.build();
737 this.naturalLanguageUsageMap.put(naturalLanguageUsage.getId(), naturalLanguageUsage);
738 return;
739 }
740
741 @Override
742 public void deleteNaturalLanguageUsage(String naturalLanguageUsageId)
743 throws RiceIllegalArgumentException {
744
745 if (this.naturalLanguageUsageMap.remove(naturalLanguageUsageId) == null) {
746 throw new RiceIllegalArgumentException(naturalLanguageUsageId);
747 }
748 return;
749 }
750
751
752
753
754 @Override
755 public String translateNaturalLanguageForObject(String naturalLanguageUsageId,
756 String typeId,
757 String krmsObjectId,
758 String languageCode)
759 throws RiceIllegalArgumentException {
760 TranslationUtility util = new TranslationUtility(this, this.termRepositoryService, this.templater);
761 return util.translateNaturalLanguageForObject(naturalLanguageUsageId, typeId, krmsObjectId, languageCode);
762 }
763
764 @Override
765 public String translateNaturalLanguageForProposition(String naturalLanguageUsageId,
766 PropositionDefinition proposition, String languageCode)
767 throws RiceIllegalArgumentException {
768 TranslationUtility util = new TranslationUtility(this, this.termRepositoryService, this.templater);
769 return util.translateNaturalLanguageForProposition(naturalLanguageUsageId, proposition, languageCode);
770 }
771
772 @Override
773 public NaturalLanguageTree translateNaturalLanguageTreeForProposition(String naturalLanguageUsageId,
774 PropositionDefinition propositionDefinintion,
775 String languageCode) throws RiceIllegalArgumentException {
776 TranslationUtility util = new TranslationUtility(this, this.termRepositoryService, this.templater);
777 return util.translateNaturalLanguageTreeForProposition(naturalLanguageUsageId, propositionDefinintion, languageCode);
778 }
779
780 @Override
781 public List<NaturalLanguageUsage> getNaturalLanguageUsagesByNamespace(String namespace) throws RiceIllegalArgumentException {
782 List<NaturalLanguageUsage> list = new ArrayList<NaturalLanguageUsage>();
783 for (NaturalLanguageUsage info : naturalLanguageUsageMap.values()) {
784 if (namespace.equals(info.getNamespace())) {
785 list.add(info);
786 }
787 }
788 return list;
789 }
790
791 @Override
792 public NaturalLanguageUsage getNaturalLanguageUsageByNameAndNamespace(String name, String namespace) throws RiceIllegalArgumentException {
793 for (NaturalLanguageUsage info : naturalLanguageUsageMap.values()) {
794 if (namespace.equals(info.getNamespace())) {
795 if (name.equals(info.getName())) {
796 return info;
797 }
798 }
799 }
800 return null;
801 }
802
803 @Override
804 public ContextDefinition selectContext(ContextSelectionCriteria contextSelectionCriteria) throws RiceIllegalArgumentException {
805 throw new UnsupportedOperationException("Not supported yet.");
806 }
807
808 @Override
809 public AgendaTreeDefinition getAgendaTree(String agendaId) throws RiceIllegalArgumentException {
810 throw new UnsupportedOperationException("Not supported yet.");
811 }
812
813 @Override
814 public List<AgendaTreeDefinition> getAgendaTrees(List<String> agendaIds) throws RiceIllegalArgumentException {
815 throw new UnsupportedOperationException("Not supported yet.");
816 }
817
818 @Override
819 public ContextDefinition createContext(ContextDefinition contextDefinition)
820 throws RiceIllegalArgumentException {
821
822 ContextDefinition orig = this.getContextByNameAndNamespace(contextDefinition.getName(), contextDefinition.getNamespace());
823 if (orig != null) {
824 throw new RiceIllegalArgumentException(contextDefinition.getNamespace() + "." + contextDefinition.getName());
825 }
826 ContextDefinition.Builder copy = ContextDefinition.Builder.create(contextDefinition);
827 if (copy.getId() == null) {
828 copy.setId(UUID.randomUUID().toString());
829 }
830 contextDefinition = copy.build();
831 contextMap.put(contextDefinition.getId(), contextDefinition);
832 return contextDefinition;
833 }
834
835 @Override
836 public ContextDefinition findCreateContext(ContextDefinition contextDefinition) throws RiceIllegalArgumentException {
837 ContextDefinition orig = this.getContextByNameAndNamespace(contextDefinition.getName(), contextDefinition.getNamespace());
838 if (orig != null) {
839 return orig;
840 }
841 return this.createContext(contextDefinition);
842 }
843
844 @Override
845 public void updateContext(ContextDefinition contextDefinition)
846 throws RiceIllegalArgumentException {
847
848 ContextDefinition.Builder copy = ContextDefinition.Builder.create(contextDefinition);
849 ContextDefinition old = this.getContext(contextDefinition.getId());
850 if (!old.getVersionNumber().equals(copy.getVersionNumber())) {
851 throw new RiceIllegalStateException("" + old.getVersionNumber());
852 }
853 copy.setVersionNumber(copy.getVersionNumber() + 1);
854 contextDefinition = copy.build();
855 this.contextMap.put(contextDefinition.getId(), contextDefinition);
856 return;
857 }
858
859 @Override
860 public void deleteContext(String id)
861 throws RiceIllegalArgumentException {
862
863 if (this.contextMap.remove(id) == null) {
864 throw new RiceIllegalArgumentException(id);
865 }
866 return;
867 }
868
869 @Override
870 public ContextDefinition getContext(String id)
871 throws RiceIllegalArgumentException {
872
873 if (!this.contextMap.containsKey(id)) {
874 throw new RiceIllegalArgumentException(id);
875 }
876 return this.contextMap.get(id);
877 }
878
879 @Override
880 public ContextDefinition getContextByNameAndNamespace(String name, String namespace)
881 throws RiceIllegalArgumentException {
882 if (name == null || name.trim().isEmpty()) {
883 throw new RiceIllegalArgumentException("name is null or empty");
884 }
885 if (namespace == null || namespace.trim().isEmpty()) {
886 throw new RiceIllegalArgumentException("name is null or empty");
887 }
888
889 for (ContextDefinition info : contextMap.values()) {
890 if (name.equals(info.getName())) {
891 if (namespace.equals(info.getNamespace())) {
892 return ContextDefinition.Builder.create(info).build();
893 }
894 }
895 }
896 return null;
897 }
898
899 @Override
900 public NaturalLanguageTemplate createNaturalLanguageTemplate(NaturalLanguageTemplate naturalLanguageTemplate)
901 throws RiceIllegalArgumentException {
902
903 try {
904 NaturalLanguageTemplate orig = this.getNaturalLanguageTemplate(naturalLanguageTemplate.getId());
905 if (orig != null) {
906 throw new RiceIllegalArgumentException(naturalLanguageTemplate.getId());
907 }
908 } catch (RiceIllegalArgumentException ex) {
909
910 }
911 NaturalLanguageTemplate.Builder copy = NaturalLanguageTemplate.Builder.create(naturalLanguageTemplate);
912 if (copy.getId() == null) {
913 copy.setId(UUID.randomUUID().toString());
914 }
915 naturalLanguageTemplate = copy.build();
916 naturalLanguageTemplateMap.put(naturalLanguageTemplate.getId(), naturalLanguageTemplate);
917 return naturalLanguageTemplate;
918 }
919
920 @Override
921 public NaturalLanguageTemplate getNaturalLanguageTemplate(String naturalLanguageTemplateId)
922 throws RiceIllegalArgumentException {
923
924 if (!this.naturalLanguageTemplateMap.containsKey(naturalLanguageTemplateId)) {
925 throw new RiceIllegalArgumentException(naturalLanguageTemplateId);
926 }
927 return this.naturalLanguageTemplateMap.get(naturalLanguageTemplateId);
928 }
929
930 @Override
931 public void updateNaturalLanguageTemplate(NaturalLanguageTemplate naturalLanguageTemplate)
932 throws RiceIllegalArgumentException {
933
934 NaturalLanguageTemplate.Builder copy = NaturalLanguageTemplate.Builder.create(naturalLanguageTemplate);
935 NaturalLanguageTemplate old = this.getNaturalLanguageTemplate(naturalLanguageTemplate.getId());
936 if (!old.getVersionNumber().equals(copy.getVersionNumber())) {
937 throw new RiceIllegalStateException("" + old.getVersionNumber());
938 }
939 copy.setVersionNumber(copy.getVersionNumber() + 1);
940 naturalLanguageTemplate = copy.build();
941 this.naturalLanguageTemplateMap.put(naturalLanguageTemplate.getId(), naturalLanguageTemplate);
942 return;
943 }
944
945 @Override
946 public void deleteNaturalLanguageTemplate(String naturalLanguageTemplateId)
947 throws RiceIllegalArgumentException {
948
949 if (this.naturalLanguageTemplateMap.remove(naturalLanguageTemplateId) == null) {
950 throw new RiceIllegalArgumentException(naturalLanguageTemplateId);
951 }
952 return;
953 }
954
955 @Override
956 public List<NaturalLanguageTemplate> findNaturalLanguageTemplatesByLanguageCode(String languageCode)
957 throws RiceIllegalArgumentException {
958 List<NaturalLanguageTemplate> list = new ArrayList<NaturalLanguageTemplate>();
959 for (NaturalLanguageTemplate nlt : this.naturalLanguageTemplateMap.values()) {
960 if (nlt.getLanguageCode().equals(languageCode)) {
961 list.add(nlt);
962 }
963 }
964 return list;
965 }
966
967 @Override
968 public NaturalLanguageTemplate findNaturalLanguageTemplateByLanguageCodeTypeIdAndNluId(String languageCode, String typeId, String naturalLanguageUsageId)
969 throws RiceIllegalArgumentException {
970 for (NaturalLanguageTemplate nlt : this.naturalLanguageTemplateMap.values()) {
971 if (nlt.getLanguageCode().equals(languageCode)) {
972 if (nlt.getTypeId().equals(typeId)) {
973 if (nlt.getNaturalLanguageUsageId().equals(naturalLanguageUsageId)) {
974 return nlt;
975 }
976 }
977 }
978 }
979 return null;
980 }
981
982 @Override
983 public List<NaturalLanguageTemplate> findNaturalLanguageTemplatesByNaturalLanguageUsage(String naturalLanguageUsageId)
984 throws RiceIllegalArgumentException {
985 List<NaturalLanguageTemplate> list = new ArrayList<NaturalLanguageTemplate>();
986 for (NaturalLanguageTemplate nlt : this.naturalLanguageTemplateMap.values()) {
987 if (nlt.getNaturalLanguageUsageId().equals(naturalLanguageUsageId)) {
988 list.add(nlt);
989 }
990 }
991 return list;
992 }
993
994 @Override
995 public List<NaturalLanguageTemplate> findNaturalLanguageTemplatesByType(String typeId)
996 throws RiceIllegalArgumentException {
997 List<NaturalLanguageTemplate> list = new ArrayList<NaturalLanguageTemplate>();
998 for (NaturalLanguageTemplate nlt : this.naturalLanguageTemplateMap.values()) {
999 if (nlt.getTypeId().equals(typeId)) {
1000 list.add(nlt);
1001 }
1002 }
1003 return list;
1004 }
1005
1006 @Override
1007 public List<NaturalLanguageTemplate> findNaturalLanguageTemplatesByTemplate(String template)
1008 throws RiceIllegalArgumentException {
1009 List<NaturalLanguageTemplate> list = new ArrayList<NaturalLanguageTemplate>();
1010 for (NaturalLanguageTemplate nlt : this.naturalLanguageTemplateMap.values()) {
1011 if (nlt.getTemplate().equals(template)) {
1012 list.add(nlt);
1013 }
1014 }
1015 return list;
1016 }
1017
1018 @Override
1019 public List<String> findContextIds(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
1020 CriteriaMatcherInMemory<ContextDefinition> instance = new CriteriaMatcherInMemory<ContextDefinition>();
1021 instance.setCriteria(queryByCriteria);
1022 Collection<ContextDefinition> selected = instance.findMatching(this.contextMap.values());
1023 List<String> list = new ArrayList<String>();
1024 for (ContextDefinition sel : selected) {
1025 list.add(sel.getId());
1026 }
1027 return list;
1028 }
1029
1030 @Override
1031 public List<String> findAgendaIds(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
1032 CriteriaMatcherInMemory<AgendaDefinition> instance = new CriteriaMatcherInMemory<AgendaDefinition>();
1033 instance.setCriteria(queryByCriteria);
1034 Collection<AgendaDefinition> selected = instance.findMatching(this.agendaMap.values());
1035 List<String> list = new ArrayList<String>();
1036 for (AgendaDefinition sel : selected) {
1037 list.add(sel.getId());
1038 }
1039 return list;
1040 }
1041
1042 @Override
1043 public List<String> findRuleIds(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
1044 CriteriaMatcherInMemory<RuleDefinition> instance = new CriteriaMatcherInMemory<RuleDefinition>();
1045 instance.setCriteria(queryByCriteria);
1046 Collection<RuleDefinition> selected = instance.findMatching(this.ruleMap.values());
1047 List<String> list = new ArrayList<String>();
1048 for (RuleDefinition sel : selected) {
1049 list.add(sel.getId());
1050 }
1051 return list;
1052 }
1053
1054 @Override
1055 public List<String> findActionIds(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
1056 CriteriaMatcherInMemory<ActionDefinition> instance = new CriteriaMatcherInMemory<ActionDefinition>();
1057 instance.setCriteria(queryByCriteria);
1058 Collection<ActionDefinition> selected = instance.findMatching(this.actionMap.values());
1059 List<String> list = new ArrayList<String>();
1060 for (ActionDefinition sel : selected) {
1061 list.add(sel.getId());
1062 }
1063 return list;
1064 }
1065
1066 @Override
1067 public List<String> findPropositionIds(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
1068 CriteriaMatcherInMemory<PropositionDefinition> instance = new CriteriaMatcherInMemory<PropositionDefinition>();
1069 instance.setCriteria(queryByCriteria);
1070 Collection<PropositionDefinition> selected = instance.findMatching(this.propositionMap.values());
1071 List<String> list = new ArrayList<String>();
1072 for (PropositionDefinition sel : selected) {
1073 list.add(sel.getId());
1074 }
1075 return list;
1076 }
1077 }