-
-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
format() and parse() in SimpleUnitFormat behave differently upon label() #124
Comments
CommonFormatDemo shows your example plus a comparison with
The advantage over Adding
You notice, not only the |
Btw, |
Thx for the insight. Indeed UCUM/EBNF do improve the refactoring+formatting, but I remain quite a bit confused by the lack of easy-ish way to get "psi^2" or "psi²" displayed (other than by force-labeling it unit as such), while:
Am I missing a fundamental reasoning here? Should I be writing my own format for this (arguably straightforward) use case? The reason I focused on this "toy" example is because I work in a field where custom units are frequent (some of them are purely for internal use and would not benefit UCUM in any way). I thus need to evaluate how much freedom (or lack thereof) the current implementations offer to downstream code in order to simplify unit parsing/manipulation by end-users. |
Thx for the update. If you say, Parsing works after labelling |
I added a few test lines to |
Hmm, that's an odd one. On my side the parsing you mentioned works, e.g. running just these few lines UnitFormat ebnf = EBNFUnitFormat.getInstance();
System.out.println(ebnf.format(ebnf.parse("psi^2")));
System.out.println(ebnf.format(ebnf.parse("psi²"))); works (and in both cases produces |
I did not try it in the UCUM demo. Since all relevant UCUM units are added to |
It does in Most importantly (I commented out the line for later testing) |
My apologies for the confusion, but I have the (surely wrong) impression this is getting slightly off-track from the original issue I reported. Here is another try at explaining what (I think) is wrong: Suppose I use SimpleUnitFormat (for now) to label a given unit. Then:
To me that's just a consistency issue, regardless of the formatter being used (but more on that in a bit). This new toy example exemplifies this behaviour a bit better than my original code: public static void main(String... args) {
UnitFormat formatter = SimpleUnitFormat.getInstance();
System.out.println("== Use case 1: playing with base units ==");
AlternateUnit<?> aCd = new AlternateUnit<>(Units.CANDELA, "altCD");
System.out.println("Candela: " + formatter.format(Units.CANDELA));
System.out.println("Candela times 2: " + formatter.format(Units.CANDELA.multiply(2)));
System.out.println("Square Candela: " + formatter.format(Units.CANDELA.pow(2)));
System.out.println("Alt. Candela: " + formatter.format(aCd));
System.out.println("Square alt. Candela: " + formatter.format(aCd.pow(2)));
System.out.println("=> The Candela shall now be known as \"CD\"");
formatter.label(Units.CANDELA, "CD");
System.out.println("Candela: " + formatter.format(Units.CANDELA));
System.out.println("Candela times 2: " + formatter.format(Units.CANDELA.multiply(2)));
System.out.println("Square Candela: " + formatter.format(Units.CANDELA.pow(2)));
System.out.println("Alt. Candela: " + formatter.format(aCd));
System.out.println("Square alt. Candela: " + formatter.format(aCd.pow(2)));
System.out.println();
System.out.println("== Use case 2: playing with product units ==");
ProductUnit<?> cdK = new ProductUnit<>(Units.CANDELA.multiply(Units.KELVIN));
AlternateUnit<?> aCdK = new AlternateUnit<>(cdK, "altCDK");
System.out.println("Candela-Kelvin: " + formatter.format(cdK));
System.out.println("Candela-Kelvin times 2: " + formatter.format(cdK.multiply(2)));
System.out.println("Square Candela-Kelvin: " + formatter.format(cdK.pow(2)));
System.out.println("Alt. Candela-Kelvin: " + formatter.format(aCdK));
System.out.println("Square alt. Candela-Kelvin: " + formatter.format(aCdK.pow(2)));
System.out.println("=> The Candela-Kelvin shall now be known as \"CDK\"");
formatter.label(cdK, "CDK");
System.out.println("Candela-Kelvin: " + formatter.format(cdK));
System.out.println("Candela-Kelvin times 2: " + formatter.format(cdK.multiply(2)));
System.out.println("Square Candela-Kelvin: " + formatter.format(cdK.pow(2)));
System.out.println("Alt. Candela-Kelvin: " + formatter.format(aCdK));
System.out.println("Square alt. Candela-Kelvin: " + formatter.format(aCdK.pow(2)));
} This produces the following:
As you may notice, the only issue lies with the product unit formatting, which is inconsistent. Now interestingly, if I reproduce the same experiment with
Here the labels are actually never taken into account (neither for product nor alternate units). |
Thanks for the clarification. It should help to narrow it down to e.g. to |
I think I found at least 2 ways to fix the formatting issue. Basically, what happens is new ProductUnit(new Element[]{ new Element(someProductUnit, exponent, 1) }); Since that's all private/inaccessible, I found two independent ways to call this line from client code:
Both options solve the formatting issue quite nicely (but I have no clue on the potential side-effects). |
Yes please propose a PR. While larger portions of code (several hundred or thousand LOC) advise those who change that much to join the JCP at least as Associate Member (contributor) smaller fixes are OK. Of course you're more than welcome to join the JCP if you want. Thanks. |
@adufour Hi, just wanted to ask, if you may have time for a PR yourself, or if someone else might better work on this based on the evidence gathered here? |
I was working on the PR (for the mislabelling part, that is), but then realised that the fix I found actually broke the formatting "before" labelling. In short:
Investigating this as we speak |
Great, thanks a lot for the update. While this has P1 at the moment, and ideally |
…imple formatting)
#124: fix handling of powered units (creation and simple formatting)
Fixed by #129 |
Consider the following code
which outputs
Regardless of the usefulness of a square psi (just a random example), wouldn't it make sense to format it as
psi^2
instead of the fully developed form once the label as been defined in the formatter? It seems the simple formatter(s) dive(s) straight into the ProductUnit's components without considering the (basic) case of a powered "labeled" unit (while powered "base" units render fine as you can see). Bug or feature?The text was updated successfully, but these errors were encountered: