Commit 55a79947 authored by Anze Vavpetic's avatar Anze Vavpetic

improved the db to aleph converter, refer the predicate copying to aleph's runtime.

parent 1cf6840a
......@@ -137,29 +137,33 @@ class Aleph_DBContext(ILP_DBContext):
determinations.append(':- determination(%s/1, %s_%s/2).' % (self.db.target_att_val, table, att))
types.extend(self.constant_type_def(table, att))
getters.extend(self.attribute_clause(table, att))
return '\n'.join(self.db_connection() + self.user_settings() + modeslist + determinations + types + getters)
local_copies = [self.local_copy(table) for table in self.db.tables]
return '\n'.join(self.db_connection() + local_copies + self.user_settings() + modeslist + determinations + types + getters)
def concept_type_def(self, table):
return ['%s(%s).' % (table, id) for (id,) in self.rows(table, [self.db.pkeys[table]])]
# var_pk = self.db.pkeys[table].capitalize()
# variables = ','.join([var_pk if col.capitalize() == var_pk else '_' for col in self.db.cols[table]])
# return ['%s(%s) :-' % (table, var_pk),
# '\t%s(%s).' % (table, variables)]
#return ['%s(%s).' % (table, id) for (id,) in self.rows(table, [self.db.pkeys[table]])]
var_pk = self.db.pkeys[table].capitalize()
variables = ','.join([var_pk if col.capitalize() == var_pk else '_' for col in self.db.cols[table]])
return ['%s(%s) :-' % (table, var_pk),
'\t%s(%s).' % (table, variables)]
def constant_type_def(self, table, att):
return ['%s(%s).' % (att, val) for val in self.db.col_vals[att]]
# var_att = att.capitalize()
# variables = ','.join([var_att if col == att else '_' for col in self.db.cols[table]])
# return ['%s(%s) :-' % (att, var_att),
# '\t%s(%s).' % (table, variables)]
# return ['%s(%s).' % (att, val) for val in self.db.col_vals[att]]
var_att = att.capitalize()
variables = ','.join([var_att if col == att else '_' for col in self.db.cols[table]])
return ['%s(%s) :-' % (att, var_att),
'\t%s(%s).' % (table, variables)]
def connecting_clause(self, table, ref_table):
pk, fk = self.db.pkeys[ref_table], self.db.connected[(ref_table, table)][0]
return ['has_%s(%s,%s).' % (ref_table,id,ref_id) for (id, ref_id) in self.rows(ref_table, [pk, fk])]
def db_connection(self):
con = self.db.connection
host, db, user, pwd = con.host, con.database, con.user, con.password
return [':- use_module(library(myddas)).', \
':- db_open(mysql, \'%s\'/\'%s\', \'%s\', \'%s\').' % (host, db, user, pwd)] + \
[':- db_import(%s, tmp_%s).' % (table, table) for table in self.db.tables]
def attribute_clause(self, table, att):
pk = self.db.pkeys[table]
return ['%s_%s(%s, %s).' % (table, att, id, val) for (id, val) in self.rows(table, [pk, att])]
def local_copy(self, table):
cols = ','.join([col.capitalize() for col in self.db.cols[table]])
return ':- repeat, tmp_%s(%s), (%s(%s), !, fail ; assertz(%s(%s)), fail).' % (table, cols, table, cols, table, cols)
if __name__ == '__main__':
from context import DBConnection, DBContext
......
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