Skip to content

Commit 734250e

Browse files
committed
refactor: replace general Exceptions by specific ones
1 parent 8b2049a commit 734250e

20 files changed

+248
-195
lines changed

at.siemens.ct.jminizinc.core/src/main/java/at/siemens/ct/jmz/mznparser/Displayable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright Siemens AG, 2016
2+
* Copyright Siemens AG, 2016-2017
33
*
44
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
55
* If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
@@ -19,7 +19,7 @@
1919
*/
2020
public interface Displayable {
2121

22-
public List<Constraint> createConstraint(String values) throws Exception;
22+
public List<Constraint> createConstraint(String values);
2323

2424
public List<InfoGUI> getInfo();
2525

at.siemens.ct.jminizinc.core/src/main/java/at/siemens/ct/jmz/mznparser/DisplayableBooleanArray.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright Siemens AG, 2016
2+
* Copyright Siemens AG, 2016-2017
33
*
44
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
55
* If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
@@ -28,7 +28,7 @@ public DisplayableBooleanArray(String name, SetExpression<Integer> range) {
2828
}
2929

3030
@Override
31-
public List<Constraint> createConstraint(String value) throws Exception {
31+
public List<Constraint> createConstraint(String value) {
3232

3333
List<Constraint> constraints = new ArrayList<>();
3434
Constraint constraint;
@@ -38,7 +38,8 @@ public List<Constraint> createConstraint(String value) throws Exception {
3838

3939
if(!(type instanceof RangeExpression))
4040
{
41-
throw new Exception(String.format("This type of variable: %s cannot be used as index for array", type.use()));
41+
throw new UnsupportedOperationException(
42+
String.format("This type of variable: %s cannot be used as index for array", type.use()));
4243
}
4344

4445
RangeExpression arrayIndexRange = (RangeExpression) type;

at.siemens.ct.jminizinc.core/src/main/java/at/siemens/ct/jmz/mznparser/DisplayableIntegerArray.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright Siemens AG, 2016
2+
* Copyright Siemens AG, 2016-2017
33
*
44
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
55
* If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
@@ -33,15 +33,16 @@ public DisplayableIntegerArray(String name, SetExpression<Integer> range, SetExp
3333
}
3434

3535
@Override
36-
public List<Constraint> createConstraint(String value) throws Exception {
36+
public List<Constraint> createConstraint(String value) {
3737
List<Constraint> constraints = new ArrayList<>();
3838

3939
String[] integerArrayValues = value.split(",");
4040

4141
SetExpression<Integer> type = this.getIntegerArray().getRange().get(0);
4242
if(!(type instanceof RangeExpression))
4343
{
44-
throw new Exception(String.format("This type of variable: %s cannot be used as index for array", type.use()));
44+
throw new UnsupportedOperationException(
45+
String.format("This type of variable: %s cannot be used as index for array", type.use()));
4546
}
4647
RangeExpression arrayIndexRange = (RangeExpression) type;
4748

@@ -54,7 +55,7 @@ public List<Constraint> createConstraint(String value) throws Exception {
5455

5556
if (integerArrayValues[i] != "Undefined" && !integerArrayValues[i].isEmpty()) {
5657
if (!MiniZincElementFactory.isNumeric(integerArrayValues[i]))
57-
throw new Exception(
58+
throw new UnsupportedOperationException(
5859
"Wrong value inserted for variable " + this.getName() + ". His value must be an integer.");
5960

6061
int variablevalue = Integer.parseInt(integerArrayValues[i]);

at.siemens.ct.jminizinc.core/src/main/java/at/siemens/ct/jmz/mznparser/DisplayableIntegerVariable.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright Siemens AG, 2016
2+
* Copyright Siemens AG, 2016-2017
33
*
44
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
55
* If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
@@ -36,15 +36,15 @@ public DisplayableIntegerVariable(String name, SetExpression<Integer> type, Expr
3636
}
3737

3838
@Override
39-
public List<Constraint> createConstraint(String value) throws Exception {
39+
public List<Constraint> createConstraint(String value) {
4040
List<Constraint> constraints = new ArrayList<>();
4141
String variableName = this.getName();
4242

4343
if (value.equals("Undefined") || value.isEmpty())
4444
return null;
4545

4646
if (!MiniZincElementFactory.isNumeric(value))
47-
throw new Exception(
47+
throw new IllegalArgumentException(
4848
"Wrong value inserted for variable " + variableName + ". His value must be an integer.");
4949

5050
int variableValue = this.parseValue(value);

at.siemens.ct.jminizinc.core/src/main/java/at/siemens/ct/jmz/mznparser/MiniZincCP.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
/**
2-
* Copyright Siemens AG, 2016
2+
* Copyright Siemens AG, 2016-2017
33
*
44
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
55
* If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
*/
77
package at.siemens.ct.jmz.mznparser;
88

99
import java.io.File;
10+
import java.io.IOException;
1011
import java.util.ArrayList;
1112
import java.util.List;
1213

@@ -22,28 +23,26 @@ public class MiniZincCP {
2223
private List<Displayable> elementsFromFile;
2324

2425
/**
25-
* creates an MiniZincCP object according to the .mzn file
26-
*
27-
* @param mznFile
28-
* - the .mzn File
29-
* @throws Exception
30-
*/
31-
public MiniZincCP(File mznFile) throws Exception {
26+
* creates an MiniZincCP object according to the .mzn file
27+
*
28+
* @param mznFile
29+
* - the .mzn File
30+
* @throws IOException
31+
*/
32+
public MiniZincCP(File mznFile) throws IOException {
3233
elementsFromFile = new ArrayList<Displayable>();
3334
parseMZN(mznFile);
3435
}
3536

3637
/**
37-
* Parses a .mzn file and populates lists with decision variables and
38-
* constraints
39-
*
40-
* @param mznFile
41-
* - the MiniZinc file for the CP (constraint problem)
42-
* @throws Exception
43-
* @throws IllegalAccessException
44-
* @throws IllegalArgumentException
45-
*/
46-
private void parseMZN(File mznFile) throws Exception {
38+
* Parses a .mzn file and populates lists with decision variables and
39+
* constraints
40+
*
41+
* @param mznFile
42+
* - the MiniZinc file for the CP (constraint problem)
43+
* @throws IOException
44+
*/
45+
private void parseMZN(File mznFile) throws IOException {
4746
List<String> linesFromMznFile = FileUtils.readLines(mznFile);
4847
Displayable mznElement;
4948

at.siemens.ct.jminizinc.core/src/main/java/at/siemens/ct/jmz/mznparser/MiniZincElementFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright Siemens AG, 2016
2+
* Copyright Siemens AG, 2016-2017
33
*
44
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
55
* If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
@@ -28,7 +28,7 @@ public class MiniZincElementFactory {
2828
private static final String RANGE_SEPARATOR = "..";
2929
private List<BasicTypeInst<?>> listWithParameters = new ArrayList<BasicTypeInst<?>>();
3030

31-
public Displayable getElementFromLine(String line) throws Exception {
31+
public Displayable getElementFromLine(String line) {
3232

3333
String instantiation, name, type, defaultValue, arrayIndex;
3434
for (PossibleVariablesDeclarationsPatterns patternAsString : PossibleVariablesDeclarationsPatterns.values()) {
@@ -52,7 +52,7 @@ public Displayable getElementFromLine(String line) throws Exception {
5252
if (arrayIndex.contains(RANGE_SEPARATOR)) {
5353
arraySize = createRange(arrayIndex);
5454
} else {
55-
throw new Exception("Unimplemented! define array range as m..n!");
55+
throw new UnsupportedOperationException("Unimplemented! define array range as m..n!");
5656
}
5757

5858
if (instantiation != null && instantiation.equals(VAR_TYPE) && defaultValue == null) {

at.siemens.ct.jminizinc.diag/src/main/java/at/siemens/ct/jmz/diag/AbstractConflictDetection.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ public AbstractConflictDetection(String mznFullFileName, Collection<? extends El
3838

3939
// TODO: Shall use Set instead of List?
4040
/**
41-
* Search for a conflict set in a given set
42-
*
43-
* @param constraintsSetC
44-
* a set of constraint
45-
* @return a conflict set
46-
* @throws Exception
47-
*/
48-
public abstract List<Constraint> getMinConflictSet(List<Constraint> constraintsSetC) throws Exception;
41+
* Search for a conflict set in a given set
42+
*
43+
* @param constraintsSetC
44+
* a set of constraint
45+
* @return a conflict set
46+
* @throws DiagnosisException
47+
*/
48+
public abstract List<Constraint> getMinConflictSet(List<Constraint> constraintsSetC) throws DiagnosisException;
4949

5050
public static List<Constraint> appendSets(List<Constraint> CS1, List<Constraint> CS2) {
5151
List<Constraint> reunion = new ArrayList<>(CS1);

at.siemens.ct.jminizinc.diag/src/main/java/at/siemens/ct/jmz/diag/ConsistencyChecker.java

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package at.siemens.ct.jmz.diag;
88

99
import java.io.File;
10+
import java.io.IOException;
1011
import java.util.Collection;
1112
import java.util.Collections;
1213
import java.util.List;
@@ -41,11 +42,11 @@ private void initialization() {
4142
}
4243

4344
/** Determines the consistency of a mzn file (Background KB)
44-
* @param mznFile - the mzn file which contains the background KB
45-
* @return true if is consistent
46-
* @throws Exception
47-
*/
48-
public boolean isConsistent(File mznFile) throws Exception {
45+
* @param mznFile - the mzn file which contains the background KB
46+
* @return true if is consistent
47+
* @throws DiagnosisException
48+
*/
49+
public boolean isConsistent(File mznFile) throws DiagnosisException {
4950
initialization();
5051
modelBuilder.reset();
5152

@@ -56,7 +57,7 @@ public boolean isConsistent(File mznFile) throws Exception {
5657
return (isSolverResultConsistent(res));
5758
}
5859

59-
public boolean isConsistent(Collection<? extends Element> fixedElements) throws Exception {
60+
public boolean isConsistent(Collection<? extends Element> fixedElements) throws DiagnosisException {
6061
initialization();
6162
modelBuilder.reset();
6263

@@ -67,17 +68,17 @@ public boolean isConsistent(Collection<? extends Element> fixedElements) throws
6768
}
6869

6970
/** Determines the consistency of given constraints set with the background KB
70-
* @param constraintsSet - the given constraints set
71-
* @param mznFile - the mzn file which contains the background KB
72-
* @return true if the given set is consistent with background KB
73-
* @throws Exception
74-
*/
75-
public boolean isConsistent(List<Constraint> constraintsSet, File mznFile) throws Exception {
71+
* @param constraintsSet - the given constraints set
72+
* @param mznFile - the mzn file which contains the background KB
73+
* @return true if the given set is consistent with background KB
74+
* @throws DiagnosisException
75+
*/
76+
public boolean isConsistent(List<Constraint> constraintsSet, File mznFile) throws DiagnosisException {
7677
return isConsistent(constraintsSet, Collections.emptySet(), mznFile);
7778
}
7879

7980
public boolean isConsistent(List<Constraint> constraintsSet, Collection<? extends Element> fixedModel, File mznFile)
80-
throws Exception {
81+
throws DiagnosisException {
8182
initialization();
8283
modelBuilder.reset();
8384

@@ -94,7 +95,8 @@ public boolean isConsistent(List<Constraint> constraintsSet, Collection<? extend
9495
return (isSolverResultConsistent(res));
9596
}
9697

97-
public boolean isConsistent(List<Constraint> constraintsSet, List<Element> decisionVariables) throws Exception {
98+
public boolean isConsistent(List<Constraint> constraintsSet, List<Element> decisionVariables)
99+
throws DiagnosisException {
98100
initialization();
99101
modelBuilder.reset();
100102
modelBuilder.add(constraintsSet);
@@ -109,15 +111,21 @@ private boolean isSolverResultConsistent(String result) {
109111
return res;
110112
}
111113

112-
private String callExecutor() throws Exception {
113-
executor.startProcess();
114-
executor.waitForSolution();
114+
private String callExecutor() throws DiagnosisException {
115+
try {
116+
executor.startProcess();
117+
executor.waitForSolution();
118+
} catch (IOException e) {
119+
throw new DiagnosisException("Solver could not be started", e);
120+
} catch (InterruptedException e) {
121+
}
122+
115123
int lastExitCode = executor.getLastExitCode();
116124
if (lastExitCode != IExecutor.EXIT_CODE_SUCCESS) {
117125
if (executor.getLastSolverErrors().isEmpty()) {
118-
throw new Exception("An error occured while running the solver. Some libraries are missing.");
126+
throw new DiagnosisException("An error occured while running the solver. Some libraries are missing.");
119127
} else {
120-
throw new Exception("An error occured while running the solver." + executor.getLastSolverErrors());
128+
throw new DiagnosisException("An error occured while running the solver." + executor.getLastSolverErrors());
121129
}
122130
}
123131

at.siemens.ct.jminizinc.diag/src/main/java/at/siemens/ct/jmz/diag/DiagnoseProgressCallback.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright Siemens AG, 2016
2+
* Copyright Siemens AG, 2016-2017
33
*
44
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
55
* If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
@@ -31,7 +31,7 @@ public interface DiagnoseProgressCallback {
3131
public void ignoredDiagnosis(List<Constraint> diagnosis, DiagnosisMetadata diagnosisMetadata);
3232

3333
public void displayPartOfDiagnosis(List<Constraint> diagnosis, List<Constraint> inputConflictSet, String nodeIndex,
34-
String indent) throws Exception;
34+
String indent);
3535

3636
public void displayPreferredDiagnosis(List<Constraint> diagnosis, List<Constraint> inputConflictSet);
3737

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright Siemens AG, 2017
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
5+
* If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package at.siemens.ct.jmz.diag;
8+
9+
/**
10+
*
11+
* @author Copyright Siemens AG, 2017
12+
*
13+
*/
14+
public class DiagnosisException extends Exception {
15+
16+
private static final long serialVersionUID = 1620929690602094866L;
17+
18+
public DiagnosisException(String message) {
19+
super(message);
20+
}
21+
22+
public DiagnosisException(String message, Throwable cause) {
23+
super(message, cause);
24+
}
25+
26+
}

0 commit comments

Comments
 (0)