Skip to content

Commit 00833db

Browse files
committed
Add the odk:import command.
Add a new command to manipulate the import declarations within an ontology, allowing to add or remove declarations as needed.
1 parent a37fdd8 commit 00833db

File tree

5 files changed

+144
-2
lines changed

5 files changed

+144
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ The following commands are provided by the plugin:
1414
* `odk:subset`: to create ontology subsets;
1515
* `odk:check-align`: to check the alignment of an ontology against an
1616
upper-level ontology and/or arbitrary root terms;
17-
* `odk:normalize`: to “normalise” an ontology.
17+
* `odk:normalize`: to “normalise” an ontology,
18+
* `odk:import`: to add/remove import declarations.
1819

1920
Building
2021
--------
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
org.incenp.obofoundry.odk.NormalizeCommand
22
org.incenp.obofoundry.odk.SubsetCommand
33
org.incenp.obofoundry.odk.CheckAlignmentCommand
4+
org.incenp.obofoundry.odk.ImportCommand

src/site/markdown/import.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
Managing import declarations
2+
============================
3+
4+
The `odk:import` command allows to programmatically add or remove import
5+
declarations from an ontology.
6+
7+
It is primarily intended for internal use by the ODK itself (which uses
8+
it when updating an ODK repository).
9+
10+
Adding an import declaration
11+
----------------------------
12+
13+
Use the `--add <IRI>` option to add an import declaration. Repeat the
14+
option to add as many declarations as needed:
15+
16+
```
17+
robot odk:import -i myont-edit.owl \
18+
--add http://purl.obolibrary.org/obo/myont/imports/a_import.owl \
19+
--add http://purl.obolibrary.org/obo/myont/imports/b_import.owl \
20+
convert -f ofn -o myont-edit.owl
21+
```
22+
23+
The option will be ignored if the input file happens to already contain
24+
a declaration for the same import.
25+
26+
Removing an import declaration
27+
------------------------------
28+
29+
Conversely, use the `--remove <IRI>` option to remove an import
30+
declaration. Again, the option may be used repeatedly in the same
31+
command:
32+
33+
```
34+
robot odk:import -i myont-edit.obo \
35+
--remove http://purl.obolibrary.org/obo/myont/imports/a_import.owl \
36+
--remove http://purl.obolibrary.org/obo/myont/imports/b_import.owl \
37+
convert --check false -o myont-edit.obo
38+
```
39+
40+
Exclusive mode
41+
--------------
42+
43+
With the `--exclusive true` option, the command will also _remove_ all
44+
import declarations that do not correspond to an import IRI added with
45+
any `--add` option.
46+
47+
Use that option to ensure that an ontology contains import declarations
48+
for a fixed set of import IRIs _and only for those IRIs_.
49+
50+
For example, the following command:
51+
52+
```
53+
robot odk:import -i myont-edit.owl \
54+
--add http://purl.obolibrary.org/obo/myont/imports/a_import.owl \
55+
--add http://purl.obolibrary.org/obo/myont/imports/b_import.owl \
56+
--exclusive true \
57+
convert -f ofn -o myont-edit.owl
58+
```
59+
60+
will (1) _add_ the declarations for the `a_import.owl` and
61+
`b_import.owl` modules, and (2) _remove_ any other declaration.
62+
63+
Use that option without any `--add` option to forcefully remove _all_
64+
import declarations from the ontology.

src/site/markdown/index.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ Currently, the ODK ROBOT plugin provides the following commands:
1515
* [odk:check-align](alignment.html), to check the alignment of an
1616
ontology against an upper-level ontology and/or arbitrary root terms;
1717
* [odk:normalize](normalize.html), to perform various normalisation
18-
operations on an ontology.
18+
operations on an ontology;
19+
* [odk:import](import.html), to add or remove import declarations in an
20+
ontology.
1921

2022
Using with the ODK
2123
------------------

0 commit comments

Comments
 (0)