Package: IdentifierGenerator
IdentifierGenerator
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
IdentifierGenerator(OWLOntology) |
|
|
|
|
|
||||||||||||||||||||
generateIdentifier(URI) |
|
|
|
|
|
||||||||||||||||||||
isIdentifierUnique(URI) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
Coverage
1: /*
2: * JOPA
3: * Copyright (C) 2023 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library 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 GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.ontodriver.owlapi.util;
19:
20: import cz.cvut.kbss.ontodriver.exception.IdentifierGenerationException;
21: import cz.cvut.kbss.ontodriver.util.IdentifierUtils;
22: import org.semanticweb.owlapi.model.IRI;
23: import org.semanticweb.owlapi.model.OWLOntology;
24:
25: import java.net.URI;
26:
27: public class IdentifierGenerator {
28:
29: private static final int GENERATION_THRESHOLD = 100;
30:
31: private final OWLOntology ontology;
32:
33: public IdentifierGenerator(OWLOntology ontology) {
34:• assert ontology != null;
35: this.ontology = ontology;
36: }
37:
38: /**
39: * Generates an identifier which is unique w.r.t. individuals in the known ontology.
40: *
41: * @param classUri URI of individual's class, used as base for the identifier
42: * @return Unique identifier
43: * @throws IdentifierGenerationException If unable to generate unique identifier
44: */
45: public URI generateIdentifier(URI classUri) {
46: boolean unique = false;
47: URI id = null;
48: int counter = 0;
49:• while (!unique && counter++ < GENERATION_THRESHOLD) {
50: id = IdentifierUtils.generateIdentifier(classUri);
51: unique = isIdentifierUnique(id);
52: }
53:• if (!unique) {
54: throw new IdentifierGenerationException("Unable to generate a unique identifier.");
55: }
56: return id;
57: }
58:
59: private boolean isIdentifierUnique(URI uri) {
60:• return !ontology.containsIndividualInSignature(IRI.create(uri));
61: }
62: }