1 | package de.uka.ipd.sdq.pcmsolver.transformations.pcm2regex; |
2 | |
3 | import java.util.List; |
4 | |
5 | import de.uka.ipd.sdq.spa.expression.Alternative; |
6 | import de.uka.ipd.sdq.spa.expression.Expression; |
7 | import de.uka.ipd.sdq.spa.expression.ExpressionFactory; |
8 | import de.uka.ipd.sdq.spa.expression.Option; |
9 | |
10 | public class Pcm2RegexHelper { |
11 | |
12 | public static Expression createAlternativesForExpressions( |
13 | List<Option> listOfExpressions) { |
14 | |
15 | if (listOfExpressions.size() == 1){ |
16 | return listOfExpressions.get(0).getRegexp(); |
17 | } else if (listOfExpressions.size() == 0){ |
18 | return null; |
19 | } |
20 | |
21 | ExpressionFactory expFactory = ExpressionFactory.eINSTANCE; |
22 | Alternative alternative = expFactory.createAlternative(); |
23 | |
24 | Option leftOption = listOfExpressions.get(0); |
25 | alternative.setLeftOption(leftOption); |
26 | |
27 | Option rightOption = expFactory.createOption(); |
28 | alternative.setRightOption(rightOption); |
29 | rightOption.setProbability(1-leftOption.getProbability()); |
30 | |
31 | if (listOfExpressions.size() == 2){ |
32 | //this is the leaf |
33 | alternative.setRightOption(listOfExpressions.get(1)); |
34 | } else { |
35 | Expression subAlternative = createAlternativesForExpressions(listOfExpressions.subList(1, listOfExpressions.size())); |
36 | rightOption.setRegexp(subAlternative); |
37 | } |
38 | return alternative; |
39 | } |
40 | |
41 | } |