example of options 3 & 4 simplifying code (ACTION-86)

I argued in todays meeting, off the cuff, that option 2 (in Pat's
email [1]) offers only aesthetic improvements, while options 3 and 4
will result in simpler code.  I claimed that without simpler code,
we'd be better off staying with option 1 (no change).  Andy asked me
to provide a concrete example of my claimed code simplification.

Perhaps Andy is thinking, quite correctly, that a good API (like
Jena's?) already encapsulates and hides the trifold nature RDF
2004 literals.  I would agree that simplifying or restructuring RDF
literals wont change/simplify application code in that world.

So the examples I've come up with depend on stepping out of the clean
OO model.  Without making any normative claims here, or getting into
the type-system wars, I observe that a lot of code in the world is
like this, and it's probably not helpful to tell people who write this
kind of code not to.

Example 1.  Encode triples in some temporary/hack syntax, eg for
            talking to a front-end which doesn't have an RDF library

def encode_term_option_1(t):
   # assumes "simple literals" already folded into datatyped literals
   # as per RDF WG Aug 2011; otherwise there would be another branch.
   if t.is_literal:
     if t.language:
       return "lt_literal("+quoted(t.text)+","+quoted(t.lang)+")"
     else:
       return "dt_literal("+quoted(t.lexrep)+","+quoted(t.datatype)+")"
   else
     return "node("+quoted(t.iri)+")"

def encode_term_option_3_or_4(t):
   # additionally assumes folded-in language tags
   if t.datatype:
     return "literal("+quoted(t.lexrep)+","+quoted(t.datatype)+")"
   else
     return "node("+quoted(t.iri)+")"

def encode_triple(s,p,o):
   return (encode_term(s)+","+   
           encode_term(p)+","+   
           encode_term(o))
           

Example 2.  Look for a string in any kind of literal (regardless of
language). This is for a naive search, where the user just types some
stuff, without us knowing their language, or whether it's part of a
literal.

def search_option_1(triples, keyword):
   for s,p,o in triples:
      if o.is_literal:
         value is o.language or o.lexrep
         if keyword in value
               yield s

def search_option_3_or_4(triples, keyword):
   for s,p,o in triples:
      if o.is_literal and keyword in o.lexrep:
         yield s

I'm sure there are more examples, but hopefully this clarifies what I'm
talking about. I recognize this is not dramatic; it's one or two
lines.  But those lines come up a lot (in this non-OO world).  And RDF
is trying to be the ultra-elegant core data bus, so there is a very
strong light shining on the odd little bits (like language tags).

Oh look, I almost got through this without mentioning JSON.  (I think
the JSON world will very much like the simplification of 3&4.)

    -- Sandro

[1] http://lists.w3.org/Archives/Public/public-rdf-wg/2011Sep/0019

Received on Wednesday, 7 September 2011 17:34:19 UTC