Skip to content

Package: JavaTransformer

JavaTransformer

nameinstructionbranchcomplexitylinemethod
JavaTransformer(TransformationConfiguration)
M: 16 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
_generateModel(OWLOntology, JCodeModel, ContextDefinition, String)
M: 97 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 16 C: 0
0%
M: 1 C: 0
0%
addField(String, JDefinedClass, JType)
M: 91 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
create(String, JCodeModel, OWLClass, OWLOntology, PropertiesType)
M: 171 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 30 C: 0
0%
M: 1 C: 0
0%
createVocabularyClass(JCodeModel)
M: 29 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
ensureCreated(String, JCodeModel, OWLClass, OWLOntology, PropertiesType)
M: 23 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
ensureUniqueIdentifier(String)
M: 21 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
generateAuthorshipDoc(JDocCommentable)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
generateClassJavadoc(OWLOntology, OWLEntity, JDocCommentable)
M: 17 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
generateDataProperty(OWLOntology, JCodeModel, ContextDefinition, OWLClass, JDefinedClass, OWLDataProperty)
M: 116 C: 0
0%
M: 9 C: 0
0%
M: 6 C: 0
0%
M: 21 C: 0
0%
M: 1 C: 0
0%
generateJavadoc(OWLOntology, OWLEntity, JDocCommentable)
M: 32 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
generateModel(OWLOntology, ContextDefinition)
M: 33 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
generateObjectProperty(OWLOntology, JCodeModel, ContextDefinition, String, OWLClass, JDefinedClass, OWLObjectProperty, PropertiesType)
M: 121 C: 0
0%
M: 11 C: 0
0%
M: 7 C: 0
0%
M: 23 C: 0
0%
M: 1 C: 0
0%
generateToStringMethod(JDefinedClass, JFieldVar, JFieldVar)
M: 51 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
generateVocabulary(OWLOntology, ContextDefinition)
M: 26 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
generateVocabulary(OWLOntology, JCodeModel, ContextDefinition)
M: 132 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 25 C: 0
0%
M: 1 C: 0
0%
isJavaClassNameAnnotation(OWLAnnotation)
M: 19 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
javaClassId(OWLOntology, OWLClass)
M: 25 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
lambda$_generateModel$0(IntegrityConstraint)
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$_generateModel$1(JDefinedClass, String, JCodeModel, OWLOntology, PropertiesType, IntegrityConstraint)
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
lambda$generateJavadoc$4(OWLAnnotation)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$generateJavadoc$5(OWLAnnotation)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$generateJavadoc$6(JDocCommentable, OWLLiteral)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$generateVocabulary$3(OWLOntology)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$javaClassId$7(OWLAnnotation)
M: 12 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
lambda$null$2(IRI)
M: 23 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
modelPackageName()
M: 23 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
resolveFieldPrefix(OWLEntity, Set)
M: 50 C: 0
0%
M: 14 C: 0
0%
M: 8 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
setParticipationConstraintCardinality(JAnnotationUse, ParticipationConstraint)
M: 20 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 207 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
toJavaNotation(String)
M: 40 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
validJavaID(String)
M: 37 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
validJavaIDForIRI(IRI)
M: 20 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%

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.jopa.owl2java;
16:
17: import com.sun.codemodel.*;
18: import cz.cvut.kbss.jopa.ic.api.AtomicSubClassConstraint;
19: import cz.cvut.kbss.jopa.ic.api.DataParticipationConstraint;
20: import cz.cvut.kbss.jopa.ic.api.ObjectParticipationConstraint;
21: import cz.cvut.kbss.jopa.model.annotations.OWLAnnotationProperty;
22: import cz.cvut.kbss.jopa.model.annotations.OWLDataProperty;
23: import cz.cvut.kbss.jopa.model.annotations.OWLObjectProperty;
24: import cz.cvut.kbss.jopa.model.annotations.Properties;
25: import cz.cvut.kbss.jopa.model.annotations.*;
26: import cz.cvut.kbss.jopa.owl2java.cli.Option;
27: import cz.cvut.kbss.jopa.owl2java.cli.PropertiesType;
28: import cz.cvut.kbss.jopa.owl2java.config.TransformationConfiguration;
29: import cz.cvut.kbss.jopa.owl2java.exception.OWL2JavaException;
30: import cz.cvut.kbss.jopa.owlapi.DatatypeTransformer;
31: import cz.cvut.kbss.jopa.vocabulary.DC;
32: import cz.cvut.kbss.jopa.vocabulary.RDFS;
33: import org.semanticweb.owlapi.model.OWLClass;
34: import org.semanticweb.owlapi.model.*;
35: import org.semanticweb.owlapi.search.EntitySearcher;
36: import org.slf4j.Logger;
37: import org.slf4j.LoggerFactory;
38:
39: import java.io.Serializable;
40: import java.text.Normalizer;
41: import java.util.*;
42:
43: import static cz.cvut.kbss.jopa.owl2java.Constants.*;
44:
45: public class JavaTransformer {
46:
47: private static final Logger LOG = LoggerFactory.getLogger(OWL2JavaTransformer.class);
48:
49: private static final String[] KEYWORDS = {"abstract",
50: "assert",
51: "boolean",
52: "break",
53: "byte",
54: "case",
55: "catch",
56: "char",
57: "class",
58: "const",
59: "continue",
60: "default",
61: "do",
62: "double",
63: "else",
64: "enum",
65: "extends",
66: "final",
67: "finally",
68: "float",
69: "for",
70: "goto",
71: "if",
72: "implements",
73: "import",
74: "instanceof",
75: "int",
76: "interface",
77: "long",
78: "native",
79: "new",
80: "package",
81: "private",
82: "protected",
83: "public",
84: "return",
85: "short",
86: "static",
87: "strictfp",
88: "super",
89: "switch",
90: "synchronized",
91: "this",
92: "throw",
93: "throws",
94: "transient",
95: "try",
96: "void",
97: "volatile",
98: "while"};
99:
100: private static final String PREFIX_STRING = "s_";
101: private static final String PREFIX_CLASS = "c_";
102: private static final String PREFIX_PROPERTY = "p_";
103: private static final String PREFIX_INDIVIDUAL = "i_";
104: private static final String PREFIX_DATATYPE = "d_";
105:
106: private JDefinedClass voc;
107: private Map<OWLEntity, JFieldRef> entities = new HashMap<>();
108:
109: private Map<OWLClass, JDefinedClass> classes = new HashMap<>();
110:
111: private final TransformationConfiguration configuration;
112:
113: JavaTransformer(TransformationConfiguration configuration) {
114: this.configuration = configuration;
115: }
116:
117: private static String validJavaIDForIRI(final IRI iri) {
118:• if (iri.getFragment() != null) {
119: return validJavaID(iri.getFragment());
120: } else {
121: int x = iri.toString().lastIndexOf("/");
122: return validJavaID(iri.toString().substring(x + 1));
123: }
124: }
125:
126: private static String validJavaID(final String s) {
127: String res = s.trim().replace("-", "_").replace("'", "_quote_").replace(".", "_dot_").replace(',', '_');
128: // Replace non-ASCII characters with ASCII ones
129: res = Normalizer.normalize(res, Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", "");
130:• if (Arrays.binarySearch(KEYWORDS, res) >= 0) {
131: res = "_" + res;
132: }
133: return res;
134: }
135:
136: private JFieldVar addField(final String name, final JDefinedClass cls,
137: final JType fieldType) {
138: String newName = name;
139:
140: int i = 0;
141:• while (cls.fields().containsKey(newName)) {
142: newName = name + "" + (++i);
143: }
144:
145: final JFieldVar fvId = cls.field(JMod.PROTECTED, fieldType, newName);
146: final String fieldName = fvId.name().substring(0, 1).toUpperCase() + fvId.name().substring(1);
147: final JMethod mSetId = cls.method(JMod.PUBLIC, void.class, "set" + fieldName);
148: final JVar v = mSetId.param(fieldType, fvId.name());
149: mSetId.body().assign(JExpr._this().ref(fvId), v);
150: final JMethod mGetId = cls.method(JMod.PUBLIC, fieldType, "get" + fieldName);
151: mGetId.body()._return(fvId);
152: return fvId;
153: }
154:
155: /**
156: * Generates an object model consisting of JOPA entity classes and a vocabulary file from the specified ontology and
157: * context definition.
158: *
159: * @param ontology Ontology from which the model is generated
160: * @param context Context information
161: * @return The generated object model
162: */
163: public ObjectModel generateModel(final OWLOntology ontology, final ContextDefinition context) {
164: try {
165: final JCodeModel cm = new JCodeModel();
166: voc = createVocabularyClass(cm);
167: generateVocabulary(ontology, cm, context);
168: _generateModel(ontology, cm, context, modelPackageName());
169: return new ObjectModel(cm);
170: } catch (JClassAlreadyExistsException e) {
171: throw new OWL2JavaException("Transformation FAILED.", e);
172: }
173: }
174:
175: private String modelPackageName() {
176: final String packageConfig = configuration.getPackageName();
177: final StringBuilder sb = new StringBuilder(packageConfig);
178:• if (!packageConfig.isEmpty()) {
179: sb.append(PACKAGE_SEPARATOR);
180: }
181: sb.append(MODEL_PACKAGE);
182: return sb.toString();
183: }
184:
185: private JDefinedClass createVocabularyClass(JCodeModel codeModel)
186: throws JClassAlreadyExistsException {
187: final String packageName = configuration.getPackageName();
188: final String className =
189:• packageName.isEmpty() ? VOCABULARY_CLASS : packageName + PACKAGE_SEPARATOR + VOCABULARY_CLASS;
190: final JDefinedClass cls = codeModel._class(className);
191: generateAuthorshipDoc(cls);
192: return cls;
193: }
194:
195: private void generateAuthorshipDoc(JDocCommentable javaElem) {
196: javaElem.javadoc().add("This class was generated by OWL2Java " + VERSION);
197: }
198:
199: /**
200: * Generates only vocabulary of the loaded ontology.
201: *
202: * @param ontology Ontology from which the vocabulary should be generated
203: * @param context Integrity constraints context, if null is supplied, the whole ontology is interpreted as
204: * integrity constraints.
205: * @return The generated object model
206: */
207: public ObjectModel generateVocabulary(final OWLOntology ontology, ContextDefinition context) {
208: try {
209: final JCodeModel cm = new JCodeModel();
210: this.voc = createVocabularyClass(cm);
211: generateVocabulary(ontology, cm, context);
212: return new ObjectModel(cm);
213: } catch (JClassAlreadyExistsException e) {
214: throw new OWL2JavaException("Vocabulary generation FAILED, because the Vocabulary class already exists.",
215: e);
216: }
217: }
218:
219: private void generateObjectProperty(final OWLOntology ontology,
220: final JCodeModel cm,
221: final ContextDefinition context,
222: final String pkg,
223: final OWLClass clazz,
224: final JDefinedClass subj,
225: final org.semanticweb.owlapi.model.OWLObjectProperty prop,
226: final PropertiesType propertiesType) {
227: final ClassObjectPropertyComputer comp = new ClassObjectPropertyComputer(
228: clazz,
229: prop,
230: context.set,
231: ontology
232: );
233:
234:• if (Card.NO != comp.getCard()) {
235: JClass filler = ensureCreated(pkg, cm, comp.getFiller(), ontology, propertiesType);
236: final String fieldName = validJavaIDForIRI(prop.getIRI());
237:
238:• switch (comp.getCard()) {
239: case MULTIPLE:
240: filler = cm.ref(java.util.Set.class).narrow(filler);
241: break;
242: case SIMPLELIST:
243: case LIST:
244: filler = cm.ref(java.util.List.class).narrow(filler);
245: break;
246: default:
247: break;
248: }
249:
250: final JFieldVar fv = addField(fieldName, subj, filler);
251: generateJavadoc(ontology, prop, fv);
252:
253:• if (comp.getCard().equals(Card.SIMPLELIST)) {
254: fv.annotate(Sequence.class).param("type", SequenceType.simple);
255: }
256:
257:
258: fv.annotate(OWLObjectProperty.class).param("iri", entities.get(prop));
259:
260: JAnnotationArrayMember use = null;
261:• for (ObjectParticipationConstraint ic : comp.getParticipationConstraints()) {
262:• if (use == null) {
263: use = fv.annotate(ParticipationConstraints.class).paramArray("value");
264: }
265: JAnnotationUse u = use.annotate(ParticipationConstraint.class)
266: .param("owlObjectIRI", entities.get(ic.getObject()));
267: setParticipationConstraintCardinality(u, ic);
268: }
269: }
270: }
271:
272: private void setParticipationConstraintCardinality(JAnnotationUse u,
273: cz.cvut.kbss.jopa.ic.api.ParticipationConstraint ic) {
274:• if (ic.getMin() != 0) {
275: u.param("min", ic.getMin());
276: }
277:• if (ic.getMin() != -1) {
278: u.param("max", ic.getMax());
279: }
280: }
281:
282: private void generateDataProperty(final OWLOntology ontology,
283: final JCodeModel cm,
284: final ContextDefinition context,
285: final OWLClass clazz,
286: final JDefinedClass subj,
287: final org.semanticweb.owlapi.model.OWLDataProperty prop) {
288: final ClassDataPropertyComputer comp = new ClassDataPropertyComputer(
289: clazz,
290: prop,
291: context.set,
292: ontology
293: );
294:
295:• if (Card.NO != comp.getCard()) {
296:
297: final JType obj = cm._ref(DatatypeTransformer.transformOWLType(comp.getFiller()));
298:
299: final String fieldName = validJavaIDForIRI(prop.getIRI());
300:
301: JFieldVar fv;
302:
303:• switch (comp.getCard()) {
304: case MULTIPLE:
305: fv = addField(fieldName, subj, cm.ref(java.util.Set.class).narrow(obj));
306: break;
307: case ONE:
308: fv = addField(fieldName, subj, obj);
309: break;
310: default:
311: throw new OWL2JavaException("Unsupported data property cardinality type " + comp.getCard());
312: }
313: generateJavadoc(ontology, prop, fv);
314:
315: fv.annotate(OWLDataProperty.class).param("iri", entities.get(prop));
316:
317: JAnnotationArrayMember use = null;
318:• for (DataParticipationConstraint ic : comp.getParticipationConstraints()) {
319:• if (use == null) {
320: use = fv.annotate(ParticipationConstraints.class).paramArray("value");
321: }
322:
323: JAnnotationUse u = use.annotate(ParticipationConstraint.class)
324: .param("owlObjectIRI", comp.getFiller().getIRI().toString());
325:
326: setParticipationConstraintCardinality(u, ic);
327: }
328: }
329: }
330:
331: private void _generateModel(final OWLOntology ontology, final JCodeModel cm,
332: final ContextDefinition context, final String pkg) {
333: LOG.info("Generating model ...");
334: final PropertiesType propertiesType = configuration.getPropertiesType();
335:
336: context.classes.add(ontology.getOWLOntologyManager().getOWLDataFactory().getOWLThing());
337:
338:• for (final OWLClass clazz : context.classes) {
339: LOG.info(" Generating class '{}'.", clazz);
340: final JDefinedClass subj = ensureCreated(pkg, cm, clazz, ontology, propertiesType);
341:
342: context.set.getClassIntegrityConstraints(clazz).stream()
343: .filter(ic -> ic instanceof AtomicSubClassConstraint).forEach(ic -> {
344: final AtomicSubClassConstraint icc = (AtomicSubClassConstraint) ic;
345: subj._extends(ensureCreated(pkg, cm, icc.getSupClass(), ontology,
346: propertiesType));
347: });
348:
349:• for (final org.semanticweb.owlapi.model.OWLObjectProperty prop : context.objectProperties) {
350: generateObjectProperty(ontology, cm, context, pkg, clazz, subj, prop, propertiesType);
351: }
352:
353:• for (org.semanticweb.owlapi.model.OWLDataProperty prop : context.dataProperties) {
354: generateDataProperty(ontology, cm, context, clazz, subj, prop);
355: }
356: }
357: }
358:
359: private void generateVocabulary(final OWLOntology o, final JCodeModel cm, ContextDefinition context) {
360: final Collection<OWLEntity> col = new LinkedHashSet<>();
361: col.add(o.getOWLOntologyManager().getOWLDataFactory().getOWLThing());
362: col.addAll(context.classes);
363: col.addAll(context.objectProperties);
364: col.addAll(context.dataProperties);
365: col.addAll(context.annotationProperties);
366: col.addAll(context.individuals);
367:
368: o.getOWLOntologyManager().ontologies().forEach(onto -> onto.getOntologyID().getOntologyIRI().ifPresent(iri -> {
369: final String fieldName = ensureUniqueIdentifier("ONTOLOGY_IRI_" + validJavaIDForIRI(iri));
370: voc.field(JMod.PUBLIC | JMod.STATIC | JMod.FINAL, String.class, fieldName, JExpr.lit(iri.toString()));
371: }));
372:
373: final Set<IRI> visitedProperties = new HashSet<>(col.size());
374:
375:• for (final OWLEntity c : col) {
376: final Optional<String> prefix = resolveFieldPrefix(c, visitedProperties);
377:• if (!prefix.isPresent()) {
378: continue;
379: }
380: final String sFieldName = ensureUniqueIdentifier(
381: PREFIX_STRING + prefix.get() + validJavaIDForIRI(c.getIRI()));
382:
383: final JFieldVar fv1 = voc.field(JMod.PUBLIC | JMod.STATIC
384: | JMod.FINAL, String.class, sFieldName, JExpr.lit(c.getIRI().toString()));
385:• if (configuration.shouldGenerateOwlapiIris()) {
386: voc.field(JMod.PUBLIC | JMod.STATIC | JMod.FINAL, IRI.class,
387: sFieldName.substring(PREFIX_STRING.length()),
388: cm.ref(IRI.class).staticInvoke("create").arg(fv1));
389: }
390: generateJavadoc(o, c, fv1);
391: entities.put(c, voc.staticRef(fv1));
392: }
393: }
394:
395: private static Optional<String> resolveFieldPrefix(OWLEntity c, Set<IRI> visitedProperties) {
396:• if (c.isOWLClass()) {
397: return Optional.of(PREFIX_CLASS);
398:• } else if (c.isOWLDatatype()) {
399: return Optional.of(PREFIX_DATATYPE);
400:• } else if (c.isOWLDataProperty() || c.isOWLObjectProperty() || c.isOWLAnnotationProperty()) {
401:• if (visitedProperties.contains(c.getIRI())) {
402: LOG.debug("Property with IRI {} already processed. Skipping.", c.getIRI());
403: return Optional.empty();
404: }
405: visitedProperties.add(c.getIRI());
406: return Optional.of(PREFIX_PROPERTY);
407:• } else if (c.isOWLNamedIndividual()) {
408: return Optional.of(PREFIX_INDIVIDUAL);
409: }
410: return Optional.of("");
411: }
412:
413: private String ensureUniqueIdentifier(String id) {
414: final StringBuilder sb = new StringBuilder(id);
415:• while (voc.fields().keySet().contains(sb.toString())) {
416: sb.append("_A");
417: }
418: return sb.toString();
419: }
420:
421: /**
422: * Generates Javadoc from rdfs:comment annotation (if present).
423: *
424: * @param ontology Ontology from which the model/vocabulary is being generated
425: * @param owlEntity Annotated entity
426: * @param javaElem Element to document with Javadoc
427: */
428: private boolean generateJavadoc(OWLOntology ontology, OWLEntity owlEntity, JDocCommentable javaElem) {
429:• if (!configuration.shouldGenerateJavadoc()) {
430: return false;
431: }
432: final Optional<OWLAnnotation> ann = EntitySearcher.getAnnotations(owlEntity, ontology)
433: .filter(a -> a.getProperty().isComment()).findFirst();
434: ann.flatMap(a -> a.getValue().asLiteral()).ifPresent(lit -> javaElem.javadoc().add(lit.getLiteral()));
435:• return ann.isPresent() && ann.get().getValue().isLiteral();
436: }
437:
438: private JDefinedClass create(final String pkg, final JCodeModel cm, final OWLClass clazz,
439: final OWLOntology ontology, final PropertiesType propertiesType) {
440: JDefinedClass cls;
441:
442: String name = pkg + PACKAGE_SEPARATOR + javaClassId(ontology, clazz);
443:
444: try {
445: cls = cm._class(name);
446:
447: cls.annotate(cz.cvut.kbss.jopa.model.annotations.OWLClass.class).param("iri", entities.get(clazz));
448: cls._implements(Serializable.class);
449:
450: generateClassJavadoc(ontology, clazz, cls);
451:
452: // @Id(generated = true) protected String id;
453: final JClass ftId = cm.ref(String.class);
454: final JFieldVar fvId = addField("id", cls, ftId);
455: JAnnotationUse a = fvId.annotate(Id.class);
456: a.param("generated", true);
457:
458: // RDFS label
459: final JClass ftLabel = cm.ref(String.class);
460: final JFieldVar fvLabel = addField("name", cls, ftLabel);
461: fvLabel.annotate(OWLAnnotationProperty.class).param("iri", cm.ref(RDFS.class).staticRef("LABEL"));
462:
463: // DC description
464: final JClass ftDescription = cm.ref(String.class);
465: final JFieldVar fvDescription = addField("description", cls, ftDescription);
466: fvDescription.annotate(OWLAnnotationProperty.class)
467: .param("iri", cm.ref(DC.Elements.class).staticRef("DESCRIPTION"));
468:
469: // @Types Set<String> types;
470: final JClass ftTypes = cm.ref(Set.class).narrow(String.class);
471: final JFieldVar fvTypes = addField("types", cls, ftTypes);
472: fvTypes.annotate(Types.class);
473:
474: // @Properties public final Map<String,Set<String>> properties;
475:• final Class propertiesTypeC = (propertiesType == PropertiesType.object ? Object.class : String.class);
476: final JClass ftProperties = cm.ref(Map.class)
477: .narrow(cm.ref(String.class), cm.ref(Set.class).narrow(propertiesTypeC));
478: final JFieldVar fvProperties = addField("properties", cls, ftProperties);
479: fvProperties.annotate(Properties.class);
480:
481: generateToStringMethod(cls, fvId, fvLabel);
482: } catch (JClassAlreadyExistsException e) {
483: LOG.trace("Class already exists. Using the existing version. {}", e.getMessage());
484: cls = cm._getClass(name);
485: }
486: return cls;
487: }
488:
489: private String javaClassId(OWLOntology ontology, OWLClass owlClass) {
490: final Optional<OWLAnnotation> res = EntitySearcher.getAnnotations(owlClass, ontology)
491:• .filter(a -> isJavaClassNameAnnotation(a) &&
492:• a.getValue().isLiteral()).findFirst();
493:• if (res.isPresent()) {
494: return res.get().getValue().asLiteral().get().getLiteral();
495: } else {
496: return toJavaNotation(validJavaIDForIRI(owlClass.getIRI()));
497: }
498: }
499:
500: private void generateClassJavadoc(OWLOntology ontology, OWLEntity owlEntity, JDocCommentable javaElem) {
501: final boolean generated = generateJavadoc(ontology, owlEntity, javaElem);
502:• if (generated) {
503: javaElem.javadoc().add("\n\n");
504: }
505: generateAuthorshipDoc(javaElem);
506: }
507:
508: private void generateToStringMethod(JDefinedClass cls, JFieldVar idField, JFieldVar labelField) {
509: final JMethod toString = cls.method(JMod.PUBLIC, String.class, "toString");
510: toString.annotate(Override.class);
511: final JBlock body = toString.body();
512: JExpression expression = JExpr.lit(cls.name() + " {");
513: expression = expression.plus(JExpr.ref(labelField.name()));
514: expression = expression.plus(JExpr.lit("<")).plus(JExpr.ref(idField.name())).plus(JExpr.lit(">"));
515: expression = expression.plus(JExpr.lit("}"));
516:
517: body._return(expression);
518: }
519:
520: private JDefinedClass ensureCreated(final String pkg, final JCodeModel cm, final OWLClass clazz,
521: final OWLOntology ontology, final PropertiesType propertiesType) {
522:• if (!classes.containsKey(clazz)) {
523: classes.put(clazz, create(pkg, cm, clazz, ontology, propertiesType));
524: }
525: return classes.get(clazz);
526: }
527:
528: private boolean isJavaClassNameAnnotation(OWLAnnotation a) {
529: final String classNameProperty = (String) configuration.getCliParams()
530: .valueOf(Option.JAVA_CLASSNAME_ANNOTATION.arg);
531:• return a.getProperty().getIRI()
532: .equals(IRI.create(classNameProperty != null ? classNameProperty : Constants.P_CLASS_NAME));
533: }
534:
535: /**
536: * Converts a class name to the Java camel case notation
537: *
538: * @param className Generated class name
539: * @return Converted class name
540: */
541: private static String toJavaNotation(String className) {
542: StringBuilder result = new StringBuilder();
543:• for (String w : className.split("_")) {
544:• if (!w.isEmpty()) {
545: result.append(w.substring(0, 1).toUpperCase()).append(w.substring(1));
546: }
547: }
548: return result.toString();
549: }
550: }