Skip to content

Commit ab0a362

Browse files
committed
improve code re-use and coverage
1 parent 5848d26 commit ab0a362

2 files changed

Lines changed: 89 additions & 75 deletions

File tree

src/main/java/org/jreliability/sl/SL.java

Lines changed: 74 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,24 @@ protected void initialize(Term term) {
140140
public double getProbabiliy(Transformer<T, Double> transformer) {
141141
operandsStack.clear();
142142
termCache.clear();
143-
evaluate(transformer);
143+
evaluateProbability(transformer);
144144
// Evaluate leaves the final bit stream of the top event on the stack
145145
BitSet bitstream = operandsStack.pop();
146146
double probability = (double) bitstream.cardinality() / bitstream.size();
147147
return probability;
148148
}
149149

150+
@Override
151+
public boolean isProvidingService(Map<T, Boolean> variables) {
152+
operandsStack.clear();
153+
termCache.clear();
154+
// Evaluate leaves the final bit stream of the top event on the stack
155+
evaluateStructureFunction(variables);
156+
BitSet bitstream = operandsStack.pop();
157+
System.err.println(bitstream);
158+
return bitstream.cardinality() > 0;
159+
}
160+
150161
/**
151162
* The evaluation performs the actual stochastic logic calculations by a
152163
* sequential processing of the different terms according to their order and
@@ -155,7 +166,7 @@ public double getProbabiliy(Transformer<T, Double> transformer) {
155166
*
156167
* @param transformer the probability of the basic events
157168
*/
158-
protected void evaluate(Transformer<T, Double> transformer) {
169+
protected void evaluateProbability(Transformer<T, Double> transformer) {
159170
for (Term term : termsForStackProcessing) {
160171
if (term instanceof LiteralTerm) {
161172
BitSet bitstream = termCache.get(term);
@@ -166,26 +177,73 @@ protected void evaluate(Transformer<T, Double> transformer) {
166177
termCache.put(term, bitstream);
167178
}
168179
operandsStack.push(bitstream);
169-
} else if (term instanceof FALSETerm) {
170-
BitSet bitstream = new BitSet(bitStreamLength);
171-
bitstream.clear();
172-
operandsStack.push(bitstream);
173-
} else if (term instanceof TRUETerm) {
174-
BitSet bitstream = new BitSet(bitStreamLength);
175-
bitstream.set(0, bitstream.size(), true);
180+
} else {
181+
evaluateNonLiteralTerms(term, bitStreamLength);
182+
}
183+
}
184+
}
185+
186+
/**
187+
* Evaluates the {@link Term} as SL would, but a bit stream length of 1
188+
*
189+
* @param variables
190+
*/
191+
protected void evaluateStructureFunction(Map<T, Boolean> variables) {
192+
for (Term term : termsForStackProcessing) {
193+
if (term instanceof LiteralTerm) {
194+
BitSet bitstream = termCache.get(term);
195+
if (bitstream == null) {
196+
@SuppressWarnings("unchecked")
197+
LiteralTerm<T> component = (LiteralTerm<T>) term;
198+
T variable = component.get();
199+
bitstream = new BitSet(bitStreamLengthStructureFunction);
200+
// Default: Set all entries to 1 (captures 1 variables and all non-specified
201+
// ones)
202+
// Careful, this only works in coherent systems!
203+
bitstream.set(0, bitstream.size(), true);
204+
// Pull 0 variables to all 0 bit streams
205+
if (variables.containsKey(variable)) {
206+
boolean isFailed = !variables.get(variable);
207+
if (isFailed) {
208+
bitstream.clear();
209+
}
210+
}
211+
termCache.put(term, bitstream);
212+
}
176213
operandsStack.push(bitstream);
177-
} else if (term instanceof ANDTerm) {
178-
evaluateAND(term);
179-
} else if (term instanceof ORTerm) {
180-
evaluateOR(term);
181-
} else if (term instanceof NOTTerm) {
182-
evaluateNOT(term);
183214
} else {
184-
throw new IllegalArgumentException("SL does not support terms of class " + term.getClass());
215+
evaluateNonLiteralTerms(term, bitStreamLengthStructureFunction);
185216
}
186217
}
187218
}
188219

220+
/**
221+
* Helper function to process all terms that are not {@link LiteralTerm}s to
222+
* enhance code re-use.
223+
*
224+
* @param term the term to evaluate
225+
* @param bitstreamlength the length of the bit stream
226+
*/
227+
protected void evaluateNonLiteralTerms(Term term, int bitstreamlength) {
228+
if (term instanceof FALSETerm) {
229+
BitSet bitstream = new BitSet(bitStreamLength);
230+
bitstream.clear();
231+
operandsStack.push(bitstream);
232+
} else if (term instanceof TRUETerm) {
233+
BitSet bitstream = new BitSet(bitStreamLength);
234+
bitstream.set(0, bitstream.size(), true);
235+
operandsStack.push(bitstream);
236+
} else if (term instanceof ANDTerm) {
237+
evaluateAND(term);
238+
} else if (term instanceof ORTerm) {
239+
evaluateOR(term);
240+
} else if (term instanceof NOTTerm) {
241+
evaluateNOT(term);
242+
} else {
243+
throw new IllegalArgumentException("SL does not support terms of class " + term.getClass());
244+
}
245+
}
246+
189247
/**
190248
* The evaluation of an {@link ANDTerm} with respective AND operation on the bit
191249
* streams of the operands.
@@ -270,65 +328,6 @@ protected BitSet generateRandomBitstream(double probability) {
270328
return bitstream;
271329
}
272330

273-
@Override
274-
public boolean isProvidingService(Map<T, Boolean> variables) {
275-
operandsStack.clear();
276-
termCache.clear();
277-
// Evaluate leaves the final bit stream of the top event on the stack
278-
evaluateStructureFunction(variables);
279-
BitSet bitstream = operandsStack.pop();
280-
System.err.println(bitstream);
281-
return bitstream.cardinality() > 0;
282-
}
283-
284-
/**
285-
* Evaluates the {@link Term} as SL would, but a bit stream length of 1
286-
*
287-
* @param variables
288-
*/
289-
protected void evaluateStructureFunction(Map<T, Boolean> variables) {
290-
for (Term term : termsForStackProcessing) {
291-
if (term instanceof LiteralTerm) {
292-
BitSet bitstream = termCache.get(term);
293-
if (bitstream == null) {
294-
@SuppressWarnings("unchecked")
295-
LiteralTerm<T> component = (LiteralTerm<T>) term;
296-
T variable = component.get();
297-
bitstream = new BitSet(bitStreamLengthStructureFunction);
298-
// Default: Set all entries to 1 (captures 1 variables and all non-specified
299-
// ones)
300-
// Careful, this only works in coherent systems!
301-
bitstream.set(0, bitstream.size(), true);
302-
// Pull 0 variables to all 0 bit streams
303-
if (variables.containsKey(variable)) {
304-
boolean isFailed = !variables.get(variable);
305-
if (isFailed) { // Set all entries to 0
306-
bitstream.clear();
307-
}
308-
}
309-
termCache.put(term, bitstream);
310-
}
311-
operandsStack.push(bitstream);
312-
} else if (term instanceof FALSETerm) {
313-
BitSet bitstream = new BitSet(bitStreamLengthStructureFunction);
314-
bitstream.clear();
315-
operandsStack.push(bitstream);
316-
} else if (term instanceof TRUETerm) {
317-
BitSet bitstream = new BitSet(bitStreamLengthStructureFunction);
318-
bitstream.set(0, bitstream.size(), true);
319-
operandsStack.push(bitstream);
320-
} else if (term instanceof ANDTerm) {
321-
evaluateAND(term);
322-
} else if (term instanceof ORTerm) {
323-
evaluateOR(term);
324-
} else if (term instanceof NOTTerm) {
325-
evaluateNOT(term);
326-
} else {
327-
throw new IllegalArgumentException("SL does not support terms of class " + term.getClass());
328-
}
329-
}
330-
}
331-
332331
/**
333332
* Returns the length of the bitstreams when using the {@link StructureFunction}
334333
* interface.

src/test/java/org/jreliability/sl/SLTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,19 @@ public void testIsProvidingService() {
223223
Assertions.assertFalse(sl.isProvidingService(failedComponents));
224224
}
225225

226+
@Test
227+
public void testGetSetBitStreamLength() {
228+
// Sensor 1 & 2 in parallel, sensor 3 in series
229+
String var1 = "sensor1";
230+
Term s1 = new LiteralTerm<>(var1);
231+
SL<String> sl = new SL<String>(s1);
232+
233+
// Default 1
234+
Assertions.assertEquals(sl.getBitStreamLengthStructureFunction(), 1);
235+
236+
sl.setBitStreamLengthStructureFunction(100);
237+
// Default 1
238+
Assertions.assertEquals(sl.getBitStreamLengthStructureFunction(), 100);
239+
}
240+
226241
}

0 commit comments

Comments
 (0)