Skip to content

Commit a37fdd8

Browse files
committed
Add the CommandLineHelper whenever possible.
Refactor to use the helper methods from the CommandLineHelper class. In particular, use the `getTerms` method instead of having our own code to read a list of IRIs from a file.
1 parent 1758d14 commit a37fdd8

File tree

2 files changed

+13
-88
lines changed

2 files changed

+13
-88
lines changed

src/main/java/org/incenp/obofoundry/odk/BasePlugin.java

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@
1818

1919
package org.incenp.obofoundry.odk;
2020

21-
import java.io.BufferedReader;
22-
import java.io.FileReader;
23-
import java.io.IOException;
24-
import java.util.HashSet;
25-
import java.util.Set;
26-
2721
import org.apache.commons.cli.CommandLine;
2822
import org.apache.commons.cli.Options;
2923
import org.obolibrary.robot.Command;
@@ -138,54 +132,4 @@ public CommandState execute(CommandState state, String[] args) throws Exception
138132
protected IRI getIRI(String term, String field) {
139133
return CommandLineHelper.maybeCreateIRI(ioHelper, term, field);
140134
}
141-
142-
/**
143-
* Reads a file and gets its contents as a set, one entry per line, excluding
144-
* any blank line and lines starting with a '#' character.
145-
*
146-
* @param filename The name of the file to read.
147-
* @return The file's lines as a set of unique strings.
148-
* @throws IOException If any I/O error occurs when reading the file.
149-
*/
150-
protected Set<String> readFile(String filename) throws IOException {
151-
BufferedReader reader = new BufferedReader(new FileReader(filename));
152-
Set<String> lines = new HashSet<>();
153-
String line = null;
154-
while ( (line = reader.readLine()) != null ) {
155-
line = line.trim();
156-
if ( !line.isEmpty() && line.charAt(0) != '#' ) {
157-
lines.add(line);
158-
}
159-
}
160-
reader.close();
161-
162-
return lines;
163-
}
164-
165-
/**
166-
* Reads a file and gets its contents as a set of IRIs, assuming one IRI per
167-
* line, excluding any blank line and lines starting with a '#' character.
168-
* CURIEs that cannot be converted to a full-length IRI are silently ignored.
169-
*
170-
* @param filename The name of the file to read.
171-
* @return The set of IRIs contained in the file.
172-
* @throws IOException If any I/O error occurs when reading the file.
173-
*/
174-
protected Set<IRI> readFileAsIRIs(String filename) throws IOException {
175-
BufferedReader reader = new BufferedReader(new FileReader(filename));
176-
Set<IRI> terms = new HashSet<>();
177-
String line = null;
178-
while ( (line = reader.readLine()) != null ) {
179-
line = line.trim();
180-
if ( !line.isEmpty() && line.charAt(0) != '#' ) {
181-
IRI termIRI = ioHelper.createIRI(line);
182-
if ( termIRI != null ) {
183-
terms.add(termIRI);
184-
}
185-
}
186-
}
187-
reader.close();
188-
189-
return terms;
190-
}
191135
}

src/main/java/org/incenp/obofoundry/odk/SubsetCommand.java

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,14 @@ public void performOperation(CommandState state, CommandLine line) throws Except
9393
extractor.setFillGaps(CommandLineHelper.getBooleanValue(line, "fill-gaps", false));
9494
extractor.setExcludeDangling(CommandLineHelper.getBooleanValue(line, "no-dangling", true));
9595
extractor.includeImports(useImports);
96-
if ( line.hasOption("follow-property") ) {
97-
for ( String property : line.getOptionValues("follow-property") ) {
98-
extractor.followProperty(getIRI(property, "follow-property"));
99-
}
96+
for ( String property : CommandLineHelper.getOptionalValues(line, "follow-property") ) {
97+
extractor.followProperty(getIRI(property, "follow-property"));
10098
}
101-
if ( line.hasOption("follow-in") ) {
102-
for ( String prefix : line.getOptionValues("follow-in") ) {
103-
extractor.includePrefix(getIRI(prefix, "follow-in").toString());
104-
}
99+
for ( String prefix : CommandLineHelper.getOptionalValues(line, "follow-in") ) {
100+
extractor.includePrefix(getIRI(prefix, "follow-in").toString());
105101
}
106-
if ( line.hasOption("not-follow-in") ) {
107-
for ( String prefix : line.getOptionValues("not-follow-in") ) {
108-
extractor.excludePrefix(getIRI(prefix, "not-follow-in").toString());
109-
}
102+
for ( String prefix : CommandLineHelper.getOptionalValues(line, "not-follow-in") ) {
103+
extractor.excludePrefix(getIRI(prefix, "not-follow-in").toString());
110104
}
111105

112106
// Setting up the initial subset
@@ -136,30 +130,17 @@ public void performOperation(CommandState state, CommandLine line) throws Except
136130
}
137131

138132
// 2. From the name or IRI of a subset defined in the ontology
139-
if ( line.hasOption("subset") ) {
140-
for ( String subsetName : line.getOptionValues("subset") ) {
141-
IRI subsetIRI = ioHelper.createIRI(subsetName);
142-
if ( subsetIRI != null ) {
143-
addToSubset(subset, extractor.getSubset(subsetIRI), "Adding tagged class {}");
144-
} else {
145-
addToSubset(subset, extractor.getSubset(subsetName), "Adding tagged class {}");
146-
}
133+
for ( String subsetName : CommandLineHelper.getOptionalValues(line, "subset") ) {
134+
IRI subsetIRI = ioHelper.createIRI(subsetName);
135+
if ( subsetIRI != null ) {
136+
addToSubset(subset, extractor.getSubset(subsetIRI), "Adding tagged class {}");
137+
} else {
138+
addToSubset(subset, extractor.getSubset(subsetName), "Adding tagged class {}");
147139
}
148140
}
149141

150142
// 3. From an explicit list of terms
151-
Set<IRI> terms = new HashSet<>();
152-
if ( line.hasOption("term") ) {
153-
for ( String term : line.getOptionValues("term") ) {
154-
terms.add(getIRI(term, "term"));
155-
}
156-
}
157-
if ( line.hasOption("term-file") ) {
158-
for ( String termFile : line.getOptionValues("term-file") ) {
159-
terms.addAll(readFileAsIRIs(termFile));
160-
}
161-
}
162-
for ( IRI term : terms ) {
143+
for ( IRI term : CommandLineHelper.getTerms(ioHelper, line, true) ) {
163144
if ( ontology.containsClassInSignature(term, useImports ? Imports.INCLUDED : Imports.EXCLUDED) ) {
164145
subset.add(factory.getOWLClass(term));
165146
logger.debug("Adding selected class {}", term);

0 commit comments

Comments
 (0)