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.



163
164
165
166
167
# File 'lib/pho/field_predicate_map.rb', line 163

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

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

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

Then automatically appends it to the provided fpmap instance



157
158
159
160
161
# File 'lib/pho/field_predicate_map.rb', line 157

def FieldPredicateMap.add_mapping(fpmap, store, property_uri, name, analyzer=nil)
  mapping = create_mapping(store, property_uri, name, analyzer)
  fpmap << mapping
  return mapping
end

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

Create a DatatypeProperty instance, automatically assigning a unique identifier to it, and 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.



172
173
174
# File 'lib/pho/field_predicate_map.rb', line 172

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

#get_by_name(name) ⇒ Object

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



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

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



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

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



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

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



191
192
193
194
195
196
197
198
# File 'lib/pho/field_predicate_map.rb', line 191

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)


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

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

#mapped_uri?(uri) ⇒ Boolean

Is there a mapping for this uri?

Returns:

  • (Boolean)


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

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

#remove(datatype_property) ⇒ Object

Remove a DatatypeProperty from the collection



221
222
223
# File 'lib/pho/field_predicate_map.rb', line 221

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

#remove_allObject

Remove all currently mapped properties



242
243
244
# File 'lib/pho/field_predicate_map.rb', line 242

def remove_all()
  @datatype_properties = Array.new
end

#remove_by_name(name) ⇒ Object

Remove a DatatypeProperty by its mapped name



226
227
228
229
230
231
# File 'lib/pho/field_predicate_map.rb', line 226

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



234
235
236
237
238
239
# File 'lib/pho/field_predicate_map.rb', line 234

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



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/pho/field_predicate_map.rb', line 247

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



272
273
274
# File 'lib/pho/field_predicate_map.rb', line 272

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