Class: Pho::FieldPredicateMap

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

Overview

Models a the Field Predicate Map configuration associated with a Platform Store.

Class methods exist to create a FieldPredicateMap instance by reading from a store, and to create DatatypeProperty instances checking that the supplied data is valid according to the same logic as used by the Platform API.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, label, datatype_properties = []) ⇒ FieldPredicateMap

Returns a new instance of FieldPredicateMap.



153
154
155
156
157
# File 'lib/pho/field_predicate_map.rb', line 153

def initialize(uri, label, datatype_properties = [])
  @uri = uri
  @label = label
  @datatype_properties = datatype_properties
end

Instance Attribute Details

#datatype_propertiesObject (readonly)

An array of DatatypeProperty instances



104
105
106
# File 'lib/pho/field_predicate_map.rb', line 104

def datatype_properties
  @datatype_properties
end

#labelObject (readonly)

Label associated with the resource in the Platform config



98
99
100
# File 'lib/pho/field_predicate_map.rb', line 98

def label
  @label
end

#uriObject (readonly)

URI for this resource



101
102
103
# File 'lib/pho/field_predicate_map.rb', line 101

def uri
  @uri
end

Class Method Details

.create_mapping(store, property_uri, name, analyzer = nil) ⇒ Object

Create a DatatypeProperty instance, automatically assigning a unique identifier to it, and adding validating the supplied data to ensure it matches the platform rules



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/pho/field_predicate_map.rb', line 139

def FieldPredicateMap.create_mapping(store, property_uri, name, analyzer=nil)
    check_value("property_uri", property_uri)
    check_value("name", name)
    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        
    if analyzer != nil && analyzer.empty?
      analyzer = nil
    end  
    suffix = get_suffix(property_uri)
    mapping_uri = store.build_uri("/config/fpmaps/1##{suffix}")
    return DatatypeProperty.new(mapping_uri, property_uri, name, analyzer)        
end

.read_from_store(store) ⇒ Object

Read a FieldPredicateMap instance from the provided store. The method will retrieve the config as JSON, and parse it to create an object instance.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/pho/field_predicate_map.rb', line 109

def FieldPredicateMap.read_from_store(store)
    resp = store.get_field_predicate_map(Pho::ACCEPT_JSON)        
    if resp.status != 200
      raise "Unable to read Field Predicate Map from store. Response code was #{resp.status}"
    end

    fpmap_uri = store.build_uri("/config/fpmaps/1")
  
    json = JSON.parse( resp.content )
    labels = json[fpmap_uri]["http:\/\/www.w3.org\/2000\/01\/rdf-schema#label"]
    label = ""
    if labels != nil
      label = labels[0]["value"]
    end
    
    fpmap = FieldPredicateMap.new(fpmap_uri, label)
    
    mapped_properties = json[fpmap_uri]["http:\/\/schemas.talis.com\/2006\/frame\/schema#mappedDatatypeProperty"]
    mapped_properties.each { |uri|
      property = json[uri["value"]]
      property_uri = property["http:\/\/schemas.talis.com\/2006\/frame\/schema#property"][0]["value"]
      name = property["http:\/\/schemas.talis.com\/2006\/frame\/schema#name"][0]["value"]
      fpmap << DatatypeProperty.new(uri["value"], property_uri, name)
    }
    
    return fpmap        
end

Instance Method Details

#<<(obj) ⇒ Object

Append a DatatypeProperty object to this map. Note that the method does not validate the object, and neither does it check for duplicate mappings.



162
163
164
# File 'lib/pho/field_predicate_map.rb', line 162

def <<(obj)
  @datatype_properties << obj
end

#get_by_name(name) ⇒ Object

Find the DatatypeProperty (if any) with the following name mapping



201
202
203
# File 'lib/pho/field_predicate_map.rb', line 201

def get_by_name(name)
  return @datatype_properties.detect { |mapping| name == mapping.name }
end

#get_by_uri(uri) ⇒ Object

Find the DatatypeProperty using a property uri



206
207
208
# File 'lib/pho/field_predicate_map.rb', line 206

def get_by_uri(uri)
  return @datatype_properties.detect { |mapping| uri == mapping.property_uri }
end

#get_name(uri) ⇒ Object

Lookup the name mapped to the specified uri

uri

the property uri to search for



169
170
171
172
173
174
175
176
# File 'lib/pho/field_predicate_map.rb', line 169

def get_name(uri)
  p = @datatype_properties.detect { |mapping| uri == mapping.property_uri }
  if p == nil
    return nil
  else
    return p.name        
  end
end

#get_property_uri(name) ⇒ Object

Lookup the property mapped to the specified name

name

the name to search for



181
182
183
184
185
186
187
188
# File 'lib/pho/field_predicate_map.rb', line 181

def get_property_uri(name)
  p = @datatype_properties.detect { |mapping| name == mapping.name }
  if p == nil
    return nil
  else
    return p.property_uri
  end
end

#mapped_name?(name) ⇒ Boolean

Is there a mapping for a property with this name?

Returns:

  • (Boolean)


191
192
193
# File 'lib/pho/field_predicate_map.rb', line 191

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

#mapped_uri?(uri) ⇒ Boolean

Is there a mapping for this uri?

Returns:

  • (Boolean)


196
197
198
# File 'lib/pho/field_predicate_map.rb', line 196

def mapped_uri?(uri)
  return get_name(uri) != nil
end

#remove(datatype_property) ⇒ Object

Remove a DatatypeProperty from the collection



211
212
213
# File 'lib/pho/field_predicate_map.rb', line 211

def remove(datatype_property)
  return @datatype_properties.delete(datatype_property)
end

#remove_allObject

Remove all currently mapped properties



232
233
234
# File 'lib/pho/field_predicate_map.rb', line 232

def remove_all()
  @datatype_properties = Array.new
end

#remove_by_name(name) ⇒ Object

Remove a DatatypeProperty by its mapped name



216
217
218
219
220
221
# File 'lib/pho/field_predicate_map.rb', line 216

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

#remove_by_uri(uri) ⇒ Object

Remove a DatatypeProperty by its mapped uri



224
225
226
227
228
229
# File 'lib/pho/field_predicate_map.rb', line 224

def remove_by_uri(uri)
  p = get_by_uri(uri)
  if (p != nil)
    return remove(p)
  end
end

#to_rdfObject

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



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/pho/field_predicate_map.rb', line 237

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}FieldPredicateMap\"/> "
  rdf << " <rdfs:label>#{@label}</rdfs:label> "
  
  @datatype_properties.each do |property|
    rdf << " <frm:mappedDatatypeProperty rdf:resource=\"#{property.uri}\"/> "
  end
              
  rdf << " </rdf:Description>"
  
  @datatype_properties.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



262
263
264
# File 'lib/pho/field_predicate_map.rb', line 262

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