Beispiel #1
0
 def searchOwlAnno(cls, fileName, className):
     owlpath = getOwlPath()
     filepath = owlpath + "/%s.owl" % (fileName)
     onto = get_ontology(filepath).load()
     owlClass = onto[className]
     annoList = owlClass.comment
     return annoList
Beispiel #2
0
 def creatOwlAnno(cls, fileName, className, property, value):
     owlpath = getOwlPath()
     filepath = owlpath + "/%s.owl" % (fileName)
     onto = get_ontology(filepath).load()
     print(list(onto.classes()))
     owlClass = onto[className]
     owlClass.comment.append(locstr(property, lang=value))
     onto.save(file=filepath)
Beispiel #3
0
 def searchOwlChildren(cls, fileName, className):
     owlpath = getOwlPath()
     filepath = owlpath + "/%s.owl" % (fileName)
     onto = get_ontology(filepath).load()
     owlClass = onto[className]
     # annoList = owlClass.comment
     descendants = owlClass.descendants()
     # return annoList,descendants
     return [children.name for children in descendants]
Beispiel #4
0
 def searchOwlClass(cls, fileName):
     owlpath = getOwlPath()
     filepath = owlpath + "/%s.owl" % (fileName)
     onto = get_ontology(filepath).load()
     owlClass = list(onto.classes())
     owlClassList = []
     for owl in owlClass:
         owlClassList.append(owl.name)
     return owlClassList[1:]
Beispiel #5
0
 def searchOwlAnnotations(cls, fileName, className):
     owlpath = getOwlPath()
     filepath = owlpath + "/%s.owl" % (fileName)
     onto = get_ontology(filepath).load()
     owlClass = onto[className]
     dirList = []
     for comment in owlClass.comment:
         dir = {'property': comment.lang, 'value': comment}
         dirList.append(dir)
     return dirList
Beispiel #6
0
 def searchOwlClassIri(cls, fileName, className):
     owlpath = getOwlPath()
     filepath = owlpath + "/%s.owl" % (fileName)
     onto = get_ontology(filepath).load()
     owlClasses = list(onto.classes())
     Iri = ''
     for owlClass in owlClasses:
         if owlClass.name == className:
             Iri = owlClass.iri
     return Iri
Beispiel #7
0
    def searchClassRelat(cls, fileName, className):
        owlpath = getOwlPath()
        filepath = owlpath + "/%s.owl" % (fileName)
        onto = get_ontology(filepath).load()
        owlClass = onto[className]
        ancestors = [owl.name for owl in list(owlClass.is_a)]
        descendants = [owl.name for owl in owlClass.descendants()]
        # 文件内容的读取
        with open(filepath, 'r', encoding='utf-8') as f:
            contents = f.read()
        contentLists = contents.split('\n\n')
        purposeLists = []
        zhengZe = '#\w*'
        dirList = []
        # 拿到包含className的元素
        for content in contentLists:
            if className in content and 'ObjectProperty' in content:
                purposeLists.append(content)
        for purpose in purposeLists:
            purposeList = purpose.split('\n')
            relation = re.search(zhengZe, purposeList[0]).group(0).lstrip('#')
            for content in purposeList[1:]:
                # className 为 domain
                if 'domain' in content and className in content:
                    for contentChild in purposeList[1:]:
                        # 查找子类
                        if 'range' in contentChild:
                            childClassName = re.search(
                                zhengZe, contentChild).group(0).lstrip('#')
                            # 判断该子类是否属于class
                            if childClassName in descendants:
                                dir = {
                                    'relation': relation,
                                    'domain': className,
                                    'range': childClassName
                                }
                                dirList.append(dir)
                # className 为 range
                else:
                    for contentParent in purposeList[1:]:
                        if 'domain' in contentParent:
                            parentClassName = re.search(
                                zhengZe, contentParent).group(0).lstrip('#')
                            # 判断该父节点是否属于class
                            if parentClassName in ancestors:
                                dir = {
                                    'relation': relation,
                                    'domain': parentClassName,
                                    'range': className
                                }
                                dirList.append(dir)
                    break

        return dirList
Beispiel #8
0
 def searchOwlParent(cls, fileName, className):
     owlpath = getOwlPath()
     filepath = owlpath + "/%s.owl" % (fileName)
     onto = get_ontology(filepath).load()
     owlClass = onto[className]
     ancestors = list(owlClass.is_a)
     parent_data = []
     for ancestor in ancestors:
         struct = {'name': ''}
         struct['name'] = ancestor.name
         parent_data.append(struct)
     return parent_data
Beispiel #9
0
 def searchOwlClassLayer(cls, fileName):
     classLayerList = []
     owlpath = getOwlPath()
     filepath = owlpath + "/%s.owl" % (fileName)
     onto = get_ontology(filepath).load()
     for owlclass in list(onto.classes()):
         direct = {
             'name': owlclass.name,
             'children': [owl.name for owl in owlclass.subclasses()]
         }
         classLayerList.append(direct)
     return classLayerList
Beispiel #10
0
 def creatOwl(cls, fileName, Owner, OLname, Des, state):
     owlpath = getOwlPath()
     filepath = owlpath + "/%s.owl" % (fileName)
     ontoFile = OntoFileUtils()
     ontoFile.filename = filepath
     ontoFile.createOntoFile()
     now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
     db = ConnectDB()
     conn = db.getConnect()
     insertLists = [Owner, OLname, now_time, Des, state]
     db.insertDBSql('ontolo_sets', insertLists, conn)
     print("creat owlfile success")
Beispiel #11
0
def ontofileupload():
    if request.method == 'POST':
        f = request.files['file']
        print(f.filename)
        filename = secure_filename(f.filename)
        owlpath = getOwlPath()
        print(owlpath)
        try:
            f.save(owlpath + '/' + str(filename))
            return '本体文件上传成功!'
        except:
            return '上传失败!'
Beispiel #12
0
def ontodelete():
    if request.method=='POST':
        del_obj=request.json['name']
        print(del_obj)
        del_lib=Ontolo_sets.query.filter_by(OLname=del_obj).first()
        db.session.delete(del_lib)
        db.session.commit()

        owlpath=getOwlPath()
        my_file = owlpath+'/'+request.json['name']+'.owl'
        if os.path.exists(my_file):
            os.remove(my_file)
            return jsonify('删除成功!')
        else:
            return jsonify('删除失败!')
Beispiel #13
0
 def delLayerClass(cls, fileName, className):
     owlpath = getOwlPath()
     filepath = owlpath + "/%s.owl" % (fileName)
     onto = get_ontology(filepath).load()
     delClass = onto[className]
     descentList = [owlClass.name for owlClass in delClass.descendants()]
     layerList = OntoOperUtils.searchOwlClassLayer(fileName)
     for descent in descentList:
         for direct in layerList:
             if descent == direct['name']:
                 layerList.remove(direct)
     for direct in layerList:
         if className in direct['children']:
             direct['children'].remove(className)
     return layerList
Beispiel #14
0
 def creatOwlRelat(cls, fileName, relationName, proRelatName, domainName,
                   rangeName):
     owlpath = getOwlPath()
     filepath = owlpath + "/%s.owl" % (fileName)
     onto = get_ontology(filepath).load()
     print(list(onto.classes()))
     proRelat = onto[proRelatName]
     domain = onto[domainName]
     range = onto[rangeName]
     owlRelat = __builtins__.type(relationName, (proRelat, ), {
         'domain': [domain],
         'range': [range]
     })
     onto.save(file=filepath)
     ####数据库部分
     ####
     print('creat owlrelat success')
Beispiel #15
0
 def rejectCreatMoreClass(cls, fileName, className):
     owlpath = getOwlPath()
     filepath = owlpath + "/%s.owl" % (fileName)
     onto = get_ontology(filepath).load()
     owlClass = onto[className]
     print(list(owlClass.descendants()))
     contentList = [
         owl.name for owl in list(owlClass.descendants())
         if owl.name != className
     ]
     print(len(contentList))
     if len(contentList) != 0:
         for content in contentList:
             layerList = OntoOperUtils.delLayerClass(fileName, content)
             return layerList
     else:
         return OntoOperUtils.searchOwlClassLayer(fileName)
Beispiel #16
0
 def creatOwlClass(cls, fileName, Name, proOwlName):
     owlpath = getOwlPath()
     filepath = owlpath + "/%s.owl" % (fileName)
     onto = get_ontology(filepath).load()
     print(list(onto.classes()))
     proClass = onto[proOwlName]
     owlClass = builtins.type(Name, (proClass, ), {})
     # print(Name,proOwlName)
     print(list(onto.classes()))
     onto.save(file=filepath)
     ####数据库部分
     # now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
     # db = ConnectDB()
     # conn = db.getConnect()
     # results = db.searchByDBColKey_All('ontolo_sets','OLname',fileName,conn)
     # for result in results:
     #     OLid = result[0]
     # insertLists = [Name, now_time, str(OLid)]
     # db.insertDBSql('ontolo_classes', insertLists, conn)
     ####
     print('creat owlclass success')
Beispiel #17
0
    def creatOwl(cls, fileName, Owner, Des, state):
        owlpath = getOwlPath()
        filepath = owlpath + "/%s.owl" % (fileName)
        onto = get_ontology("http://test.org/onto.owl")
        with onto:

            class 超类(Thing):
                pass

            class 超关系(ObjectProperty):
                pass

        onto.save(file=filepath)
        ####数据库部分
        now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        db = ConnectDB()
        conn = db.getConnect()
        insertLists = [fileName, Owner, now_time, Des, state]
        db.insertDBSql('ontolo_sets', insertLists, conn)
        ####
        print("creat owlfile success")
Beispiel #18
0
    def delOwlClass(cls, fileName, className):
        owlpath = getOwlPath()
        filepath = owlpath + "/%s.owl" % (fileName)
        with open(filepath, 'r', encoding='utf-8') as f:
            contents = f.read()
        contentLists = contents.split('\n\n')
        childrenLists = OntoOperUtils.searchOwlChildren(fileName, className)
        for i in range(len(childrenLists)):
            for j in range(len(contentLists)):
                if childrenLists[i] in contentLists[
                        j] and 'owl:Class' in contentLists[j]:
                    contentLists[j] = ''
        for i in range(len(contentLists)):
            if className in contentLists[
                    i] and 'owl:ObjectProperty' in contentLists[i]:
                relLists = contentLists[i].split('\n')
                if className in relLists[2]:
                    contentLists[i] = ''
                    break
                else:
                    for j in range(len(relLists)):
                        if className in relLists[j]: relLists[j] = ''
                contentLists[i] = ''
                relLists = [x for x in relLists if x != '']
                for relList in relLists[:-1]:
                    contentLists[i] += relList + '\n'
                contentLists[i] += relLists[-1] + '\n\n'
            else:
                contentLists[i] += '\n\n'
        string = ""
        for content in contentLists:
            string += content

        f = open(filepath, 'w', encoding='utf-8')
        f.write(string)
        f.close()
        print('del owlclass success')
Beispiel #19
0
 def searchOwlRelat(cls, fileName):
     owlpath = getOwlPath()
     filepath = owlpath + "/%s.owl" % (fileName)
     onto = get_ontology(filepath).load()
     owlRelatList = list(onto.properties())
     return owlRelatList