Class: ActiveRubinstein::SparqlFormulator

Inherits:
Object
  • Object
show all
Defined in:
lib/active_rubinstein/sparql_formulator.rb

Overview

constructs SPARQL from abstract ActiveRubic queries

Class Method Summary collapse

Class Method Details

.construct(action, options = {}) ⇒ Object

Parameters:

  • 1 action (Symbol; :all, :images_of, :properties etc) – see RDFS::Resource and the child objects for possible actions.

  • 2 options (Hash)



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/active_rubinstein/sparql_formulator.rb', line 8

def self.construct( action, options={} )
  @@log.rubinstein "Constructing SPARQL, options: #{options}"
  # todo: insert limit into sparql
  #             if limit then
  # BUG: Jena doesn't process .limit in SPARQL
  subject = options[ :subject ]
  predicate = options[ :predicate ] || options[ :property ]
  object = options[ :object ]
  owlclass = options[ :class ]
  @@log.debug "  => constructing Query to find #{action}"

#         subject = RDFS::Resource.new( subject.uri ) if subject.is_a? RdfAbout
#         predicate = RDFS::Resource.new( predicate.uri ) if predicate.is_a? Property or predicate.is_a? RdfAbout
#         object = RDFS::Resource.new( object.uri ) if object.is_a? RdfAbout
#         owlclass = RDFS::Resource.new( owlclass.uri ) if owlclass.is_a? OwlClass
# 
#         subject = RDFS::Resource.new( subject ) if subject.is_a? String
#         predicate = RDFS::Resource.new( predicate ) if predicate.is_a? String
#         object = RDFS::Resource.new( object ) if object.is_a? String
#         owlclass = RDFS::Resource.new( owlclass ) if owlclass.is_a? String


# ActiveRubic::Base.find :images_of, :subject => KUOPIO::kuopion_museo
  if action.is_a? Symbol
    case action
    when :all then # BROKEN
  #               query = Query.new.distinct( :resource ).where( :resource, :x, :y )
      q = Query.new.distinct( :image ).where( :image, RDF::type, RDFS::Resource.new("media://image") )

    when :images_of then
      unless defined? subject
        @@log.error " ! No subject defined for action #{action}, cannot formulate SPARQL"
      else
        @@log.rubinstein "Find images of #{subject}"
        q = Query.new.select( :image ).
          where( :image, RDF::subject, subject ).
          where( :image, RDF::type, RDFS::Resource.new("media://image") )

        # select by image rating
        if options[ :rating ] # can be only 0 to 5
          q.where( :image, IPTC::Rating, options[ :rating ] )
        end
      end

    # not used!!!!
    when :random_image_of
      unless subject
        @@log.error " ! No subject defined for query #{action}, cannot formulate SPARQL"
      else
        # FIXME: Randomizing is not handled here, it's hacked into RDFS::Resource
        q = Query.new.select( :image ).
          where( :image, RDF::subject, subject ).
          where( :image, RDF::type, RDFS::Resource.new("media://image") )

        # select by image rating
        if options[ :rating ] # can be only 0 to 5
          q.where( :image, IPTC::Rating, options[ :rating ] )
        end
      end

    when :markers
      unless owlclass
        @@log.error " ! No class defined for query #{action}"
      else
        # Query for all instances that belong to this class and have GEO::lat and GEO::long defined
        q = Query.new.select( :markers ). \
            where( :markers, RDF::type, owlclass ). \
            where( :markers, GEO::lat, :lat ).where( :markers, GEO::long, :long )

      end

      # property handling moved to jena_query, since the objects should
      # be transformed to OWL::ObjectProperty and generic SPARQL returns
      # only RDFS::Resource objects
#           when :properties then
#             unless defined? subject
#               @@log.error " ! No subject defined for action #{action}, cannot formulate SPARQL"
#             else
#               @@log.deep "Querying for properties of #{subject}"
#               q = Query.new.select( :property ) \
#                 .where( :property, :any, OWL::ObjectProperty ) \
#                 .where( RDFS::Resource.new( subject ), :property, :within_range )
#             end

    when :within_range then
      unless subject
        @@log.debug "Subject not defined, using Symbol"
        subject = :subject
#                 @@log.error " ! No subject defined for action #{action}, cannot formulate SPARQL"
      end

      if ! defined? predicate
        @@log.error " ! No predicate defined for action #{action}, cannot formulate SPARQL"
      else
        @@log.deep "Querying for #{subject} #{predicate} ?"
        q = Query.new.select( :within_range ) \
          .where( subject, predicate, :within_range )
#                   .where( RDFS::Resource.new( subject ), :property, :within_range )
      end

    end

    # insert LIMIT into SPARQL
    limit = options[ :limit ]
    if !limit.nil? and !limit.is_a? Fixnum
      @@log.warn "Limit must be a Fixnum, got #{limit} (#{limit.class})"
    elsif !limit.nil?
      @@log.rubinstein " => Limit: #{limit}"
      q.limit( limit )
    end
  
    return q

  else # expect the action variable is a RDFS::Resource predicate
    unless defined? subject
      @@log.error " ! No subject defined to query #{action}, cannot formulate SPARQL"
    else
      @@log.deep "Constructing SPARQL #{subject} #{action} ?"
      return Query.new.distinct( :object ). \
      where( subject, action, :object )
    end
  end
end