prologowlswi-prologxmlschema

Duration comparison in prolog


I made an OWL ontology with some xsd:duration datatype. Then I converted it into prolog facts using Thea.

I have the following xsd:duration structure PnYnMnDTnHnMnS. How can I write prolog rules to compare two durations, in order to establish which one is greater?

When I retrieve data from my prolog facts, it has this form:

literal(type('http://www.w3.org/2001/XMLSchema#duration', Value))

I managed to extract Value using:

extractDuration(Literal, Result):-arg(1,Literal,Out1),arg(2,Out1,Result).

Which leads to (for example):

'P5D', 'PT10H', or 'P3DT15H32M'

Still have no idea how to compare them.


Solution

  • This representation is not going to be easy. Why not something more conventional?

    Ideally, you would represent internally a duration as seconds (a float) (depends what granularity you think you need). Then it is trivial to compare or calculate. You can use a date/time library to convert that to a human-readable format.

    Alternatively, you can use something like YYYY-MM-DDTHH:MM:SS. This compares correctly (so, 0001-12-30T00:00:00 is smaller than 0002-00-00T00:00:00); but this has the problem that you always have to normalize it and this is not at all trivial.