You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Martynas Jusevičius <ma...@graphity.org> on 2013/11/14 03:02:34 UTC

Building xsd:dateTime from xsd:gYear + xsd:gMonth + xsd:gDay

Hey,

I have datetime components as 3 separate literals, with xsd:gYear,
xsd:gMonth, xsd:gDay respective datatypes. Not all mandatory - the
format can be YYYY, YYYY-MM, YYYY-MM-DD.

Now how do I combine those into a single xsd:dateTime Literal? A
concrete use case would be converting time:inDateTime values into
time:inXSDDateTime values:
http://www.w3.org/TR/owl-time/#calclock

I came up with the code below which seems to work but is not pretty
(and doesn't deal with time). I also looked at DateTimeStruct but
either way there seemed to be some datatype mismatch. I think it would
make more sense for DateTimeStruct to use the builder pattern instead
of static methods.

Is there a better way?

        if (resource.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#inDateTime")))
        {
            Calendar calendar = Calendar.getInstance();
            Resource dateTimeDesc =
resource.getPropertyResourceValue(ResourceFactory.createProperty("http://www.w3.org/2006/time#inDateTime"));

            if (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#year")))
            {
                RDFNode object =
dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#year")).getObject();
                if (object.isLiteral())
                {
                    Literal literal = object.asLiteral();
                    calendar.set(Calendar.YEAR,
Integer.parseInt(literal.getLexicalForm()));
                }
            }
            else throw new DateTimeParseException("time:year value is missing");

            if (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#month")))
            {
                RDFNode object =
dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#month")).getObject();
                if (object.isLiteral())
                {
                    Literal literal = object.asLiteral();
                    calendar.set(Calendar.MONTH,
Integer.parseInt(literal.getLexicalForm()));
                }
            }

            if (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#day")))
            {
                RDFNode object =
dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#day")).getObject();
                if (object.isLiteral())
                {
                    Literal literal = object.asLiteral();
                    calendar.set(Calendar.DAY_OF_MONTH,
Integer.parseInt(literal.getLexicalForm()));
                }
            }

            calendar.set(Calendar.HOUR, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            Literal dateTime = resource.getModel().createTypedLiteral(calendar);
            resource.addLiteral(ResourceFactory.createProperty("http://www.w3.org/2006/time#inXSDDateTime"),
dateTime);
        }

Martynas
graphityhq.com

Re: Building xsd:dateTime from xsd:gYear + xsd:gMonth + xsd:gDay

Posted by Andy Seaborne <an...@apache.org>.
On 14/11/13 20:28, Martynas Jusevičius wrote:
> OK. One more question - most of the examples I was looking at (like
> the one from StackOverflow) use simple integer values like
> "11"^^xsd:gMonth, and not the "--11"^^xsd:gMonth syntax. But strictly
> speaking, these are illegal values?

Yes - strictly illegal.

	Andy

>
> On Thu, Nov 14, 2013 at 9:11 PM, Andy Seaborne <an...@apache.org> wrote:
>> On 14/11/13 18:29, Martynas Jusevičius wrote:
>>>
>>> BTW, there seems to be a related question on StackOverflow:
>>>
>>> http://answers.semanticweb.com/questions/610/ordering-by-time-in-sparql-query
>>>
>>> I might just give up building an xsd:dateTime and use separate
>>> year/month/day components.
>>>
>>> On Thu, Nov 14, 2013 at 7:26 PM, Martynas Jusevičius
>>> <ma...@graphity.org> wrote:
>>>>
>>>> Andy, now I'm confused. Where are you looking?
>>
>>
>> My mistake - I just grepped for "month" and didn't look carefully enough.
>>
>> gMonth and friends are OK to parse, albeit a syntax that people don't engage
>> with.  But my experience all date/times formats suffer from bad data if not
>> machine generated, whether xsd, RFC or whatever.
>>
>>
>>>> I checked the RDF/XML
>>>> version of Time ontology and it says:
>>>>
>>>>     <owl:DatatypeProperty rdf:ID="year">
>>>>       <rdfs:domain rdf:resource="#DateTimeDescription" />
>>>>       <rdfs:range  rdf:resource="&xsd;gYear" />
>>>>     </owl:DatatypeProperty>
>>>>
>>>>     <owl:DatatypeProperty rdf:ID="month">
>>>>       <rdfs:domain rdf:resource="#DateTimeDescription" />
>>>>       <rdfs:range  rdf:resource="&xsd;gMonth" />
>>>>     </owl:DatatypeProperty>
>>>>
>>>>     <owl:DatatypeProperty rdf:ID="day">
>>>>       <rdfs:domain rdf:resource="#DateTimeDescription" />
>>>>       <rdfs:range  rdf:resource="&xsd;gDay" />
>>>>     </owl:DatatypeProperty>
>>>>
>>


Re: Building xsd:dateTime from xsd:gYear + xsd:gMonth + xsd:gDay

Posted by Martynas Jusevičius <ma...@graphity.org>.
OK. One more question - most of the examples I was looking at (like
the one from StackOverflow) use simple integer values like
"11"^^xsd:gMonth, and not the "--11"^^xsd:gMonth syntax. But strictly
speaking, these are illegal values?

On Thu, Nov 14, 2013 at 9:11 PM, Andy Seaborne <an...@apache.org> wrote:
> On 14/11/13 18:29, Martynas Jusevičius wrote:
>>
>> BTW, there seems to be a related question on StackOverflow:
>>
>> http://answers.semanticweb.com/questions/610/ordering-by-time-in-sparql-query
>>
>> I might just give up building an xsd:dateTime and use separate
>> year/month/day components.
>>
>> On Thu, Nov 14, 2013 at 7:26 PM, Martynas Jusevičius
>> <ma...@graphity.org> wrote:
>>>
>>> Andy, now I'm confused. Where are you looking?
>
>
> My mistake - I just grepped for "month" and didn't look carefully enough.
>
> gMonth and friends are OK to parse, albeit a syntax that people don't engage
> with.  But my experience all date/times formats suffer from bad data if not
> machine generated, whether xsd, RFC or whatever.
>
>
>>> I checked the RDF/XML
>>> version of Time ontology and it says:
>>>
>>>    <owl:DatatypeProperty rdf:ID="year">
>>>      <rdfs:domain rdf:resource="#DateTimeDescription" />
>>>      <rdfs:range  rdf:resource="&xsd;gYear" />
>>>    </owl:DatatypeProperty>
>>>
>>>    <owl:DatatypeProperty rdf:ID="month">
>>>      <rdfs:domain rdf:resource="#DateTimeDescription" />
>>>      <rdfs:range  rdf:resource="&xsd;gMonth" />
>>>    </owl:DatatypeProperty>
>>>
>>>    <owl:DatatypeProperty rdf:ID="day">
>>>      <rdfs:domain rdf:resource="#DateTimeDescription" />
>>>      <rdfs:range  rdf:resource="&xsd;gDay" />
>>>    </owl:DatatypeProperty>
>>>
>

Re: Building xsd:dateTime from xsd:gYear + xsd:gMonth + xsd:gDay

Posted by Andy Seaborne <an...@apache.org>.
On 14/11/13 18:29, Martynas Jusevičius wrote:
> BTW, there seems to be a related question on StackOverflow:
> http://answers.semanticweb.com/questions/610/ordering-by-time-in-sparql-query
>
> I might just give up building an xsd:dateTime and use separate
> year/month/day components.
>
> On Thu, Nov 14, 2013 at 7:26 PM, Martynas Jusevičius
> <ma...@graphity.org> wrote:
>> Andy, now I'm confused. Where are you looking?

My mistake - I just grepped for "month" and didn't look carefully enough.

gMonth and friends are OK to parse, albeit a syntax that people don't 
engage with.  But my experience all date/times formats suffer from bad 
data if not machine generated, whether xsd, RFC or whatever.

>> I checked the RDF/XML
>> version of Time ontology and it says:
>>
>>    <owl:DatatypeProperty rdf:ID="year">
>>      <rdfs:domain rdf:resource="#DateTimeDescription" />
>>      <rdfs:range  rdf:resource="&xsd;gYear" />
>>    </owl:DatatypeProperty>
>>
>>    <owl:DatatypeProperty rdf:ID="month">
>>      <rdfs:domain rdf:resource="#DateTimeDescription" />
>>      <rdfs:range  rdf:resource="&xsd;gMonth" />
>>    </owl:DatatypeProperty>
>>
>>    <owl:DatatypeProperty rdf:ID="day">
>>      <rdfs:domain rdf:resource="#DateTimeDescription" />
>>      <rdfs:range  rdf:resource="&xsd;gDay" />
>>    </owl:DatatypeProperty>
>>


Re: Building xsd:dateTime from xsd:gYear + xsd:gMonth + xsd:gDay

Posted by Martynas Jusevičius <ma...@graphity.org>.
BTW, there seems to be a related question on StackOverflow:
http://answers.semanticweb.com/questions/610/ordering-by-time-in-sparql-query

I might just give up building an xsd:dateTime and use separate
year/month/day components.

On Thu, Nov 14, 2013 at 7:26 PM, Martynas Jusevičius
<ma...@graphity.org> wrote:
> Andy, now I'm confused. Where are you looking? I checked the RDF/XML
> version of Time ontology and it says:
>
>   <owl:DatatypeProperty rdf:ID="year">
>     <rdfs:domain rdf:resource="#DateTimeDescription" />
>     <rdfs:range  rdf:resource="&xsd;gYear" />
>   </owl:DatatypeProperty>
>
>   <owl:DatatypeProperty rdf:ID="month">
>     <rdfs:domain rdf:resource="#DateTimeDescription" />
>     <rdfs:range  rdf:resource="&xsd;gMonth" />
>   </owl:DatatypeProperty>
>
>   <owl:DatatypeProperty rdf:ID="day">
>     <rdfs:domain rdf:resource="#DateTimeDescription" />
>     <rdfs:range  rdf:resource="&xsd;gDay" />
>   </owl:DatatypeProperty>
>
> On Thu, Nov 14, 2013 at 7:10 PM, Andy Seaborne <an...@apache.org> wrote:
>> On 14/11/13 16:42, Martynas Jusevičius wrote:
>>>
>>> OK I was probably too quick - now I realized the syntax of xsd:gMonth
>>> and xsd:gDay is not so simple...
>>
>>
>> :-)
>>
>> http://www.w3.org/TR/xmlschema11-2/#dateTime for details.
>>
>> The range of time:month is a non-negative integer so may be single digit.
>> That'll need normalizing and checking to use xsd: where it must be two
>> digits.  Ditto time:day.  And maybe people write years as two digits.
>>
>> gMonth and gDay have "missing parts" indicators "--" and "---"
>> but I don't see any g* here.
>>
>>         Andy
>>
>>
>>>
>>> On Thu, Nov 14, 2013 at 4:05 PM, Martynas Jusevičius
>>> <ma...@graphity.org> wrote:
>>>>
>>>> I came up with an approach that concatenates lexical values and
>>>> doesn't need Calendar or DateTimeStruct.
>>>>
>>>> Not sure however how this aligns with the range of time:inXSDDateTime
>>>> which is xsd:dateTime - can xsd:gYear/xsd:gMonthDay/xsd:date be
>>>> treated as xsd:dateTime values? I guess I'll have to typecast them in
>>>> SPARQL.
>>>>
>>>>          if
>>>> (resource.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#inDateTime")))
>>>>          {
>>>>              Literal dateTime;
>>>>              Resource dateTimeDesc =
>>>>
>>>> resource.getPropertyResourceValue(ResourceFactory.createProperty("http://www.w3.org/2006/time#inDateTime"));
>>>>
>>>>              if
>>>> (!dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#year")))
>>>>                  throw new DateTimeParseException("time:year value is
>>>> missing");
>>>>              RDFNode yearObject =
>>>>
>>>> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#year")).getObject();
>>>>              if (!yearObject.isLiteral())
>>>>                  throw new DateTimeParseException("time:year value is
>>>> not a Literal");
>>>>
>>>>              if
>>>> (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#month")))
>>>>              {
>>>>                  RDFNode monthObject =
>>>>
>>>> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#month")).getObject();
>>>>                  if (!monthObject.isLiteral())
>>>>                      throw new DateTimeParseException("time:month value
>>>> is not a Literal");
>>>>
>>>>                  if
>>>>
>>>> (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#day")))
>>>>                  {
>>>>                      RDFNode dayObject =
>>>>
>>>> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#day")).getObject();
>>>>                      if (!dayObject.isLiteral())
>>>>                          throw new DateTimeParseException("time:day
>>>> value is not a Literal");
>>>>
>>>>                      dateTime =
>>>>
>>>> resource.getModel().createTypedLiteral(yearObject.asLiteral().getLexicalForm()
>>>> + "-" +
>>>>                                  monthObject.asLiteral().getLexicalForm()
>>>> + "-" +
>>>>                                  dayObject.asLiteral().getLexicalForm(),
>>>>                              XSDDatatype.XSDdate);
>>>>                  }
>>>>                  else
>>>>                  {
>>>>                      dateTime =
>>>>
>>>> resource.getModel().createTypedLiteral(yearObject.asLiteral().getLexicalForm()
>>>> + "-" +
>>>>
>>>> monthObject.asLiteral().getLexicalForm(),
>>>>                              XSDDatatype.XSDgMonthDay);
>>>>                  }
>>>>              }
>>>>              else
>>>>                  dateTime = yearObject.asLiteral();
>>>>
>>>>
>>>> resource.addLiteral(ResourceFactory.createProperty("http://www.w3.org/2006/time#inXSDDateTime"),
>>>> dateTime);
>>>>          }
>>>>
>>>> On Thu, Nov 14, 2013 at 3:08 PM, Martynas Jusevičius
>>>> <ma...@graphity.org> wrote:
>>>>>
>>>>> I'll try DateTimeStruct again, but that basically means I need my own
>>>>> copy of the class, since I currently cannot extend it to override the
>>>>> private constructor?
>>>>>
>>>>> On Thu, Nov 14, 2013 at 2:15 PM, Andy Seaborne <an...@apache.org> wrote:
>>>>>>
>>>>>> Hi there,
>>>>>>
>>>>>> DateTimeStruct is, well, a struct. The fields are public.  You could
>>>>>> write a
>>>>>> builder to target that.  The default constructor could be made public.
>>>>>> The
>>>>>> statics are specific patterns for the XSD date/time datatypes with
>>>>>> validation.
>>>>>>
>>>>>> DateTimeStruct represents the Date/time Seven-property model of XSD.
>>>>>> It can
>>>>>> produce the string for xsd:date or xsd:dateTime but not the gregorial
>>>>>> g*
>>>>>> datatypes.
>>>>>>
>>>>>> java.util.calendar is OK as a value but, in the details, unusable for
>>>>>> XSD
>>>>>> types.  Why not set DateTimeStruct fields?
>>>>>>
>>>>>> javax.xml.datatype.XMLGregorianCalendar could be of use - it has
>>>>>> getters and
>>>>>> setters.
>>>>>>
>>>>>> For DateTimeStruct or XMLGregorianCalendar, you can then use
>>>>>>
>>>>>> createTypedLiteral(String lex, RDFDatatype dtype)
>>>>>>
>>>>>>
>>>>>>> Not all mandatory - the
>>>>>>> format can be YYYY, YYYY-MM, YYYY-MM-DD.
>>>>>>
>>>>>>
>>>>>> You could build the lexical form.
>>>>>>
>>>>>>          Andy
>>>>>>
>>>>>>
>>>>>> On 14/11/13 02:02, Martynas Jusevičius wrote:
>>>>>>>
>>>>>>>
>>>>>>> Hey,
>>>>>>>
>>>>>>> I have datetime components as 3 separate literals, with xsd:gYear,
>>>>>>> xsd:gMonth, xsd:gDay respective datatypes. Not all mandatory - the
>>>>>>> format can be YYYY, YYYY-MM, YYYY-MM-DD.
>>>>>>>
>>>>>>> Now how do I combine those into a single xsd:dateTime Literal? A
>>>>>>> concrete use case would be converting time:inDateTime values into
>>>>>>> time:inXSDDateTime values:
>>>>>>> http://www.w3.org/TR/owl-time/#calclock
>>>>>>>
>>>>>>> I came up with the code below which seems to work but is not pretty
>>>>>>> (and doesn't deal with time). I also looked at DateTimeStruct but
>>>>>>> either way there seemed to be some datatype mismatch. I think it would
>>>>>>> make more sense for DateTimeStruct to use the builder pattern instead
>>>>>>> of static methods.
>>>>>>>
>>>>>>> Is there a better way?
>>>>>>>
>>>>>>>           if
>>>>>>>
>>>>>>> (resource.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#inDateTime")))
>>>>>>>           {
>>>>>>>               Calendar calendar = Calendar.getInstance();
>>>>>>>               Resource dateTimeDesc =
>>>>>>>
>>>>>>>
>>>>>>> resource.getPropertyResourceValue(ResourceFactory.createProperty("http://www.w3.org/2006/time#inDateTime"));
>>>>>>>
>>>>>>>               if
>>>>>>>
>>>>>>> (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#year")))
>>>>>>>               {
>>>>>>>                   RDFNode object =
>>>>>>>
>>>>>>>
>>>>>>> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#year")).getObject();
>>>>>>>                   if (object.isLiteral())
>>>>>>>                   {
>>>>>>>                       Literal literal = object.asLiteral();
>>>>>>>                       calendar.set(Calendar.YEAR,
>>>>>>> Integer.parseInt(literal.getLexicalForm()));
>>>>>>>                   }
>>>>>>>               }
>>>>>>>               else throw new DateTimeParseException("time:year value
>>>>>>> is
>>>>>>> missing");
>>>>>>>
>>>>>>>               if
>>>>>>>
>>>>>>> (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#month")))
>>>>>>>               {
>>>>>>>                   RDFNode object =
>>>>>>>
>>>>>>>
>>>>>>> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#month")).getObject();
>>>>>>>                   if (object.isLiteral())
>>>>>>>                   {
>>>>>>>                       Literal literal = object.asLiteral();
>>>>>>>                       calendar.set(Calendar.MONTH,
>>>>>>> Integer.parseInt(literal.getLexicalForm()));
>>>>>>>                   }
>>>>>>>               }
>>>>>>>
>>>>>>>               if
>>>>>>>
>>>>>>> (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#day")))
>>>>>>>               {
>>>>>>>                   RDFNode object =
>>>>>>>
>>>>>>>
>>>>>>> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#day")).getObject();
>>>>>>>                   if (object.isLiteral())
>>>>>>>                   {
>>>>>>>                       Literal literal = object.asLiteral();
>>>>>>>                       calendar.set(Calendar.DAY_OF_MONTH,
>>>>>>> Integer.parseInt(literal.getLexicalForm()));
>>>>>>>                   }
>>>>>>>               }
>>>>>>>
>>>>>>>               calendar.set(Calendar.HOUR, 0);
>>>>>>>               calendar.set(Calendar.MINUTE, 0);
>>>>>>>               calendar.set(Calendar.SECOND, 0);
>>>>>>>               Literal dateTime =
>>>>>>> resource.getModel().createTypedLiteral(calendar);
>>>>>>>
>>>>>>>
>>>>>>> resource.addLiteral(ResourceFactory.createProperty("http://www.w3.org/2006/time#inXSDDateTime"),
>>>>>>> dateTime);
>>>>>>>           }
>>>>>>>
>>>>>>> Martynas
>>>>>>> graphityhq.com
>>>>>>>
>>>>>>
>>

Re: Building xsd:dateTime from xsd:gYear + xsd:gMonth + xsd:gDay

Posted by Martynas Jusevičius <ma...@graphity.org>.
Andy, now I'm confused. Where are you looking? I checked the RDF/XML
version of Time ontology and it says:

  <owl:DatatypeProperty rdf:ID="year">
    <rdfs:domain rdf:resource="#DateTimeDescription" />
    <rdfs:range  rdf:resource="&xsd;gYear" />
  </owl:DatatypeProperty>

  <owl:DatatypeProperty rdf:ID="month">
    <rdfs:domain rdf:resource="#DateTimeDescription" />
    <rdfs:range  rdf:resource="&xsd;gMonth" />
  </owl:DatatypeProperty>

  <owl:DatatypeProperty rdf:ID="day">
    <rdfs:domain rdf:resource="#DateTimeDescription" />
    <rdfs:range  rdf:resource="&xsd;gDay" />
  </owl:DatatypeProperty>

On Thu, Nov 14, 2013 at 7:10 PM, Andy Seaborne <an...@apache.org> wrote:
> On 14/11/13 16:42, Martynas Jusevičius wrote:
>>
>> OK I was probably too quick - now I realized the syntax of xsd:gMonth
>> and xsd:gDay is not so simple...
>
>
> :-)
>
> http://www.w3.org/TR/xmlschema11-2/#dateTime for details.
>
> The range of time:month is a non-negative integer so may be single digit.
> That'll need normalizing and checking to use xsd: where it must be two
> digits.  Ditto time:day.  And maybe people write years as two digits.
>
> gMonth and gDay have "missing parts" indicators "--" and "---"
> but I don't see any g* here.
>
>         Andy
>
>
>>
>> On Thu, Nov 14, 2013 at 4:05 PM, Martynas Jusevičius
>> <ma...@graphity.org> wrote:
>>>
>>> I came up with an approach that concatenates lexical values and
>>> doesn't need Calendar or DateTimeStruct.
>>>
>>> Not sure however how this aligns with the range of time:inXSDDateTime
>>> which is xsd:dateTime - can xsd:gYear/xsd:gMonthDay/xsd:date be
>>> treated as xsd:dateTime values? I guess I'll have to typecast them in
>>> SPARQL.
>>>
>>>          if
>>> (resource.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#inDateTime")))
>>>          {
>>>              Literal dateTime;
>>>              Resource dateTimeDesc =
>>>
>>> resource.getPropertyResourceValue(ResourceFactory.createProperty("http://www.w3.org/2006/time#inDateTime"));
>>>
>>>              if
>>> (!dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#year")))
>>>                  throw new DateTimeParseException("time:year value is
>>> missing");
>>>              RDFNode yearObject =
>>>
>>> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#year")).getObject();
>>>              if (!yearObject.isLiteral())
>>>                  throw new DateTimeParseException("time:year value is
>>> not a Literal");
>>>
>>>              if
>>> (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#month")))
>>>              {
>>>                  RDFNode monthObject =
>>>
>>> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#month")).getObject();
>>>                  if (!monthObject.isLiteral())
>>>                      throw new DateTimeParseException("time:month value
>>> is not a Literal");
>>>
>>>                  if
>>>
>>> (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#day")))
>>>                  {
>>>                      RDFNode dayObject =
>>>
>>> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#day")).getObject();
>>>                      if (!dayObject.isLiteral())
>>>                          throw new DateTimeParseException("time:day
>>> value is not a Literal");
>>>
>>>                      dateTime =
>>>
>>> resource.getModel().createTypedLiteral(yearObject.asLiteral().getLexicalForm()
>>> + "-" +
>>>                                  monthObject.asLiteral().getLexicalForm()
>>> + "-" +
>>>                                  dayObject.asLiteral().getLexicalForm(),
>>>                              XSDDatatype.XSDdate);
>>>                  }
>>>                  else
>>>                  {
>>>                      dateTime =
>>>
>>> resource.getModel().createTypedLiteral(yearObject.asLiteral().getLexicalForm()
>>> + "-" +
>>>
>>> monthObject.asLiteral().getLexicalForm(),
>>>                              XSDDatatype.XSDgMonthDay);
>>>                  }
>>>              }
>>>              else
>>>                  dateTime = yearObject.asLiteral();
>>>
>>>
>>> resource.addLiteral(ResourceFactory.createProperty("http://www.w3.org/2006/time#inXSDDateTime"),
>>> dateTime);
>>>          }
>>>
>>> On Thu, Nov 14, 2013 at 3:08 PM, Martynas Jusevičius
>>> <ma...@graphity.org> wrote:
>>>>
>>>> I'll try DateTimeStruct again, but that basically means I need my own
>>>> copy of the class, since I currently cannot extend it to override the
>>>> private constructor?
>>>>
>>>> On Thu, Nov 14, 2013 at 2:15 PM, Andy Seaborne <an...@apache.org> wrote:
>>>>>
>>>>> Hi there,
>>>>>
>>>>> DateTimeStruct is, well, a struct. The fields are public.  You could
>>>>> write a
>>>>> builder to target that.  The default constructor could be made public.
>>>>> The
>>>>> statics are specific patterns for the XSD date/time datatypes with
>>>>> validation.
>>>>>
>>>>> DateTimeStruct represents the Date/time Seven-property model of XSD.
>>>>> It can
>>>>> produce the string for xsd:date or xsd:dateTime but not the gregorial
>>>>> g*
>>>>> datatypes.
>>>>>
>>>>> java.util.calendar is OK as a value but, in the details, unusable for
>>>>> XSD
>>>>> types.  Why not set DateTimeStruct fields?
>>>>>
>>>>> javax.xml.datatype.XMLGregorianCalendar could be of use - it has
>>>>> getters and
>>>>> setters.
>>>>>
>>>>> For DateTimeStruct or XMLGregorianCalendar, you can then use
>>>>>
>>>>> createTypedLiteral(String lex, RDFDatatype dtype)
>>>>>
>>>>>
>>>>>> Not all mandatory - the
>>>>>> format can be YYYY, YYYY-MM, YYYY-MM-DD.
>>>>>
>>>>>
>>>>> You could build the lexical form.
>>>>>
>>>>>          Andy
>>>>>
>>>>>
>>>>> On 14/11/13 02:02, Martynas Jusevičius wrote:
>>>>>>
>>>>>>
>>>>>> Hey,
>>>>>>
>>>>>> I have datetime components as 3 separate literals, with xsd:gYear,
>>>>>> xsd:gMonth, xsd:gDay respective datatypes. Not all mandatory - the
>>>>>> format can be YYYY, YYYY-MM, YYYY-MM-DD.
>>>>>>
>>>>>> Now how do I combine those into a single xsd:dateTime Literal? A
>>>>>> concrete use case would be converting time:inDateTime values into
>>>>>> time:inXSDDateTime values:
>>>>>> http://www.w3.org/TR/owl-time/#calclock
>>>>>>
>>>>>> I came up with the code below which seems to work but is not pretty
>>>>>> (and doesn't deal with time). I also looked at DateTimeStruct but
>>>>>> either way there seemed to be some datatype mismatch. I think it would
>>>>>> make more sense for DateTimeStruct to use the builder pattern instead
>>>>>> of static methods.
>>>>>>
>>>>>> Is there a better way?
>>>>>>
>>>>>>           if
>>>>>>
>>>>>> (resource.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#inDateTime")))
>>>>>>           {
>>>>>>               Calendar calendar = Calendar.getInstance();
>>>>>>               Resource dateTimeDesc =
>>>>>>
>>>>>>
>>>>>> resource.getPropertyResourceValue(ResourceFactory.createProperty("http://www.w3.org/2006/time#inDateTime"));
>>>>>>
>>>>>>               if
>>>>>>
>>>>>> (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#year")))
>>>>>>               {
>>>>>>                   RDFNode object =
>>>>>>
>>>>>>
>>>>>> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#year")).getObject();
>>>>>>                   if (object.isLiteral())
>>>>>>                   {
>>>>>>                       Literal literal = object.asLiteral();
>>>>>>                       calendar.set(Calendar.YEAR,
>>>>>> Integer.parseInt(literal.getLexicalForm()));
>>>>>>                   }
>>>>>>               }
>>>>>>               else throw new DateTimeParseException("time:year value
>>>>>> is
>>>>>> missing");
>>>>>>
>>>>>>               if
>>>>>>
>>>>>> (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#month")))
>>>>>>               {
>>>>>>                   RDFNode object =
>>>>>>
>>>>>>
>>>>>> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#month")).getObject();
>>>>>>                   if (object.isLiteral())
>>>>>>                   {
>>>>>>                       Literal literal = object.asLiteral();
>>>>>>                       calendar.set(Calendar.MONTH,
>>>>>> Integer.parseInt(literal.getLexicalForm()));
>>>>>>                   }
>>>>>>               }
>>>>>>
>>>>>>               if
>>>>>>
>>>>>> (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#day")))
>>>>>>               {
>>>>>>                   RDFNode object =
>>>>>>
>>>>>>
>>>>>> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#day")).getObject();
>>>>>>                   if (object.isLiteral())
>>>>>>                   {
>>>>>>                       Literal literal = object.asLiteral();
>>>>>>                       calendar.set(Calendar.DAY_OF_MONTH,
>>>>>> Integer.parseInt(literal.getLexicalForm()));
>>>>>>                   }
>>>>>>               }
>>>>>>
>>>>>>               calendar.set(Calendar.HOUR, 0);
>>>>>>               calendar.set(Calendar.MINUTE, 0);
>>>>>>               calendar.set(Calendar.SECOND, 0);
>>>>>>               Literal dateTime =
>>>>>> resource.getModel().createTypedLiteral(calendar);
>>>>>>
>>>>>>
>>>>>> resource.addLiteral(ResourceFactory.createProperty("http://www.w3.org/2006/time#inXSDDateTime"),
>>>>>> dateTime);
>>>>>>           }
>>>>>>
>>>>>> Martynas
>>>>>> graphityhq.com
>>>>>>
>>>>>
>

Re: Building xsd:dateTime from xsd:gYear + xsd:gMonth + xsd:gDay

Posted by Andy Seaborne <an...@apache.org>.
On 14/11/13 16:42, Martynas Jusevičius wrote:
> OK I was probably too quick - now I realized the syntax of xsd:gMonth
> and xsd:gDay is not so simple...

:-)

http://www.w3.org/TR/xmlschema11-2/#dateTime for details.

The range of time:month is a non-negative integer so may be single 
digit.  That'll need normalizing and checking to use xsd: where it must 
be two digits.  Ditto time:day.  And maybe people write years as two digits.

gMonth and gDay have "missing parts" indicators "--" and "---"
but I don't see any g* here.

	Andy

>
> On Thu, Nov 14, 2013 at 4:05 PM, Martynas Jusevičius
> <ma...@graphity.org> wrote:
>> I came up with an approach that concatenates lexical values and
>> doesn't need Calendar or DateTimeStruct.
>>
>> Not sure however how this aligns with the range of time:inXSDDateTime
>> which is xsd:dateTime - can xsd:gYear/xsd:gMonthDay/xsd:date be
>> treated as xsd:dateTime values? I guess I'll have to typecast them in
>> SPARQL.
>>
>>          if (resource.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#inDateTime")))
>>          {
>>              Literal dateTime;
>>              Resource dateTimeDesc =
>> resource.getPropertyResourceValue(ResourceFactory.createProperty("http://www.w3.org/2006/time#inDateTime"));
>>
>>              if (!dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#year")))
>>                  throw new DateTimeParseException("time:year value is missing");
>>              RDFNode yearObject =
>> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#year")).getObject();
>>              if (!yearObject.isLiteral())
>>                  throw new DateTimeParseException("time:year value is
>> not a Literal");
>>
>>              if (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#month")))
>>              {
>>                  RDFNode monthObject =
>> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#month")).getObject();
>>                  if (!monthObject.isLiteral())
>>                      throw new DateTimeParseException("time:month value
>> is not a Literal");
>>
>>                  if
>> (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#day")))
>>                  {
>>                      RDFNode dayObject =
>> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#day")).getObject();
>>                      if (!dayObject.isLiteral())
>>                          throw new DateTimeParseException("time:day
>> value is not a Literal");
>>
>>                      dateTime =
>> resource.getModel().createTypedLiteral(yearObject.asLiteral().getLexicalForm()
>> + "-" +
>>                                  monthObject.asLiteral().getLexicalForm() + "-" +
>>                                  dayObject.asLiteral().getLexicalForm(),
>>                              XSDDatatype.XSDdate);
>>                  }
>>                  else
>>                  {
>>                      dateTime =
>> resource.getModel().createTypedLiteral(yearObject.asLiteral().getLexicalForm()
>> + "-" +
>>                                  monthObject.asLiteral().getLexicalForm(),
>>                              XSDDatatype.XSDgMonthDay);
>>                  }
>>              }
>>              else
>>                  dateTime = yearObject.asLiteral();
>>
>>              resource.addLiteral(ResourceFactory.createProperty("http://www.w3.org/2006/time#inXSDDateTime"),
>> dateTime);
>>          }
>>
>> On Thu, Nov 14, 2013 at 3:08 PM, Martynas Jusevičius
>> <ma...@graphity.org> wrote:
>>> I'll try DateTimeStruct again, but that basically means I need my own
>>> copy of the class, since I currently cannot extend it to override the
>>> private constructor?
>>>
>>> On Thu, Nov 14, 2013 at 2:15 PM, Andy Seaborne <an...@apache.org> wrote:
>>>> Hi there,
>>>>
>>>> DateTimeStruct is, well, a struct. The fields are public.  You could write a
>>>> builder to target that.  The default constructor could be made public.  The
>>>> statics are specific patterns for the XSD date/time datatypes with
>>>> validation.
>>>>
>>>> DateTimeStruct represents the Date/time Seven-property model of XSD.  It can
>>>> produce the string for xsd:date or xsd:dateTime but not the gregorial g*
>>>> datatypes.
>>>>
>>>> java.util.calendar is OK as a value but, in the details, unusable for XSD
>>>> types.  Why not set DateTimeStruct fields?
>>>>
>>>> javax.xml.datatype.XMLGregorianCalendar could be of use - it has getters and
>>>> setters.
>>>>
>>>> For DateTimeStruct or XMLGregorianCalendar, you can then use
>>>>
>>>> createTypedLiteral(String lex, RDFDatatype dtype)
>>>>
>>>>
>>>>> Not all mandatory - the
>>>>> format can be YYYY, YYYY-MM, YYYY-MM-DD.
>>>>
>>>> You could build the lexical form.
>>>>
>>>>          Andy
>>>>
>>>>
>>>> On 14/11/13 02:02, Martynas Jusevičius wrote:
>>>>>
>>>>> Hey,
>>>>>
>>>>> I have datetime components as 3 separate literals, with xsd:gYear,
>>>>> xsd:gMonth, xsd:gDay respective datatypes. Not all mandatory - the
>>>>> format can be YYYY, YYYY-MM, YYYY-MM-DD.
>>>>>
>>>>> Now how do I combine those into a single xsd:dateTime Literal? A
>>>>> concrete use case would be converting time:inDateTime values into
>>>>> time:inXSDDateTime values:
>>>>> http://www.w3.org/TR/owl-time/#calclock
>>>>>
>>>>> I came up with the code below which seems to work but is not pretty
>>>>> (and doesn't deal with time). I also looked at DateTimeStruct but
>>>>> either way there seemed to be some datatype mismatch. I think it would
>>>>> make more sense for DateTimeStruct to use the builder pattern instead
>>>>> of static methods.
>>>>>
>>>>> Is there a better way?
>>>>>
>>>>>           if
>>>>> (resource.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#inDateTime")))
>>>>>           {
>>>>>               Calendar calendar = Calendar.getInstance();
>>>>>               Resource dateTimeDesc =
>>>>>
>>>>> resource.getPropertyResourceValue(ResourceFactory.createProperty("http://www.w3.org/2006/time#inDateTime"));
>>>>>
>>>>>               if
>>>>> (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#year")))
>>>>>               {
>>>>>                   RDFNode object =
>>>>>
>>>>> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#year")).getObject();
>>>>>                   if (object.isLiteral())
>>>>>                   {
>>>>>                       Literal literal = object.asLiteral();
>>>>>                       calendar.set(Calendar.YEAR,
>>>>> Integer.parseInt(literal.getLexicalForm()));
>>>>>                   }
>>>>>               }
>>>>>               else throw new DateTimeParseException("time:year value is
>>>>> missing");
>>>>>
>>>>>               if
>>>>> (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#month")))
>>>>>               {
>>>>>                   RDFNode object =
>>>>>
>>>>> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#month")).getObject();
>>>>>                   if (object.isLiteral())
>>>>>                   {
>>>>>                       Literal literal = object.asLiteral();
>>>>>                       calendar.set(Calendar.MONTH,
>>>>> Integer.parseInt(literal.getLexicalForm()));
>>>>>                   }
>>>>>               }
>>>>>
>>>>>               if
>>>>> (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#day")))
>>>>>               {
>>>>>                   RDFNode object =
>>>>>
>>>>> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#day")).getObject();
>>>>>                   if (object.isLiteral())
>>>>>                   {
>>>>>                       Literal literal = object.asLiteral();
>>>>>                       calendar.set(Calendar.DAY_OF_MONTH,
>>>>> Integer.parseInt(literal.getLexicalForm()));
>>>>>                   }
>>>>>               }
>>>>>
>>>>>               calendar.set(Calendar.HOUR, 0);
>>>>>               calendar.set(Calendar.MINUTE, 0);
>>>>>               calendar.set(Calendar.SECOND, 0);
>>>>>               Literal dateTime =
>>>>> resource.getModel().createTypedLiteral(calendar);
>>>>>
>>>>> resource.addLiteral(ResourceFactory.createProperty("http://www.w3.org/2006/time#inXSDDateTime"),
>>>>> dateTime);
>>>>>           }
>>>>>
>>>>> Martynas
>>>>> graphityhq.com
>>>>>
>>>>


Re: Building xsd:dateTime from xsd:gYear + xsd:gMonth + xsd:gDay

Posted by Martynas Jusevičius <ma...@graphity.org>.
OK I was probably too quick - now I realized the syntax of xsd:gMonth
and xsd:gDay is not so simple...

On Thu, Nov 14, 2013 at 4:05 PM, Martynas Jusevičius
<ma...@graphity.org> wrote:
> I came up with an approach that concatenates lexical values and
> doesn't need Calendar or DateTimeStruct.
>
> Not sure however how this aligns with the range of time:inXSDDateTime
> which is xsd:dateTime - can xsd:gYear/xsd:gMonthDay/xsd:date be
> treated as xsd:dateTime values? I guess I'll have to typecast them in
> SPARQL.
>
>         if (resource.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#inDateTime")))
>         {
>             Literal dateTime;
>             Resource dateTimeDesc =
> resource.getPropertyResourceValue(ResourceFactory.createProperty("http://www.w3.org/2006/time#inDateTime"));
>
>             if (!dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#year")))
>                 throw new DateTimeParseException("time:year value is missing");
>             RDFNode yearObject =
> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#year")).getObject();
>             if (!yearObject.isLiteral())
>                 throw new DateTimeParseException("time:year value is
> not a Literal");
>
>             if (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#month")))
>             {
>                 RDFNode monthObject =
> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#month")).getObject();
>                 if (!monthObject.isLiteral())
>                     throw new DateTimeParseException("time:month value
> is not a Literal");
>
>                 if
> (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#day")))
>                 {
>                     RDFNode dayObject =
> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#day")).getObject();
>                     if (!dayObject.isLiteral())
>                         throw new DateTimeParseException("time:day
> value is not a Literal");
>
>                     dateTime =
> resource.getModel().createTypedLiteral(yearObject.asLiteral().getLexicalForm()
> + "-" +
>                                 monthObject.asLiteral().getLexicalForm() + "-" +
>                                 dayObject.asLiteral().getLexicalForm(),
>                             XSDDatatype.XSDdate);
>                 }
>                 else
>                 {
>                     dateTime =
> resource.getModel().createTypedLiteral(yearObject.asLiteral().getLexicalForm()
> + "-" +
>                                 monthObject.asLiteral().getLexicalForm(),
>                             XSDDatatype.XSDgMonthDay);
>                 }
>             }
>             else
>                 dateTime = yearObject.asLiteral();
>
>             resource.addLiteral(ResourceFactory.createProperty("http://www.w3.org/2006/time#inXSDDateTime"),
> dateTime);
>         }
>
> On Thu, Nov 14, 2013 at 3:08 PM, Martynas Jusevičius
> <ma...@graphity.org> wrote:
>> I'll try DateTimeStruct again, but that basically means I need my own
>> copy of the class, since I currently cannot extend it to override the
>> private constructor?
>>
>> On Thu, Nov 14, 2013 at 2:15 PM, Andy Seaborne <an...@apache.org> wrote:
>>> Hi there,
>>>
>>> DateTimeStruct is, well, a struct. The fields are public.  You could write a
>>> builder to target that.  The default constructor could be made public.  The
>>> statics are specific patterns for the XSD date/time datatypes with
>>> validation.
>>>
>>> DateTimeStruct represents the Date/time Seven-property model of XSD.  It can
>>> produce the string for xsd:date or xsd:dateTime but not the gregorial g*
>>> datatypes.
>>>
>>> java.util.calendar is OK as a value but, in the details, unusable for XSD
>>> types.  Why not set DateTimeStruct fields?
>>>
>>> javax.xml.datatype.XMLGregorianCalendar could be of use - it has getters and
>>> setters.
>>>
>>> For DateTimeStruct or XMLGregorianCalendar, you can then use
>>>
>>> createTypedLiteral(String lex, RDFDatatype dtype)
>>>
>>>
>>>> Not all mandatory - the
>>>> format can be YYYY, YYYY-MM, YYYY-MM-DD.
>>>
>>> You could build the lexical form.
>>>
>>>         Andy
>>>
>>>
>>> On 14/11/13 02:02, Martynas Jusevičius wrote:
>>>>
>>>> Hey,
>>>>
>>>> I have datetime components as 3 separate literals, with xsd:gYear,
>>>> xsd:gMonth, xsd:gDay respective datatypes. Not all mandatory - the
>>>> format can be YYYY, YYYY-MM, YYYY-MM-DD.
>>>>
>>>> Now how do I combine those into a single xsd:dateTime Literal? A
>>>> concrete use case would be converting time:inDateTime values into
>>>> time:inXSDDateTime values:
>>>> http://www.w3.org/TR/owl-time/#calclock
>>>>
>>>> I came up with the code below which seems to work but is not pretty
>>>> (and doesn't deal with time). I also looked at DateTimeStruct but
>>>> either way there seemed to be some datatype mismatch. I think it would
>>>> make more sense for DateTimeStruct to use the builder pattern instead
>>>> of static methods.
>>>>
>>>> Is there a better way?
>>>>
>>>>          if
>>>> (resource.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#inDateTime")))
>>>>          {
>>>>              Calendar calendar = Calendar.getInstance();
>>>>              Resource dateTimeDesc =
>>>>
>>>> resource.getPropertyResourceValue(ResourceFactory.createProperty("http://www.w3.org/2006/time#inDateTime"));
>>>>
>>>>              if
>>>> (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#year")))
>>>>              {
>>>>                  RDFNode object =
>>>>
>>>> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#year")).getObject();
>>>>                  if (object.isLiteral())
>>>>                  {
>>>>                      Literal literal = object.asLiteral();
>>>>                      calendar.set(Calendar.YEAR,
>>>> Integer.parseInt(literal.getLexicalForm()));
>>>>                  }
>>>>              }
>>>>              else throw new DateTimeParseException("time:year value is
>>>> missing");
>>>>
>>>>              if
>>>> (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#month")))
>>>>              {
>>>>                  RDFNode object =
>>>>
>>>> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#month")).getObject();
>>>>                  if (object.isLiteral())
>>>>                  {
>>>>                      Literal literal = object.asLiteral();
>>>>                      calendar.set(Calendar.MONTH,
>>>> Integer.parseInt(literal.getLexicalForm()));
>>>>                  }
>>>>              }
>>>>
>>>>              if
>>>> (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#day")))
>>>>              {
>>>>                  RDFNode object =
>>>>
>>>> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#day")).getObject();
>>>>                  if (object.isLiteral())
>>>>                  {
>>>>                      Literal literal = object.asLiteral();
>>>>                      calendar.set(Calendar.DAY_OF_MONTH,
>>>> Integer.parseInt(literal.getLexicalForm()));
>>>>                  }
>>>>              }
>>>>
>>>>              calendar.set(Calendar.HOUR, 0);
>>>>              calendar.set(Calendar.MINUTE, 0);
>>>>              calendar.set(Calendar.SECOND, 0);
>>>>              Literal dateTime =
>>>> resource.getModel().createTypedLiteral(calendar);
>>>>
>>>> resource.addLiteral(ResourceFactory.createProperty("http://www.w3.org/2006/time#inXSDDateTime"),
>>>> dateTime);
>>>>          }
>>>>
>>>> Martynas
>>>> graphityhq.com
>>>>
>>>

Re: Building xsd:dateTime from xsd:gYear + xsd:gMonth + xsd:gDay

Posted by Martynas Jusevičius <ma...@graphity.org>.
I came up with an approach that concatenates lexical values and
doesn't need Calendar or DateTimeStruct.

Not sure however how this aligns with the range of time:inXSDDateTime
which is xsd:dateTime - can xsd:gYear/xsd:gMonthDay/xsd:date be
treated as xsd:dateTime values? I guess I'll have to typecast them in
SPARQL.

        if (resource.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#inDateTime")))
        {
            Literal dateTime;
            Resource dateTimeDesc =
resource.getPropertyResourceValue(ResourceFactory.createProperty("http://www.w3.org/2006/time#inDateTime"));

            if (!dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#year")))
                throw new DateTimeParseException("time:year value is missing");
            RDFNode yearObject =
dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#year")).getObject();
            if (!yearObject.isLiteral())
                throw new DateTimeParseException("time:year value is
not a Literal");

            if (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#month")))
            {
                RDFNode monthObject =
dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#month")).getObject();
                if (!monthObject.isLiteral())
                    throw new DateTimeParseException("time:month value
is not a Literal");

                if
(dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#day")))
                {
                    RDFNode dayObject =
dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#day")).getObject();
                    if (!dayObject.isLiteral())
                        throw new DateTimeParseException("time:day
value is not a Literal");

                    dateTime =
resource.getModel().createTypedLiteral(yearObject.asLiteral().getLexicalForm()
+ "-" +
                                monthObject.asLiteral().getLexicalForm() + "-" +
                                dayObject.asLiteral().getLexicalForm(),
                            XSDDatatype.XSDdate);
                }
                else
                {
                    dateTime =
resource.getModel().createTypedLiteral(yearObject.asLiteral().getLexicalForm()
+ "-" +
                                monthObject.asLiteral().getLexicalForm(),
                            XSDDatatype.XSDgMonthDay);
                }
            }
            else
                dateTime = yearObject.asLiteral();

            resource.addLiteral(ResourceFactory.createProperty("http://www.w3.org/2006/time#inXSDDateTime"),
dateTime);
        }

On Thu, Nov 14, 2013 at 3:08 PM, Martynas Jusevičius
<ma...@graphity.org> wrote:
> I'll try DateTimeStruct again, but that basically means I need my own
> copy of the class, since I currently cannot extend it to override the
> private constructor?
>
> On Thu, Nov 14, 2013 at 2:15 PM, Andy Seaborne <an...@apache.org> wrote:
>> Hi there,
>>
>> DateTimeStruct is, well, a struct. The fields are public.  You could write a
>> builder to target that.  The default constructor could be made public.  The
>> statics are specific patterns for the XSD date/time datatypes with
>> validation.
>>
>> DateTimeStruct represents the Date/time Seven-property model of XSD.  It can
>> produce the string for xsd:date or xsd:dateTime but not the gregorial g*
>> datatypes.
>>
>> java.util.calendar is OK as a value but, in the details, unusable for XSD
>> types.  Why not set DateTimeStruct fields?
>>
>> javax.xml.datatype.XMLGregorianCalendar could be of use - it has getters and
>> setters.
>>
>> For DateTimeStruct or XMLGregorianCalendar, you can then use
>>
>> createTypedLiteral(String lex, RDFDatatype dtype)
>>
>>
>>> Not all mandatory - the
>>> format can be YYYY, YYYY-MM, YYYY-MM-DD.
>>
>> You could build the lexical form.
>>
>>         Andy
>>
>>
>> On 14/11/13 02:02, Martynas Jusevičius wrote:
>>>
>>> Hey,
>>>
>>> I have datetime components as 3 separate literals, with xsd:gYear,
>>> xsd:gMonth, xsd:gDay respective datatypes. Not all mandatory - the
>>> format can be YYYY, YYYY-MM, YYYY-MM-DD.
>>>
>>> Now how do I combine those into a single xsd:dateTime Literal? A
>>> concrete use case would be converting time:inDateTime values into
>>> time:inXSDDateTime values:
>>> http://www.w3.org/TR/owl-time/#calclock
>>>
>>> I came up with the code below which seems to work but is not pretty
>>> (and doesn't deal with time). I also looked at DateTimeStruct but
>>> either way there seemed to be some datatype mismatch. I think it would
>>> make more sense for DateTimeStruct to use the builder pattern instead
>>> of static methods.
>>>
>>> Is there a better way?
>>>
>>>          if
>>> (resource.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#inDateTime")))
>>>          {
>>>              Calendar calendar = Calendar.getInstance();
>>>              Resource dateTimeDesc =
>>>
>>> resource.getPropertyResourceValue(ResourceFactory.createProperty("http://www.w3.org/2006/time#inDateTime"));
>>>
>>>              if
>>> (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#year")))
>>>              {
>>>                  RDFNode object =
>>>
>>> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#year")).getObject();
>>>                  if (object.isLiteral())
>>>                  {
>>>                      Literal literal = object.asLiteral();
>>>                      calendar.set(Calendar.YEAR,
>>> Integer.parseInt(literal.getLexicalForm()));
>>>                  }
>>>              }
>>>              else throw new DateTimeParseException("time:year value is
>>> missing");
>>>
>>>              if
>>> (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#month")))
>>>              {
>>>                  RDFNode object =
>>>
>>> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#month")).getObject();
>>>                  if (object.isLiteral())
>>>                  {
>>>                      Literal literal = object.asLiteral();
>>>                      calendar.set(Calendar.MONTH,
>>> Integer.parseInt(literal.getLexicalForm()));
>>>                  }
>>>              }
>>>
>>>              if
>>> (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#day")))
>>>              {
>>>                  RDFNode object =
>>>
>>> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#day")).getObject();
>>>                  if (object.isLiteral())
>>>                  {
>>>                      Literal literal = object.asLiteral();
>>>                      calendar.set(Calendar.DAY_OF_MONTH,
>>> Integer.parseInt(literal.getLexicalForm()));
>>>                  }
>>>              }
>>>
>>>              calendar.set(Calendar.HOUR, 0);
>>>              calendar.set(Calendar.MINUTE, 0);
>>>              calendar.set(Calendar.SECOND, 0);
>>>              Literal dateTime =
>>> resource.getModel().createTypedLiteral(calendar);
>>>
>>> resource.addLiteral(ResourceFactory.createProperty("http://www.w3.org/2006/time#inXSDDateTime"),
>>> dateTime);
>>>          }
>>>
>>> Martynas
>>> graphityhq.com
>>>
>>

Re: Building xsd:dateTime from xsd:gYear + xsd:gMonth + xsd:gDay

Posted by Martynas Jusevičius <ma...@graphity.org>.
I'll try DateTimeStruct again, but that basically means I need my own
copy of the class, since I currently cannot extend it to override the
private constructor?

On Thu, Nov 14, 2013 at 2:15 PM, Andy Seaborne <an...@apache.org> wrote:
> Hi there,
>
> DateTimeStruct is, well, a struct. The fields are public.  You could write a
> builder to target that.  The default constructor could be made public.  The
> statics are specific patterns for the XSD date/time datatypes with
> validation.
>
> DateTimeStruct represents the Date/time Seven-property model of XSD.  It can
> produce the string for xsd:date or xsd:dateTime but not the gregorial g*
> datatypes.
>
> java.util.calendar is OK as a value but, in the details, unusable for XSD
> types.  Why not set DateTimeStruct fields?
>
> javax.xml.datatype.XMLGregorianCalendar could be of use - it has getters and
> setters.
>
> For DateTimeStruct or XMLGregorianCalendar, you can then use
>
> createTypedLiteral(String lex, RDFDatatype dtype)
>
>
>> Not all mandatory - the
>> format can be YYYY, YYYY-MM, YYYY-MM-DD.
>
> You could build the lexical form.
>
>         Andy
>
>
> On 14/11/13 02:02, Martynas Jusevičius wrote:
>>
>> Hey,
>>
>> I have datetime components as 3 separate literals, with xsd:gYear,
>> xsd:gMonth, xsd:gDay respective datatypes. Not all mandatory - the
>> format can be YYYY, YYYY-MM, YYYY-MM-DD.
>>
>> Now how do I combine those into a single xsd:dateTime Literal? A
>> concrete use case would be converting time:inDateTime values into
>> time:inXSDDateTime values:
>> http://www.w3.org/TR/owl-time/#calclock
>>
>> I came up with the code below which seems to work but is not pretty
>> (and doesn't deal with time). I also looked at DateTimeStruct but
>> either way there seemed to be some datatype mismatch. I think it would
>> make more sense for DateTimeStruct to use the builder pattern instead
>> of static methods.
>>
>> Is there a better way?
>>
>>          if
>> (resource.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#inDateTime")))
>>          {
>>              Calendar calendar = Calendar.getInstance();
>>              Resource dateTimeDesc =
>>
>> resource.getPropertyResourceValue(ResourceFactory.createProperty("http://www.w3.org/2006/time#inDateTime"));
>>
>>              if
>> (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#year")))
>>              {
>>                  RDFNode object =
>>
>> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#year")).getObject();
>>                  if (object.isLiteral())
>>                  {
>>                      Literal literal = object.asLiteral();
>>                      calendar.set(Calendar.YEAR,
>> Integer.parseInt(literal.getLexicalForm()));
>>                  }
>>              }
>>              else throw new DateTimeParseException("time:year value is
>> missing");
>>
>>              if
>> (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#month")))
>>              {
>>                  RDFNode object =
>>
>> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#month")).getObject();
>>                  if (object.isLiteral())
>>                  {
>>                      Literal literal = object.asLiteral();
>>                      calendar.set(Calendar.MONTH,
>> Integer.parseInt(literal.getLexicalForm()));
>>                  }
>>              }
>>
>>              if
>> (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#day")))
>>              {
>>                  RDFNode object =
>>
>> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#day")).getObject();
>>                  if (object.isLiteral())
>>                  {
>>                      Literal literal = object.asLiteral();
>>                      calendar.set(Calendar.DAY_OF_MONTH,
>> Integer.parseInt(literal.getLexicalForm()));
>>                  }
>>              }
>>
>>              calendar.set(Calendar.HOUR, 0);
>>              calendar.set(Calendar.MINUTE, 0);
>>              calendar.set(Calendar.SECOND, 0);
>>              Literal dateTime =
>> resource.getModel().createTypedLiteral(calendar);
>>
>> resource.addLiteral(ResourceFactory.createProperty("http://www.w3.org/2006/time#inXSDDateTime"),
>> dateTime);
>>          }
>>
>> Martynas
>> graphityhq.com
>>
>

Re: Building xsd:dateTime from xsd:gYear + xsd:gMonth + xsd:gDay

Posted by Andy Seaborne <an...@apache.org>.
Hi there,

DateTimeStruct is, well, a struct. The fields are public.  You could 
write a builder to target that.  The default constructor could be made 
public.  The statics are specific patterns for the XSD date/time 
datatypes with validation.

DateTimeStruct represents the Date/time Seven-property model of XSD.  It 
can produce the string for xsd:date or xsd:dateTime but not the 
gregorial g* datatypes.

java.util.calendar is OK as a value but, in the details, unusable for 
XSD types.  Why not set DateTimeStruct fields?

javax.xml.datatype.XMLGregorianCalendar could be of use - it has getters 
and setters.

For DateTimeStruct or XMLGregorianCalendar, you can then use

createTypedLiteral(String lex, RDFDatatype dtype)

 > Not all mandatory - the
 > format can be YYYY, YYYY-MM, YYYY-MM-DD.

You could build the lexical form.

	Andy

On 14/11/13 02:02, Martynas Jusevičius wrote:
> Hey,
>
> I have datetime components as 3 separate literals, with xsd:gYear,
> xsd:gMonth, xsd:gDay respective datatypes. Not all mandatory - the
> format can be YYYY, YYYY-MM, YYYY-MM-DD.
>
> Now how do I combine those into a single xsd:dateTime Literal? A
> concrete use case would be converting time:inDateTime values into
> time:inXSDDateTime values:
> http://www.w3.org/TR/owl-time/#calclock
>
> I came up with the code below which seems to work but is not pretty
> (and doesn't deal with time). I also looked at DateTimeStruct but
> either way there seemed to be some datatype mismatch. I think it would
> make more sense for DateTimeStruct to use the builder pattern instead
> of static methods.
>
> Is there a better way?
>
>          if (resource.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#inDateTime")))
>          {
>              Calendar calendar = Calendar.getInstance();
>              Resource dateTimeDesc =
> resource.getPropertyResourceValue(ResourceFactory.createProperty("http://www.w3.org/2006/time#inDateTime"));
>
>              if (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#year")))
>              {
>                  RDFNode object =
> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#year")).getObject();
>                  if (object.isLiteral())
>                  {
>                      Literal literal = object.asLiteral();
>                      calendar.set(Calendar.YEAR,
> Integer.parseInt(literal.getLexicalForm()));
>                  }
>              }
>              else throw new DateTimeParseException("time:year value is missing");
>
>              if (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#month")))
>              {
>                  RDFNode object =
> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#month")).getObject();
>                  if (object.isLiteral())
>                  {
>                      Literal literal = object.asLiteral();
>                      calendar.set(Calendar.MONTH,
> Integer.parseInt(literal.getLexicalForm()));
>                  }
>              }
>
>              if (dateTimeDesc.hasProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#day")))
>              {
>                  RDFNode object =
> dateTimeDesc.getProperty(ResourceFactory.createProperty("http://www.w3.org/2006/time#day")).getObject();
>                  if (object.isLiteral())
>                  {
>                      Literal literal = object.asLiteral();
>                      calendar.set(Calendar.DAY_OF_MONTH,
> Integer.parseInt(literal.getLexicalForm()));
>                  }
>              }
>
>              calendar.set(Calendar.HOUR, 0);
>              calendar.set(Calendar.MINUTE, 0);
>              calendar.set(Calendar.SECOND, 0);
>              Literal dateTime = resource.getModel().createTypedLiteral(calendar);
>              resource.addLiteral(ResourceFactory.createProperty("http://www.w3.org/2006/time#inXSDDateTime"),
> dateTime);
>          }
>
> Martynas
> graphityhq.com
>