Class: ObjectManager

Inherits:
Object
  • Object
show all
Defined in:
lib/active_rdf/objectmanager/object_manager.rb

Class Method Summary collapse

Class Method Details

.construct_class(resource) ⇒ Object

constructs Ruby class for the given resource (and puts it into the module as defined by the registered namespace abbreviations)



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
# File 'lib/active_rdf/objectmanager/object_manager.rb', line 43

def self.construct_class(resource)
   # get prefix abbreviation and localname from type
   # e.g. :foaf and Person
   localname = Namespace.localname(resource)
   prefix = Namespace.prefix(resource)

   # find (ruby-acceptable) names for the module and class
   # e.g. FOAF and Person
   if prefix.nil?
    # if the prefix is unknown, we create our own from the full URI
     modulename = create_module_name(resource)
     $log.debug "ObjectManager: construct_class: constructing modulename #{modulename} from URI #{resource}"
  else
    # otherwise we convert the registered prefix into a module name
    modulename = prefix_to_module(prefix)
    $log.debug "ObjectManager: construct_class: constructing modulename #{modulename} from registered prefix #{prefix}"
   end
   klassname = localname_to_class(localname)

   # look whether module defined
   # else: create it
   _module = if Object.const_defined?(modulename.to_sym)
       $log.debug "ObjectManager: construct_class: module name #{modulename} previously defined"
      Object.const_get(modulename.to_sym)
    else
       $log.debug "ObjectManager: construct_class: defining module name #{modulename} now"
      Object.const_set(modulename, Module.new)
    end

  # look whether class defined in that module
  if _module.const_defined?(klassname.to_sym)
     $log.debug "ObjectManager: construct_class: given class #{klassname} defined in the module"
    # if so, return the existing class
    _module.const_get(klassname.to_sym)
  else
     $log.debug "ObjectManager: construct_class: creating given class #{klassname}"
    # otherwise: create it, inside that module, as subclass of RDFS::Resource
    # (using toplevel Class.new to prevent RDFS::Class.new from being called)
    klass = _module.module_eval("#{klassname} = Object::Class.new(RDFS::Resource)")
    klass.class_uri = RDFS::Resource.new(resource.uri)
    klass
  end
end

.construct_classesObject

allows users to invoke methods on classes (e.g. FOAF::Person) without getting symbol undefined errors (because e.g. foaf:person wasnt encountered before so no class was created for it)



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
# File 'lib/active_rdf/objectmanager/object_manager.rb', line 14

def self.construct_classes
  # find all rdf:types and construct class for each of them
  #q = Query.new.select(:t).where(:s,Namespace.lookup(:rdf,:type),:t)

# find everything defined as rdfs:class or owl:class
type = Namespace.lookup(:rdf,:type)
rdfsklass = Namespace.lookup(:rdfs,:Class)

# TODO: we should not do this, we should not support OWL
# instead, owl:Class is defined as subclass-of rdfs:Class, so if the 
# reasoner has access to owl definition it should work out fine.
owlklass = Namespace.lookup(:owl,:Class)

klasses = []
  klasses << Query.new.distinct(:s).where(:s,type,rdfsklass).execute
  klasses << Query.new.distinct(:s).where(:s,type,owlklass).execute

# flattening to get rid of nested arrays
# compacting array to get rid of nil (if one of these queries returned nil)
klasses = klasses.flatten.compact
$log.debug "ObjectManager: construct_classes: classes found: #{klasses}"

# then we construct a Ruby class for each found rdfs:class
# and return the set of all constructed classes
  klasses.collect { |t| construct_class(t) }
end