summaryrefslogtreecommitdiff
path: root/lib/owl-serializer.rb
blob: 8965bf2ca86bb51f4292d8416d74b2a78aac8167 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
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
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
require 'rdf'
require 'rdf/raptor'
require 'rdf/ntriples'

# RDF namespaces
include RDF
OT = RDF::Vocabulary.new 'http://www.opentox.org/api/1.1#'

module OpenTox

  class OwlSerializer

    def initialize(klass,uri)

			@model = RDF::Graph.new(uri)

      @triples = []
      @triples << [ OT[klass], RDF.type, OWL.Class ]
      @triples << [ RDF::URI.new(uri), RDF.type, OT[klass] ]

      @classes = [ OT[klass] ]
      @object_properties = []
      @annotation_properties = []
      @objects = [ uri ]

    end
    
    def self.create(klass, uri)
      OpenTox::OwlSerializer.new(klass,uri)
    end

    def rdf
      @triples.each { |statement| @model << statement }
      RDF::Writer.for(:rdfxml).buffer do |writer|
        writer << @model
      end
    end

    def object_property(subject,predicate,object,object_class)
      s = [ RDF::URI.new(subject), predicate, RDF::URI.new(object) ] # 
      @triples << s unless @triples.include? s
      unless @object_properties.include? predicate
        @triples << [ predicate, RDF.type, OWL.ObjectProperty ]
        @object_properties << predicate
      end
      unless @objects.include? object
        @triples << [ RDF::URI.new(object), RDF.type, object_class ]
        @objects << object
      end
      unless @classes.include? object_class
        @triples << [ object_class, RDF.type, OWL.Class ]
        @classes << object_class
      end
    end

    def annotation_property(subject, predicate, value, datatype)
      s = [ RDF::URI.new(subject), predicate, RDF::Literal.new(value, :datatype => datatype) ]
      @triples << s unless @triples.include? s
      unless @annotation_properties.include? predicate
        @triples << [ predicate, RDF.type, OWL.AnnotationProperty ]
        @annotation_properties << predicate
      end
    end
  end
end