Skip to content

Commit

Permalink
Merge pull request #250 from kbss-cvut/enhancement#249-configurable-t…
Browse files Browse the repository at this point in the history
…ypes-language

Enhancement#249 configurable types language
  • Loading branch information
ledsoft authored Oct 31, 2023
2 parents c3e29f4 + a95570e commit fb32cf4
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 40 deletions.
27 changes: 25 additions & 2 deletions src/main/java/cz/cvut/kbss/termit/config/ServiceConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import cz.cvut.kbss.termit.aspect.ChangeTrackingAspect;
import cz.cvut.kbss.termit.aspect.VocabularyContentModificationAspect;
import cz.cvut.kbss.termit.exception.ResourceNotFoundException;
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.impl.DefaultRedirectStrategy;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.tika.utils.StringUtils;
import org.aspectj.lang.Aspects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
Expand All @@ -37,6 +43,8 @@
@Configuration
public class ServiceConfig {

private static final Logger LOG = LoggerFactory.getLogger(ServiceConfig.class);

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
Expand Down Expand Up @@ -71,12 +79,27 @@ public LocalValidatorFactoryBean validatorFactoryBean() {
}

@Bean("termTypesLanguage")
public ClassPathResource termTypesLanguageFile() {
public Resource termTypesLanguageFile(cz.cvut.kbss.termit.util.Configuration config) {
if (!StringUtils.isBlank(config.getLanguage().getTypes().getSource())) {
return createFileSystemResource(config.getLanguage().getTypes().getSource(), "types");
}
return new ClassPathResource("languages/types.ttl");
}

private Resource createFileSystemResource(String path, String type) {
final FileSystemResource source = new FileSystemResource(path);
if (!source.exists()) {
throw new ResourceNotFoundException(type + " language file '" + path + "' not found.");
}
LOG.info("Will load term {} from '{}'.", type, path);
return source;
}

@Bean("termStatesLanguage")
public ClassPathResource termStatesLanguageFile() {
public Resource termStatesLanguageFile(cz.cvut.kbss.termit.util.Configuration config) {
if (!StringUtils.isBlank(config.getLanguage().getStates().getSource())) {
return createFileSystemResource(config.getLanguage().getStates().getSource(), "states");
}
return new ClassPathResource("languages/states.ttl");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import cz.cvut.kbss.termit.util.TypeAwareResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
import cz.cvut.kbss.termit.util.Utils;
import cz.cvut.kbss.termit.util.Vocabulary;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
import org.eclipse.rdf4j.model.vocabulary.RDF;
import org.eclipse.rdf4j.model.vocabulary.RDFS;
import org.eclipse.rdf4j.model.vocabulary.SKOS;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.rio.RDFParseException;
import org.eclipse.rdf4j.rio.Rio;
import org.eclipse.rdf4j.rio.UnsupportedRDFormatException;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import java.io.IOException;
Expand All @@ -33,11 +32,11 @@
@Service
public class TermStateLanguageService {

private final ClassPathResource termStatesLanguageTtl;
private final Resource termStatesLanguageTtl;

private List<RdfsResource> cache;

public TermStateLanguageService(@Qualifier("termStatesLanguage") ClassPathResource termStatesLanguageTtl) {
public TermStateLanguageService(@Qualifier("termStatesLanguage") Resource termStatesLanguageTtl) {
this.termStatesLanguageTtl = termStatesLanguageTtl;
}

Expand All @@ -60,18 +59,19 @@ private List<RdfsResource> loadTermStates() {
final Model model = Rio.parse(termStatesLanguageTtl.getInputStream(), RDFFormat.TURTLE);
return model.filter(null, RDF.TYPE, vf.createIRI(Vocabulary.s_c_stav_pojmu))
.stream().map(s -> {
final Resource state = s.getSubject();
final org.eclipse.rdf4j.model.Resource state = s.getSubject();
final RdfsResource res = new RdfsResource();
res.setUri(URI.create(state.stringValue()));
final Model statements = model.filter(state, null, null);
res.setTypes(statements.filter(state, RDF.TYPE, null).stream()
.map(ts -> ts.getObject().stringValue()).collect(Collectors.toSet()));
res.setLabel(Utils.resolveTranslations(state, RDFS.LABEL, statements));
res.setComment(Utils.resolveTranslations(state, RDFS.COMMENT, statements));
res.setLabel(Utils.resolveTranslations(state, SKOS.PREF_LABEL, statements));
res.setComment(Utils.resolveTranslations(state, SKOS.SCOPE_NOTE, statements));
return res;
}).collect(Collectors.toList());
} catch (IOException | RDFParseException | UnsupportedRDFormatException e) {
throw new LanguageRetrievalException("Unable to load term states language from file " + termStatesLanguageTtl.getPath(), e);
throw new LanguageRetrievalException(
"Unable to load term states language from file " + termStatesLanguageTtl.getFilename(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
Expand All @@ -45,12 +46,12 @@
@Service
public class UfoTermTypesService {

private final ClassPathResource languageTtlUrl;
private final Resource languageTtlUrl;

private List<Term> cache;

@Autowired
public UfoTermTypesService(@Qualifier("termTypesLanguage") ClassPathResource languageTtlUrl) {
public UfoTermTypesService(@Qualifier("termTypesLanguage") Resource languageTtlUrl) {
this.languageTtlUrl = languageTtlUrl;
}

Expand Down Expand Up @@ -81,16 +82,21 @@ private List<Term> loadTermTypes() {
final org.eclipse.rdf4j.model.Resource type = s.getSubject();
final Term term = new Term(URI.create(type.stringValue()));
final Model statements = model.filter(type, null, null);
term.setLabel(Utils.resolveTranslations(type, org.eclipse.rdf4j.model.vocabulary.RDFS.LABEL, statements));
term.setDescription(Utils.resolveTranslations(type, org.eclipse.rdf4j.model.vocabulary.RDFS.COMMENT, statements));
term.setSubTerms(statements.filter(type, SKOS.NARROWER, null).stream()
term.setLabel(Utils.resolveTranslations(type, SKOS.PREF_LABEL, statements));
term.setDescription(Utils.resolveTranslations(type, SKOS.SCOPE_NOTE, statements));
final Set<URI> subTerms = statements.filter(type, SKOS.NARROWER, null).stream()
.filter(st -> st.getObject().isIRI())
.map(st -> new TermInfo(URI.create(st.getObject().stringValue())))
.collect(Collectors.toSet()));
.map(st -> URI.create(st.getObject().stringValue()))
.collect(Collectors.toSet());
model.filter(null, SKOS.BROADER, type).stream()
.filter(st -> st.getSubject().isIRI())
.map(st -> URI.create(st.getSubject().stringValue()))
.forEach(subTerms::add);
term.setSubTerms(subTerms.stream().map(TermInfo::new).collect(Collectors.toSet()));
return term;
}).collect(Collectors.toList());
} catch (IOException | RDFParseException | UnsupportedRDFormatException e) {
throw new LanguageRetrievalException("Unable to load term types from file " + languageTtlUrl.getPath(), e);
throw new LanguageRetrievalException("Unable to load term types from file " + languageTtlUrl.getFilename(), e);
}
}
}
59 changes: 58 additions & 1 deletion src/main/java/cz/cvut/kbss/termit/util/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public class Configuration {
private ACL acl = new ACL();
private Mail mail = new Mail();
private Security security = new Security();
private Language language = new Language();

public String getUrl() {
return url;
Expand Down Expand Up @@ -219,6 +220,15 @@ public void setSecurity(Security security) {
this.security = security;
}

public Language getLanguage() {
return language;
}

public void setLanguage(Language language) {
this.language = language;
}


public static class Persistence {
/**
* OntoDriver class for the repository.
Expand Down Expand Up @@ -763,7 +773,7 @@ public enum ProviderType {

/**
* Claim in the authentication token provided by the OIDC service containing roles mapped to TermIt user roles.
*
* <p>
* Supports nested objects via dot notation.
*/
private String roleClaim = "realm_access.roles";
Expand All @@ -784,4 +794,51 @@ public void setRoleClaim(String roleClaim) {
this.roleClaim = roleClaim;
}
}

public static class Language {

/**
* Path to a file containing definition of the language of types terms can be classified with.
* <p>
* The file must be in Turtle format. The term definitions must use SKOS terminology for attributes (prefLabel,
* scopeNote and broader/narrower).
*/
private LanguageSource types = new LanguageSource();

/**
* Path to a file containing definition of the language of states terms can be in with. The file must be in
* Turtle format. The term definitions must use SKOS terminology for attributes (prefLabel, scopeNote and
* broader/narrower).
*/
private LanguageSource states = new LanguageSource();

public LanguageSource getTypes() {
return types;
}

public void setTypes(LanguageSource types) {
this.types = types;
}

public LanguageSource getStates() {
return states;
}

public void setStates(LanguageSource states) {
this.states = states;
}

public static class LanguageSource {

private String source;

public String getSource() {
return source;
}

public void setSource(String source) {
this.source = source;
}
}
}
}
12 changes: 6 additions & 6 deletions src/main/resources/languages/states.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
@prefix termit-pojem: <http://onto.fel.cvut.cz/ontologies/application/termit/pojem/> .

termit-pojem:navrhovaný-pojem a pdp:stav-pojmu , termit-pojem:úvodní-stav-pojmu ;
rdfs:label "Navrhovaný pojem"@cs , "Proposed term"@en ;
rdfs:comment "Pojem, který ještě nebyl schválen k publikaci."@cs , "A proposed term that has not been confirmed for publication, yet."@en .
skos:prefLabel "Navrhovaný pojem"@cs , "Proposed term"@en ;
skos:scopeNote "Pojem, který ještě nebyl schválen k publikaci."@cs , "A proposed term that has not been confirmed for publication, yet."@en .

termit-pojem:publikovaný-pojem a pdp:stav-pojmu ;
rdfs:label "Publikovaný pojem"@cs , "Published term"@en ;
rdfs:comment "Pojem, který byl publikován a používá se."@cs , "A published term that is in use."@en .
skos:prefLabel "Publikovaný pojem"@cs , "Published term"@en ;
skos:scopeNote "Pojem, který byl publikován a používá se."@cs , "A published term that is in use."@en .

termit-pojem:zrušený-pojem a pdp:stav-pojmu , termit-pojem:koncový-stav-pojmu ;
rdfs:label "Zrušený pojem"@cs , "Cancelled term"@en ;
rdfs:comment "Pojem, který byl zrušen a již by se neměl používat."@cs , "A term that has been cancelled and should not be used anymore."@en .
skos:prefLabel "Zrušený pojem"@cs , "Cancelled term"@en ;
skos:scopeNote "Pojem, který byl zrušen a již by se neměl používat."@cs , "A term that has been cancelled and should not be used anymore."@en .
25 changes: 12 additions & 13 deletions src/main/resources/languages/types.ttl
Original file line number Diff line number Diff line change
@@ -1,54 +1,53 @@
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix ufo: <http://onto.fel.cvut.cz/ontologies/ufo/> .

ufo:individual a skos:Concept ;
rdfs:label "Individuál"@cs,
skos:prefLabel "Individuál"@cs,
"Individual"@en ;
skos:narrower ufo:event,
ufo:intrinsic-trope,
ufo:object,
ufo:relator .

ufo:type a skos:Concept ;
rdfs:label "Typ"@cs,
skos:prefLabel "Typ"@cs,
"Type"@en ;
skos:narrower ufo:event-type,
ufo:intrinsic-trope-type,
ufo:object-type,
ufo:relator-type .

ufo:event a skos:Concept ;
rdfs:label "Událost"@cs,
skos:prefLabel "Událost"@cs,
"Event"@en ;
rdfs:comment "An event, perdurant in the ontological sense. Events do not change its properties over time."@en .
skos:scopeNote "An event, perdurant in the ontological sense. Events do not change its properties over time."@en .

ufo:event-type a skos:Concept ;
rdfs:label "Typ události"@cs,
skos:prefLabel "Typ události"@cs,
"Event Type"@en .

ufo:intrinsic-trope a skos:Concept ;
rdfs:label "Vlastnost"@cs,
skos:prefLabel "Vlastnost"@cs,
"Aspect"@en .

ufo:intrinsic-trope-type a skos:Concept ;
rdfs:label "Typ vlastnosti"@cs,
skos:prefLabel "Typ vlastnosti"@cs,
"Aspect Type"@en .

ufo:object a skos:Concept ;
rdfs:label "Objekt"@cs,
skos:prefLabel "Objekt"@cs,
"Object"@en ;
rdfs:comment "Object is any identifiable endurant entity existence of which is not directly dependent on an existence of another entity."@en .
skos:scopeNote "Object is any identifiable endurant entity existence of which is not directly dependent on an existence of another entity."@en .

ufo:object-type a skos:Concept ;
rdfs:label "Typ objektu"@cs,
skos:prefLabel "Typ objektu"@cs,
"Object Type"@en .

ufo:relator a skos:Concept ;
rdfs:label "Vztah"@cs,
skos:prefLabel "Vztah"@cs,
"Relator"@en .

ufo:relator-type a skos:Concept ;
rdfs:label "Typ vztahu"@cs,
skos:prefLabel "Typ vztahu"@cs,
"Relation"@en .
Loading

0 comments on commit fb32cf4

Please sign in to comment.