Class: GRel::QL::QueryContext
- Inherits:
-
Object
- Object
- GRel::QL::QueryContext
- Defined in:
- lib/grel/ql.rb
Overview
end of to_triples
Constant Summary collapse
- TUPLE =
"TUPLE"
- NODE =
"NODE"
Instance Attribute Summary collapse
-
#last_registered_subject ⇒ Object
readonly
Returns the value of attribute last_registered_subject.
-
#limit ⇒ Object
Returns the value of attribute limit.
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
-
#offset ⇒ Object
Returns the value of attribute offset.
-
#optional ⇒ Object
Returns the value of attribute optional.
-
#optional_triples ⇒ Object
Returns the value of attribute optional_triples.
-
#order ⇒ Object
readonly
Returns the value of attribute order.
-
#orig_query ⇒ Object
readonly
Returns the value of attribute orig_query.
-
#projection ⇒ Object
readonly
Returns the value of attribute projection.
-
#top_level ⇒ Object
Returns the value of attribute top_level.
-
#triples ⇒ Object
Returns the value of attribute triples.
-
#unions ⇒ Object
Returns the value of attribute unions.
Instance Method Summary collapse
- #append(triples) ⇒ Object
- #fresh_filter_predicate ⇒ Object
-
#initialize(graph = nil) ⇒ QueryContext
constructor
A new instance of QueryContext.
- #next_node_id ⇒ Object
-
#node_to_optional(node_id) ⇒ Object
, last_registered_subject).
- #query_keys ⇒ Object
- #register_node(id, node_id, inverse = false) ⇒ Object
- #register_query(query) ⇒ Object
- #required_properties ⇒ Object
- #run ⇒ Object
- #to_sparql_describe(preamble = true) ⇒ Object
- #to_sparql_select(preamble = true) ⇒ Object
- #union(other_context) ⇒ Object
Constructor Details
#initialize(graph = nil) ⇒ QueryContext
Returns a new instance of QueryContext.
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/grel/ql.rb', line 286 def initialize(graph=nil) @id_counter = -1 @filters_counter = -1 @triples = [] @optional_triples = [] @optional_bgps = [] @projection = {} @nodes = {} @graph = graph @optional = false @last_registered_subject = [] @unions = [] @limit = nil @offset = nil @orig_query = nil @query_keys = [] @top_level = true end |
Instance Attribute Details
#last_registered_subject ⇒ Object (readonly)
Returns the value of attribute last_registered_subject.
283 284 285 |
# File 'lib/grel/ql.rb', line 283 def last_registered_subject @last_registered_subject end |
#limit ⇒ Object
Returns the value of attribute limit.
283 284 285 |
# File 'lib/grel/ql.rb', line 283 def limit @limit end |
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
283 284 285 |
# File 'lib/grel/ql.rb', line 283 def nodes @nodes end |
#offset ⇒ Object
Returns the value of attribute offset.
283 284 285 |
# File 'lib/grel/ql.rb', line 283 def offset @offset end |
#optional ⇒ Object
Returns the value of attribute optional.
284 285 286 |
# File 'lib/grel/ql.rb', line 284 def optional @optional end |
#optional_triples ⇒ Object
Returns the value of attribute optional_triples.
284 285 286 |
# File 'lib/grel/ql.rb', line 284 def optional_triples @optional_triples end |
#order ⇒ Object (readonly)
Returns the value of attribute order.
283 284 285 |
# File 'lib/grel/ql.rb', line 283 def order @order end |
#orig_query ⇒ Object (readonly)
Returns the value of attribute orig_query.
283 284 285 |
# File 'lib/grel/ql.rb', line 283 def orig_query @orig_query end |
#projection ⇒ Object (readonly)
Returns the value of attribute projection.
283 284 285 |
# File 'lib/grel/ql.rb', line 283 def projection @projection end |
#top_level ⇒ Object
Returns the value of attribute top_level.
284 285 286 |
# File 'lib/grel/ql.rb', line 284 def top_level @top_level end |
#triples ⇒ Object
Returns the value of attribute triples.
284 285 286 |
# File 'lib/grel/ql.rb', line 284 def triples @triples end |
#unions ⇒ Object
Returns the value of attribute unions.
284 285 286 |
# File 'lib/grel/ql.rb', line 284 def unions @unions end |
Instance Method Details
#append(triples) ⇒ Object
331 332 333 334 335 336 337 |
# File 'lib/grel/ql.rb', line 331 def append(triples) if(@optional) @optional_triples += triples else @triples += triples end end |
#fresh_filter_predicate ⇒ Object
374 375 376 377 |
# File 'lib/grel/ql.rb', line 374 def fresh_filter_predicate @filters_counter+=1 "?P_#{@id_counter}_#{@filters_counter}" end |
#next_node_id ⇒ Object
339 340 341 342 |
# File 'lib/grel/ql.rb', line 339 def next_node_id @id_counter += 1 @id_counter end |
#node_to_optional(node_id) ⇒ Object
, last_registered_subject)
368 369 370 371 372 |
# File 'lib/grel/ql.rb', line 368 def node_to_optional(node_id) #, last_registered_subject) @projection.delete(node_id) @nodes.delete(node_id) # @last_registered_subject << last_registered_subject end |
#query_keys ⇒ Object
313 314 315 316 317 318 319 |
# File 'lib/grel/ql.rb', line 313 def query_keys sets = [[@query_keys,@orig_query]] unions.each do |c| sets += c.query_keys end sets end |
#register_node(id, node_id, inverse = false) ⇒ Object
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/grel/ql.rb', line 344 def register_node(id,node_id,inverse=false) @filters_counter = -1 subject = id predicate = "?P_mg_#{node_id}" object = "?O_mg_#{node_id}" if(id.nil?) subject = "?S_mg_#{node_id}" @projection[subject] = true if(!inverse) else @projection[id] = true if(!inverse) end @last_registered_subject << subject # @projection[predicate] = true # @projection[object] = true @nodes[node_id] = subject [subject, predicate, object] end |
#register_query(query) ⇒ Object
305 306 307 308 309 310 311 |
# File 'lib/grel/ql.rb', line 305 def register_query(query) @orig_query = query query.each_pair do |k,v| @query_keys << k if(k != :@id && k.to_s.index("$inv_").nil?) end self end |
#required_properties ⇒ Object
500 501 502 503 |
# File 'lib/grel/ql.rb', line 500 def required_properties props = [] end |
#run ⇒ Object
496 497 498 |
# File 'lib/grel/ql.rb', line 496 def run @graph.query(to_sparql_describe) end |
#to_sparql_describe(preamble = true) ⇒ Object
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'lib/grel/ql.rb', line 379 def to_sparql_describe(preamble=true) bgps = @triples.map{|t| if(t.is_a?(Filter)) t.acum else t.join(' ') end }.join(" . ") optional_bgps = @optional_bgps.map{|optional_triples| "OPTIONAL { "+ optional_triples.map { |t| if(t.is_a?(Filter)) t.acum else t.join(' ') end }.join(" . ") + " }" }.join(" ") main_bgp = "#{bgps}" unless(@optional_bgps.empty?) main_bgp += " #{optional_bgps}" end query = if(preamble) query = "PREFIX : <http://grel.org/vocabulary#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX rdfs: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" query = query + " PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX fn: <http://www.w3.org/2005/xpath-functions#> " if(@unions.length>0) union_bgps = @unions.map{|u| u.to_sparql_describe(false) }.join(" UNION ") union_projections = @unions.map{|u| u.projection.keys }.flatten.uniq all_subjects = (@projection.keys + union_projections).uniq query += "DESCRIBE #{all_subjects.join(' ')} WHERE { { #{main_bgp} } UNION #{union_bgps} }" else query += "DESCRIBE #{@projection.keys.join(' ')} WHERE { #{main_bgp} }" end else "{ #{main_bgp} }" end end |
#to_sparql_select(preamble = true) ⇒ Object
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 |
# File 'lib/grel/ql.rb', line 420 def to_sparql_select(preamble=true) projection = {} bgps = @triples.map{ |t| s,p,o = t if(s.to_s.index("?X_") == 0 && p.to_s.index("?X_") == 0 && o.to_s.index("?X_") == 0) elsif(s.to_s.index("?S_mg") == 0 && p.to_s.index("?P_mg") == 0 && o.to_s.index("?O_mg") == 0) nil else projection[s] = true if((s.to_s.index("?") == 0) && s.to_s.index("?X_").nil? && s.index("_mg_").nil?) projection[p] = true if((p.to_s.index("?") == 0) && p.to_s.index("?X_").nil? && p.index("_mg_").nil?) projection[o] = true if((o.to_s.index("?") == 0) && o.to_s.index("?X_").nil? && o.index("_mg_").nil?) if(t.is_a?(Filter)) t.acum else t.join(' ') end end }.compact.join(" . ") optional_bgps = @optional_bgps.map{|optional_triples| "OPTIONAL { "+ optional_triples.map { |t| s,p,o = t if(s.to_s.index("?") == 0 && p.to_s.index("?") == 0 && o.to_s.index("?") == 0) nil else projection[s] = true if((s.to_s.index("?") == 0) && s.to_s.index("?X_").nil?) projection[p] = true if((p.to_s.index("?") == 0) && p.to_s.index("?X_").nil?) projection[o] = true if((o.to_s.index("?") == 0) && o.to_s.index("?X_").nil?) if(t.is_a?(Filter)) t.acum else t.join(' ') end end }.compact.join(" . ") + " }" }.join(" ") main_bgp = "#{bgps}" unless(@optional_bgps.empty?) main_bgp += " #{optional_bgps}" end query = if(preamble) query = "PREFIX : <http://grel.org/vocabulary#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX rdfs: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" query = query + " PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX fn: <http://www.w3.org/2005/xpath-functions#> " if(@unions.length>0) union_bgps = @unions.map{|u| u.to_sparql_describe(false) }.join(" UNION ") all_subjects = projection.keys query += "SELECT DISTINCT #{all_subjects.join(' ')} WHERE { { #{main_bgp} } UNION #{union_bgps} }" else all_subjects = projection.keys query += "SELECT DISTINCT #{all_subjects.join(' ')} WHERE { #{main_bgp} }" end else "{ #{main_bgp} }" end end |
#union(other_context) ⇒ Object
491 492 493 494 |
# File 'lib/grel/ql.rb', line 491 def union(other_context) @unions << other_context self end |