1. March Liu
  2. Socrates

Source

Socrates / src / socrates / types.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from socrates import meta

class Subject(object):
    def __init__(self, storage, segments):
        self.__storage = storage
        self.segments = segments
        self.subject_id=segments[0].subject_id
        self.prdexprpaser = Prdexprpaser()
    def write(self, prdexpr, obj):
        segment = self.__storage.writeSegment(self.subject_id, prdexpr, obj)
        self.segments.append(segment)
    def Session(self, ):
        return self.__storage.Session
    def commit(self):
        return self.Session.commit()
    def rollback(self):
        return self.Session.rollback()

    def object(self, pname):
        re = self.objects(pname)
        if len(re)==0:
            return None
        elif len(re)==1:
            return re[0]
        else:
            return re
    def __eq__(self, obj):
        return self.subject_id==obj.sbuject_id
    def predicates(self):
        return tuple(set(segment.predicate for segment in self.segments))
    def objects(self, pname):
        pid = self.__storage.get_predicate_id_by_name(pname)
        return [segment.object for segment in self.segments if segment.predicate_id==pid]

    pnames = property(lambda self:set(segment.predicate.name for segment in self.segments))

class Predicate(Subject):
    objType = property(lambda self:self.object('objType'))
    objTypes = property(lambda self:tuple(segment.object 
                                          for segment in self.segments 
                                          if semgents.predicate_id==meta.OBJTYPE))
    name = property(lambda self:self.object('name'))

class Type(Subject):
    name = property(lambda self:self.object('name'))
    storage = property(lambda self:self.object('storage'))

class Segment(object):
    def __init__(self, subject_id, predicate_id, obj):
        self.subject_id = subject_id
        self.predicate_id = predicate_id
        self.obj=obj
    def __getattr__(self, key):
        if key == 'subject':
            return self.getSubject()
        elif key == 'predicate':
            return self.getPredicate()
        elif key == 'object':
            return self.getObject()
        else:
            return self.__dict__[key]
    def __setattr__(self, key, value):
        if key == 'object':
            self.setObject(value)
        else:
            self.__dict__[key] = value