Skip to content

Package: IdentifierGenerator

IdentifierGenerator

nameinstructionbranchcomplexitylinemethod
IdentifierGenerator(StorageConnector)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
generateIdentifier(URI)
M: 0 C: 38
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 10
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2020 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.jena.util;
16:
17: import cz.cvut.kbss.ontodriver.exception.IdentifierGenerationException;
18: import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
19: import cz.cvut.kbss.ontodriver.util.IdentifierUtils;
20: import cz.cvut.kbss.ontodriver.util.Vocabulary;
21: import org.apache.jena.rdf.model.Property;
22: import org.apache.jena.rdf.model.RDFNode;
23: import org.apache.jena.rdf.model.ResourceFactory;
24:
25: import java.net.URI;
26:
27: public class IdentifierGenerator {
28:
29: private static final int GENERATOR_THRESHOLD = 64;
30:
31: private final StorageConnector storageConnector;
32:
33: public IdentifierGenerator(StorageConnector storageConnector) {
34: this.storageConnector = storageConnector;
35: }
36:
37: /**
38: * Generates a unique identifier based on the specified class URI.
39: *
40: * @param classUri Type URI, used as the identifier base
41: * @return Generated identifier
42: */
43: public URI generateIdentifier(URI classUri) {
44: int i = 0;
45: boolean exists;
46: final Property property = ResourceFactory.createProperty(Vocabulary.RDF_TYPE);
47: final RDFNode type = ResourceFactory.createResource(classUri.toString());
48: URI result;
49: do {
50: result = IdentifierUtils.generateIdentifier(classUri);
51: exists = storageConnector.contains(ResourceFactory.createResource(result.toString()), property, type, null);
52: i++;
53:• } while (exists && i < GENERATOR_THRESHOLD);
54:• if (i >= GENERATOR_THRESHOLD) {
55: throw new IdentifierGenerationException(
56: "Failed to generate a unique identifier in " + GENERATOR_THRESHOLD + " attempts.");
57: }
58: return result;
59: }
60: }