Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.emoflon.gips.intermediate.GipsIntermediate.RuleMapping
import org.emoflon.gips.intermediate.GipsIntermediate.VariableType
import org.emoflon.ibex.patternmodel.IBeXPatternModel.IBeXContextAlternatives
import org.emoflon.ibex.patternmodel.IBeXPatternModel.IBeXContextPattern
import org.eclipse.xtend.core.richstring.EndIf

class RuleMappingTemplate extends ClassGeneratorTemplate<RuleMapping> {

Expand Down Expand Up @@ -52,18 +53,32 @@ class RuleMappingTemplate extends ClassGeneratorTemplate<RuleMapping> {
«ENDIF»

public «className»(final String milpVariable, final boolean hasBinaryVariable, final «data.mapping2matchClassName.get(context)» match) {
super(milpVariable, hasBinaryVariable, match);
super(milpVariable, hasBinaryVariable, match);

«IF !context.freeVariables.isNullOrEmpty»
«FOR v : context.freeVariables»
«v.name.toFirstLower» = new «GipsImportManager.variableToJavaDataType(v, imports)»(name + "->«v.name»");
«IF v.type === VariableType.REAL»
«v.name.toFirstLower».setUpperBound(«v.upperBound»);
«v.name.toFirstLower».setLowerBound(«v.lowerBound»);
«ELSE»
«v.name.toFirstLower».setUpperBound((int)«v.upperBound»);
«v.name.toFirstLower».setLowerBound((int)«v.lowerBound»);
«ENDIF»
«ENDFOR»
«ENDIF»

«IF !context.freeVariables.isNullOrEmpty»
«FOR v : context.freeVariables»
«v.name.toFirstLower» = new «GipsImportManager.variableToJavaDataType(v, imports)»(name + "->«v.name»");
«ENDFOR»
«ENDIF»

«IF !context.boundVariables.isNullOrEmpty»
«FOR v : context.boundVariables»
«v.name.toFirstLower» = new «GipsImportManager.variableToJavaDataType(v, imports)»(name + "->«v.name»");
«ENDFOR»
«FOR v : context.boundVariables»
«v.name.toFirstLower» = new «GipsImportManager.variableToJavaDataType(v, imports)»(name + "->«v.name»");
«IF v.type === VariableType.REAL»
«v.name.toFirstLower».setUpperBound(«v.upperBound»);
«v.name.toFirstLower».setLowerBound(«v.lowerBound»);
«ELSE»
«v.name.toFirstLower».setUpperBound((int)«v.upperBound»);
«v.name.toFirstLower».setLowerBound((int)«v.lowerBound»);
«ENDIF»
«ENDFOR»
«ENDIF»
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public GipsBooleanExpression tryRule(GipslFactory factory, GipsBooleanExpression
relational.setOperator(RelationalOperator.EQUAL);
relational.setLeft(EcoreUtil.copy(pattern.getNodeA()));

if (pattern.getLiteral().getValue().equals("1")) {
relational.setRight(GeneratorUtil.createArithmeticLiteral(factory, "0"));
if (pattern.getLiteral().getValue() == 1) {
relational.setRight(GeneratorUtil.createIntegerLiteral(factory, 0));
} else {
relational.setRight(GeneratorUtil.createArithmeticLiteral(factory, "1"));
relational.setRight(GeneratorUtil.createIntegerLiteral(factory, 1));
}

return relational;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public GipsBooleanExpression tryRule(GipslFactory factory, GipsBooleanExpression
for (var element : pattern.getOtherNodes())
summands.add(EcoreUtil.copy(element));

var minusN = factory.createGipsArithmeticLiteral();
minusN.setValue(Integer.toString(1 - pattern.getOtherNodes().size()));
var minusN = factory.createGipsIntegerLiteral();
minusN.setValue(1 - pattern.getOtherNodes().size());
summands.add(minusN);

// B + C + ... + (1-n)
Expand All @@ -81,8 +81,7 @@ public GipsBooleanExpression tryRule(GipslFactory factory, GipsBooleanExpression
// A <= B*M
// ...
for (var element : pattern.getOtherNodes()) {
var bigM = GeneratorUtil.createArithmeticLiteral(factory,
Double.toString(PluginPreferences.getBigMValue()));
var bigM = GeneratorUtil.createDoubleLiteral(factory, PluginPreferences.getBigMValue());

var bigMProduct = factory.createGipsArithmeticProduct();
bigMProduct.setOperator(GipsProductOperator.MULT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ public GipsBooleanExpression tryRule(GipslFactory factory, GipsBooleanExpression
for (var element : pattern.getOtherNodes())
summands.add(EcoreUtil.copy(element));

var minusN = factory.createGipsArithmeticLiteral();
minusN.setValue(Integer.toString(1 - pattern.getOtherNodes().size()));
var minusN = GeneratorUtil.createIntegerLiteral(factory, 1 - pattern.getOtherNodes().size());
summands.add(minusN);

// B + C + ... + (1-n)
Expand All @@ -95,7 +94,7 @@ public GipsBooleanExpression tryRule(GipslFactory factory, GipsBooleanExpression
var sum = GeneratorUtil.sum(factory, GipsSumOperator.PLUS, summands);

// n
var n = GeneratorUtil.createArithmeticLiteral(factory, Double.toString(summands.size()));
var n = GeneratorUtil.createIntegerLiteral(factory, summands.size());

// n * A
var nProduct = factory.createGipsArithmeticProduct();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ public GipsBooleanExpression tryRule(GipslFactory factory, GipsBooleanExpression
var product = factory.createGipsArithmeticProduct();
product.setOperator(GipsProductOperator.MULT);
product.setLeft(EcoreUtil.copy(pattern.getNodeA()));
product.setRight(
GeneratorUtil.createArithmeticLiteral(factory, Integer.toString(pattern.getOtherNodes().size())));
product.setRight(GeneratorUtil.createIntegerLiteral(factory, pattern.getOtherNodes().size()));

// B + C (+ D + ...) <= A*n
var conjunctTwo = factory.createGipsRelationalExpression();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.emoflon.gips.build.gipsl.preprocess;

import static org.emoflon.gips.gipsl.generator.GeneratorUtil.createArithmeticLiteral;

import org.eclipse.emf.ecore.util.EcoreUtil;
import org.emoflon.gips.build.preference.PluginPreferences;
import org.emoflon.gips.gipsl.generator.GeneratorUtil;
Expand Down Expand Up @@ -41,7 +39,7 @@ public GipsBooleanExpression tryRule(GipslFactory factory, GipsBooleanExpression
if (!pattern.matchPattern(expression))
return null;

var one = createArithmeticLiteral(factory, "1");
var one = GeneratorUtil.createIntegerLiteral(factory, 1);

// 1-B
var oneMinusB = factory.createGipsArithmeticSum();
Expand All @@ -55,7 +53,7 @@ public GipsBooleanExpression tryRule(GipslFactory factory, GipsBooleanExpression
conjunctOne.setLeft(EcoreUtil.copy(oneMinusB));
conjunctOne.setRight(EcoreUtil.copy(pattern.getNodeA()));

var bigM = createArithmeticLiteral(factory, Double.toString(PluginPreferences.getBigMValue()));
var bigM = GeneratorUtil.createDoubleLiteral(factory, PluginPreferences.getBigMValue());

// (1-B)*M
var bigMProduct = factory.createGipsArithmeticProduct();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public GipsBooleanExpression tryRule(GipslFactory factory, GipsBooleanExpression
var relational = factory.createGipsRelationalExpression();
relational.setOperator(RelationalOperator.EQUAL);
relational.setLeft(EcoreUtil.copy(pattern.getNodeA()));
relational.setRight(GeneratorUtil.createArithmeticLiteral(factory, "1"));
relational.setRight(GeneratorUtil.createIntegerLiteral(factory, 1));

// if (expression.eContainer() instanceof GipsBooleanNegation) {
// var brackets = factory.createGipsBooleanBracket();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EcorePackage;
import org.emoflon.gips.gipsl.gipsl.GipsArithmeticLiteral;
import org.emoflon.gips.gipsl.gipsl.GipsDoubleLiteral;
import org.emoflon.gips.gipsl.gipsl.GipsIntegerLiteral;
import org.emoflon.gips.gipsl.gipsl.GipsVariable;
import org.emoflon.gips.intermediate.GipsIntermediate.ArithmeticBinaryExpression;
import org.emoflon.gips.intermediate.GipsIntermediate.ArithmeticExpression;
Expand Down Expand Up @@ -310,27 +313,89 @@ public static Set<ConstantReference> extractConstantReference(final RelationalEx
}

public static double getUpperBound(final GipsVariable gipsVar, final VariableType type) {
if (type == VariableType.BINARY) {
return 1;
} else if (type == VariableType.INTEGER) {
return Integer.MAX_VALUE;
} else if (type == VariableType.REAL) {
return Double.MAX_VALUE;
if (gipsVar.getInterval() != null && !gipsVar.getInterval().isUpperInfinity()) {
var upperBound = getValue(gipsVar.getInterval().getUpperBound());
return getLeastUpperBound(upperBound, type);
}

throw new UnsupportedOperationException();
return getMaxValue(type);
}

public static double getLowerBound(final GipsVariable gipsVar, final VariableType type) {
if (type == VariableType.BINARY) {
return 0;
} else if (type == VariableType.INTEGER) {
return Integer.MIN_VALUE;
} else if (type == VariableType.REAL) {
return -Double.MAX_VALUE;
if (gipsVar.getInterval() != null && !gipsVar.getInterval().isLowerInfinity()) {
var lowerBound = getValue(gipsVar.getInterval().getLowerBound());
return getGreatestLowerBound(lowerBound, type);
}

throw new UnsupportedOperationException();
return getMinValue(type);
}

private static double getValue(final GipsArithmeticLiteral literal) {
return switch (literal) {
case GipsIntegerLiteral val -> val.getValue();
case GipsDoubleLiteral val -> val.getValue();
default -> throw new IllegalArgumentException("Unexpected value: " + literal);
};
}

public static double getLeastUpperBound(double upperBound, VariableType type) {
if (Double.isNaN(upperBound))
throw new IllegalArgumentException("Boundary value cannot be NaN.");

return switch (type) {
case BINARY -> getMaxValue(VariableType.BINARY);
case REAL -> upperBound;
case INTEGER -> {
double floored = Math.floor(upperBound);
if (floored <= getMinValue(VariableType.INTEGER))
yield getMinValue(VariableType.INTEGER);

if (floored >= getMaxValue(VariableType.INTEGER))
yield getMaxValue(VariableType.INTEGER);

yield floored;
}
default -> throw new IllegalArgumentException("Unexpected value: " + type);
};
}

public static double getGreatestLowerBound(double lowerBound, VariableType type) {
if (Double.isNaN(lowerBound))
throw new IllegalArgumentException("Boundary value cannot be NaN.");

return switch (type) {
case BINARY -> getMinValue(VariableType.BINARY);
case REAL -> lowerBound;
case INTEGER -> {
double ceiled = Math.ceil(lowerBound);
if (ceiled <= getMinValue(VariableType.INTEGER))
yield getMinValue(VariableType.INTEGER);

if (ceiled >= getMaxValue(VariableType.INTEGER))
yield getMaxValue(VariableType.INTEGER);

yield ceiled;
}
default -> throw new IllegalArgumentException("Unexpected value: " + type);
};
}

public static double getMaxValue(VariableType type) {
return switch (type) {
case BINARY -> 1.0;
case INTEGER -> Integer.MAX_VALUE;
case REAL -> Double.MAX_VALUE;
default -> throw new IllegalArgumentException("Unexpected value: " + type);
};
}

public static double getMinValue(VariableType type) {
return switch (type) {
case BINARY -> 0.0;
case INTEGER -> Integer.MIN_VALUE;
case REAL -> -Double.MAX_VALUE;
default -> throw new IllegalArgumentException("Unexpected value: " + type);
};
}

public static ExpressionReturnType extractReturnType(BooleanExpression expression) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.emoflon.gips.gipsl.gipsl.GipsArithmeticUnary;
import org.emoflon.gips.gipsl.gipsl.GipsConstantReference;
import org.emoflon.gips.gipsl.gipsl.GipsConstraint;
import org.emoflon.gips.gipsl.gipsl.GipsDoubleLiteral;
import org.emoflon.gips.gipsl.gipsl.GipsIntegerLiteral;
import org.emoflon.gips.gipsl.gipsl.GipsLinearFunction;
import org.emoflon.gips.gipsl.gipsl.GipsLinearFunctionReference;
import org.emoflon.gips.gipsl.gipsl.GipsObjective;
Expand Down Expand Up @@ -162,22 +164,20 @@ public ArithmeticExpression transform(final GipsArithmeticBracket bracket) throw
}

public ArithmeticExpression transform(final GipsArithmeticLiteral literal) throws Exception {
try {
int value = Integer.parseInt(literal.getValue());
IntegerLiteral intLit = factory.createIntegerLiteral();
intLit.setLiteral(value);
return intLit;
} catch (Exception e) {
try {
double dValue = Double.parseDouble(literal.getValue());
DoubleLiteral doubleLit = factory.createDoubleLiteral();
doubleLit.setLiteral(dValue);
return doubleLit;
} catch (Exception e2) {
throw new IllegalArgumentException(
"Value <" + literal.getValue() + "> can't be parsed to neither integer nor double value.");
}
}
return switch (literal) {
case GipsIntegerLiteral value -> {
IntegerLiteral lit = factory.createIntegerLiteral();
lit.setLiteral(value.getValue());
yield lit;
}
case GipsDoubleLiteral value -> {
DoubleLiteral lit = factory.createDoubleLiteral();
lit.setLiteral(value.getValue());
yield lit;
}
default -> throw new IllegalArgumentException(
"Value <" + literal + "> can't be parsed to neither integer nor double value.");
};
}

public ArithmeticExpression transform(final GipsArithmeticConstant constant) throws Exception {
Expand Down
20 changes: 17 additions & 3 deletions org.emoflon.gips.gipsl/model/generated/Gipsl.ecore
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
<eClassifiers xsi:type="ecore:EClass" name="GipsVariable">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="type" eType="ecore:EClass http://www.eclipse.org/emf/2002/Ecore#//EDataType"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="interval" eType="#//GipsInterval"
containment="true"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="bound" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EBoolean"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="GipsMapping">
Expand All @@ -83,6 +85,14 @@
<eClassifiers xsi:type="ecore:EClass" name="GipsTypeExtensionVariable" eSuperTypes="#//GipsVariable">
<eStructuralFeatures xsi:type="ecore:EReference" name="attribute" eType="ecore:EClass http://www.eclipse.org/emf/2002/Ecore#//EAttribute"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="GipsInterval">
<eStructuralFeatures xsi:type="ecore:EReference" name="lowerBound" eType="#//GipsArithmeticLiteral"
containment="true"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="lowerInfinity" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EBoolean"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="upperBound" eType="#//GipsArithmeticLiteral"
containment="true"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="upperInfinity" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EBoolean"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="GipsConstant">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="expression" eType="#//GipsBooleanExpression"
Expand Down Expand Up @@ -150,9 +160,7 @@
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="GipsArithmeticExpression" eSuperTypes="#//GipsBooleanExpression"/>
<eClassifiers xsi:type="ecore:EClass" name="GipsArithmeticOperand" eSuperTypes="#//GipsArithmeticExpression"/>
<eClassifiers xsi:type="ecore:EClass" name="GipsArithmeticLiteral" eSuperTypes="#//GipsArithmeticOperand">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="value" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="GipsArithmeticLiteral" eSuperTypes="#//GipsArithmeticOperand"/>
<eClassifiers xsi:type="ecore:EClass" name="GipsArithmeticConstant" eSuperTypes="#//GipsArithmeticOperand">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="value" eType="#//GipsConstantLiteral"/>
</eClassifiers>
Expand Down Expand Up @@ -315,6 +323,12 @@
<eLiterals name="NOT_EMPTY" value="1" literal="notEmpty"/>
<eLiterals name="COUNT" value="2" literal="count"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="GipsDoubleLiteral" eSuperTypes="#//GipsArithmeticLiteral">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="value" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EDouble"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="GipsIntegerLiteral" eSuperTypes="#//GipsArithmeticLiteral">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="value" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="GipsBooleanImplication" eSuperTypes="#//GipsBooleanExpression">
<eStructuralFeatures xsi:type="ecore:EReference" name="left" eType="#//GipsBooleanExpression"
containment="true"/>
Expand Down
Loading