Source

Socrates / src / socrates / meta.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-

from sqlalchemy.sql import bindparam 

"""
* 元数据定义:

** 基础词汇

 - 主语 subject
 - 谓词 predicate
 - 客体 object 为隐含定义,无需指明
 - is 是每个主语必有的谓词,无需指明
 - is 定义主语,此子句的 id 即 subject id
 - 容器可以为元素指定统一的 type 约束,也可以是无类型的
 - 容器指定元素类型的时候,元素可以只有值,没有类型
 - elementType 
 - type 定义类型 subject
 - objType 定义谓词的客体类型
 - objType 是谓词的隐式定义内容
 - name objType string ,所以 string 语义表是基础组件,要与relation一起预建

** relation

 - subject is type
 - predicate is subject
 - is is predicate
 - is objType subject
 - objType is predicate
 - objType objType subject
 - type is subject
 - string is type
 - name is predicate
 - name objType string
 - storage is predicate
 - storage objType string

** 常量定义

"""

SUBJECT = 1
PREDICATE = 2
IS = 3
OBJTYPE = 4
TYPE = 5
STRING = 6
NAME = 7
SET = 8
STORAGE = 9

def init_meta(engine, subjects, strings, sequence, clearfirst=False):
    from sqlalchemy.orm import sessionmaker
    session = sessionmaker(bind=engine)()
    #conn = session.connection()
    # conn.execute(subjects.insert(),
    #              {"id":SUBJECT, },
    #              {"id":PREDICATE, },
    #              {"id":IS, },
    #              {"id":OBJTYPE, },
    #              {"id":TYPE, },
    #              {"id":STRING, },
    #              {"id":NAME, },
    #              {"id":STORAGE, },
    #              )
    insert = lambda subject, predicate, obj : session.execute(subjects.insert().values(
            subject_id=subject, predicate_id=predicate, obj=obj))

    update = lambda _id, subject, predicate, obj : session.execute(subjects.update().values(
            subject_id=subject, predicate_id=predicate, obj=obj).where(subjects.c.id==_id))

    name = lambda subject, name: session.execute(strings.insert().values( 
            subject_id=subject, predicate_id=NAME, obj=name))

    storage = lambda subject, storage: session.execute(strings.insert().values( 
                subject_id=subject, predicate_id=STORAGE, obj=storage))

    if clearfirst:
        session.execute(strings.delete())
        session.execute(subjects.delete())

    for i in range(9):
        session.execute(subjects.insert().values(subject_id=sequence()))
    session.flush()

    update(SUBJECT, SUBJECT, IS, TYPE)
    name(SUBJECT, 'subject')
    storage(SUBJECT, 'segment_subject')

    update(PREDICATE, PREDICATE, IS, SUBJECT)
    name(PREDICATE, 'predicate')

    update(IS, IS, IS, PREDICATE)
    name(IS, 'is')
    insert(IS, OBJTYPE, SUBJECT)

    update(OBJTYPE, OBJTYPE, IS, PREDICATE)
    name(OBJTYPE, 'objType')
    insert(OBJTYPE, OBJTYPE, SUBJECT)

    update(TYPE, TYPE, IS, SUBJECT)
    name(TYPE, 'type')

    update(STRING, STRING, IS, TYPE)
    name(STRING, 'string')
    storage(STRING, 'segment_string')

    update(NAME, NAME, IS, PREDICATE)
    name(NAME, 'name')
    insert(NAME, OBJTYPE, STRING)

    update(SET, SET, IS, PREDICATE)
    name(SET, 'set')
    insert(SET, OBJTYPE, SUBJECT)

    update(STORAGE, STORAGE, IS, PREDICATE)
    name(STORAGE, 'storage')
    insert(STORAGE, OBJTYPE, STRING)

    session.commit()
    session.close()

def init_schema(engine, recreate=False):
    if recreate:
        engine.execute('drop schema triples cascade;')
        engine.execute('create schema triples;')

    metadata.create_all(bind=engine)
    init_meta(engine)

    
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.