|
| 1 | +/* |
| 2 | + * ODK ROBOT Plugin |
| 3 | + * Copyright © 2025 Damien Goutte-Gattat |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.incenp.obofoundry.odk; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.HashSet; |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +import org.apache.commons.cli.CommandLine; |
| 26 | +import org.obolibrary.robot.CommandLineHelper; |
| 27 | +import org.obolibrary.robot.CommandState; |
| 28 | +import org.semanticweb.owlapi.model.AddImport; |
| 29 | +import org.semanticweb.owlapi.model.IRI; |
| 30 | +import org.semanticweb.owlapi.model.OWLDataFactory; |
| 31 | +import org.semanticweb.owlapi.model.OWLImportsDeclaration; |
| 32 | +import org.semanticweb.owlapi.model.OWLOntology; |
| 33 | +import org.semanticweb.owlapi.model.OWLOntologyChange; |
| 34 | +import org.semanticweb.owlapi.model.RemoveImport; |
| 35 | + |
| 36 | +/** |
| 37 | + * A command to edit import declarations within an ontology. |
| 38 | + */ |
| 39 | +public class ImportCommand extends BasePlugin { |
| 40 | + |
| 41 | + public ImportCommand() { |
| 42 | + super("import", "add/remove import declarations", "robot import [--add IMPORT|--remove IMPORT]"); |
| 43 | + |
| 44 | + options.addOption(null, "add", true, "inject an import declaration"); |
| 45 | + options.addOption(null, "remove", true, "remove an import declaration"); |
| 46 | + options.addOption(null, "exclusive", true, |
| 47 | + "if true, replace any existing declarations by the ones set by --add"); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void performOperation(CommandState state, CommandLine line) throws Exception { |
| 52 | + OWLOntology ontology = state.getOntology(); |
| 53 | + OWLDataFactory fac = ontology.getOWLOntologyManager().getOWLDataFactory(); |
| 54 | + ArrayList<OWLOntologyChange> changes = new ArrayList<>(); |
| 55 | + |
| 56 | + for ( String imp : CommandLineHelper.getOptionalValues(line, "add") ) { |
| 57 | + changes.add(new AddImport(ontology, fac.getOWLImportsDeclaration(IRI.create(imp)))); |
| 58 | + } |
| 59 | + for (String imp : CommandLineHelper.getOptionalValues(line, "remove")) { |
| 60 | + changes.add(new RemoveImport(ontology, fac.getOWLImportsDeclaration(IRI.create(imp)))); |
| 61 | + } |
| 62 | + |
| 63 | + if ( CommandLineHelper.getBooleanValue(line, "exclusive", false) ) { |
| 64 | + Set<String> added = new HashSet<>(CommandLineHelper.getOptionalValues(line, "add")); |
| 65 | + for ( OWLImportsDeclaration decl : ontology.getImportsDeclarations() ) { |
| 66 | + if ( !added.contains(decl.getIRI().toString()) ) { |
| 67 | + changes.add(new RemoveImport(ontology, decl)); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + ontology.getOWLOntologyManager().applyChanges(changes); |
| 73 | + } |
| 74 | +} |
0 commit comments