Commit 507d4afb authored by rwm's avatar rwm
Browse files

Separated values from labels for restriction enumerations.

parent b89780bd
......@@ -8,6 +8,8 @@ import java.util.regex.Pattern;
import javax.xml.namespace.QName;
import org.openrdf.model.Literal;
......@@ -21,33 +23,30 @@ import org.openrdf.model.vocabulary.XMLSchema;
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 {
QName type;
ConceptImpl concept;
Resource resource;
final QName type;
final ConceptImpl concept;
final Resource resource;
final Object[] enumValues;
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) ){
if( !rdf.hasStatement(resource, RDF.TYPE, OWL.RESTRICTION, false) ){
throw new OntologyException("Type owl:Restriction expected for "+restriction);
}
if( !rdf.hasStatement(restriction, OWL.ONPROPERTY, RDF.VALUE, false) ){
if( !rdf.hasStatement(resource, 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);
Value o = RDFUtils.getObject(rdf, resource, OWL.ALLVALUESFROM);
if( o != null ){
String localPart;
if( o.equals(XMLSchema.INTEGER) || o.equals(XMLSchema.INT) || o.equals(XMLSchema.LONG) ){
......@@ -66,8 +65,34 @@ public class RestrictionImpl implements ValueRestriction {
}
if( localPart != null ){
this.type = new QName(XMLSchema.NAMESPACE,localPart);
}else{
this.type = null;
// XXX allow restriction without type?
}
}else{
this.type = null;
}
// load enum values
o = RDFUtils.getObject(rdf, resource, OWL.ONEOF);
if( o != null ){
final List<String> list = new ArrayList<>();
RDFUtils.forEachRDFListItem(rdf, (Resource)o, v -> {
try {
Literal literal = RDFUtils.getLiteralObject(rdf, (Resource)v, RDF.VALUE);
// TODO: use correct types from literal
list.add(literal.getLabel());
} catch (Exception e) {
// TODO log error
}
});
enumValues = list.toArray(new String[list.size()]);
}else{
// no enumeration values
enumValues = null;
}
}
@Override
......@@ -76,7 +101,11 @@ public class RestrictionImpl implements ValueRestriction {
}
@Override
public EnumValue[] getEnumeration(Locale locale) throws OntologyException {
public String[] getEnumerationLabels(Locale locale) throws OntologyException {
if( enumValues == null ){
// restriction does not have enum values
return null;
}
// load enum
RepositoryConnection rdf = concept.getStore().getConnection();
Value o;
......@@ -85,22 +114,28 @@ public class RestrictionImpl implements ValueRestriction {
if( o == null )return null;
final String language = (locale==null)?null:locale.toString();
final List<EnumValue> list = new ArrayList<>();
final List<String> 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()));
list.add(label);
} catch (Exception e) {
// TODO log warning
}
// TODO log error
}
//Literal literal = RDFUtils.getLiteralObject(rdf, (Resource)v, RDF.VALUE);
// TODO: use correct types from literal
});
return list.toArray(new EnumValue[list.size()]);
return list.toArray(new String[list.size()]);
} catch (RepositoryException e) {
throw new OntologyException(e);
}
}
@Override
public Object[] getEnumerationValues() {
return enumValues;
}
@Override
public Number minInclusive() {
......@@ -133,4 +168,5 @@ public class RestrictionImpl implements ValueRestriction {
}
}
......@@ -10,7 +10,6 @@ import org.junit.Before;
import org.junit.Test;
import de.sekmi.histream.ontology.Concept;
import de.sekmi.histream.ontology.EnumValue;
import de.sekmi.histream.ontology.OntologyException;
import de.sekmi.histream.ontology.ValueRestriction;
import de.sekmi.histream.ontology.skos.Store;
......@@ -38,22 +37,26 @@ public class OntologyTest {
public void enumRestrictionTest() throws OntologyException{
Concept c = store.getConceptByNotation("T:Enum");
Assert.assertNotNull(c);
// German language
// Values
ValueRestriction rest = c.getValueRestriction();
Assert.assertNotNull(rest);
EnumValue[] e = rest.getEnumeration(Locale.GERMAN);
Assert.assertNotNull(e);
Assert.assertEquals(2, e.length);
Assert.assertEquals("1", e[0].getValue());
Assert.assertEquals("1_de", e[0].getPrefLabel());
Assert.assertEquals("2", e[1].getValue());
Assert.assertEquals("2_de", e[1].getPrefLabel());
Object[] values = rest.getEnumerationValues();
Assert.assertEquals("1", values[0]);
Assert.assertEquals("2", values[1]);
// German language
e = rest.getEnumeration(Locale.ENGLISH);
Assert.assertNotNull(e);
Assert.assertEquals(2, e.length);
Assert.assertEquals("1", e[0].getValue());
Assert.assertEquals("1_en", e[0].getPrefLabel());
String[] labels = rest.getEnumerationLabels(Locale.GERMAN);
Assert.assertNotNull(labels);
Assert.assertEquals(2, labels.length);
Assert.assertEquals("1_de", labels[0]);
Assert.assertEquals("2_de", labels[1]);
// English language
labels = rest.getEnumerationLabels(Locale.ENGLISH);
Assert.assertNotNull(labels);
Assert.assertEquals(2, labels.length);
Assert.assertEquals("1_en", labels[0]);
Assert.assertEquals("2_en", labels[1]);
}
......
......@@ -23,7 +23,6 @@ import javax.xml.stream.XMLStreamWriter;
import de.sekmi.histream.Plugin;
import de.sekmi.histream.ontology.Concept;
import de.sekmi.histream.ontology.EnumValue;
import de.sekmi.histream.ontology.Ontology;
import de.sekmi.histream.ontology.OntologyException;
import de.sekmi.histream.ontology.ValueRestriction;
......@@ -145,7 +144,7 @@ public class Import implements AutoCloseable{
xml.writeEndElement();
// TestID, TestName,
EnumValue[] ev = vr.getEnumeration(locale);
Object[] ev = vr.getEnumerationValues();
if( ev != null ){
xml.writeStartElement("DataType");
xml.writeCharacters("Enum");
......@@ -156,10 +155,20 @@ public class Import implements AutoCloseable{
xml.writeEndElement();
xml.writeStartElement("EnumValues");
for( EnumValue v : ev ){
// load enum values
String[] labels = vr.getEnumerationLabels(locale);
if( labels == null ){
// no labels for the specified language
// use values as labels
labels = new String[ev.length];
for( int i=0; i<labels.length; i++ ){
labels[i] = ev[i].toString();
}
}
for( int i=0; i<ev.length; i++ ){
xml.writeStartElement("Val");
xml.writeAttribute("description",v.getPrefLabel());
xml.writeCharacters(v.getValue().toString());
xml.writeAttribute("description",labels[i]);
xml.writeCharacters(ev[i].toString());
xml.writeEndElement();
}
xml.writeEndElement();
......
package de.sekmi.histream.ontology;
public class EnumValue {
private String label;
private Object value;
public EnumValue(String label, Object value){
this.label = label;
this.value = value;
}
public String getPrefLabel(){return label;}
public Object getValue(){return value;}
}
......@@ -7,13 +7,17 @@ import javax.xml.namespace.QName;
public interface ValueRestriction {
QName getType();
EnumValue[] getEnumeration(Locale locale) throws OntologyException;
Object[] getEnumerationValues();
String[] getEnumerationLabels(Locale locale) throws OntologyException;
Number minInclusive();
Number maxInclusive();
Integer minLength();
Integer maxLength();
Pattern getPattern();
// TODO implement validation (maybe necessary to separate enum values and enum labels)
//boolean validate(Value value);
public static final String XSD_NAMESPACE = "http://www.w3.org/2001/XMLSchema#";
public static final QName TYPE_DECIMAL = new QName(XSD_NAMESPACE,"decimal");
......
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