XBRL を datamapper で処理する試作
datamapper で XBRL データを保持する試作をしてみた。
まずは instance データ中の情報の一部だけを 変換する例をつくり、
どの程度の速度が出るかをみてみよう。
10000 項目のデータ (10 インスタンスがそれぞれ 10 コンテキスト * 100 科目を持つ) を作り、ちょっとした検索を行う。
以下が、ソースコードと実行結果例だが、速度的にはちょっと遅いかもしれない。
検索は、致命的に遅い訳ではないので、今後 実装を進めていこうと思う。
$ cat samler.rb
require 'rubygems'
require 'dm-core'
require 'dm-more'
require 'dm-types'
require 'pp'
require 'benchmark'
NUM_INSTANCE = 10
NUM_CONTEXT = 10
NUM_FACT_PER_CONTEXT = 100
class Xbrlinstance
include DataMapper::Resource
property :id, Integer, :serial => true
property :fpath, String
has n, :units
has n, :contexts
has n, :facts
has n, :footnotes
end
class Unit
include DataMapper::Resource
property :id, Integer, :serial => true
belongs_to :xbrlinstance
has n, :facts
property :name, String
property :measure, String
property :numerator, String
property :denominator, String
end
class Context
include DataMapper::Resource
property :id, Integer, :serial => true
belongs_to :xbrlinstance
has n, :facts
property :name, String
property :identifier, String
property :period_startDate, Date, :default => nil
property :period_endDate, Date, :default => nil
property :period_instant, Date, :default => nil
property :period_forever, Boolean, :dafault => false
property :senario, Text # not yet impl
property :segment, Text # not yet impl
end
class Footnote
include DataMapper::Resource
property :id, Integer, :serial => true
belongs_to :xbrlinstance
property :val, Text # not yet impl
end
class Fact
include DataMapper::Resource
property :id, Integer, :serial => true
belongs_to :xbrlinstance
property :element, String
property :val, Text
belongs_to :context # @contxtRef
belongs_to :unit # @unitRef
def inspect
super.to_s +
" contextRef='#{Context.all('id' => self.context_id)[0].name.to_s}'" +
" unitRef='#{Unit.all('id' => self.unit_id)[0].name.to_s}'" +
" val='#{val}'\n"
end
end
def generate_data
# instance を登録
1.upto(NUM_INSTANCE) do |i|
inst = Xbrlinstance.find_or_create(:fpath => "A-#{i}.xml")
u1 = inst.units.build(:name => "unit-01", :measure => "measure")
# fact を登録
1.upto(NUM_CONTEXT) do |j|
c = inst.contexts.build(:name => "context-#{j}", :period_instant => Date.new)
1.upto(NUM_FACT_PER_CONTEXT) do |k|
f = inst.facts.build(:element => "foo:bar-#{k}", :val => "#{i*10000 + j*1000 + k}")
u1.facts << f
c.facts << f
end
end
inst.save
end
end
def usage
puts "usage: ruby #{__FILE__} [mem | file [init]]"
exit(0)
end
usage if ARGV.size == 0
puts Benchmark.measure {
while ARGV.size > 0
opt = ARGV.shift
if (opt == "mem")
puts "--- using memory"
DataMapper.setup(:default, 'sqlite3::memory:''')
puts "--- init data"
DataMapper.auto_migrate!
generate_data()
elsif (opt == "file")
puts "--- using file"
DataMapper.setup(:default, "sqlite3://#{ File.dirname(File.expand_path(__FILE__))}/db.sqlite3")
opt2 = ARGV.shift
if (opt2 == nil)
puts "--- open exist data"
DataMapper.auto_upgrade!
elsif (opt2 == "init")
puts "--- init data"
DataMapper.auto_migrate!
generate_data()
else
usage
end
else
usage
end
end
# pp Xbrlinstance.first('id' => '2')
# pp Xbrlinstance.all
pp Unit.all('name' => 'unit-01').size
pp Unit.all('xbrlinstance.fpath' => 'A-5.xml', 'name' => 'unit-02').size
pp Context.all.size
pp Context.all('xbrlinstance.fpath' => 'A-5.xml').size
pp Fact.all.size
pp Fact.all('unit.name' => 'unit-01').size
pp Fact.all('xbrlinstance.fpath' => 'A-5.xml').size
pp Fact.all('xbrlinstance.fpath' => 'A-5.xml', 'context.name' => 'context-9').size
pp Fact.all('xbrlinstance.fpath' => 'A-5.xml', 'element' => 'foo:bar-5').size
pp Fact.all('xbrlinstance.fpath' => 'A-5.xml', 'element' => 'foo:bar-5', 'context.name' => 'context-2').size
}
# ------- 実行結果例 -----
# ruby sample.rb file init
# ...
# 31.190000 13.210000 44.400000 ( 73.532679)
# -----------------------------------------------
# $ ruby sample.rb
# "file"
# --- using file
# --- open exist data
# 10
# 0
# 100
# 10
# 10000
# 10000
# 1000
# 100
# 10
# 1
# 1.280000 0.020000 1.300000 ( 1.311618)
# -----------------------------------------------
# ruby sample.rb mem
# ...
# 24.110000 0.180000 24.290000 ( 24.516180)
# -----------------------------------------------
« ピックアップ:一流の脳を作るための育児法, 日本の「人波プール」に外国人あ然, etc... | トップページ | ピックアップ:木星に何らかの物体が衝突, 第34回漢字小委員会が開催される, etc... »
この記事へのコメントは終了しました。
« ピックアップ:一流の脳を作るための育児法, 日本の「人波プール」に外国人あ然, etc... | トップページ | ピックアップ:木星に何らかの物体が衝突, 第34回漢字小委員会が開催される, etc... »
コメント