| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| GConstraint |
|
| 0.0;0 |
| 1 | /* | |
| 2 | This file is part of Combean. | |
| 3 | ||
| 4 | Combean is free software; you can redistribute it and/or modify | |
| 5 | it under the terms of the GNU General Public License as published by | |
| 6 | the Free Software Foundation; either version 2 of the License, or | |
| 7 | (at your option) any later version. | |
| 8 | ||
| 9 | Combean is distributed in the hope that it will be useful, | |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 | GNU General Public License for more details. | |
| 13 | ||
| 14 | You should have received a copy of the GNU General Public License | |
| 15 | along with Foobar; if not, write to the Free Software | |
| 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
| 17 | */ | |
| 18 | package net.sourceforge.combean.mathprog.grooml; | |
| 19 | ||
| 20 | import net.sourceforge.combean.except.GModelException; | |
| 21 | import net.sourceforge.combean.interfaces.mathprog.lp.LPConstraint; | |
| 22 | import net.sourceforge.combean.mathprog.lp.model.statics.LPModelStringifier; | |
| 23 | ||
| 24 | /** | |
| 25 | * The rhs of a row in an LP. | |
| 26 | * | |
| 27 | * Simple data holder that implements LPConstraint. Some parsing | |
| 28 | * functionality for the construction of constraint objects. | |
| 29 | */ | |
| 30 | class GConstraint implements LPConstraint { | |
| 31 | ||
| 32 | private String name = ""; | |
| 33 | private byte relation = LPConstraint.REL_UNDEFINED; | |
| 34 | private double rhs = 0.0; | |
| 35 | ||
| 36 | private static Map relSyms = [ | |
| 37 | '=':LPConstraint.REL_EQUAL, | |
| 38 | '>=':LPConstraint.REL_GREATER, | |
| 39 | '<=':LPConstraint.REL_LESS, | |
| 40 | ] | |
| 41 | ||
| 42 | /** | |
| 43 | * Construct a constraint. | |
| 44 | * | |
| 45 | * @params name the name of the constraint | |
| 46 | * @params relation the relation symbol of the constraint | |
| 47 | * @params rhs the value on the rhs of the constraint | |
| 48 | */ | |
| 49 | public GConstraint(String name, byte relation, double rhs) { | |
| 50 | 340 | this.name = name; |
| 51 | 340 | this.relation = relation; |
| 52 | 340 | this.rhs = rhs; |
| 53 | } | |
| 54 | ||
| 55 | /** | |
| 56 | * Construct a constraint from a String definition. | |
| 57 | * | |
| 58 | * @params name the name of the constraint | |
| 59 | * @params constrDef the definition of the constraint with the format | |
| 60 | * "<relation symbol> <value of rhs>", e.g. "<= 1.0". The relation symbols | |
| 61 | * =, >= and <= are supported. | |
| 62 | */ | |
| 63 | public GConstraint(String name, String constrDef) { | |
| 64 | 790 | def match = (constrDef =~ /^\s*([<=>]+)\s*(-?\d*\.?\d+)\s*$/); |
| 65 | 790 | if (match.count != 1) { |
| 66 | 5 | throw new GModelException("Illegal constraint definition '$constrDef'"); |
| 67 | } | |
| 68 | 785 | String relSym = match[0][1]; |
| 69 | 785 | if (!relSyms.containsKey(relSym)) { |
| 70 | 5 | throw new GModelException("Illegal relation symbol '$relSym' "+ |
| 71 | "in constraint definition '$constrDef'"); | |
| 72 | } | |
| 73 | 780 | double rhs = Double.parseDouble(match[0][2]); |
| 74 | 780 | this.name = name; |
| 75 | 780 | this.relation = relSyms[relSym]; |
| 76 | 780 | this.rhs = rhs; |
| 77 | } | |
| 78 | ||
| 79 | /** | |
| 80 | * @returns the relation as relation symbol (e.g. '<=') | |
| 81 | */ | |
| 82 | public String getRelSym() { | |
| 83 | 155 | return LPModelStringifier.getRelationSymbol(relation); |
| 84 | } | |
| 85 | ||
| 86 | public String getName() { | |
| 87 | 145 | return this.name; |
| 88 | } | |
| 89 | ||
| 90 | public void setName(String name) { | |
| 91 | 335 | this.name = name; |
| 92 | } | |
| 93 | ||
| 94 | public byte getRelation() { | |
| 95 | 315 | return this.relation; |
| 96 | } | |
| 97 | ||
| 98 | public double getRhs() { | |
| 99 | 315 | return this.rhs; |
| 100 | } | |
| 101 | ||
| 102 | public String toString() { | |
| 103 | 40 | return "$relSym $rhs '$name'"; |
| 104 | } | |
| 105 | ||
| 106 | public String toSummaryString() { | |
| 107 | 115 | return "$relSym $rhs"; |
| 108 | } | |
| 109 | } |