Skip to content
This repository has been archived by the owner on May 26, 2021. It is now read-only.

SimpleUnitFormat won't parse what it just formatted #201

Closed
aaime opened this issue Sep 25, 2018 · 10 comments
Closed

SimpleUnitFormat won't parse what it just formatted #201

aaime opened this issue Sep 25, 2018 · 10 comments
Labels

Comments

@aaime
Copy link

aaime commented Sep 25, 2018

The following little program shows a case in which the SimpleUnitFormat does not seem to be able to parse back what it just created:

import javax.measure.Unit;

import tec.uom.se.format.SimpleUnitFormat;

public class RoundtripDemo {

    public static void main(String[] args) {
        String unit = "µmol*m^-2*446.2";
        SimpleUnitFormat format = SimpleUnitFormat.getInstance();
        Unit<?> parsed = format.parse(unit);
        String formatted = format.format(parsed);
        System.out.println("Formatted version: " + formatted);
        Unit<?> parsed2 = format.parse(formatted);
    }
}

The output being:

Formatted version: (1/m²)*446.2·µmol
Exception in thread "main" javax.measure.format.ParserException: Parse Error
	at tec.uom.se.format.SimpleUnitFormat$DefaultFormat.parseProductUnit(SimpleUnitFormat.java:520)
	at tec.uom.se.format.SimpleUnitFormat$DefaultFormat.parseProductUnit(SimpleUnitFormat.java:447)
	at tec.uom.se.format.SimpleUnitFormat.parseObject(SimpleUnitFormat.java:269)
	at tec.uom.se.format.SimpleUnitFormat$DefaultFormat.parse(SimpleUnitFormat.java:787)
	at tec.uom.se.format.SimpleUnitFormat$DefaultFormat.parse(SimpleUnitFormat.java:782)
	at tec.uom.se.format.SimpleUnitFormat$DefaultFormat.parse(SimpleUnitFormat.java:773)
	at tec.uom.se.format.SimpleUnitFormat$FinalDefaultFormat.parse(SimpleUnitFormat.java:803)
	at tec.uom.se.format.SimpleUnitFormat$FinalDefaultFormat.parse(SimpleUnitFormat.java:795)
	at RoundTripper.main(RoundTripper.java:13)

Considering that it might be classpath related, here is what I have:

[INFO] |  |  |  |     \- systems.uom:systems-common-java8:jar:0.7.2:compile
[INFO] |  |  |  |        +- tec.uom:uom-se:jar:1.0.8:compile
[INFO] |  |  |  |        |  +- javax.measure:unit-api:jar:1.0:compile
[INFO] |  |  |  |        |  \- tec.uom.lib:uom-lib-common:jar:1.0.2:compile
[INFO] |  |  |  |        +- si.uom:si-quantity:jar:0.7.1:compile
[INFO] |  |  |  |        \- si.uom:si-units-java8:jar:0.7.1:compile
@aaime
Copy link
Author

aaime commented Sep 25, 2018

By the way, I've tried upgrading the dependencies to:

[INFO] |  |  |  |     \- systems.uom:systems-common-java8:jar:0.9:compile
[INFO] |  |  |  |        +- tec.uom:uom-se:jar:1.0.9:compile
[INFO] |  |  |  |        |  +- javax.measure:unit-api:jar:1.0:compile
[INFO] |  |  |  |        |  \- tec.uom.lib:uom-lib-common:jar:1.0.2:compile
[INFO] |  |  |  |        +- si.uom:si-quantity:jar:0.9:compile
[INFO] |  |  |  |        \- si.uom:si-units-java8:jar:0.9:compile

Result is still the same.

@keilw
Copy link
Member

keilw commented Sep 25, 2018

You should not use something like "µ" with SimpleUnitFormat.Flavor.ASCII. Have to check the rest in more detail but these two are not compatible. Did you try DEFAULT?

@aaime
Copy link
Author

aaime commented Sep 25, 2018

Ah sorry, I actually tried both ASCII and DEFAULT, both fail (the output is slightly different, the substance does not change, the class cannot parse what it just formatted). Let me update the code sample.

@keilw
Copy link
Member

keilw commented Sep 25, 2018

It's a little weird, but currently our focus is on Indriya where a sort of round-trip from SimpleUnitFormat to EBNFUnitFormat works: https://github.com/unitsofmeasurement/uom-demos/blob/master/console/basic/src/main/java/tech/uom/demo/format/RoundTripDemo.java (I found the term "Tripper" a little irritating;-)

The same does not work in uom-se, I cannot say, if and who could backport everything that's been improved with Indriya into uom-se, because it will ultimately replace it?
However, if EBNFUnitFormat is applied all the way throughout the demo, it swallows something because Formatted version: µmol·/m². Parsing a "*/" operation leads to an obvious and correct parsing error.

@aaime Would you be able to use EDR level Indriya? Currently si-units-0.9 as opposed to si-units-java8-0.9 is based on Indriya 1.0, not 2.0-EDR. We may skip 1.2 in the upcoming 1.0 version of the RI-based SI Units, if that is desired. Although 2.0-EDR is a prerelease, it is more mature and advanced.

@aaime
Copy link
Author

aaime commented Sep 26, 2018

D'oh, sorry, that's not what I mean, had to search the term on urbandictionary.com (not a native english speaker). Updated the example code!
I found https://github.com/unitsofmeasurement/indriya... is that going to be a replacement for "systems.uom:systems-common-java8"?

@keilw
Copy link
Member

keilw commented Sep 26, 2018

Not for "systems.uom:systems-common-java8", that (until it may eventually be archived) is supposed to use uom-se, but the modules without the "java8" qualifier, e.g. "systems.uom:systems-common" are backed by Indriya 1.0 already. Indriya is the new Reference Implementation (RI) and when JSR 385 goes live, Indiya 2.0 will be Final. The most recent version is the 2.0-EDR Milestone.

@aaime
Copy link
Author

aaime commented Sep 26, 2018

Do you have a guess about when Indiya will be released? Even just a vague guess, weeks, months, years?

@keilw
Copy link
Member

keilw commented Sep 28, 2018

It is a pretty clear guess thanks to JCP.next and the SI system undergoing many changes early next year. Even if the SI changes were postponed (unlikely but not impossible) we plan to release 2.0 in Q1 of 2019.

@keilw keilw added the analysis label Jan 27, 2019
@keilw keilw added wontfix and removed analysis labels Dec 7, 2019
@keilw keilw closed this as completed Dec 7, 2019
@aaime
Copy link
Author

aaime commented Dec 8, 2019

I see you closing the ticket, does that mean we should migrated to Indriya?
I see a 2.0 released a few months ago: https://github.com/unitsofmeasurement/indriya/releases

@keilw
Copy link
Member

keilw commented Dec 8, 2019

Yes, if you can and are not bound by a legacy application that uses 1.0 and can't be changed, please do.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants