1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krms.impl.provider.repository;
17
18 import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
19 import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
20 import org.kuali.rice.krms.framework.engine.Proposition;
21 import org.kuali.rice.krms.framework.engine.PropositionResult;
22 import org.kuali.rice.krms.framework.type.PropositionTypeService;
23 import org.kuali.rice.krms.impl.type.KrmsTypeResolver;
24
25 import java.util.Collections;
26 import java.util.List;
27
28
29
30
31
32
33
34 final class LazyProposition implements Proposition {
35
36 private final PropositionDefinition propositionDefinition;
37 private final KrmsTypeResolver resolver;
38
39 private final Object mutex = new Object();
40
41
42 private volatile Proposition proposition;
43
44 LazyProposition(PropositionDefinition propositionDefinition, KrmsTypeResolver resolver) {
45 this.propositionDefinition = propositionDefinition;
46 this.resolver = resolver;
47 this.proposition = null;
48 }
49
50 @Override
51 public PropositionResult evaluate(ExecutionEnvironment environment) {
52 return getProposition().evaluate(environment);
53 }
54
55
56
57
58 private Proposition getProposition() {
59 Proposition localProposition = proposition;
60 if (localProposition == null) {
61 synchronized (mutex) {
62 localProposition = proposition;
63 if (localProposition == null) {
64 proposition = localProposition = constructProposition();
65 }
66 }
67 }
68 return localProposition;
69 }
70
71 private Proposition constructProposition() {
72 Proposition proposition = null;
73 if (propositionDefinition != null) {
74 PropositionTypeService propositionTypeService = resolver.getPropositionTypeService(propositionDefinition);
75 proposition = propositionTypeService.loadProposition(propositionDefinition);
76 }
77 if (proposition == null) {
78 proposition = new Proposition() {
79 @Override
80 public PropositionResult evaluate(ExecutionEnvironment environment) {
81 return new PropositionResult(true);
82 }
83
84 @Override
85 public List<Proposition> getChildren() {
86 return Collections.emptyList();
87 }
88
89 @Override
90 public boolean isCompound() {
91 return false;
92 }
93 };
94 }
95 return proposition;
96 }
97
98 @Override
99 public List<Proposition> getChildren() {
100 return getProposition().getChildren();
101 }
102
103 @Override
104 public boolean isCompound() {
105 return getProposition().isCompound();
106 }
107 }