php - SPARQL query to get all parent of a node -


i have tables in mysql database like:

+-------+------------+-------------+-----------+ |  id   |  subject   |  predicate  |  object   | +-------+------------+-------------+-----------+ |  1    | atm        |  subclassof | network   | +-------+------------+-------------+-----------+ |  2    | arpanet    |  subclassof | network   | +-------+------------+-------------+-----------+ |  3    | network    |  subclassof | main      | +-------+------------+-------------+-----------+ |  5    | software   |  subclassof | main      | +-------+------------+-------------+-----------+ |  7    | linux      |  subclassof | software  | +-------+------------+-------------+-----------+ |  8    | windows    |  subclassof | software  | +-------+------------+-------------+-----------+ |  12   | xp         |  subclassof | windows   | +-------+------------+-------------+-----------+ |  13   | win7       |  subclassof | windows   | +-------+------------+-------------+-----------+ |  14   | win8       |  subclassof | windows   | +-------+------------+-------------+-----------+ 

for predicate subclassof have tree view this:

main    |__ network    |         |__ atm    |         |__ arpanet    |    |__ software               |__ linux               |__ windows                         |__ xp                         |__ win7                         |__ win8 

i want create form can select start node , parent that. example choosing win7 want get:

main, software, windows,win7


step2: there way print nodes simple text this:

main    |__ software               |__ windows                         |__ win7 

your data can represented in rdf data.n3:

@prefix : <http://example.org/> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .  :network rdfs:subclassof :main .  :atm rdfs:subclassof :network . :arpanet rdfs:subclassof :network .  :software rdfs:subclassof :main .  :linux rdfs:subclassof :software . :windows rdfs:subclassof :software .  :xp rdfs:subclassof :windows . :win7 rdfs:subclassof :windows . :win8 rdfs:subclassof :windows . 

from here, want sparql query finds things connected particular class path (including empty path) of rdfs:subclassof properties.

prefix : <http://example.org/> prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>  select ?superclass {    :win7 rdfs:subclassof* ?superclass } 
-------------- | superclass | ============== | :win7      | | :windows   | | :software  | | :main      | -------------- 

the results in query aren't ordered position in path (though in case happen be). if need them in order, can (which based on this answer computing position of elements in rdf list):

prefix : <http://example.org/> prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>  select ?class {    :win7 rdfs:subclassof* ?mid .   ?mid rdfs:subclassof* ?class . } group ?class order count(?mid) 

this finds each ancestor ?class of :win7 each ?mid intermediate ancestor. ancestor ?class, distance computed number of intermediate relations in between (count(?mid)). orders results based distance, :win7 closest ancestor, :windows after that, , on.

you can of fancy formatting want this:

prefix : <http://example.org/> prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>  select (group_concat( ?name ; separator="--" )  ?path) {   {     select ?name {        :win7 rdfs:subclassof* ?mid .       ?mid rdfs:subclassof* ?class .       bind( strafter( str(?class), "http://example.org/") ?name )     }     group ?class ?name     order count(?mid)   } } 
----------------------------------- | path                            | =================================== | "win7--windows--software--main" | ----------------------------------- 

it might possible fancier string processing , multiline string. might @ latter part of this answer there fancy formatting nicely aligned matrix ideas.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -