rtm-activerecord 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/DISCLAIMER +13 -0
  2. data/LICENSE +201 -0
  3. data/README +4 -0
  4. data/lib/rtm/activerecord/001_initial_schema.rb +119 -0
  5. data/lib/rtm/activerecord/association_and_role.rb +54 -0
  6. data/lib/rtm/activerecord/base.rb +83 -0
  7. data/lib/rtm/activerecord/connect.rb +106 -0
  8. data/lib/rtm/activerecord/io/from_xtm2.rb +266 -0
  9. data/lib/rtm/activerecord/io/from_xtm2_libxml.rb +97 -0
  10. data/lib/rtm/activerecord/io/to_jtm.rb +141 -0
  11. data/lib/rtm/activerecord/io/to_string.rb +130 -0
  12. data/lib/rtm/activerecord/io/to_xtm1.rb +162 -0
  13. data/lib/rtm/activerecord/io/to_xtm2.rb +163 -0
  14. data/lib/rtm/activerecord/io/to_yaml.rb +132 -0
  15. data/lib/rtm/activerecord/literal_index.rb +38 -0
  16. data/lib/rtm/activerecord/locators.rb +58 -0
  17. data/lib/rtm/activerecord/merging.rb +310 -0
  18. data/lib/rtm/activerecord/name_variant_occurrence.rb +86 -0
  19. data/lib/rtm/activerecord/persistent_code_output.rb +22 -0
  20. data/lib/rtm/activerecord/quaaxtm2rtm.rb +116 -0
  21. data/lib/rtm/activerecord/quaaxtm2rtmviews.rb +137 -0
  22. data/lib/rtm/activerecord/set_wrapper.rb +101 -0
  23. data/lib/rtm/activerecord/sugar/association/hash_access.rb +22 -0
  24. data/lib/rtm/activerecord/sugar/role/counterparts.rb +56 -0
  25. data/lib/rtm/activerecord/sugar/topic/characteristics.rb +15 -0
  26. data/lib/rtm/activerecord/sugar/topic/counterparts.rb +18 -0
  27. data/lib/rtm/activerecord/sugar/topic/hash_access.rb +78 -0
  28. data/lib/rtm/activerecord/sugar/topic/identifier_direct.rb +14 -0
  29. data/lib/rtm/activerecord/sugar/topic/predefined_associations.rb +53 -0
  30. data/lib/rtm/activerecord/tm_construct.rb +66 -0
  31. data/lib/rtm/activerecord/tm_delegator.rb +417 -0
  32. data/lib/rtm/activerecord/tm_set_delegator.rb +226 -0
  33. data/lib/rtm/activerecord/tmdm.rb +309 -0
  34. data/lib/rtm/activerecord/topic.rb +204 -0
  35. data/lib/rtm/activerecord/topic_map.rb +435 -0
  36. data/lib/rtm/activerecord/traverse_associations.rb +90 -0
  37. data/lib/rtm/activerecord.rb +106 -0
  38. metadata +120 -0
@@ -0,0 +1,86 @@
1
+ # Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig.
2
+ # License: Apache License, Version 2.0
3
+
4
+ module RTM::AR
5
+ class Name < Reifiable
6
+ include RTM::Name
7
+ wrapper_cache
8
+
9
+ parent :topic
10
+ property :type, :aka => [:ty,:t], :rw => true, :type => :Topic, :wrap => true
11
+ property_set :scope, :type => :Topic, :wrap => true, :add => :scope, :remove => :scope
12
+
13
+ property_set :variants, :aka => :vs, :type => :Variant, :wrap => true,
14
+ :create => :variant_internal, :create_aka => :cv,
15
+ :create_args => [ {:name => :value, :type => [:String, :Locator]}, {:name => :scope, :type => :Collection}]
16
+
17
+ def create_variant(arg1, arg2, arg3 = :nothing)
18
+ if arg1.is_a?(RTM::Locator)
19
+ raise("create_variant(value,scope), value: Locator, scope must be an Array") unless arg2.is_a?(Array)
20
+ if arg2.empty?
21
+ raise("create_variant(value,scope), value: Locator, scope must not be empty")
22
+ end
23
+ arg2.each do |theme|
24
+ raise("create_variant(value,scope), value: Locator, scope must contain Topic/Topic-References") unless (theme.is_a?(RTM::Topic) || theme.is_a?(String) || theme.is_a?(RTM::Locator))
25
+ end
26
+ return create_variant_internal(arg1, topic_map._topic_by_locator!(arg2) + self.scope.map{|t| t.__getobj__})
27
+ elsif arg1.is_a?(String)
28
+ if arg3 == :nothing
29
+ raise("create_variant(value,scope), value: String, scope must be an Array") unless arg2.is_a?(Array)
30
+ if arg2.empty?
31
+ raise("create_variant(value,scope), value: String, scope must not be empty")
32
+ end
33
+ arg2.each do |theme|
34
+ raise("create_variant(value,scope), value: String, scope must contain Topic/Topic-References") unless (theme.is_a?(RTM::Topic) || theme.is_a?(String) || theme.is_a?(RTM::Locator))
35
+ end
36
+ return create_variant_internal(arg1, topic_map._topic_by_locator!(arg2) + self.scope.map{|t| t.__getobj__})
37
+ elsif (arg2.is_a?(Locator) || arg2.is_a?(String))
38
+ if ((arg2 == RTM::PSI[:String]) || (arg2 == topic_map.create_locator(RTM::PSI[:String])))
39
+ return create_variant_internal(arg1, topic_map.create_locator(RTM::PSI[:String]), topic_map._topic_by_locator!(arg3) + self.scope.map{|t| t.__getobj__})
40
+ elsif ((arg2 == RTM::PSI[:IRI]) || (arg2 == topic_map.create_locator(RTM::PSI[:IRI])))
41
+ return create_variant_internal(arg1, topic_map.create_locator(RTM::PSI[:IRI]), topic_map._topic_by_locator!(arg3) + self.scope.map{|t| t.__getobj__})
42
+ elsif ((arg2 == RTM::PSI[:XML]) || (arg2 == topic_map.create_locator(RTM::PSI[:XML])))
43
+ return create_variant_internal(arg1, topic_map.create_locator(RTM::PSI[:XML]), topic_map._topic_by_locator!(arg3) + self.scope.map{|t| t.__getobj__})
44
+ else
45
+ raise("create_variant(value,datatype,scope), value: String, unknown datatype")
46
+ end
47
+ else
48
+ raise("create_variant(value,datatype,scope), datatype must be a String (identifier) or Locator")
49
+ end
50
+ else
51
+ raise "create_variant: value not a Locator or String"
52
+ end
53
+ end
54
+
55
+ property :value, :rw => true, :type => :String, :aka => :v
56
+
57
+ equality [:value, :type, :scope, :parent]
58
+ end
59
+ TopicName = Name
60
+
61
+ class Variant < Reifiable
62
+ include RTM::Variant
63
+ wrapper_cache
64
+
65
+ parent :name, :aka => :topic_name
66
+ property :value, :rw => true, :type => :String, :aka => :v
67
+ property :datatype, :rw => true, :type => :Locator #, :wrap => true
68
+ property_set :scope, :type => :Topic, :wrap => true, :add => :scope, :remove => :scope
69
+
70
+ equality [:value, :datatype, :scope, :parent]
71
+ end
72
+
73
+ class Occurrence < Reifiable
74
+ include RTM::Occurrence
75
+ wrapper_cache
76
+
77
+ parent :topic
78
+ property :value, :rw => true, :type => :String, :aka => :v
79
+ property :datatype, :rw => true, :type => :String
80
+
81
+ property_set :scope, :type => :Topic, :wrap => true, :add => :scope, :remove => :scope
82
+ property :type, :aka => [:ty,:t], :rw => true, :type => :Topic, :wrap => true
83
+
84
+ equality [:value, :datatype, :scope, :type, :parent]
85
+ end
86
+ end
@@ -0,0 +1,22 @@
1
+ # Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig.
2
+ # License: Apache License, Version 2.0
3
+
4
+ enable_persistent_code_output = false
5
+ class Module
6
+ alias :module_eval_without_output :module_eval
7
+ def module_eval(code,file,line)
8
+ module_eval_without_output(code,file,line)
9
+ File.open("/tmp/rtm.rb","a") do |f|
10
+ #puts self, self.class
11
+ @@first_class = true
12
+ @@last_self ||= nil
13
+ if ! @@last_self || @@last_self != self
14
+ f.puts "end\n\n" unless @@first_class
15
+ @@first_class = false
16
+ @@last_self = self
17
+ f.puts "#{self.class.to_s.downcase} #{self}"
18
+ end
19
+ f.puts code
20
+ end
21
+ end
22
+ end if enable_persistent_code_output
@@ -0,0 +1,116 @@
1
+ # Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig.
2
+ # License: Apache License, Version 2.0
3
+
4
+ require 'active_record'
5
+
6
+
7
+ module RTM
8
+ module AR
9
+ module TMDM
10
+ class Topic < ActiveRecord::Base;end
11
+ class ItemIdentifier < ActiveRecord::Base
12
+ belongs_to :topic_map_construct
13
+ end
14
+ class SubjectIdentifier < ActiveRecord::Base
15
+ belongs_to :topic
16
+ end
17
+
18
+ class SubjectLocator < ActiveRecord::Base
19
+ belongs_to :topic
20
+ end
21
+
22
+ class QuaaxTM2RTM < ActiveRecord::Migration
23
+ def self.up
24
+ rename_table("qtm_topicmap", "topic_maps" )
25
+ rename_column("topic_maps", "baselocator", "base_locator")
26
+
27
+ rename_table("qtm_topic", "topics")
28
+ rename_column("topics", "topicmap_id", "topic_map_id")
29
+
30
+ add_column :topics, :reified_id, :integer
31
+ add_column :topics, :reified_type, :string
32
+
33
+ rename_table "qtm_association", "associations"
34
+ rename_column "associations", "type_id", "ttype_id"
35
+ rename_column "associations", "topicmap_id", "topic_map_id"
36
+
37
+ rename_table "qtm_assocrole", "association_roles"
38
+ rename_column "association_roles", "type_id", "ttype_id"
39
+ rename_column "association_roles", "reference", "topic_id"
40
+
41
+ rename_table "qtm_topicname", "topic_names"
42
+ rename_column "topic_names", "type_id", "ttype_id"
43
+
44
+ rename_table "qtm_occurrence", "occurrences"
45
+ rename_column "occurrences", "type_id", "ttype_id"
46
+
47
+ rename_table "qtm_variant", "variants"
48
+ rename_column "variants", "topicname_id", "topic_name_id"
49
+
50
+ rename_table "qtm_topicmapconstructref", "item_identifiers"
51
+ rename_column "item_identifiers", "locator", "reference"
52
+ add_column :item_identifiers, :topic_map_id, :integer
53
+ ItemIdentifier.reset_column_information
54
+ ItemIdentifier.find(:all).each do |si|
55
+ si.update_attribute :topic_map_id, si.topic_map_construct.topic_map_id
56
+ end
57
+
58
+ create_table :item_identifiers do |t|
59
+ t.column :topic_map_id, :integer, :null => false
60
+ t.column :reference, :string, :null => false
61
+ t.column :topic_map_construct_id, :integer
62
+ t.column :topic_map_construct_type, :string
63
+ end
64
+ SubjectIdentifier.reset_column_information
65
+ SubjectIdentifier.find(:all).each do |si|
66
+ si.update_attribute :topic_map_id, si.topic.topic_map_id
67
+ end
68
+
69
+ rename_table "qtm_subjectidentifier", "subject_identifiers"
70
+ rename_column :subject_identifiers, "locator", "reference"
71
+ add_column :subject_identifiers, :topic_map_id, :integer
72
+
73
+ SubjectIdentifier.reset_column_information
74
+ SubjectIdentifier.find(:all).each do |si|
75
+ si.update_attribute :topic_map_id, si.topic.topic_map_id
76
+ end
77
+
78
+ rename_table "qtm_subjectlocator", "subject_locators"
79
+ rename_column :subject_locators, "locator", "reference"
80
+ add_column :subject_locators, :topic_map_id, :integer
81
+
82
+ Topic.reset_column_information
83
+ SubjectLocator.reset_column_information
84
+ SubjectLocator.find(:all).each do |si|
85
+ si.update_attribute :topic_map_id, si.topic.topic_map_id
86
+ end
87
+
88
+ create_table :scoped_objects_topics do |t|
89
+ t.column :scoped_object_id, :integer
90
+ t.column :scoped_object_type, :string
91
+ t.column :topic_id, :integer
92
+ end
93
+
94
+ # TODO: instance of
95
+ # scope
96
+ # reifier
97
+ end
98
+
99
+ def self.down
100
+ raise "not supported"
101
+ end
102
+
103
+ end
104
+ end
105
+ end
106
+ end
107
+
108
+ #if __FILE__ == $0
109
+ # puts "Generating SQLite3 Databse tmdm.sqlite3."
110
+ # ActiveRecord::Base.establish_connection(
111
+ # :adapter => "sqlite3",
112
+ # :database => "tmdm.sqlite3"
113
+ # )
114
+ # RTM::AR::TMDM::QuaaxTM2RTM.migrate(:up)
115
+ #end
116
+
@@ -0,0 +1,137 @@
1
+ # Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig.
2
+ # License: Apache License, Version 2.0
3
+
4
+ require 'active_record'
5
+
6
+
7
+ module RTM
8
+ module AR
9
+ module TMDM
10
+ class Topic < ActiveRecord::Base;end
11
+ class ItemIdentifier < ActiveRecord::Base
12
+ belongs_to :topic_map_construct
13
+ end
14
+ class SubjectIdentifier < ActiveRecord::Base
15
+ belongs_to :topic
16
+ end
17
+
18
+ class SubjectLocator < ActiveRecord::Base
19
+ belongs_to :topic
20
+ end
21
+
22
+ class QuaaxTM2RTMViews < ActiveRecord::Migration
23
+ def self.up
24
+ create_view "topic_maps", "select id, baselocator from qtm_topicmap" do |t|
25
+ t.column :id
26
+ t.column :base_locator
27
+ end
28
+
29
+ create_view "topics", "select id, topicmap_id, null, null from qtm_topic" do |t|
30
+ t.column :id
31
+ t.column :topic_map_id
32
+ t.column :reified_id
33
+ t.column :reified_type
34
+ end
35
+
36
+ create_view "associations", "select id, topicmap_id, type_id from qtm_association" do |t|
37
+ t.column :id
38
+ t.column :topic_map_id
39
+ t.column :ttype_id
40
+ end
41
+
42
+ create_view "association_roles", "select id, association_id, type_id, reference from qtm_assocrole" do |t|
43
+ t.column :id
44
+ t.column :association_id
45
+ t.column :ttype_id
46
+ t.column :topic_id
47
+ end
48
+
49
+ create_view "topic_names", "select id, topic_id, type_id, value from qtm_topicname" do |t|
50
+ t.column :id
51
+ t.column :topic_id
52
+ t.column :ttype_id
53
+ t.column :value
54
+ end
55
+
56
+ create_view "occurrence", "select id, topic_id, type_id, value, datatype from qtm_occurrence" do |t|
57
+ t.column :id
58
+ t.column :topic_id
59
+ t.column :ttype_id
60
+ t.column :value
61
+ t.column :datatype
62
+ end
63
+
64
+ create_view "variants", "select id, topicname_id, value, datatype from qtm_variant" do |t|
65
+ t.column :id
66
+ t.column :topic_name_id
67
+ t.column :value
68
+ t.column :datatype
69
+ end
70
+
71
+ rename_table "qtm_topicmapconstructref", "item_identifiers"
72
+ rename_column "item_identifiers", "locator", "reference"
73
+ add_column :item_identifiers, :topic_map_id, :integer
74
+ ItemIdentifier.reset_column_information
75
+ ItemIdentifier.find(:all).each do |si|
76
+ si.update_attribute :topic_map_id, si.topic_map_construct.topic_map_id
77
+ end
78
+
79
+ create_table :item_identifiers do |t|
80
+ t.column :topic_map_id, :integer, :null => false
81
+ t.column :reference, :string, :null => false
82
+ t.column :topic_map_construct_id, :integer
83
+ t.column :topic_map_construct_type, :string
84
+ end
85
+ SubjectIdentifier.reset_column_information
86
+ SubjectIdentifier.find(:all).each do |si|
87
+ si.update_attribute :topic_map_id, si.topic.topic_map_id
88
+ end
89
+
90
+ rename_table "qtm_subjectidentifier", "subject_identifiers"
91
+ rename_column :subject_identifiers, "locator", "reference"
92
+ add_column :subject_identifiers, :topic_map_id, :integer
93
+
94
+ SubjectIdentifier.reset_column_information
95
+ SubjectIdentifier.find(:all).each do |si|
96
+ si.update_attribute :topic_map_id, si.topic.topic_map_id
97
+ end
98
+
99
+ rename_table "qtm_subjectlocator", "subject_locators"
100
+ rename_column :subject_locators, "locator", "reference"
101
+ add_column :subject_locators, :topic_map_id, :integer
102
+
103
+ Topic.reset_column_information
104
+ SubjectLocator.reset_column_information
105
+ SubjectLocator.find(:all).each do |si|
106
+ si.update_attribute :topic_map_id, si.topic.topic_map_id
107
+ end
108
+
109
+ create_table :scoped_objects_topics do |t|
110
+ t.column :scoped_object_id, :integer
111
+ t.column :scoped_object_type, :string
112
+ t.column :topic_id, :integer
113
+ end
114
+
115
+ # TODO: instance of
116
+ # scope
117
+ # reifier
118
+ end
119
+
120
+ def self.down
121
+ raise "not supported"
122
+ end
123
+
124
+ end
125
+ end
126
+ end
127
+ end
128
+
129
+ #if __FILE__ == $0
130
+ # puts "Generating SQLite3 Databse tmdm.sqlite3."
131
+ # ActiveRecord::Base.establish_connection(
132
+ # :adapter => "sqlite3",
133
+ # :database => "tmdm.sqlite3"
134
+ # )
135
+ # RTM::AR::TMDM::QuaaxTM2RTM.migrate(:up)
136
+ #end
137
+
@@ -0,0 +1,101 @@
1
+ # Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig.
2
+ # License: Apache License, Version 2.0
3
+
4
+ module RTM::AR
5
+ class Topics < TMSetDelegator
6
+ def wrap(obj)
7
+ Topic.wrap(obj)
8
+ end
9
+ end
10
+ class Constructs < TMSetDelegator
11
+ def wrap(obj)
12
+ Construct.wrap(obj)
13
+ end
14
+ end
15
+ class Associations < TMSetDelegator
16
+ def wrap(obj)
17
+ Association.wrap(obj)
18
+ end
19
+ end
20
+ class Names < TMSetDelegator
21
+ def wrap(obj)
22
+ Name.wrap(obj)
23
+ end
24
+ end
25
+ class Occurrences < TMSetDelegator
26
+ def wrap(obj)
27
+ Occurrence.wrap(obj)
28
+ end
29
+ end
30
+ class Roles < TMSetDelegator
31
+ def wrap(obj)
32
+ Role.wrap(obj)
33
+ end
34
+ end
35
+ class Reifiables < TMSetDelegator
36
+ def wrap(obj)
37
+ Construct.wrap(obj)
38
+ end
39
+ end
40
+ class Variants < TMSetDelegator
41
+ def wrap(obj)
42
+ Variant.wrap(obj)
43
+ end
44
+ end
45
+ class TopicMaps < TMSetDelegator
46
+ def [](obj)
47
+ # support for getting topic_map by base_locator
48
+ if obj.is_a? String
49
+ # return wrap(__getobj__.find { |tm| tm.base_locator == obj })
50
+ return wrap(TMDM::TopicMap.find_by_base_locator(obj))
51
+ end
52
+ # normal index retrieval
53
+ super
54
+ end
55
+
56
+ def wrap(obj)
57
+ TopicMap.wrap(obj)
58
+ end
59
+ end
60
+ class Locators < TMSetDelegator
61
+ def [](obj)
62
+ # support for getting locators by reference
63
+ if obj.is_a? String
64
+ return wrap(__getobj__.find { |tm| tm.reference == obj })
65
+ end
66
+ # normal index retrieval
67
+ super
68
+ end
69
+ def wrap(obj)
70
+ return nil unless obj
71
+ return obj if obj.respond_to? :__getobj__
72
+ case obj.class.name
73
+ when "RTM::AR::TMDM::ItemIdentifier"
74
+ ItemIdentifier.wrap(obj)
75
+ when "RTM::AR::TMDM::SubjectIdentifier"
76
+ SubjectIdentifier.wrap(obj)
77
+ when "RTM::AR::TMDM::SubjectLocator"
78
+ SubjectLocator.wrap(obj)
79
+ else
80
+ Locator.wrap(obj)
81
+ end
82
+ end
83
+ end
84
+
85
+ class ItemIdentifiers < Locators
86
+ def wrap(obj)
87
+ ItemIdentifier.wrap(obj)
88
+ end
89
+ end
90
+ class SubjectIdentifiers < Locators
91
+ def wrap(obj)
92
+ SubjectIdentifier.wrap(obj)
93
+ end
94
+
95
+ end
96
+ class SubjectLocators < Locators
97
+ def wrap(obj)
98
+ SubjectLocator.wrap(obj)
99
+ end
100
+ end
101
+ end