-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknowledge_base.rb
183 lines (148 loc) · 3.52 KB
/
knowledge_base.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
=begin
--------------------------------------------------------------------------------
Info about the content models (knowledge base) for this data store.
--------------------------------------------------------------------------------
=end
require 'tempfile'
class KnowledgeBase
def self.create(props, instance)
case props.kb_type
when "sdb"
SdbKnowledgeBase.new(props)
when "tdb"
TdbKnowledgeBase.new(props)
when "custom"
require instance.file("custom_knowledge_base")
CustomKnowledgeBase.new(props)
else
raise SettingsError.new("Settings do not contain a valid value for kb_type: #{props.kb_type}")
end
end
end
# The template for KnowledgeBase classes
class KnowledgeBase
def confirm()
raise "KnowledgeBase.confirm() not implemented."
end
def erase()
raise "KnowledgeBase.erase() not implemented."
end
def create()
raise "KnowledgeBase.create() not implemented."
end
def size()
raise "KnowledgeBase.size() not implemented."
end
def empty?()
raise "KnowledgeBase.empty?() not implemented."
end
def running?()
raise "KnowledgeBase.running?() not implemented."
end
def startup()
raise "KnowledgeBase.startup() not implemented."
end
def shutdown()
raise "KnowledgeBase.shutdown() not implemented."
end
def to_s()
raise "KnowledgeBase.to_s() not implemented."
end
end
class TdbKnowledgeBase < KnowledgeBase
def initialize(props)
@path = props.tdb_path || "#{props.vivo_home}/tdbContent"
end
def confirm()
raise SettingsError.new("TDB directory doesn't exist at #{@path}") unless Dir.exist?(@path)
end
def erase()
system("rm -rf #{@path}")
end
def create()
system("mkdir #{@path}")
end
def size()
begin
return 0 unless /^(\d+)/ =~ `du -s #{@path}`
$1.to_i()
rescue
0
end
end
def empty?()
return size() == 0
end
def running?()
true
end
def startup()
# No need to start up
end
def shutdown()
# No need to shut down
end
def to_s()
if empty?
"tdb: empty"
else
"tdb: #{size()} bytes"
end
end
end
class SdbKnowledgeBase < KnowledgeBase
def initialize(props)
@db_name = props.db_name
raise SettingsError.new("Settings do not contain a value for db_name") unless @db_name
end
def mysql(commands)
puts "executing: #{commands}"
file = Tempfile.new('kb')
file.write(commands)
file.close
system("mysql -u root < #{file.path}")
file.unlink
end
def confirm()
databases = `echo "show databases;" | mysql -u root`
raise SettingsError.new("Database #{@db_name} doesn't exist") unless databases.match("^#{@db_name}$")
end
def erase()
mysql("drop database #{@db_name}; ")
end
def create()
mysql(
"create database #{@db_name} character set utf8 ; " +
"grant all on #{@db_name}.* to vivoUser@localhost identified by 'vivoPass' ;"
)
end
def size()
begin
command = "select TABLE_ROWS from information_schema.TABLES where TABLE_SCHEMA = '#{@db_name}' and TABLE_NAME = 'Quads';"
response = `echo "#{command}" | mysql -u root`
return 0 unless /(\d+)/ =~ response
return $1.to_i()
rescue
0
end
end
def empty?()
return size() == 0
end
def running?()
true
end
def startup()
# No need to start up
end
def shutdown()
# No need to shut down
end
def to_s()
if empty?
"database '#{@db_name}': empty"
else
"database '#{@db_name}': #{size()} triples"
end
end
end