bio-publisci 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,25 +1,16 @@
1
1
  module PubliSci
2
2
  module Prov
3
3
  class Association
4
- def subject(sub=nil)
5
- if sub
6
- @subject = sub
7
- else
8
- @subject ||= "#{Prov.base_url}/assoc/#{Time.now.nsec.to_s(32)}"
9
- end
4
+ include Prov::Element
5
+
6
+
7
+ def __label
8
+ # raise "MissingInternalLabel: no __label for #{self.inspect}" unless @__label
9
+ @__label ||= Time.now.nsec.to_s(32)
10
10
  end
11
11
 
12
12
  def agent(agent=nil)
13
- if agent
14
- # agent = Prov.agents[agent.to_sym] if agent.is_a?(String) || agent.is_a?(Symbol)
15
- # raise "UnkownAgent #{ag}" unless agent
16
- # puts "Warning: overwriting agent #{@agent.subject}" if @agent
17
- @agent = agent
18
- elsif @agent.is_a? Symbol
19
- @agent = Prov.agents[@agent]
20
- else
21
- @agent
22
- end
13
+ basic_keyword(:agent,:agents,agent)
23
14
  end
24
15
 
25
16
  def had_plan(*args, &block)
@@ -31,15 +22,18 @@ module Prov
31
22
  Prov.register(args[0], p)
32
23
  elsif args.size == 0
33
24
  if @plan.is_a? Symbol
25
+ raise "UnknownPlan: #{@plan}" unless Prov.plans[@plan]
34
26
  @plan = Prov.plans[@plan]
35
27
  end
36
28
  @plan
37
29
  elsif args.size == 1
38
- if args[0].is_a? Symbol
39
- raise "UnknownPlan: #{args[0]}" unless Prov.plans[args[0]]
40
- @plan = Prov.plans[args[0]]
41
- else
30
+ if Prov.plans[args[0]]
42
31
  @plan = args[0]
32
+ else
33
+ p = Prov::Plan.new
34
+ p.__label=args[0]
35
+ @plan = p
36
+ Prov.register(args[0], p)
43
37
  end
44
38
  else
45
39
  name = args.shift
@@ -57,10 +51,50 @@ module Prov
57
51
  end
58
52
  alias_method :plan, :had_plan
59
53
 
54
+ def had_role(*args, &block)
55
+ if block_given?
56
+ p = Prov::Role.new
57
+ p.instance_eval(&block)
58
+ p.__label=args[0]
59
+ @role = p
60
+ # puts p.class
61
+ Prov.register(args[0], p)
62
+ elsif args.size == 0
63
+ if @role.is_a? Symbol
64
+ raise "UnknownRole: #{@role}" unless (Prov.registry[:role]||={})[@role]
65
+ @role = Prov.registry[:role][@role]
66
+ end
67
+ @role
68
+ elsif args.size == 1
69
+ if (Prov.registry[:role]||={})[args[0]]
70
+ @role = args[0]
71
+ else
72
+ p = Prov::Role.new
73
+ p.__label=args[0]
74
+ @role = p
75
+ Prov.register(args[0], p)
76
+ end
77
+ else
78
+ name = args.shift
79
+ args = Hash[*args]
80
+ p = Prov::Role.new
81
+
82
+ p.__label=name
83
+ p.subject args[:subject]
84
+ (args.keys - [:subject]).map{|k|
85
+ raise "Unkown Role setting #{k}" unless try_auto_set(p,k,args[k])
86
+ }
87
+ @role = p
88
+ Prov.register(name, p)
89
+ end
90
+ end
91
+ alias_method :role, :had_role
92
+
60
93
  def to_n3
61
94
  str = "<#{subject}> a prov:Association ;\n"
62
95
  str << "\tprov:agent <#{agent}> ;\n"
63
96
  str << "\tprov:hadPlan <#{plan}> ;\n" if plan
97
+ str << "\tprov:hadRole <#{role}> ;\n" if role
64
98
  str[-2] = ".\n"
65
99
  str
66
100
  end
@@ -16,6 +16,7 @@ module PubliSci
16
16
  if activity
17
17
  @had_activity = activity
18
18
  elsif @had_activity.is_a? Symbol
19
+ raise "UnknownActivity #{@had_activity}" unless Prov.activities[@had_activity]
19
20
  @had_activity = Prov.activities[@had_activity]
20
21
  else
21
22
  @had_activity
@@ -27,6 +28,7 @@ module PubliSci
27
28
  if entity
28
29
  @entity = entity
29
30
  elsif @entity.is_a? Symbol
31
+ raise "UnknownEntity #{@entity}" unless Prov.entities[@entity]
30
32
  @entity = Prov.entities[@entity]
31
33
  else
32
34
  @entity
@@ -16,97 +16,40 @@ module PubliSci
16
16
  Prov.registry.clear
17
17
  end
18
18
 
19
- def agent(name,args={}, &block)
19
+ def named_element(name,element_class,args={},&block)
20
+ el = element_class.new
21
+ el.__label=name
20
22
  if block_given?
21
- a = Prov::Agent.new
22
- a.instance_eval(&block)
23
- a.__label=name
24
- Prov.register(name, a)
23
+ el.instance_eval(&block)
24
+ Prov.register(name,el)
25
25
  else
26
- # name = args.shift
27
- # args = Hash[*args]
28
- a = Prov::Agent.new
29
-
30
- a.__label=name
31
-
32
- a.subject args[:subject]
33
-
34
- (args.keys - [:subject]).map{|k|
35
- raise "Unkown agent setting #{k}" unless try_auto_set(a,k,args[k])
26
+ args.keys.map{|k|
27
+ raise "Unkown #{element_class} setting #{k}" unless try_auto_set(el,k,args[k])
36
28
  }
37
-
38
-
39
- Prov.register(name, a)
29
+ Prov.register(name,el)
40
30
  end
41
31
  end
42
32
 
33
+ def agent(name,args={}, &block)
34
+ named_element(name,Prov::Agent,args,&block)
35
+ end
36
+
43
37
  def organization(name,args={},&block)
44
38
  args[:type] = :organization
45
39
  agent(name,args,&block)
46
40
  end
47
41
 
48
42
  def entity(name, args={}, &block)
49
- if block_given?
50
- e = Prov::Entity.new
51
- e.instance_eval(&block)
52
- e.__label=name
53
- Prov.register(name, e)
54
- else
55
- # name = args.shift
56
- # args = Hash[*args]
57
- e = Prov::Entity.new
58
-
59
- e.__label=name
60
- e.subject args[:subject]
61
- (args.keys - [:subject]).map{|k|
62
- raise "Unkown entity setting #{k}" unless try_auto_set(e,k,args[k])
63
- }
64
-
65
- Prov.register(name, e)
66
- end
43
+ named_element(name,Prov::Entity,args,&block)
67
44
  end
68
45
  alias_method :data, :entity
69
46
 
70
47
  def plan(name, args={}, &block)
71
- if block_given?
72
- p = Prov::Plan.new
73
- p.instance_eval(&block)
74
- p.__label=name
75
- Prov.register(name, e)
76
- else
77
- p = Prov::Plan.new
78
-
79
- p.__label=name
80
- p.subject args[:subject]
81
- (args.keys - [:subject]).map{|k|
82
- raise "Unkown plan setting #{k}" unless try_auto_set(p,k,args[k])
83
- }
84
-
85
-
86
- Prov.register(name, p)
87
- end
48
+ named_element(name,Prov::Plan,args,&block)
88
49
  end
89
50
 
90
51
  def activity(name,args={}, &block)
91
- if block_given?
92
- act = Prov::Activity.new
93
- act.instance_eval(&block)
94
- act.__label=name
95
- Prov.register(name, act)
96
- else
97
-
98
- act.subject args[:subject]
99
-
100
- (args.keys - [:subject]).map{|k|
101
- raise "Unkown agent setting #{k}" unless try_auto_set(act,k,args[k])
102
- }
103
-
104
- a = Prov::Activity.new
105
-
106
- act.__label=name
107
- Prov.register(name, act)
108
- raise "has based activity creation not yet implemented"
109
- end
52
+ named_element(name,Prov::Activity,args,&block)
110
53
  end
111
54
 
112
55
  def base_url(url)
@@ -118,10 +61,12 @@ module PubliSci
118
61
  agents = Prov.agents.values.map(&:to_n3).join
119
62
  activities = Prov.activities.values.map(&:to_n3).join
120
63
  plans = Prov.plans.values.map(&:to_n3).join
121
- associations = Prov.associations.map(&:to_n3).join
122
- derivations = Prov.registry[:derivation].map(&:to_n3).join if Prov.registry[:derivation]
64
+ associations = Prov.registry[:associations].values.map(&:to_n3).join if Prov.registry[:associations]
65
+ derivations = Prov.registry[:derivation].values.map(&:to_n3).join if Prov.registry[:derivation]
66
+ usages = Prov.registry[:usage].values.map(&:to_n3).join if Prov.registry[:usage]
67
+ roles = Prov.registry[:role].values.map(&:to_n3).join if Prov.registry[:role]
123
68
 
124
- str = "#{entities}#{agents}#{activities}#{plans}#{associations}#{derivations}"
69
+ str = "#{entities}#{agents}#{activities}#{plans}#{associations}#{derivations}#{usages}#{roles}"
125
70
 
126
71
  if abbreviate
127
72
  abbreviate_known(str)
@@ -134,10 +79,20 @@ module PubliSci
134
79
  Prov.registry
135
80
  end
136
81
 
137
- # def vocabulary(url)
138
- # raise "InvalidVocabulary: #{url} is not a valid URI" unless RDF::Resource(url).valid?
139
- # RDF::Vocabulary.new(url)
140
- # end
82
+ def to_repository(repo=:in_memory,turtle_string=(Prov.prefixes+generate_n3))
83
+ case repo
84
+ when :in_memory
85
+ repo = RDF::Repository.new
86
+ when :fourstore
87
+ repo = RDF::FourStore::Repository.new('http://localhost:8080')
88
+ end
89
+ f = Tempfile.new('repo')
90
+ f.write(turtle_string)
91
+ f.close
92
+ repo.load(f.path, :format => :ttl)
93
+ f.unlink
94
+ repo
95
+ end
141
96
 
142
97
  private
143
98
  def try_auto_set(object,method,args)
@@ -151,11 +106,12 @@ module PubliSci
151
106
 
152
107
  def abbreviate_known(turtle)
153
108
  ttl = turtle.dup
154
- %w{activity assoc agent plan entity derivation}.each{|element|
109
+ %w{activity assoc agent plan entity derivation usage role}.each{|element|
155
110
  ttl.gsub!(%r{<#{Prov.base_url}/#{element}/([\w|\d]+)>}, "#{element}:" + '\1')
156
111
  }
157
112
 
158
113
  ttl.gsub!(%r{<http://gsocsemantic.wordpress.com/([\w|\d]+)>}, 'me:\1')
114
+ ttl.gsub!(%r{<http://www.w3.org/ns/prov#([\w|\d]+)>}, 'prov:\1')
159
115
  ttl
160
116
  end
161
117
  end
@@ -20,6 +20,10 @@ module PubliSci
20
20
  @subject = s
21
21
  end
22
22
 
23
+ def subject_id
24
+ self.class.to_s.split('::').last.downcase
25
+ end
26
+
23
27
  def __label=(l)
24
28
  @__label = l
25
29
  end
@@ -41,12 +45,76 @@ module PubliSci
41
45
  "activity"
42
46
  when Plan
43
47
  "plan"
48
+ when Association
49
+ "assoc"
44
50
  else
45
- raise "MissingSubject: No automatic subject generation for #{self}"
51
+ subject_id()
52
+ # raise "MissingSubject: No automatic subject generation for #{self}"
46
53
  end
47
54
 
48
55
  "#{Prov.base_url}/#{category}/#{__label}"
49
56
  end
57
+
58
+ def try_auto_set(object,method,args)
59
+ if object.methods.include? method
60
+ object.send(method,args)
61
+ true
62
+ else
63
+ false
64
+ end
65
+ end
66
+
67
+ def basic_keyword(var,type,identifier=nil)
68
+ ivar = instance_variable_get("@#{var}")
69
+
70
+ if identifier
71
+ instance_variable_set("@#{var}", identifier)
72
+ elsif ivar.is_a? Symbol
73
+ raise "NotRegistered: #{type}" unless Prov.registry[type]
74
+ raise "Unknown#{type.capitalize}: #{ivar}" unless Prov.registry[type][ivar]
75
+ instance_variable_set("@#{var}", Prov.registry[type][ivar])
76
+ else
77
+ ivar
78
+ end
79
+ end
80
+
81
+ def basic_list(var,type,collection_class,identifier=nil)
82
+
83
+ if identifier
84
+ unless instance_variable_get("@#{var}")
85
+ instance_variable_set("@#{var}",collection_class.new)
86
+ end
87
+ instance_variable_get("@#{var}") << identifier
88
+ # (@used ||= Uses.new) << identifier
89
+ # elsif @used
90
+ # @used.map{|u| u.is_a?(Symbol) ? Prov.entities[u] : u}
91
+ # elsif instance_variable_get("@#{var}")
92
+ # @used.dereference
93
+ else
94
+ instance_variable_get("@#{var}")
95
+ end
96
+ end
97
+
98
+ def block_list(var,type,instance_class,collection_class,name=nil,&block)
99
+ if block_given?
100
+ inst = instance_class.new
101
+ inst.instance_eval(&block)
102
+ unless instance_variable_get("@#{var}")
103
+ instance_variable_set("@#{var}",collection_class.new)
104
+ end
105
+ instance_variable_get("@#{var}") << inst
106
+ Prov.register(type,inst)
107
+ else
108
+ if name
109
+ unless instance_variable_get("@#{var}")
110
+ instance_variable_set("@#{var}",collection_class.new)
111
+ end
112
+ instance_variable_get("@#{var}") << name
113
+ else
114
+ instance_variable_get("@#{var}")
115
+ end
116
+ end
117
+ end
50
118
  end
51
119
  end
52
120
  end
@@ -1,17 +1,18 @@
1
1
  module PubliSci
2
2
  module Prov
3
3
  class Entity
4
+ include Prov::Element
5
+
6
+ attr_accessor :derived_from
7
+ attr_accessor :attributed_to
8
+
4
9
  class Derivations < Array
5
- def [](index)
6
- if self.fetch(index).is_a? Symbol
7
- Prov.entities[self.fetch(index)]
8
- else
9
- self.fetch(index)
10
- end
10
+ include PubliSci::Prov::Dereferencable
11
+ def method
12
+ :entities
11
13
  end
12
14
  end
13
15
 
14
- include Prov::Element
15
16
 
16
17
  def source(s=nil)
17
18
  if s
@@ -22,57 +23,45 @@ module PubliSci
22
23
  end
23
24
 
24
25
  def generated_by(activity=nil)
25
- if activity
26
- @generated_by = activity
27
- elsif @generated_by.is_a? Symbol
28
- @generated_by = Prov.activities[@generated_by]
29
- else
30
- @generated_by
31
- end
26
+ basic_keyword(:generated_by,:activities,activity)
32
27
  end
33
28
 
34
29
  def attributed_to(agent=nil)
35
- if agent
36
- @attributed_to = agent
37
- elsif @attributed_to.is_a? Symbol
38
- @attributed_to = Prov.agents[@attributed_to]
39
- else
40
- @attributed_to
41
- end
30
+ basic_keyword(:attributed_to,:agents,agent)
31
+ # if agent
32
+ # @attributed_to = agent
33
+ # elsif @attributed_to.is_a? Symbol
34
+ # raise "UnknownAgent: #{@attributed_to}" unless Prov.agents[@attributed_to]
35
+ # @attributed_to = Prov.agents[@attributed_to]
36
+ # else
37
+ # @attributed_to
38
+ # end
42
39
  end
43
40
 
44
41
  def derived_from(entity=nil,&block)
45
- if block_given?
46
- deriv = Derivation.new
47
- deriv.instance_eval(&block)
48
- (@derived_from ||= Derivations.new) << deriv
49
- Prov.register(nil,deriv)
50
- else
51
- if entity
52
- (@derived_from ||= Derivations.new) << entity
53
- else
54
- @derived_from
55
- end
56
- end
57
- end
42
+ block_list(:derived_from,:derivations,Derivation,Derivations,entity,&block)
43
+ # if block_given?
44
+ # deriv = Derivation.new
45
+ # deriv.instance_eval(&block)
46
+ # (@derived_from ||= Derivations.new) << deriv
47
+ # Prov.register(nil,deriv)
48
+ # else
49
+ # if entity
58
50
 
59
- # def derived_from[](entity)
60
- # if @derived_from && @derived_from[entity]
61
- # if entity.is_a? Symbol
62
- # Prov.entities[entity]
63
- # else
64
- # entity
65
- # end
66
- # end
67
- # end
51
+ # (@derived_from ||= Derivations.new) << entity
52
+ # else
53
+ # @derived_from
54
+ # end
55
+ # end
56
+ end
68
57
 
69
58
  def to_n3
70
59
  str = "<#{subject}> a prov:Entity ;\n"
71
60
  str << "\tprov:wasGeneratedBy <#{generated_by}> ;\n" if generated_by
72
61
  str << "\tprov:wasAttributedTo <#{attributed_to}> ;\n" if attributed_to
73
62
  if derived_from
74
- derived_from.map{|der|
75
- der = Prov.entities[der] if der.is_a?(Symbol) && Prov.entities[der]
63
+ derived_from.size.times.each{|k|
64
+ der = derived_from[k] # if der.is_a?(Symbol) && Prov.entities[der]
76
65
 
77
66
  if der.is_a? Derivation
78
67
  str << "\tprov:wasDerivedFrom <#{der.entity}> ;\n"
@@ -83,11 +72,6 @@ module PubliSci
83
72
  }
84
73
  end
85
74
 
86
- # if custom
87
- # @custom.map{|k,v|
88
- # str << "\t<#{k.to_s}> <#{v.to_s}> ;\n"
89
- # }
90
- # end
91
75
  add_custom(str)
92
76
 
93
77
  str << %Q(\trdfs:label "#{__label}" .\n\n)