Class: Pho::QueryProfile

Inherits:
Object
  • Object
show all
Defined in:
lib/pho/query_profile.rb

Overview

Models the QueryProfile configuration associated with a Platform store

Class methods exist to read a QueryProfile from a store, providing some convenience over the basic Store methods.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, label, field_weights = Array.new) ⇒ QueryProfile

Returns a new instance of QueryProfile.



94
95
96
97
98
# File 'lib/pho/query_profile.rb', line 94

def initialize(uri, label, field_weights=Array.new)
  @uri = uri
  @label = label
  @field_weights = field_weights
end

Instance Attribute Details

#field_weightsObject (readonly)

The list of field weightings



51
52
53
# File 'lib/pho/query_profile.rb', line 51

def field_weights
  @field_weights
end

#labelObject (readonly)

Label associated with the resource in the Platform config



45
46
47
# File 'lib/pho/query_profile.rb', line 45

def label
  @label
end

#uriObject (readonly)

URI for this resource



48
49
50
# File 'lib/pho/query_profile.rb', line 48

def uri
  @uri
end

Class Method Details

.create_weighting(store, name, weight) ⇒ Object

Create a FieldWeighting object suitable for adding to this store. Will ensure that the name of the propery is valid according to the Platform naming rules

store

the store that the weighting is to be created for

name

name of the field to be weighted

weight

the weighting of the field



84
85
86
87
88
89
90
91
92
# File 'lib/pho/query_profile.rb', line 84

def QueryProfile.create_weighting(store, name, weight)
  if !name.match(/^[a-zA-Z][a-zA-Z0-9]*$/)
    raise "Name does not conform to regular expression: ^[a-zA-Z][a-zA-Z0-9]*$"
  end        
  
  weight_uri = store.build_uri("/config/queryprofiles/1##{name}")
  return FieldWeighting.new(weight_uri, name, weight)
  
end

.read_from_store(store) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/pho/query_profile.rb', line 53

def QueryProfile.read_from_store(store)
  resp = store.get_query_profile(Pho::ACCEPT_JSON)        
  if resp.status != 200
    raise "Unable to read Query Profile from store. Response code was #{resp.status}"
  end

  qp_uri = store.build_uri("/config/queryprofiles/1")
  
  json = JSON.parse( resp.content )

  label = json[qp_uri]["http:\/\/www.w3.org\/2000\/01\/rdf-schema#label"][0]["value"]
  qp = QueryProfile.new(qp_uri, label)
  
  field_weights = json[qp_uri]["http:\/\/schemas.talis.com\/2006\/bigfoot\/configuration#fieldWeight"]
  field_weights.each { |uri|
    property = json[uri["value"]]
    name = property["http:\/\/schemas.talis.com\/2006\/frame\/schema#name"][0]["value"]
    weight = property["http:\/\/schemas.talis.com\/2006\/bigfoot\/configuration#weight"][0]["value"]
    qp << FieldWeighting.new(uri["value"], name, weight)
  }
  
  return qp        
  
end

Instance Method Details

#<<(weight) ⇒ Object



100
101
102
# File 'lib/pho/query_profile.rb', line 100

def <<(weight)
    @field_weights << weight    
end

#get_by_name(name) ⇒ Object

Retrieve a FieldWeighing by name



105
106
107
# File 'lib/pho/query_profile.rb', line 105

def get_by_name(name)
  return @field_weights.detect { |field| field.name == name }  
end

#mapped_name?(name) ⇒ Boolean

Is there a field weighting for a property with this name?

Returns:

  • (Boolean)


128
129
130
# File 'lib/pho/query_profile.rb', line 128

def mapped_name?(name)
  return get_by_name(name) != nil
end

#remove(fw) ⇒ Object

Remove a FieldWeighting from the collection



118
119
120
# File 'lib/pho/query_profile.rb', line 118

def remove(fw)
  return @field_weights.delete(fw)
end

#remove_allObject

Remove all field weights



123
124
125
# File 'lib/pho/query_profile.rb', line 123

def remove_all()
  @field_weights = Array.new
end

#remove_by_name(name) ⇒ Object

Remove a FieldWeighting by name



110
111
112
113
114
115
# File 'lib/pho/query_profile.rb', line 110

def remove_by_name(name)
  fw = get_by_name(name)
  if (fw != nil)
    return remove(fw)
  end   
end

#to_rdfObject

Dump this object to an RDF/XML representation suitable for submitting to the Platform



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/pho/query_profile.rb', line 133

def to_rdf
  rdf = "<rdf:RDF xmlns:frm=\"#{Pho::Namespaces::FRAME}\" "
  rdf << " xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" "
  rdf << " xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\" "
  rdf << " xmlns:bf=\"#{Pho::Namespaces::CONFIG}\" > " 
   
  rdf << " <rdf:Description rdf:about=\"#{@uri}\"> "
  
  rdf << " <rdf:type rdf:resource=\"#{Pho::Namespaces::CONFIG}QueryProfile\"/> "
  rdf << " <rdfs:label>#{@label}</rdfs:label> "
  
  @field_weights.each do |property|
    rdf << " <bf:fieldWeight rdf:resource=\"#{property.uri}\"/> "
  end
              
  rdf << " </rdf:Description>"
  
  @field_weights.each do |property|
    rdf << property.to_rdf(false)
  end
        
  rdf << "</rdf:RDF>"
end

#upload(store) ⇒ Object

Upload an RDF/XML presentation of this object to the provided Platform Store



158
159
160
# File 'lib/pho/query_profile.rb', line 158

def upload(store)
    return store.put_query_profile(self.to_rdf)  
end