Exemple #1
0
    def loadTable(self, rf2file):
        print("Reading active relationships from relationship snapshot table")
        db = RF2DBConnection()
        rdb = RelationshipDB()
        fname = self._fname
        if not cp_values.ss:
            print("Error: Transitive closure not implemented for FULL tables")
            return
        if not rdb.hascontent():
            print("Error: Transitive table load requires Snapshot releationship table")
            return

        db.execute("SELECT sourceId, destinationId FROM %s WHERE %s AND typeId = %s" %
                   (rdb._fname, self.fltr, is_a))

        print("Computing transitive closure")
        tc = list(transitive_closure(db))
        print("Dropping indices")
        TransitiveClosureDB._dropindices(self._fname)
        print("Writing records to table")
        for s in range(0, len(tc), chunksize):
            TransitiveClosureDB._writeblock(tc[s:s+chunksize], db, fname)
        print("Creating indices")
        TransitiveClosureDB._createindices(fname)
        print("Computing root entries")
        db.execute_query("UPDATE %(fname)s t1 LEFT JOIN %(fname)s t2 ON t1.parent=t2.child "
                         "SET t1.isRoot=1 WHERE t1.depth=1 AND t2.child IS null" % vars())
        db.commit()
 def __contains__(self, item: RF2_Quad) -> bool:
     query = "SELECT count(t.id) FROM %s" % RelationshipDB.fname()
     query += " WHERE " + \
              "t_contains.sourceId = %s AND t_contains.destinationId = %s AND t_contains.typeId = %s and t_contains.gid = %s" % \
              (item.s, item.t, item.a, item.g)
     query += " FROM (%s) AS t_contains" % self.as_sql()
     return int(list(self._execute_query(self._countSTMT % query))[0]) > 0
Exemple #3
0
    def loadTable(self, rf2file):
        from rf2db.db.RF2RelationshipFile import RelationshipDB
        from rf2db.db.RF2StatedRelationshipFile import StatedRelationshipDB
        rdb = RelationshipDB()
        srdb = StatedRelationshipDB()
        if not rdb.hascontent() or not srdb.hascontent():
            print(("Relationship databases must be loaded before loading %s" % self._fname))
            return

        super(CanonicalCoreDB, self).loadTable(rf2file)

        print("Updating Stated Relationship File")
        srdb.updateFromCanonical(self._fname)

        print("Updating Relationship File")
        rdb.updateFromCanonical(self._fname)
    def __init__(self, query=None, rf: bool=None):
        """
        Construct a set sctidGroups from a set of quads
        :param query: quad query
        :param rf: reverse flag if source_quads is not supplied
        :return:

        Note: This function assumes that the following table has been created:
               relationship_ss_ext as:
               create table relationship_ss_ext as
                    select *, IF(relationshipGroup > 0, relationshipGroup, id) as gid from relationship_ss
               and it should have indices on source, type, dest, source+type, dest+type
        """
        super().__init__()
        assert rf is not None, "Reverse flag must be supplied"
        self._len = None                # number of elements
        self._rf = rf
        sord = 'destinationId' if rf else 'sourceId'

        if query is not None:
            self._query = "SELECT DISTINCT idg.id, idg.gid"
            self._query += " FROM (%s) AS idg" % query
        else:
            self._query = "SELECT DISTINCT idg.%s AS id, idg.gid" % sord
            self._query += " FROM %s AS idg WHERE active=1 AND locked=0" % (RelationshipDB.fname() + '_ext')
Exemple #5
0
 def setUp(self):
     setConfig()
     self.reldb = RelationshipDB()
     self.csdb = ChangeSetDB()
     self.concdb = ConceptDB()
     self.testchangeset = str(self.csdb.new(**add_changeset_parms.parse().dict).referencedComponentId.uuid)
     self.concrec = self.concdb.add(self.testchangeset, moduleid=str(cimiModule))
     self.concid = self.concrec.id
Exemple #6
0
class NewDescriptionTestCase(unittest.TestCase):
    def setUp(self):
        setConfig()
        self.reldb = RelationshipDB()
        self.csdb = ChangeSetDB()
        self.concdb = ConceptDB()
        self.testchangeset = str(self.csdb.new(**add_changeset_parms.parse().dict).referencedComponentId.uuid)
        self.concrec = self.concdb.add(self.testchangeset, moduleid=str(cimiModule))
        self.concid = self.concrec.id

    def tearDown(self):
        self.csdb.rollback(self.testchangeset)

    def testaddexisting(self):
        parms = new_rel_parms.parse(effectiveTime='20141131',
                                    moduleId=str(cimiModule),
                                    changeset=self.testchangeset,
                                    source=test_source,
                                    target=test_target)
        dbrec = self.reldb.add(**parms.dict)
        self.assertIsNone(dbrec)
        self.assertEqual("Cannot change the definition of an existing concept (74400008)",
                         self.reldb.invalid_add_reason(**parms.dict))

    def testaddnew(self):
        parms = new_rel_parms.parse(moduleId=str(cimiModule),
                                    changeset=self.testchangeset,
                                    source=self.concid,
                                    target=test_target)
        dbrec = self.reldb.add(**parms.dict)
        self.assertIsNotNone(dbrec, self.reldb.invalid_add_reason(**parms.dict))
        etn = effectivetimenow()
        relid = dbrec.id
        self.assertEqual('RF2Relationship(id:%s, effectiveTime:%s, active:1, moduleId:11000160102, '
                         'sourceId:%s, destinationId:85315007, relationshipGroup:0, typeId:116680003, '
                         'characteristicTypeId:900000000000010007, modifierId:900000000000451002, isCanonical:0)'
                         % (relid, etn, self.concid),
                         str(dbrec))
 def __init__(self, rf: bool=False, atts: Sctids=None, eq: bool=True, ecv: Sctids=None, query=None, _mt_instance=None):
     """
     Construct a set of quads from
     :param rf: reverse flag.  If true ecv applies to source.  If false, destination
     :param atts: attributes (types)
     :param eq: If true, test for equal, if false, for not equal
     :param ecv: sources / destinations for testing
     :param query:  query to use instead of constructing.  Still need rf
     :return:
     """
     Set.__init__(self, Quads)
     _Instance.__init__(self, RF2_Quad)
     RF2_Substrate_Common.__init__(self)
     self._val = self
     self._type = Quads
     if _mt_instance:
         self._query = "SELECT id AS rid, sourceId AS id, typeId, destinationId, gid FROM %s WHERE 0" % \
                       (RelationshipDB.fname() + "_ext")
         self.rf = False
         self._len = 0
     else:
         self._len = None                # number of elements
         if query:
             self._query = query
         else:
             self._query = "SELECT id AS rid, sourceId" + (" AS id," if not rf else ",")
             self._query += " typeId, destinationId" + (" AS id," if rf else ",")
             self._query += " gid FROM %s" % RelationshipDB.fname() + '_ext'
             self._query += " WHERE "
             if atts is not None:
                 self._query += (("typeId IN (%s)" % atts.as_sql()) if eq else
                                 ("typeId NOT IN (%s)" % atts.as_sql())) + " AND "
             if ecv is not None:
                 self._query += (("sourceId IN (%s)" % ecv.as_sql()) if rf else
                                 ("destinationId IN (%s)" % ecv.as_sql())) + " AND "
             self._query += "active=1 AND locked=0"
         self.rf = rf
 def _rollback(cls, db, changeset, **args):
     db1 = StatedRelationshipDB().connect()
     from rf2db.db.RF2RelationshipFile import RelationshipDB
     for subj in cls.subjs(db, changeset):
         RelationshipDB._tcdb().remove(db1, subj)
     return super(StatedRelationshipDB, cls)._rollback(db, changeset, **args)