From 1c7ae8333669b59e37a5717e606b784feb1c490a Mon Sep 17 00:00:00 2001 From: rooosyf Date: Wed, 23 Nov 2022 17:33:48 +0100 Subject: [PATCH 1/9] plugins: ontology-importer: fix on drawChain and improvement on drawAxioms --- .../ontology-importer/ontology_importer.py | 117 +++++++++--------- 1 file changed, 59 insertions(+), 58 deletions(-) diff --git a/eddy/plugins/ontology-importer/ontology_importer.py b/eddy/plugins/ontology-importer/ontology_importer.py index fd3a95cb..2cf6e1d0 100644 --- a/eddy/plugins/ontology-importer/ontology_importer.py +++ b/eddy/plugins/ontology-importer/ontology_importer.py @@ -2603,63 +2603,9 @@ def getInvolvedDiagrams(self, axioms): msgbox.exec_() def drawAxioms(self, dict): - - for str_ax in dict.keys(): - QtCore.QCoreApplication.processEvents() - - axiom = dict[str_ax]['axiom'] - diagrams = dict[str_ax]['involvedDiagrams'] - #print(axiom, diagrams) - if len(diagrams) == 0: - # if no diagram involved: - # draw on the activeOne - diag = self.session.mdi.activeDiagram() - if diag: - res = self.draw(axiom, diag) - n = res[0] - else: - project_diagrams = list(self.project.diagrams()) - diag = project_diagrams[0] - res = self.draw(axiom, diag) - n = res[0] - - if len(diagrams) == 1: - # if only one diagram involved: - # draw on the one - diag = diagrams[0] - project_diagrams = self.project.diagrams() - for d in project_diagrams: - if d.name == diag: - diagram = d - break - res = self.draw(axiom, diagram) - n = res[0] - - if len(diagrams) > 1: - # if more than one diagram involved: - # draw on the activeOne if involved - diag = self.session.mdi.activeDiagram() - if diag: - if diag.name in diagrams: - - res = self.draw(axiom, diag) - n = res[0] - # else draw on any of the involved ones - else: - diag = diagrams[0] - project_diagrams = self.project.diagrams() - for d in project_diagrams: - if d.name == diag: - diagram = d - break - res = self.draw(axiom, diagram) - n = res[0] - - # snap to grid # - self.session.doSnapTopGrid() - - # focus on the last drawn element # - self.session.doFocusItem(n) + ''' + Draw each axiom in the related diagram and insert in db. + ''' conn = None try: conn = sqlite3.connect(self.db_filename) @@ -2682,6 +2628,55 @@ def drawAxioms(self, dict): with conn: for ax in dict.keys(): # for each checked axiom: + QtCore.QCoreApplication.processEvents() + + axiom = dict[ax]['axiom'] + diagrams = dict[ax]['involvedDiagrams'] + # print(axiom, diagrams) + if len(diagrams) == 0: + # if no diagram involved: + # draw on the activeOne + diag = self.session.mdi.activeDiagram() + if diag: + res = self.draw(axiom, diag) + n = res[0] + else: + project_diagrams = list(self.project.diagrams()) + diag = project_diagrams[0] + res = self.draw(axiom, diag) + n = res[0] + + if len(diagrams) == 1: + # if only one diagram involved: + # draw on the one + diag = diagrams[0] + project_diagrams = self.project.diagrams() + for d in project_diagrams: + if d.name == diag: + diagram = d + break + res = self.draw(axiom, diagram) + n = res[0] + + if len(diagrams) > 1: + # if more than one diagram involved: + # draw on the activeOne if involved + diag = self.session.mdi.activeDiagram() + if diag: + if diag.name in diagrams: + res = self.draw(axiom, diag) + n = res[0] + # else draw on any of the involved ones + else: + diag = diagrams[0] + project_diagrams = self.project.diagrams() + for d in project_diagrams: + if d.name == diag: + diagram = d + break + res = self.draw(axiom, diagram) + n = res[0] + for ontology in project_ontologies: # for each imported ontology: # CHECK if AXIOM belongs to ONTOLOGY # @@ -2705,6 +2700,12 @@ def drawAxioms(self, dict): conn.commit() conn.close() + # snap to grid # + self.session.doSnapTopGrid() + + # focus on the last drawn element # + self.session.doFocusItem(n) + def draw(self, axiom, diagram, x=0, y=0): QtCore.QCoreApplication.processEvents() @@ -3258,7 +3259,7 @@ def drawAxiom(self, axiom, diagram, x, y): chain = axiom.getPropertyChain() property = axiom.getSuperProperty() - n = self.drawChain(chain, property, diagram, x, y)[0] + n = self.drawChain(chain, property, diagram, x, y) return n if ax_type == 'SubClassOf': From 6c961c61883e4a06969f9c40899a51c3ca97d0fe Mon Sep 17 00:00:00 2001 From: rooosyf Date: Thu, 24 Nov 2022 16:04:43 +0100 Subject: [PATCH 2/9] plugins:ontology-importer: fix on filePath missing --- eddy/plugins/ontology-importer/ontology_importer.py | 1 + 1 file changed, 1 insertion(+) diff --git a/eddy/plugins/ontology-importer/ontology_importer.py b/eddy/plugins/ontology-importer/ontology_importer.py index 2cf6e1d0..2de26a63 100644 --- a/eddy/plugins/ontology-importer/ontology_importer.py +++ b/eddy/plugins/ontology-importer/ontology_importer.py @@ -169,6 +169,7 @@ def __init__(self, spec, session): self.afwset = set() self.vm = None self.space = DiagramPropertiesForm.MinSpace + self.filePath = None def printConceptNode(self, iri, x, y, diagram): From ed8d0e730739ac20c4e8a4e6400fac23b4f3e239 Mon Sep 17 00:00:00 2001 From: rooosyf Date: Thu, 24 Nov 2022 20:04:11 +0100 Subject: [PATCH 3/9] plugins:ontology-importer: possibility to draw only declared entities --- .../ontology-importer/ontology_importer.py | 32 ++++++++++++++++--- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/eddy/plugins/ontology-importer/ontology_importer.py b/eddy/plugins/ontology-importer/ontology_importer.py index 2de26a63..66adfe26 100644 --- a/eddy/plugins/ontology-importer/ontology_importer.py +++ b/eddy/plugins/ontology-importer/ontology_importer.py @@ -1471,9 +1471,9 @@ def open(self): axioms[ontology].append(row[0]) # NOT DRAWN # - cursor.execute('''SELECT axiom, type_of_axiom, func_axiom + cursor.execute('''SELECT axiom, type_of_axiom, func_axiom, iri_dict FROM axiom - WHERE ontology_iri = ? and ontology_version = ? and type_of_axiom != 'Declaration' + WHERE ontology_iri = ? and ontology_version = ? and type_of_axiom != 'FunctionalObjectProperty' and type_of_axiom != 'TransitiveObjectProperty' and type_of_axiom != 'SymmetricObjectProperty' @@ -1485,9 +1485,25 @@ def open(self): and (?, ?, axiom) not in (SELECT project_iri, project_version, axiom FROM drawn)''', (ontology_iri, ontology_version, self.project_iri, self.project_version)) rows = cursor.fetchall() + all_dicts = [] + for r in rows: + iri_dict = r[3] + d = ast.literal_eval(iri_dict) + iris = [d[k] for k in d.keys()] + all_dicts.append(iris) not_drawn[ontology] = [] - for row in rows: - not_drawn[ontology].append([row[0], row[1], row[2]]) + for r in rows: + if r[1] == 'Declaration': + iri_dict = r[3] + d = ast.literal_eval(iri_dict) + if d: + entityIRI = d[list(d.keys())[0]] + countList = [entityIRI in el for el in all_dicts] + count = countList.count(True) + if count == 1: + not_drawn[ontology].append([r[0], r[1], r[2]]) + else: + not_drawn[ontology].append([r[0], r[1], r[2]]) # DRAWN # drawn[ontology] = [a for a in axioms[ontology] if a not in not_drawn[ontology]] @@ -2020,7 +2036,10 @@ def string2axiom2(self, ax): for axiom in axioms: QtCore.QCoreApplication.processEvents() # get axiom we needed to parse by filtering on type # - if str(axiom.getAxiomType()) == axiom_type: + if axiom_type == 'Declaration': + if str(axiom) == ax: + return axiom + elif str(axiom.getAxiomType()) == axiom_type: functional_axiom = axiom return functional_axiom return None @@ -3014,6 +3033,9 @@ def drawAxiom(self, axiom, diagram, x, y): ax_type = str(axiom.getAxiomType()) #print(ax_type) + if ax_type == 'Declaration': + entity = axiom.getEntity() + return [self.createNode(entity, diagram, x, y)] if ax_type == 'HasKey': From 3b9564809551f706bca7c6525fb72377ba1c06ea Mon Sep 17 00:00:00 2001 From: rooosyf Date: Mon, 5 Dec 2022 19:14:32 +0100 Subject: [PATCH 4/9] plugins: ontology-importer: handling punning --- .../ontology-importer/ontology_importer.py | 172 +++++++++--------- 1 file changed, 89 insertions(+), 83 deletions(-) diff --git a/eddy/plugins/ontology-importer/ontology_importer.py b/eddy/plugins/ontology-importer/ontology_importer.py index 66adfe26..73184a92 100644 --- a/eddy/plugins/ontology-importer/ontology_importer.py +++ b/eddy/plugins/ontology-importer/ontology_importer.py @@ -2765,7 +2765,7 @@ def drawPropertyExpression(self, axiom, diagram, x, y): propIRI = prop.getIRI() - propNode = self.findNode(propIRI, diagram) if self.findNode(propIRI, diagram) != 'null' else self.createNode(prop, diagram, x, y) + propNode = self.findNode(prop, diagram) if self.findNode(prop, diagram) != 'null' else self.createNode(prop, diagram, x, y) else: res = self.draw(prop, diagram, x, y) @@ -3298,7 +3298,7 @@ def drawAxiom(self, axiom, diagram, x, y): if self.isAtomic(sub): subIRI = sub.getIRI() - subNode = self.findNode(subIRI, diagram) + subNode = self.findNode(sub, diagram) if subNode != 'null': subDrawn = True @@ -3306,7 +3306,7 @@ def drawAxiom(self, axiom, diagram, x, y): if self.isAtomic(sup): supIRI = sup.getIRI() - supNode = self.findNode(supIRI, diagram) + supNode = self.findNode(sup, diagram) if supNode != 'null': supDrawn = True @@ -3543,7 +3543,7 @@ def drawObjHasSelf(self, property, diagram, x, y): if self.isAtomic(property): propIri = property.getIRI() - propNode = self.findNode(propIri, diagram) if self.findNode(propIri, diagram) != 'null' else self.createNode(property, diagram, x, y) + propNode = self.findNode(property, diagram) if self.findNode(property, diagram) != 'null' else self.createNode(property, diagram, x, y) else: @@ -3586,7 +3586,7 @@ def drawDataMinCardinality(self, card, property, ce, diagram, x, y): if self.isAtomic(property): propIri = property.getIRI() - propNode = self.findNode(propIri, diagram) + propNode = self.findNode(property, diagram) if propNode != 'null': propDrawn = True @@ -3599,7 +3599,7 @@ def drawDataMinCardinality(self, card, property, ce, diagram, x, y): isLiteral = True if not ce.isTopEntity() and not isLiteral: - ceNode = self.findNode(ceIri, diagram) + ceNode = self.findNode(ce, diagram) if ceNode != 'null': ceDrawn = True @@ -3756,7 +3756,7 @@ def drawObjMinCardinality(self, card, property, ce, diagram, x, y): if self.isAtomic(property): propIri = property.getIRI() - propNode = self.findNode(propIri, diagram) + propNode = self.findNode(property, diagram) if propNode != 'null': propDrawn = True @@ -3769,7 +3769,7 @@ def drawObjMinCardinality(self, card, property, ce, diagram, x, y): isLiteral = True if not ce.isTopEntity() and not isLiteral: - ceNode = self.findNode(ceIri, diagram) + ceNode = self.findNode(ce, diagram) if ceNode != 'null': ceDrawn = True @@ -3925,7 +3925,7 @@ def drawDataMaxCardinality(self, card, property, ce, diagram, x, y): if self.isAtomic(property): propIri = property.getIRI() - propNode = self.findNode(propIri, diagram) + propNode = self.findNode(property, diagram) if propNode != 'null': propDrawn = True @@ -3938,7 +3938,7 @@ def drawDataMaxCardinality(self, card, property, ce, diagram, x, y): isLiteral = True if not ce.isTopEntity() and not isLiteral: - ceNode = self.findNode(ceIri, diagram) + ceNode = self.findNode(ce, diagram) if ceNode != 'null': ceDrawn = True @@ -4095,7 +4095,7 @@ def drawObjMaxCardinality(self, card, property, ce, diagram, x, y): if self.isAtomic(property): propIri = property.getIRI() - propNode = self.findNode(propIri, diagram) + propNode = self.findNode(property, diagram) if propNode != 'null': propDrawn = True @@ -4108,7 +4108,7 @@ def drawObjMaxCardinality(self, card, property, ce, diagram, x, y): isLiteral = True if not ce.isTopEntity() and not isLiteral: - ceNode = self.findNode(ceIri, diagram) + ceNode = self.findNode(ce, diagram) if ceNode != 'null': ceDrawn = True @@ -4264,7 +4264,7 @@ def drawDataExactCardinality(self, card, property, ce, diagram, x, y): if self.isAtomic(property): propIri = property.getIRI() - propNode = self.findNode(propIri, diagram) + propNode = self.findNode(property, diagram) if propNode != 'null': propDrawn = True @@ -4277,7 +4277,7 @@ def drawDataExactCardinality(self, card, property, ce, diagram, x, y): isLiteral = True if not ce.isTopEntity() and not isLiteral: - ceNode = self.findNode(ceIri, diagram) + ceNode = self.findNode(ce, diagram) if ceNode != 'null': ceDrawn = True @@ -4434,7 +4434,7 @@ def drawObjExactCardinality(self, card, property, ce, diagram, x, y): if self.isAtomic(property): propIri = property.getIRI() - propNode = self.findNode(propIri, diagram) + propNode = self.findNode(property, diagram) if propNode != 'null': propDrawn = True @@ -4447,7 +4447,7 @@ def drawObjExactCardinality(self, card, property, ce, diagram, x, y): isLiteral = True if not ce.isTopEntity() and not isLiteral: - ceNode = self.findNode(ceIri, diagram) + ceNode = self.findNode(ce, diagram) if ceNode != 'null': ceDrawn = True @@ -4600,7 +4600,7 @@ def drawObjComplementOf(self, operand, diagram, x, y): if self.isAtomic(operand): iri = operand.getIRI() - node = self.findNode(iri, diagram) if self.findNode(iri, diagram) !='null' else self.createNode(operand, diagram, x, y) + node = self.findNode(operand, diagram) if self.findNode(operand, diagram) !='null' else self.createNode(operand, diagram, x, y) else: @@ -4656,7 +4656,7 @@ def drawObjSomeValuesFrom(self, property, ce, diagram, x, y): if self.isAtomic(property): propIri = property.getIRI() - propNode = self.findNode(propIri, diagram) + propNode = self.findNode(property, diagram) if propNode != 'null': propDrawn = True @@ -4669,7 +4669,7 @@ def drawObjSomeValuesFrom(self, property, ce, diagram, x, y): isLiteral = True if not ce.isTopEntity() and not isLiteral: - ceNode = self.findNode(ceIri, diagram) + ceNode = self.findNode(ce, diagram) if ceNode != 'null': ceDrawn = True @@ -4822,7 +4822,7 @@ def drawDataSomeValuesFrom(self, property, ce, diagram, x, y): if self.isAtomic(property): propIri = property.getIRI() - propNode = self.findNode(propIri, diagram) + propNode = self.findNode(property, diagram) if propNode != 'null': propDrawn = True @@ -4835,7 +4835,7 @@ def drawDataSomeValuesFrom(self, property, ce, diagram, x, y): isLiteral = True if not ce.isTopEntity() and not isLiteral: - ceNode = self.findNode(ceIri, diagram) + ceNode = self.findNode(ce, diagram) if ceNode != 'null': ceDrawn = True @@ -4989,7 +4989,7 @@ def drawObjAllValuesFrom(self, property, ce, diagram, x, y): if self.isAtomic(property): propIri = property.getIRI() - propNode = self.findNode(propIri, diagram) + propNode = self.findNode(property, diagram) if propNode != 'null': propDrawn = True @@ -5002,7 +5002,7 @@ def drawObjAllValuesFrom(self, property, ce, diagram, x, y): isLiteral = True if not ce.isTopEntity() and not isLiteral: - ceNode = self.findNode(ceIri, diagram) + ceNode = self.findNode(ce, diagram) if ceNode != 'null': ceDrawn = True @@ -5157,7 +5157,7 @@ def drawDataAllValuesFrom(self, property, ce, diagram, x, y): if self.isAtomic(property): propIri = property.getIRI() - propNode = self.findNode(propIri, diagram) + propNode = self.findNode(property, diagram) if propNode != 'null': propDrawn = True @@ -5170,7 +5170,7 @@ def drawDataAllValuesFrom(self, property, ce, diagram, x, y): isLiteral = True if not ce.isTopEntity() and not isLiteral: - ceNode = self.findNode(ceIri, diagram) + ceNode = self.findNode(ce, diagram) if ceNode != 'null': ceDrawn = True @@ -5330,7 +5330,7 @@ def drawObjUnionOf(self, operands, diagram, x, y): if self.isAtomic(e) and not isinstance(e, self.OWLLiteral): iri = e.getIRI() - node = self.findNode(iri, diagram) + node = self.findNode(e, diagram) if node != 'null': nodes.append(node) @@ -5352,7 +5352,7 @@ def drawObjUnionOf(self, operands, diagram, x, y): if self.isAtomic(op): nIri = op.getIRI() - if self.findNode(nIri, diagram) == 'null': + if self.findNode(op, diagram) == 'null': x = starting_x + 150 y = starting_y @@ -5523,7 +5523,7 @@ def drawObjOneOf(self, operands, diagram, x, y): if self.isAtomic(e) and not isinstance(e, self.OWLLiteral): iri = e.getIRI() - node = self.findNode(iri, diagram) + node = self.findNode(e, diagram) if node != 'null': nodes.append(node) @@ -5548,7 +5548,7 @@ def drawObjOneOf(self, operands, diagram, x, y): if self.isAtomic(op): nIri = op.getIRI() - if self.findNode(nIri, diagram) == 'null': + if self.findNode(op, diagram) == 'null': x = starting_x y = starting_y @@ -5622,7 +5622,7 @@ def drawDataOneOf(self, operands, diagram, x, y): if self.isAtomic(e) and not isinstance(e, self.OWLLiteral): iri = e.getIRI() - node = self.findNode(iri, diagram) + node = self.findNode(e, diagram) if node != 'null': nodes.append(node) @@ -5661,7 +5661,7 @@ def drawDataOneOf(self, operands, diagram, x, y): elif self.isAtomic(op): nIri = op.getIRI() - if self.findNode(nIri, diagram) == 'null': + if self.findNode(op, diagram) == 'null': x = starting_x y = starting_y @@ -5721,7 +5721,7 @@ def drawObjIntersectionOf(self, operands, diagram, x, y): if self.isAtomic(e) and not isinstance(e, self.OWLLiteral): iri = e.getIRI() - node = self.findNode(iri, diagram) + node = self.findNode(e, diagram) if node != 'null': nodes.append(node) xPos.append(node.pos().x()) @@ -5740,7 +5740,7 @@ def drawObjIntersectionOf(self, operands, diagram, x, y): if self.isAtomic(op): nIri = op.getIRI() - if self.findNode(nIri, diagram) == 'null': + if self.findNode(op, diagram) == 'null': x = starting_x + 150 y = starting_y @@ -5829,7 +5829,7 @@ def drawHasKey(self, ce, keys, diagram, x, y): if self.isAtomic(e): iri = e.getIRI() - node = self.findNode(iri, diagram) + node = self.findNode(e, diagram) if node != 'null': nodes.append(node) x_positions.append(node.pos().x()) @@ -5843,7 +5843,7 @@ def drawHasKey(self, ce, keys, diagram, x, y): if self.isAtomic(ce): cIRI = ce.getIRI() - cNode = self.findNode(cIRI, diagram) + cNode = self.findNode(ce, diagram) if cNode != 'null': starting_x = cNode.pos().x() - (125 * len(keys)/2) @@ -5878,7 +5878,7 @@ def drawHasKey(self, ce, keys, diagram, x, y): if self.isAtomic(c): iri = c.getIRI() - node = self.findNode(iri, diagram) + node = self.findNode(c, diagram) if node == 'null': @@ -5931,7 +5931,7 @@ def drawHasKey(self, ce, keys, diagram, x, y): if self.isAtomic(ce): cIri = ce.getIRI() - cNode = self.findNode(cIri, diagram) if self.findNode(cIri, diagram) != 'null' else self.createNode(ce, diagram, x_med, y_med-75) + cNode = self.findNode(ce, diagram) if self.findNode(ce, diagram) != 'null' else self.createNode(ce, diagram, x_med, y_med-75) if self.isIsolated(cNode): cNode.setPos(x_med, y_med-75) @@ -5957,7 +5957,7 @@ def drawChain(self, chain, property, diagram, x, y): if self.isAtomic(e): iri = e.getIRI() - node = self.findNode(iri, diagram) + node = self.findNode(e, diagram) if node != 'null': nodes.append(node) x_positions.append(node.pos().x()) @@ -5970,7 +5970,7 @@ def drawChain(self, chain, property, diagram, x, y): if self.isAtomic(property): cIRI = property.getIRI() - cNode = self.findNode(cIRI, diagram) + cNode = self.findNode(property, diagram) if cNode != 'null': @@ -6008,7 +6008,7 @@ def drawChain(self, chain, property, diagram, x, y): else: iri = prop.getIRI() - node = self.findNode(iri, diagram) + node = self.findNode(prop, diagram) if node == 'null': @@ -6053,7 +6053,7 @@ def drawChain(self, chain, property, diagram, x, y): if self.isAtomic(property): pIri = property.getIRI() - pNode = self.findNode(pIri, diagram) if self.findNode(pIri, + pNode = self.findNode(property, diagram) if self.findNode(property, diagram) != 'null' else self.createNode( property, diagram, x_med, y_med - 75) @@ -6074,7 +6074,7 @@ def drawObjPropertyRange(self, property, rang, diagram, x, y): if self.isAtomic(property): iri = property.getIRI() - propNode = self.findNode(iri, diagram) + propNode = self.findNode(property, diagram) if propNode != 'null': propDrawn = True @@ -6082,7 +6082,7 @@ def drawObjPropertyRange(self, property, rang, diagram, x, y): if self.isAtomic(rang) and not isinstance(rang, self.OWLLiteral): iri = rang.getIRI() - domainNode = self.findNode(iri, diagram) + domainNode = self.findNode(rang, diagram) if domainNode != 'null': domDrawn = True @@ -6242,7 +6242,7 @@ def drawDataPropertyRange(self, property, range, diagram, x, y): if self.isAtomic(property): iri = property.getIRI() - propNode = self.findNode(iri, diagram) + propNode = self.findNode(property, diagram) if propNode != 'null': propDrawn = True @@ -6250,7 +6250,7 @@ def drawDataPropertyRange(self, property, range, diagram, x, y): if self.isAtomic(range) and not isinstance(range, self.OWLLiteral): iri = range.getIRI() - domainNode = self.findNode(iri, diagram) + domainNode = self.findNode(range, diagram) if domainNode != 'null': domDrawn = True @@ -6413,7 +6413,7 @@ def drawObjPropertyDomain(self, property, domain, diagram, x, y): if self.isAtomic(property): iri = property.getIRI() - propNode = self.findNode(iri, diagram) + propNode = self.findNode(property, diagram) if propNode != 'null': propDrawn = True @@ -6421,7 +6421,7 @@ def drawObjPropertyDomain(self, property, domain, diagram, x, y): if self.isAtomic(domain) and not isinstance(domain, self.OWLLiteral): iri = domain.getIRI() - domainNode = self.findNode(iri, diagram) + domainNode = self.findNode(domain, diagram) if domainNode != 'null': @@ -6583,7 +6583,7 @@ def drawDataPropertyDomain(self, property, domain, diagram, x, y): if self.isAtomic(property): iri = property.getIRI() - propNode = self.findNode(iri, diagram) + propNode = self.findNode(property, diagram) if propNode != 'null': propDrawn = True @@ -6591,7 +6591,7 @@ def drawDataPropertyDomain(self, property, domain, diagram, x, y): if self.isAtomic(domain) and not isinstance(domain, self.OWLLiteral): iri = domain.getIRI() - domainNode = self.findNode(iri, diagram) + domainNode = self.findNode(domain, diagram) if domainNode != 'null': @@ -6760,7 +6760,7 @@ def drawInverseObjProperties(self, first, second, diagram, x, y): if self.isAtomic(first): firstIRI = first.getIRI() - firstNode = self.findNode(firstIRI, diagram) + firstNode = self.findNode(first, diagram) if firstNode != 'null': firstDrawn = True @@ -6768,7 +6768,7 @@ def drawInverseObjProperties(self, first, second, diagram, x, y): if self.isAtomic(second): secondIRI = second.getIRI() - secondNode = self.findNode(secondIRI, diagram) + secondNode = self.findNode(second, diagram) if secondNode != 'null': secondDrawn = True @@ -6936,14 +6936,14 @@ def drawSubProperty(self, sub, sup, diagram, x, y): if self.isAtomic(sub): sub_iri = sub.getIRI() - node0 = self.findNode(sub_iri, diagram) + node0 = self.findNode(sub, diagram) if node0 != 'null': subDrawn = True if self.isAtomic(sup): sup_iri = sup.getIRI() - node1 = self.findNode(sup_iri, diagram) + node1 = self.findNode(sup, diagram) if node1 != 'null': supDrawn = True @@ -7119,7 +7119,7 @@ def drawEquivalentClasses(self, expressions, diagram, x, y): for e in expressions: if self.isAtomic(e): iri = e.getIRI() - node = self.findNode(iri, diagram) + node = self.findNode(e, diagram) if node != 'null': nodes.append(node) if self.isIsolated(node): @@ -7190,7 +7190,7 @@ def drawEquivalentClasses(self, expressions, diagram, x, y): if ex.isType(self.EntityType.CLASS): iri = ex.getIRI() - node = self.findNode(iri, diagram) + node = self.findNode(ex, diagram) if node == 'null': @@ -7337,7 +7337,7 @@ def drawEquivalentProperties(self, expressions, diagram, x, y): for e in expressions: if self.isAtomic(e): iri = e.getIRI() - node = self.findNode(iri, diagram) + node = self.findNode(e, diagram) if node != 'null': nodes.append(node) @@ -7367,7 +7367,7 @@ def drawEquivalentProperties(self, expressions, diagram, x, y): if ex.isType(self.EntityType.DATA_PROPERTY) or ex.isType(self.EntityType.OBJECT_PROPERTY): iri = ex.getIRI() - node = self.findNode(iri, diagram) + node = self.findNode(ex, diagram) if node == 'null': @@ -7485,17 +7485,17 @@ def drawClassAssertion(self, indiv, classs, diagram, x, y): if self.isAtomic(classs): class_iri = classs.getIRI() - if self.findNode(class_iri, diagram) != 'null': + if self.findNode(classs, diagram) != 'null': - node1 = self.findNode(class_iri, diagram) + node1 = self.findNode(classs, diagram) classDrawn = True if self.isAtomic(indiv): indiv_iri = indiv.getIRI() - if self.findNode(indiv_iri, diagram) != 'null': + if self.findNode(indiv, diagram) != 'null': - node0 = self.findNode(indiv_iri, diagram) + node0 = self.findNode(indiv, diagram) indivDrawn = True if classDrawn: @@ -7628,7 +7628,7 @@ def drawPropertyAssertion(self, prop, indiv, value, diagram, x, y): if self.isAtomic(prop): propIri = prop.getIRI() - propNode = self.findNode(propIri, diagram) + propNode = self.findNode(prop, diagram) if propNode != 'null': propDrawn = True @@ -7636,7 +7636,7 @@ def drawPropertyAssertion(self, prop, indiv, value, diagram, x, y): if self.isAtomic(indiv): indvIri = indiv.getIRI() - indivNode = self.findNode(indvIri, diagram) + indivNode = self.findNode(indiv, diagram) if indivNode != 'null': indivDrawn = True @@ -7645,7 +7645,7 @@ def drawPropertyAssertion(self, prop, indiv, value, diagram, x, y): if isinstance(value, self.OWLNamedIndividual): valueIri = value.getIRI() - valueNode = self.findNode(valueIri, diagram) + valueNode = self.findNode(value, diagram) if valueNode != 'null': valueDrawn = True @@ -7933,7 +7933,7 @@ def drawNegativePropertyAssertion(self, prop, indiv, value, diagram, x, y): if self.isAtomic(prop): propIri = prop.getIRI() - propNode = self.findNode(propIri, diagram) + propNode = self.findNode(prop, diagram) if propNode != 'null': propDrawn = True @@ -7941,7 +7941,7 @@ def drawNegativePropertyAssertion(self, prop, indiv, value, diagram, x, y): if self.isAtomic(indiv): indvIri = indiv.getIRI() - indivNode = self.findNode(indvIri, diagram) + indivNode = self.findNode(indiv, diagram) if indivNode != 'null': indivDrawn = True @@ -7950,7 +7950,7 @@ def drawNegativePropertyAssertion(self, prop, indiv, value, diagram, x, y): if isinstance(value, self.OWLNamedIndividual): valueIri = value.getIRI() - valueNode = self.findNode(valueIri, diagram) + valueNode = self.findNode(value, diagram) if valueNode != 'null': valueDrawn = True @@ -8243,7 +8243,7 @@ def drawDiffIndiv(self, individuals, diagram, x, y): if self.isAtomic(e): iri = e.getIRI() - node = self.findNode(iri, diagram) + node = self.findNode(e, diagram) if node != 'null': nodes.append(node) @@ -8274,7 +8274,7 @@ def drawDiffIndiv(self, individuals, diagram, x, y): if i.isType(self.EntityType.NAMED_INDIVIDUAL): iri = i.getIRI() - node = self.findNode(iri, diagram) + node = self.findNode(i, diagram) if node == 'null': x = starting_x + 125 @@ -8359,7 +8359,7 @@ def drawSameIndiv(self, individuals, diagram, x, y): for e in individuals: if self.isAtomic(e): iri = e.getIRI() - node = self.findNode(iri, diagram) + node = self.findNode(e, diagram) if node != 'null': nodes.append(node) @@ -8392,7 +8392,7 @@ def drawSameIndiv(self, individuals, diagram, x, y): if i.isType(self.EntityType.NAMED_INDIVIDUAL): iri = i.getIRI() - node = self.findNode(iri, diagram) + node = self.findNode(i, diagram) if node == 'null': @@ -8481,7 +8481,7 @@ def drawDisjointClasses(self, expressions, diagram, x, y): for e in expressions: if self.isAtomic(e): iri = e.getIRI() - node = self.findNode(iri, diagram) + node = self.findNode(e, diagram) if node != 'null': nodes.append(node) @@ -8520,7 +8520,7 @@ def drawDisjointClasses(self, expressions, diagram, x, y): if ex.isType(self.EntityType.CLASS): iri = ex.getIRI() - node = self.findNode(iri, diagram) + node = self.findNode(ex, diagram) if node == 'null': @@ -8662,7 +8662,7 @@ def drawDisjointUnion(self, classes, classs, diagram, x, y): if self.isAtomic(e): iri = e.getIRI() - node = self.findNode(iri, diagram) + node = self.findNode(e, diagram) if node != 'null': nodes.append(node) x_positions.append(node.pos().x()) @@ -8676,7 +8676,7 @@ def drawDisjointUnion(self, classes, classs, diagram, x, y): if self.isAtomic(classs): cIRI = classs.getIRI() - cNode = self.findNode(cIRI, diagram) + cNode = self.findNode(classs, diagram) if cNode != 'null': @@ -8713,7 +8713,7 @@ def drawDisjointUnion(self, classes, classs, diagram, x, y): if c.isType(self.EntityType.CLASS): iri = c.getIRI() - node = self.findNode(iri, diagram) + node = self.findNode(c, diagram) if node == 'null': @@ -8761,7 +8761,7 @@ def drawDisjointUnion(self, classes, classs, diagram, x, y): if self.isAtomic(classs): cIri = classs.getIRI() - cNode = self.findNode(cIri, diagram) if self.findNode(cIri, diagram) != 'null' else self.createNode(classs, diagram, x_med, y_med-100) + cNode = self.findNode(classs, diagram) if self.findNode(classs, diagram) != 'null' else self.createNode(classs, diagram, x_med, y_med-100) if self.isIsolated(cNode): cNode.setPos(x_med, y_med-100) @@ -8830,7 +8830,7 @@ def drawDisjointDataProperties(self, expressions, diagram, x, y): for e in expressions: if self.isAtomic(e): iri = e.getIRI() - node = self.findNode(iri, diagram) + node = self.findNode(e, diagram) if node != 'null': nodes.append(node) x_positions.append(node.pos().x()) @@ -8869,7 +8869,7 @@ def drawDisjointDataProperties(self, expressions, diagram, x, y): else: if ex.isType(self.EntityType.DATA_PROPERTY): iri = ex.getIRI() - node = self.findNode(iri, diagram) + node = self.findNode(ex, diagram) if node == 'null': @@ -8980,7 +8980,7 @@ def drawDisjointObjectProperties(self, expressions, diagram, x, y): for e in expressions: if self.isAtomic(e): iri = e.getIRI() - node = self.findNode(iri, diagram) + node = self.findNode(e, diagram) if node != 'null': nodes.append(node) x_positions.append(node.pos().x()) @@ -9018,7 +9018,7 @@ def drawDisjointObjectProperties(self, expressions, diagram, x, y): if ex.isType(self.EntityType.OBJECT_PROPERTY): iri = ex.getIRI() - node = self.findNode(iri, diagram) + node = self.findNode(ex, diagram) if node == 'null': @@ -9446,12 +9446,18 @@ def createNode(self, ex, diagram, x, y): return - def findNode(self, iri, diagram): + def findNode(self, item, diagram): + Types = { + Item.AttributeNode: 'DataProperty', + Item.ConceptNode: 'Class', + Item.IndividualNode: 'NamedIndividual', + Item.RoleNode: 'ObjectProperty', + } ### FIND NODE BY IRI IN THE DIAGRAM ### for el in diagram.items(): - if el.isNode() and (el.type() == Item.ConceptNode or el.type() == Item.IndividualNode or el.type() == Item.RoleNode or el.type() == Item.AttributeNode) and str(iri) == str(el.iri): + if el.isNode() and el.type() in Types.keys() and (Types[el.type()] == str(item.getEntityType())) and str(item.getIRI()) == str(el.iri): return el # IF NOT FOUND, RETURN 'NULL' From d8903eb472a54b53d5780087e25ce7d4abcd0258 Mon Sep 17 00:00:00 2001 From: rooosyf Date: Mon, 5 Dec 2022 19:33:29 +0100 Subject: [PATCH 5/9] plugins: ontology-importer: fix on possibility to draw declared entities --- .../ontology-importer/ontology_importer.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/eddy/plugins/ontology-importer/ontology_importer.py b/eddy/plugins/ontology-importer/ontology_importer.py index 73184a92..68dacfc0 100644 --- a/eddy/plugins/ontology-importer/ontology_importer.py +++ b/eddy/plugins/ontology-importer/ontology_importer.py @@ -1462,13 +1462,18 @@ def open(self): # ALL # ontology_iri, ontology_version = ontology - cursor.execute('''SELECT axiom + cursor.execute('''SELECT axiom, iri_dict FROM axiom WHERE ontology_iri = ? and ontology_version = ?''', (ontology_iri, ontology_version)) rows = cursor.fetchall() axioms[ontology] = [] + all_dicts = [] for row in rows: axioms[ontology].append(row[0]) + iri_dict = row[1] + d = ast.literal_eval(iri_dict) + iris = [d[k] for k in d.keys()] + all_dicts.append(iris) # NOT DRAWN # cursor.execute('''SELECT axiom, type_of_axiom, func_axiom, iri_dict @@ -1485,12 +1490,6 @@ def open(self): and (?, ?, axiom) not in (SELECT project_iri, project_version, axiom FROM drawn)''', (ontology_iri, ontology_version, self.project_iri, self.project_version)) rows = cursor.fetchall() - all_dicts = [] - for r in rows: - iri_dict = r[3] - d = ast.literal_eval(iri_dict) - iris = [d[k] for k in d.keys()] - all_dicts.append(iris) not_drawn[ontology] = [] for r in rows: if r[1] == 'Declaration': @@ -1499,8 +1498,8 @@ def open(self): if d: entityIRI = d[list(d.keys())[0]] countList = [entityIRI in el for el in all_dicts] - count = countList.count(True) - if count == 1: + cnt = countList.count(True) + if cnt == 1: not_drawn[ontology].append([r[0], r[1], r[2]]) else: not_drawn[ontology].append([r[0], r[1], r[2]]) From 249ece7743b6b64f33c28689b02d9dfdb72f2286 Mon Sep 17 00:00:00 2001 From: rooosyf Date: Tue, 13 Dec 2022 20:40:12 +0100 Subject: [PATCH 6/9] plugins: ontology-importer: db version 2 --- .../ontology-importer/ontology_importer.py | 66 +++++++++++++------ eddy/plugins/ontology-importer/plugin.spec | 2 +- 2 files changed, 47 insertions(+), 21 deletions(-) diff --git a/eddy/plugins/ontology-importer/ontology_importer.py b/eddy/plugins/ontology-importer/ontology_importer.py index 68dacfc0..528aa6fc 100644 --- a/eddy/plugins/ontology-importer/ontology_importer.py +++ b/eddy/plugins/ontology-importer/ontology_importer.py @@ -129,7 +129,7 @@ CREATE TABLE IF NOT EXISTS axiom ( axiom TEXT, type_of_axiom TEXT, - func_axiom TEXT, + manch_axiom TEXT, ontology_iri TEXT, ontology_version TEXT, iri_dict TEXT, @@ -1122,7 +1122,7 @@ def doOpenOntologyFile(self, my_owl_file: Optional[str] = None): values (?, ?, ?, ?, ?, ?) """, ( str(self.project.ontologyIRI), self.project.version, - self.ontology_iri, self.ontology_version, str(axiom), + self.ontology_iri, self.ontology_version, str(ax), str(self.session))) conn.commit() processed.append(ax) @@ -1191,6 +1191,27 @@ def checkDatabase(self): cursor = conn.cursor() version = self.spec.getint('database', 'version') nversion = int(cursor.execute('PRAGMA user_version').fetchone()[0]) + upgrade_commands = {1 : """ + BEGIN TRANSACTION; + + UPDATE drawn + SET axiom = (SELECT a.func_axiom + FROM axiom a + WHERE a.axiom = drawn.axiom + AND a.ontology_iri = drawn.ontology_iri + AND a.ontology_version = drawn.ontology_version + ); + + -- Scambia axiom con func_axiom nella tabella axiom + + UPDATE axiom SET axiom = func_axiom, func_axiom = axiom; + + -- Rinomina func_axiom + + ALTER TABLE axiom RENAME COLUMN func_axiom TO manch_axiom; + + COMMIT; + """} if nversion > self.spec.getint('database', 'version'): msgbox = QtWidgets.QMessageBox() msgbox.setIconPixmap(QtGui.QIcon(':/icons/48/ic_warning_black').pixmap(48)) @@ -1212,7 +1233,12 @@ def checkDatabase(self): else: raise DatabaseError( 'Incompatible import database version {} > {}'.format(nversion, version)) - + else: + while nversion != version: + cursor.executescript(upgrade_commands[nversion]) + nversion = nversion + 1 + conn.executescript("PRAGMA user_version = {version};".format( + version=nversion)) def dispose(self): """ Executed whenever the plugin is going to be destroyed. @@ -1383,16 +1409,16 @@ def insertInDB(self, ontology): # getting the axiom in Manchester Syntax # - axiom = renderer.render(ax) + manch_axiom = renderer.render(ax) # keep the original form (Functional Syntax # funcAxiom = ax QtCore.QCoreApplication.processEvents() # INSERT AXIOM IN DB # conn.execute(""" - insert or ignore into axiom (axiom, type_of_axiom, func_axiom, ontology_iri, ontology_version, iri_dict) + insert or ignore into axiom (axiom, type_of_axiom, manch_axiom, ontology_iri, ontology_version, iri_dict) values (?, ?, ?, ?, ?, ?) - """, (str(axiom).strip(), str(ax_type), str(funcAxiom).strip(), ontology_iri, ontology_version, iri_dict)) + """, (str(funcAxiom).strip(), str(ax_type), str(manch_axiom).strip(), ontology_iri, ontology_version, iri_dict)) conn.commit() QtCore.QCoreApplication.processEvents() @@ -1476,7 +1502,7 @@ def open(self): all_dicts.append(iris) # NOT DRAWN # - cursor.execute('''SELECT axiom, type_of_axiom, func_axiom, iri_dict + cursor.execute('''SELECT axiom, type_of_axiom, manch_axiom, iri_dict FROM axiom WHERE ontology_iri = ? and ontology_version = ? and type_of_axiom != 'FunctionalObjectProperty' @@ -1641,7 +1667,7 @@ def __init__(self, not_drawn, project): for pair in not_drawn[k]: - ax = pair[0] + ax = pair[2] ax_type = pair[1] if ax_type in ['SubClassOf', 'EquivalentClasses', 'DisjointClasses']: @@ -1898,7 +1924,6 @@ def accept(self): axiomsToDraw = {} cantDraw = [] - for ax in self.checkedAxioms: # for each checked axiom: QtCore.QCoreApplication.processEvents() @@ -1907,19 +1932,20 @@ def accept(self): for k in self.not_drawn.keys(): for triple in self.not_drawn[k]: - manch_ax = triple[0] - func_ax = triple[2] - if manch_ax == ax: + manch_ax = triple[2] + func_ax = triple[0] + if manch_ax == ax and func_ax not in axiomsToDraw.keys(): # try to parse Functional axiom string into Axiom # functional_axiom = self.string2axiom2(func_ax) + break; if functional_axiom: - axiomsToDraw[ax] = functional_axiom + axiomsToDraw[func_ax] = functional_axiom else: # if problems with Functional parsing -> try to parse Manchester axiom string into Axiom # manchester_axiom = self.string2axiom(ax) if manchester_axiom: - axiomsToDraw[ax] = manchester_axiom + axiomsToDraw[func_ax] = manchester_axiom else: # if axioms can't be parsed -> can't draw message # cantDraw.append(ax) @@ -1971,7 +1997,7 @@ def string2axiom2(self, ax): cursor = conn.cursor() cursor.execute('''SELECT * FROM axiom - WHERE ontology_iri = ? and ontology_version = ? and func_axiom = ? + WHERE ontology_iri = ? and ontology_version = ? and axiom = ? ''', (ontology_iri, ontology_version, ax)) if len(cursor.fetchall()) > 0: @@ -1982,7 +2008,7 @@ def string2axiom2(self, ax): manager = self.OWLManager().createOWLOntologyManager() # get all declaration axioms of the ontology to pass to parser # - cursor.execute('''SELECT axiom, iri_dict, func_axiom + cursor.execute('''SELECT manch_axiom, iri_dict, axiom FROM axiom WHERE ontology_iri = ? and ontology_version = ? and type_of_axiom = ? ''', @@ -2018,7 +2044,7 @@ def string2axiom2(self, ax): cursor.execute('''SELECT iri_dict, type_of_axiom FROM axiom - WHERE ontology_iri = ? and ontology_version = ? and func_axiom = ? + WHERE ontology_iri = ? and ontology_version = ? and axiom = ? ''', (ontology_iri, ontology_version, ax)) rows = cursor.fetchall() @@ -2074,7 +2100,7 @@ def string2axiom(self, ax): cursor = conn.cursor() cursor.execute('''SELECT * FROM axiom - WHERE ontology_iri = ? and ontology_version = ? and axiom = ? + WHERE ontology_iri = ? and ontology_version = ? and manch_axiom = ? ''', (ontology_iri, ontology_version, ax)) if len(cursor.fetchall()) > 0: @@ -2087,7 +2113,7 @@ def string2axiom(self, ax): # create new ontology # o = manager.createOntology() # get all declaration axioms of the ontology to pass to parser # - cursor.execute('''SELECT axiom, iri_dict + cursor.execute('''SELECT manch_axiom, iri_dict FROM axiom WHERE ontology_iri = ? and ontology_version = ? and type_of_axiom = ? ''', @@ -2231,7 +2257,7 @@ def string2axiom(self, ax): # get fullIRIs dict and type of axiom # cursor.execute('''SELECT iri_dict, type_of_axiom FROM axiom - WHERE ontology_iri = ? and ontology_version = ? and axiom = ? + WHERE ontology_iri = ? and ontology_version = ? and manch_axiom = ? ''', (ontology_iri, ontology_version, ax)) rows = cursor.fetchall() diff --git a/eddy/plugins/ontology-importer/plugin.spec b/eddy/plugins/ontology-importer/plugin.spec index 8c31c08f..dfe0bba8 100644 --- a/eddy/plugins/ontology-importer/plugin.spec +++ b/eddy/plugins/ontology-importer/plugin.spec @@ -38,4 +38,4 @@ name: Ontology Importer version: 0.1 [database] -version: 1 +version: 2 From d73a134d423878d6dddc52a9060f803631cf9977 Mon Sep 17 00:00:00 2001 From: rooosyf Date: Wed, 28 Dec 2022 12:44:17 +0100 Subject: [PATCH 7/9] plugins: ontology-importer: added temporary tables for correct importations saving --- .../ontology-importer/ontology_importer.py | 95 ++++++++++++++++--- 1 file changed, 84 insertions(+), 11 deletions(-) diff --git a/eddy/plugins/ontology-importer/ontology_importer.py b/eddy/plugins/ontology-importer/ontology_importer.py index 528aa6fc..f47a60cc 100644 --- a/eddy/plugins/ontology-importer/ontology_importer.py +++ b/eddy/plugins/ontology-importer/ontology_importer.py @@ -1101,7 +1101,15 @@ def doOpenOntologyFile(self, my_owl_file: Optional[str] = None): processed = [] not_processed = [] total = [] - + conn.executescript("""CREATE TABLE IF NOT EXISTS temp_drawn ( + project_iri TEXT, + project_version TEXT, + ontology_iri TEXT, + ontology_version TEXT, + axiom TEXT, + session_id TEXT, + PRIMARY KEY (project_iri, project_version, ontology_iri, ontology_version, axiom));""") + conn.commit() for ax in self.axioms: total.append(ax) @@ -1125,6 +1133,15 @@ def doOpenOntologyFile(self, my_owl_file: Optional[str] = None): self.ontology_iri, self.ontology_version, str(ax), str(self.session))) conn.commit() + conn.execute("""insert or ignore into temp_drawn (project_iri, project_version, ontology_iri, ontology_version, axiom, session_id) + values (?, ?, ?, ?, ?, ?)""", + ( + str(self.project.ontologyIRI), + self.project.version, + self.ontology_iri, self.ontology_version, + str(ax), + str(self.session))) + conn.commit() processed.append(ax) QtCore.QCoreApplication.processEvents() @@ -1178,6 +1195,14 @@ def onNoSave(self): importation = Importation(self.project) importation.removeFromDB() + db = expandPath(K_IMPORTS_DB) + if os.path.exists(db): + conn = sqlite3.connect(db) + conn.executescript("""DROP TABLE IF EXISTS temp_importation;""") + conn.commit() + conn.executescript("""DROP TABLE IF EXISTS temp_drawn;""") + conn.commit() + conn.close() def checkDatabase(self): """ @@ -1454,6 +1479,21 @@ def insertInDB(self, ontology): values (?, ?, ?, ?, ?) """, (self.project_iri, self.project_version, ontology_iri, ontology_version, str(session))) conn.commit() + conn.executescript("""CREATE TABLE IF NOT EXISTS temp_importation( + project_iri TEXT, + project_version TEXT, + ontology_iri TEXT, + ontology_version TEXT, + session_id TEXT, + PRIMARY KEY (project_iri, project_version, ontology_iri, ontology_version) + );""") + conn.commit() + conn.execute(""" + insert into temp_importation (project_iri, project_version, ontology_iri, ontology_version, session_id) + values (?, ?, ?, ?, ?) + """, (self.project_iri, self.project_version, ontology_iri, ontology_version, str(session))) + conn.commit() + conn.close() return False @@ -1543,16 +1583,34 @@ def removeFromDB(self): conn = sqlite3.connect(self.db_filename) with conn: - ### REMOVE IMPORTATIONS NOT SAVED ### - conn.execute( - 'delete from importation where project_iri = ? and project_version = ? and session_id = ?', - (self.project_iri, self.project_version, str(self.project.session))) - conn.commit() - ### REMOVE DRAWS NOT SAVED ### - conn.execute( - 'delete from drawn where project_iri = ? and project_version = ? and session_id = ?', - (self.project_iri, self.project_version, str(self.project.session))) - conn.commit() + cursor = conn.cursor() + cursor.execute("""SELECT name FROM sqlite_master WHERE type='table' AND name='temp_importation';""") + temp_impo = len(cursor.fetchall()) > 0 + if temp_impo: + cursor.execute('''SELECT project_iri, project_version, ontology_iri, ontology_version, session_id + FROM temp_importation''') + rows = cursor.fetchall() + ### REMOVE IMPORTATIONS NOT SAVED ### + for row in rows: + conn.execute( + 'delete from importation where project_iri = ? and project_version = ? and ontology_iri = ? and ontology_version = ? and session_id = ?', + (row[0], row[1], row[2], row[3], row[4])) + conn.commit() + + cursor.execute( + """SELECT name FROM sqlite_master WHERE type='table' AND name='temp_drawn';""") + temp_drawn = len(cursor.fetchall()) > 0 + if temp_drawn: + ### REMOVE DRAWS NOT SAVED ### + cursor.execute('''SELECT project_iri, project_version, ontology_iri, ontology_version, axiom, session_id + FROM temp_drawn''') + rows = cursor.fetchall() + ### REMOVE IMPORTATIONS NOT SAVED ### + for row in rows: + conn.execute( + 'delete from drawn where project_iri = ? and project_version = ? and ontology_iri = ? and ontology_version = ? and axiom = ? and session_id = ?', + (row[0], row[1], row[2], row[3], row[4], row[5])) + conn.commit() class AxiomSelectionDialog(QtWidgets.QDialog, HasWidgetSystem): @@ -2657,6 +2715,16 @@ def drawAxioms(self, dict): except Exception as e: print(e) + conn.executescript("""CREATE TABLE IF NOT EXISTS temp_drawn ( + project_iri TEXT, + project_version TEXT, + ontology_iri TEXT, + ontology_version TEXT, + axiom TEXT, + session_id TEXT, + PRIMARY KEY (project_iri, project_version, ontology_iri, ontology_version, axiom));""") + conn.commit() + # INSERT AXIOMS IN THE DRAWN TABLE # cursor = conn.cursor() # GET ALL THE ONTOLOGIES IMPORTED IN THE PROJECT # @@ -2743,6 +2811,11 @@ def drawAxioms(self, dict): cur.execute(sql, ( self.project_iri, self.project_version, ontology_iri, ontology_version, str(ax), str(self.session))) conn.commit() + sql2 = 'INSERT INTO temp_drawn (project_iri, project_version, ontology_iri, ontology_version, axiom, session_id) VALUES (?, ?, ?, ?, ?, ?)' + cur.execute(sql2, ( + self.project_iri, self.project_version, ontology_iri, ontology_version, + str(ax), str(self.session))) + conn.commit() conn.close() # snap to grid # From 7283cce28fdf3204677e220a620a1a73134fdc2f Mon Sep 17 00:00:00 2001 From: rooosyf Date: Wed, 28 Dec 2022 17:18:44 +0100 Subject: [PATCH 8/9] plugins: ontology-importer: connected sgnProjectSaved and added check on session_id on temp tables --- .../ontology-importer/ontology_importer.py | 50 +++++++++++++++---- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/eddy/plugins/ontology-importer/ontology_importer.py b/eddy/plugins/ontology-importer/ontology_importer.py index f47a60cc..bd9159d7 100644 --- a/eddy/plugins/ontology-importer/ontology_importer.py +++ b/eddy/plugins/ontology-importer/ontology_importer.py @@ -1195,13 +1195,41 @@ def onNoSave(self): importation = Importation(self.project) importation.removeFromDB() + self.onSave() + + @QtCore.pyqtSlot() + def onSave(self): db = expandPath(K_IMPORTS_DB) if os.path.exists(db): conn = sqlite3.connect(db) - conn.executescript("""DROP TABLE IF EXISTS temp_importation;""") - conn.commit() - conn.executescript("""DROP TABLE IF EXISTS temp_drawn;""") - conn.commit() + cursor = conn.cursor() + # check if there is any temporary importation + cursor.execute( + """SELECT name FROM sqlite_master WHERE type='table' AND name='temp_importation';""") + temp_impo = len(cursor.fetchall()) > 0 + if temp_impo: + # remove all the importations of the saved session from temp table -> make them permanent + cursor.execute("delete from temp_importation where session_id = ?", (str(self.project.session),)) + conn.commit() + cursor.execute("select * from temp_importation") + if not cursor.fetchall(): + # if no more temporary importations -> drop temp table + conn.executescript("""DROP TABLE IF EXISTS temp_importation;""") + conn.commit() + # check if there is any temporary draw + cursor.execute( + """SELECT name FROM sqlite_master WHERE type='table' AND name='temp_drawn';""") + temp_drawn = len(cursor.fetchall()) > 0 + if temp_drawn: + # remove all the drawn axioms of the saved session from temp table -> make them permanent + cursor.execute("delete from temp_drawn where session_id = ?", + (str(self.project.session),)) + conn.commit() + cursor.execute("select * from temp_drawn") + if not cursor.fetchall(): + # if no more temporary drawn -> drop temp table + conn.executescript("""DROP TABLE IF EXISTS temp_drawn;""") + conn.commit() conn.close() def checkDatabase(self): @@ -1271,6 +1299,7 @@ def dispose(self): # DISCONNECT SIGNALS/SLOTS self.debug('Disconnecting from active session') disconnect(self.session.sgnNoSaveProject, self.onNoSave) + disconnect(self.session.sgnProjectSaved, self.onSave) # UNINSTALL WIDGETS FROM THE ACTIVE SESSION self.debug('Uninstalling OWL 2 importer controls from "view" toolbar') @@ -1309,6 +1338,7 @@ def start(self): # CONFIGURE SIGNALS/SLOTS connect(self.session.sgnNoSaveProject, self.onNoSave) + connect(self.session.sgnProjectSaved, self.onSave) connect(self.session.sgnStartOwlImport, self.doOpenOntologyFile) # importation in DB # @@ -1579,16 +1609,17 @@ def removeFromDB(self): db_exists = os.path.exists(self.db_filename) if db_exists: - conn = sqlite3.connect(self.db_filename) with conn: cursor = conn.cursor() + # check if there are temporary importations cursor.execute("""SELECT name FROM sqlite_master WHERE type='table' AND name='temp_importation';""") temp_impo = len(cursor.fetchall()) > 0 if temp_impo: + # get the temporary importations of the current session cursor.execute('''SELECT project_iri, project_version, ontology_iri, ontology_version, session_id - FROM temp_importation''') + FROM temp_importation where session_id = ?''', (str(self.project.session),)) rows = cursor.fetchall() ### REMOVE IMPORTATIONS NOT SAVED ### for row in rows: @@ -1597,15 +1628,16 @@ def removeFromDB(self): (row[0], row[1], row[2], row[3], row[4])) conn.commit() + # check if there are temporary drawn axioms cursor.execute( """SELECT name FROM sqlite_master WHERE type='table' AND name='temp_drawn';""") temp_drawn = len(cursor.fetchall()) > 0 if temp_drawn: - ### REMOVE DRAWS NOT SAVED ### + # get the temporary draws of the current session cursor.execute('''SELECT project_iri, project_version, ontology_iri, ontology_version, axiom, session_id - FROM temp_drawn''') + FROM temp_drawn where session_id = ?''', (str(self.project.session),)) rows = cursor.fetchall() - ### REMOVE IMPORTATIONS NOT SAVED ### + ### REMOVE DRAWS NOT SAVED ### for row in rows: conn.execute( 'delete from drawn where project_iri = ? and project_version = ? and ontology_iri = ? and ontology_version = ? and axiom = ? and session_id = ?', From b252f06d717b3ba328532c5bd08f7c08749da7c9 Mon Sep 17 00:00:00 2001 From: rooosyf Date: Thu, 29 Dec 2022 16:24:07 +0100 Subject: [PATCH 9/9] ui: session: emit sgnNoSaveProject only when closing without saving Previous to this, the signal was emitted even when the project was closed from a clean state, i.e. without changes triggering the confirmation dialog, and resulted in the unintended removal of any importation process started during the current session. --- eddy/ui/session.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/eddy/ui/session.py b/eddy/ui/session.py index b142d387..3f0d8223 100644 --- a/eddy/ui/session.py +++ b/eddy/ui/session.py @@ -3472,6 +3472,7 @@ def closeEvent(self, closeEvent: QtGui.QCloseEvent) -> None: close = False elif msgbox.result() == QtWidgets.QMessageBox.No: save = False + self.sgnNoSaveProject.emit() elif msgbox.result() == QtWidgets.QMessageBox.Yes: save = True @@ -3481,8 +3482,6 @@ def closeEvent(self, closeEvent: QtGui.QCloseEvent) -> None: # SAVE THE CURRENT PROJECT IF NEEDED if save: self.sgnSaveProject.emit() - else: - self.sgnNoSaveProject.emit() # DISPOSE ALL THE PLUGINS for plugin in self.plugins(): self.pmanager.dispose(plugin)