/** * Adds a {@link Unit} to this {@link UnitDefinition}. * * @param u the unit to add. */ public void addUnit(Unit u) { getListOfUnits().add(u); }
/** * Returns a specific Unit instance belonging to this UnitDefinition. * * @param i * an integer, the index of the Unit to be returned. * @return the ith Unit of this UnitDefinition */ public Unit getUnit(int i) { return getListOfUnits().get(i); }
/** * Removes all {@link Unit} elements from the list of Units in this object. */ public void clear() { getListOfUnits().clear(); }
@Override public TreeNode getChildAt(int index) { if (index < 0) { throw new IndexOutOfBoundsException(MessageFormat.format( resourceBundle.getString("IndexSurpassesBoundsException"), index, 0)); } int count = super.getChildCount(), pos = 0; if (index < count) { return super.getChildAt(index); } else { index -= count; } if (listOfUnits != null) { if (index == pos) { return getListOfUnits(); } pos++; } throw new IndexOutOfBoundsException(MessageFormat.format( resourceBundle.getString("IndexExceedsBoundsException"), index, +Math.min(pos, 0))); }
@Override public boolean check(ValidationContext ctx, UnitDefinition ud) { boolean success = true; if (ud.isSetListOfUnits()) { for (Unit u : ud.getListOfUnits()) { success = success && u.getOffset() == 0; } } return success; }
/** * Creates an {@link UnitDefinition} instance from a given UnitDefinition. * * @param unitDefinition the {@link UnitDefinition} to clone. */ public UnitDefinition(UnitDefinition unitDefinition) { super(unitDefinition); if (unitDefinition.isSetListOfUnits()) { setListOfUnits(unitDefinition.getListOfUnits().clone()); } }
/** * Ensures that level and version combination of a unit are the same then * these that are defined here. * * @param unit */ private void setLevelAndVersion(UnitDefinition unit) { if ((unit.getLevel() != level) || (unit.getVersion() != version)) { unit.setLevel(level); unit.setVersion(version); unit.getListOfUnits().setLevel(level); unit.getListOfUnits().setVersion(version); for (Unit u : unit.getListOfUnits()) { u.setLevel(level); u.setVersion(version); } } }
/** * Ensures that level and version combination of a unit are the same then * these that are defined here. * * @param unit */ protected void setLevelAndVersion(UnitDefinition unit) { if ((unit.getLevel() != level) || (unit.getVersion() != version)) { unit.setLevel(level); unit.setVersion(version); unit.getListOfUnits().setLevel(level); unit.getListOfUnits().setVersion(version); for (Unit u : unit.getListOfUnits()) { u.setLevel(level); u.setVersion(version); } } }
@Override public boolean check(ValidationContext ctx, UnitDefinition ud) { if (ud.isSetListOfUnits() || ud.isListOfUnitsEmpty()) { return new UnknownElementValidationFunction<TreeNodeWithChangeSupport>().check(ctx, ud.getListOfUnits()); } return true; }
@Override public boolean check(ValidationContext ctx, UnitDefinition ud) { if (ud.isSetListOfUnits() || ud.isListOfUnitsEmpty()) { return new UnknownAttributeValidationFunction<TreeNodeWithChangeSupport>().check(ctx, ud.getListOfUnits()); } return true; } };
for (Unit unit : u.getListOfUnits()) { getListOfUnits().add(unit);
@Override public boolean check(ValidationContext ctx, UnitDefinition ud) { boolean success = true; if (ud.isSetListOfUnits()) { for (Unit u : ud.getListOfUnits()) { if (!u.isCelsius()) { success = success && Unit.isUnitKind(u.getKind(), ctx.getLevel(), ctx.getVersion()); } } } return success; } };
/** * * @param rootExponent * @param radiant * @return * @throws SBMLException */ private <T> ASTNode2Value<Double> root(double rootExponent, ASTNode2Value radiant) throws SBMLException { UnitDefinition ud = radiant.getUnits().clone(); for (Unit u : ud.getListOfUnits()) { if ((((u.getExponent() / rootExponent) % 1d) != 0d) && !u.isDimensionless() && !u.isInvalid()) { new UnitException(MessageFormat.format( "Cannot perform power or root operation due to incompatibility with a unit exponent. Given are {0,number} as the exponent of the unit and {1,number} as the root exponent for the current computation.", u.getExponent(), rootExponent)); } if (!(u.isDimensionless() || u.isInvalid())) { u.setExponent(u.getExponent() / rootExponent); } } ASTNode2Value<Double> value = new ASTNode2Value<Double>(ud, this); value.setValue(Double.valueOf(Math.pow(radiant.toDouble(), 1d / rootExponent))); return value; }
/** * * @return the UnitDefinition instance which has the substanceUnistID of * this KineticLaw as id. Return null if it doesn't exist. */ @Deprecated public UnitDefinition getSubstanceUnitsInstance() { Model m = getModel(); if ((m != null) && isSetSubstanceUnits()) { return m.getUnitDefinition(substanceUnitsID); } if (unitsID != null) { UnitDefinition def = new UnitDefinition(UnitDefinition.SUBSTANCE, getLevel(), getVersion()); for (Unit unit : getUnitsInstance().getListOfUnits()) { if (unit.isVariantOfSubstance()) { def.addUnit(unit); } } return def; } return null; }
/** * * @param rootExponent * @param radiant * @return * @throws SBMLException */ protected ASTNodeValue root(double rootExponent, ASTNodeValue radiant) throws SBMLException { UnitDefinition ud = radiant.getUnits().clone(); for (Unit u : ud.getListOfUnits()) { if ((((u.getExponent() / rootExponent) % 1d) != 0d) && !u.isDimensionless() && !u.isInvalid()) { new UnitException(MessageFormat.format( "Cannot perform power or root operation due to incompatibility with a unit exponent. Given are {0,number} as the exponent of the unit and {1,number} as the root exponent for the current computation.", u.getExponent(), rootExponent)); // TODO - this exception is never thrown } if (!(u.isDimensionless() || u.isInvalid())) { u.setExponent(u.getExponent() / rootExponent); } } ASTNodeValue value = new ASTNodeValue(ud, this); value.setValue(Double.valueOf(Math.pow(radiant.toDouble(), 1d / rootExponent))); return value; }
/** * * @param rootExponent * @param radiant * @return * @throws SBMLException */ protected ASTNodeValue root(double rootExponent, ASTNodeValue radiant) throws SBMLException { UnitDefinition ud = radiant.getUnits().clone(); for (Unit u : ud.getListOfUnits()) { if ((((u.getExponent() / rootExponent) % 1d) != 0d) && !u.isDimensionless() && !u.isInvalid()) { // TODO - report an error ? new UnitException(MessageFormat.format( "Cannot perform power or root operation due to incompatibility with a unit exponent. Given are {0,number} as the exponent of the unit and {1,number} as the root exponent for the current computation.", u.getExponent(), rootExponent)); } if (!(u.isDimensionless() || u.isInvalid())) { u.setExponent(u.getExponent() / rootExponent); } } ASTNodeValue value = new ASTNodeValue(ud, this); value.setValue(Double.valueOf(Math.pow(radiant.toDouble(), 1d / rootExponent))); return value; }
@Override @Deprecated public void setUnits(UnitDefinition units) { if (units != null) { units = units.simplify(); if (units.isVariantOfSubstance()) { setSubstanceUnits(units.getUnit(0)); } else if (units.isVariantOfTime()) { setTimeUnits(units.getUnit(0)); } else if (units.isVariantOfSubstancePerTime()) { for (Unit unit : units.getListOfUnits()) { if (unit.isVariantOfSubstance()) { setSubstanceUnits(unit); } else { // must be variant of time^-1 Unit u = unit.clone(); u.setExponent(-unit.getExponent()); setTimeUnits(u); } } } else { throw new IllegalArgumentException(MessageFormat.format( ILLEGAL_UNIT_KIND_EXCEPTION_MSG, UnitDefinition .printUnits(units, true))); } } else { unsetUnits(); } }
/** * * @return the UnitDefinition instance which has the timeUnistID of this * KineticLaw as id. Return null if it doesn't exist. */ @Deprecated public UnitDefinition getTimeUnitsInstance() { Model m = getModel(); if ((m != null) && isSetTimeUnits()) { return m.getUnitDefinition(timeUnitsID); } if (unitsID != null) { UnitDefinition def = new UnitDefinition(UnitDefinition.TIME, getLevel(), getVersion()); Unit time; for (Unit unit : getUnitsInstance().getListOfUnits()) { time = unit.clone(); time.setExponent(-unit.getExponent()); if (unit.isVariantOfTime()) { def.addUnit(time); } } return def; } return null; }
for (Unit unit : ud.getListOfUnits()) {
/** * Orders alphabetically the {@link Unit} objects within the * {@link #listOfUnits} of a {@link UnitDefinition}. * * @param ud * the {@link UnitDefinition} object whose {@link Unit}s are to be * reordered. */ public static void reorder(UnitDefinition ud) { if (1 < ud.getUnitCount()) { ListOf<Unit> orig = ud.getListOfUnits(); ListOf<Unit> units = new ListOf<Unit>(ud.getLevel(), ud.getVersion()); units.setSBaseListType(Type.listOfUnits); orig.removeAllTreeNodeChangeListeners(); units.add(orig.remove(orig.size() - 1)); int i, j; for (i = orig.size() - 1; i >= 0; i--) { Unit u = orig.remove(i); j = 0; while ((j < units.size()) && (0 < u.getKind().compareTo(units.get(j).getKind()))) { j++; } units.add(j, u); } ud.setListOfUnits(units); } }