@Override public boolean check(ValidationContext ctx, SBaseWithDerivedUnit sb) { UnitDefinition ud = ValidationTools.getDerivedUnitDefinition(ctx, sb); return ud != null && ud.getUnitCount() > 0 && !ud.getUnit(0).isInvalid(); } };
@Override public boolean check(ValidationContext ctx, UnitDefinition ud) { if (ctx.isLevelAndVersionLessThan(2, 4) && ud.getId().equals("volume") && ud.getNumUnits() == 1) { Unit u = ud.getUnit(0); if (u.isMetre()) { return u.getExponent() == 3; } } return true; } };
/** * Creates an HTML string representation of this UnitDefinition. * @param ud * * @return */ public static String toHTML(UnitDefinition ud) { StringBuilder sb = new StringBuilder(); if (ud != null) { for (int i = 0; i < ud.getUnitCount(); i++) { Unit unit = ud.getUnit(i); if (i > 0) { sb.append(' '); sb.append(C_DOT); sb.append(' '); } sb.append(toHTML(unit)); } } return sb.toString(); }
@Override public boolean check(ValidationContext ctx, UnitDefinition ud) { if (ctx.isLevelAndVersionLessThan(2, 4) && ud.getId().equals("volume") && ud.getNumUnits() == 1) { Unit u = ud.getUnit(0); if (u.isLitre()) { return u.getExponent() == 1; } } return true; } };
/** * Creates an HTML string representation of this UnitDefinition. * @param ud * * @return */ public static String toHTML(UnitDefinition ud) { StringBuilder sb = new StringBuilder(); if (ud != null) { for (int i = 0; i < ud.getUnitCount(); i++) { Unit unit = ud.getUnit(i); if (i > 0) { sb.append(' '); sb.append(C_DOT); sb.append(' '); } sb.append(toHTML(unit)); } } return sb.toString(); }
@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(); } }
@Override public boolean check(ValidationContext ctx, Parameter p) { UnitDefinition def = p.getUnitsInstance(); if (def != null && def.isSetListOfUnits() && def.getUnitCount() == 1) { // Celsius not allowed return !def.getUnit(0).isCelsius(); } return true; } };
/** * Checks whether the given {@link UnitDefinition} and the * {@link UnitDefinition} or {@link Unit} represented by the given * {@link String} are equivalent. * * @param ud a {@link UnitDefinition} * @param units a String representing either a {@link UnitDefinition} id or a unit {@link Kind}. * @return {@code true} if both units are equivalent * @see #areEquivalent(UnitDefinition, UnitDefinition) * @see Unit#areEquivalent(Unit, Unit) */ public static boolean areEquivalent(UnitDefinition ud, String units) { UnitDefinition ud2 = ud.getModel().getUnitDefinition(units); if (ud2 != null) { return areEquivalent(ud, ud2); } else if (ud.isUnitKind() && Unit.isUnitKind(units, ud.getLevel(), ud.getVersion())) { return Unit.areEquivalent(ud.getUnit(0), units); } return false; }
/** * Checks if this UnitDefinition is invalid. * * @return {@code true} if this UnitDefinition is invalid. */ public boolean isInvalid() { UnitDefinition ud = clone().simplify(); if (ud.getUnitCount() == 1) { return ud.getUnit(0).isInvalid(); } return false; }
/** * Tests whether this {@link UnitDefinition} contains * exactly one {@link Unit} that itself represents a {@link Kind}, i.e., * multiplier = 1, exponent = 1, scale = 1. Note that this method requires * the level and version attributes of this {@link UnitDefinition} to be * set. * * @return {@code true} if this {@link UnitDefinition} contains * exactly one {@link Unit} that itself represents a simple {@link Kind}. */ public boolean isUnitKind() { if (getUnitCount() == 1) { return Unit.isUnitKind(getUnit(0).getKind(), getLevel(), getVersion()); } return false; }
/** * Tests if a given unit definition is a variant * of the predefined unit identifier 'dimensionless'. * * @return {@code true} if this UnitDefinition is a variant of the predefined * unit * dimensionless; false otherwise. */ public boolean isVariantOfDimensionless() { if (isSetListOfUnits()) { UnitDefinition ud = clone(); if (isInvalidSBMLAllowed()) { ud.putUserObject(JSBML.ALLOW_INVALID_SBML, Boolean.TRUE); } ud.convertToSIAndSubstanceUnits(); // if after the simplify() call no more units exists, it's dimensionless if (ud.getUnitCount() == 0) { return true; } else if (ud.getUnitCount() == 1) { return ud.getUnit(0).isDimensionless(); } return false; } // If no listOfUnits is set, it's technically dimensionless return true; }
/** * Throws an {@link IllegalArgumentException} if the given units do not * represent a dimensionless or invalid unit. * * @param units */ protected void checkForDimensionlessOrInvalidUnits(UnitDefinition units) { units.simplify(); String illegal = null; if (units.getUnitCount() == 1) { Kind kind = units.getUnit(0).getKind(); if ((kind != Kind.DIMENSIONLESS) && (kind != Kind.ITEM) && (kind != Kind.RADIAN) && (kind != Kind.STERADIAN) && (kind != Kind.INVALID)) { illegal = kind.toString(); } } else { illegal = units.toString(); } if (illegal != null && (!allowInvalidModel) ) { throw new IllegalArgumentException( new UnitException(MessageFormat.format( "An invalid or dimensionless unit is required but given is {0}.", illegal))); } }
@Override public boolean check(ValidationContext ctx, UnitDefinition ud) { if (ud.getId().equals("length")) { if (ctx.isLevelAndVersionLesserEqualThan(2, 1)) { // Meter not allowed on L2V1 if (ctx.isLevelAndVersionEqualTo(2, 1) && ud.getUnitCount() > 0 && ud.getUnit(0).getKind() == Kind.METER) { return false; } return ud.isVariantOfLength(); } else { return ud.isVariantOfLength() || ud.isVariantOfDimensionless(); } } return true; } };
private boolean isVariantOfArea(ValidationContext ctx, UnitDefinition ud) { // TODO - to really use the level and version of the context, we need to clone the UnitDefinition // and change the level and version of the UnitDefinition and Unit instances so that all the methods // on UnitDefinition or Unit use this level and version as well. if (ctx.getLevel() == 1) { return ud.isVariantOfArea(); } ud = ud.clone().simplify(); if (ud.getUnitCount() == 1) { Unit u = ud.getUnit(0); return u.getKind() == Kind.METRE && u.getExponent() == 2; } return false; } };
/** * Throws an {@link IllegalArgumentException} if the given units do not * represent a dimensionless or invalid unit. * * @param units */ private void checkForDimensionlessOrInvalidUnits(UnitDefinition units) { units.simplify(); String illegal = null; if (units.getUnitCount() == 1) { Kind kind = units.getUnit(0).getKind(); if ((kind != Kind.DIMENSIONLESS) && (kind != Kind.ITEM) && (kind != Kind.RADIAN) && (kind != Kind.STERADIAN) && (kind != Kind.INVALID)) { illegal = kind.toString(); } } else { illegal = units.toString(); } if (illegal != null) { throw new IllegalArgumentException( new UnitException(MessageFormat.format( "An invalid or dimensionless unit is required but given is {0}.", illegal))); } }
/** * Tests if a given unit definition is a variant * of the predefined unit identifier 'substance per area'. * * @return {@code true} if this UnitDefinition is a variant of the predefined * unit 'substance per area'. */ public boolean isVariantOfSubstancePerArea() { if (isSetListOfUnits()) { UnitDefinition ud = clone(); if (isInvalidSBMLAllowed()) { ud.putUserObject(JSBML.ALLOW_INVALID_SBML, Boolean.TRUE); } ud.convertToSIAndSubstanceUnits(); if (ud.getUnitCount() == 2) { if (ud.getUnit(0).isVariantOfSubstance()) { Unit two = ud.getUnit(1).clone(); two.setExponent(-two.getExponent()); return two.isVariantOfArea(); } else if (ud.getUnit(1).isVariantOfSubstance()) { Unit one = ud.getUnit(0).clone(); one.setExponent(-one.getExponent()); return one.isVariantOfArea(); } } } return false; }
@Override @SuppressWarnings("deprecation") public String getDerivedUnits() { UnitDefinition ud = getDerivedUnitDefinition(); Model m = getModel(); if (m != null) { if (m.getUnitDefinition(ud.getId()) != null) { return ud.getId(); } } if (ud.getUnitCount() == 1) { Unit u = ud.getUnit(0); if ((u.getOffset() == 0) && (u.getMultiplier() == 1) && (u.getScale() == 0) && (u.getExponent() == 1)) { return u.getKind().toString().toLowerCase(); } } return null; }
@Override public boolean check(ValidationContext ctx, UnitDefinition ud) { if (ud.getId().equals("volume")) { if (ctx.isLevelAndVersionLessThan(2, 4)) { if (ud.getUnitCount() == 1) { Unit u = ud.getUnit(0); if (ctx.getLevel() == 1) { return u.isLitre(); } else if (ctx.isLevelAndVersionEqualTo(2, 1)) { return u.getKind() == Kind.LITRE || (u.getKind() == Kind.METRE && u.getExponent() == 3); } else { return u.getKind() == Kind.LITRE || (u.getKind() == Kind.METRE && u.getExponent() == 3) || ud.isVariantOfDimensionless(); } } else { return ud.isVariantOfVolume(); } } else { return ud.isVariantOfVolume() || (ud.getNumUnits() == 1 && ud.getUnit(0).isDimensionless()); } } return true; } };