sdl-ng 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/lib/sdl/exporters.rb CHANGED
@@ -1,9 +1,13 @@
1
- require_relative 'exporters/xml_mapping'
2
- require_relative 'exporters/exporter'
3
- require_relative 'exporters/schema_exporter'
4
- require_relative 'exporters/service_exporter'
5
- require_relative 'exporters/xsd_schema_exporter'
6
- require_relative 'exporters/xml_service_exporter'
7
- require_relative 'exporters/rdf_mapping'
8
- require_relative 'exporters/rdf_exporter'
9
- require_relative 'exporters/markdown_service_exporter'
1
+ module SDL::Exporters
2
+ extend ActiveSupport::Autoload
3
+
4
+ autoload :Exporter
5
+ autoload :SchemaExporter
6
+ autoload :ServiceExporter
7
+ autoload :RDFExporter
8
+ autoload :RDFMapping
9
+ autoload :MarkdownServiceExporter
10
+ autoload :XMLMapping
11
+ autoload :XMLServiceExporter
12
+ autoload :XSDSchemaExporter
13
+ end
@@ -1,19 +1,15 @@
1
- module SDL
2
- module Exporters
3
- class Exporter
4
- attr :compendium
5
- attr :options
1
+ class SDL::Exporters::Exporter
2
+ attr :compendium
3
+ attr :options
6
4
 
7
- def initialize(compendium, options = {})
8
- @compendium = compendium
9
- @options = options
10
- end
5
+ def initialize(compendium, options = {})
6
+ @compendium = compendium
7
+ @options = options
8
+ end
11
9
 
12
- def export_to_file(path, content)
13
- File.open(path, 'w') do |f|
14
- f.write(content)
15
- end
16
- end
10
+ def export_to_file(path, content)
11
+ File.open(path, 'w') do |f|
12
+ f.write(content)
17
13
  end
18
14
  end
19
15
  end
@@ -1,5 +1,3 @@
1
- require 'active_support/i18n'
2
-
3
1
  class SDL::Exporters::MarkdownServiceExporter < SDL::Exporters::ServiceExporter
4
2
  def export_service(service)
5
3
  buf = StringIO.new
@@ -1,33 +1,29 @@
1
1
  require 'rdf'
2
2
  require 'rdf/rdfxml'
3
3
 
4
- module SDL
5
- module Exporters
6
- class RDFExporter < ServiceExporter
7
- @@s = RDF::Vocabulary.new('http://www.open-service-compendium.org/')
4
+ class SDL::Exporters::RDFExporter < ServiceExporter
5
+ @@s = RDF::Vocabulary.new('http://www.open-service-compendium.org/')
8
6
 
9
- def export_service(service)
10
- graph = RDF::Graph.new
7
+ def export_service(service)
8
+ graph = RDF::Graph.new
11
9
 
12
- service.facts.each do |fact|
13
- graph << [RDF::URI.new(service.uri), @@s["has_#{fact.class.local_name.underscore}"], RDF::URI.new(fact.uri)]
10
+ service.facts.each do |fact|
11
+ graph << [RDF::URI.new(service.uri), @@s["has_#{fact.class.local_name.underscore}"], RDF::URI.new(fact.uri)]
14
12
 
15
- expand_properties(fact, graph)
16
- end
13
+ expand_properties(fact, graph)
14
+ end
17
15
 
18
- graph.dump(:rdf)
19
- end
16
+ graph.dump(:rdf)
17
+ end
20
18
 
21
- def expand_properties(type_instance, graph)
22
- type_instance.property_values.each do |property, value|
23
- [value].flatten.each do |v|
24
- graph << [RDF::URI.new(type_instance.uri), @@s["#{property.name.underscore}"], v.rdf_object] unless v.nil?
25
- end
19
+ def expand_properties(type_instance, graph)
20
+ type_instance.property_values.each do |property, value|
21
+ [value].flatten.each do |v|
22
+ graph << [RDF::URI.new(type_instance.uri), @@s["#{property.name.underscore}"], v.rdf_object] unless v.nil?
23
+ end
26
24
 
27
- if property.type < SDL::Base::Type
28
- [value].flatten.each do |v| expand_properties(v, graph) end
29
- end
30
- end
25
+ if property.type < SDL::Base::Type
26
+ [value].flatten.each do |v| expand_properties(v, graph) end
31
27
  end
32
28
  end
33
29
  end
@@ -1,5 +1,4 @@
1
1
  require 'rdf'
2
- require 'active_support/inflector'
3
2
 
4
3
  [String, Fixnum, Nokogiri::XML::Element].each do |klass|
5
4
  klass.class_eval do
@@ -1,9 +1,5 @@
1
- module SDL
2
- module Exporters
3
- class SchemaExporter < Exporter
4
- def export_schema_to_file(path)
5
- export_to_file path, export_schema
6
- end
7
- end
1
+ class SDL::Exporters::SchemaExporter < Exporter
2
+ def export_schema_to_file(path)
3
+ export_to_file path, export_schema
8
4
  end
9
5
  end
@@ -1,9 +1,5 @@
1
- module SDL
2
- module Exporters
3
- class ServiceExporter < Exporter
4
- def export_service_to_file(service, path)
5
- export_to_file path, export_service(service)
6
- end
7
- end
1
+ class SDL::Exporters::ServiceExporter < Exporter
2
+ def export_service_to_file(service, path)
3
+ export_to_file path, export_service(service)
8
4
  end
9
- end
5
+ end
@@ -1,35 +1,31 @@
1
- module SDL
2
- module Exporters
3
- class XMLServiceExporter < ServiceExporter
4
- def export_service(service)
5
- builder = Nokogiri::XML::Builder.new do |xml|
6
- xml.service('xmlns' => 'http://www.open-service-compendium.org') do
7
- service.facts.each do |fact|
8
- xml.send(fact.class.xsd_element_name + '_') do
9
- serialize_type_instance fact, xml
10
- end
11
- end
1
+ class SDL::Exporters::XMLServiceExporter < ServiceExporter
2
+ def export_service(service)
3
+ builder = Nokogiri::XML::Builder.new do |xml|
4
+ xml.service('xmlns' => 'http://www.open-service-compendium.org') do
5
+ service.facts.each do |fact|
6
+ xml.send(fact.class.xsd_element_name + '_') do
7
+ serialize_type_instance fact, xml
12
8
  end
13
9
  end
14
-
15
- builder.to_xml
16
10
  end
11
+ end
12
+
13
+ builder.to_xml
14
+ end
17
15
 
18
- def serialize_type_instance(type_instance, xml)
19
- type_instance.property_values.each do |property, value|
20
- [value].flatten.each do |v|
21
- if v.class < SDL::Base::Type
22
- xml.send(property.xsd_element_name + '_', (!value.is_a?(Array) && value.identifier) ? {'identifier' => value.identifier.to_s} : {}) do
23
- v.annotations.each do |annotation|
24
- xml.annotation annotation
25
- end
26
- xml.documentation v.documentation if (!value.is_a?(Array) && value.identifier)
27
- serialize_type_instance(v, xml)
28
- end
29
- else
30
- xml.send(property.xsd_element_name + '_', v)
16
+ def serialize_type_instance(type_instance, xml)
17
+ type_instance.property_values.each do |property, value|
18
+ [value].flatten.each do |v|
19
+ if v.class < SDL::Base::Type
20
+ xml.send(property.xsd_element_name + '_', (!value.is_a?(Array) && value.identifier) ? {'identifier' => value.identifier.to_s} : {}) do
21
+ v.annotations.each do |annotation|
22
+ xml.annotation annotation
31
23
  end
24
+ xml.documentation v.documentation if (!value.is_a?(Array) && value.identifier)
25
+ serialize_type_instance(v, xml)
32
26
  end
27
+ else
28
+ xml.send(property.xsd_element_name + '_', v)
33
29
  end
34
30
  end
35
31
  end
@@ -1,65 +1,60 @@
1
- require 'active_support/inflector'
2
1
  require 'nokogiri'
3
2
 
4
- module SDL
5
- module Exporters
6
- ##
7
- # The XSD schema exporter creates an XML Schema Definition for consuming service descriptions using the XML provided
8
- # by the XMLServiceExporter.
9
- #
10
- # The schema consists of the following main components:
11
- # - The definition of the root node, i.e., a service. The service contains an arbitrary number of elements of
12
- # service fact types.
13
- # - The definition of service fact classes and SDL types
14
- # - The definition of a type base, containing annotations and documentation
15
- class XSDSchemaExporter < SchemaExporter
16
- def export_schema
17
- export_schema_xml.to_xml
18
- end
3
+ ##
4
+ # The XSD schema exporter creates an XML Schema Definition for consuming service descriptions using the XML provided
5
+ # by the XMLServiceExporter.
6
+ #
7
+ # The schema consists of the following main components:
8
+ # - The definition of the root node, i.e., a service. The service contains an arbitrary number of elements of
9
+ # service fact types.
10
+ # - The definition of service fact classes and SDL types
11
+ # - The definition of a type base, containing annotations and documentation
12
+ class SDL::Exporters::XSDSchemaExporter < SchemaExporter
13
+ def export_schema
14
+ export_schema_xml.to_xml
15
+ end
19
16
 
20
- def export_schema_xml
21
- Nokogiri::XML::Builder.new do |xml|
22
- xml['ns'].schema('xmlns' => 'http://www.open-service-compendium.org', 'targetNamespace' => 'http://www.open-service-compendium.org', 'xmlns:ns' => 'http://www.w3.org/2001/XMLSchema', 'elementFormDefault' => 'qualified') do
23
- xml['ns'].element :name => 'service' do
24
- document(xml, I18n.t('sdl.xml.service_root'))
25
- xml['ns'].complexType do
26
- xml['ns'].choice :maxOccurs => 'unbounded' do
27
- @compendium.fact_classes.each do |fact_class|
28
- xml['ns'].element :name => fact_class.xsd_element_name, :type => fact_class.xsd_type_name do
29
- document(xml, fact_class.documentation)
30
- end
31
- end
17
+ def export_schema_xml
18
+ Nokogiri::XML::Builder.new do |xml|
19
+ xml['ns'].schema('xmlns' => 'http://www.open-service-compendium.org', 'targetNamespace' => 'http://www.open-service-compendium.org', 'xmlns:ns' => 'http://www.w3.org/2001/XMLSchema', 'elementFormDefault' => 'qualified') do
20
+ xml['ns'].element :name => 'service' do
21
+ document(xml, I18n.t('sdl.xml.service_root'))
22
+ xml['ns'].complexType do
23
+ xml['ns'].choice :maxOccurs => 'unbounded' do
24
+ @compendium.fact_classes.each do |fact_class|
25
+ xml['ns'].element :name => fact_class.xsd_element_name, :type => fact_class.xsd_type_name do
26
+ document(xml, fact_class.documentation)
32
27
  end
33
28
  end
34
29
  end
30
+ end
31
+ end
35
32
 
36
- xml['ns'].complexType :name => 'SDLTypeBase' do
37
- document(xml, I18n.t('sdl.xml.typebase'))
38
- xml['ns'].choice do
39
- xml['ns'].element :name => 'documentation', :minOccurs => 0, :maxOccurs => 'unbounded', :type => 'ns:string' do
40
- document(xml, I18n.t('sdl.xml.documentation'))
41
- end
42
- xml['ns'].element :name => 'annotation', :minOccurs => 0, :maxOccurs => 'unbounded', :type => 'ns:string' do
43
- document(xml, I18n.t('sdl.xml.annotation'))
44
- end
45
- end
46
- xml['ns'].attribute :name => 'identifier' do
47
- document(xml, I18n.t('sdl.xml.identifier'))
48
- end
33
+ xml['ns'].complexType :name => 'SDLTypeBase' do
34
+ document(xml, I18n.t('sdl.xml.typebase'))
35
+ xml['ns'].choice do
36
+ xml['ns'].element :name => 'documentation', :minOccurs => 0, :maxOccurs => 'unbounded', :type => 'ns:string' do
37
+ document(xml, I18n.t('sdl.xml.documentation'))
38
+ end
39
+ xml['ns'].element :name => 'annotation', :minOccurs => 0, :maxOccurs => 'unbounded', :type => 'ns:string' do
40
+ document(xml, I18n.t('sdl.xml.annotation'))
49
41
  end
42
+ end
43
+ xml['ns'].attribute :name => 'identifier' do
44
+ document(xml, I18n.t('sdl.xml.identifier'))
45
+ end
46
+ end
50
47
 
51
- (@compendium.fact_classes + @compendium.types).each do |fact_class|
52
- xml['ns'].complexType :name => fact_class.xsd_type_name do
53
- document(xml, fact_class.documentation)
54
- xml['ns'].complexContent do
55
- xml['ns'].extension :base => fact_class.is_sub? ? fact_class.superclass.xsd_type_name : 'SDLTypeBase' do
56
- unless fact_class.properties.empty?
57
- xml['ns'].sequence do
58
- fact_class.properties.each do |property|
59
- extend_property(property, xml) do
60
- document(xml, property.documentation)
61
- end
62
- end
48
+ (@compendium.fact_classes + @compendium.types).each do |fact_class|
49
+ xml['ns'].complexType :name => fact_class.xsd_type_name do
50
+ document(xml, fact_class.documentation)
51
+ xml['ns'].complexContent do
52
+ xml['ns'].extension :base => fact_class.is_sub? ? fact_class.superclass.xsd_type_name : 'SDLTypeBase' do
53
+ unless fact_class.properties.empty?
54
+ xml['ns'].sequence do
55
+ fact_class.properties.each do |property|
56
+ extend_property(property, xml) do
57
+ document(xml, property.documentation)
63
58
  end
64
59
  end
65
60
  end
@@ -69,26 +64,26 @@ module SDL
69
64
  end
70
65
  end
71
66
  end
67
+ end
68
+ end
72
69
 
73
- # Creates an xml element corresponding to the SDL property
74
- def extend_property(property, xml)
75
- if property.multi?
76
- xml['ns'].element :name => property.name.singularize, :type => property.type.xml_type, :minOccurs => 0, :maxOccurs => 'unbounded' do
77
- yield
78
- end
79
- else
80
- xml['ns'].element :name => property.name, :type => property.type.xml_type, :minOccurs => 0 do
81
- yield
82
- end
83
- end
70
+ # Creates an xml element corresponding to the SDL property
71
+ def extend_property(property, xml)
72
+ if property.multi?
73
+ xml['ns'].element :name => property.name.singularize, :type => property.type.xml_type, :minOccurs => 0, :maxOccurs => 'unbounded' do
74
+ yield
84
75
  end
85
-
86
- # Shortcut for adding an XSD documentation annotation
87
- def document(xml, documentation)
88
- xml['ns'].annotation do
89
- xml['ns'].documentation documentation
90
- end
76
+ else
77
+ xml['ns'].element :name => property.name, :type => property.type.xml_type, :minOccurs => 0 do
78
+ yield
91
79
  end
92
80
  end
93
81
  end
82
+
83
+ # Shortcut for adding an XSD documentation annotation
84
+ def document(xml, documentation)
85
+ xml['ns'].annotation do
86
+ xml['ns'].documentation documentation
87
+ end
88
+ end
94
89
  end
@@ -1,5 +1,5 @@
1
1
  module SDL
2
2
  module NG
3
- VERSION = "0.0.4"
3
+ VERSION = "0.0.5"
4
4
  end
5
5
  end
data/lib/sdl/receivers.rb CHANGED
@@ -1,11 +1,12 @@
1
- require_relative 'receivers/type_instance_receiver'
2
- require_relative 'receivers/type_receiver'
3
- require_relative 'receivers/fact_receiver'
4
- require_relative 'receivers/service_receiver'
5
-
6
-
7
1
  module SDL
8
2
  module Receivers
3
+ extend ActiveSupport::Autoload
4
+
5
+ autoload :FactReceiver
6
+ autoload :ServiceReceiver
7
+ autoload :TypeReceiver
8
+ autoload :TypeInstanceReceiver
9
+
9
10
  #
10
11
  def self.set_value(type_class, type_instance, *property_values, compendium)
11
12
  property_values.zip(type_class.properties(true)).each do |value, property|
@@ -1,15 +1,11 @@
1
- module SDL
2
- module Receivers
3
- class FactReceiver < TypeReceiver
4
- def base_class
5
- SDL::Base::Fact
6
- end
7
-
8
- def register_sdltype(type)
9
- false
10
- end
1
+ class SDL::Receivers::FactReceiver < SDL::Receivers::TypeReceiver
2
+ def base_class
3
+ SDL::Base::Fact
4
+ end
11
5
 
12
- alias :subfact :subtype
13
- end
6
+ def register_sdltype(type)
7
+ false
14
8
  end
9
+
10
+ alias :subfact :subtype
15
11
  end
@@ -1,63 +1,57 @@
1
1
  require 'verbs'
2
2
 
3
- require_relative '../base/service'
3
+ class SDL::Receivers::ServiceReceiver
4
+ include ActiveSupport::Inflector
4
5
 
5
- module SDL
6
- module Receivers
7
- class ServiceReceiver
8
- include ActiveSupport::Inflector
6
+ attr :service
7
+ attr :compendium
9
8
 
10
- attr :service
11
- attr :compendium
9
+ def initialize(sym, compendium)
10
+ @service = SDL::Base::Service.new(sym.to_s)
11
+ @compendium = compendium
12
12
 
13
- def initialize(sym, compendium)
14
- @service = SDL::Base::Service.new(sym.to_s)
15
- @compendium = compendium
16
-
17
- compendium.fact_classes.each do |fact_class|
18
- define_singleton_method("is_#{fact_class.local_name.underscore.verb.conjugate(:tense => :past, :person => :third, :plurality => :singular, :aspect => :perfective)}") do |*args, &block|
19
- add_fact fact_class, *args, &block
20
- end
13
+ compendium.fact_classes.each do |fact_class|
14
+ define_singleton_method("is_#{fact_class.local_name.underscore.verb.conjugate(:tense => :past, :person => :third, :plurality => :singular, :aspect => :perfective)}") do |*args, &block|
15
+ add_fact fact_class, *args, &block
16
+ end
21
17
 
22
- define_singleton_method("has_#{fact_class.local_name.underscore}") do |*args, &block|
23
- add_fact fact_class, *args, &block
24
- end
18
+ define_singleton_method("has_#{fact_class.local_name.underscore}") do |*args, &block|
19
+ add_fact fact_class, *args, &block
20
+ end
25
21
 
26
- define_singleton_method("#{fact_class.local_name.underscore}") do |*args, &block|
27
- add_fact fact_class, *args, &block
28
- end
29
- end
22
+ define_singleton_method("#{fact_class.local_name.underscore}") do |*args, &block|
23
+ add_fact fact_class, *args, &block
30
24
  end
25
+ end
26
+ end
31
27
 
32
- private
33
- def add_fact(fact_class, *property_values, &block)
34
- fact_instance = fact_class.new
35
- fact_instance.service = @service
28
+ private
29
+ def add_fact(fact_class, *property_values, &block)
30
+ fact_instance = fact_class.new
31
+ fact_instance.service = @service
36
32
 
37
- SDL::Receivers.set_value(fact_class, fact_instance, *property_values, @compendium)
33
+ SDL::Receivers.set_value(fact_class, fact_instance, *property_values, @compendium)
38
34
 
39
- if block_given?
40
- SDL::Receivers::TypeInstanceReceiver.new(fact_instance, @compendium).instance_eval &block
41
- end
35
+ if block_given?
36
+ SDL::Receivers::TypeInstanceReceiver.new(fact_instance, @compendium).instance_eval &block
37
+ end
42
38
 
43
- @service.facts << fact_instance
44
- end
39
+ @service.facts << fact_instance
40
+ end
45
41
 
46
- ##
47
- # Catches calls to methods named similarily to possible predefined type instances
48
- def method_missing(name, *args)
49
- possible_type_instances = @compendium.type_instances.map{|k, v| v[name]}.select{|v| v != nil}
50
-
51
- unless possible_type_instances.nil? || possible_type_instances.empty?
52
- if possible_type_instances.length > 1
53
- raise Exception.new("Multiple possibilities for #{name} in #{caller[0]}")
54
- else
55
- possible_type_instances[0]
56
- end
57
- else
58
- raise Exception.new("I do not know what to do with '#{name}' in #{caller[0]}")
59
- end
42
+ ##
43
+ # Catches calls to methods named similarily to possible predefined type instances
44
+ def method_missing(name, *args)
45
+ possible_type_instances = @compendium.type_instances.map{|k, v| v[name]}.select{|v| v != nil}
46
+
47
+ unless possible_type_instances.nil? || possible_type_instances.empty?
48
+ if possible_type_instances.length > 1
49
+ raise Exception.new("Multiple possibilities for #{name} in #{caller[0]}")
50
+ else
51
+ possible_type_instances[0]
60
52
  end
53
+ else
54
+ raise Exception.new("I do not know what to do with '#{name}' in #{caller[0]}")
55
+ end
61
56
  end
62
- end
63
57
  end