Commit f1d4864b authored by Anze Vavpetic's avatar Anze Vavpetic

bug fix in generating the background knowledge file

parent a7d20a96
from collections import defaultdict
from django import forms
import mysql.connector as sql
......@@ -8,37 +9,47 @@ class DBConnection:
self.host = host
self.database = database
def cursor(self):
self.con = sql.connect(user=self.user, password=self.password, host=self.host, database=self.database)
return self.con.cursor()
def close(self):
self.con.close()
def connect(self):
return sql.connect(user=self.user, password=self.password, host=self.host, database=self.database)
class DBContext:
def __init__(self, connection):
self.connection = connection
cursor = connection.cursor()
con = connection.connect()
cursor = con.cursor()
cursor.execute('SHOW tables')
self.tables = [table for (table,) in cursor]
self.cols = {}
for table in self.tables:
cursor.execute("SELECT column_name FROM information_schema.columns WHERE table_name = '%s'" % table)
self.cols[table] = [col for (col,) in cursor]
self.all_cols = dict(self.cols)
self.connected = {}
cursor.execute(
"SELECT table_name, column_name, referenced_table_name, referenced_column_name \
FROM information_schema.KEY_COLUMN_USAGE \
WHERE referenced_table_name IS NOT NULL")
self.fkeys = defaultdict(set)
for (table, col, ref_table, ref_col) in cursor:
self.connected[(table, ref_table)] = (col, ref_col)
self.connected[(ref_table, table)] = (ref_col, col)
self.fkeys[table].add(col)
self.pkeys = {}
cursor.execute(
"SELECT table_name, column_name \
FROM information_schema.KEY_COLUMN_USAGE \
WHERE constraint_name='PRIMARY' and table_schema='%s'" % connection.database)
for (table, pk) in cursor:
self.pkeys[table] = pk
self.target_table = self.tables[0]
self.connection.close()
self.target_att = None
con.close()
def update(self, postdata):
widget_id = postdata.get('widget_id')[0]
self.target_table = postdata.get('target_table%s' % widget_id)[0]
self.tables = postdata.get('tables%s' % widget_id)
self.target_att = postdata.get('target_att%s' % widget_id)[0]
self.tables = postdata.get('tables%s' % widget_id, [])
# Propagate the selected tables
for table in self.cols.keys():
if table not in self.tables:
......@@ -48,8 +59,11 @@ class DBContext:
continue
del self.connected[pair]
for table in self.tables:
self.cols[table] = postdata.get('%s_columns%s' % (table, widget_id))
self.cols[table] = postdata.get('%s_columns%s' % (table, widget_id), [])
import cPickle
cPickle.dump(self, open('context_example.pkl', 'w'))
def __repr__(self):
return str((self.target_table, self.tables, self.cols, self.connected))
return str((self.target_table, self.target_att, self.tables, self.cols, self.connected))
......@@ -66,9 +66,9 @@ class RSD_DBContext(ILP_DBContext):
'\t%s(%s).' % (ref_table, ','.join(ref_table_args))]
def attribute_clause(self, table, att):
var_table, var_att = table.capitalize(), att.capitalize()
return ['has_%s(%s, %s) :-' % (att, var_table, var_att),
'\t%s(%s).' % (table, ','.join([att.capitalize() for att in self.db.cols[table]]))]
var_table, var_att, pk = table.capitalize(), att.capitalize(), self.db.pkeys[table]
return ['%s_%s(%s, %s) :-' % (table, att, var_table, var_att),
'\t%s(%s).' % (table, ','.join([att.capitalize() if att!=pk else var_table for att in self.db.cols[table]]))]
def db_connection(self):
con = self.db.connection
......@@ -91,8 +91,18 @@ class Aleph_DBContext(ILP_DBContext):
from context import DBConnection, DBContext
context = DBContext(DBConnection('root','','localhost','test'))
context.target_att = 'shape'
context.target_table = 'trains'
context.target_att = 'direction'
rsd = RSD_DBContext(context)
print rsd.all_examples()
print rsd.background_knowledge()
\ No newline at end of file
ex, bk = rsd.all_examples(), rsd.background_knowledge()
print ex
print bk
f = open('trains_mysql.pl','w')
f.write(ex)
f.close()
f = open('trains_mysql.b','w')
f.write(bk)
f.close()
\ No newline at end of file
......@@ -3,10 +3,13 @@ MySQL interaction views.
@author: Anze Vavpetic <anze.vavpetic@ijs.si>
'''
import json
from django.shortcuts import render
from context import DBContext
def mysql_db_context(request, input_dict, output_dict, widget):
con = input_dict['connection']
initial_context = DBContext(con)
return render(request, 'interactions/db_context.html', {'widget':widget, 'context': initial_context})
initial_target_cols = initial_context.cols[initial_context.target_table]
cols_dump = json.dumps(initial_context.cols)
return render(request, 'interactions/db_context.html', {'widget':widget, 'context': initial_context, 'target_cols' : initial_target_cols, 'cols' : cols_dump})
......@@ -5,6 +5,7 @@ MySQL connectivity library.
'''
import mysql.connector as sql
from context import DBConnection, DBContext
from ilp_db_context import RSD_DBContext
def mysql_connect(input_dict):
user = str(input_dict['user'])
......@@ -21,4 +22,8 @@ def mysql_db_context_finished(postdata, input_dict, output_dict):
con = input_dict['connection']
context = DBContext(con)
context.update(postdata)
return {'context' : context}
\ No newline at end of file
return {'context' : context}
def mysql_rsd_converter(input_dict):
rsd = RSD_DBContext(input_dict['context'])
return {'examples' : rsd.all_examples(), 'bk' : rsd.background_knowledge()}
\ No newline at end of file
......@@ -4,24 +4,56 @@
<label>Target table:</label>
<select name="target_table{{widget.pk}}">
{% for table in context.tables %}
<option value="{{table}}">{{table}}</option>
<option value="{{table}}" {% if forloop.first %}selected="selected"{% endif %}>{{table}}</option>
{% endfor %}
</select>
<label>Target attribute:</label>
<select name="target_att{{widget.pk}}">
{% for col in target_cols %}
<option value="{{col}}" {% if forloop.first %}selected="selected"{% endif %}>{{col}}</option>
{% endfor %}
</select>
<label>Select tables to be used:</label>
<select multiple name="tables{{widget.pk}}">
{% for table in context.tables %}
<option value="{{table}}">{{table}}</option>
<option value="{{table}}" selected="selected">{{table}}</option>
{% endfor %}
</select multiple>
{% for table, cols in context.cols.items %}
<label>Select columns for {{table}}:</label>
<select multiple name="{{table}}_columns{{widget.pk}}">
{% for col in cols %}
<option value="{{col}}">{{col}}</option>
<option value="{{col}}" selected="selected">{{col}}</option>
{% endfor %}
</select multiple>
{% endfor %}
<label>Detected connections between tables:</label>
<table id="connections{{widget.pk}}">
<thead>
<tr><th>Table</th><th>Referenced table</th><th>Column</th><th>Referenced column</th></tr>
</thead>
<tbody>
{% for tables, cols in context.connected.items %}
<tr><td>{{tables.0}}</td><td>{{tables.1}}</td><td>{{cols.0}}</td><td>{{cols.1}}</td></tr>
{% endfor %}
</tbody>
</table>
<input type="hidden" name="widget_id" value="{{widget.pk}}"/>
</form>
<script type="text/javascript">
// Columns data.
var cols = {{cols|safe}};
$('select[name="target_table{{widget.pk}}"]').change(function () {
var selected_table = $(this).first('option:selected').val();
var available_cols = cols[selected_table];
// Change the list of options as the target attribute
var att_select = $('select[name="target_att{{widget.pk}}"]');
att_select.empty();
for (idx in available_cols) {
var col = available_cols[idx];
att_select.append($('<option></option>').attr('value', col).text(col));
}
});
</script>
</div>
</div>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment