owlentityremover.java
来自「Semantic Web Ontology Editor」· Java 代码 · 共 957 行 · 第 1/3 页
JAVA
957 行
if (desc instanceof OWLClass && ((OWLClass) desc).equals(remEntity)) { if (replEntity!=null) return replEntity; else return this.DISCARD; } else if (desc instanceof OWLAnd) { // if description is intersection, get operands OWLAnd and = (OWLAnd) desc; Set operands = and.getOperands(); boolean changed = false; for (Iterator iter = new HashSet(operands).iterator(); iter.hasNext();) { OWLDescription andOp = (OWLDescription) iter.next(); Object check = this.traceDescription(andOp); if (check!=null) { // match found for entity changed = true; operands.remove(andOp); // add new (updated) description if any if (check instanceof OWLDescription) operands.add((OWLDescription) check); } } if (changed) { return ontology.getOWLDataFactory().getOWLAnd(operands); } } else if (desc instanceof OWLOr) { // if description is union, get operands OWLOr or = (OWLOr) desc; Set operands = or.getOperands(); boolean changed = false; for (Iterator iter = new HashSet(operands).iterator(); iter.hasNext();) { OWLDescription orOp = (OWLDescription) iter.next(); Object check = this.traceDescription(orOp); if (check!=null) { // match found for entity changed = true; operands.remove(orOp); // add new (updated) description if any if (check instanceof OWLDescription) operands.add((OWLDescription) check); } } if (changed) { return ontology.getOWLDataFactory().getOWLOr(operands); } } else if (desc instanceof OWLNot) { OWLNot not = (OWLNot) desc; Object check = traceDescription(not.getOperand()); if (check!=null) { // match found for entity // either return updated ~Desc or DISCARD if (check instanceof OWLDescription) return ontology.getOWLDataFactory().getOWLNot((OWLDescription) check); else { if (replEntity!=null) return replEntity; else return this.DISCARD; // e.g. ~A. where entity = A } } } else if (desc instanceof OWLRestriction) { // check property of restriction first to discard entire restriction if (((OWLRestriction) desc).getProperty().equals(remEntity)) { if (replEntity!=null) { // create OWLRestriction replacing prop w/ replEntity return replaceRestriction((OWLRestriction) desc, (OWLProperty) replEntity); } else return this.DISCARD; } // check some/all value restriction if (desc instanceof OWLObjectQuantifiedRestriction) { OWLObjectQuantifiedRestriction objRes = (OWLObjectQuantifiedRestriction) desc; OWLObjectProperty oprop = objRes.getObjectProperty(); Object check = traceDescription(objRes.getDescription()); if (check!=null) { if (check instanceof OWLDescription) { // return new restriction OWLObjectQuantifiedRestriction rest = null; if (objRes instanceof OWLObjectAllRestriction) rest = ontology.getOWLDataFactory().getOWLObjectAllRestriction(oprop, (OWLDescription) check); else rest = ontology.getOWLDataFactory().getOWLObjectSomeRestriction(oprop, (OWLDescription) check); return rest; } else { if (replEntity!=null) return replEntity; else return this.DISCARD; // e.g exists(P,C), where entity = P; } } } // finally check hasValue restriction if (desc instanceof OWLObjectValueRestriction) { OWLObjectValueRestriction objRes = (OWLObjectValueRestriction) desc; // discard if match found for individual if (objRes.getIndividual().equals(remEntity)) { if (replEntity!=null) return replEntity; else return this.DISCARD; } } } else if (desc instanceof OWLEnumeration) { // check contents of enum to see if entity found boolean changed = false; Set indOps = ((OWLEnumeration) desc).getIndividuals(); for (Iterator iter = new HashSet(indOps).iterator(); iter.hasNext();) { OWLIndividual ind = (OWLIndividual) iter.next(); if (ind.equals(remEntity)) { // match found changed = true; indOps.remove(ind); if (replEntity!=null) indOps.add(replEntity); } } if (changed) { if (indOps.size()==0) { if (replEntity!=null) return replEntity; else return this.DISCARD; } else { return ontology.getOWLDataFactory().getOWLEnumeration(indOps); } } } } catch (OWLException ex) { ex.printStackTrace(); } return null; } public void removeChange(Object subj, Object obj, int relType) throws OWLException { OntologyChange changed = null; switch (relType) { case SUPERCLASS: changed = new RemoveSuperClass(ontology, (OWLClass) subj, (OWLDescription) obj, null); changes.add(changed); break; case EQUIVALENTCLASS: changed = new RemoveEquivalentClass(ontology, (OWLClass) subj, (OWLDescription) obj, null); changes.add(changed); break; case ENUMERATION: changed = new RemoveEnumeration(ontology, (OWLClass) subj, (OWLEnumeration) obj, null); changes.add(changed); break; case SUBCLASSAXIOM: OWLSubClassAxiom axiom = ontology.getOWLDataFactory().getOWLSubClassAxiom((OWLDescription) subj, (OWLDescription) obj); changed = new RemoveClassAxiom(ontology, axiom, null); changes.add(changed); break; case EQUCLASSAXIOM: OWLEquivalentClassesAxiom equAxiom = ontology.getOWLDataFactory().getOWLEquivalentClassesAxiom((Set) subj); changed = new RemoveClassAxiom(ontology, equAxiom, null); changes.add(changed); break; case DISJOINTAXIOM: OWLDisjointClassesAxiom disAxiom = ontology.getOWLDataFactory().getOWLDisjointClassesAxiom((Set) subj); changed = new RemoveClassAxiom(ontology, disAxiom, null); changes.add(changed); break; case SUPERPROPERTY: changed = new RemoveSuperProperty(ontology, (OWLProperty) subj, (OWLProperty) obj, null); changes.add(changed); break; case SUBPROPAXIOM: OWLSubPropertyAxiom subPAxiom = ontology.getOWLDataFactory().getOWLSubPropertyAxiom((OWLProperty) subj, (OWLProperty) obj); changed = new RemovePropertyAxiom(ontology, subPAxiom, null); changes.add(changed); break; case EQUPROPAXIOM: OWLEquivalentPropertiesAxiom equPAxiom = ontology.getOWLDataFactory().getOWLEquivalentPropertiesAxiom((Set) subj); changed = new RemovePropertyAxiom(ontology, equPAxiom, null); changes.add(changed); break; case DOMAIN: changed = new RemoveDomain(ontology, (OWLProperty) subj, (OWLDescription) obj, null); changes.add(changed); break; case RANGE: changed = new RemoveObjectPropertyRange(ontology, (OWLObjectProperty) subj, (OWLDescription) obj, null); changes.add(changed); break; case INVERSE: changed = new RemoveInverse(ontology, (OWLObjectProperty) subj, (OWLObjectProperty) obj, null); changes.add(changed); break; case INDIVIDUALCLASS: changed = new RemoveIndividualClass(ontology, (OWLIndividual) subj, (OWLDescription) obj, null); changes.add(changed); break; case SAMEAS: OWLSameIndividualsAxiom same = ontology.getOWLDataFactory().getOWLSameIndividualsAxiom((Set) subj); changed = new RemoveIndividualAxiom(ontology, same, null); changes.add(changed); break; case DIFFFROM: OWLDifferentIndividualsAxiom diff = ontology.getOWLDataFactory().getOWLDifferentIndividualsAxiom((Set) subj); changed = new RemoveIndividualAxiom(ontology, diff, null); changes.add(changed); break; } } public void addChange(Object subj, Object obj, int relType) throws OWLException { OntologyChange changed = null; switch (relType) { case ANNOTATION: changed = new AddAnnotationInstance(ontology, (OWLEntity) subj, ((OWLAnnotationInstance) obj).getProperty(), ((OWLAnnotationInstance) obj).getContent(), null); changes.add(changed); break; case SUPERCLASS: changed = new AddSuperClass(ontology, (OWLClass) subj, (OWLDescription) obj, null); changes.add(changed); break; case EQUIVALENTCLASS: changed = new AddEquivalentClass(ontology, (OWLClass) subj, (OWLDescription) obj, null); changes.add(changed); break; case ENUMERATION: changed = new AddEnumeration(ontology, (OWLClass) subj, (OWLEnumeration) obj, null); changes.add(changed); break; case SUBCLASSAXIOM: OWLSubClassAxiom axiom = ontology.getOWLDataFactory().getOWLSubClassAxiom((OWLDescription) subj, (OWLDescription) obj); changed = new AddClassAxiom(ontology, axiom, null); changes.add(changed); break; case EQUCLASSAXIOM: OWLEquivalentClassesAxiom equAxiom = ontology.getOWLDataFactory().getOWLEquivalentClassesAxiom((Set) subj); changed = new AddClassAxiom(ontology, equAxiom, null); changes.add(changed); break; case DISJOINTAXIOM: OWLDisjointClassesAxiom disAxiom = ontology.getOWLDataFactory().getOWLDisjointClassesAxiom((Set) subj); changed = new AddClassAxiom(ontology, disAxiom, null); changes.add(changed); break; case SUPERPROPERTY: changed = new AddSuperProperty(ontology, (OWLProperty) subj, (OWLProperty) obj, null); changes.add(changed); break; case EQUPROPAXIOM: OWLEquivalentPropertiesAxiom equPAxiom = ontology.getOWLDataFactory().getOWLEquivalentPropertiesAxiom((Set) subj); changed = new AddPropertyAxiom(ontology, equPAxiom, null); changes.add(changed); break; case DOMAIN: changed = new AddDomain(ontology, (OWLProperty) subj, (OWLDescription) obj, null); changes.add(changed); break; case RANGE: changed = new AddObjectPropertyRange(ontology, (OWLObjectProperty) subj, (OWLDescription) obj, null); changes.add(changed); break; case INVERSE: changed = new AddInverse(ontology, (OWLObjectProperty) subj, (OWLObjectProperty) obj, null); changes.add(changed); break; case INDIVIDUALCLASS: changed = new AddIndividualClass(ontology, (OWLIndividual) subj, (OWLDescription) obj, null); changes.add(changed); break; case SAMEAS: OWLSameIndividualsAxiom same = ontology.getOWLDataFactory().getOWLSameIndividualsAxiom((Set) subj); changed = new AddIndividualAxiom(ontology, same, null); changes.add(changed); break; case DIFFFROM: OWLDifferentIndividualsAxiom diff = ontology.getOWLDataFactory().getOWLDifferentIndividualsAxiom((Set) subj); changed = new AddIndividualAxiom(ontology, diff, null); changes.add(changed); break; } } private OWLRestriction replaceRestriction(OWLRestriction res, OWLProperty prop) { try { OWLDataFactory df = res.getOWLDataFactory(); // object prop restrictions if (res instanceof OWLObjectAllRestriction) { return df.getOWLObjectAllRestriction((OWLObjectProperty) prop, ((OWLObjectAllRestriction) res).getDescription()); } else if (res instanceof OWLObjectSomeRestriction) { return df.getOWLObjectSomeRestriction((OWLObjectProperty) prop, ((OWLObjectSomeRestriction) res).getDescription()); } else if (res instanceof OWLObjectValueRestriction) { return df.getOWLObjectValueRestriction((OWLObjectProperty) prop, ((OWLObjectValueRestriction) res).getIndividual()); } else if (res instanceof OWLObjectCardinalityRestriction) { return df.getOWLObjectCardinalityRestriction((OWLObjectProperty) prop, ((OWLObjectCardinalityRestriction) res).getAtLeast(), ((OWLObjectCardinalityRestriction) res).getAtMost()); } // dataprop restrictions else if (res instanceof OWLDataAllRestriction) { return df.getOWLDataAllRestriction((OWLDataProperty) prop, ((OWLDataAllRestriction) res).getDataType()); } else if (res instanceof OWLDataSomeRestriction) { return df.getOWLDataSomeRestriction((OWLDataProperty) prop, ((OWLDataSomeRestriction) res).getDataType()); } else if (res instanceof OWLDataValueRestriction) { return df.getOWLDataValueRestriction((OWLDataProperty) prop, ((OWLDataValueRestriction) res).getValue()); } else if (res instanceof OWLDataCardinalityRestriction) { return df.getOWLDataCardinalityRestriction((OWLDataProperty) prop, ((OWLDataCardinalityRestriction) res).getAtLeast(), ((OWLDataCardinalityRestriction) res).getAtMost()); } } catch (Exception ex) { ex.printStackTrace(); } return null; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?