/** * Creates a Unit instance from a kind, level and version. If the level is * set and is superior or equal to 3 the multiplier, scale, offset and * exponent are {@code null}. * * @param kind * @param level * @param version */ public Unit(Kind kind, int level, int version) { super(level, version); if ((kind == null) || !isUnitKind(kind, level, version) && (kind != Kind.INVALID)) { throw new IllegalArgumentException(MessageFormat.format( "Unit kind {0} is undefined for SBML Level {1,number,integer} Version {2,number,integer}.", kind != null ? kind.toString() : "null", level, version)); } initDefaults(); setKind(kind); }
return false; return isUnitKind(kind, level, version);
@Override public boolean check(ValidationContext ctx, ASTNode node) { if (node.isSetUnits()) { String units = node.getUnits(); if (units != null && !units.isEmpty()) { // Checks if the unit is predefined or defined in the model if (!(Unit.isUnitKind(units, ctx.getLevel(), ctx.getVersion())) && node.getUnitsInstance() == null) { return false; } } } return true; }
@Override @Deprecated public boolean isSetUnitsInstance() { if (isSetSubstanceUnitsInstance() && isSetTimeUnitsInstance()) { return true; } if (unitsID != null) { if (Unit.isUnitKind(unitsID, getLevel(), getVersion())) { return true; } Model model = getModel(); return model == null ? false : model.getUnitDefinition(unitsID) != null; } return false; }
@Override public boolean isSetUnitsInstance() { if (isSetUnits()) { if (Unit.isUnitKind(unitsID, getLevel(), getVersion())) { return true; } Model model = getModel(); return model == null ? false : model .getUnitDefinition(unitsID) != null; } 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; }
/** * Checks that the given units is a valid units in the {@link Model} (validation rule 10313). * * @param ctx the validation context * @param m the model * @param units the units value to check * @return {@code false} if the given units is not a valid units in the {@link Model}, {@code true} otherwise. */ public static boolean checkUnit(ValidationContext ctx, Model m, String units) { boolean definedInModel = false; if (m != null) { definedInModel = m.getUnitDefinition(units) != null; } if (! (definedInModel || Unit.isUnitKind(units, ctx.getLevel(), ctx.getVersion()) || Unit.isPredefined(units, ctx.getLevel()))) { return false; } return definedInModel; }
@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; } };
@Override public boolean check(ValidationContext ctx, UnitDefinition ud) { // it is allowed to have substance, area, time, ... as id for UnitDefinition - && !Unit.isPredefined(ud) return ValidationTools.isId(ud.getId(), ctx.getLevel(), ctx.getVersion()) && !Unit.isUnitKind(ud.getId(), ctx.getLevel(), ctx.getVersion()); } };
@Override public UnitDefinition getUnitsInstance() { String unitsId = unitsID; if (isSetUnits()) { int level = getLevel(), version = getVersion(); if (Unit.isUnitKind(unitsId, level, version)) { UnitDefinition ud = new UnitDefinition(unitsId + UnitDefinition.BASE_UNIT_SUFFIX, level, version); ud.addUnit(Unit.Kind.valueOf(unitsId.toUpperCase())); return ud; } } else { unitsId = getPredefinedUnitID(); } Model model = getModel(); return model == null ? null : model.getUnitDefinition(unitsId); }
return null; } else if (!(isPredefined(id, level) || (Unit.isUnitKind(id, level, version)))) { logger.warn(MessageFormat.format( "No such predefined unit ''{0}'' in SBML Level {1,number,integer}.", id,
/** * 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; }
@Override public boolean check(ValidationContext ctx, Parameter p) { if (p.isSetUnits()) { String units = p.getUnits(); Model m = p.getModel(); if (! (Unit.isUnitKind(units, ctx.getLevel(), ctx.getVersion()) || Unit.isPredefined(units, ctx.getLevel()) || (m != null && m.getUnitDefinition(units) != null))) { ValidationConstraint.logError(ctx, CORE_20701, p, p.getId(), p.getUnits()); return false; } } return true; } };
@Override @Deprecated public UnitDefinition getUnitsInstance() { if (unitsID != null) { if (Unit.isUnitKind(unitsID, getLevel(), getVersion())) { UnitDefinition ud = new UnitDefinition(unitsID, getLevel(), getVersion()); ud.addUnit(Unit.Kind.valueOf(unitsID.toUpperCase())); return ud; } Model model = getModel(); return model == null ? null : model.getUnitDefinition(unitsID); } UnitDefinition substancePerTimeUnits = getSubstanceUnitsInstance(); UnitDefinition timeUnits = getTimeUnitsInstance(); substancePerTimeUnits = (substancePerTimeUnits == null) ? new UnitDefinition( getLevel(), getVersion()) : substancePerTimeUnits.clone(); if (timeUnits != null) { substancePerTimeUnits.divideBy(timeUnits); substancePerTimeUnits.setId(getSubstanceUnits() + "_per_" + getTimeUnits()); } else if (!substancePerTimeUnits.isSetId()) { substancePerTimeUnits.setId(getSubstanceUnits()); } return substancePerTimeUnits; }
@Override public boolean check(ValidationContext ctx, SBaseWithUnit sb) { if (sb.isSetUnits() && (sb instanceof Compartment || sb instanceof Species || sb instanceof Parameter || sb instanceof LocalParameter)) { String unit = sb.getUnits(); Model m = sb.getModel(); boolean definedInModel = false; if (m != null) { definedInModel = m.getUnitDefinition(unit) != null; } if (! (definedInModel || Unit.isUnitKind(unit, ctx.getLevel(), ctx.getVersion()) || Unit.isPredefined(unit, ctx.getLevel()))) { if (ctx.getLevel() > 2 || (ctx.getLevel() == 2 && ctx.getVersion() >= 5)) { ValidationConstraint.logError(ctx, CORE_10313, sb, sb.getUnits(), sb.getElementName(), sb.getId()); } else { ValidationConstraint.logError(ctx, CORE_99303, sb, sb.getUnits(), sb.getElementName(), sb.getId()); } return false; } } return true; } };