Class: ActiveRDF::ObjectManager

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

Class Method Summary collapse

Class Method Details

.construct_class(class_or_resource_or_uri) ⇒ Object

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



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

def ObjectManager.construct_class(class_or_resource_or_uri)
  case class_or_resource_or_uri
    when Class, Module
      return class_or_resource_or_uri
    when RDFS::Resource
      resource = class_or_resource_or_uri
    when String
      resource = RDFS::Resource.new(class_or_resource_or_uri)
    else raise ActiveRdfError, "ObjectManager: can't construct class from #{class_or_resource_or_uri.inspect}"
  end

  # 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)
  ActiveRdfLogger::log_debug(self) { "Construct_class: constructing modulename #{modulename} from URI #{resource}" }
  else
    # otherwise we convert the registered prefix into a module name
    modulename = prefix_to_module(prefix)
  ActiveRdfLogger::log_debug(self) { "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)
  ActiveRdfLogger::log_debug(self) { "ObjectManager: construct_class: module name #{modulename} previously defined" }
      Object.const_get(modulename.to_sym)
    else
  ActiveRdfLogger::log_debug(self) { "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)
  ActiveRdfLogger::log_debug(self) { "ObjectManager: construct_class: given class #{klassname} defined in the module" }
    # if so, return the existing class
    _module.const_get(klassname.to_sym)
  else
  ActiveRdfLogger::log_debug(self) { "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 = resource
    klass
  end
end

.construct_classesObject

Constructs empty Ruby classes for all RDF types found in the data. 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)



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/active_rdf/objectmanager/object_manager.rb', line 10

def ObjectManager.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)

  # 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.
  klasses = []
  klasses << Query.new.distinct(:s).where(:s,RDF::type,RDFS::Class).execute
  klasses << Query.new.distinct(:s).where(:s,RDF::type,OWL::Class).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
ActiveRdfLogger::log_debug(self) { "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