|
1
|
|
|
package it.cnr.istc.pst.platinum.ai.lang.ddl.v3.parser;
|
|
2
|
|
|
|
|
3
|
|
|
import java.util.ArrayList;
|
|
4
|
|
|
import java.util.Collections;
|
|
5
|
|
|
import java.util.List;
|
|
6
|
|
|
|
|
7
|
|
|
import org.antlr.runtime.Token;
|
|
8
|
|
|
|
|
9
|
|
|
/**
|
|
10
|
|
|
*
|
|
11
|
|
|
* @author Riccardo De Benedictis
|
|
12
|
|
|
*/
|
|
13
|
|
|
public class DDLNumericParameterConstraint extends DDLParameterConstraint {
|
|
14
|
|
|
|
|
15
|
|
|
private DDLNumericParameterConstraintType constraintType;
|
|
16
|
|
|
private String leftTerm;
|
|
17
|
|
|
private long leftCoefficient;
|
|
18
|
|
|
private int absoluteValue = 0;
|
|
19
|
|
|
private final List<String> rightVariables = new ArrayList<>();
|
|
20
|
|
|
private final List<Integer> rightCoefficients = new ArrayList<>();
|
|
21
|
|
|
|
|
22
|
|
|
/**
|
|
23
|
|
|
*
|
|
24
|
|
|
* @param payload
|
|
25
|
|
|
*/
|
|
26
|
|
|
public DDLNumericParameterConstraint(Token payload) {
|
|
27
|
|
|
super(payload, DDLParameterConstraintType.NUMERIC);
|
|
28
|
|
|
}
|
|
29
|
|
|
|
|
30
|
|
|
@Override
|
|
31
|
|
|
void parse() {
|
|
32
|
|
|
String constraint_type = getText();
|
|
33
|
|
|
if (constraint_type.equals("=")) {
|
|
34
|
|
|
constraintType = DDLNumericParameterConstraintType.EQ;
|
|
35
|
|
|
}
|
|
36
|
|
|
if (constraint_type.equals(">")) {
|
|
37
|
|
|
constraintType = DDLNumericParameterConstraintType.GT;
|
|
38
|
|
|
}
|
|
39
|
|
|
if (constraint_type.equals("<")) {
|
|
40
|
|
|
constraintType = DDLNumericParameterConstraintType.LT;
|
|
41
|
|
|
}
|
|
42
|
|
|
if (constraint_type.equals(">=")) {
|
|
43
|
|
|
constraintType = DDLNumericParameterConstraintType.GE;
|
|
44
|
|
|
}
|
|
45
|
|
|
if (constraint_type.equals("<=")) {
|
|
46
|
|
|
constraintType = DDLNumericParameterConstraintType.LE;
|
|
47
|
|
|
}
|
|
48
|
|
|
if (constraint_type.equals("!=")) {
|
|
49
|
|
|
constraintType = DDLNumericParameterConstraintType.NEQ;
|
|
50
|
|
|
}
|
|
51
|
|
|
if (getChild(0).getType() == ddl3Lexer.VarID) {
|
|
52
|
|
|
leftTerm = getChild(0).getText();
|
|
53
|
|
|
leftCoefficient = 1l;
|
|
54
|
|
|
} else if (getChild(0).getText().equals("*")) {
|
|
55
|
|
|
long coefficient = getChild(0).getChild(0).getText().equals("INF") ? Long.MAX_VALUE - 1 : Long.parseLong(getChild(0).getChild(0).getText());
|
|
56
|
|
|
if (getChild(0).getChild(0).getChildCount() > 0) {
|
|
57
|
|
|
coefficient = getChild(0).getChild(0).getChild(0).getText().equals("-") ? -coefficient : coefficient;
|
|
58
|
|
|
}
|
|
59
|
|
|
leftTerm = getChild(1).getChild(1).getText();
|
|
60
|
|
|
leftCoefficient = coefficient;
|
|
61
|
|
|
}
|
|
62
|
|
|
if (getChild(1).getType() == ddl3Lexer.VarID) {
|
|
63
|
|
|
rightVariables.add(getChild(1).getText());
|
|
64
|
|
|
// rightCoefficients.add(1l);
|
|
65
|
|
|
rightCoefficients.add(1);
|
|
66
|
|
|
} else if (getChild(1).getText().equals("*")) {
|
|
67
|
|
|
int coefficient = getChild(1).getChild(0).getText().equals("INF") ? Integer.MAX_VALUE - 1 : Integer.parseInt(getChild(1).getChild(0).getText());
|
|
68
|
|
|
if (getChild(1).getChild(0).getChildCount() > 0) {
|
|
69
|
|
|
coefficient = getChild(1).getChild(0).getChild(0).getText().equals("-") ? -coefficient : coefficient;
|
|
70
|
|
|
}
|
|
71
|
|
|
rightVariables.add(getChild(1).getChild(1).getText());
|
|
72
|
|
|
rightCoefficients.add(coefficient);
|
|
73
|
|
|
} else {
|
|
74
|
|
|
int coefficient = getChild(1).getText().equals("INF") ? Integer.MAX_VALUE - 1 : Integer.parseInt(getChild(1).getText());
|
|
75
|
|
|
if (getChild(1).getChildCount() > 0) {
|
|
76
|
|
|
coefficient = getChild(1).getChild(0).getText().equals("-") ? -coefficient : coefficient;
|
|
77
|
|
|
}
|
|
78
|
|
|
rightVariables.add(null);
|
|
79
|
|
|
rightCoefficients.add(coefficient);
|
|
80
|
|
|
}
|
|
81
|
|
|
for (int i = 2; i < getChildCount(); i++) {
|
|
82
|
|
|
if (getChild(i).getText().equals("+")) {
|
|
83
|
|
|
if (getChild(i).getChild(0).getType() == ddl3Lexer.VarID) {
|
|
84
|
|
|
rightVariables.add(getChild(i).getChild(0).getText());
|
|
85
|
|
|
rightCoefficients.add(1);
|
|
86
|
|
|
} else if (getChild(i).getChild(0).getText().equals("*")) {
|
|
87
|
|
|
rightVariables.add(getChild(i).getChild(0).getChild(1).getText());
|
|
88
|
|
|
rightCoefficients.add(Integer.parseInt(getChild(i).getChild(0).getChild(0).getText()));
|
|
89
|
|
|
} else {
|
|
90
|
|
|
int coefficient = getChild(i).getChild(0).getText().equals("INF") ? Integer.MAX_VALUE - 1 : Integer.parseInt(getChild(i).getChild(0).getText());
|
|
91
|
|
|
if (getChild(i).getChildCount() > 0) {
|
|
92
|
|
|
coefficient = getChild(i).getChild(0).getText().equals("-") ? -coefficient : coefficient;
|
|
93
|
|
|
}
|
|
94
|
|
|
absoluteValue += coefficient;
|
|
95
|
|
|
}
|
|
96
|
|
|
} else {
|
|
97
|
|
|
if (getChild(i).getChild(0).getType() == ddl3Lexer.VarID) {
|
|
98
|
|
|
rightVariables.add(getChild(i).getChild(0).getText());
|
|
99
|
|
|
rightCoefficients.add(-1);
|
|
100
|
|
|
} else if (getChild(i).getChild(0).getText().equals("*")) {
|
|
101
|
|
|
rightVariables.add(getChild(i).getChild(0).getChild(1).getText());
|
|
102
|
|
|
rightCoefficients.add(-Integer.parseInt(getChild(i).getChild(0).getChild(0).getText()));
|
|
103
|
|
|
} else {
|
|
104
|
|
|
long coefficient = getChild(i).getChild(0).getText().equals("INF") ? Integer.MAX_VALUE - 1 : Integer.parseInt(getChild(i).getChild(0).getText());
|
|
105
|
|
|
if (getChild(i).getChildCount() > 0) {
|
|
106
|
|
|
coefficient = getChild(i).getChild(0).getText().equals("-") ? -coefficient : coefficient;
|
|
107
|
|
|
}
|
|
108
|
|
|
absoluteValue -= coefficient;
|
|
109
|
|
|
}
|
|
110
|
|
|
}
|
|
111
|
|
|
}
|
|
112
|
|
|
}
|
|
113
|
|
|
|
|
114
|
|
|
public boolean isBinary() {
|
|
115
|
|
|
return rightVariables.size() == 1 && absoluteValue == 0 && rightVariables.get(0) != null;
|
|
116
|
|
|
}
|
|
117
|
|
|
|
|
118
|
|
|
/**
|
|
119
|
|
|
* @return the constraintType
|
|
120
|
|
|
*/
|
|
121
|
|
|
public DDLNumericParameterConstraintType getNumericConstraintType() {
|
|
122
|
|
|
return constraintType;
|
|
123
|
|
|
}
|
|
124
|
|
|
|
|
125
|
|
|
/**
|
|
126
|
|
|
* @return the leftTerm
|
|
127
|
|
|
*/
|
|
128
|
|
|
public String getLeftTerm() {
|
|
129
|
|
|
return leftTerm;
|
|
130
|
|
|
}
|
|
131
|
|
|
|
|
132
|
|
|
/**
|
|
133
|
|
|
* @return the leftCoefficient
|
|
134
|
|
|
*/
|
|
135
|
|
|
public long getLeftCoefficient() {
|
|
136
|
|
|
return leftCoefficient;
|
|
137
|
|
|
}
|
|
138
|
|
|
|
|
139
|
|
|
/**
|
|
140
|
|
|
* @return the rightVariables
|
|
141
|
|
|
*/
|
|
142
|
|
|
public List<String> getRightVariables() {
|
|
143
|
|
|
return Collections.unmodifiableList(rightVariables);
|
|
144
|
|
|
}
|
|
145
|
|
|
|
|
146
|
|
|
/**
|
|
147
|
|
|
* @return the rightCoefficients
|
|
148
|
|
|
*/
|
|
149
|
|
|
public List<Integer> getRightCoefficients() {
|
|
150
|
|
|
return Collections.unmodifiableList(rightCoefficients);
|
|
151
|
|
|
}
|
|
152
|
|
|
|
|
153
|
|
|
// public int getAbsoluteValue() {
|
|
154
|
|
|
// return absoluteValue;
|
|
155
|
|
|
// }
|
|
156
|
|
|
|
|
157
|
|
|
public enum DDLNumericParameterConstraintType {
|
|
158
|
|
|
|
|
159
|
|
|
EQ, LT, GT, LE, GE, NEQ;
|
|
160
|
|
|
}
|
|
161
|
|
|
}
|
|
162
|
|
|
|