local_db.py 2.24 KB
#! /usr/bin/python
# -*- coding: utf-8 -*-

from sqlalchemy import Column, Integer, String, Float
from sqlalchemy.ext.declarative import declarative_base, AbstractConcreteBase
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine

from db_address import DB_ADDRESS

from transform_frame import TransformationRules

Base = declarative_base()

VERYSHORT = 50
SHORT = 250
LONG = 1250

class Subtree(Base):
    __tablename__ = 'descendant'
    id = Column(Integer, primary_key=True)
    ancestor = Column(Integer, nullable=False)
    descendant = Column(Integer, nullable = False)

class AbstractTransformationTable(AbstractConcreteBase, Base):
    __phantom__ = None

def make_table(name):
    class TransformationTable(AbstractTransformationTable):
        __tablename__ = name
        __mapper_args__ = { 'polymorphic_identity': name, 'concrete':True}
        __table_args__ = {'extend_existing': True}

        id = Column(Integer, primary_key=True)
        frame1 = Column(Integer, index=True)
        signature1 = Column(String(SHORT))
        frame2 = Column(Integer)
        signature2 = Column(String(SHORT))
        similarity = Column(Float, index=True)

        def __init__(self, frame1, signature1, frame2, signature2, similarity):
            self.frame1 = frame1
            self.signature1 = signature1
            self.frame2 = frame2
            self.signature2 = signature2
            self.similarity = similarity

    return TransformationTable

class FrameIdTranslator(Base):
    __tablename__ = 'frame_id_translator'
    id = Column(Integer, primary_key=True)
    slowal_id = Column(Integer)
    unifier_id = Column(Integer)

class ArgumentIdTranslator(Base):
    __tablename__ = 'argument_id_translator'
    id = Column(Integer, primary_key=True)
    slowal_id = Column(Integer)
    unifier_id = Column(Integer)
    
def get_db_data(): # session, dictionary of TransformationTable
    rules = TransformationRules.get_rules()
    TT_dict = {str(rule): make_table('similarity_{}'.format(rule.sign())) for rule in rules}
    engine = create_engine(DB_ADDRESS)
    DBSession = sessionmaker(bind=engine)
    session = DBSession()
    return (session, TT_dict)

if __name__ == '__main__':
    # creating empty database disabled -- db_import instead
    pass