|
| 1 | +/* |
| 2 | + * ODK ROBOT Plugin |
| 3 | + * Copyright © 2025 Nico Matentzoglu |
| 4 | + * This Command was strongly inspired by https://github.com/owlcollab/owltools/blob/master/OWLTools-Runner/src/main/java/owltools/cli/CommandRunner.java |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.incenp.obofoundry.odk; |
| 21 | + |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.HashSet; |
| 24 | +import java.util.Set; |
| 25 | + |
| 26 | +import org.apache.commons.cli.CommandLine; |
| 27 | +import org.obolibrary.robot.CommandState; |
| 28 | +import org.semanticweb.owlapi.model.IRI; |
| 29 | +import org.semanticweb.owlapi.model.OWLAnnotation; |
| 30 | +import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom; |
| 31 | +import org.semanticweb.owlapi.model.OWLAnnotationProperty; |
| 32 | +import org.semanticweb.owlapi.model.OWLAxiom; |
| 33 | +import org.semanticweb.owlapi.model.OWLDataFactory; |
| 34 | +import org.semanticweb.owlapi.model.OWLEntity; |
| 35 | +import org.semanticweb.owlapi.model.OWLOntology; |
| 36 | +import org.semanticweb.owlapi.model.OWLOntologyManager; |
| 37 | +import org.slf4j.Logger; |
| 38 | +import org.slf4j.LoggerFactory; |
| 39 | + |
| 40 | +/** |
| 41 | + * A command to obsolete an entity and replace all its usages with a replacement |
| 42 | + * entity. |
| 43 | + * <p> |
| 44 | + * This command takes an entity to obsolete and a replacement entity. It will: |
| 45 | + * <ul> |
| 46 | + * <li>Replace all usages of the obsolete entity with the replacement entity |
| 47 | + * throughout the ontology |
| 48 | + * <li>Remove the label, comment, and definition from the obsolete entity |
| 49 | + * <li>Mark the obsolete entity as deprecated |
| 50 | + * <li>Add a new label prefixed with "obsolete" to the obsolete entity |
| 51 | + * <li>Add the original label of the obsolete entity as an exact synonym on the |
| 52 | + * replacement entity (with a dbxref annotation pointing to the obsolete entity) |
| 53 | + * <li>Add a "term replaced by" annotation on the obsolete entity pointing to the |
| 54 | + * replacement entity |
| 55 | + * </ul> |
| 56 | + */ |
| 57 | +public class ObsoleteReplaceCommand extends BasePlugin { |
| 58 | + |
| 59 | + private static final Logger logger = LoggerFactory.getLogger(ObsoleteReplaceCommand.class); |
| 60 | + |
| 61 | + public ObsoleteReplaceCommand() { |
| 62 | + super("obsolete-replace", "obsolete an entity and replace it with another", |
| 63 | + "robot obsolete-replace --obsolete TERM --replacement TERM"); |
| 64 | + |
| 65 | + options.addOption(null, "obsolete", true, "entity to obsolete (CURIE or IRI)"); |
| 66 | + options.addOption(null, "replacement", true, "replacement entity (CURIE or IRI)"); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void performOperation(CommandState state, CommandLine line) throws Exception { |
| 71 | + if (!line.hasOption("obsolete") || !line.hasOption("replacement")) { |
| 72 | + throw new IllegalArgumentException("Both --obsolete and --replacement options are required"); |
| 73 | + } |
| 74 | + |
| 75 | + IRI obsoleteIRI = getIRI(line.getOptionValue("obsolete"), "obsolete"); |
| 76 | + IRI replacementIRI = getIRI(line.getOptionValue("replacement"), "replacement"); |
| 77 | + |
| 78 | + OWLOntology ontology = state.getOntology(); |
| 79 | + OWLOntologyManager manager = ontology.getOWLOntologyManager(); |
| 80 | + OWLDataFactory factory = manager.getOWLDataFactory(); |
| 81 | + |
| 82 | + OWLEntity obsoleteEntity = getEntity(ontology, obsoleteIRI); |
| 83 | + if (obsoleteEntity == null) { |
| 84 | + throw new IllegalArgumentException("Entity not found: " + obsoleteIRI); |
| 85 | + } |
| 86 | + |
| 87 | + OWLEntity replacementEntity = getEntity(ontology, replacementIRI); |
| 88 | + if (replacementEntity == null) { |
| 89 | + throw new IllegalArgumentException("Replacement entity not found: " + replacementIRI); |
| 90 | + } |
| 91 | + |
| 92 | + String originalLabel = getLabel(ontology, obsoleteIRI); |
| 93 | + |
| 94 | + Set<OWLAxiom> axiomsToRemove = new HashSet<>(); |
| 95 | + for (OWLAnnotationAssertionAxiom axiom : ontology.getAnnotationAssertionAxioms(obsoleteIRI)) { |
| 96 | + IRI propertyIRI = axiom.getProperty().getIRI(); |
| 97 | + if (propertyIRI.equals(Constants.RDFS_LABEL) || propertyIRI.equals(Constants.RDFS_COMMENT) |
| 98 | + || propertyIRI.equals(Constants.IAO_DEFINITION)) { |
| 99 | + axiomsToRemove.add(axiom); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + logger.info("Removing {} annotation axioms from obsolete entity", axiomsToRemove.size()); |
| 104 | + manager.removeAxioms(ontology, axiomsToRemove); |
| 105 | + |
| 106 | + replaceEntity(ontology, obsoleteEntity, replacementEntity); |
| 107 | + |
| 108 | + Set<OWLAxiom> axiomsToAdd = new HashSet<>(); |
| 109 | + axiomsToAdd.add(factory.getOWLDeclarationAxiom(obsoleteEntity)); |
| 110 | + axiomsToAdd.add(factory.getOWLAnnotationAssertionAxiom(factory.getOWLDeprecated(), obsoleteIRI, |
| 111 | + factory.getOWLLiteral(true))); |
| 112 | + |
| 113 | + String newLabel = "obsolete " + (originalLabel != null ? originalLabel : getLocalName(obsoleteIRI)); |
| 114 | + axiomsToAdd.add(factory.getOWLAnnotationAssertionAxiom(factory.getRDFSLabel(), obsoleteIRI, |
| 115 | + factory.getOWLLiteral(newLabel))); |
| 116 | + |
| 117 | + if (originalLabel != null) { |
| 118 | + OWLAnnotationProperty hasExactSynonym = factory.getOWLAnnotationProperty(Constants.HAS_EXACT_SYNONYM); |
| 119 | + OWLAnnotationProperty hasDbXref = factory.getOWLAnnotationProperty(Constants.HAS_DBXREF); |
| 120 | + OWLAnnotation dbxrefAnnotation = factory.getOWLAnnotation(hasDbXref, |
| 121 | + factory.getOWLLiteral(getShortForm(obsoleteIRI))); |
| 122 | + Set<OWLAnnotation> synonymAnnotations = Collections.singleton(dbxrefAnnotation); |
| 123 | + axiomsToAdd.add(factory.getOWLAnnotationAssertionAxiom(hasExactSynonym, replacementIRI, |
| 124 | + factory.getOWLLiteral(originalLabel), synonymAnnotations)); |
| 125 | + } |
| 126 | + |
| 127 | + OWLAnnotationProperty termReplacedBy = factory.getOWLAnnotationProperty(Constants.TERM_REPLACED_BY); |
| 128 | + axiomsToAdd.add(factory.getOWLAnnotationAssertionAxiom(termReplacedBy, obsoleteIRI, |
| 129 | + factory.getOWLLiteral(getShortForm(replacementIRI)))); |
| 130 | + |
| 131 | + logger.info("Adding {} new axioms to obsolete entity and replacement", axiomsToAdd.size()); |
| 132 | + manager.addAxioms(ontology, axiomsToAdd); |
| 133 | + } |
| 134 | + |
| 135 | + private OWLEntity getEntity(OWLOntology ontology, IRI iri) { |
| 136 | + for (OWLEntity entity : ontology.getSignature()) { |
| 137 | + if (entity.getIRI().equals(iri)) { |
| 138 | + return entity; |
| 139 | + } |
| 140 | + } |
| 141 | + return null; |
| 142 | + } |
| 143 | + |
| 144 | + private String getLabel(OWLOntology ontology, IRI entityIRI) { |
| 145 | + for (OWLAnnotationAssertionAxiom axiom : ontology.getAnnotationAssertionAxioms(entityIRI)) { |
| 146 | + if (axiom.getProperty().isLabel() && axiom.getValue().isLiteral()) { |
| 147 | + return axiom.getValue().asLiteral().get().getLiteral(); |
| 148 | + } |
| 149 | + } |
| 150 | + return null; |
| 151 | + } |
| 152 | + |
| 153 | + private void replaceEntity(OWLOntology ontology, OWLEntity obsolete, OWLEntity replacement) { |
| 154 | + OWLOntologyManager manager = ontology.getOWLOntologyManager(); |
| 155 | + OWLDataFactory factory = manager.getOWLDataFactory(); |
| 156 | + Set<OWLAxiom> axiomsToRemove = new HashSet<>(); |
| 157 | + Set<OWLAxiom> axiomsToAdd = new HashSet<>(); |
| 158 | + |
| 159 | + for (OWLAxiom axiom : ontology.getAxioms()) { |
| 160 | + if (axiom.getSignature().contains(obsolete)) { |
| 161 | + axiomsToRemove.add(axiom); |
| 162 | + OWLAxiom replacedAxiom = replaceInAxiom(axiom, factory, obsolete, replacement); |
| 163 | + axiomsToAdd.add(replacedAxiom); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + logger.info("Replacing {} axioms containing the obsolete entity", axiomsToRemove.size()); |
| 168 | + manager.removeAxioms(ontology, axiomsToRemove); |
| 169 | + manager.addAxioms(ontology, axiomsToAdd); |
| 170 | + } |
| 171 | + |
| 172 | + private OWLAxiom replaceInAxiom(OWLAxiom axiom, OWLDataFactory factory, OWLEntity obsolete, |
| 173 | + OWLEntity replacement) { |
| 174 | + EntityReplacementVisitor visitor = new EntityReplacementVisitor(factory, obsolete, replacement); |
| 175 | + axiom.accept(visitor); |
| 176 | + return visitor.duplicateObject(axiom); |
| 177 | + } |
| 178 | + |
| 179 | + private String getShortForm(IRI iri) { |
| 180 | + String iriString = iri.toString(); |
| 181 | + if (iriString.contains("#")) { |
| 182 | + return iriString.substring(iriString.lastIndexOf('#') + 1); |
| 183 | + } else if (iriString.contains("/")) { |
| 184 | + return iriString.substring(iriString.lastIndexOf('/') + 1); |
| 185 | + } |
| 186 | + return iriString; |
| 187 | + } |
| 188 | + |
| 189 | + private String getLocalName(IRI iri) { |
| 190 | + String shortForm = getShortForm(iri); |
| 191 | + if (shortForm.contains("_")) { |
| 192 | + return shortForm.replace('_', ':'); |
| 193 | + } |
| 194 | + return shortForm; |
| 195 | + } |
| 196 | +} |
0 commit comments