{"id":56,"date":"2015-10-09T18:07:22","date_gmt":"2015-10-09T18:07:22","guid":{"rendered":"http:\/\/cors1601.unl.edu\/cdrhdev\/?p=56"},"modified":"2017-04-18T14:27:04","modified_gmt":"2017-04-18T14:27:04","slug":"rdf-sparql-3","status":"publish","type":"post","link":"https:\/\/cdrhdev.unl.edu\/log\/2015\/rdf-sparql-3\/","title":{"rendered":"Figuring Out RDF and SPARQL: Part III Some Queries"},"content":{"rendered":"<p>[Originally posted by Jessica Dussault, October 9, 2015 on Github Pages]<\/p>\n<p>In <a href=\"https:\/\/cdrhdev.unl.edu\/log\/2015\/rdf-sparql-2\/\">part II<\/a> of this series, we learned how to run a basic query on some RDF. In this part, I\u2019ll be explaining some of the queries that we are running behind the scenes to power the <a href=\"http:\/\/earlywashingtondc.org\/\">O Say Can You See website<\/a>. This post needs a theme, and one of the better known individuals from the project is Francis Scott Key, so let\u2019s all welcome him to the stage.<\/p>\n<figure id=\"attachment_157\" aria-describedby=\"caption-attachment-157\" style=\"width: 288px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-157\" src=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/key_portrait.jpg\" alt=\"Painting of Francis Scott Key\" width=\"288\" height=\"370\" srcset=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/key_portrait.jpg 288w, https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/key_portrait-234x300.jpg 234w\" sizes=\"auto, (max-width: 288px) 100vw, 288px\" \/><figcaption id=\"caption-attachment-157\" class=\"wp-caption-text\">Francis Scott Key:<br \/>A slave owner who represented slaves in court. He is probably better known for penning the words now used in the Star Spangled Banner<\/figcaption><\/figure>\n<p><!-- source of above image: \"Francis Scott Key by Joseph Wood c1825\" by attributed to Joseph Wood (1778-1830) - [1]. Licensed under Public Domain via Wikimedia Commons - https:\/\/commons.wikimedia.org\/wiki\/File:Francis_Scott_Key_by_Joseph_Wood_c1825.jpg#\/media\/File:Francis_Scott_Key_by_Joseph_Wood_c1825.jpg --><\/p>\n<h2 id=\"o-say-can-you-see-early-washington-dc-law--family\">O Say Can You See: Early Washington, D.C., Law &amp; Family<\/h2>\n<p>The O Say Can You See (OSCYS) project explores relationships between individuals involved in legal proceedings in Washington, D.C. from 1800 to 1862. Legal documents like summonses, minute books, verdicts, and depositions were located, digitized, and marked up. From those documents, relationships between individuals were gathered, described in a TTL file, and defined in an OWL file.<\/p>\n<p>We use SPARQL on the TTL and OWL files to power a few features that show up for each person in OSCYS. On an individual\u2019s page, we list all of their immediate relationships and link to that person\u2019s network visualization. The visualization has not only links to other people, but the type of link. If you are less interested in a specific person and more interested in a general type of relationship, then head to the \u201cpeople\u201d page where the types of connections (spouse of, attorney for, etc) between all the individuals in OSCYS are browsable. Additionally, there is a search feature that allows you to look at how many ways the attorneys are related. All of this information is being powered by RDF. I\u2019ve explained the queries that I used to get the information below, if you would like to jump straight to the explanations. However, a little background information about how our metadata expert, Laura Weakly, encoded the relationships is probably the best place to start!<\/p>\n<ul>\n<li><a href=\"#background\">Background: OWL and TTL<\/a><\/li>\n<li><a href=\"#immediate\">Direct Connections<\/a><\/li>\n<li><a href=\"#visualization\">Network Visualization<\/a><\/li>\n<li><a href=\"#connections\">Connection Types<\/a><\/li>\n<li><a href=\"#attorneys\">Attorney Search<\/a><\/li>\n<\/ul>\n<h2 id=\"background\">Background<\/h2>\n<p>Much of the information about each person is described in a TEI file (available here: <a href=\"http:\/\/cdrhsites.unl.edu\/data\/projects\/oscys\/tei\/oscys.persons.xml\">caution, large file<\/a>). However, information specific to the relationships between individuals are stored in a <a href=\"http:\/\/earlywashingtondc.org\/relationships.ttl\">turtle file<\/a> with this type of setup:<\/p>\n<div class=\"highlighter-rouge\">\n<pre class=\"highlight\"><code>person 1   &gt;   attorney of   &gt;   person 2\r\nperson 2   &gt;   client of     &gt;   person 1\r\n<\/code><\/pre>\n<\/div>\n<p>More information about each relationship is stored in the <a href=\"http:\/\/earlywashingtondc.org\/ontology.owl\">OWL file<\/a>. The owl file says:<\/p>\n<div class=\"highlighter-rouge\">\n<pre class=\"highlight\"><code>attorney of   &gt;   inverse of   &gt;   client of\r\nattorney of   &gt;   type         &gt;   legal relationship\r\nclient of     &gt;   inverse of   &gt;   attorney of\r\nclient of     &gt;   type         &gt;   legal relationship\r\n<\/code><\/pre>\n<\/div>\n<p>Using a combination of the two documents, we can now start to answer some questions.<\/p>\n<ul>\n<li>Who is a given person related to?<\/li>\n<li>Who is related to someone who is related to a given person?<\/li>\n<li>Which people have any type of family relationships?<\/li>\n<li>Which people are the attorneys of someone else?<\/li>\n<\/ul>\n<p>This is what a typical query setup looks like for OSCYS:<\/p>\n<figure class=\"highlight\">\n<pre><code class=\"language-sparql\" data-lang=\"sparql\">PREFIX osrdf: &lt;http:\/\/earlywashingtondc.org\/rdf\/oscys.relationships#&gt;\r\nPREFIX oscys: &lt;http:\/\/earlywashingtondc.org\/rdf\/oscys.objectproperties.owl#&gt;\r\n\r\nSELECT *\r\nFROM &lt;http:\/\/earlywashingtondc.org\/rdf\/oscys.relationships.ttl&gt;\r\nWHERE {\r\n  \/\/ query goes here\r\n}<\/code><\/pre>\n<\/figure>\n<p>This should look mostly familiar, if you were following along at home with part II, except for the PREFIX declarations at the top. The prefix is a tool to make things more human readable. It\u2019s much nicer to get a result that says <code class=\"highlighter-rouge\">osrdf:per.000001<\/code> rather than one that says <code class=\"highlighter-rouge\">&lt;http:\/\/earlywashingtondc.org\/rdf\/oscys.relationships#per.000001&gt;<\/code>.<\/p>\n<p>If you want to try any of these queries on your own, feel free to visit <a href=\"http:\/\/sparql.org\/sparql.html\">the sparqler<\/a> and paste these queries in (ignore the target graph URI field). Except in a few cases, the above generic setup should be sufficient if you simply paste in the \u201cWHERE\u201d portion of the query.<\/p>\n<figure id=\"attachment_158\" aria-describedby=\"caption-attachment-158\" style=\"width: 1352px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-158\" src=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/network_rollover_1.png\" alt=\"Somewhat abstract view of a visualization showing the connections between points.\" width=\"1352\" height=\"334\" srcset=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/network_rollover_1.png 1352w, https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/network_rollover_1-300x74.png 300w, https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/network_rollover_1-768x190.png 768w, https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/network_rollover_1-1024x253.png 1024w, https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/network_rollover_1-1280x316.png 1280w\" sizes=\"auto, (max-width: 1352px) 100vw, 1352px\" \/><figcaption id=\"caption-attachment-158\" class=\"wp-caption-text\">Now we can create cool stuff like this visualization! Karin made art for the home page of OSCYS with real data from the project.<\/figcaption><\/figure>\n<h2 id=\"connections\">Direct Connections<\/h2>\n<p>Alright, let\u2019s take a look at probably the simplest SPARQL query behind the scenes of the OSCYS site. Each person\u2019s page has a list of their immediate relationships which is drawn from the TTL. <a href=\"http:\/\/earlywashingtondc.org\/people\/per.000001\">Francis Scott Key\u2019s page<\/a> looks something like this:<\/p>\n<div class=\"img_caption\">\n<figure id=\"attachment_162\" aria-describedby=\"caption-attachment-162\" style=\"width: 364px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-162\" src=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/immediate_relationships.jpg\" alt=\"F.S. Key's relationships\" width=\"364\" height=\"424\" srcset=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/immediate_relationships.jpg 364w, https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/immediate_relationships-258x300.jpg 258w\" sizes=\"auto, (max-width: 364px) 100vw, 364px\" \/><figcaption id=\"caption-attachment-162\" class=\"wp-caption-text\">Francis did a lot of attorneying, so it would have taken a much larger screenshot to get past &#8220;Attorney Against&#8221; to the miles of &#8220;Attorney For&#8221;s.<\/figcaption><\/figure>\n<\/div>\n<p>This is the query used to generate the list on <a href=\"http:\/\/earlywashingtondc.org\/people\/per.000001\">Francis Scott Key\u2019s page<\/a>.<\/p>\n<figure class=\"highlight\">\n<pre><code class=\"language-sparql\" data-lang=\"sparql\">PREFIX osrdf: &lt;http:\/\/earlywashingtondc.org\/rdf\/oscys.relationships#&gt;\r\nPREFIX oscys: &lt;http:\/\/earlywashingtondc.org\/rdf\/oscys.objectproperties.owl#&gt;\r\n\r\nSELECT *\r\nFROM &lt;http:\/\/earlywashingtondc.org\/rdf\/oscys.relationships.ttl&gt;\r\nWHERE {\r\n  osrdf:per.000001 ?rel1 ?per1 .\r\n  ?per1 oscys:fullName ?name1\r\n}\r\nORDER BY ?rel1 ?name1<\/code><\/pre>\n<\/figure>\n<p>This will return results look something like the following, but in json, which is then manipulated into HTML.<\/p>\n<table>\n<thead>\n<tr>\n<th>rel1<\/th>\n<th>per1<\/th>\n<th>name1<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>oscys:attorneyAgainst<\/td>\n<td>osrdf:per.001177<\/td>\n<td>\u201cWhiting, Elizabeth\u201d<\/td>\n<\/tr>\n<tr>\n<td>oscys:attorneyFor<\/td>\n<td>osrdf:per.000474<\/td>\n<td>\u201cHumphreys, Wood\u201d<\/td>\n<\/tr>\n<tr>\n<td>oscys:attorneyFor<\/td>\n<td>osrdf:per.000002<\/td>\n<td>\u201cBen\u201d<\/td>\n<\/tr>\n<tr>\n<td>oscys:opposingAttorneys<\/td>\n<td>osrdf:per.000584<\/td>\n<td>\u201cBussard, William\u201d<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>By default, when you view Key\u2019s page, you are presented with the results in HTML along with Key\u2019s other information. However, on any of the people pages in OSCYS you can add <code class=\"highlighter-rouge\">.xml<\/code> or <code class=\"highlighter-rouge\">.json<\/code> to the end of the URLs to view two different formats. This is how you would see some different formats for Francis Scott Key\u2019s query results:<\/p>\n<ul>\n<li><a href=\"http:\/\/earlywashingtondc.org\/people\/per.000001.xml\">xml results for Key\u2019s immediate connections<\/a><\/li>\n<li><a href=\"http:\/\/earlywashingtondc.org\/people\/per.000001.json\">json results for Key\u2019s immediate connections<\/a><\/li>\n<\/ul>\n<p>So how does the SPARQL query work? Let\u2019s take a closer look at the query and walk through it.<\/p>\n<figure class=\"highlight\">\n<pre><code class=\"language-sparql\" data-lang=\"sparql\">WHERE {\r\n  osrdf:per.000001 ?rel1 ?per1 .\r\n  ?per1 oscys:fullName ?name1\r\n}<\/code><\/pre>\n<\/figure>\n<p>In the first line we plug in Key\u2019s id, per.000001, in such a way that SPARQL knows we are using an entity from our rdf document. Remember that we defined the prefix <code class=\"highlighter-rouge\">osrdf<\/code> at the top of our query to allow that style of shorthand. The first line also asks SPARQL to find anything related to Key\u2019s identity in the RDF file.<code class=\"highlighter-rouge\">?rel1<\/code> = the relationship and <code class=\"highlighter-rouge\">?per1<\/code> = the person (or thing) related to Key.<\/p>\n<p>In the second line, we use that same variable, <code class=\"highlighter-rouge\">?per1<\/code> again. Now we\u2019re looking for the fullNames of any of the <code class=\"highlighter-rouge\">?per1<\/code> items found on the first line of the query, and anything that matches becomes <code class=\"highlighter-rouge\">?name1<\/code>. If <code class=\"highlighter-rouge\">?per1<\/code> doesn\u2019t have a fullName, then it is omitted from the results which is probably okay \u2013 we\u2019re mostly interested in people who can be identified at least with a name.<\/p>\n<div class=\"img_caption\">\n<figure id=\"attachment_160\" aria-describedby=\"caption-attachment-160\" style=\"width: 400px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-160\" src=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/key_flag.jpg\" alt=\"Francis Scott Key and friends watching Fort McHenry's bombardment\" width=\"400\" height=\"341\" srcset=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/key_flag.jpg 400w, https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/key_flag-300x256.jpg 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><figcaption id=\"caption-attachment-160\" class=\"wp-caption-text\">An illustration that includes some people with whom Francis Scott Key presumably had a direct connection.<\/figcaption><\/figure>\n<p><!-- Source of image: http:\/\/www.prattlibrary.org\/uploadedImages\/www\/books_and_media\/blog\/Pratt_Library_Blog\/Key_Flag.jpg --><\/p>\n<\/div>\n<h2 id=\"visualization\">Network Visualizations<\/h2>\n<p>In addition to the direct connections, each person has a visualization that looks at their immediate social network. A person knows a person who knows a person. That is to say, Mary Bell below is the spouse of Daniel Bell who is the client of Gilbert L Giberson. Mary knows several individuals who in turn seem to have quite a few connections. This is a screenshot of part of her visualization <a href=\"http:\/\/earlywashingtondc.org\/people\/network\/per.001253\">here<\/a>.<\/p>\n<div class=\"img_caption\">\n<figure id=\"attachment_163\" aria-describedby=\"caption-attachment-163\" style=\"width: 679px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-163\" src=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/mary_bell.jpg\" alt=\"Visualization depicting the network of Mary Bell\" width=\"679\" height=\"607\" srcset=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/mary_bell.jpg 679w, https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/mary_bell-300x268.jpg 300w\" sizes=\"auto, (max-width: 679px) 100vw, 679px\" \/><figcaption id=\"caption-attachment-163\" class=\"wp-caption-text\">Mary&#8217;s network has larger clumps around people she is connected to who are themselves well connected.<\/figcaption><\/figure>\n<\/div>\n<p>Some of the people have such large networks that though the SPARQL queries are fast, the JavaScript making the visualization gets a bit sluggish. Either way, it\u2019s a bit overwhelming to take in at a glance, so for that reason we\u2019re going to be using Mary Bell as our query subject instead of Francis Scott Key.<\/p>\n<div class=\"img_caption\">\n<figure id=\"attachment_165\" aria-describedby=\"caption-attachment-165\" style=\"width: 681px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-165\" src=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/key_overwhelming.jpg\" alt=\"Visualization depicting Francis Scott Key's network. It is so packed with names as to be almost unreadable. \" width=\"681\" height=\"611\" srcset=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/key_overwhelming.jpg 681w, https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/key_overwhelming-300x269.jpg 300w\" sizes=\"auto, (max-width: 681px) 100vw, 681px\" \/><figcaption id=\"caption-attachment-165\" class=\"wp-caption-text\">Francis Scott Key was involved in an awful lot of cases in OSCYS, so he ended up knowing just about everybody.<\/figcaption><\/figure>\n<\/div>\n<p>For the visualizations we ask \u201cfind the people Bell knows and the people that THOSE people know. While you\u2019re at it, get the type of the relationships and the names of all the people.\u201d As with the direct relationships, you can add <code class=\"highlighter-rouge\">.xml<\/code> or <code class=\"highlighter-rouge\">.json<\/code> to the end of that URL to view the results used to create the visualization (<a href=\"http:\/\/earlywashingtondc.org\/people\/network\/per.000204.xml\">xml<\/a>) (<a href=\"http:\/\/earlywashingtondc.org\/people\/network\/per.000204.json\">json<\/a>).<\/p>\n<p>There\u2019s a lot required from this query. We want the names and the people once removed from Bell. We also want to know the type of each relationship so that we can make family ties distinct from, say, legal ties.<\/p>\n<p>The RDF file can tell you that person A is related to person B as \u201cparentOf\u201d but it does not have the right information to tell you that \u201cparentOf\u201d is a family relationship. That information is stored in the OWL file. So how do we get around that problem? The first clue is that our OWL file can be queried with SPARQL the same way that we have been querying the RDF. Let\u2019s try grabbing all the relationships and their corresponding type. Note that there is an extra prefix at the top in order to get at \u201csubPropertyOf\u201d.<\/p>\n<figure class=\"highlight\">\n<pre><code class=\"language-sparql\" data-lang=\"sparql\">PREFIX oscys: &lt;http:\/\/earlywashingtondc.org\/rdf\/oscys.objectproperties.owl#&gt;\r\nPREFIX rdfs: &lt;http:\/\/www.w3.org\/2000\/01\/rdf-schema#&gt;\r\n\r\nSELECT *\r\nFROM &lt;http:\/\/earlywashingtondc.org\/rdf\/oscys.objectproperties.owl&gt;\r\nWHERE {\r\n  ?relationship rdfs:subPropertyOf ?relationshipType\r\n}<\/code><\/pre>\n<\/figure>\n<p>The results we get are probably somewhat predictable. On the left is a list of specific relationships, on the right is the category that they belong to.<\/p>\n<table>\n<thead>\n<tr>\n<th>?relationship<\/th>\n<th>?relationshipType<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>oscys:deponentOf<\/td>\n<td>oscys:legalRelationship<\/td>\n<\/tr>\n<tr>\n<td>oscys:witnessFor<\/td>\n<td>oscys:legalRelationship<\/td>\n<\/tr>\n<tr>\n<td>oscys:neighborOf<\/td>\n<td>oscys:socialRelationship<\/td>\n<\/tr>\n<tr>\n<td>oscys:siblingOf<\/td>\n<td>oscys:familyRelationship<\/td>\n<\/tr>\n<tr>\n<td>oscys:indenturedTo<\/td>\n<td>oscys:workRelationship<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Great, now we just need to combine it with results from the RDF so that we can attach information that says \u201cparentOf\u201d is a family relationship while \u201cattorneyOf\u201d is a legal one, etc. We can do that by combining the graphs with two FROM clauses. Here\u2019s the whole shebang:<\/p>\n<figure class=\"highlight\">\n<pre><code class=\"language-sparql\" data-lang=\"sparql\">PREFIX osrdf: &lt;http:\/\/earlywashingtondc.org\/rdf\/oscys.relationships#&gt;\r\nPREFIX oscys: &lt;http:\/\/earlywashingtondc.org\/rdf\/oscys.objectproperties.owl#&gt;\r\nPREFIX rdfs: &lt;http:\/\/www.w3.org\/2000\/01\/rdf-schema#&gt;\r\n\r\nSELECT *\r\nFROM &lt;http:\/\/earlywashingtondc.org\/rdf\/oscys.relationships.ttl&gt;\r\nFROM &lt;http:\/\/earlywashingtondc.org\/rdf\/oscys.objectproperties.owl&gt;\r\nWHERE {\r\n  osrdf:per.001253 ?rel01 ?per1 .\r\n  OPTIONAL { \r\n    ?per1 ?rel12 ?per2 .\r\n    ?per2 oscys:fullName ?name2 .\r\n  }\r\n  osrdf:per.001253 oscys:fullName ?name0 .\r\n  ?per1 oscys:fullName ?name1 .\r\n  OPTIONAL { ?rel01 rdfs:subPropertyOf ?rel01type } .\r\n  OPTIONAL { ?rel12 rdfs:subPropertyOf ?rel12type } .\r\n  FILTER ( ?per2 != osrdf:per.001253 )\r\n}<\/code><\/pre>\n<\/figure>\n<p>The first line of the WHERE clause should look familiar. <code class=\"highlighter-rouge\">?osrdf:per.001253 ?rel01 ?per1<\/code> is just getting a list of everything related in some way to Francis Scott Key. Then we come across some optional triples.<\/p>\n<figure class=\"highlight\">\n<pre><code class=\"language-sparql\" data-lang=\"sparql\">OPTIONAL {\r\n  ?per1 ?rel12 ?per2 .\r\n  ?per2 oscys:fullName ?name2 .\r\n}<\/code><\/pre>\n<\/figure>\n<p>Using our findings for the <code class=\"highlighter-rouge\">?per1<\/code> variable, we look to find anything related and its fullName. An example would be \u201cMary Bell knows Person A and Person A knows Person B whose name is John Marbury.\u201d The second leg is made optional because it is possible that Bell may be connected to somebody who has no other relationships of their own. Though unlikely, should this situation come up, if the Person A to Person B triple were not optional, it would omit more solitary individuals from the results.<\/p>\n<p>Now it\u2019s time for our new OWL query for the relationship types make an appearance, also wrapped in an OPTIONAL. The subPropertyOf queries are optional because it\u2019s possible that a connection has been added for two individuals that hasn\u2019t been defined in the OWL file (perhaps a misspelling or a first of its kind relationship). AttrneyOf [sic] and personalPhysicianOf might not be yet valid according to the OWL file, but we want to make sure that these orphaned relationships still appear in the results or else we may not notice that something needs to be resolved.<\/p>\n<figure class=\"highlight\">\n<pre><code class=\"language-sparql\" data-lang=\"sparql\">OPTIONAL { ?rel01 rdfs:subPropertyOf ?rel01type } .\r\nOPTIONAL { ?rel12 rdfs:subPropertyOf ?rel12type } .<\/code><\/pre>\n<\/figure>\n<p>The final line of the query is a FILTER. The filter is simply avoiding some duplication. If Person A knows Person B then we can probably assume that Person B knows Person A, too. We don\u2019t need to return Person A in the search results. If that\u2019s the kind of thing that you would like to know, then just remove the FILTER line to view all the results.<\/p>\n<p>Because the type of relationship is returned as part of the query, now you can do things like this!<\/p>\n<div class=\"img_caption\">\n<figure id=\"attachment_166\" aria-describedby=\"caption-attachment-166\" style=\"width: 327px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-166\" src=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/bell_family.jpg\" alt=\"Part of a visualization showing family ties in Bell's network\" width=\"327\" height=\"329\" srcset=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/bell_family.jpg 327w, https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/bell_family-150x150.jpg 150w, https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/bell_family-298x300.jpg 298w\" sizes=\"auto, (max-width: 327px) 100vw, 327px\" \/><figcaption id=\"caption-attachment-166\" class=\"wp-caption-text\">These are some of the family ties highlighted in Bell&#8217;s network. Most of her connections are legal ones, so it is interesting to look at family and social relationships amongst the busy mishmash of the lines.<\/figcaption><\/figure>\n<\/div>\n<h2 id=\"connection-typesa-nameconnectionsa\">Connection Types<a name=\"connections\"><\/a><\/h2>\n<p>You can also look through all of the types of relationships in OSCYS, which is a handy tool if you\u2019re particularly interested in families, or slaveholders, or judges, or whatever sort of connection suits your curiosity. The search is <a href=\"http:\/\/earlywashingtondc.org\/people\/connection_type\">here<\/a>. This is example of what the list of people <a href=\"http:\/\/earlywashingtondc.org\/people\/connection_type?utf8=%E2%9C%93&amp;type=deposedBy&amp;commit=Go%21\">deposed by<\/a> other people looks like:<\/p>\n<div class=\"img_caption\">\n<figure id=\"attachment_167\" aria-describedby=\"caption-attachment-167\" style=\"width: 853px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-167\" src=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/deposed_by.jpg\" alt=\"People deposed by other people\" width=\"853\" height=\"230\" srcset=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/deposed_by.jpg 853w, https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/deposed_by-300x81.jpg 300w, https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/deposed_by-768x207.jpg 768w\" sizes=\"auto, (max-width: 853px) 100vw, 853px\" \/><figcaption id=\"caption-attachment-167\" class=\"wp-caption-text\">These people&#8217;s regimes were all overthrown. I find it is more interesting to picture that definition than someone testifying in court.<\/figcaption><\/figure>\n<\/div>\n<p>This is another pretty straightforward query. No need to consult an OWL file this time. Just ask \u201cfind a person connected to another person with that relationship.\u201d<\/p>\n<figure class=\"highlight\">\n<pre><code class=\"language-sparql\" data-lang=\"sparql\">WHERE { ?per1 oscys:deposedBy ?per2 .\r\n        ?per1 oscys:fullName ?per1name .\r\n        ?per2 oscys:fullName ?per2name .\r\n      }<\/code><\/pre>\n<\/figure>\n<h2 id=\"attorneys\">Attorney Search<\/h2>\n<p>As attorneys rubbed elbows quite a lot, we built a tool to explore the various interactions between attorneys. This search looks for immediate connections, attorneys who might have been two removed from each other, and more, all the way up to four people removed. Some attorneys may not have ever had vague connections with each other, even after several jumps through a social network, which is in itself noteworthy. Try it out yourself by visiting the <a href=\"http:\/\/earlywashingtondc.org\/people\/relationships\">Attorney Relationship Finder of Science<\/a>.<\/p>\n<p>For the purpose of this blogpost, we\u2019ll be looking at the ways that Francis Scott Key worked with Elias Boudinot Caldwell who was <a href=\"http:\/\/www.jstor.org\/stable\/40067168\">an attorney, clerk, War of 1812 vet, and Presbyterian minister<\/a>.<\/p>\n<div class=\"img_caption\">\n<figure id=\"attachment_164\" aria-describedby=\"caption-attachment-164\" style=\"width: 234px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-164\" src=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/caldwell.jpg\" alt=\"Elias Boudinot Caldwell\" width=\"234\" height=\"288\" \/><figcaption id=\"caption-attachment-164\" class=\"wp-caption-text\">Mr. Caldwell was described by his granddaughter as having &#8220;<a href=\"https:\/\/www.jstor.org\/stable\/40067168?seq=1#page_scan_tab_contents\">good blood, brains, and enough wealth for those days.<\/a>&#8220;<\/figcaption><\/figure>\n<p><!-- Source of image: http:\/\/www.jstor.org\/stable\/40067168 --><\/p>\n<\/div>\n<p>This is just a small snippet of the ways in which <a href=\"http:\/\/earlywashingtondc.org\/people\/relationships?utf8=%E2%9C%93&amp;per1id=per.000001&amp;per2id=per.000194&amp;commit=Find+Relationships\">Francis Scott Key knew Elias Boudinot Caldwell<\/a>:<\/p>\n<p><!-- Source of image: https:\/\/upload.wikimedia.org\/wikipedia\/commons\/2\/2e\/John_C_Calhoun_by_Mathew_Brady%2C_1849.jpg --><!-- Source of relative \/ entertainer of Calhoun: Sketch of Elias Boudinot Caldwell: Reprint from \"American Monthly Magzine\" Allen C. Clark Records of the Columbia Historical Society, Washington, D.C. Vol. 24 (1922), pp. 204-213 Published by: Historical Society of Washington, D.C. Stable URL: http:\/\/www.jstor.org\/stable\/40067168 Page Count: 11 --><\/p>\n<div class=\"img_caption\">\n<figure id=\"attachment_161\" aria-describedby=\"caption-attachment-161\" style=\"width: 1184px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-161\" src=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/elias_caldwell.jpg\" alt=\"Immediately and not so immediately relationships\" width=\"1184\" height=\"449\" srcset=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/elias_caldwell.jpg 1184w, https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/elias_caldwell-300x114.jpg 300w, https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/elias_caldwell-768x291.jpg 768w, https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/elias_caldwell-1024x388.jpg 1024w\" sizes=\"auto, (max-width: 1184px) 100vw, 1184px\" \/><figcaption id=\"caption-attachment-161\" class=\"wp-caption-text\">This goes all the way up to four people apart. I wouldn&#8217;t recommend trying it on a mobile screen.<\/figcaption><\/figure>\n<\/div>\n<p>I struggled a bit when trying to put together this aspect of the relationship querying. What I wanted was \u201cgiven one node, find any possible connections to another given node.\u201d I don\u2019t know of a way to do this with SPARQL, and I doubt that it is even possible. If there is a way, please drop me a line! Undaunted, I began work on a mega-query that would approximate finding any possible connection by checking for immediate connections, then if that was a bust, trying to find a two removed connection, etc, all the way up to four people removed. My mega-query was quickly revealed to be a horrible monster.<\/p>\n<div class=\"img_caption\">\n<figure id=\"attachment_168\" aria-describedby=\"caption-attachment-168\" style=\"width: 368px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-168\" src=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/The_Persistence_of_Memory.jpg\" alt=\"Salvador Dali's Persistence of Memory\" width=\"368\" height=\"271\" srcset=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/The_Persistence_of_Memory.jpg 368w, https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/The_Persistence_of_Memory-300x221.jpg 300w\" sizes=\"auto, (max-width: 368px) 100vw, 368px\" \/><figcaption id=\"caption-attachment-168\" class=\"wp-caption-text\">This is how that query made me feel.<\/figcaption><\/figure>\n<\/div>\n<p>Somewhere in the layers and layers of OPTIONAL clauses, I started losing confidence that my query was actually doing what I wanted it to do. I also wasn\u2019t sure if I cared anymore. It took an eternity to run (and by that I mean like 8 minutes). Time to try something else.<\/p>\n<p>Instead of one big query with lots of finicky moving pieces, I decided to send four queries. It\u2019s not perfect, I\u2019m not super excited about it, but it is working so I can\u2019t complain too much. The direct relationship query is the simplest, as you might expect. It\u2019s nearly identical to a query already being used on an individual\u2019s page except that in this case, I wanted both a specific starting person and a specific ending person.<\/p>\n<figure class=\"highlight\">\n<pre><code class=\"language-sparql\" data-lang=\"sparql\">WHERE {\r\n  osrdf:per.000001 ?rel osrdf:per.000194 .\r\n  osrdf:per.000001 oscys:fullName ?name1 .\r\n  osrdf:per.000194 oscys:fullName ?name2 \r\n}<\/code><\/pre>\n<\/figure>\n<p>Besides the above query for direct relationships, I send queries for two and three connections removed, and then eventually end up with the query for four connections removed. You\u2019ll notice that I\u2019m filtering out some of the relationships. This is because if Person A knows person B, there\u2019s a pretty good chance person B knows person A, too. It seems silly to have the results show up in big chains of the same people.<\/p>\n<figure class=\"highlight\">\n<pre><code class=\"language-sparql\" data-lang=\"sparql\">WHERE {\r\n  BIND(osrdf:per.000001 AS ?per1) .\r\n  BIND(osrdf:per.000194 AS ?goal) .\r\n  ?per1 ?rel12 ?per2 .\r\n  ?per2 ?rel23 ?per3 .\r\n  ?per3 ?rel34 ?per4 .\r\n  ?per4 ?rel4g ?goal .\r\n  ?per1 oscys:fullName ?name1 .\r\n  ?per2 oscys:fullName ?name2 .\r\n  ?per3 oscys:fullName ?name3 .\r\n  ?per4 oscys:fullName ?name4 .\r\n  ?goal oscys:fullName ?nameg .\r\n  FILTER ( ?per3 != ?per1 ) .\r\n  FILTER (?per2 != ?per4 )\r\n  FILTER (?per3 != ?goal )\r\n}<\/code><\/pre>\n<\/figure>\n<p>At the top I use BIND to tell SPARQL to use <code class=\"highlighter-rouge\">per.000001<\/code> as <code class=\"highlighter-rouge\">?per1<\/code> and <code class=\"highlighter-rouge\">per.000194<\/code> as <code class=\"highlighter-rouge\">?goal<\/code>. Makes the query look a bit more readable, in my opinion. After that it\u2019s pretty straightforward \u2013 find a person related to Francis Scott Key, then find people that those people know, then find people that those people know, etc. I\u2019m also returning their names which means that unnamed individuals and their connections will not show up in the results.<\/p>\n<div class=\"img_caption\">\n<figure id=\"attachment_156\" aria-describedby=\"caption-attachment-156\" style=\"width: 216px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-156\" src=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/calhoun.jpg\" alt=\"John C. Calhoun\" width=\"216\" height=\"308\" srcset=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/calhoun.jpg 216w, https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/calhoun-210x300.jpg 210w\" sizes=\"auto, (max-width: 216px) 100vw, 216px\" \/><figcaption id=\"caption-attachment-156\" class=\"wp-caption-text\">Though this is a bit outside of the scope of OSCYS, Elias Boudinot Caldwell was related to John C. Calhoun (the &#8216;C&#8217; stands for &#8216;Caldwell&#8217;), and <a href=\"https:\/\/www.jstor.org\/stable\/40067168?seq=1#page_scan_tab_contents\">entertained Calhoun for dinner on at least one occasion<\/a>. Calhoun was a war hawk, an inspiration to secessionists, and an adamant supporter of slavery, which may have added an interesting dynamic when Caldwell <a href=\"http:\/\/earlywashingtondc.org\/people\/per.000194\">defended several slaves in court<\/a>.<\/figcaption><\/figure>\n<\/div>\n<h2 id=\"the-future\">The Future<\/h2>\n<p>These are just a sampling of the types of questions we can be asking of our RDF data. We could potentially use it to construct family trees. Maybe there would be something interesting revealed if we looked at networks of people related to a specific case. If we marked up other aspects of the OSCYS data besides the people (like locations, cases, events), there might be even more fascinating connections to find.<\/p>\n<div class=\"img_caption\">\n<figure id=\"attachment_159\" aria-describedby=\"caption-attachment-159\" style=\"width: 466px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-159\" src=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/family.jpg\" alt=\"Visualization depicting groupings of families with no labels\" width=\"466\" height=\"461\" srcset=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/family.jpg 466w, https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2015\/10\/family-300x297.jpg 300w\" sizes=\"auto, (max-width: 466px) 100vw, 466px\" \/><figcaption id=\"caption-attachment-159\" class=\"wp-caption-text\">The above is an image taken from an early experiment to visualize family networks.<\/figcaption><\/figure>\n<\/div>\n<div class=\"img_caption\"><\/div>\n<div class=\"img_caption\">Additionally, something we haven\u2019t explored much is linking our data to external sources. We did go so far as to include oscys:sameAs in order to point at <a href=\"http:\/\/viaf.org\/\">Virtual International Authority Files<\/a>, for the very few individuals who have VIAF records.<\/div>\n<table>\n<thead>\n<tr>\n<th>?per<\/th>\n<th>?name<\/th>\n<th>?info<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>osrdf:per.000738<\/td>\n<td>Brent, William Leigh<\/td>\n<td>https:\/\/viaf.org\/viaf\/38728091<\/td>\n<\/tr>\n<tr>\n<td>osrdf:per.000004<\/td>\n<td>Cranch, William<\/td>\n<td>https:\/\/viaf.org\/viaf\/27114318<\/td>\n<\/tr>\n<tr>\n<td>osrdf:per.000003<\/td>\n<td>Brent, William<\/td>\n<td>https:\/\/viaf.org\/viaf\/18936621<\/td>\n<\/tr>\n<tr>\n<td>osrdf:per.000001<\/td>\n<td>Key, Francis Scott<\/td>\n<td>https:\/\/viaf.org\/viaf\/824500<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>That brings an end to a longwinded blogpost about the queries that are working behind the scenes for OSCYS. In the next installment, I\u2019ll be talking about how I chose to query RDF with Fuseki from the Ruby on Rails framework!<\/p>\n<ul>\n<li><a href=\"https:\/\/cdrhdev.unl.edu\/log\/2015\/rdf-sparql-1\/\">Figuring out RDF and SPARQL: Part I Triples<\/a><\/li>\n<li><a href=\"https:\/\/cdrhdev.unl.edu\/log\/2015\/rdf-sparql-2\/\">Figuring out RDF and SPARQL: Part II Getting Set Up<\/a><\/li>\n<li>Figuring out RDF and SPARQL: Part III Some Queries<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>[Originally posted by Jessica Dussault, October 9, 2015 on Github Pages] In part II of this series, we learned how to run a basic query on some RDF. In this part, I\u2019ll be explaining some of the queries that we are running behind the scenes to power the O Say Can You See website. This&hellip;<\/p>\n <a href=\"https:\/\/cdrhdev.unl.edu\/log\/2015\/rdf-sparql-3\/\" title=\"Figuring Out RDF and SPARQL: Part III Some Queries\" class=\"entry-more-link\"><span>Read More<\/span> <span class=\"screen-reader-text\">Figuring Out RDF and SPARQL: Part III Some Queries<\/span><\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"Layout":"1c","footnotes":""},"categories":[10,9],"tags":[14,12,13,15],"class_list":["entry","author-jdussault","post-56","post","type-post","status-publish","format-standard","category-introduction","category-linked-data","tag-owl","tag-rdf","tag-sparql","tag-ttl"],"_links":{"self":[{"href":"https:\/\/cdrhdev.unl.edu\/log\/wp-json\/wp\/v2\/posts\/56","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cdrhdev.unl.edu\/log\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cdrhdev.unl.edu\/log\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cdrhdev.unl.edu\/log\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cdrhdev.unl.edu\/log\/wp-json\/wp\/v2\/comments?post=56"}],"version-history":[{"count":10,"href":"https:\/\/cdrhdev.unl.edu\/log\/wp-json\/wp\/v2\/posts\/56\/revisions"}],"predecessor-version":[{"id":220,"href":"https:\/\/cdrhdev.unl.edu\/log\/wp-json\/wp\/v2\/posts\/56\/revisions\/220"}],"wp:attachment":[{"href":"https:\/\/cdrhdev.unl.edu\/log\/wp-json\/wp\/v2\/media?parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cdrhdev.unl.edu\/log\/wp-json\/wp\/v2\/categories?post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cdrhdev.unl.edu\/log\/wp-json\/wp\/v2\/tags?post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}