This section will guide you through extending the application created in Creating a Windows Forms Application To Access RDF Data Using The Virtuoso ADO.Net Provider so that it will dereference external IRIs.


In RDFDemo when the sparql endpoint is queried to get the description of the selected item it executes a query that is restricted to the local Northwind dataset. The query is something like

PREFIX nwind: <>
WHERE {?s a nwind:Customer}

If you examine the ExtendedStringHandler class you will see that the dataset clause, from <>, is hard coded. This means that when when the selected IRI is a link to an external data store, such as dbpedia, there is no matching data and an error is displayed. If the application is to be able to dereference external IRIs then the hard coded dataset clause needs to be removed and then we can use a Virtuoso extension to SPARQL, get:soft, that tells Virtuoso that it needs to go and look elsewhere for the graph. However, this will result in a loss of performance when exploring the local Northwind dataset. To minimize the impact on performance we will first query the local Northwind dataset and if there are no matching triples returned then we will use a more generic query that will look elsewhere for matching data.

Step 1 - Add the alternative query to the ExtendedString Class.

  • Open the RDFDemo project in Visual Studio

  • Open the ExtendedStringHandler class.

  • Remove DescribeCommand by removing the line

    StringBuilder DescribeCommand;

    and substitute the following:

    StringBuilder DescribeCommandSimple, DescribeCommandGeneral;
  • In the ExtendedStringHandler constructor the sparql query that was DescribeCommand becomes DescribeCommandSimple and we define a new query for DescribeCommandGeneral.

    DescribeCommandSimple = new StringBuilder("sparql select * from <> where {<" + iri.ToString() + "> ?p ?o}");       // Replace with your  URIQA DefaultHost setting
    DescribeCommandGeneral = new StringBuilder("sparql define get:soft " + '"'.ToString() + "soft" + '"'.ToString() + " select * from <" + iri.ToString() + "> where { <" + iri.ToString() + "> ?p ?o }");
  • The single describeCommand property needs to be replaced with the two new properties, DescribeCommandSimple and DescribeCommandGeneral

    public string describeCommandSimpleText
            return DescribeCommandSimple.ToString();
    public string describeCommandGeneralText
            return DescribeCommandGeneral.ToString();
  • Finally, the getDescribeData method needs changing to:

    public void getDescribeData()
        VirtuosoCommand myCommand = new VirtuosoCommand(this.describeCommandSimpleText, this.ParentConnection);
        VirtuosoDataAdapter myAdapter = new VirtuosoDataAdapter();
        myAdapter.SelectCommand = myCommand;
        // Tried the simple version if fails to get the data try
        // to look elsewhere.
        if (describeDataSet.DataTable1.Rows.Count == 0)
            myCommand.CommandText = describeCommandGeneralText;

Step 2 - Build and Run the Application

You will see the same starting form:

Figure 2.146. Build and Run the Application

Build and Run the Application

Select a Customer and then select the link to the City in dbpedia. This will now open up another window displaying information about the city from dbpedia. Be patient as it may take a little while to open.

Figure 2.147. Customer


Step 3 - Changing the Form Title

Notice that in displayData method that we look for a and create a title for the form from it.

foreach (DataRow row in table1.Rows)
  if (row[0].ToString() == "")
      StringBuilder title = new StringBuilder(row[1].ToString() + " details");
      label1.Text = title.ToString();

This worked well for the Northwind subjects but less well now we are getting data from other graphs. To change the title of the forms used to display the data:

  • Add an new member variable to hold the IRI that we exploring to the bock of member variables

    StringBuilder DescribeCommandSimple, DescribeCommandGeneral;
    VirtuosoConnection ParentConnection;
    List<Label> labelList = new List<Label>();
    List<TextBox> textBoxList = new List<TextBox>();
    DescribeDataSet describeDataSet = new DescribeDataSet();
    Boolean isIRI = false;
    SqlExtendedString ParentIRI;
  • Assign a value to ParentIRI in the constructor:

    public ExtendedStringHandler(SqlExtendedString iri, VirtuosoConnection parentConnection)
          ParentConnection = parentConnection;
          if (iri.IriType == SqlExtendedStringType.IRI)
              ParentIRI = iri;
              isIRI = true;
              DescribeCommandSimple = new StringBuilder("sparql select * from <> where {<" + iri.ToString() + "> ?p ?o}");      // Replace with your  URIQA DefaultHost setting
              DescribeCommandGeneral = new StringBuilder("sparql define get:soft " + '"'.ToString() + "soft" + '"'.ToString() + " select * from <" + iri.ToString() + "> where { <" + iri.ToString() + "> ?p ?o }");
  • Remove the existing foreach block that sets the form title and replace with the following lines:

    StringBuilder title = new StringBuilder(ParentIRI.ToString() + " details");
    label1.Text = title.ToString();
  • Build and run the application.

    Figure 2.148. Build and run the application

    Build and run the application

Next Steps

The application now allows you to explore data and follow links from your locally held data into the external web of data. Looking at the data displayed in the form it would be nice to make the labels for the properties more compact. The label is a very precise definition but for our purposes it would be clearer to label the property just population. In the next step will be to modify the application so that it displays more readable labels.