Package: IdentifierGenerator
IdentifierGenerator
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
IdentifierGenerator(OWLOntology) |
|
|
|
|
|
||||||||||||||||||||
generateIdentifier(URI) |
|
|
|
|
|
||||||||||||||||||||
isIdentifierUnique(URI) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
Coverage
1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.owlapi.util;
16:
17: import cz.cvut.kbss.ontodriver.exception.IdentifierGenerationException;
18: import org.semanticweb.owlapi.model.IRI;
19: import org.semanticweb.owlapi.model.OWLOntology;
20:
21: import java.net.URI;
22: import java.util.Random;
23:
24: public class IdentifierGenerator {
25:
26: private static final int GENERATION_THRESHOLD = 100;
27: private static final Random RANDOM = new Random();
28:
29: private final OWLOntology ontology;
30:
31: public IdentifierGenerator(OWLOntology ontology) {
32:• assert ontology != null;
33: this.ontology = ontology;
34: }
35:
36: /**
37: * Generates an identifier which is unique w.r.t. individuals in the known ontology.
38: *
39: * @param classUri URI of individual's class, used as base for the identifier
40: * @return Unique identifier
41: * @throws IdentifierGenerationException If unable to generate unique identifier
42: */
43: public URI generateIdentifier(URI classUri) {
44: boolean unique = false;
45: URI id = null;
46: int counter = 0;
47:• while (!unique && counter++ < GENERATION_THRESHOLD) {
48:• if (classUri.getFragment() != null) {
49: id = URI.create(classUri.toString() + "_instance" + RANDOM.nextInt());
50: } else {
51: String base = classUri.toString();
52:• if (base.endsWith("/")) {
53: id = URI.create(base + "_instance" + RANDOM.nextInt());
54: } else {
55: id = URI.create(base + "#instance" + RANDOM.nextInt());
56: }
57: }
58: unique = isIdentifierUnique(id);
59: }
60:• if (!unique) {
61: throw new IdentifierGenerationException("Unable to generate a unique identifier.");
62: }
63: return id;
64: }
65:
66: boolean isIdentifierUnique(URI uri) {
67:• return !ontology.containsIndividualInSignature(IRI.create(uri));
68: }
69: }