Class: BioInterchange::Writer
- Inherits:
-
Object
- Object
- BioInterchange::Writer
- Defined in:
- lib/biointerchange/writer.rb
Direct Known Subclasses
Genomics::RDFWriter, Phylogenetics::CDAORDFWriter, TextMining::RDFWriter
Instance Method Summary collapse
-
#add_prefix(uri_prefix, abbreviation_prefix) ⇒ Object
Adds a URI prefix that should be abbreviated when serializing triples in Turtle.
-
#close ⇒ Object
Finishes serializing triples.
-
#create_triple(subject, predicate, object, datatype = nil) ⇒ Object
Creates a new triple and serializes it.
-
#initialize(ostream) ⇒ Writer
constructor
Creates a new instance of a writer that will use the provided output stream to serialize object model instances.
-
#serialize(model, uri_prefix = nil) ⇒ Object
Serializes an object model instance.
-
#set_base(uri_prefix) ⇒ Object
Sets the base URI prefix that is output/used when serializing triples in Turtle.
Constructor Details
#initialize(ostream) ⇒ Writer
Creates a new instance of a writer that will use the provided output stream to serialize object model instances.
When you override this method, please make sure that the parameter ‘ostream` is assigned to the instance variable `@ostream`. The instance variable `@ostream` will be used by the serialization helper-method `create_triple`.
ostream
-
instance of IO or derivative class
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/biointerchange/writer.rb', line 12 def initialize(ostream) raise BioInterchange::Exceptions::ImplementationWriterError, 'The output stream is not an instance of IO or its subclasses.' unless ostream.kind_of?(IO) @ostream = ostream @prefixes = { '<http://purl.org/dc/terms/' => 'dc:', '<http://www.w3.org/1999/02/22-rdf-syntax-ns#' => 'rdf:', '<http://www.w3.org/2000/01/rdf-schema#' => 'rdfs:', '<http://www.w3.org/2002/07/owl#' => 'owl:', '<http://www.w3.org/2001/XMLSchema#' => 'xsd:' } end |
Instance Method Details
#add_prefix(uri_prefix, abbreviation_prefix) ⇒ Object
Adds a URI prefix that should be abbreviated when serializing triples in Turtle.
94 95 96 |
# File 'lib/biointerchange/writer.rb', line 94 def add_prefix(uri_prefix, abbreviation_prefix) @prefixes["<#{uri_prefix}"] = "#{abbreviation_prefix}:" end |
#close ⇒ Object
Finishes serializing triples.
82 83 84 85 86 |
# File 'lib/biointerchange/writer.rb', line 82 def close if BioInterchange::format == :turtle then serialize_turtle() end end |
#create_triple(subject, predicate, object, datatype = nil) ⇒ Object
Creates a new triple and serializes it.
subject
-
a string or RDF::URI instance denoting the subject of the triple
predicate
-
a string or RDF::URI instance denoting the predicate of the triple
object
-
RDF::URI instance for URIs or other Ruby class for literals
datatype
-
optional datatype describing literal types
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 |
# File 'lib/biointerchange/writer.rb', line 38 def create_triple(subject, predicate, object, datatype = nil) subject_uri = subject subject_uri = subject_uri.to_s unless subject_uri.instance_of?(String) subject_uri = "<#{subject_uri}>".sub(/\s/, '%20') predicate_uri = predicate predicate_uri = predicate_uri.to_s unless predicate_uri.instance_of?(String) predicate_uri = "<#{predicate_uri}>".sub(/\s/, '%20') object_representation = nil if object.kind_of?(RDF::URI) then object_uri = object.to_s object_representation = "<#{object_uri}>".sub(/\s/, '%20') elsif object.kind_of?(Integer) then object_representation = object.to_s elsif object.kind_of?(Float) then object_representation = object.to_s elsif object.instance_of?(TrueClass) or object.instance_of?(FalseClass) then object_representation = object.to_s else if datatype then object_representation = "\"#{object.to_s}\"^^<#{datatype.to_s}>" else object_representation = "\"#{object.to_s}\"" end end begin if BioInterchange::format == :turtle then serialize_turtle(subject_uri, predicate_uri, object_representation) else @ostream.puts("#{subject_uri} #{predicate_uri} #{object_representation} .") end rescue Errno::EPIPE # Whenever an output pipe disappears, then the user may be happy with what he/she # has seen and hit Ctrl-C, or, piped the output through a UNIX command line tool # such as "head". exit 0 end subject end |
#serialize(model, uri_prefix = nil) ⇒ Object
Serializes an object model instance.
model
-
an object model instance
uri_prefix
-
optional URI prefix that should be used in the RDFization of individuals/class instances
28 29 30 |
# File 'lib/biointerchange/writer.rb', line 28 def serialize(model, uri_prefix = nil) raise BioInterchange::Exceptions::ImplementationWriterError, 'You must implement this method, which takes an object model and serializes it into the output stream provided as constructor (\'initialize\') argument.' end |
#set_base(uri_prefix) ⇒ Object
Sets the base URI prefix that is output/used when serializing triples in Turtle.
89 90 91 |
# File 'lib/biointerchange/writer.rb', line 89 def set_base(uri_prefix) @prefixes["<#{uri_prefix}"] = '<' end |