From: <lor...@us...> - 2013-03-20 12:08:44
|
Revision: 3914 http://sourceforge.net/p/dl-learner/code/3914 Author: lorenz_b Date: 2013-03-20 12:08:41 +0000 (Wed, 20 Mar 2013) Log Message: ----------- Started OWL axiom pattern detection. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/reasoning/SPARQLReasoner.java trunk/interfaces/src/main/java/org/dllearner/cli/Enrichment.java Added Paths: ----------- trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/ trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/AbstractRenderer.java trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/FullIRIEntityShortFromProvider.java trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/ManchesterOWLSyntaxEditorParser.java trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/ManchesterOWLSyntaxOWLObjectRendererImpl.java trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/ManchesterOWLSyntaxObjectRenderer.java trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomPatternFinder.java trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLAxiomRenamer.java trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLClassExpressionOrderingComparator.java trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/OWLClassExpressionRenamer.java Added: trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/AbstractRenderer.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/AbstractRenderer.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/AbstractRenderer.java 2013-03-20 12:08:41 UTC (rev 3914) @@ -0,0 +1,219 @@ +package org.dllearner.algorithms.pattern; + +/* + * This file is part of the OWL API. + * + * The contents of this file are subject to the LGPL License, Version 3.0. + * + * Copyright (C) 2011, The University of Manchester + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/. + * + * + * Alternatively, the contents of this file may be used under the terms of the Apache License, Version 2.0 + * in which case, the provisions of the Apache License Version 2.0 are applicable instead of those above. + * + * Copyright 2011, University of Manchester + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +import java.io.IOException; +import java.io.Writer; +import java.util.ArrayList; +import java.util.List; +import java.util.StringTokenizer; + +import org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax; +import org.semanticweb.owlapi.io.OWLRendererException; +import org.semanticweb.owlapi.io.OWLRendererIOException; +import org.semanticweb.owlapi.util.ShortFormProvider; + +/** + * Author: Matthew Horridge<br> + * The University Of Manchester<br> + * Bio-Health Informatics Group<br> + * Date: 25-Apr-2007<br><br> + */ +@SuppressWarnings("javadoc") +public class AbstractRenderer { + + private ShortFormProvider shortFormProvider; + + private int lastNewLinePos = -1; + + private int currentPos; + + private Writer writer; + + private List<Integer> tabs; + + private boolean useTabbing = true; + + private boolean useWrapping = true; + + public AbstractRenderer(Writer writer, ShortFormProvider shortFormProvider) { + this.writer = writer; + this.shortFormProvider = shortFormProvider; + tabs = new ArrayList<Integer>(); + pushTab(0); + } + + + public void setUseTabbing(boolean useTabbing) { + this.useTabbing = useTabbing; + } + + + public void setUseWrapping(boolean useWrapping) { + this.useWrapping = useWrapping; + } + + + public boolean isUseWrapping() { + return useWrapping; + } + + + public boolean isUseTabbing() { + return useTabbing; + } + + +// public void setShortFormProvider(ShortFormProvider shortFormProvider) { +// this.shortFormProvider = shortFormProvider; +// } + + + public void flush() throws OWLRendererException { + try { + writer.flush(); + } + catch (IOException e) { + throw new OWLRendererIOException(e); + } + } + + protected void pushTab(int size) { + tabs.add(0, size); + } + + protected void incrementTab(int increment) { + int base = 0; + if(!tabs.isEmpty()) { + base = tabs.get(0); + } + tabs.add(0, base + increment); + } + + protected void popTab() { + tabs.remove(0); + } + + protected void writeTab() { + int tab = tabs.get(0); + for(int i = 0; i < tab; i++) { + write(" "); + } + } + + protected int getIndent() { + return currentPos - lastNewLinePos - 2; + } + + + protected void write(String s) { + if(s == null) { + return; + } + int indexOfNewLine = s.indexOf('\n'); + if(indexOfNewLine != -1) { + lastNewLinePos = currentPos + indexOfNewLine; + } + currentPos += s.length(); + try { + writer.write(s); + } + catch (IOException e) { + e.printStackTrace(); + } + } + + protected void write(char ch) { + write(Character.toString(ch)); + } + + protected void write(String s, int lineLen) { + StringTokenizer tokenizer = new StringTokenizer(s, " \n\t-", true); + int currentLineLength = 0; + while(tokenizer.hasMoreTokens()) { + String curToken = tokenizer.nextToken(); + write(curToken); + if(curToken.equals("\n")) { + writeTab(); + } + currentLineLength += curToken.length(); + if(currentLineLength > lineLen && curToken.trim().length() != 0 && tokenizer.hasMoreTokens()) { + writeNewLine(); + currentLineLength = 0; + } + } + } + + protected void writeSpace() { + write(" "); + } + + protected void write(ManchesterOWLSyntax keyword) { + write(" ", keyword, keyword.isSectionKeyword() ? ": " : " "); + } + + protected void writeFrameKeyword(ManchesterOWLSyntax keyword) { + write("", keyword, ": "); + } + + protected void writeSectionKeyword(ManchesterOWLSyntax keyword) { + write(" ", keyword, ": "); + } + + protected void writeNewLine() { + write("\n"); + if (useTabbing) { + writeTab(); + } + } + + protected void write(String prefix, ManchesterOWLSyntax keyword, String suffix) { + write(prefix); + write(keyword.toString()); + write(suffix); + } + + protected ShortFormProvider getShortFormProvider() { + return shortFormProvider; + } + +} + Added: trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/FullIRIEntityShortFromProvider.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/FullIRIEntityShortFromProvider.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/FullIRIEntityShortFromProvider.java 2013-03-20 12:08:41 UTC (rev 3914) @@ -0,0 +1,17 @@ +package org.dllearner.algorithms.pattern; + +import org.semanticweb.owlapi.model.OWLEntity; +import org.semanticweb.owlapi.util.ShortFormProvider; + +public class FullIRIEntityShortFromProvider implements ShortFormProvider{ + + @Override + public void dispose() { + } + + @Override + public String getShortForm(OWLEntity entity) { + return entity.toStringID(); + } + +} Added: trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/ManchesterOWLSyntaxEditorParser.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/ManchesterOWLSyntaxEditorParser.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/pattern/ManchesterOWLSyntaxEditorParser.java 2013-03-20 12:08:41 UTC (rev 3914) @@ -0,0 +1,4129 @@ +package org.dllearner.algorithms.pattern; + +/* + * This file is part of the OWL API. + * + * The contents of this file are subject to the LGPL License, Version 3.0. + * + * Copyright (C) 2011, The University of Manchester + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/. + * + * + * Alternatively, the contents of this file may be used under the terms of the Apache License, Version 2.0 + * in which case, the provisions of the Apache License Version 2.0 are applicable instead of those above. + * + * Copyright 2011, University of Manchester + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntax; +import org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntaxOntologyFormat; +import org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntaxOntologyHeader; +import org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntaxTokenizer; +import org.coode.owlapi.manchesterowlsyntax.OntologyAxiomPair; +import org.semanticweb.owlapi.expression.OWLEntityChecker; +import org.semanticweb.owlapi.expression.OWLOntologyChecker; +import org.semanticweb.owlapi.expression.ParserException; +import org.semanticweb.owlapi.model.AddAxiom; +import org.semanticweb.owlapi.model.AddImport; +import org.semanticweb.owlapi.model.AddOntologyAnnotation; +import org.semanticweb.owlapi.model.IRI; +import org.semanticweb.owlapi.model.OWLAnnotation; +import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom; +import org.semanticweb.owlapi.model.OWLAnnotationProperty; +import org.semanticweb.owlapi.model.OWLAnnotationSubject; +import org.semanticweb.owlapi.model.OWLAnnotationValue; +import org.semanticweb.owlapi.model.OWLAxiom; +import org.semanticweb.owlapi.model.OWLClass; +import org.semanticweb.owlapi.model.OWLClassAxiom; +import org.semanticweb.owlapi.model.OWLClassExpression; +import org.semanticweb.owlapi.model.OWLDataFactory; +import org.semanticweb.owlapi.model.OWLDataProperty; +import org.semanticweb.owlapi.model.OWLDataPropertyCharacteristicAxiom; +import org.semanticweb.owlapi.model.OWLDataPropertyExpression; +import org.semanticweb.owlapi.model.OWLDataRange; +import org.semanticweb.owlapi.model.OWLDatatype; +import org.semanticweb.owlapi.model.OWLEntity; +import org.semanticweb.owlapi.model.OWLFacetRestriction; +import org.semanticweb.owlapi.model.OWLImportsDeclaration; +import org.semanticweb.owlapi.model.OWLIndividual; +import org.semanticweb.owlapi.model.OWLLiteral; +import org.semanticweb.owlapi.model.OWLNamedIndividual; +import org.semanticweb.owlapi.model.OWLObjectProperty; +import org.semanticweb.owlapi.model.OWLObjectPropertyAxiom; +import org.semanticweb.owlapi.model.OWLObjectPropertyCharacteristicAxiom; +import org.semanticweb.owlapi.model.OWLObjectPropertyExpression; +import org.semanticweb.owlapi.model.OWLOntology; +import org.semanticweb.owlapi.model.OWLOntologyChange; +import org.semanticweb.owlapi.model.OWLOntologyID; +import org.semanticweb.owlapi.model.OWLOntologyLoaderConfiguration; +import org.semanticweb.owlapi.model.OWLOntologyManager; +import org.semanticweb.owlapi.model.OWLPropertyAssertionAxiom; +import org.semanticweb.owlapi.model.OWLPropertyExpression; +import org.semanticweb.owlapi.model.OWLSubClassOfAxiom; +import org.semanticweb.owlapi.model.OWLSubPropertyChainOfAxiom; +import org.semanticweb.owlapi.model.SWRLAtom; +import org.semanticweb.owlapi.model.SWRLBuiltInAtom; +import org.semanticweb.owlapi.model.SWRLDArgument; +import org.semanticweb.owlapi.model.SWRLDifferentIndividualsAtom; +import org.semanticweb.owlapi.model.SWRLIArgument; +import org.semanticweb.owlapi.model.SWRLIndividualArgument; +import org.semanticweb.owlapi.model.SWRLLiteralArgument; +import org.semanticweb.owlapi.model.SWRLRule; +import org.semanticweb.owlapi.model.SWRLSameIndividualAtom; +import org.semanticweb.owlapi.model.SWRLVariable; +import org.semanticweb.owlapi.model.SetOntologyID; +import org.semanticweb.owlapi.model.UnloadableImportException; +import org.semanticweb.owlapi.util.DefaultPrefixManager; +import org.semanticweb.owlapi.util.NamespaceUtil; +import org.semanticweb.owlapi.vocab.DublinCoreVocabulary; +import org.semanticweb.owlapi.vocab.Namespaces; +import org.semanticweb.owlapi.vocab.OWL2Datatype; +import org.semanticweb.owlapi.vocab.OWLFacet; +import org.semanticweb.owlapi.vocab.OWLRDFVocabulary; +import org.semanticweb.owlapi.vocab.SWRLBuiltInsVocabulary; +import org.semanticweb.owlapi.vocab.XSDVocabulary; + + +/** + * Author: Matthew Horridge<br> The University Of Manchester<br> Bio-Health Informatics Group<br> Date: + * 10-Sep-2007<br><br> + * <p/> + * A parser for the Manchester OWL Syntax. All properties must be defined before they are used. For example, consider + * the restriction hasPart some Leg. The parser must know in advance whether or not hasPart is an object property or a + * data property so that Leg gets parsed correctly. In a tool, such as an editor, it is expected that hasPart will + * already exists as either a data property or an object property. If a complete ontology is being parsed, it is + * expected that hasPart will have been defined at the top of the file before it is used in any class expressions or + * property assertions (e.g. ObjectProperty: hasPart) + */ +@SuppressWarnings({"unused","javadoc"}) +public class ManchesterOWLSyntaxEditorParser { + + // This parser was built by hand! After struggling with terrible + // error messages produced by ANTLR (or JavaCC) I decides to construct + // this parser by hand. The error messages that this parser generates + // are specific to the Manchester OWL Syntax and are such that it should + // be easy to use this parser in tools such as editors. + + private OWLOntologyLoaderConfiguration configuration; + + protected OWLDataFactory dataFactory; + + private List<ManchesterOWLSyntaxTokenizer.Token> tokens; + + private int tokenIndex; + + private OWLEntityChecker owlEntityChecker; + + private OWLOntologyChecker owlOntologyChecker = new OWLOntologyChecker() { + public OWLOntology getOntology(String name) { + return null; + } + }; + + private String base; + + private Set<String> classNames; + + private Set<String> objectPropertyNames; + + private Set<String> dataPropertyNames; + + private Set<String> individualNames; + + private Set<String> dataTypeNames; + + private Set<String> annotationPropertyNames; + + private Map<String, SWRLBuiltInsVocabulary> ruleBuiltIns = new TreeMap<String, SWRLBuiltInsVocabulary>(); + + private DefaultPrefixManager pm = new DefaultPrefixManager(); + + public static final String AND = ManchesterOWLSyntax.AND.toString(); + + public static final String OR = ManchesterOWLSyntax.OR.toString(); + + public static final String INVERSE = ManchesterOWLSyntax.INVERSE.toString(); + + public static final String SOME = ManchesterOWLSyntax.SOME.toString(); + + public static final String SELF = ManchesterOWLSyntax.SELF.toString(); + + public static final String ONLY = ManchesterOWLSyntax.ONLY.toString(); + + public static final String VALUE = ManchesterOWLSyntax.VALUE.toString(); + + public static final String MIN = ManchesterOWLSyntax.MIN.toString(); + + public static final String MAX = ManchesterOWLSyntax.MAX.toString(); + + public static final String EXACTLY = ManchesterOWLSyntax.EXACTLY.toString(); + + public static final String ONLYSOME = ManchesterOWLSyntax.ONLYSOME.toString(); + + public static final String NOT = ManchesterOWLSyntax.NOT.toString(); + + public static final String CLASS = ManchesterOWLSyntax.CLASS.toString() + ":"; + + public static final String DATATYPE = ManchesterOWLSyntax.DATATYPE.toString() + ":"; + + public static final String SUB_CLASS_OF = ManchesterOWLSyntax.SUBCLASS_OF.toString() + ":"; + + public static final String SUPER_CLASS_OF = ManchesterOWLSyntax.SUPERCLASS_OF.toString() + ":"; + + public static final String INSTANCES = "Instances:"; + + public static final String EQUIVALENT_TO = ManchesterOWLSyntax.EQUIVALENT_TO.toString() + ":"; + + public static final String EQUIVALENT_CLASSES = ManchesterOWLSyntax.EQUIVALENT_CLASSES.toString() + ":"; + + public static final String EQUIVALENT_PROPERTIES = ManchesterOWLSyntax.EQUIVALENT_PROPERTIES.toString() + ":"; + + public static final String DISJOINT_WITH = ManchesterOWLSyntax.DISJOINT_WITH.toString() + ":"; + + public static final String DISJOINT_UNION_OF = ManchesterOWLSyntax.DISJOINT_UNION_OF.toString() + ":"; + + public static final String HAS_KEY = ManchesterOWLSyntax.HAS_KEY.toString() + ":"; + + public static final String DISJOINT_CLASSES = ManchesterOWLSyntax.DISJOINT_CLASSES.toString() + ":"; + + public static final String DISJOINT_PROPERTIES = ManchesterOWLSyntax.DISJOINT_PROPERTIES.toString() + ":"; + + public static final String OBJECT_PROPERTY = ManchesterOWLSyntax.OBJECT_PROPERTY.toString() + ":"; + + public static final String DATA_PROPERTY = ManchesterOWLSyntax.DATA_PROPERTY.toString() + ":"; + + public static final String ANNOTATION_PROPERTY = ManchesterOWLSyntax.ANNOTATION_PROPERTY.toString() + ":"; + + public static final String SUB_PROPERTY_OF = ManchesterOWLSyntax.SUB_PROPERTY_OF.toString() + ":"; + + public static final String SUPER_PROPERTY_OF = ManchesterOWLSyntax.SUPER_PROPERTY_OF.toString() + ":"; + + public static final String DOMAIN = ManchesterOWLSyntax.DOMAIN.toString() + ":"; + + public static final String RANGE = ManchesterOWLSyntax.RANGE.toString() + ":"; + + public static final String INVERSES = ManchesterOWLSyntax.INVERSES.toString() + ":"; + + public static final String CHARACTERISTICS = ManchesterOWLSyntax.CHARACTERISTICS.toString() + ":"; + + public static final String INDIVIDUAL = ManchesterOWLSyntax.INDIVIDUAL.toString() + ":"; + + public static final String INDIVIDUALS = ManchesterOWLSyntax.INDIVIDUALS.toString() + ":"; + + public static final String ANNOTATIONS = ManchesterOWLSyntax.ANNOTATIONS.toString() + ":"; + + public static final String TYPES = ManchesterOWLSyntax.TYPES.toString() + ":"; + + public static final String TYPE = ManchesterOWLSyntax.TYPE.toString() + ":"; + + public static final String FACTS = ManchesterOWLSyntax.FACTS.toString() + ":"; + + public static final String SAME_AS = ManchesterOWLSyntax.SAME_AS.toString() + ":"; + + public static final String SAME_INDIVIDUAL = ManchesterOWLSyntax.SAME_INDIVIDUAL.toString() + ":"; + + public static final String DIFFERENT_FROM = ManchesterOWLSyntax.DIFFERENT_FROM.toString() + ":"; + + public static final String DIFFERENT_INDIVIDUALS = ManchesterOWLSyntax.DIFFERENT_INDIVIDUALS.toString() + ":"; + + public static final String VALUE_PARTITION = "ValuePartition:"; + + public static final String ONTOLOGY = ManchesterOWLSyntax.ONTOLOGY.toString() + ":"; + + public static final String PREFIX = ManchesterOWLSyntax.PREFIX.toString() + ":"; + + public static final String IMPORT = ManchesterOWLSyntax.IMPORT.toString() + ":"; + + public static final String SUB_PROPERTY_CHAIN = ManchesterOWLSyntax.SUB_PROPERTY_CHAIN.toString() + ":"; + + public static final String FUNCTIONAL = ManchesterOWLSyntax.FUNCTIONAL.toString() + ":"; + + public static final String INVERSE_FUNCTIONAL = ManchesterOWLSyntax.INVERSE_FUNCTIONAL.toString() + ":"; + + public static final String SYMMETRIC = ManchesterOWLSyntax.SYMMETRIC.toString() + ":"; + + public static final String ANTI_SYMMETRIC = ManchesterOWLSyntax.ANTI_SYMMETRIC.toString() + ":"; + + public static final String ASYMMETRIC = ManchesterOWLSyntax.ASYMMETRIC.toString() + ":"; + + public static final String TRANSITIVE = ManchesterOWLSyntax.TRANSITIVE.toString() + ":"; + + public static final String REFLEXIVE = ManchesterOWLSyntax.REFLEXIVE.toString() + ":"; + + public static final String IRREFLEXIVE = ManchesterOWLSyntax.IRREFLEXIVE.toString() + ":"; + + public static final String INVERSE_OF = ManchesterOWLSyntax.INVERSE_OF + ":"; + + public static final String RULE = ManchesterOWLSyntax.RULE + ":"; + + private Set<String> potentialKeywords; + + private OWLOntology defaultOntology = null; + + private boolean allowEmptyFrameSections = false; + + + private Map<String, AnnotatedListItemParser<OWLDataProperty, ?>> dataPropertyFrameSections = new HashMap<String, AnnotatedListItemParser<OWLDataProperty, ?>>(); + + + public ManchesterOWLSyntaxEditorParser(OWLDataFactory dataFactory, String s) { + this(new OWLOntologyLoaderConfiguration(), dataFactory, s); + } + + public boolean isAllowEmptyFrameSections() { + return allowEmptyFrameSections; + } + + public void setAllowEmptyFrameSections(boolean allowEmptyFrameSections) { + this.allowEmptyFrameSections = allowEmptyFrameSections; + } + + public ManchesterOWLSyntaxEditorParser(OWLOntologyLoaderConfiguration configuration, OWLDataFactory dataFactory, String s) { + this.configuration = configuration; + this.dataFactory = dataFactory; + potentialKeywords = new HashSet<String>(); + + classNames = new HashSet<String>(); + objectPropertyNames = new HashSet<String>(); + dataPropertyNames = new HashSet<String>(); + individualNames = new HashSet<String>(); + dataTypeNames = new HashSet<String>(); + annotationPropertyNames = new HashSet<String>(); + pm.setPrefix("rdf:", Namespaces.RDF.toString()); + pm.setPrefix("rdfs:", Namespaces.RDFS.toString()); + pm.setPrefix("owl:", Namespaces.OWL.toString()); + pm.setPrefix("dc:", DublinCoreVocabulary.NAME_SPACE); + NamespaceUtil u = new NamespaceUtil(); + + initialiseClassFrameSections(); + initialiseObjectPropertyFrameSections(); + initialiseDataPropertyFrameSections(); + initialiseAnnotationPropertyFrameSections(); + initialiseIndividualFrameSections(); + + + for (XSDVocabulary v : XSDVocabulary.values()) { + dataTypeNames.add(v.getIRI().getFragment()); + dataTypeNames.add("xsd:" + v.getIRI().getFragment()); + } + dataTypeNames.add(OWLRDFVocabulary.RDF_XML_LITERAL.getIRI().getFragment()); + dataTypeNames.add("rdf:" + OWLRDFVocabulary.RDF_XML_LITERAL.getIRI().getFragment()); + + dataTypeNames.add(dataFactory.getTopDatatype().getIRI().getFragment()); + + for (IRI iri : OWLRDFVocabulary.BUILT_IN_ANNOTATION_PROPERTY_IRIS) { + String[] res = u.split(iri.toString(), null); + annotationPropertyNames.add(u.getPrefix(res[0]) + ":" + res[1]); + } + + base = "http://www.semanticweb.org#"; + owlEntityChecker = new DefaultEntityChecker(); + tokens = new ArrayList<ManchesterOWLSyntaxTokenizer.Token>(); + tokens.addAll(getTokenizer(s).tokenize()); + tokenIndex = 0; + + for (SWRLBuiltInsVocabulary v : SWRLBuiltInsVocabulary.values()) { + ruleBuiltIns.put(v.getShortName(), v); + ruleBuiltIns.put(v.getIRI().toQuotedString(), v); + } + } + + protected ManchesterOWLSyntaxTokenizer getTokenizer(String s) { + return new ManchesterOWLSyntaxTokenizer(s); + } + + public OWLDataFactory getDataFactory() { + return dataFactory; + } + + private Map<String, AnnotatedListItemParser<OWLClass, ?>> classFrameSections = new HashMap<String, AnnotatedListItemParser<OWLClass, ?>>(); + + public void initialiseClassFrameSections() { + initialiseSection(new EntityAnnotationsListItemParser<OWLClass>(), classFrameSections); + initialiseSection(new ClassSubClassOfListItemParser(), classFrameSections); + initialiseSection(new ClassEquivalentToListItemParser(), classFrameSections); + initialiseSection(new ClassDisjointWithListItemParser(), classFrameSections); + initialiseSection(new ClassHasKeyListItemParser(), classFrameSections); + // Extensions + initialiseSection(new ClassSuperClassOfListItemParser(), classFrameSections); + initialiseSection(new ClassDisjointClassesListItemParser(), classFrameSections); + initialiseSection(new ClassIndividualsListItemParser(), classFrameSections); + } + + private Map<String, AnnotatedListItemParser<OWLObjectProperty, ?>> objectPropertyFrameSections = new HashMap<String, AnnotatedListItemParser<OWLObjectProperty, ?>>(); + + private void initialiseObjectPropertyFrameSections() { + initialiseSection(new EntityAnnotationsListItemParser<OWLObjectProperty>(), objectPropertyFrameSections); + initialiseSection(new ObjectPropertySubPropertyOfListItemParser(), objectPropertyFrameSections); + initialiseSection(new ObjectPropertyEquivalentToListItemParser(), objectPropertyFrameSections); + initialiseSection(new ObjectPropertyDisjointWithListItemParser(), objectPropertyFrameSections); + initialiseSection(new ObjectPropertyDomainListItemParser(), objectPropertyFrameSections); + initialiseSection(new ObjectPropertyRangeListItemParser(), objectPropertyFrameSections); + initialiseSection(new ObjectPropertyInverseOfListItemParser(), objectPropertyFrameSections); + initialiseSection(new ObjectPropertyCharacteristicsItemParser(), objectPropertyFrameSections); + initialiseSection(new ObjectPropertySubPropertyChainListItemParser(), objectPropertyFrameSections); + // Extensions + initialiseSection(new ObjectPropertySuperPropertyOfListItemParser(), objectPropertyFrameSections); + } + + private void initialiseDataPropertyFrameSections() { + initialiseSection(new DataPropertySubPropertyOfListItemParser(), dataPropertyFrameSections); + initialiseSection(new DataPropertyEquivalentToListItemParser(), dataPropertyFrameSections); + initialiseSection(new DataPropertyDisjointWithListItemParser(), dataPropertyFrameSections); + initialiseSection(new DataPropertyDomainListItemParser(), dataPropertyFrameSections); + initialiseSection(new DataPropertyRangeListItemParser(), dataPropertyFrameSections); + initialiseSection(new DataPropertyCharacteristicsItemParser(), dataPropertyFrameSections); + initialiseSection(new EntityAnnotationsListItemParser<OWLDataProperty>(), dataPropertyFrameSections); + } + + + private Map<String, AnnotatedListItemParser<OWLAnnotationProperty, ?>> annotationPropertyFrameSections = new HashMap<String, AnnotatedListItemParser<OWLAnnotationProperty, ?>>(); + + private void initialiseAnnotationPropertyFrameSections() { + initialiseSection(new AnnotationPropertySubPropertyOfListItemParser(), annotationPropertyFrameSections); + initialiseSection(new AnnotationPropertyDomainListItemParser(), annotationPropertyFrameSections); + initialiseSection(new AnnotationPropertyRangeListItemParser(), annotationPropertyFrameSections); + initialiseSection(new EntityAnnotationsListItemParser<OWLAnnotationProperty>(), annotationPropertyFrameSections); + } + + private Map<String, AnnotatedListItemParser<OWLIndividual, ?>> individualFrameSections = new HashMap<String, AnnotatedListItemParser<OWLIndividual, ?>>(); + + public void initialiseIndividualFrameSections() { + initialiseSection(new IndividualAnnotationItemParser(), individualFrameSections); + initialiseSection(new IndividualTypesItemParser(), individualFrameSections); + initialiseSection(new IndividualFactsItemParser(), individualFrameSections); + initialiseSection(new IndividualSameAsItemParser(), individualFrameSections); + initialiseSection(new IndividualDifferentFromItemParser(), individualFrameSections); + // Extensions + initialiseSection(new IndividualDifferentIndividualsItemParser(), individualFrameSections); + } + + + + protected List<ManchesterOWLSyntaxTokenizer.Token> getTokens() { + return tokens; + } + + + protected void reset() { + tokenIndex = 0; + } + + + public String getBase() { + return base; + } + + + public void setBase(String base) { + this.base = base; + } + + + public OWLEntityChecker getOWLEntityChecker() { + return owlEntityChecker; + } + + + public void setOWLEntityChecker(OWLEntityChecker owlEntityChecker) { + this.owlEntityChecker = owlEntityChecker; + } + + + public boolean isOntologyName(String name) { + return owlOntologyChecker.getOntology(name) != null; + } + + + public boolean isClassName(String name) { + return classNames.contains(name) || owlEntityChecker != null && owlEntityChecker.getOWLClass(name) != null; + } + + + public OWLOntology getOntology(String name) { + return owlOntologyChecker.getOntology(name); + } + + + public void setOWLOntologyChecker(OWLOntologyChecker owlOntologyChecker) { + this.owlOntologyChecker = owlOntologyChecker; + } + + + public boolean isObjectPropertyName(String name) { + return objectPropertyNames.contains(name) || owlEntityChecker != null && owlEntityChecker.getOWLObjectProperty(name) != null; + } + + + public boolean isAnnotationPropertyName(String name) { + return annotationPropertyNames.contains(name) || owlEntityChecker != null && owlEntityChecker.getOWLAnnotationProperty(name) != null; + } + + + public boolean isDataPropertyName(String name) { + return dataPropertyNames.contains(name) || owlEntityChecker != null && owlEntityChecker.getOWLDataProperty(name) != null; + } + + + public boolean isIndividualName(String name) { + return individualNames.contains(name) || owlEntityChecker != null && owlEntityChecker.getOWLIndividual(name) != null; + } + + + public boolean isDatatypeName(String name) { + return dataTypeNames.contains(name) || owlEntityChecker != null && owlEntityChecker.getOWLDatatype(name) != null; + } + + + public boolean isSWRLBuiltin(String name) { + return ruleBuiltIns.containsKey(name); + } + + + public OWLClass getOWLClass(String name) { + OWLClass cls = owlEntityChecker.getOWLClass(name); + if (cls == null && classNames.contains(name)) { + cls = dataFactory.getOWLClass(getIRI(name)); + } + return cls; + } + + + public OWLObjectProperty getOWLObjectProperty(String name) { + OWLObjectProperty prop = owlEntityChecker.getOWLObjectProperty(name); + if (prop == null && objectPropertyNames.contains(name)) { + prop = dataFactory.getOWLObjectProperty(getIRI(name)); + } + return prop; + } + + + public OWLIndividual getOWLIndividual(String name) { + if (name.startsWith("_:")) { + return dataFactory.getOWLAnonymousIndividual(name); + } + return getOWLNamedIndividual(name); + } + + private OWLNamedIndividual getOWLNamedIndividual(String name) { + OWLNamedIndividual ind = owlEntityChecker.getOWLIndividual(name); + if (ind == null && individualNames.contains(name)) { + ind = dataFactory.getOWLNamedIndividual(getIRI(name)); + } + return ind; + } + + + public OWLDataProperty getOWLDataProperty(String name) { + OWLDataProperty prop = owlEntityChecker.getOWLDataProperty(name); + if (prop == null && dataPropertyNames.contains(name)) { + prop = dataFactory.getOWLDataProperty(getIRI(name)); + } + return prop; + } + + + public OWLDatatype getOWLDatatype(String name) { + OWLDatatype dt = owlEntityChecker.getOWLDatatype(name); + if (dt == null && dataTypeNames.contains(name)) { + dt = dataFactory.getOWLDatatype(getIRI(name)); + } + return dt; + } + + + public OWLAnnotationProperty getOWLAnnotationProperty(String name) { + OWLAnnotationProperty prop = owlEntityChecker.getOWLAnnotationProperty(name); + if (prop == null && annotationPropertyNames.contains(name)) { + prop = dataFactory.getOWLAnnotationProperty(getIRI(name)); + } + return prop; + } + + + protected ManchesterOWLSyntaxTokenizer.Token getLastToken() { + if (tokenIndex - 1 > -1) { + return tokens.get(tokenIndex - 1); +// return tokenIndex < tokens.size() ? tokens.get(tokenIndex) : tokens.get(tokens.size() - 1); + } + else { + return tokens.get(0); + } + } + + + protected String peekToken() { + return getToken().getToken(); + } + + //////////////////////////////////////////////////////////////////////////////////////////////////// + // + // Tokenizer + // + //////////////////////////////////////////////////////////////////////////////////////////////////// + + + protected String consumeToken() { + String token = getToken().getToken(); + if (tokenIndex < tokens.size() - 1) { + tokenIndex++; + } + + return token; + } + + + protected void consumeToken(String expected) throws ParserException { + String tok = consumeToken(); + if (!tok.equals(expected)) { + throw createException(expected); + } + } + + + public ManchesterOWLSyntaxTokenizer.Token getToken() { + return tokens.get((tokenIndex < tokens.size()) ? tokenIndex : tokenIndex - 1); + } + + + public int getTokenPos() { + return getToken().getPos(); + } + + + public int getTokenCol() { + return getToken().getCol(); + } + + + public int getTokenRow() { + return getToken().getRow(); + } + + //////////////////////////////////////////////////////////////////////////////////////////////////// + // + // Parser + // + //////////////////////////////////////////////////////////////////////////////////////////////////// + + + /** + * Parses an OWL class expression that is represented in Manchester OWL Syntax + * @return The parsed class expression + * @throws ParserException If a class expression could not be parsed. + */ + public OWLClassExpression parseClassExpression() throws ParserException { + OWLClassExpression desc = parseIntersection(); + if (!consumeToken().equals(ManchesterOWLSyntaxTokenizer.EOF)) { + throw createException(ManchesterOWLSyntaxTokenizer.EOF); + } + return desc; + } + + + public OWLClassExpression parseIntersection() throws ParserException { + Set<OWLClassExpression> ops = new HashSet<OWLClassExpression>(); + String kw = AND; + while (kw.equalsIgnoreCase(AND)) { + potentialKeywords.remove(AND); + ops.add(parseUnion()); + potentialKeywords.add(AND); + kw = peekToken(); + if (kw.equalsIgnoreCase(AND)) { + kw = consumeToken(); + } + else if (kw.equalsIgnoreCase("that")) { + consumeToken(); + kw = AND; + } + } + if (ops.size() == 1) { + return ops.iterator().next(); + } + else { + return dataFactory.getOWLObjectIntersectionOf(ops); + } + } + + + public OWLClassExpression parseUnion() throws ParserException { + Set<OWLClassExpression> ops = new HashSet<OWLClassExpression>(); + String kw = OR; + while (kw.equalsIgnoreCase(OR)) { + potentialKeywords.remove(OR); + ops.add(parseNonNaryClassExpression()); + potentialKeywords.add(OR); + kw = peekToken(); + if (kw.equalsIgnoreCase(OR)) { + kw = consumeToken(); + } + } + if (ops.size() == 1) { + return ops.iterator().next(); + } + else { + return dataFactory.getOWLObjectUnionOf(ops); + } + } + + + public OWLObjectPropertyExpression parseObjectPropertyExpression(boolean allowUndeclared) throws ParserException { + String tok = consumeToken(); + if (tok.equalsIgnoreCase(INVERSE)) { + String open = peekToken(); + boolean brackets = false; + if (open.equals("(")) { + consumeToken(); + brackets = true; + } + OWLObjectPropertyExpression prop = parseObjectPropertyExpression(); + if (brackets) { + String close = consumeToken(); + if (!close.equals(")")) { + throw createException(")"); + } + } + return dataFactory.getOWLObjectInverseOf(prop); + } + else { + if (!allowUndeclared && !isObjectPropertyName(tok)) { + throw createException(false, true, false, false, false, false, INVERSE); + } + return getOWLObjectProperty(tok); + } + } + + + public OWLObjectPropertyExpression parseObjectPropertyExpression() throws ParserException { + return parseObjectPropertyExpression(false); + } + + + public OWLPropertyExpression<?,?> parsePropertyExpression() throws ParserException { + String tok = peekToken(); + if (isObjectPropertyName(tok)) { + return parseObjectPropertyExpression(); + } + else if (tok.equalsIgnoreCase(INVERSE)) { + return parseObjectPropertyExpression(); + } + else if (isDataPropertyName(tok)) { + return parseDataProperty(); + } + else { + consumeToken(); + throw createException(false, true, true, false, false, false, INVERSE); + } + } + + + public OWLClassExpression parseRestriction() throws ParserException { + String tok = peekToken(); + if (isObjectPropertyName(tok) || tok.equalsIgnoreCase(INVERSE)) { + return parseObjectRestriction(); + } + else if (isDataPropertyName(tok)) { + return parseDataRestriction(); + } + else { + consumeToken(); + throw createException(false, true, true, false); + } + } + + + /** + * Parses all class expressions except ObjectIntersectionOf and ObjectUnionOf + * @return The class expression which was parsed + * @throws ParserException if a non-nary class expression could not be parsed + */ + public OWLClassExpression parseNonNaryClassExpression() throws ParserException { + + String tok = peekToken(); + if (tok.equalsIgnoreCase(NOT)) { + consumeToken(); + OWLClassExpression complemented = parseNestedClassExpression(false); + return dataFactory.getOWLObjectComplementOf(complemented); + } + else if (isObjectPropertyName(tok) || tok.equalsIgnoreCase(INVERSE)) { + return parseObjectRestriction(); + } + else if (isDataPropertyName(tok)) { + // Data restriction + return parseDataRestriction(); + } + else if (tok.equals("{")) { + return parseObjectOneOf(); + } + else if (tok.equals("(")) { + return parseNestedClassExpression(false); + } + else if (isClassName(tok)) { + consumeToken(); + return getOWLClass(tok); + } + // Add option for strict class name checking + else { + consumeToken(); + throw createException(true, true, true, false, false, false, "(", "{", NOT, INVERSE); + } + } + + + public OWLClassExpression parseObjectRestriction() throws ParserException { + OWLObjectPropertyExpression prop = parseObjectPropertyExpression(); + String kw = consumeToken(); + if (kw.equalsIgnoreCase(SOME)) { + String possSelfToken = peekToken(); + if (possSelfToken.equalsIgnoreCase(SELF)) { + consumeToken(); + return dataFactory.getOWLObjectHasSelf(prop); + } + else { + OWLClassExpression filler = null; + try { + filler = parseNestedClassExpression(false); + } + catch (ParserException e) { + Set<String> keywords = new HashSet<String>(); + keywords.addAll(e.getExpectedKeywords()); + keywords.add(SELF); + throw createException(e.isClassNameExpected(), e.isObjectPropertyNameExpected(), e.isDataPropertyNameExpected(), e.isIndividualNameExpected(), e.isDatatypeNameExpected(), e.isAnnotationPropertyNameExpected(), keywords.toArray(new String[keywords.size()])); + } + return dataFactory.getOWLObjectSomeValuesFrom(prop, filler); + } + } + else if (kw.equalsIgnoreCase(ONLY)) { + OWLClassExpression filler = parseNestedClassExpression(false); + return dataFactory.getOWLObjectAllValuesFrom(prop, filler); + } + else if (kw.equalsIgnoreCase(VALUE)) { + String indName = consumeToken(); + if (!isIndividualName(indName)) { + throw createException(false, false, false, true); + } + return dataFactory.getOWLObjectHasValue(prop, getOWLIndividual(indName)); + } + else if (kw.equalsIgnoreCase(MIN)) { + int card = parseInteger(); + OWLClassExpression filler = parseNestedClassExpression(true); + if (filler != null) { + return dataFactory.getOWLObjectMinCardinality(card, prop, filler); + } + else { + return dataFactory.getOWLObjectMinCardinality(card, prop); + } + } + else if (kw.equalsIgnoreCase(MAX)) { + int card = parseInteger(); + OWLClassExpression filler = parseNestedClassExpression(true); + if (filler != null) { + return dataFactory.getOWLObjectMaxCardinality(card, prop, filler); + } + else { + return dataFactory.getOWLObjectMaxCardinality(card, prop); + } + } + else if (kw.equalsIgnoreCase(EXACTLY)) { + int card = parseInteger(); + OWLClassExpression filler = parseNestedClassExpression(true); + if (filler != null) { + return dataFactory.getOWLObjectExactCardinality(card, prop, filler); + } + else { + return dataFactory.getOWLObjectExactCardinality(card, prop); + } + } + else if (kw.equalsIgnoreCase(ONLYSOME)) { + String tok = peekToken(); + Set<OWLClassExpression> descs = new HashSet<OWLClassExpression>(); + if (!tok.equals("[")) { + descs.add(parseIntersection()); + } + else { + descs.addAll(parseClassExpressionList("[", "]")); + } + Set<OWLClassExpression> ops = new HashSet<OWLClassExpression>(); + for (OWLClassExpression desc : descs) { + ops.add(dataFactory.getOWLObjectSomeValuesFrom(prop, desc)); + } + OWLClassExpression filler; + if (descs.size() == 1) { + filler = descs.iterator().next(); + } + else { + filler = dataFactory.getOWLObjectUnionOf(descs); + } + ops.add(dataFactory.getOWLObjectAllValuesFrom(prop, filler)); + return dataFactory.getOWLObjectIntersectionOf(ops); + } + else if (kw.equalsIgnoreCase(SELF)) { + return dataFactory.getOWLObjectHasSelf(prop); + } + else { + // Error! + throw createException(SOME, ONLY, VALUE, MIN, MAX, EXACTLY, SELF); + } + } + + + public OWLClassExpression parseDataRestriction() throws ParserException { + OWLDataPropertyExpression prop = parseDataProperty(); + String kw = consumeToken(); + if (kw.equalsIgnoreCase(SOME)) { + OWLDataRange rng = parseDataRange(); + if(rng==null) { + throw new ParserException(getTokenSequence(), getTokenPos(), getTokenRow(), getTokenCol(), true, false, false, false, true, false, Collections.<String>emptySet()); +// return dataFactory.getOWLDataSomeValuesFrom(prop, rng) + } + return dataFactory.getOWLDataSomeValuesFrom(prop, rng); + } + else if (kw.equalsIgnoreCase(ONLY)) { + OWLDataRange rng = parseDataRange(); + return dataFactory.getOWLDataAllValuesFrom(prop, rng); + } + else if (kw.equalsIgnoreCase(VALUE)) { + OWLLiteral con = parseConstant(); + return dataFactory.getOWLDataHasValue(prop, con); + } + else if (kw.equalsIgnoreCase(MIN)) { + int card = parseInteger(); + OWLDataRange rng = parseDataRange(); + if (rng != null) { + return dataFactory.getOWLDataMinCardinality(card, prop, rng); + } + else { + return dataFactory.getOWLDataMinCardinality(card, prop); + } + } + else if (kw.equalsIgnoreCase(EXACTLY)) { + int card = parseInteger(); + OWLDataRange rng = parseDataRange(); + if (rng != null) { + return dataFactory.getOWLDataExactCardinality(card, prop, rng); + } + else { + return dataFactory.getOWLDataExactCardinality(card, prop); + } + } + else if (kw.equalsIgnoreCase(MAX)) { + int card = parseInteger(); + OWLDataRange rng = parseDataRange(); + if (rng != null) { + return dataFactory.getOWLDataMaxCardinality(card, prop, rng); + } + else { + return dataFactory.getOWLDataMaxCardinality(card, prop); + } + } + throw createException(SOME, ONLY, VALUE, MIN, EXACTLY, MAX); + } + + + public OWLFacet parseFacet() throws ParserException { + String facet = consumeToken(); + if (facet.equals(">")) { + if (peekToken().equals("=")) { + consumeToken(); + return OWLFacet.MIN_INCLUSIVE; + } + else { + return OWLFacet.MIN_EXCLUSIVE; + } + } + else if (facet.equals("<")) { + if (peekToken().equals("=")) { + consumeToken(); + return OWLFacet.MAX_INCLUSIVE; + } + else { + return OWLFacet.MAX_EXCLUSIVE; + } + } + return OWLFacet.getFacetBySymbolicName(facet); + } + + + public OWLDatatype parseDatatype() throws ParserException { + String name = consumeToken(); + return getOWLDatatype(name); + } + + + public OWLDataRange parseDataRange() throws ParserException { + return parseDataIntersectionOf(); + } + + public OWLDataRange parseDataIntersectionOf() throws ParserException { + String sep = AND; + Set<OWLDataRange> ranges = new HashSet<OWLDataRange>(); + while (sep.equals(AND)) { + ranges.add(parseDataUnionOf()); + sep = peekToken(); + if (sep.equals(AND)) { + consumeToken(); + } + } + if (ranges.size() == 1) { + return ranges.iterator().next(); + } + else { + return dataFactory.getOWLDataIntersectionOf(ranges); + } + } + + public OWLDataRange parseDataUnionOf() throws ParserException { + String sep = OR; + Set<OWLDataRange> ranges = new HashSet<OWLDataRange>(); + while (sep.equals(OR)) { + ranges.add(parseDataRangePrimary()); + sep = peekToken(); + if (sep.equals(OR)) { + consumeToken(); + } + } + if (ranges.size() == 1) { + return ranges.iterator().next(); + } + else { + return dataFactory.getOWLDataUnionOf(ranges); + } + } + + private OWLDataRange parseDataRangePrimary() throws ParserException { + String tok = peekToken(); + + if (isDatatypeName(tok)) { + consumeToken(); + OWLDatatype datatype = getOWLDatatype(tok); + String next = peekToken(); + if (next.equals("[")) { + // Restricted data range + consumeToken(); + String sep = ","; + Set<OWLFacetRestriction> facetRestrictions = new HashSet<OWLFacetRestriction>(); + while (sep.equals(",")) { + OWLFacet fv = parseFacet(); + if (fv == null) { + throw createException(OWLFacet.getFacets().toArray(new String[OWLFacet.getFacetIRIs().size()])); + } + OWLLiteral con = parseConstant(); + // if (!con.isRDFPlainLiteral()) { + // con = dataFactory.getOWLLiteral(con.getLiteral()); + // } + facetRestrictions.add(dataFactory.getOWLFacetRestriction(fv, con)); + sep = consumeToken(); + } + if (!sep.equals("]")) { + throw createException("]"); + } + return dataFactory.getOWLDatatypeRestriction(datatype, facetRestrictions); + } + else { + return datatype; + } + } + else if (tok.equalsIgnoreCase(NOT)) { + return parseDataComplementOf(); + } + else if (tok.equals("{")) { + return parseDataOneOf(); + } + else if (tok.equals("(")) { + consumeToken(); + OWLDataRange rng = parseDataRange(); + consumeToken(")"); + return rng; + } + else if (!tok.equals(ManchesterOWLSyntaxTokenizer.EOF)) { + consumeToken(); + throw createException(false, false, false, false, true, false, NOT, "{"); + } + return null; + } + + + public Set<OWLDataRange> parseDataRangeList() throws ParserException { + String sep = ","; + Set<OWLDataRange> ranges = new HashSet<OWLDataRange>(); + while (sep.equals(",")) { + potentialKeywords.remove(","); + OWLDataRange rng = parseDataRange(); + ranges.add(rng); + potentialKeywords.add(","); + sep = peekToken(); + if (sep.equals(",")) { + consumeToken(); + } + } + return ranges; + } + + + private OWLDataRange parseDataOneOf() throws ParserException { + consumeToken(); + Set<OWLLiteral> cons = new HashSet<OWLLiteral>(); + String sep = ","; + while (sep.equals(",")) { + OWLLiteral con = parseConstant(); + cons.add(con); + sep = consumeToken(); + } + if (!sep.equals("}")) { + throw createException(",", "}"); + } + return dataFactory.getOWLDataOneOf(cons); + } + + + private OWLDataRange parseDataComplementOf() throws ParserException { + String not = consumeToken(); + if (!not.equalsIgnoreCase(NOT)) { + throw createException(NOT); + } + OWLDataRange complementedDataRange = parseDataRangePrimary(); + return dataFactory.getOWLDataComplementOf(complementedDataRange); + } + + public OWLLiteral parseLiteral() throws ParserException { + String tok = consumeToken(); + if (tok.startsWith("\"")) { + String lit = ""; + if (tok.length() > 2) { + lit = tok.substring(1, tok.length() - 1); + } + if (peekToken().equals("^")) { + consumeToken(); + if (!peekToken().equals("^")) { + throw createException("^"); + } + consumeToken(); + return dataFactory.getOWLLiteral(lit, parseDatatype()); + } + else if (peekToken().startsWith("@")) { + // Plain literal with a language tag + String lang = consumeToken().substring(1); + return dataFactory.getOWLLiteral(lit, lang); + } + else { + // Plain literal without a language tag + return dataFactory.getOWLLiteral(lit, ""); + } + } + else { + try { + int i = Integer.parseInt(tok); + return dataFactory.getOWLLiteral(tok, OWL2Datatype.XSD_INTEGER); + } + catch (NumberFormatException e) { + // Ignore - not interested + } + if (tok.endsWith("f")||tok.endsWith("F")) { + try { + // XXX this extra F might qualify as Float a Double INF/-INF + float f = Float.parseFloat(tok.replace("INF", "Infinity").replace("inf", "Infinity")); + return dataFactory.getOWLLiteral(Float.toString(f).replace("Infinity", "INF"), OWL2Datatype.XSD_FLOAT); + } + catch (NumberFormatException e) { + // Ignore - not interested + } + } + try { + double d = Double.parseDouble(tok); + return dataFactory.getOWLLiteral(tok, OWL2Datatype.XSD_DOUBLE); + } + catch (NumberFormatException e) { + // Ignore - not interested + } + + if (tok.equals("true")) { + return dataFactory.getOWLLiteral(true); + } + else if (tok.equals("false")) { + return dataFactory.getOWLLiteral(false); + } + } + throw createException(false, false, false, false, false, false, "true", "false", "$integer$", "$float$", "$double$", "\"$Literal$\"", "\"$Literal$\"^^<datatype>", "\"$Literal$\"@<lang>"); + } + + /** + * @deprecated Use {@link #parseLiteral()} instead + */ + @Deprecated + public OWLLiteral parseConstant() throws ParserException { + return parseLiteral(); + } + + + public int parseInteger() throws ParserException { + String i = consumeToken(); + try { + return Integer.parseInt(i); + } + catch (NumberFormatException e) { + throw new ParserException(Arrays.asList(getToken().getToken()), getTokenPos(), getTokenRow(), true, getTokenCol()); + } + } + + + public String getLineCol() { + return "Encountered " + getLastToken() + " at " + getTokenRow() + ":" + getTokenCol() + " "; + } + + + private OWLClassExpression parseNestedClassExpression(boolean lookaheadCheck) throws ParserException { + String tok = peekToken(); + if (tok.equals("(")) { + consumeToken(); + OWLClassExpression desc = parseIntersection(); + String closeBracket = consumeToken(); + if (!closeBracket.equals(")")) { + // Error! + throw createException(")"); + } + return desc; + } + else if (tok.equals("{")) { + return parseObjectOneOf(); + } + else if (isClassName(tok)) { + String name = consumeToken(); + return getOWLClass(name); + } + else if (!tok.equals(ManchesterOWLSyntaxTokenizer.EOF) || !lookaheadCheck) { + consumeToken(); + throw createException(true, false, false, false, false, false, "(", "{"); + } + return null; + } + + + public OWLClassExpression parseObjectOneOf() throws ParserException { + String open = consumeToken(); + if (!open.equals("{")) { + throw createException("{"); + } + String sep = ","; + Set<OWLIndividual> inds = new HashSet<OWLIndividual>(); + while (sep.equals(",")) { + OWLIndividual ind = parseIndividual(); + inds.add(ind); + sep = peekToken(); + if (sep.equals(",")) { + consumeToken(); + } + } + String close = consumeToken(); + if (!close.equals("}")) { + throw createException("}", ","); + } + return dataFactory.getOWLObjectOneOf(inds); + } + + + + private <F> void initialiseSection(AnnotatedListItemParser<F, ?> parser, Map<String, AnnotatedListItemParser<F, ?>> map, String ... synonyms) { + map.put(parser.getFrameSectionKeyword(), parser); + for(String syn : synonyms) { + map.put(syn, parser); + } + } + + public Set<OntologyAxiomPair> parseFrames() throws ParserException { + Set<OntologyAxiomPair> axioms = new HashSet<OntologyAxiomPair>(); + Set<String> possible = new HashSet<String>(); + resetPossible(possible); + while (true) { + String tok = peekToken(); + if (tok.equalsIgnoreCase(CLASS)) { + potentialKeywords.clear(); + resetPossible(possible); + axioms.addAll(parseClassFrame()); + possible.addAll(classFrameSections.keySet()); + } + else if (tok.equalsIgnoreCase(OBJECT_PROPERTY)) { + potentialKeywords.clear(); + resetPossible(possible); + axioms.addAll(parseObjectPropertyFrame()); + possible.addAll(objectPropertyFrameSections.keySet()); + } + else if (tok.equalsIgnoreCase(DATA_PROPERTY)) { + potentialKeywords.clear(); + resetPossible(possible); + axioms.addAll(parseDataPropertyFrame()); + possible.ad... [truncated message content] |