Commits

March Liu committed 0824f51

registe new predicate or new objType when write segment if need.

  • Participants
  • Parent commits 1a915a8

Comments (0)

Files changed (1)

src/socrates/core.py

         self.Session.commit()
         return segmentType
 
-    def newPredicate(self, name, objtype_name):
+    def newPredicate(self, name, objtype_name=None):
         """
 >>> from sqlalchemy import DateTime
 >>> storage = Storage("sqlite://", create=True)
             pid = self.sequence()
             self.Session.execute(subjects.insert().values(subject_id=pid, predicate_id=meta.IS, obj=meta.PREDICATE))
             self.Session.execute(strings.insert().values(subject_id=pid, predicate_id=meta.NAME, obj=name))
-        objType = self.get_type_id_by_name(objtype_name)
-        storid = self.Session.query(subjects.c.obj).\
-            filter(subjects.c.subject_id==objType).\
-            filter(subjects.c.predicate_id==meta.STORAGE).scalar()
-        self.Session.execute(subjects.insert().values(subject_id=pid, predicate_id=meta.OBJTYPE, obj=objType))
+        if objtype_name:
+            objType = self.get_type_id_by_name(objtype_name)
+            storid = self.Session.query(subjects.c.obj).\
+                filter(subjects.c.subject_id==objType).\
+                filter(subjects.c.predicate_id==meta.STORAGE).scalar()
+            self.Session.execute(subjects.insert().values(subject_id=pid, predicate_id=meta.OBJTYPE, obj=objType))
         self.Session.commit()
         return pid
 
         pname, tnames = self.parsePredicateExpression(predicateExpr)
         if len(tnames)>1:
             raise 'The expr %s extract object types more than one %s. Where I will write?'%(predicateExpr, tnames)
+        tname = tnames[0]
 
         needclose = True
-        subjects = self.registed_storage["subject"]
-        strings = self.registed_storage["string"]
+        Subject = self.registed_type["subject"]
+        String = self.registed_type["string"]
         if subject_id:
             sid = subject_id
         else:
             sid = self.sequence()
         pid = self.get_predicate_id_by_name(pname)
-        SegmentType = self.registed_type[tnames[0]]
+        if not (pid and self.Session.query(Subject).\
+                    filter(Subject.subject_id==pid).\
+                    filter(Subject.predicate_id==meta.OBJTYPE).\
+                    filter(Subject.obj==self.get_type_id_by_name(tname)).count()):
+            pid = self.newPredicate(pname, tname)
+        SegmentType = self.registed_type[tname]
         segment = SegmentType(sid, pid, obj)
         
         self.Session.add(segment)
                 yield row
         self.Session.commit()
 
-    def parsePredicateExpression(self, expr):
+    def parsePredicateExpression(self, expr, ):
         pname, tname = self.prdParser(expr)
+        
         if tname:
             tnames = (tname,)
         else: