|
| 1 | +package org.emoflon.gips.build.gipsl.preprocess; |
| 2 | + |
| 3 | +import java.util.Objects; |
| 4 | + |
| 5 | +import org.eclipse.emf.ecore.util.EcoreUtil; |
| 6 | +import org.emoflon.gips.gipsl.gipsl.GipsArithmeticExpression; |
| 7 | +import org.emoflon.gips.gipsl.gipsl.GipsBooleanBracket; |
| 8 | +import org.emoflon.gips.gipsl.gipsl.GipsBooleanConjunction; |
| 9 | +import org.emoflon.gips.gipsl.gipsl.GipsBooleanDisjunction; |
| 10 | +import org.emoflon.gips.gipsl.gipsl.GipsBooleanExpression; |
| 11 | +import org.emoflon.gips.gipsl.gipsl.GipsBooleanImplication; |
| 12 | +import org.emoflon.gips.gipsl.gipsl.GipsBooleanLiteral; |
| 13 | +import org.emoflon.gips.gipsl.gipsl.GipsBooleanNegation; |
| 14 | +import org.emoflon.gips.gipsl.gipsl.GipsConstraint; |
| 15 | +import org.emoflon.gips.gipsl.gipsl.GipsRelationalExpression; |
| 16 | +import org.emoflon.gips.gipsl.gipsl.GipslFactory; |
| 17 | +import org.emoflon.gips.gipsl.gipsl.GipslPackage; |
| 18 | + |
| 19 | +public class GipslPreprocessor { |
| 20 | + |
| 21 | + private final static PreprocessorRule[] rules = new PreprocessorRule[] { // |
| 22 | + new RuleEquivalenceShortcutA(), // |
| 23 | + new RuleEquivalenceShortcutB(), // |
| 24 | + new RuleEquivalenceShortcutC(), // |
| 25 | + new RuleEquivalenceShortcutD(), // |
| 26 | + new RuleImplicationShortcutA() }; |
| 27 | + |
| 28 | + private final GipslFactory factory; |
| 29 | + |
| 30 | + public GipslPreprocessor() { |
| 31 | + this(GipslPackage.eINSTANCE.getGipslFactory()); |
| 32 | + } |
| 33 | + |
| 34 | + public GipslPreprocessor(GipslFactory factory) { |
| 35 | + this.factory = Objects.requireNonNull(factory, "factory"); |
| 36 | + } |
| 37 | + |
| 38 | + public GipsBooleanExpression preprocess(GipsConstraint constraint) { |
| 39 | + GipsBooleanExpression expression = constraint.getExpression(); |
| 40 | + |
| 41 | + // This part can be used for multi-pass rule application (whenever this may |
| 42 | + // becomes relevant in the future). It is commented out because, at present, no |
| 43 | + // rule requires multiple passes. |
| 44 | +// GipsBooleanExpression currentPass = applyRules(expression); |
| 45 | +// GipsBooleanExpression previousPass = null; |
| 46 | +// |
| 47 | +// while (currentPass != null) { |
| 48 | +// previousPass = currentPass; |
| 49 | +// currentPass = applyRules(currentPass); |
| 50 | +// } |
| 51 | +// |
| 52 | +// return previousPass != null ? previousPass : expression; |
| 53 | + |
| 54 | + GipsBooleanExpression newExpression = tryToApplyRules(expression); |
| 55 | + return newExpression != null ? newExpression : expression; |
| 56 | + } |
| 57 | + |
| 58 | + private GipsBooleanExpression tryToApplyRules(GipsBooleanExpression expression) { |
| 59 | + // depth first |
| 60 | + var newExpression = switch (expression) { |
| 61 | + case GipsBooleanImplication implication -> tryExpand(implication); |
| 62 | + case GipsBooleanConjunction conjunction -> tryExpand(conjunction); |
| 63 | + case GipsBooleanDisjunction disjunction -> tryExpand(disjunction); |
| 64 | + case GipsBooleanBracket bracket -> tryExpand(bracket); |
| 65 | + case GipsBooleanNegation negation -> tryExpand(negation); |
| 66 | + case GipsBooleanLiteral literal -> null; |
| 67 | + case GipsArithmeticExpression arithmetic -> null; |
| 68 | + case GipsRelationalExpression relational -> null; |
| 69 | + default -> throw new IllegalArgumentException("Unexpected value: " + expression); |
| 70 | + }; |
| 71 | + |
| 72 | + if (newExpression != null) { |
| 73 | + var replacement = tryAllRules(newExpression); |
| 74 | + return replacement != null ? replacement : newExpression; |
| 75 | + } |
| 76 | + |
| 77 | + return tryAllRules(expression); |
| 78 | + } |
| 79 | + |
| 80 | + private GipsBooleanExpression tryExpand(GipsBooleanImplication expression) { |
| 81 | + var newLeft = tryToApplyRules(expression.getLeft()); |
| 82 | + var newRight = tryToApplyRules(expression.getRight()); |
| 83 | + if (newLeft == null && newRight == null) |
| 84 | + return null; |
| 85 | + |
| 86 | + var newExpression = factory.createGipsBooleanImplication(); |
| 87 | + newExpression.setOperator(expression.getOperator()); |
| 88 | + newExpression.setLeft(newLeft != null ? newLeft : EcoreUtil.copy(expression.getLeft())); |
| 89 | + newExpression.setRight(newRight != null ? newRight : EcoreUtil.copy(expression.getRight())); |
| 90 | + return newExpression; |
| 91 | + } |
| 92 | + |
| 93 | + private GipsBooleanExpression tryExpand(GipsBooleanConjunction expression) { |
| 94 | + var newLeft = tryToApplyRules(expression.getLeft()); |
| 95 | + var newRight = tryToApplyRules(expression.getRight()); |
| 96 | + if (newLeft == null && newRight == null) |
| 97 | + return null; |
| 98 | + |
| 99 | + var newExpression = factory.createGipsBooleanConjunction(); |
| 100 | + newExpression.setLeft(newLeft != null ? newLeft : EcoreUtil.copy(expression.getLeft())); |
| 101 | + newExpression.setRight(newRight != null ? newRight : EcoreUtil.copy(expression.getRight())); |
| 102 | + return newExpression; |
| 103 | + } |
| 104 | + |
| 105 | + private GipsBooleanExpression tryExpand(GipsBooleanDisjunction expression) { |
| 106 | + var newLeft = tryToApplyRules(expression.getLeft()); |
| 107 | + var newRight = tryToApplyRules(expression.getRight()); |
| 108 | + if (newLeft == null && newRight == null) |
| 109 | + return null; |
| 110 | + |
| 111 | + var newExpression = factory.createGipsBooleanDisjunction(); |
| 112 | + newExpression.setLeft(newLeft != null ? newLeft : EcoreUtil.copy(expression.getLeft())); |
| 113 | + newExpression.setRight(newRight != null ? newRight : EcoreUtil.copy(expression.getRight())); |
| 114 | + return newExpression; |
| 115 | + } |
| 116 | + |
| 117 | + private GipsBooleanExpression tryExpand(GipsBooleanBracket expression) { |
| 118 | + var newOperand = tryToApplyRules(expression.getOperand()); |
| 119 | + if (newOperand == null) |
| 120 | + return null; |
| 121 | + |
| 122 | + var newExpression = factory.createGipsBooleanBracket(); |
| 123 | + newExpression.setOperand(newOperand); |
| 124 | + return newExpression; |
| 125 | + } |
| 126 | + |
| 127 | + private GipsBooleanExpression tryExpand(GipsBooleanNegation expression) { |
| 128 | + var newOperand = tryToApplyRules(expression.getOperand()); |
| 129 | + if (newOperand == null) |
| 130 | + return null; |
| 131 | + |
| 132 | + var newExpression = factory.createGipsBooleanNegation(); |
| 133 | + newExpression.setOperand(newOperand); |
| 134 | + return newExpression; |
| 135 | + } |
| 136 | + |
| 137 | + private GipsBooleanExpression tryAllRules(GipsBooleanExpression expression) { |
| 138 | + for (var rule : rules) { |
| 139 | + GipsBooleanExpression newExpression = rule.tryRule(factory, expression); |
| 140 | + if (newExpression != null) |
| 141 | + return newExpression; |
| 142 | + } |
| 143 | + |
| 144 | + return null; |
| 145 | + } |
| 146 | + |
| 147 | +} |
0 commit comments