Module: ActsAsRdf::InstanceMethods

Defined in:
lib/acts_as_rdf.rb

Overview

All the methods available to a record that has had acts_as_rdf specified.

Instance Method Summary collapse

Instance Method Details

#to_rdf(request = nil, format = :rdfxml) ⇒ Object

format is for active_rdf library



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
# File 'lib/acts_as_rdf.rb', line 47

def to_rdf(request = nil, format = :rdfxml)
  base_uri = RDF::URI.new(get_base_uri(request))

  RDF::Writer.for(format).buffer(:base_uri => base_uri, :default_namespace => base_uri, :prefixes => get_prefixes()) do |writer|
    writer << [to_uri(self, base_uri), RDF.type, to_ontology_uri(self, base_uri)]

    self.attributes.each do |attr, value|
      writer << [to_uri(self, base_uri), to_uri(attr, base_uri), RDF::Literal.new(value)]
      if ('name'.eql?(attr)) then
        writer << [to_uri(self, base_uri), RDF::RDFS.label, RDF::Literal.new(value)]
      end
    end

    self.class.reflect_on_all_associations.each do |soc|
      if self.respond_to? soc.name
        association_objects = self.send soc.name
        if association_objects != nil
          if 'Array'.eql?(association_objects.class.name)
            association_objects.each do |obj|
              writer << [to_uri(self, base_uri), to_uri(soc.class_name, base_uri), to_uri(obj, base_uri)]
            end
          else
            writer << [to_uri(self, base_uri), to_uri(soc.class_name, base_uri), to_uri(association_objects, base_uri)]
          end
        end
      end
    end
  end
end

#to_rdfs(request = nil, format = :rdfxml) ⇒ Object

this will return the rdfs schema for this model in owl.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/acts_as_rdf.rb', line 78

def to_rdfs(request = nil, format = :rdfxml)
  base_uri = RDF::URI.new(get_base_uri(request))

  RDF::Writer.for(format).buffer(:base_uri => base_uri, :default_namespace => base_uri, :prefixes => get_prefixes()) do |writer|
    writer << [to_ontology_uri(self, base_uri), RDF::RDFS.label, RDF::Literal.new(self.class.name)]
    writer << [to_uri(self, base_uri), RDF.type, RDF::OWL['Class']]

    self.attributes.each do |attr, value|
      writer << [to_uri(attr, base_uri), RDF.type, RDF::OWL['DatatypeProperty']]
      writer << [to_uri(attr, base_uri), RDF::RDFS.label, RDF::Literal.new(attr)]
      writer << [to_uri(attr, base_uri), RDF::RDFS.domain, to_ontology_uri(attr, base_uri)]
      obj = RDF::Literal.new(value)
      if obj.has_datatype? then
        writer << [to_uri(attr, base_uri), RDF::RDFS.range, RDF::Literal.new(value).datatype]
      end
    end
    socs = self.class.reflect_on_all_associations
    socs.each do |soc|
      writer << [to_uri(soc.class_name, base_uri), RDF.type, RDF::OWL['ObjectProperty']]
      writer << [to_uri(soc.class_name, base_uri), RDF::RDFS.label, RDF::Literal.new(soc.class_name)]
      writer << [to_uri(soc.class_name, base_uri), RDF::RDFS.domain, to_ontology_uri(soc, base_uri)]

      # ranges
      if self.respond_to? soc.name
        association_objects = self.send soc.name # needs to handle plural case
        if !association_objects.nil?
          association_objects = association_objects[0]
        end
        if !association_objects.nil?
          writer << [to_ontology_uri(soc.class_name, base_uri), RDF::RDFS.range, to_ontology_uri(association_objects, base_uri)]
        end
      end
    end
  end
end