/** * 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; }
UnitDefinition ud[] = new UnitDefinition[getUnitCount()]; Set<TreeNodeChangeListener> listeners = new HashSet<TreeNodeChangeListener>(getListOfTreeNodeChangeListeners()); removeAllTreeNodeChangeListeners(); Unit unit = removeUnit(i); UnitDefinition tempUD = new UnitDefinition(getLevel(), getVersion()); tempUD.addUnit(unit); for (Unit unit : u.getListOfUnits()) { getListOfUnits().add(unit); simplify(); addAllChangeListeners(listeners);
/** * Returns a simplified version of the given {@link UnitDefinition}. * * @param ud a unit definition * @return a simplified version of the given {@link UnitDefinition}. In order * to make sure that the original {@link UnitDefinition} is not * changed, it is cloned before the simplification. */ public static UnitDefinition simplify(UnitDefinition ud) { return ud.clone().simplify(); }
/** * * @param unit */ public void setUnits(UnitDefinition unit) { unitDef = unit; if (unit != null) { /* * This is important to avoid unnecessary calls to change listeners or to * avoid that the actual model is modified due to this operation. */ if ((unit.getParent() != null) || (unit.getTreeNodeChangeListenerCount() > 0)) { unitDef = unitDef.clone(); } unitDef = unitDef.simplify(); } }
/** * 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; }
boolean isVariantOfArea = false; if (isSetListOfUnits()) { UnitDefinition ud = clone(); if (isInvalidSBMLAllowed()) { ud.putUserObject(JSBML.ALLOW_INVALID_SBML, Boolean.TRUE); ud.convertToSIUnits(); if (ud.getUnitCount() == 1) { Unit unit = ud.getUnit(0); isVariantOfArea = unit.isVariantOfArea(); if (getLevelAndVersion().compareTo(3, 1) >= 0 && getUnitCount() > 1 && !isVariantOfArea) { ud = clone(); if (isInvalidSBMLAllowed()) { ud.putUserObject(JSBML.ALLOW_INVALID_SBML, Boolean.TRUE); ud.convertToSIUnits(false); // so that dimensionless is not lost if combined with metre. for (Unit unit : ud.getListOfUnits()) {
UnitDefinition ud[] = new UnitDefinition[getUnitCount()]; Set<TreeNodeChangeListener> listeners = new HashSet<TreeNodeChangeListener>(getListOfTreeNodeChangeListeners()); removeAllTreeNodeChangeListeners(); for (int i = ud.length - 1; i >= 0; i--) { ud[i] = Unit.convertToSI(removeUnit(i)); for (Unit unit : u.getListOfUnits()) { getListOfUnits().add(unit); if (isInvalidSBMLAllowed()) { unit.putUserObject(JSBML.ALLOW_INVALID_SBML, Boolean.TRUE); simplify(); addAllChangeListeners(listeners);
@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; }
logger.debug("10501 - unit = " + ud + " " + UnitDefinition.printUnits(ud)); if (ud == null || ud.getNumChildren() == 0 || ud.getUnit(0).isInvalid()) if (ud != null && ud.isInvalid()) { logger.debug("10501 - unit n = " + ud2 + " " + UnitDefinition.printUnits(ud2)); if (ud2 == null || !UnitDefinition.areEquivalent(ud, ud2)) { if (ud2 != null && ud2.isInvalid()) { if (ud == null || ud.simplify().isInvalid()) { if (!ud.isVariantOfTime()) { if ((ud == null || def == null || def.isInvalid()) || ud.isInvalid()) if (!UnitDefinition.areEquivalent(ud, def)) { return false; if (def == null || !def.isVariantOfDimensionless()) { return false; if (value.getUnits().isInvalid()) { return false; if (!value.getUnits().isVariantOfDimensionless()) {
if (UnitDefinition.areCompatible(left.getUnits(), right.getUnits())) { if (!left.getUnits().isInvalid() || !right.getUnits().isInvalid()) { left.getUnits().simplify(); right.getUnits().simplify(); int mean, scale1, scale2; double v1 = left.toNumber().doubleValue(), v2 = right.toNumber().doubleValue(); for (int i = 0; i < left.getUnits().getUnitCount(); i++) { Unit u1 = left.getUnits().getUnit(i); Unit u2 = right.getUnits().getUnit(i); if (((u1.getMultiplier() != u2.getMultiplier()) && (u1.getScale() != u2.getScale()) && (u1.getExponent() != u2.getExponent())) String.format( "Cannot combine the units %s and %s in addition, subtraction, comparison or any equivalent operation.", UnitDefinition.printUnits(left.getUnits(), true), UnitDefinition.printUnits(right.getUnits(), true)));
/** * * @param unitsMap * @param model the original model from which a sub-model is to be created. * @param elementUnits */ private static void processUnitsMap(Map<String, UnitDefinition> unitsMap, Model model, String elementUnits) { Logger debugLogger = Logger.getLogger(SubModel.class); debugLogger.debug("processUnitsMap called with " + elementUnits); UnitDefinition unit = model.getUnitDefinition(elementUnits); debugLogger.debug("processUnitsMap: unit = " + elementUnits); if (unit != null && !unitsMap.containsKey(unit.getId())) { unitsMap.put(unit.getId(), unit); } }
@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; } };
/** * 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; }
/** * Creates a dimensionless unit definition object encapsulated in an * {@link ASTNode2Value}. * @param type The type of the dimensionless value, e.g., Boolean.class etc. * * @return */ private <T> ASTNode2Value<T> dimensionless(Class<T> type) { UnitDefinition ud = new UnitDefinition(level, version); ud.addUnit(Unit.Kind.DIMENSIONLESS); return new ASTNode2Value<T>(ud, this); }
case 1: ud = UnitDefinition.substance(getLevel(), getVersion()); listOfPredefinedUnitDefinitions.add(ud); ud = UnitDefinition.time(getLevel(), getVersion()); listOfPredefinedUnitDefinitions.add(ud); ud = UnitDefinition.volume(getLevel(), getVersion()); listOfPredefinedUnitDefinitions.add(ud); case 2: ud = UnitDefinition.substance(getLevel(), getVersion()); listOfPredefinedUnitDefinitions.add(ud); ud = UnitDefinition.volume(getLevel(), getVersion()); listOfPredefinedUnitDefinitions.add(ud); ud = UnitDefinition.area(getLevel(), getVersion()); listOfPredefinedUnitDefinitions.add(ud); ud = UnitDefinition.length(getLevel(), getVersion()); listOfPredefinedUnitDefinitions.add(ud); ud = UnitDefinition.time(getLevel(), getVersion()); listOfPredefinedUnitDefinitions.add(ud);
if (assignmentDerivedUnit != null && assignmentDerivedUnit.isInvalid()) { // TODO - apply this change to other tests assignmentDerivedUnit = assignmentDerivedUnit.clone().convertToSIUnits(); varDerivedUnit = varDerivedUnit.clone().convertToSIUnits(); logger.debug("haveEquivalentUnits SI - " + assignment.getClass().getSimpleName() + " unit = " + UnitDefinition.printUnits(assignmentDerivedUnit)); logger.debug("haveEquivalentUnits SI - " + var.getClass().getSimpleName() + " unit = " + UnitDefinition.printUnits(varDerivedUnit));
UnitDefinition klDerivedUnit = ValidationTools.getDerivedUnitDefinition(ctx, kl).clone().convertToSIUnits(); expectedUnit = m.getSubstanceUnitsInstance().clone().divideBy(m.getTimeUnitsInstance()); } else if (m.isSetTimeUnits() && m.isSetExtentUnits()) { expectedUnit = m.getExtentUnitsInstance().clone().divideBy(m.getTimeUnitsInstance()); if (klDerivedUnit != null && klDerivedUnit.isInvalid()) { if (klDerivedUnit != null && expectedUnit != null && !klDerivedUnit.isInvalid()) { klDerivedUnit = klDerivedUnit.clone().convertToSIUnits(); expectedUnit.convertToSIUnits();
if (variableUnits != null && !variableUnits.isInvalid()) { Model m = variable.getModel(); variableUnits = variableUnits.clone().divideBy(m.getTimeUnitsInstance()); if (!variableUnits.isInvalid()) { UnitDefinition ruleUnits = ValidationTools.getDerivedUnitDefinition(ctx, rateRule); if (ruleUnits != null && !ruleUnits.isInvalid()) { check = UnitDefinition.areEquivalent(variableUnits, ruleUnits);
@Override public <T> ASTNode2Value<Double> getConstantAvogadro(String name) { // TODO: If there is a different value in a later SBML specification, this must be checked here. ASTNode2Value<Double> value = new ASTNode2Value<Double>(Maths.AVOGADRO_L3V1, this); UnitDefinition perMole = new UnitDefinition(level, version); perMole.setLevel(level); perMole.setId("per_mole"); perMole.addUnit(new Unit(1d, 0, Kind.MOLE, -1d, level, version)); value.setUnits(perMole); return value; }