Skip to content

Commit

Permalink
[Enhancement #170] Allow disabling generation of owl:Thing entity cla…
Browse files Browse the repository at this point in the history
…ss in OWL2Java.
  • Loading branch information
ledsoft committed Jun 6, 2023
1 parent 537b42c commit dbf01ec
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 8 deletions.
1 change: 1 addition & 0 deletions jopa-owl2java/README.md
Expand Up @@ -22,5 +22,6 @@ Possible configuration parameters are:
| -pt | `string` | Type to use for `@Properties` value. Options are `String` and `Object` . |
| -doc | `true` | Whether to generate Javadoc using values of `rdfs:comment` axioms. |
| -ann | `true` | Whether to automatically generate annotation fields corresponding to `rdfs:label` and `dc:description` for all entity classes. |
| -thing | `true` | Whether to automatically generate an entity class corresponding to `owl:Thing`. |

OWL2Java CLI prints help for all the supported tasks, including explanation of all options.
Expand Up @@ -341,7 +341,9 @@ private void generateModelImpl(final OWLOntology ontology, final JCodeModel cm,
LOG.info("Generating model ...");
final PropertiesType propertiesType = configuration.getPropertiesType();

context.classes.add(ontology.getOWLOntologyManager().getOWLDataFactory().getOWLThing());
if (configuration.shouldGenerateThing()) {
context.classes.add(ontology.getOWLOntologyManager().getOWLDataFactory().getOWLThing());
}

for (final OWLClass clazz : context.classes) {
LOG.info(" Generating class '{}'.", clazz);
Expand All @@ -355,7 +357,9 @@ private void generateModelImpl(final OWLOntology ontology, final JCodeModel cm,
extendClass.set(true);
});

if (!extendClass.get()) {addCommonClassFields(cm, subj, propertiesType);}
if (!extendClass.get()) {
addCommonClassFields(cm, subj, propertiesType);
}
for (final org.semanticweb.owlapi.model.OWLObjectProperty prop : context.objectProperties) {
generateObjectProperty(ontology, cm, context, pkg, clazz, subj, prop);
}
Expand Down
Expand Up @@ -96,10 +96,14 @@ public enum Option {
GENERATE_JAVADOC_FROM_COMMENT("doc", "generate Javadoc from rdfs:comment annotations"),

/**
* Whether automatic generation of name ({@literal rdfs:label}) and description ({@literal dc:description}) fields
* should be disabled.
* Whether to automatically generate name ({@literal rdfs:label}) and description ({@literal dc:description}) fields.
*/
GENERATE_ANNOTATION_FIELDS("ann", "automatically generate rdfs:label and dc:description attributes for all entities");
GENERATE_ANNOTATION_FIELDS("ann", "automatically generate rdfs:label and dc:description attributes for all entities"),

/**
* Whether to automatically generate an entity class corresponding to {@literal owl:Thing}.
*/
GENERATE_THING("thing", "automatically generate an entity class corresponding to owl:Thing");

public final String arg;
final String description;
Expand Down
Expand Up @@ -69,7 +69,12 @@ public class Defaults {
/**
* @see Option#GENERATE_ANNOTATION_FIELDS
*/
public static final boolean GENERATE_LABEL_DESCRIPTION_FIELDS = true;
public static final boolean GENERATE_ANNOTATION_FIELDS = true;

/**
* @see Option#GENERATE_THING
*/
public static final boolean GENERATE_THING = true;

private Defaults() {
throw new AssertionError();
Expand Down
Expand Up @@ -36,6 +36,8 @@ public class TransformationConfiguration {

private final boolean generateAnnotationFields;

private final boolean generateThing;

private final CliParams cliParams;

private TransformationConfiguration(TransformationConfigurationBuilder builder) {
Expand All @@ -47,6 +49,7 @@ private TransformationConfiguration(TransformationConfigurationBuilder builder)
this.preferMultilingualStrings = builder.preferMultilingualStrings;
this.propertiesType = builder.propertiesType;
this.generateAnnotationFields = builder.generateAnnotationFields;
this.generateThing = builder.generateThing;
this.cliParams = CliParams.empty();
}

Expand All @@ -60,7 +63,8 @@ private TransformationConfiguration(CliParams cliParams) {
this.generateJavadoc = cliParams.is(Option.GENERATE_JAVADOC_FROM_COMMENT.arg, Defaults.GENERATE_JAVADOC_FROM_COMMENT);
this.preferMultilingualStrings = cliParams.is(Option.PREFER_MULTILINGUAL_STRINGS.arg, Defaults.PREFER_MULTILINGUAL_STRINGS);
this.propertiesType = PropertiesType.fromParam(cliParams.valueOf(Option.PROPERTIES_TYPE.arg));
this.generateAnnotationFields = cliParams.is(Option.GENERATE_ANNOTATION_FIELDS.arg, Defaults.GENERATE_LABEL_DESCRIPTION_FIELDS);
this.generateAnnotationFields = cliParams.is(Option.GENERATE_ANNOTATION_FIELDS.arg, Defaults.GENERATE_ANNOTATION_FIELDS);
this.generateThing = cliParams.is(Option.GENERATE_THING.arg, Defaults.GENERATE_THING);
}

public String getContext() {
Expand Down Expand Up @@ -99,6 +103,10 @@ public boolean shouldGenerateAnnotationFields() {
return generateAnnotationFields;
}

public boolean shouldGenerateThing() {
return generateThing;
}

public CliParams getCliParams() {
return cliParams;
}
Expand All @@ -119,7 +127,8 @@ public static class TransformationConfigurationBuilder {
private boolean owlapiIris = Defaults.WITH_IRIS;
private boolean generateJavadoc = Defaults.GENERATE_JAVADOC_FROM_COMMENT;
private boolean preferMultilingualStrings = Defaults.PREFER_MULTILINGUAL_STRINGS;
private boolean generateAnnotationFields = Defaults.GENERATE_LABEL_DESCRIPTION_FIELDS;
private boolean generateAnnotationFields = Defaults.GENERATE_ANNOTATION_FIELDS;
private boolean generateThing = Defaults.GENERATE_THING;

public TransformationConfigurationBuilder context(String context) {
this.context = context;
Expand Down Expand Up @@ -161,6 +170,11 @@ public TransformationConfigurationBuilder generateAnnotationFields(boolean gener
return this;
}

public TransformationConfigurationBuilder generateThing(boolean generateThing) {
this.generateThing = generateThing;
return this;
}

public TransformationConfiguration build() {
return new TransformationConfiguration(this);
}
Expand Down
Expand Up @@ -235,4 +235,15 @@ void generateModelDoesNotGenerateLabelAndDescriptionFieldsWhenDisabled() {
assertThat(fields, not(hasKey(Constants.LABEL_FIELD_NAME)));
assertThat(fields, not(hasKey(Constants.DESCRIPTION_FIELD_NAME)));
}

@Test
void generateModelDoesNotGenerateThingEntityClassWhenDisabled() {
this.sut = new JavaTransformer(TransformationConfiguration.builder().packageName("").generateThing(false).build());
final ContextDefinition context = new ContextDefinition();
context.parse();
final ObjectModel result = sut.generateModel(ontology, context);
final JDefinedClass resultClass =
result.getCodeModel()._getClass(Constants.MODEL_PACKAGE + Constants.PACKAGE_SEPARATOR + "Thing");
assertNull(resultClass);
}
}

0 comments on commit dbf01ec

Please sign in to comment.