Commits

March Liu committed 3bfc34d

debug

  • Participants
  • Parent commits b07d6c1

Comments (0)

Files changed (3)

File src/socrates/core.py

 from sqlalchemy import create_engine, MetaData, Sequence, Table, Column, String, select, join, Integer, and_
 from sqlalchemy.orm import sessionmaker, mapper
 import meta
-from socrates.types import Subject, Segment
+from socrates.types import Subject, Segment, Predicate, Type
 
 class Storage(object):
     """
 
             for r in res:
                 if r.name != 'string':
-                    self.registeType(r.name, storage=r.table,)
+                    self.registeType(r.name, storage=r.storage,)
+        session.close()
     session = property(lambda self:self.Session())
 
     def registeType(self, typename, storage=None, colType=None, create=False, ):
 
         subjects = self.registed_storage['subject']
         strings = self.registed_storage['string']
-        session = self.session
 
         for segmentType in self.registed_type.values():
             if storname == segmentType.table.name:
                 return segmentType
 
         if create:
+            session = self.Session()
             if colType == None:
                 raise 'must give the object storage type if you want to create storage'
 
             session.execute(strings.insert().values(subject_id=typeid, predicate_id=meta.NAME, obj=typename),)
             session.execute(strings.insert().values(subject_id=typeid, predicate_id=meta.STORAGE, obj=table.name),)
             session.commit()
+            session.close()
         else:
             table = Table(storname, self.metadata, 
                           Column('subject_id', Integer, default=self.sequence), 
 >>> when = storage.getSubjectById(pid)
 >>> print when.subject_id==pid
 True
+>>> print when.name
+when
+>>> print when.objType.name
+datetime
         """
         session = self.session
         Subject = self.registed_type["subject"]
         session.execute(subjects.insert().values(subject_id=pid, predicate_id=meta.OBJTYPE, obj=objType))
         session.execute(strings.insert().values(subject_id=pid, predicate_id=meta.NAME, obj=name))
         session.commit()
+        session.close()
         return pid
 
     def writeSubject(self, subject_id, segments, **pairs):
         """
         此版本不保证操作的原子性,亦未做性能调优。
+>>> storage = Storage("sqlite://", create=True)
+>>> pid = storage.newPredicate('kssoid', 'string')
+>>> sid = storage.writeSubject(500, {'kssoid':'liuxin2'})
+>>> print sid
+500
+>>> sid = storage.writeSubject(None, {'kssoid':'nobody'})
+>>> print sid
+11
         """
         if subject_id:
             sid = subject_id
 True
 >>> print segment.obj
 nobody
+>>> print segment.subject_id
+12
         """
         session=self.session
         subjects = self.registed_storage["subject"]
             sid = subject_id
         else:
             sid = self.sequence()
-        pid = session.query(strings.c.subject_id,).\
-            filter(strings.c.predicate_id==meta.NAME).\
-            filter(strings.c.obj==predicate).scalar()
+        pid = self.get_preidcate_id_by_name(predicate)
         objType = session.query(subjects.c.obj).\
             filter(subjects.c.subject_id==pid).\
             filter(subjects.c.predicate_id==meta.OBJTYPE).scalar() 
         segment = SegmentType(sid, pid, obj)
         session.add(segment)
         session.commit()
+        session.close()
         return segment 
 
     def initStorage(self, schema=None):
                       self.segment_id, self.subject_id, 
                       self.predicate_id, self.obj,
                       schema=schname)
-
         table.create()
         self.registed_storage["subject"]=table
         self.registed_type["subject"] = self.__make_type_helper("subject", table)
         for Type in self.registed_type.values():
             for segment in session.query(Type).filter(Type.subject_id==subject_id):
                 yield segment
+        session.close()
 
     def getSubjectById(self, subject_id):
-        return Subject(self, [segment for segment in self.getSegmentsBySubjectId(subject_id)])
+        """
+>>> storage = Storage("sqlite://", create=True)
+>>> pid = storage.newPredicate('kssoid', 'string')
+>>> kssoid = storage.getSubjectById(pid)
+>>> print kssoid.name
+kssoid
+>>> segment = storage.writeSegment(500, 'kssoid', 'liuxin2')
+>>> print segment.predicate.name
+kssoid
+>>> subject = storage.getSubjectById(segment.subject_id)
+>>> print subject.pnames
+set([u'kssoid'])
+        """
+        segments = [segment for segment in self.getSegmentsBySubjectId(subject_id)]
+        subject_is = [segment 
+                      for segment in segments 
+                      if segment.predicate_id==meta.IS and segment.obj in (meta.PREDICATE, meta.TYPE)]
+        if subject_is:
+            if subject_is[0].obj==meta.PREDICATE:
+                return Predicate(self, segments)
+            else:
+                return Type(self, segments)
+        return Subject(self, segments)
 
     def get_preidcate_id_by_name(self, name):
         """
         """
         SegmentString = self.registed_type['string']
         SegmentSubject = self.registed_type['subject']
-        session=self.session
-        return session.query(SegmentSubject.subject_id,).\
+        session=self.Session()
+        re = session.query(SegmentSubject.subject_id,).\
             filter(SegmentSubject.predicate_id==meta.IS).\
             filter(SegmentSubject.obj==meta.PREDICATE).\
             filter(select([SegmentString.obj], 
                           and_(SegmentString.subject_id==SegmentSubject.subject_id,
                                SegmentString.predicate_id==meta.NAME)).as_scalar()==name).scalar()
+        session.close()
+        return re
 
     def __make_type_helper(self, typename, table):
         def setSubject(_self, subject):

File src/socrates/meta.py

     insert(STORAGE, OBJTYPE, STRING)
 
     session.commit()
+    session.close()
 
 def init_schema(engine, recreate=False):
     if recreate:

File src/socrates/types.py

 
 class Subject(object):
     def __init__(self, storage, segments):
-        self.storage = storage
+        self.__storage = storage
         self.segments = segments
         self.subject_id=segments[0].subject_id
     def write(self, predicate, obj):
-        segment = self.storage.writeSegment(self.subject_id, predicate, obj)
+        segment = self.__storage.writeSegment(self.subject_id, predicate, obj)
         self.segments.append(segment)
     def predicate(self, name):
-        pid = self.storage.get_preidcate_id_by_name(name)
-        re = [segment.obj for segment in self.segments if segment.predicate_id==pid]
+        pid = self.__storage.get_preidcate_id_by_name(name)
+        re = [segment.object for segment in self.segments if segment.predicate_id==pid]
         if len(re)==0:
             return None
         elif len(re)==1:
             return re[0]
         else:
             return re
+    pnames = property(lambda self:set([segment.predicate.name for segment in self.segments]))
 
 class Predicate(Subject):
     objType = property(lambda self:self.predicate('objType'))