001package org.dllearner.configuration.spring.editors;
002
003import org.dllearner.utilities.OWLAPIUtils;
004import org.dllearner.utilities.owl.OWLAPIRenderers;
005import org.semanticweb.owlapi.model.IRI;
006import org.semanticweb.owlapi.model.OWLClassExpression;
007import uk.ac.manchester.cs.owl.owlapi.OWLClassImpl;
008
009import java.util.regex.Pattern;
010
011/**
012 * Basic Property Editor for OWL class expressions.  
013 * Doesn't have GUI support yet but we could add that later if we wanted.
014 * @author Lorenz Buehmann
015 *
016 */
017public class ClassExpressionPropertyEditor extends AbstractPropertyEditor<OWLClassExpression>{
018        
019        @Override
020        public String getAsText() {
021                return OWLAPIRenderers.toManchesterOWLSyntax(value);
022        }
023        
024
025        final static Pattern whitespace = Pattern.compile("\\s");
026        @Override
027        public void setAsText(String s) throws IllegalArgumentException {
028                if (!whitespace.matcher(s).find() && Pattern.compile(":").matcher(s).find()) {
029                        // it already is a full URI
030                        value = new OWLClassImpl(IRI.create(s));
031                } else {
032                        // quote IRIs
033                        s = s.replaceAll("(?<=^|\\s|\\()((?:([^:/?#\\s]*):)(?://([^/?#]*?))?([^?#]*?)(?:\\?([^#]*?))?(?:#(.*?))?)(?=\\)|\\s|$)", "<$1>");
034                        // Bad hack to allow unparsed Manchester expressions. You need to decode this IRI and use the Manchester Parser once you have the Ontology
035                        IRI iri = IRI.create(OWLAPIUtils.UNPARSED_OCE + s);
036                        value = new OWLClassImpl(iri);
037                }
038        }
039}