Saturday, December 5, 2015

SPARQL Guide for the Visual Basic 6 (VB6) Developer

Topics: , , , ,

SPARQL Guide for the Visual Basic 6 (VB6) Developer


This is the Visual Basic 6 variation of 's SPARQL guides for PHP, Perl, Python, JavaScript developers and my guide forClassic ASP developers. I wanted to share this because quite a number of developers (including me) are still using Visual Basic 6 and It could also be used for Linked Data Application development. Visual Basic is on the top 20 most popular programming languages according to TIOBE Index as at december 2015


A simple guide usable by any Visual Basic 6 developer seeking to exploit SPARQL without hassles.


SPARQL is a powerful query language, results serialization format, and an HTTP based data access protocol from the W3C. It provides a mechanism for accessing and integrating data across Deductive Database Systems (colloquially referred to as triple or quad stores in Semantic Web and Linked Data circles) -- database systems (or data spaces) that manage proposition oriented records in 3-tuple (triples) or 4-tuple (quads) form.


SPARQL queries are actually HTTP payloads (typically). Thus, using a RESTful client-server interaction pattern, you can dispatch calls to a SPARQL compliant data server and receive a payload for local processing e.g. local object binding re. Visual Basic 6 (vb6).

  • Determine which SPARQL endpoint you want to access e.g. DBpedia or a local Virtuoso instance (typically: http://localhost:8890/sparql).
  • If using Virtuoso, and you want to populate its quad store using SPARQL, assign "SPARQL_SPONGE" privileges to user "SPARQL" (this is basic control, more sophisticated WebID based ACLs are available for controlling SPARQL access).
  • Make sure MSXML parser is installed on the server or you can download from here.
  • Download VB-JSON: A Visual Basic 6 (VB6) JSON Parser Class Library.
  • In Visual Basic, create a new Standard EXE project. Create and add a new module, "module1.bas".
  • Add a project reference to Microsoft XML version 2.0, Microsoft scripting runtime, Microsoft ActiveX Data Objects library.
  • Unzip the VB-JSON file. Extract the following files: "json.bas", "cJSONScript.cls" and "cStringBuilder.cls". Add them to the Visual Basic project
  • Paste the following code in "module1.bas" file. See the inline comments for a description of how the code functions.
  • Save and run the Visual Basic project.
 Private m_SafeChar(0 To 255) As Boolean
 Sub main()
  Dim buf As New cStringBuilder

    Dim p As Object
  Dim result As String
  Dim url As New cStringBuilder
  Dim dsn As String
  Dim parameters As String
  Dim sparql As New cStringBuilder
  On Error Resume Next
  ' Setting Data Source Name (DSN)
  dsn = ""
  ' HTTP URL is constructed accordingly with JSON query results format in mind.
  parameters = "default-graph-uri=&should-sponge=&format=application%2Fsparql-results%2Bjson&timeout=0&debug=on"

  ' SPARQL Query
  ' List all the landlocked countries in Africa
  sparql.Append "PREFIX rdfs:  "
  sparql.Append "PREFIX type:  "
  sparql.Append "PREFIX prop:  "
  sparql.Append "SELECT ?country_name ?population ?ind1 "
  sparql.Append "WHERE { "
  sparql.Append "    ?country a type:LandlockedCountries, yago:AfricanCountries ; "
  sparql.Append "             rdfs:label ?country_name ; "
  sparql.Append "             prop:populationEstimate ?population . "
  sparql.Append "             FILTER (?population > 15000000) . "
  sparql.Append "             filter (lang(?country_name) = 'en'). "
  sparql.Append "             bind( str(?country_name) as ?ind1 ). "
  sparql.Append "} "
  ' construct the SPARQL URL
  url.Append dsn
  url.Append "?"
  url.Append "query="
  url.Append URLEncode(sparql.toString)
  url.Append "&"
  url.Append parameters
  result = WebRequest(url.toString)
  DoEvents: DoEvents
  ' Parse SPARQL JSON results
  Set p = JSON.parse(result)
  DoEvents: DoEvents
  If Not (p Is Nothing) Then
     If JSON.GetParserErrors <> "" Then
     ' Errors 
     MsgBox JSON.GetParserErrors, vbInformation, "Parsing Error(s) occured"
     ' No Errors
     Dim cnt As Integer
     Dim maxrecs As Integer
     Dim sparqldata As String
     maxrecs = IIf(CInt(p.Item("results").Item("bindings").Count) > 4, 5, CInt(p.Item("results").Item("bindings").Count))
     cnt = 1
     buf.Append "Retrieved Data:" & vbNewLine
     buf.Append "" & vbNewLine
     Do While True
    buf.Append p.Item("results").Item("bindings").Item(cnt).Item("country_name").Item("value") & vbNewLine
    If cnt = maxrecs Then Exit Do
    cnt = cnt + 1
     End If
     MsgBox "An error occurred parsing file "
  End If

 MsgBox buf.toString
 End Sub

 Public Function WebRequest(url As String) As String
  Dim http As MSXML2.XMLHTTP
  Set http = CreateObject("MSXML2.ServerXMLHTTP")

  http.Open "GET", url, False

  WebRequest = http.responseText
  Set http = Nothing
 End Function

 ' Return a URL safe encoding of txt.
 Private Function URLEncode(ByVal txt As String) As String
 Dim i As Integer
 Dim ch As String
 Dim ch_asc As Integer
 Dim result As String


  result = ""
  For i = 1 To Len(txt)
   ' Translate the next character.
   ch = Mid$(txt, i, 1)
   ch_asc = Asc(ch)
   If ch_asc = vbKeySpace Then
    ' Use a plus.
    result = result & "+"
   ElseIf m_SafeChar(ch_asc) Then
    ' Use the character.
    result = result & ch
    ' Convert the character to hex.
    result = result & "%" & Right$("0" & _
     Hex$(ch_asc), 2)
   End If
  Next i

  URLEncode = result
 End Function

 ' Set m_SafeChar(i) = True for characters that
 ' do not need protection.
 Private Sub SetSafeChars()
 Static done_before As Boolean
 Dim i As Integer

  If done_before Then Exit Sub
  done_before = True

  For i = 0 To 47
   m_SafeChar(i) = False
  Next i
  For i = 48 To 57
   m_SafeChar(i) = True
  Next i
  For i = 58 To 64
   m_SafeChar(i) = False
  Next i
  For i = 65 To 90
   m_SafeChar(i) = True
  Next i
  For i = 91 To 96
   m_SafeChar(i) = False
  Next i
  For i = 97 To 122
   m_SafeChar(i) = True
  Next i
  For i = 123 To 255
   m_SafeChar(i) = False
  Next i
 End Sub

  Retrieved Data 

  Burkina Faso

JSON was chosen over XML (re. output format) since this is about a "no-brainer installation and utilization" guide for a Javascript developer that already knows how to use Javascript for HTTP based data access within HTML. SPARQL just provides an added bonus to URL dexterity (delivered via URI abstraction) with regards to constructing Data Source Names or Addresses.

No comments:

Post a Comment