/** * 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(); } }
/** * * @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; }
@Override public ASTNodeValue rem(List<ASTNode> values) { if (values.size() == 2) { ASTNode numerator = values.get(0); ASTNode denominator = values.get(1); UnitDefinition ud = numerator.compile(this).getUnits().clone(); ASTNodeValue value = new ASTNodeValue(ud, this); value.setValue(numerator.compile(this).toDouble() % denominator.compile(this).toDouble()); return value; } return new ASTNodeValue(this); }
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()); klDerivedUnit = klDerivedUnit.clone().convertToSIUnits(); expectedUnit.convertToSIUnits();
@Override public <T> ASTNode2Value<Double> times(List<ASTNode2> values) throws SBMLException { UnitDefinition ud = new UnitDefinition(level, version); UnitDefinition v; double d = 1d; for (ASTNode2 value : values) { ASTNode2Value<?> av = value.compile(this); v = av.getUnits().clone(); setLevelAndVersion(v); ud.multiplyWith(v); d *= av.toDouble(); } ASTNode2Value<Double> value = new ASTNode2Value(ud, this); value.setValue(Double.valueOf(d)); return value; }
@Override public ASTNodeValue times(List<ASTNode> values) throws SBMLException { UnitDefinition ud = new UnitDefinition(level, version); UnitDefinition v; double d = 1d; for (ASTNode value : values) { ASTNodeValue av = value.compile(this); v = av.getUnits().clone(); // System.out.println("UnitsCompiler - times - " + value.getVariable().getElementName() + " " + value.getVariable().getId() + " unit = " + UnitDefinition.printUnits(v)); setLevelAndVersion(v); ud.multiplyWith(v); d *= av.toDouble(); } ASTNodeValue value = new ASTNodeValue(ud, this); value.setValue(Double.valueOf(d)); return value; }
@Override public <T> ASTNode2Value<Double> frac(ASTNode2 numerator, ASTNode2 denominator) throws SBMLException { UnitDefinition ud = numerator.compile(this).getUnits().clone(); UnitDefinition denom = denominator.compile(this).getUnits().clone(); setLevelAndVersion(ud); setLevelAndVersion(denom); ud.divideBy(denom); ASTNode2Value<Double> value = new ASTNode2Value<Double>(ud, this); value.setValue(numerator.compile(this).toDouble() / denominator.compile(this).toDouble()); return value; }
@Override public ASTNodeValue frac(ASTNode numerator, ASTNode denominator) throws SBMLException { UnitDefinition ud = numerator.compile(this).getUnits().clone(); UnitDefinition denom = denominator.compile(this).getUnits().clone(); // System.out.println("UnitsCompiler - divide/frac - numerator unit = " + UnitDefinition.printUnits(ud)); // System.out.println("UnitsCompiler - divide/frac - denominator unit = " + UnitDefinition.printUnits(denom)); setLevelAndVersion(ud); setLevelAndVersion(denom); ud.divideBy(denom); // System.out.println("UnitsCompiler - divide/frac - result unit = " + UnitDefinition.printUnits(ud) + "\n"); ASTNodeValue value = new ASTNodeValue(ud, this); value.setValue(numerator.compile(this).toDouble() / denominator.compile(this).toDouble()); return value; }
/** * * @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; }
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; } };
/** * * @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; }
/** * 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; }
@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; }
UnitDefinition ud = clone();
extendUnits = extendUnits.clone().multiplyWith(cfUnits);
/** * 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; }