Commit 01f4aa4e authored by rwm's avatar rwm
Browse files

refactored ValueRestriction, XML Schema datatypes as QName

parent 162e788e
......@@ -33,6 +33,9 @@ public class ConceptImpl implements Concept {
Resource getResource(){
return res;
}
Store getStore(){
return store;
}
public String toString(){
return res.toString();
......@@ -81,7 +84,7 @@ public class ConceptImpl implements Concept {
}
@Override
public ValueRestriction getValueRestriction(Locale locale) throws OntologyException {
public ValueRestriction getValueRestriction() throws OntologyException {
try {
RepositoryResult<Statement> rs = store.getConnection().getStatements(getResource(), HIStreamOntology.DWH_RESTRICTION, null, false);
......@@ -94,7 +97,7 @@ public class ConceptImpl implements Concept {
if( !(obj instanceof Resource) ){
throw new OntologyException("dwh:restriction expected to be a rdf resource");
}
ValueRestriction ret = RestrictionImpl.loadFromRDF(store.getConnection(), (Resource)obj, locale);
ValueRestriction ret = new RestrictionImpl(this, (Resource)obj);
if( rs.hasNext() ){
throw new OntologyException("More than one dwh:restriction for "+res);
......
package de.sekmi.histream.ontology.skos;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.regex.Pattern;
import javax.xml.namespace.QName;
import org.openrdf.model.Literal;
import org.openrdf.model.Resource;
import org.openrdf.model.URI;
import org.openrdf.model.Value;
import org.openrdf.model.vocabulary.OWL;
import org.openrdf.model.vocabulary.RDF;
......@@ -18,23 +22,84 @@ import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;
import de.sekmi.histream.ontology.EnumValue;
import de.sekmi.histream.ontology.OntologyException;
import de.sekmi.histream.ontology.ValueRestriction;
public class RestrictionImpl implements ValueRestriction {
Class<?> type;
EnumValue[] enumValues;
QName type;
ConceptImpl concept;
Resource resource;
RestrictionImpl(ConceptImpl concept, Resource restriction) throws RepositoryException, OntologyException{
RepositoryConnection rdf = concept.getStore().getConnection();
this.concept = concept;
this.resource = restriction;
if( !rdf.hasStatement(restriction, RDF.TYPE, OWL.RESTRICTION, false) ){
throw new OntologyException("Type owl:Restriction expected for "+restriction);
}
if( !rdf.hasStatement(restriction, OWL.ONPROPERTY, RDF.VALUE, false) ){
throw new OntologyException("owl:Restriction only supported owl:onProperty rdf:value");
}
// load type
Value o = RDFUtils.getObject(rdf, restriction, OWL.ALLVALUESFROM);
if( o != null ){
String localPart;
if( o.equals(XMLSchema.INTEGER) || o.equals(XMLSchema.INT) || o.equals(XMLSchema.LONG) ){
// integer
localPart = ((URI)o).getLocalName();
}else if( o.equals(XMLSchema.POSITIVE_INTEGER) ){
// TODO positive integer
localPart = ((URI)o).getLocalName();
}else if( o.equals(XMLSchema.STRING) ){
localPart = ((URI)o).getLocalName();
}else if( o.equals(XMLSchema.DECIMAL) || o.equals(XMLSchema.FLOAT) || o.equals(XMLSchema.DOUBLE) ){
localPart = ((URI)o).getLocalName();
}else{
// TODO: check for extended / restricted types, e.g. positive float
localPart = null;
}
if( localPart != null ){
this.type = new QName(XMLSchema.NAMESPACE,localPart);
}
}
}
@Override
public Class<?> getType() {
public QName getType() {
return type;
}
@Override
public EnumValue[] getEnumeration() {
return enumValues;
public EnumValue[] getEnumeration(Locale locale) throws OntologyException {
// load enum
RepositoryConnection rdf = concept.getStore().getConnection();
Value o;
try {
o = RDFUtils.getObject(rdf, resource, OWL.ONEOF);
if( o == null )return null;
final String language = (locale==null)?null:locale.toString();
final List<EnumValue> list = new ArrayList<>();
RDFUtils.forEachRDFListItem(rdf, (Resource)o, v -> {
try {
String label = RDFUtils.getLocalString(rdf, (Resource)v, SKOS.PREF_LABEL, language);
Literal literal = RDFUtils.getLiteralObject(rdf, (Resource)v, RDF.VALUE);
// TODO: use correct types from literal
list.add(new EnumValue(label, literal.stringValue()));
} catch (Exception e) {
// TODO log warning
}
});
return list.toArray(new EnumValue[list.size()]);
} catch (RepositoryException e) {
throw new OntologyException(e);
}
}
@Override
......@@ -66,55 +131,6 @@ public class RestrictionImpl implements ValueRestriction {
// TODO Auto-generated method stub
return null;
}
static RestrictionImpl loadFromRDF(RepositoryConnection rdf, Resource restrictionObject, Locale locale) throws OntologyException{
RestrictionImpl ret = new RestrictionImpl();
// TODO: load restriction
try {
if( !rdf.hasStatement(restrictionObject, RDF.TYPE, OWL.RESTRICTION, false) ){
throw new OntologyException("Type owl:Restriction expected for "+restrictionObject);
}
if( !rdf.hasStatement(restrictionObject, OWL.ONPROPERTY, RDF.VALUE, false) ){
throw new OntologyException("owl:Restriction only supported owl:onProperty rdf:value");
}
// load enum
Value o = RDFUtils.getObject(rdf, restrictionObject, OWL.ONEOF);
if( o != null ){
final String language = (locale==null)?null:locale.toString();
final List<EnumValue> list = new ArrayList<>();
RDFUtils.forEachRDFListItem(rdf, (Resource)o, v -> {
try {
String label = RDFUtils.getLocalString(rdf, (Resource)v, SKOS.PREF_LABEL, language);
Literal literal = RDFUtils.getLiteralObject(rdf, (Resource)v, RDF.VALUE);
// TODO: use correct types from literal
list.add(new EnumValue(label, literal.stringValue()));
} catch (Exception e) {
// TODO log warning
}
});
ret.enumValues = list.toArray(new EnumValue[list.size()]);
}
// load type
o = RDFUtils.getObject(rdf, restrictionObject, OWL.ALLVALUESFROM);
if( o != null ){
if( o.equals(XMLSchema.INTEGER) || o.equals(XMLSchema.INT) || o.equals(XMLSchema.LONG) ){
ret.type = Integer.class;
}else if( o.equals(XMLSchema.STRING) ){
ret.type = String.class;
}else if( o.equals(XMLSchema.DECIMAL) || o.equals(XMLSchema.FLOAT) || o.equals(XMLSchema.DOUBLE) ){
ret.type = BigDecimal.class;
}else{
// TODO: check for extended / restricted types
}
}
} catch (RepositoryException e) {
throw new OntologyException(e);
}
return ret;
}
}
......@@ -30,7 +30,7 @@ public class OntologyTest {
Assert.assertNotNull(c);
Assert.assertEquals(1, c.getIDs().length);
Assert.assertEquals("T:type:int", c.getIDs()[0]);
ValueRestriction rest = c.getValueRestriction(Locale.GERMAN);
ValueRestriction rest = c.getValueRestriction();
Assert.assertNotNull(rest);
}
......@@ -39,9 +39,9 @@ public class OntologyTest {
Concept c = store.getConceptByNotation("T:Enum");
Assert.assertNotNull(c);
// German language
ValueRestriction rest = c.getValueRestriction(Locale.GERMAN);
ValueRestriction rest = c.getValueRestriction();
Assert.assertNotNull(rest);
EnumValue[] e = rest.getEnumeration();
EnumValue[] e = rest.getEnumeration(Locale.GERMAN);
Assert.assertNotNull(e);
Assert.assertEquals(2, e.length);
Assert.assertEquals("1", e[0].getValue());
......@@ -49,8 +49,7 @@ public class OntologyTest {
Assert.assertEquals("2", e[1].getValue());
Assert.assertEquals("2_de", e[1].getPrefLabel());
// German language
rest = c.getValueRestriction(Locale.ENGLISH);
e = rest.getEnumeration();
e = rest.getEnumeration(Locale.ENGLISH);
Assert.assertNotNull(e);
Assert.assertEquals(2, e.length);
Assert.assertEquals("1", e[0].getValue());
......
......@@ -76,9 +76,8 @@ public interface Concept {
/**
* Get restrictions for the values of this concept.
* @param locale locale for labels (e.g. in enumerations)
* @return value restrictions or {@code null} if no restrictions are provided
* @throws OntologyException for ontology errors
*/
ValueRestriction getValueRestriction(Locale locale) throws OntologyException;
ValueRestriction getValueRestriction() throws OntologyException;
}
package de.sekmi.histream.ontology;
import java.util.Locale;
import java.util.regex.Pattern;
import javax.xml.namespace.QName;
public interface ValueRestriction {
Class<?> getType();
// TODO move locale to getEnumeration
EnumValue[] getEnumeration();
QName getType();
EnumValue[] getEnumeration(Locale locale) throws OntologyException;
Number minInclusive();
Number maxInclusive();
Integer minLength();
Integer maxLength();
Pattern getPattern();
public static final String XSD_NAMESPACE = "http://www.w3.org/2001/XMLSchema#";
public static final QName TYPE_DECIMAL = new QName(XSD_NAMESPACE,"decimal");
public static final QName TYPE_INTEGER = new QName(XSD_NAMESPACE,"integer");
public static final QName TYPE_STRING = new QName(XSD_NAMESPACE,"string");
public static final QName TYPE_FLOAT = new QName(XSD_NAMESPACE,"float");
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment