<?xml version='1.0' encoding='utf-8'?>
<?xml-stylesheet href="/style/browser.xsl" type="text/xsl"?>
<essay xmlns="http://docbook.org/ns/docbook"
       xmlns:xlink="http://www.w3.org/1999/xlink"
       xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
       xmlns:dc='http://purl.org/dc/elements/1.1/'
       xmlns:dcterms="http://purl.org/dc/terms/"
       xmlns:gal='http://norman.walsh.name/rdf/gallery#'>
<info>
<title>Not in RDF</title>
<volumenum>7</volumenum>
<issuenum>54</issuenum>
<pubdate>2004-04-02T06:03:00-05:00</pubdate>
<date>$Date: 2006-07-14 10:06:19 -0400 (Fri, 14 Jul 2006) $</date>
<author><personname>
<firstname>Norman</firstname><surname>Walsh</surname>
</personname></author>
<copyright><year>2004</year><holder>Norman Walsh</holder></copyright>
<abstract>
<para>There isn’t really a simple “not” operator in RDF. Nevertheless,
it’s useful, particularly for establishing default values.
So what can we do?</para>
</abstract>
</info>

<epigraph>
<attribution><personname><firstname>Homer J.</firstname>
<surname>Simpson</surname></personname></attribution>
<para xml:id='p1'>Perhaps, but let's not get bogged
down in semantics.</para>
</epigraph>

<para xml:id='p2'>The semantics of “not” (more accurately, of
“<link xlink:href="http://en.wikipedia.org/wiki/Complement_(set_theory)">complement</link>”)
turn out to be tricky to express in RDF, not just in
practice, but in theory as well. To understand why, we have to consider
world views. RDF
generally subscribes to an “open world” view, it accepts
that at any given time, the available statements represent limited
knowledge and that there’s more out there somewhere, it just hasn’t been
encountered yet.</para>

<para xml:id='p3'>The semantics of “not” require a closed world view: informally,
“not” asks if, in the known universe of information, is it the case that
some statement has not been made? Consider the following
statements:</para>

<orderedlist>
<listitem><simpara>Maple trees have green leaves.</simpara></listitem>
<listitem><simpara>Red Maple trees have red leaves.</simpara></listitem>
<listitem><simpara>Oak trees have green leaves.</simpara></listitem>
<listitem><simpara>Elm trees have brown bark.</simpara></listitem>
<listitem><simpara>Cherry trees have edible fruit.</simpara></listitem>
</orderedlist>

<para xml:id='p4'>Now ponder the question, “which trees do not have green leaves?”
If we take those five statements as the total universe of information,
the answer is elm and cherry trees, but anyone who’s seen an elm or cherry
tree knows that’s not true.</para>

<para xml:id='p5'>Nevertheless, it is sometimes useful to be able to express “not,” particularly
for establishing default values. So what can we do?</para>

<para xml:id='p6'>Before continuing, let’s pause to reflect on the fact that we can’t express
“not” in RDF any more than we can express “blue, italic text” in XML.
An XML document is just a tree of information items and an RDF document is just
a collection of statements. We need an application to interpret
our XML markup to make some text blue and italic. Similarly, we need an application
to interpret the semantics of our RDF statements. In this essay, we use
<link xlink:href="http://www.w3.org/2000/10/swap/doc/cwm"
><application>cwm</application></link><indexterm><primary>cwm</primary>
</indexterm> to interpret the RDF.</para>

<para xml:id='p7'>Let’s work through this with a concrete example. We know that most trees
have green leaves, so let’s try to make that the default. Here are our four
statements, recast as RDF using
<link xlink:href="http://www.w3.org/2000/10/swap/Primer">N3</link> notation.</para>

<example>
<title>/2004/04/02/examples/trees.n3</title>
<programlisting><inlinemediaobject>
<textobject>
<textdata fileref="examples/trees.n3"/>
</textobject>
</inlinemediaobject></programlisting>
</example>

<para xml:id='p8'>That describes five resources, says they’re each trees
as defined by our tree schema, and gives them some descriptive characteristics.
We want to find a way to say that if the color of a tree’s leaves is not
specified, assume they’re green.
</para>

<para xml:id='p9'>The answer to our “not” question lies in <application>cwm</application>’s
<function>log:semantics</function>, <function>log:includes</function>
and <function>log:notIncludes</function> operators:</para>

<variablelist>
<varlistentry>
<term><function>log:semantics</function></term>
<listitem>
<para xml:id='p10'>This operator loads an RDF document and returns
a <emphasis>formula</emphasis>, a closed set of statements.</para>
</listitem>
</varlistentry>

<varlistentry>
<term><function>log:includes</function></term>
<listitem>
<para xml:id='p11'>Allows us to ask if a particular statement occurs in a given formula.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term><function>log:notIncludes</function></term>
<listitem>
<para xml:id='p12'>Allows us to ask if a particular statement
<emphasis>does not</emphasis> occur in a given formula.
</para>
</listitem>
</varlistentry>
</variablelist>

<para xml:id='p13'>In other words, these operators allow us
to treat a single document as a closed world and that gives the “not”
operator something to sink its teeth into.</para>

<para xml:id='p14'>With these tools, we can construct a rule for
<application>cwm</application> to evaluate:</para>

<example>
<title>/2004/04/02/examples/rules.n3</title>
<programlistingco>
<areaspec>
  <area xml:id="log-forAll" coords="6"/>
  <area xml:id="log-semantics" coords="8"/>
  <area xml:id="log-includes" coords="9"/>
  <area xml:id="log-notIncludes" coords="10"/>
  <area xml:id="implies" coords="11"/>
</areaspec>
<programlisting><inlinemediaobject>
<textobject>
<textdata fileref="examples/rules.n3"/>
</textobject>
</inlinemediaobject></programlisting>
</programlistingco>
</example>

<para xml:id='p15'>What does this mean?</para>

<calloutlist>
<callout arearefs="log-forAll">
<simpara>First, we establish that <literal>:TREE</literal> is a
<link xlink:href="http://www.w3.org/2000/10/swap/doc/Rules">universal variable</link>
so that <application>cwm</application> will allow it to range over all the
subjects.</simpara>
</callout>
<callout arearefs="log-semantics">
<simpara>Next, we construct a formula from the statements in
<filename>trees.n3</filename>.</simpara>
</callout>
<callout arearefs="log-includes">
<simpara>At this point, <literal>:TREE</literal> will range over all the statements
in the formula. We want to select only the trees, so we limit the range to those
subjects that are <literal>tree:Tree</literal>s.
</simpara>
</callout>
<callout arearefs="log-notIncludes">
<simpara>Our real goal, of course, is to narrow the selection to only
those subjects that do not have a <literal>desc:leaves</literal>
property. That’s what <function>log:notIncludes</function> does.
In this statement, <literal>[]</literal> represents a blank
node; blank nodes only match if nothing else does. If we wanted to
make some other default, for example, for trees that had green leaves,
we could use the value <literal>"green"</literal> here instead.
In any event, this statements limits <literal>:TREE</literal> to range
over only those subjects that do not have a <literal>tree:leaves</literal> property
that has a value.</simpara>
</callout>
<callout arearefs="implies">
<simpara>Finally, we end with an implication. For any subject that is selected,
we give it the property <literal>tree:leaves</literal> with the value “green”.
</simpara>
</callout>
</calloutlist>

<para xml:id='p16'>Now we just have to get <application>cwm</application> to apply our rule:</para>

<programlisting>cwm --n3 trees.n3 rules.n3 --think --data &gt; output.n3</programlisting>

<para xml:id='p17'>And voilá:</para>

<example>
<title>/2004/04/02/examples/output.n3</title>
<programlisting><inlinemediaobject>
<textobject>
<textdata fileref="examples/output.n3"/>
</textobject>
</inlinemediaobject></programlisting>
</example>

<para xml:id='p18'>Our elm and cherry trees have grown green leaves!</para>

<para xml:id='p19'>Credit where it’s due: several folks on
<link xlink:href="irc://irc.freenode.net/rdfig"/>, particularly
<personname><firstname>Dan</firstname>
<surname>Connolly</surname></personname>, patiently
<link xlink:href="http://www.ilrt.bris.ac.uk/discovery/chatlogs/rdfig/2004-03-26#T20-58-44"
>talked me through this</link> the other day.
I think Dan pointed me to
<link xlink:href="http://www.w3.org/2000/10/swap/doc/Reach">the answer</link> once before,
but I didn’t grok it at the time.</para>

</essay>
