rtriplify 0.0.0 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/rtriplify.rb CHANGED
@@ -1,17 +1,16 @@
1
1
  require 'configatron'
2
2
 
3
- #loading helpers and controllers
3
+ if File.exists?('config/triplify.yml')
4
+ #loading helpers and controllers
4
5
 
5
- %w{ models controllers helpers}.each do |dir|
6
- path = File.join(File.dirname(__FILE__), 'app', dir)
7
- $LOAD_PATH << path
8
- ActiveSupport::Dependencies.load_paths << path
9
- ActiveSupport::Dependencies.load_once_paths.delete(path)
6
+ %w{ models controllers helpers}.each do |dir|
7
+ path = File.join(File.dirname(__FILE__), 'app', dir)
8
+ $LOAD_PATH << path
9
+ ActiveSupport::Dependencies.load_paths << path
10
+ ActiveSupport::Dependencies.load_once_paths.delete(path)
11
+ end
12
+ # load settings
13
+ configatron.configure_from_yaml('config/triplify.yml')
10
14
  end
11
15
 
12
- # load settings
13
- configatron.configure_from_yaml('vendor/plugins/rtriplify/lib/config/database.yml')
14
-
15
-
16
-
17
16
 
data/lib/tripleizer.rb CHANGED
@@ -1,129 +1,348 @@
1
1
  require 'configatron'
2
+ require 'json'
2
3
 
3
4
  class Tripleizer
4
5
  def initialize output=nil
5
6
  @object_properties = configatron.objectProperties.to_hash
6
- @object_namespaches = configatron.namespaces.to_hash
7
+ @object_namespaces = configatron.namespaces.to_hash
7
8
  @class_map = configatron.classMap.to_hash
8
9
  @output=output
9
10
  @version="0.0.1"
10
-
11
+ @json_hash = Hash.new
12
+ @output_json = nil;
13
+ @base_uri = "www.base_uri"
11
14
  end
12
15
 
13
16
  attr_accessor :output
14
17
  attr_reader :version
15
-
16
- def tripleize (tr_self,c =nil,id=nil)
17
- #$this->writeTriple($self,$this->uri('rdfs:comment'),'Generated by Triplify '.$this->version.' (http://Triplify.org)',true);
18
-
19
- #if($this->config['license'])
20
- # $this->writeTriple($self,'http://creativecommons.org/ns#license',$this->config['license']);
21
-
22
-
23
- # configatron.queries.to_hash.each do |_class,q|
24
- # puts k.to_s+ " \n"
25
- # cols,group=nil,nil
26
- #
27
- # #TODO:mehrere Unterabfragen
28
- #
29
- # case configatron.LinkedDataDepth
30
- # when 3
31
- # if !id
32
- # return #datentiefe von 3 gibts laut config nicht TODO:abfangen
18
+ attr_accessor :output_json
19
+ attr_accessor :base_uri
20
+
21
+
22
+ # def all request,headers
23
+ # #get all models
24
+ # #render all models
25
+ # ret=""
26
+ # t = Tripleizer.new
27
+ # $base_uri= t.uri request.env['REQUEST_URI'].to_s[0..-5]
28
+ # #t.output_json = request.content_type.try(:to_sym)==:json
29
+ # #content_type = t.output_json ? 'application/json' : 'text/plain'
30
+ # ret << t.write_rdf_head
31
+ # model_groups = eval("configatron.query").to_hash
32
+ # model_groups.each do |model_group_name,model_group|
33
+ # model_group.each do |model_name,model_attributes|
34
+ # if model_name.to_s =="sql_query"
35
+ # ret<<t.write_sql(model_group_name,model_attributes,t.output)
36
+ # else
37
+ # ret<<t.write_model(model_name,model_group_name)
33
38
  # end
34
- # when 2
35
- # if !c
36
- # write_triple uri(_class),uri('rdf:type'), uri('owl:Class')
37
- # end
38
- # #TODO continue 2
39
- # return
40
- # when 1
41
- #
42
- # else
43
- # #TODO:DataDepthERROR
44
39
  # end
45
- # #TODO:some magic sql-query building
46
- #
47
- # #query sql
48
- # #for each result make_triples
49
- #
50
40
  # end
41
+ # t_metadata = TriplifyMetadata.new
42
+ # ret<<t_metadata.write_metadata(t).to_s
43
+ # ret
44
+ # end
45
+
46
+ def rdf rdf_start_class , var=[]
47
+ class_map = Hash.new
48
+ var.each do |c|
49
+ class_map[c[0].class.to_s]= c
50
+ end
51
+ rdf_string =""
52
+ #get configuration start ->classes
53
+ m = search_models(rdf_start_class)
54
+
55
+ m.values[0].each do |mod,attributes|
56
+ unless mod.eql?("sql_query")
57
+ t_mod = Model.new(mod, rdf_start_class, class_map[mod.to_s])
58
+ #if sql...
59
+ key = t_mod.get_key
60
+ t_mod.get_rows.each do |item|
61
+ rdf_string<<rdf_by_id(mod,rdf_start_class,eval("item.#{key}"))
62
+ end
63
+ end
64
+ end
65
+ rdf_string
66
+ end
67
+
68
+ #
69
+ def rdf_by_id model_name, class_name, id
70
+ puts model_name
71
+ ret=""
72
+ #TODO...mehrere Models
73
+
74
+ model = Model.new(model_name,class_name)
75
+ data_types= model.get_datatypes
76
+ #build hash
77
+ item=model.get_row_by_id(id).first
78
+ line = Hash.new
79
+ subline = Hash.new
80
+ remove =Array.new
81
+
82
+ model.model_attributes.each do |k,v|
83
+ if v.to_s.include?("*") || v.to_s[0..9]=="new_model("
84
+ remove.push(k.to_s) unless remove.index(k.to_s)
85
+ m_class,field=v.to_s.split("*")
86
+ ref_pred,ref_model=k.to_s.split"->"
87
+ if v.to_s[0..5]=="MODEL("
88
+ m_class.gsub!("MODEL(","Model.new(")
89
+ submodel= eval(m_class.to_s)
90
+ sub_rows= submodel.get_rows
91
+ else
92
+ submodel= eval("item.#{m_class.to_s.downcase}")
93
+ sub_rows= submodel
94
+ end
95
+ sub_rows.each do |subitem|
96
+ sub_id=eval("subitem.#{field}")
97
+ ret<< make_triples({:id=>id,k=>sub_id },class_name,"",Array.new,false)
98
+ ret<<rdf_by_id( submodel.model_name ? submodel.model_name : m_class , ref_model,sub_id).to_s
99
+ end
100
+ else
101
+ write_line = true;
102
+ #FIXME: use last "." split
103
+ if k.to_s.include? "->"
104
+ line[k.to_s]=eval("item."+v.to_s)
105
+ #write key ref
106
+ ref_pred,ref_model=k.to_s.split"->"
107
+ #write referenced model
108
+ #ret<< make_triples({:id=>id,k=>sub_id },class_name,"",Array.new,false)
109
+ m= search_models(ref_model)
110
+ m.values[0].each do |mod,val|
111
+ ret<<(rdf_by_id mod, ref_model, eval("item."+v.to_s)).to_s
112
+ end
113
+ else
114
+ if v.to_s.include? "."
115
+ write_line = nil unless eval("item."+v.to_s.downcase.split(".")[0])
116
+ end
117
+ begin
118
+ if v.to_s[0..5]=="CONST("
119
+ line[k.to_s],data_types[k.to_s]= model.get_const(v)
120
+ #datatype to uri format
121
+ if data_types[k.to_s]=="LINK"
122
+ line[k.to_s] =uri line[k.to_s]
123
+ data_types.delete k.to_s
124
+ @object_properties[k.to_s]="t"
125
+ else
126
+ data_types[k.to_s] = uri(data_types[k.to_s])
127
+ end
128
+ else
129
+ line[k.to_s]=eval("item."+v.to_s) if write_line
130
+
131
+ end
132
+ rescue Exception => ex
133
+ line[k.to_s]=v.to_s if write_line
134
+ end
135
+ end
136
+ end
137
+ # end
138
+ end
139
+ extract_id_line(model.model_attributes, line, item,data_types)
140
+ remove.each { |rem_attrib| line.delete(rem_attrib) }
141
+ #get triples of row
142
+ ret<<make_triples(line, class_name , "", data_types)
143
+ #render :text => t.make_triples(c1, controller , "", t.dbd_types)
144
+ ret
145
+ end
146
+
147
+ # rdfa generation starts here
148
+ def get_rdfa rdf_start_class , var=[]
149
+ #write namespace
150
+ "<div #{@object_namespaces.collect {|pre,ns| "xmlns:"<<pre.to_s<<"=\""<<ns.to_s<<"\" " }}>\n #{get_rdfa_root(rdf_start_class,var)} </div>"
151
+ end
152
+
153
+ # the root rdf node
154
+ def get_rdfa_root rdf_start_class , var=[]
155
+ #map each var on its classname
156
+ rdfa_string = ""
157
+ class_map = Hash.new
158
+ var.each do |c|
159
+ class_map[c[0].class.to_s]= c
160
+ end
161
+
162
+ #get configuration start ->classes
163
+ m = search_models(rdf_start_class)
164
+
165
+ m.values[0].each do |mod,attributes|
166
+ unless mod.eql?("sql_query")
167
+ t_mod = Model.new(mod, rdf_start_class, class_map[mod.to_s])
168
+ #if sql...
169
+ key = t_mod.get_key
170
+ t_mod.get_rows.each do |item|
171
+ rdfa_string<<get_rdfa_by_id(rdf_start_class,eval("item.#{key}"))
172
+ end
173
+ end
174
+ end
175
+ rdfa_string
176
+ end
177
+
178
+ # generate RDFa tags by a specified ID
179
+ def get_rdfa_by_id rdf_start_class , id
180
+ #map each var on its classname
181
+ id = id.to_s
182
+ rdfa_string = "<div about=\"#{rdf_start_class+"/"<<id}\" typeof=\"#{@class_map[rdf_start_class.to_sym]}\">\n"
183
+ #get configuration start ->classes
184
+ m = search_models(rdf_start_class)
185
+ m.values[0].each do |mod,attributes|
186
+ unless mod.to_s.eql?("sql_query")
187
+ t_mod = Model.new(mod, rdf_start_class)
188
+ #if sql...
189
+ item=t_mod.get_row_by_id(id).first
190
+ t_mod.model_attributes.each do |name,link_field|
191
+ #property
192
+ #link to other ress
193
+ if name.to_s.include? "->"
194
+ m_class,role_mod = name.to_s.split "->"
195
+
196
+ if role_mod.eql?("sql_query")
197
+ hello=""
198
+ else
199
+ if link_field.include?("*")
200
+ field_class,field= link_field.to_s.split("*")
201
+ if field_class.to_s[0..5]=="Model("
202
+ field_class.gsub!("MODEL(","Model.new(")
203
+ submodel= eval(field_class.to_s).get_rows
204
+ rdfa_string<<"<div rel=\"#{m_class}\">\n"
205
+ submodel.each do |line|
206
+ rdfa_string<<get_rdfa_by_id(role_mod , eval("line.#{field.to_s}"))
207
+ end
208
+ rdfa_string<<"</div>\n"
209
+ else
210
+ subitem = eval("item.#{field_class}")
211
+ rdfa_string<<"<div rel=\"#{m_class}\">\n"
212
+ subitem.each do |subline|
213
+ rdfa_string<< get_rdfa_by_id(role_mod , eval("subline.#{field.to_s}"))
214
+ end
215
+ rdfa_string<<"</div>\n"
216
+ end
217
+ else
218
+ rdfa_string<<"<div rel=\"#{m_class}\">\n"
219
+ rdfa_string << get_rdfa_by_id(role_mod , eval("item.#{link_field.to_s}"))
220
+ rdfa_string<<"</div>\n"
221
+ end
222
+ end
223
+ else
224
+ write=true
225
+ #just a property
226
+ #CONST(
227
+ #Model(
228
+ if link_field.to_s[0..5] =="CONST("
229
+ write=false
230
+ val,data_type= t_mod.get_const(link_field)
231
+ if data_type=="LINK"
232
+ #link
233
+ rdfa_string<<"<div rel= \"#{name}\" resource=\"#{val}\"> </div>\n"
234
+ else
235
+ #value
236
+ rdfa_string<<"<div property= \"#{name}\" content=\"#{val}\" datatype=\"#{data_type}\"> </div>\n"
237
+ end
238
+ else
239
+ m_class,field= link_field.to_s.split("*")
240
+ if name.to_s[0..5]=="MODEL("
241
+ m_class.gsub!("MODEL(","Model.new(")
242
+ submodel= eval(m_class.to_s).get_rows
243
+ submodel.each do |line|
244
+ datatype="string"
245
+ rdfa_string<<"<div property= \"#{ref_pred}\" content=\"#{eval("line.#{field.to_s.downcase}")}\" datatype=\"#{data_type}\"> </div>\n"
246
+ end
247
+ end
248
+ if field
249
+ write = false
250
+ #rdfa_string<<get_rdfa_by_id("","")
251
+ end
252
+ end
253
+ rdfa_string<<"<div property= \"#{name}\" content=\"#{eval("item.#{link_field}")}\"></div>\n" if write
254
+ end
255
+ end
256
+ end
257
+ end
258
+ rdfa_string << "</div>\n"
51
259
  end
52
260
 
261
+ #not used
262
+ def tripleize (tr_self,c =nil,id=nil)
263
+
264
+ end
265
+
266
+ # Find all the models to the given key ( the RDF-Class name)
267
+ # returns a hash with the RDF-Class name as key and the models as value
53
268
  def search_models key
54
269
  model_groups = eval("configatron.query").to_hash
55
270
  model_groups.each do |model_group_name,model_group|
56
271
  if model_group_name.to_s.downcase == key.downcase
57
- return model_group
272
+ return {model_group_name=>model_group}
58
273
  end
59
274
  end
275
+ nil
60
276
  end
61
-
62
- #
63
- def write_model model_name,model_attributes, output
64
- puts model_name
65
277
 
66
- if model_name.to_s == "OptionValue"
67
- hello = "Product"
68
- end
69
- model = Model.new(model_name)
70
- #dtypes =dbd_types model,model.model_attributes
71
- #
72
-
278
+ # write the model(s) depending on its RDF-Class name.
279
+ # This is one of the core funktions of rtriplify
280
+ def write_model model_name, class_name
281
+ puts model_name
282
+ ret=""
283
+ model = Model.new(model_name,class_name)
73
284
  data_types= model.get_datatypes
74
285
  #build hash
75
286
  model.get_rows.each do |item|
76
287
  line = Hash.new
77
288
  subline = Hash.new
78
289
  #and now add all mapping-attributes
79
-
290
+ remove =Array.new
80
291
  model.model_attributes.each do |k,v|
81
-
82
- if k.to_s.include?("->")
83
- #find all subelements
84
-
85
- submodel = eval("item."+v.downcase)
86
-
87
- if submodel.class != (Array)
88
- if submodel
89
- sub_m = Model.new(submodel.class.to_s)
90
- subline[:id] = item.id
91
- subline[k] = eval("submodel."+sub_m.get_key.to_s)
92
- #subline[k.split"->"[0]] = eval("item."+k.split"->"[1]+".id")
93
- sub_data_types = model.get_datatypes
94
- extract_id_line(model.model_attributes, subline, item, sub_data_types)
95
- output.write make_triples(subline, model.model_name , "", sub_data_types,false)
96
- end
292
+ # if k.to_s.include?("->")
293
+ if v.to_s.include?("*") || v.to_s[0..9]=="new_model("
294
+
295
+ remove.push(k.to_s) unless remove.index(k.to_s)
296
+ m_class,field=v.to_s.split("*")
297
+ ref_pred,ref_model=k.to_s.split"->"
298
+ if v.to_s[0..5]=="MODEL("
299
+ m_class.gsub!("MODEL(","Model.new(")
300
+ submodel= eval(m_class.to_s).get_rows
97
301
  else
98
- unless submodel.empty?
99
- sub_m = Model.new(submodel[0].class.to_s)
100
- submodel.each do |submodel_item|
101
- subline[:id] = item.id
102
- subline[k] = eval("submodel_item."+sub_m.get_key.to_s)
103
- #subline[k.split"->"[0]] = eval("item."+k.split"->"[1]+".id")
104
- sub_data_types = model.get_datatypes
105
- extract_id_line(model.model_attributes, subline, item,sub_data_types)
106
- output.write make_triples(subline, model.model_name , "", sub_data_types,false)
107
- end
108
- end
302
+ submodel= eval("item.#{m_class.to_s.downcase}")
303
+ end
304
+ submodel.each do |subitem|
305
+ subline[:id] = eval("item.#{model.get_key}")
306
+ subline[k] = eval("subitem.#{field}")
307
+ #extract_id_line(m.model_attributes, subline, subitem, Array.new)
308
+ ret<<make_triples(subline, class_name, false,Array.new,false )
309
+ subline.clear
109
310
  end
110
311
  else
111
312
  write_line = true;
112
313
  if v.to_s.include? "."
113
- write_line = nil unless eval("item."+v.to_s.split(".")[0])
314
+ write_line = nil unless eval("item."+v.to_s.downcase.split(".")[0])
315
+ end
316
+ begin
317
+ if v.to_s[0..5]=="CONST("
318
+ line[k.to_s],data_types[k.to_s]= model.get_const(v)
319
+ #datatype to uri format
320
+ if data_types[k.to_s]=="LINK"
321
+ line[k.to_s] =uri line[k.to_s]
322
+ data_types.delete k.to_s
323
+ @object_properties[k.to_s]="t"
324
+ else
325
+ data_types[k.to_s] = uri(data_types[k.to_s])
326
+ end
327
+ else
328
+ line[k.to_s]=eval("item."+v.to_s) if write_line
329
+ end
330
+ rescue Exception => ex
331
+ line[k.to_s]=v.to_s if write_line
114
332
  end
115
- line[k.to_s]=eval("item."+v.to_s) if write_line
116
333
  end
334
+ # end
117
335
  end
118
336
  extract_id_line(model.model_attributes, line, item,data_types)
119
-
120
- #watch if there is a mapping to another object with "->"
337
+ remove.each { |rem_attrib| line.delete(rem_attrib) }
121
338
  #get triples of row
122
- output.write make_triples(line, model.model_name , "", data_types)
339
+ ret<<make_triples(line, class_name , "", data_types)
123
340
  #render :text => t.make_triples(c1, controller , "", t.dbd_types)
124
341
  end
342
+ ret
125
343
  end
126
344
 
345
+ # search for the ID field and set it in line var- to the "id"-key
127
346
  def extract_id_line model_attributes, line,item,dtypes
128
347
  #look if id is mapped to another field
129
348
  id_keys = model_attributes.to_hash.keys
@@ -141,35 +360,37 @@ class Tripleizer
141
360
  end
142
361
  end
143
362
 
363
+ # Executes a sql command. Better to not use this function
144
364
  def write_sql model_name, model_attributes,output
145
365
  model_attributes.each do|key,query|
146
366
  sql= ActiveRecord::Base.connection();
147
367
  (sql.select_all query).each do |row|
148
- output.write make_triples(row,model_name,"")
368
+ make_triples(row,model_name,"")
149
369
  end
150
370
  end
151
371
  end
152
372
 
153
- def write_rdf_head output
373
+ # the standard triples at the beginning of your rdf output ( version and licence)
374
+ def write_rdf_head
154
375
  # $this->writeTriple($self,$this->uri('rdfs:comment'),'Generated by Triplify '.$this->version.' (http://Triplify.org)',true);
155
- output.write write_triple($base_uri, uri("rdfs:comment"), 'Generated by Triplify '+'version'+ " (http://Triplify.org) ", "literal")
376
+ write_triple(@base_uri, uri("rdfs:comment"), 'Generated by Triplify '+'version'+ " (http://Triplify.org) ", "literal" )
156
377
  unless configatron.license.blank?
157
378
  #$this->writeTriple($self,'http://creativecommons.org/ns#license',$this->config['license']);
158
- output.write write_triple($base_uri,'http://creativecommons.org/ns#license',configatron.license)
379
+ write_triple(@base_uri,'http://creativecommons.org/ns#license',configatron.license, "literal" )
159
380
  end
160
381
  end
161
382
 
162
-
163
-
383
+ # generate the triples from a given hash
164
384
  def make_triples (c1,rdf_class,maketype,dtypes=Array.new,rdf_type=true)
165
385
  rdf_ns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
166
386
  ipref= uri rdf_class.to_s+'/'
167
387
  is_literal=false
168
388
  dtype,lang,ret="","",""
169
389
  object_property = nil
390
+ triples =""
170
391
 
171
392
  unless c1[:id]
172
- id_row = c1.select {|k,v| k =~ /^(id|ID|iD|Id)$/}
393
+ id_row = c1.select {|k,v| k =~ /^(id|ID|iD|Id)$/}
173
394
  c1[:id] = id_row[0][1]
174
395
  c1.delete id_row[0][0]
175
396
 
@@ -181,11 +402,12 @@ class Tripleizer
181
402
  #write model :-)
182
403
 
183
404
  c= @class_map[rdf_class.to_sym] ?@class_map[rdf_class.to_sym]:rdf_class
184
- ret+= write_triple subject, rdf_ns+"type", uri(c, @object_namespaches[:vocabulary]) if rdf_type
185
-
405
+ triples << write_triple(subject, rdf_ns+"type", uri(c, @object_namespaces[:vocabulary])) if rdf_type
406
+ # write_triple(subject,predi cate,object ,is_literal=false,dtype="",lang="",json=nil)
186
407
  c1.delete :id unless rdf_type
187
408
 
188
409
  c1.each do |k,v|
410
+ dtype=""
189
411
  k=k.to_s
190
412
  if v.to_s.empty?
191
413
  next
@@ -214,10 +436,10 @@ class Tripleizer
214
436
  #callbackfunktion
215
437
  #
216
438
 
217
- prop= self.uri(k,@object_namespaches[:vocabulary])
439
+ prop= self.uri(k,@object_namespaces[:vocabulary])
218
440
 
219
441
  unless object_property
220
- is_literal= true
442
+ is_literal= true
221
443
  object= v.to_s
222
444
  else
223
445
  is_literal= false
@@ -225,37 +447,46 @@ class Tripleizer
225
447
  #TODO: fixme "/" in the middle
226
448
  #object= uri "#{object_property}/#{object_property && object_property[-1,1].to_s !="/" ?"/":":"}#{v}"
227
449
  object= uri "#{object_property}#{object_property[-1,1].to_s !="/" ? "/":":"}#{v}"
450
+ if object[0..1] == "t/"
451
+ object = object.to_s[2..-1]
452
+ end
228
453
  end
229
- ret +=write_triple(subject,prop,object,is_literal,dtype,lang).to_s
230
- end
231
- ret
454
+ triples<<write_triple(subject,prop,object,is_literal,dtype,lang).to_s
455
+ end
456
+ triples
232
457
  end
233
458
 
459
+ # triple will be given to its defined output (json||inner_variable||return value
234
460
  def write_triple(subject,predicate,object,is_literal=false,dtype="",lang="")
235
- #if json -
236
- #end
237
- #else
238
- #(lang?"@#{lang}":'')
239
-
240
- #define the object
241
- if(is_literal)
242
- object = "\"#{object.to_s.gsub('"','%22')}\""+ (dtype.empty? ? (lang.empty? ? "": "@#{lang}" ):"^^<#{dtype}>" )
461
+ if @output_json
462
+ oa = {:value=> object,:type=> is_literal ? 'literal' : 'uri'}
463
+ oa['datatype']=dtype if is_literal && dtype
464
+ oa['language']=lang if is_literal && lang
465
+ add_json_pair subject, predicate,oa
466
+
243
467
  else
244
- object = ( object[0..1] == "_:" ) ? object : "<#{object}>"
245
- end
246
- #object="\"#{object[1..-2].gsub('"','\"')}\""
247
- #define the subject
248
- subject = ( subject[0..1] == "_:" ) ? subject : "<#{subject}>"
249
- if @output
250
- @output.write "#{subject} <#{predicate}> #{object} .\n"
468
+ #(lang?"@#{lang}":'')
469
+
470
+ #define the object
471
+ if(is_literal)
472
+ object = "\"#{object.to_s.gsub('"','%22')}\""+ (dtype.empty? ? (lang.empty? ? "": "@#{lang}" ):"^^<#{dtype}>" )
473
+ else
474
+ object = ( object[0..1] == "_:" ) ? object : "<#{object}>"
475
+ end
476
+ #object="\"#{object[1..-2].gsub('"','\"')}\""
477
+ #define the subject
478
+ subject = ( subject[0..1] == "_:" ) ? subject : "<#{subject}>"
479
+ if @output
480
+ @output.write "#{subject} <#{predicate}> #{object} .\n"
481
+ end
482
+ return "#{subject} <#{predicate}> #{object} .\n"
251
483
  end
252
- "#{subject} <#{predicate}> #{object} .\n"
253
484
  end
254
485
 
255
486
  #generates an uri with the given name
256
487
  def uri (name,default="")
257
488
  name=name.to_s
258
-
489
+
259
490
  if name.try(:include?, "://")
260
491
  return name[0..name.length-2] if name[-1,1]=="/"
261
492
  return name[0..name.length]
@@ -264,7 +495,7 @@ class Tripleizer
264
495
 
265
496
  if name.index(":")
266
497
  #$this->ns[substr($name,0,strpos($name,':'))].$this->normalizeLocalName(substr($name,strpos($name,':')+1))
267
- t= @object_namespaches[name.split(":")[0].to_sym]
498
+ t= @object_namespaces[name.split(":")[0].to_sym]
268
499
  t ||=""
269
500
  t += "/" unless (t[t.length-1..t.length-1] == ("/" || "#") || t.blank?) || name.try(:include?, ":")
270
501
  return uri( t+ normalize_local_name(name.split(":")[1])+"/")
@@ -272,14 +503,22 @@ class Tripleizer
272
503
  ##($default?$default:$GLOBALS['baseURI']).$this->normalizeLocalName($name));
273
504
  #falls bei default hinten ein "/" ist abschneiden
274
505
  #default= default[0.. default.length-2] unless default[default.length-1..default.length-1] == "/"
275
- t= default.blank? ? $base_uri : default
506
+ t= default.blank? ? @base_uri : default
276
507
  t += "/" unless t[t.length-1..t.length-1]=="/"
277
508
  return uri( t + normalize_local_name(name))
278
509
  end
279
510
  end
280
511
  end
281
512
 
282
- ### returns an hash with columnname as key and datatype as value
513
+ # extract the base uri for triple generation from given request
514
+ # if param "set" is true, the inner base_uri variable off class will be set too
515
+ def generate_base_uri request, set=nil
516
+ b_uri= uri request.env['REQUEST_URI'].to_s[0..-request.env['PATH_INFO'].length]
517
+ @base_uri = b_uri if set
518
+ b_uri
519
+ end
520
+
521
+ # returns an hash with columnname as key and datatype as value
283
522
  def dbd_types (model,model_attributes)
284
523
  #hard coded mapping look mapping table
285
524
  mapping = Hash[ "binary"=>"base64Binary","boolean"=>"boolean","date"=>"date","datetime"=>"dateTime","decimal"=>"decimal","float"=>"float","integer"=>"integer","string"=>"string","text"=>"string","time"=>"time","timestamp"=>"dateTime",]
@@ -296,7 +535,23 @@ class Tripleizer
296
535
  end
297
536
  #some encoding stuff
298
537
  def normalize_local_name name
299
- CGI::escape(name).gsub(/%2F/,'/').gsub(/%23/,'#')
538
+ CGI::escape(name).gsub(/%2F/,'/').gsub(/%23/,'#')
539
+ end
540
+
541
+ def json
542
+ @json_hash.to_json
543
+ end
544
+
545
+ def add_json_pair subject, predicate,oa
546
+ if @json_hash.key? subject
547
+ if @json_hash[subject].key? predicate
548
+ @json_hash[subject][predicate].merge!(oa)
549
+ else
550
+ @json_hash[subject][predicate]=oa
551
+ end
552
+ else
553
+ @json_hash[subject]=Hash.new
554
+ @json_hash[subject][predicate]=oa
555
+ end
300
556
  end
301
-
302
557
  end
@@ -1,9 +1,9 @@
1
1
  require 'configatron'
2
2
 
3
3
  class TriplifyMetadata
4
- def write_metadata tripleizer,output
4
+ def write_metadata tripleizer
5
5
  p = ProvenanceWriter.new
6
- p.describe_provenance "http://triplify.org",tripleizer,output
6
+ p.describe_provenance "http://triplify.org",tripleizer
7
7
  end
8
8
  end
9
9
 
@@ -12,7 +12,7 @@ class ProvenanceWriter
12
12
 
13
13
  end
14
14
 
15
- def describe_provenance( subject, tripleizer,output)
15
+ def describe_provenance( subject, tripleizer)
16
16
  @now = Time.new
17
17
  @triplify_instance = get_new_bnode_id
18
18
  tripleizer.write_triple(subject, tripleizer.uri("rdf:type" ), tripleizer.uri("prv:DataItem" ))