├── .gitignore ├── src ├── test │ ├── resources │ │ ├── cars.jpg │ │ ├── catalog-v001.xml │ │ ├── disambiguate.owl │ │ ├── prefix.owl │ │ ├── demo.owl │ │ ├── output-prefix.owl │ │ ├── output.owl │ │ ├── demo-out.owl │ │ ├── individuals.owl │ │ ├── cars.owl │ │ └── dev.owl │ └── java │ │ └── uk │ │ └── ac │ │ └── ebi │ │ ├── BrainUtilsTest.java │ │ ├── BrainQueryTest.java │ │ └── BrainPopulationTest.java └── main │ └── java │ └── uk │ └── ac │ └── ebi │ └── brain │ ├── error │ ├── StorageException.java │ ├── ClassExpressionException.java │ ├── NonExistingEntityException.java │ ├── DataRangeException.java │ ├── ExistingClassException.java │ ├── NamedIndividualException.java │ ├── NonExistingClassException.java │ ├── ExistingNamedIndividualException.java │ ├── ExistingObjectPropertyException.java │ ├── NewOntologyException.java │ ├── ExistingEntityException.java │ ├── ObjectPropertyExpressionException.java │ ├── BrainException.java │ ├── DataPropertyExpressionException.java │ ├── NonExistingNamedIndividualException.java │ ├── ExistingDataPropertyException.java │ ├── NonExistingDataPropertyException.java │ ├── NonExistingObjectPropertyException.java │ ├── ExistingAnnotationPropertyException.java │ ├── NonExistingAnnotationPropertyException.java │ ├── BadPrefixException.java │ └── BadNameException.java │ └── core │ └── Brain.java ├── .settings ├── org.eclipse.m2e.core.prefs ├── org.eclipse.core.resources.prefs └── org.eclipse.jdt.core.prefs ├── .project ├── .classpath ├── README.md └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/test/resources/cars.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/loopasam/Brain/HEAD/src/test/resources/cars.jpg -------------------------------------------------------------------------------- /.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | #Wed Sep 12 10:05:32 BST 2012 2 | activeProfiles= 3 | eclipse.preferences.version=1 4 | resolveWorkspaceProjects=true 5 | version=1 6 | -------------------------------------------------------------------------------- /.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding//src/test/java=UTF-8 4 | encoding//src/test/resources=UTF-8 5 | encoding/=UTF-8 6 | -------------------------------------------------------------------------------- /src/test/resources/catalog-v001.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /src/main/java/uk/ac/ebi/brain/error/StorageException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package uk.ac.ebi.brain.error; 5 | 6 | /** 7 | * Exception thrown in case of storage problem. 8 | * @author Samuel Croset 9 | */ 10 | @SuppressWarnings("serial") 11 | public class StorageException extends BrainException { 12 | 13 | /** 14 | * @param e 15 | */ 16 | public StorageException(Exception e) { 17 | super(e); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 4 | org.eclipse.jdt.core.compiler.compliance=1.6 5 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 6 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 7 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 8 | org.eclipse.jdt.core.compiler.source=1.6 9 | -------------------------------------------------------------------------------- /src/test/resources/disambiguate.owl: -------------------------------------------------------------------------------- 1 | Prefix: owl: 2 | Prefix: rdf: 3 | Prefix: xml: 4 | Prefix: xsd: 5 | Prefix: rdfs: 6 | Prefix: example: 7 | 8 | 9 | Ontology: 10 | 11 | 12 | Class: owl:Thing 13 | 14 | 15 | Class: example:Dog 16 | -------------------------------------------------------------------------------- /src/test/resources/prefix.owl: -------------------------------------------------------------------------------- 1 | Prefix: : 2 | Prefix: owl: 3 | Prefix: rdf: 4 | Prefix: xml: 5 | Prefix: xsd: 6 | Prefix: rdfs: 7 | Prefix: example: 8 | 9 | 10 | 11 | Ontology: 12 | 13 | 14 | Class: owl:Thing 15 | 16 | 17 | Class: B 18 | 19 | 20 | Class: example:A 21 | 22 | SubClassOf: 23 | B 24 | 25 | 26 | -------------------------------------------------------------------------------- /src/test/resources/demo.owl: -------------------------------------------------------------------------------- 1 | Prefix: xsd: 2 | Prefix: owl: 3 | Prefix: : 4 | Prefix: xml: 5 | Prefix: rdf: 6 | Prefix: rdfs: 7 | 8 | 9 | 10 | Ontology: 11 | 12 | 13 | ObjectProperty: part-of 14 | 15 | 16 | Class: owl:Thing 17 | 18 | 19 | Class: Cell 20 | 21 | 22 | Class: Nucleus 23 | 24 | SubClassOf: 25 | part-of some Cell 26 | 27 | 28 | -------------------------------------------------------------------------------- /src/test/resources/output-prefix.owl: -------------------------------------------------------------------------------- 1 | Prefix: : 2 | Prefix: owl: 3 | Prefix: rdf: 4 | Prefix: xml: 5 | Prefix: xsd: 6 | Prefix: rdfs: 7 | 8 | 9 | 10 | Ontology: 11 | 12 | 13 | Class: owl:Thing 14 | 15 | 16 | Class: 17 | 18 | 19 | Class: 20 | 21 | SubClassOf: 22 | 23 | 24 | 25 | Class: C 26 | 27 | 28 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Brain 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/test/resources/output.owl: -------------------------------------------------------------------------------- 1 | Prefix: : 2 | Prefix: dc: 3 | Prefix: owl: 4 | Prefix: rdf: 5 | Prefix: xml: 6 | Prefix: xsd: 7 | Prefix: rdfs: 8 | 9 | 10 | 11 | Ontology: 12 | 13 | 14 | ObjectProperty: part-of 15 | 16 | 17 | Class: owl:Thing 18 | 19 | 20 | Class: Cell 21 | 22 | 23 | Class: A 24 | 25 | 26 | Class: Nucleus 27 | 28 | SubClassOf: 29 | part-of some Cell 30 | 31 | 32 | -------------------------------------------------------------------------------- /src/test/resources/demo-out.owl: -------------------------------------------------------------------------------- 1 | Prefix: : 2 | Prefix: owl: 3 | Prefix: rdf: 4 | Prefix: xml: 5 | Prefix: xsd: 6 | Prefix: rdfs: 7 | 8 | 9 | 10 | Ontology: 11 | 12 | 13 | ObjectProperty: part-of 14 | 15 | 16 | Class: owl:Thing 17 | 18 | 19 | Class: Cell 20 | 21 | 22 | Class: 23 | 24 | 25 | Class: M 26 | 27 | 28 | Class: Nucleus 29 | 30 | SubClassOf: 31 | part-of some Cell 32 | 33 | 34 | -------------------------------------------------------------------------------- /src/test/resources/individuals.owl: -------------------------------------------------------------------------------- 1 | Prefix: xsd: 2 | Prefix: owl: 3 | Prefix: xml: 4 | Prefix: rdf: 5 | Prefix: rdfs: 6 | 7 | 8 | 9 | Ontology: 10 | 11 | 12 | AnnotationProperty: rdfs:label 13 | 14 | 15 | Datatype: rdf:PlainLiteral 16 | 17 | 18 | Class: 19 | 20 | Annotations: 21 | rdfs:label "'a human'" 22 | 23 | 24 | Class: 25 | 26 | Annotations: 27 | rdfs:label "'a fireman'" 28 | 29 | SubClassOf: 30 | 31 | 32 | 33 | Individual: 34 | 35 | Types: 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /src/main/java/uk/ac/ebi/brain/error/ClassExpressionException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright [2013] EMBL - European Bioinformatics Institute 3 | * Licensed under the Apache License, Version 2.0 (the 4 | * "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of 6 | * the License at 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 | * KIND, either express or implied. See the License for the 12 | * specific language governing permissions and limitations 13 | * under the License. 14 | */ 15 | package uk.ac.ebi.brain.error; 16 | 17 | /** 18 | * Exception thrown in case a class expression couldn't be parsed. 19 | * @author Samuel Croset 20 | */ 21 | @SuppressWarnings("serial") 22 | public class ClassExpressionException extends BrainException { 23 | 24 | public ClassExpressionException(Exception e) { 25 | super(e); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/uk/ac/ebi/brain/error/NonExistingEntityException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright [2013] EMBL - European Bioinformatics Institute 3 | * Licensed under the Apache License, Version 2.0 (the 4 | * "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of 6 | * the License at 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 | * KIND, either express or implied. See the License for the 12 | * specific language governing permissions and limitations 13 | * under the License. 14 | */ 15 | package uk.ac.ebi.brain.error; 16 | 17 | /** 18 | * Exception thrown in case of non existing query for an entity. 19 | * @author Samuel Croset 20 | */ 21 | @SuppressWarnings("serial") 22 | public class NonExistingEntityException extends BrainException { 23 | 24 | public NonExistingEntityException(String message) { 25 | super(message); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/uk/ac/ebi/brain/error/DataRangeException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright [2013] EMBL - European Bioinformatics Institute 3 | * Licensed under the Apache License, Version 2.0 (the 4 | * "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of 6 | * the License at 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 | * KIND, either express or implied. See the License for the 12 | * specific language governing permissions and limitations 13 | * under the License. 14 | */ 15 | package uk.ac.ebi.brain.error; 16 | 17 | /** 18 | * Exception thrown in case of problematic data range. 19 | * @author Samuel Croset 20 | */ 21 | @SuppressWarnings("serial") 22 | public class DataRangeException extends BrainException { 23 | 24 | /** 25 | * @param e 26 | */ 27 | public DataRangeException(Exception e) { 28 | super(e); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/uk/ac/ebi/brain/error/ExistingClassException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright [2013] EMBL - European Bioinformatics Institute 3 | * Licensed under the Apache License, Version 2.0 (the 4 | * "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of 6 | * the License at 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 | * KIND, either express or implied. See the License for the 12 | * specific language governing permissions and limitations 13 | * under the License. 14 | */ 15 | package uk.ac.ebi.brain.error; 16 | 17 | /** 18 | * Exception thrown in case of a class is already present. 19 | * @author Samuel Croset 20 | * 21 | */ 22 | @SuppressWarnings("serial") 23 | public class ExistingClassException extends ExistingEntityException { 24 | 25 | public ExistingClassException(String message) { 26 | super(message); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/uk/ac/ebi/brain/error/NamedIndividualException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright [2013] EMBL - European Bioinformatics Institute 3 | * Licensed under the Apache License, Version 2.0 (the 4 | * "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of 6 | * the License at 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 | * KIND, either express or implied. See the License for the 12 | * specific language governing permissions and limitations 13 | * under the License. 14 | */ 15 | package uk.ac.ebi.brain.error; 16 | 17 | /** 18 | * Exception thrown in case a named individual couldn't be parsed. 19 | * @author Samuel Croset 20 | */ 21 | @SuppressWarnings("serial") 22 | public class NamedIndividualException extends BrainException { 23 | 24 | /** 25 | * @param e 26 | */ 27 | public NamedIndividualException(Exception e) { 28 | super(e); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/uk/ac/ebi/brain/error/NonExistingClassException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright [2013] EMBL - European Bioinformatics Institute 3 | * Licensed under the Apache License, Version 2.0 (the 4 | * "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of 6 | * the License at 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 | * KIND, either express or implied. See the License for the 12 | * specific language governing permissions and limitations 13 | * under the License. 14 | */ 15 | package uk.ac.ebi.brain.error; 16 | 17 | /** 18 | * Exception thrown in case of non existing query for a class expression. 19 | * @author Samuel Croset 20 | */ 21 | @SuppressWarnings("serial") 22 | public class NonExistingClassException extends NonExistingEntityException { 23 | 24 | public NonExistingClassException(String message) { 25 | super(message); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/uk/ac/ebi/brain/error/ExistingNamedIndividualException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright [2013] EMBL - European Bioinformatics Institute 3 | * Licensed under the Apache License, Version 2.0 (the 4 | * "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of 6 | * the License at 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 | * KIND, either express or implied. See the License for the 12 | * specific language governing permissions and limitations 13 | * under the License. 14 | */ 15 | package uk.ac.ebi.brain.error; 16 | 17 | /** 18 | * Exception thrown in case of a named individual is already present. 19 | * @author Samuel Croset 20 | * 21 | */ 22 | @SuppressWarnings("serial") 23 | public class ExistingNamedIndividualException extends ExistingEntityException { 24 | public ExistingNamedIndividualException(String message) { 25 | super(message); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/uk/ac/ebi/brain/error/ExistingObjectPropertyException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright [2013] EMBL - European Bioinformatics Institute 3 | * Licensed under the Apache License, Version 2.0 (the 4 | * "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of 6 | * the License at 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 | * KIND, either express or implied. See the License for the 12 | * specific language governing permissions and limitations 13 | * under the License. 14 | */ 15 | package uk.ac.ebi.brain.error; 16 | 17 | /** 18 | * Exception thrown in case of an object property is already present. 19 | * @author Samuel Croset 20 | * 21 | */ 22 | @SuppressWarnings("serial") 23 | public class ExistingObjectPropertyException extends ExistingEntityException { 24 | 25 | public ExistingObjectPropertyException(String message) { 26 | super(message); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/uk/ac/ebi/brain/error/NewOntologyException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright [2013] EMBL - European Bioinformatics Institute 3 | * Licensed under the Apache License, Version 2.0 (the 4 | * "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of 6 | * the License at 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 | * KIND, either express or implied. See the License for the 12 | * specific language governing permissions and limitations 13 | * under the License. 14 | */ 15 | package uk.ac.ebi.brain.error; 16 | 17 | /** 18 | * Exception thrown in case of problem while creating a new ontology. 19 | * @author Samuel Croset 20 | * 21 | */ 22 | @SuppressWarnings("serial") 23 | public class NewOntologyException extends BrainException { 24 | 25 | /** 26 | * @param e 27 | */ 28 | public NewOntologyException(Exception e) { 29 | super(e); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/uk/ac/ebi/brain/error/ExistingEntityException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright [2013] EMBL - European Bioinformatics Institute 3 | * Licensed under the Apache License, Version 2.0 (the 4 | * "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of 6 | * the License at 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 | * KIND, either express or implied. See the License for the 12 | * specific language governing permissions and limitations 13 | * under the License. 14 | */ 15 | package uk.ac.ebi.brain.error; 16 | 17 | /** 18 | * Exception thrown in case of an entity is already present. 19 | * @author Samuel Croset 20 | * 21 | */ 22 | @SuppressWarnings("serial") 23 | public class ExistingEntityException extends BrainException { 24 | 25 | /** 26 | * @param message 27 | */ 28 | public ExistingEntityException(String message) { 29 | super(message); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/uk/ac/ebi/brain/error/ObjectPropertyExpressionException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright [2013] EMBL - European Bioinformatics Institute 3 | * Licensed under the Apache License, Version 2.0 (the 4 | * "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of 6 | * the License at 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 | * KIND, either express or implied. See the License for the 12 | * specific language governing permissions and limitations 13 | * under the License. 14 | */ 15 | package uk.ac.ebi.brain.error; 16 | 17 | /** 18 | * Exception thrown in case an object property couldn't be parsed. 19 | * @author Samuel Croset 20 | */ 21 | @SuppressWarnings("serial") 22 | public class ObjectPropertyExpressionException extends BrainException { 23 | 24 | /** 25 | * @param e 26 | */ 27 | public ObjectPropertyExpressionException(Exception e) { 28 | super(e); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/uk/ac/ebi/brain/error/BrainException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright [2013] EMBL - European Bioinformatics Institute 3 | * Licensed under the Apache License, Version 2.0 (the 4 | * "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of 6 | * the License at 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 | * KIND, either express or implied. See the License for the 12 | * specific language governing permissions and limitations 13 | * under the License. 14 | */ 15 | package uk.ac.ebi.brain.error; 16 | 17 | /** 18 | * Generic type of expression 19 | * @author Samuel Croset 20 | * 21 | */ 22 | @SuppressWarnings("serial") 23 | public class BrainException extends Exception { 24 | 25 | public BrainException(String message) { 26 | super(message); 27 | } 28 | 29 | /** 30 | * @param e 31 | */ 32 | public BrainException(Exception e) { 33 | super(e); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/uk/ac/ebi/brain/error/DataPropertyExpressionException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright [2013] EMBL - European Bioinformatics Institute 3 | * Licensed under the Apache License, Version 2.0 (the 4 | * "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of 6 | * the License at 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 | * KIND, either express or implied. See the License for the 12 | * specific language governing permissions and limitations 13 | * under the License. 14 | */ 15 | package uk.ac.ebi.brain.error; 16 | 17 | /** 18 | * Exception thrown in case a data property couldn't be parsed. 19 | * @author Samuel Croset 20 | * 21 | */ 22 | @SuppressWarnings("serial") 23 | public class DataPropertyExpressionException extends BrainException { 24 | 25 | /** 26 | * @param e 27 | */ 28 | public DataPropertyExpressionException(Exception e) { 29 | super(e); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/uk/ac/ebi/brain/error/NonExistingNamedIndividualException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright [2013] EMBL - European Bioinformatics Institute 3 | * Licensed under the Apache License, Version 2.0 (the 4 | * "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of 6 | * the License at 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 | * KIND, either express or implied. See the License for the 12 | * specific language governing permissions and limitations 13 | * under the License. 14 | */ 15 | package uk.ac.ebi.brain.error; 16 | 17 | //TODO do the doc here 18 | 19 | /** 20 | * Exception thrown in case of non existing query for a named individual. 21 | * @author Samuel Croset 22 | */ 23 | @SuppressWarnings("serial") 24 | public class NonExistingNamedIndividualException extends NonExistingEntityException { 25 | public NonExistingNamedIndividualException(String message) { 26 | super(message); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/uk/ac/ebi/brain/error/ExistingDataPropertyException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright [2013] EMBL - European Bioinformatics Institute 3 | * Licensed under the Apache License, Version 2.0 (the 4 | * "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of 6 | * the License at 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 | * KIND, either express or implied. See the License for the 12 | * specific language governing permissions and limitations 13 | * under the License. 14 | */ 15 | package uk.ac.ebi.brain.error; 16 | 17 | /** 18 | * Exception thrown in case of a data property is already present. 19 | * @author Samuel Croset 20 | * 21 | */ 22 | @SuppressWarnings("serial") 23 | public class ExistingDataPropertyException extends ExistingEntityException { 24 | 25 | /** 26 | * @param message 27 | */ 28 | public ExistingDataPropertyException(String message) { 29 | super(message); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/uk/ac/ebi/brain/error/NonExistingDataPropertyException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright [2013] EMBL - European Bioinformatics Institute 3 | * Licensed under the Apache License, Version 2.0 (the 4 | * "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of 6 | * the License at 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 | * KIND, either express or implied. See the License for the 12 | * specific language governing permissions and limitations 13 | * under the License. 14 | */ 15 | package uk.ac.ebi.brain.error; 16 | 17 | /** 18 | * Exception thrown in case of non existing query for a data property. 19 | * @author Samuel Croset 20 | */ 21 | @SuppressWarnings("serial") 22 | public class NonExistingDataPropertyException extends NonExistingEntityException { 23 | 24 | /** 25 | * @param message 26 | */ 27 | public NonExistingDataPropertyException(String message) { 28 | super(message); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/uk/ac/ebi/brain/error/NonExistingObjectPropertyException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright [2013] EMBL - European Bioinformatics Institute 3 | * Licensed under the Apache License, Version 2.0 (the 4 | * "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of 6 | * the License at 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 | * KIND, either express or implied. See the License for the 12 | * specific language governing permissions and limitations 13 | * under the License. 14 | */ 15 | package uk.ac.ebi.brain.error; 16 | 17 | /** 18 | * Exception thrown in case of non existing query for an object property. 19 | * @author Samuel Croset 20 | */ 21 | @SuppressWarnings("serial") 22 | public class NonExistingObjectPropertyException extends BrainException { 23 | 24 | /** 25 | * @param message 26 | */ 27 | public NonExistingObjectPropertyException(String message) { 28 | super(message); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/uk/ac/ebi/brain/error/ExistingAnnotationPropertyException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright [2013] EMBL - European Bioinformatics Institute 3 | * Licensed under the Apache License, Version 2.0 (the 4 | * "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of 6 | * the License at 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 | * KIND, either express or implied. See the License for the 12 | * specific language governing permissions and limitations 13 | * under the License. 14 | */ 15 | package uk.ac.ebi.brain.error; 16 | 17 | /** 18 | * Exception thrown in case of an annotation property is already present. 19 | * @author Samuel Croset 20 | */ 21 | @SuppressWarnings("serial") 22 | public class ExistingAnnotationPropertyException extends ExistingEntityException { 23 | 24 | /** 25 | * @param message 26 | */ 27 | public ExistingAnnotationPropertyException(String message) { 28 | super(message); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/uk/ac/ebi/brain/error/NonExistingAnnotationPropertyException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright [2013] EMBL - European Bioinformatics Institute 3 | * Licensed under the Apache License, Version 2.0 (the 4 | * "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of 6 | * the License at 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 | * KIND, either express or implied. See the License for the 12 | * specific language governing permissions and limitations 13 | * under the License. 14 | */ 15 | package uk.ac.ebi.brain.error; 16 | 17 | 18 | /** 19 | * Exception thrown in case of non existing query for an annotation property. 20 | * @author Samuel Croset 21 | */ 22 | @SuppressWarnings("serial") 23 | public class NonExistingAnnotationPropertyException extends NonExistingEntityException { 24 | 25 | /** 26 | * @param message 27 | */ 28 | public NonExistingAnnotationPropertyException(String message) { 29 | super(message); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/uk/ac/ebi/brain/error/BadPrefixException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright [2013] EMBL - European Bioinformatics Institute 3 | * Licensed under the Apache License, Version 2.0 (the 4 | * "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of 6 | * the License at 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 | * KIND, either express or implied. See the License for the 12 | * specific language governing permissions and limitations 13 | * under the License. 14 | */ 15 | package uk.ac.ebi.brain.error; 16 | 17 | import java.net.MalformedURLException; 18 | 19 | /** 20 | * Exception thrown in case of problematic prefix 21 | * @author Samuel Croset 22 | * 23 | */ 24 | @SuppressWarnings("serial") 25 | public class BadPrefixException extends BrainException { 26 | 27 | public BadPrefixException(String message) { 28 | super(message); 29 | } 30 | 31 | /** 32 | * @param e 33 | */ 34 | public BadPrefixException(MalformedURLException e) { 35 | super(e); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /src/main/java/uk/ac/ebi/brain/error/BadNameException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright [2013] EMBL - European Bioinformatics Institute 3 | * Licensed under the Apache License, Version 2.0 (the 4 | * "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of 6 | * the License at 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 | * KIND, either express or implied. See the License for the 12 | * specific language governing permissions and limitations 13 | * under the License. 14 | */ 15 | package uk.ac.ebi.brain.error; 16 | 17 | import java.net.MalformedURLException; 18 | import java.net.URISyntaxException; 19 | 20 | /** 21 | * Exception thrown in case of problematic name for OWL entities. 22 | * @author Samuel Croset 23 | * 24 | */ 25 | @SuppressWarnings("serial") 26 | public class BadNameException extends BrainException { 27 | 28 | /** 29 | * @param message 30 | */ 31 | public BadNameException(String message) { 32 | super(message); 33 | } 34 | 35 | /** 36 | * @param e 37 | */ 38 | public BadNameException(MalformedURLException e) { 39 | super(e); 40 | } 41 | 42 | /** 43 | * @param e 44 | */ 45 | public BadNameException(URISyntaxException e) { 46 | super(e); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Brain - Library for biomedical knowledge manipulation 2 | 3 | **Brain** helps you to quickly and easily buid OWL ontologies and knowledge bases. It aims at bridging the gap 4 | between graphical user interfaces (GUI) such as [Protege](http://protege.stanford.edu/) and the 5 | [OWL-API](http://owlapi.sourceforge.net/). 6 | 7 | The library is 8 | useful to develop Semantic Web applications and particularly suited for the biomedical domain. 9 | Brain wraps the interaction with the OWL-API an relies on [Elk](http://elk-reasoner.googlecode.com/) 10 | for reasoning tasks. 11 | 12 | # Features 13 | 14 | ## Manchester syntax support 15 | The interaction with Brain is done with the user friendly Manchester syntax. 16 | Brain exposes a series of method to help you validate and transform strings of 17 | characters into OWL expressions. 18 | 19 | ## Web Application development 20 | Brain has been created for Semantic Web application developers. The library is thread-safe 21 | which allows you to handle ontologies in a web server setting. 22 | 23 | ## Scalability 24 | 25 | OWL is a very demanding in terms of computing, but thanks to the 26 | [OWL 2 EL](https://github.com/loopasam/Brain/wiki/OWL-2-EL) profile, it is possible to 27 | thread reasoning some tasks in parallel (Elk reasoner). Brain therefore supports the OWL 2 EL to help you 28 | build scalable solutions. 29 | 30 | ## Query 31 | OWL knowledge bases can be easily queried with Brain. Powerful 32 | questions can be quickly answered with the help of the Elk reasoner. 33 | Brain accepts the formulation of queries from labels as well as from class 34 | expressions. 35 | 36 | # Documentation 37 | 38 | Read the wiki for more details: https://github.com/loopasam/Brain/wiki 39 | -------------------------------------------------------------------------------- /src/test/java/uk/ac/ebi/BrainUtilsTest.java: -------------------------------------------------------------------------------- 1 | package uk.ac.ebi; 2 | 3 | 4 | import static org.junit.Assert.assertEquals; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | import org.junit.Test; 10 | import org.semanticweb.owlapi.model.OWLClass; 11 | 12 | import uk.ac.ebi.brain.core.Brain; 13 | import uk.ac.ebi.brain.error.BrainException; 14 | 15 | public class BrainUtilsTest { 16 | 17 | @Test 18 | public void learnAndDisambiguateTest() throws BrainException { 19 | Brain brain = new Brain(); 20 | brain.addClass("http://www.foo.org/Dog"); 21 | brain.learnAndDisambiguate("src/test/resources/disambiguate.owl"); 22 | List classes = brain.getSubClasses("Thing", true); 23 | assertEquals(classes.size(), 1); 24 | OWLClass owlClass = brain.getOWLClass(classes.get(0)); 25 | assertEquals("http://www.foo.org/Dog", owlClass.getIRI().toString()); 26 | } 27 | 28 | @Test 29 | public void jaccardSimilarityTest() throws BrainException { 30 | Brain brain = new Brain(); 31 | brain.learn("src/test/resources/cars.owl"); 32 | float index = brain.getJaccardSimilarityIndex("Car", "MiniVan"); 33 | assertEquals(0.5, index, 1e-15); 34 | 35 | float index1 = brain.getJaccardSimilarityIndex("Sedan", "Car"); 36 | assertEquals(0.800, index1, 1e-3); 37 | 38 | float index2 = brain.getJaccardSimilarityIndex("Thing", "Thing"); 39 | assertEquals(1.000, index2, 1e-3); 40 | 41 | brain.sleep(); 42 | } 43 | 44 | @Test 45 | public void jaccardGroupSimilarityTest() throws BrainException { 46 | Brain brain = new Brain(); 47 | brain.learn("src/test/resources/cars.owl"); 48 | List group1 = new ArrayList(); 49 | group1.add("Sedan"); 50 | group1.add("SportCoupe"); 51 | 52 | List group2 = new ArrayList(); 53 | group2.add("MiniVan"); 54 | group2.add("ToyotaCorrola"); 55 | 56 | float index = brain.getJaccardSimilarityIndex(group1, group2); 57 | assertEquals(0.556, index, 1e-3); 58 | 59 | brain.sleep(); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/test/resources/cars.owl: -------------------------------------------------------------------------------- 1 | Prefix: xsd: 2 | Prefix: owl: 3 | Prefix: xml: 4 | Prefix: rdf: 5 | Prefix: rdfs: 6 | 7 | 8 | 9 | Ontology: 10 | 11 | 12 | Class: 13 | 14 | SubClassOf: 15 | 16 | 17 | 18 | Class: 19 | 20 | SubClassOf: 21 | 22 | 23 | 24 | Class: 25 | 26 | SubClassOf: 27 | , 28 | 29 | 30 | 31 | Class: 32 | 33 | SubClassOf: 34 | 35 | 36 | 37 | Class: 38 | 39 | SubClassOf: 40 | 41 | 42 | 43 | Class: 44 | 45 | SubClassOf: 46 | 47 | 48 | 49 | Class: 50 | 51 | SubClassOf: 52 | 53 | 54 | 55 | Class: 56 | 57 | 58 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | uk.ac.ebi.brain 4 | Brain 5 | 1.5.2-SNAPSHOT 6 | jar 7 | Brain 8 | Facade for the OWL-API for biomedical knowledge manipulation 9 | http://loopasam.github.com/Brain/ 10 | 11 | 12 | The Apache Software License, Version 2.0 13 | http://www.apache.org/licenses/LICENSE-2.0.txt 14 | repo 15 | 16 | 17 | 18 | scm:git:git@github.com:loopasam/Brain.git 19 | scm:git:git@github.com:loopasam/Brain.git 20 | git@github.com:loopasam/Brain.git 21 | 22 | 23 | 24 | https://github.com/loopasam 25 | Samuel Croset 26 | samuel.croset@gmail.com 27 | 28 | 29 | 30 | UTF-8 31 | 32 | 33 | 34 | junit 35 | junit 36 | 4.10 37 | test 38 | 39 | 40 | net.sourceforge.owlapi 41 | owlapi-api 42 | 3.4.10 43 | 44 | 45 | org.semanticweb.elk 46 | elk-owlapi 47 | 0.4.1 48 | 49 | 50 | 51 | 52 | 53 | org.apache.maven.plugins 54 | maven-gpg-plugin 55 | 1.5 56 | 57 | 58 | sign-artifacts 59 | verify 60 | 61 | sign 62 | 63 | 64 | 65 | 66 | 67 | org.apache.maven.plugins 68 | maven-compiler-plugin 69 | 2.5.1 70 | 71 | 1.6 72 | 1.6 73 | 74 | 75 | 76 | maven-assembly-plugin 77 | 78 | 79 | jar-with-dependencies 80 | 81 | 82 | 83 | 84 | 85 | 86 | org.sonatype.oss 87 | oss-parent 88 | 7 89 | 90 | -------------------------------------------------------------------------------- /src/test/resources/dev.owl: -------------------------------------------------------------------------------- 1 | Prefix: : 2 | Prefix: dc: 3 | Prefix: owl: 4 | Prefix: rdf: 5 | Prefix: xml: 6 | Prefix: xsd: 7 | Prefix: rdfs: 8 | 9 | 10 | 11 | Ontology: 12 | 13 | 14 | AnnotationProperty: rdfs:seeAlso 15 | 16 | 17 | AnnotationProperty: rdfs:isDefinedBy 18 | 19 | 20 | AnnotationProperty: rdfs:label 21 | 22 | 23 | AnnotationProperty: testing 24 | 25 | 26 | AnnotationProperty: rdfs:comment 27 | 28 | 29 | Datatype: rdf:PlainLiteral 30 | 31 | 32 | Datatype: xsd:string 33 | 34 | 35 | Datatype: rdfs:Literal 36 | 37 | 38 | ObjectProperty: part-of 39 | 40 | Characteristics: 41 | Transitive 42 | 43 | 44 | Class: owl:Thing 45 | 46 | DisjointWith: 47 | R 48 | 49 | 50 | Class: Q 51 | 52 | SubClassOf: 53 | part-of some K 54 | 55 | 56 | Class: R 57 | 58 | DisjointWith: 59 | owl:Thing 60 | 61 | 62 | Class: O 63 | 64 | EquivalentTo: 65 | part-of some M 66 | 67 | 68 | Class: M 69 | 70 | EquivalentTo: 71 | N 72 | 73 | 74 | Class: N 75 | 76 | EquivalentTo: 77 | M 78 | 79 | SubClassOf: 80 | owl:Thing 81 | 82 | 83 | Class: K 84 | 85 | SubClassOf: 86 | owl:Thing 87 | 88 | 89 | Class: L 90 | 91 | SubClassOf: 92 | K 93 | 94 | 95 | Class: I 96 | 97 | SubClassOf: 98 | part-of some L 99 | 100 | 101 | Class: ID01 102 | 103 | Annotations: 104 | rdfs:label "tail"^^xsd:string 105 | 106 | SubClassOf: 107 | part-of some ID03 108 | 109 | 110 | Class: J 111 | 112 | SubClassOf: 113 | I 114 | 115 | 116 | Class: ID03 117 | 118 | Annotations: 119 | rdfs:label "animal"^^xsd:string 120 | 121 | SubClassOf: 122 | owl:Thing 123 | 124 | 125 | Class: G 126 | 127 | Annotations: 128 | testing "value2", 129 | testing "value1" 130 | 131 | 132 | Class: ID02 133 | 134 | Annotations: 135 | rdfs:label "dog"^^xsd:string 136 | 137 | SubClassOf: 138 | ID03 139 | 140 | 141 | Class: H 142 | 143 | SubClassOf: 144 | G 145 | 146 | 147 | Class: F 148 | 149 | SubClassOf: 150 | E, 151 | H 152 | 153 | 154 | Class: E 155 | 156 | SubClassOf: 157 | D 158 | 159 | 160 | Class: D 161 | 162 | SubClassOf: 163 | A 164 | 165 | 166 | Class: C 167 | 168 | SubClassOf: 169 | B 170 | 171 | 172 | Class: B 173 | 174 | SubClassOf: 175 | A 176 | 177 | 178 | Class: A 179 | 180 | Annotations: 181 | rdfs:comment "comment attached to the class"^^xsd:string, 182 | rdfs:isDefinedBy "something"^^xsd:string, 183 | testing "whatever"^^xsd:string, 184 | rdfs:seeAlso "bar"^^xsd:string, 185 | rdfs:label "pouet"^^xsd:string 186 | 187 | EquivalentTo: 188 | Z 189 | 190 | 191 | Class: Z 192 | 193 | Annotations: 194 | rdfs:label "'pouet pouet'"^^xsd:string 195 | 196 | EquivalentTo: 197 | A 198 | 199 | 200 | -------------------------------------------------------------------------------- /src/test/java/uk/ac/ebi/BrainQueryTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package uk.ac.ebi; 5 | 6 | import static org.junit.Assert.assertEquals; 7 | import static org.junit.Assert.assertNotNull; 8 | 9 | import java.util.List; 10 | 11 | import org.junit.After; 12 | import org.junit.Before; 13 | import org.junit.Test; 14 | 15 | import uk.ac.ebi.brain.core.Brain; 16 | import uk.ac.ebi.brain.error.BrainException; 17 | import uk.ac.ebi.brain.error.ClassExpressionException; 18 | import uk.ac.ebi.brain.error.ExistingClassException; 19 | import uk.ac.ebi.brain.error.ExistingEntityException; 20 | 21 | 22 | /** 23 | * @author Samuel Croset 24 | * 25 | */ 26 | public class BrainQueryTest { 27 | 28 | Brain brain; 29 | 30 | @Before 31 | public void bootstrap() throws BrainException { 32 | brain = new Brain("http://localhost/", "http://localhost/test.owl"); 33 | brain.learn("src/test/resources/dev.owl"); 34 | } 35 | 36 | @After 37 | public void dispose() { 38 | brain.sleep(); 39 | } 40 | 41 | @Test 42 | public void getNamedSubClass() throws BrainException { 43 | Brain brain = new Brain(); 44 | brain.addClass("Animal"); 45 | brain.addClass("Lion"); 46 | brain.subClassOf("Lion", "Animal"); 47 | brain.getSubClasses("Animal", false); 48 | assertEquals(true, brain.getSubClasses("Animal", false).contains("Lion")); 49 | assertEquals(true, brain.getSubClasses("Animal", false).contains("Lion")); 50 | } 51 | 52 | @Test 53 | public void getNamedIndividual() throws BrainException { 54 | Brain brain = new Brain(); 55 | brain.learn("src/test/resources/individuals.owl"); 56 | Listindividuals = brain.getInstances("Human", false); 57 | assertEquals(1, individuals.size()); 58 | assertEquals("Joe", individuals.get(0)); 59 | individuals = brain.getInstances("Human", true); 60 | assertEquals(0, individuals.size()); 61 | individuals = brain.getInstances("Fireman", true); 62 | assertEquals(1, individuals.size()); 63 | individuals = brain.getInstancesFromLabel("'a human'", false); 64 | assertEquals(1, individuals.size()); 65 | } 66 | 67 | //TODO more test could be done regarding individuals - like anonymous class expressions (from label for instance) 68 | 69 | @Test 70 | public void getUnsatisfiableClasses() throws BrainException { 71 | Brain brain = new Brain(); 72 | brain.addClass("A"); 73 | brain.addClass("B"); 74 | brain.addClass("C"); 75 | brain.disjointClasses("A", "B"); 76 | brain.subClassOf("C", "A"); 77 | brain.subClassOf("C", "B"); 78 | brain.sleep(); 79 | assertEquals(1, brain.getUnsatisfiableClasses().size()); 80 | brain.sleep(); 81 | } 82 | 83 | @Test 84 | public void reStart() throws BrainException { 85 | brain.sleep(); 86 | List subClasses = brain.getSubClasses("I", false); 87 | assertEquals(2, subClasses.size()); 88 | } 89 | 90 | @Test 91 | public void owlProfileTest() throws BrainException { 92 | boolean satisfied = brain.hasElProfile(); 93 | assertEquals(true, satisfied); 94 | List violations = brain.getElProfileViolations(); 95 | assertEquals(0, violations.size()); 96 | } 97 | 98 | @Test 99 | public void prefixesAndLearnTest() throws BrainException { 100 | Brain newBrain = new Brain(); 101 | newBrain.addClass("http://whatever.com/TEST_A"); 102 | newBrain.prefix("http://whatever.com/", "whatever"); 103 | brain.prefix("http://www.example.org/", "example"); 104 | brain.learn(newBrain); 105 | assertEquals("http://whatever.com/", brain.getPrefixManager().getPrefix("whatever:")); 106 | assertEquals("http://www.example.org/", brain.getPrefixManager().getPrefix("example:")); 107 | } 108 | 109 | 110 | @Test 111 | public void classifyTest() { 112 | brain.classify(); 113 | } 114 | 115 | @Test 116 | public void getDirectSubClassesTest() throws ClassExpressionException{ 117 | List subClasses = brain.getSubClasses("I", true); 118 | assertEquals(1, subClasses.size()); 119 | } 120 | 121 | @Test 122 | public void getIndirectSubClassesTest() throws BrainException { 123 | List subClasses = brain.getSubClasses("G", false); 124 | assertEquals(3, subClasses.size()); 125 | } 126 | 127 | @Test 128 | public void getDirectAnonymousSubClassesTest() throws BrainException { 129 | List subClasses = brain.getSubClasses("part-of some L", true); 130 | assertEquals(1, subClasses.size()); 131 | } 132 | 133 | @Test 134 | public void getIndirectAnonymousSubClassesTest() throws BrainException { 135 | List subClasses = brain.getSubClasses("part-of some L", false); 136 | assertEquals(3, subClasses.size()); 137 | } 138 | 139 | @Test 140 | public void getAnonymousClassesNewOntology() throws BrainException { 141 | Brain brain = new Brain(); 142 | brain.addClass("A"); 143 | brain.addClass("B"); 144 | brain.addObjectProperty("part-of"); 145 | brain.subClassOf("B", "part-of some A"); 146 | List subClasses = brain.getSubClasses("part-of some A", true); 147 | assertEquals(1, subClasses.size()); 148 | } 149 | 150 | @Test(expected = ClassExpressionException.class) 151 | public void getAnonymousClassesErrorNewOntology() throws BrainException { 152 | Brain brain = new Brain(); 153 | brain.addClass("A"); 154 | brain.addClass("B"); 155 | brain.addObjectProperty("part-of"); 156 | brain.subClassOf("B", "part-of some C"); 157 | List subClasses = brain.getSubClasses("part-of some A", true); 158 | assertEquals(1, subClasses.size()); 159 | } 160 | 161 | 162 | @Test 163 | public void getSuperClassesTest() throws BrainException { 164 | List superClasses = brain.getSuperClasses("C", false); 165 | assertEquals(4, superClasses.size()); 166 | List superClasses1 = brain.getSuperClasses("C", true); 167 | assertEquals(1, superClasses1.size()); 168 | } 169 | 170 | @Test 171 | public void getEquivalentClassesTest() throws BrainException { 172 | List equivalentClasses = brain.getEquivalentClasses("M"); 173 | assertEquals(1, equivalentClasses.size()); 174 | assertEquals("N", equivalentClasses.get(0)); 175 | } 176 | 177 | @Test(expected = ClassExpressionException.class) 178 | public void errorsWhenQueryingForLabel() throws BrainException { 179 | List subClasses = brain.getSubClasses("ID01", false); 180 | assertEquals(1, subClasses.size()); 181 | @SuppressWarnings("unused") 182 | List subClasses1 = brain.getSubClassesFromLabel("ID011", false); 183 | } 184 | 185 | @Test 186 | public void getSubClassesFromLabelTest() throws BrainException { 187 | List subClasses = brain.getSubClassesFromLabel("animal", false); 188 | assertEquals(2, subClasses.size()); 189 | assertEquals("ID02", subClasses.get(0)); 190 | assertEquals("R", subClasses.get(1)); 191 | 192 | List subClasses1 = brain.getSubClassesFromLabel("part-of some animal", false); 193 | assertEquals(2, subClasses1.size()); 194 | assertEquals("ID01", subClasses1.get(0)); 195 | } 196 | 197 | @Test 198 | public void getFromSpaceSeparatedLabels() throws BrainException { 199 | List equivalents = brain.getEquivalentClassesFromLabel("'pouet pouet'"); 200 | assertEquals(1, equivalents.size()); 201 | assertEquals("A", equivalents.get(0)); 202 | } 203 | 204 | @Test 205 | public void getSuperClassesFromLabelTest() throws BrainException { 206 | List superClasses = brain.getSuperClassesFromLabel("animal", false); 207 | assertEquals(1, superClasses.size()); 208 | assertEquals("Thing", superClasses.get(0)); 209 | 210 | List subClasses1 = brain.getSuperClassesFromLabel("part-of some animal", false); 211 | assertEquals(1, subClasses1.size()); 212 | assertEquals("Thing", subClasses1.get(0)); 213 | } 214 | 215 | @Test 216 | public void getEquivalentClassesFromLabelTest() throws BrainException { 217 | List equivalentClasses = brain.getEquivalentClassesFromLabel("pouet"); 218 | assertEquals(1, equivalentClasses.size()); 219 | assertEquals("Z", equivalentClasses.get(0)); 220 | } 221 | 222 | @Test 223 | public void getEquivalentAnonClassesTest() throws BrainException { 224 | List equivalentClasses = brain.getEquivalentClasses("part-of some M"); 225 | assertEquals(1, equivalentClasses.size()); 226 | assertEquals("O", equivalentClasses.get(0)); 227 | } 228 | 229 | @Test 230 | public void isSubClassTest() throws BrainException { 231 | boolean isSubClass = brain.isSubClass("D", "Thing", false); 232 | assertEquals(true, isSubClass); 233 | boolean isSubClass1 = brain.isSubClass("Q", "part-of some K", false); 234 | assertEquals(true, isSubClass1); 235 | boolean isSubClass2 = brain.isSubClass("part-of some K", "Q", false); 236 | assertEquals(false, isSubClass2); 237 | } 238 | 239 | @Test 240 | public void isSuperClassTest() throws BrainException { 241 | boolean isSuperClass = brain.isSuperClass("Thing", "D", false); 242 | assertEquals(true, isSuperClass); 243 | boolean isSuperClass1 = brain.isSuperClass("Q", "part-of some K", false); 244 | assertEquals(false, isSuperClass1); 245 | boolean isSuperClass2 = brain.isSuperClass("part-of some K", "Q", false); 246 | assertEquals(true, isSuperClass2); 247 | } 248 | 249 | @Test 250 | public void knowsTest() { 251 | boolean knowsClass = brain.knowsClass("M"); 252 | assertEquals(true, knowsClass); 253 | boolean notknowsClass = brain.knowsClass("POUET"); 254 | assertEquals(false, notknowsClass); 255 | boolean notknowsObjectProperty = brain.knowsObjectProperty("part-of"); 256 | assertEquals(true, notknowsObjectProperty); 257 | boolean notknowsDataProperty = brain.knowsDataProperty("age"); 258 | assertEquals(false, notknowsDataProperty); 259 | boolean notknowsAnnotationProperty = brain.knowsAnnotationProperty("testing"); 260 | assertEquals(true, notknowsAnnotationProperty); 261 | } 262 | 263 | @Test 264 | public void getLabelTest() throws BrainException { 265 | String label = brain.getLabel("A"); 266 | assertEquals("pouet", label); 267 | String comment = brain.getComment("A"); 268 | assertEquals("comment attached to the class", comment); 269 | String isDefinedBy = brain.getIsDefinedBy("A"); 270 | assertEquals("something", isDefinedBy); 271 | String seeAlso = brain.getSeeAlso("A"); 272 | assertEquals("bar", seeAlso); 273 | String testing = brain.getAnnotation("A", "testing"); 274 | assertEquals("whatever", testing); 275 | } 276 | 277 | @Test 278 | public void getAnnotationArrayTest() throws BrainException { 279 | List annotations = brain.getAnnotations("G", "testing"); 280 | assertEquals(2, annotations.size()); 281 | assertEquals("value2", annotations.get(0)); 282 | assertEquals("value1", annotations.get(1)); 283 | } 284 | 285 | @Test 286 | public void learnFromLocalFile() throws BrainException { 287 | Brain brain = new Brain(); 288 | brain.learn("src/test/resources/demo.owl"); 289 | brain.addClass("A"); 290 | brain.save("src/test/resources/output.owl"); 291 | } 292 | 293 | @Test(expected = ExistingClassException.class) 294 | public void learnSupportsIdenticalIRIs() throws BrainException { 295 | Brain brain = new Brain(); 296 | brain.addClass("http://www.example.org/N"); 297 | brain.learn("src/test/resources/dev.owl"); 298 | brain.addClass("M"); 299 | } 300 | 301 | @Test(expected = ExistingEntityException.class) 302 | public void learnSupportsIdenticalIRIsButNotForDifferentTypes() throws BrainException { 303 | Brain brain = new Brain(); 304 | brain.addClass("http://www.example.org/part-of"); 305 | brain.learn("src/test/resources/dev.owl"); 306 | } 307 | 308 | @Test 309 | public void learnOntologyFromTheWeb() throws BrainException { 310 | Brain brain = new Brain(); 311 | brain.learn("https://raw.github.com/loopasam/Brain/master/src/test/resources/demo.owl"); 312 | assertNotNull(brain.getOWLClass("Cell")); 313 | } 314 | 315 | @Test 316 | public void learnFromOtherBrainTest() throws BrainException { 317 | Brain brain = new Brain(); 318 | brain.addClass("A"); 319 | Brain brain1 = new Brain(); 320 | brain1.learn(brain); 321 | assertNotNull(brain1.getOWLClass("A")); 322 | } 323 | 324 | @Test(expected = ExistingEntityException.class) 325 | public void learnFromOtherBrainErrorTest() throws BrainException { 326 | Brain brain = new Brain(); 327 | brain.addClass("http://example.org/A"); 328 | brain.addClass("B"); 329 | Brain brain1 = new Brain(); 330 | brain1.addClass("A"); 331 | brain1.learn(brain); 332 | } 333 | 334 | 335 | @Test 336 | public void getTopClass() throws BrainException { 337 | Brain brain = new Brain(); 338 | assertNotNull(brain.getOWLClass("Thing")); 339 | } 340 | 341 | 342 | @Test 343 | public void getUnstatisfiableClasses() throws BrainException { 344 | List unsatisfiableClasses = brain.getUnsatisfiableClasses(); 345 | assertEquals(1, unsatisfiableClasses.size()); 346 | assertEquals("R", unsatisfiableClasses.get(0)); 347 | } 348 | 349 | } 350 | -------------------------------------------------------------------------------- /src/test/java/uk/ac/ebi/BrainPopulationTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package uk.ac.ebi; 5 | 6 | import static org.junit.Assert.assertEquals; 7 | import static org.junit.Assert.assertNotNull; 8 | 9 | import java.io.File; 10 | import java.util.List; 11 | 12 | import org.junit.After; 13 | import org.junit.Before; 14 | import org.junit.Test; 15 | import org.semanticweb.owlapi.apibinding.OWLManager; 16 | import org.semanticweb.owlapi.model.IRI; 17 | import org.semanticweb.owlapi.model.OWLClass; 18 | import org.semanticweb.owlapi.model.OWLNamedIndividual; 19 | import org.semanticweb.owlapi.model.OWLOntology; 20 | import org.semanticweb.owlapi.model.OWLOntologyCreationException; 21 | import org.semanticweb.owlapi.model.OWLOntologyManager; 22 | 23 | import uk.ac.ebi.brain.core.Brain; 24 | import uk.ac.ebi.brain.error.BadNameException; 25 | import uk.ac.ebi.brain.error.BadPrefixException; 26 | import uk.ac.ebi.brain.error.BrainException; 27 | import uk.ac.ebi.brain.error.ClassExpressionException; 28 | import uk.ac.ebi.brain.error.DataPropertyExpressionException; 29 | import uk.ac.ebi.brain.error.ExistingClassException; 30 | import uk.ac.ebi.brain.error.ExistingDataPropertyException; 31 | import uk.ac.ebi.brain.error.ExistingEntityException; 32 | import uk.ac.ebi.brain.error.ExistingNamedIndividualException; 33 | import uk.ac.ebi.brain.error.ExistingObjectPropertyException; 34 | import uk.ac.ebi.brain.error.NewOntologyException; 35 | import uk.ac.ebi.brain.error.NonExistingEntityException; 36 | import uk.ac.ebi.brain.error.ObjectPropertyExpressionException; 37 | 38 | /** 39 | * @author Samuel Croset 40 | * 41 | */ 42 | public class BrainPopulationTest { 43 | 44 | Brain brain; 45 | 46 | @Before 47 | public void bootstrap() throws BrainException { 48 | brain = new Brain(); 49 | } 50 | 51 | @After 52 | public void dispose() { 53 | brain.sleep(); 54 | } 55 | 56 | @Test(expected = BadPrefixException.class) 57 | public void wrongPrefixTest() throws BrainException { 58 | brain = new Brain("htt://www.example.org/", "http://www.example.org/demo.owl"); 59 | } 60 | 61 | @Test 62 | public void addClassTest() throws BrainException{ 63 | brain.addClass("A"); 64 | assertEquals(true, brain.getOntology().containsClassInSignature(IRI.create(brain.getPrefixManager().getDefaultPrefix() + "A"))); 65 | } 66 | 67 | @Test 68 | public void addExternalClassTest() throws BrainException{ 69 | brain.addClass("http://www.example.org/A"); 70 | assertEquals(true, brain.getOntology().containsClassInSignature(IRI.create("http://www.example.org/A"))); 71 | assertNotNull(brain.getOWLClass("A")); 72 | } 73 | 74 | @Test(expected = BadNameException.class) 75 | public void addClassWithBadNameTest() throws BrainException { 76 | brain.addClass("Blood Coagulation"); 77 | } 78 | 79 | @Test(expected = ExistingClassException.class) 80 | public void addExistingClassTest() throws BrainException{ 81 | brain.addClass("A"); 82 | assertEquals(true, brain.getOntology().containsClassInSignature(IRI.create(brain.getPrefixManager().getDefaultPrefix() + "A"))); 83 | brain.addClass("A"); 84 | } 85 | 86 | @Test 87 | public void getOwlClassTest() throws BrainException { 88 | OWLClass owlClassA = brain.addClass("A"); 89 | OWLClass owlClassAprime = brain.getOWLClass("A"); 90 | assertEquals(owlClassA, owlClassAprime); 91 | } 92 | 93 | @Test(expected = NonExistingEntityException.class) 94 | public void getNonExsistingOwlClassTest() throws NonExistingEntityException { 95 | brain.getOWLClass("A"); 96 | } 97 | 98 | @Test 99 | public void subClassOfTest() throws BrainException { 100 | brain.addClass("A"); 101 | brain.addClass("B"); 102 | brain.subClassOf("A", "B"); 103 | } 104 | 105 | @Test 106 | public void equivalentClassesTest() throws BrainException{ 107 | brain.addClass("A"); 108 | brain.addClass("B"); 109 | brain.equivalentClasses("A", "B"); 110 | } 111 | 112 | @Test 113 | public void disjointClassesTest() throws BrainException{ 114 | brain.addClass("A"); 115 | brain.addClass("B"); 116 | brain.disjointClasses("A", "B"); 117 | } 118 | 119 | @Test(expected = ClassExpressionException.class) 120 | public void subClassOfErrorTest() throws ClassExpressionException{ 121 | brain.subClassOf("A", "C"); 122 | } 123 | 124 | @Test 125 | public void saveOntology() throws BrainException { 126 | brain.addClass("A"); 127 | brain.addClass("B"); 128 | brain.subClassOf("A", "B"); 129 | brain.save("src/test/resources/output.owl"); 130 | } 131 | 132 | @Test 133 | public void addExternalIndividualTest() throws BrainException { 134 | brain.addNamedIndividual("http://www.example.org/a"); 135 | assertEquals(true, brain.getOntology().containsIndividualInSignature(IRI.create("http://www.example.org/a"))); 136 | assertNotNull(brain.getOWLNamedIndividual("a")); 137 | } 138 | 139 | @Test(expected = BadNameException.class) 140 | public void addNamedIndividualWithBadNameTest() throws BrainException { 141 | brain.addClass("pouet pouet"); 142 | } 143 | 144 | @Test(expected = ExistingNamedIndividualException.class) 145 | public void addExistingNamedIndividualTest() throws BrainException{ 146 | brain.addNamedIndividual("a"); 147 | assertEquals(true, brain.getOntology().containsIndividualInSignature(IRI.create(brain.getPrefixManager().getDefaultPrefix() + "a"))); 148 | brain.addNamedIndividual("a"); 149 | } 150 | 151 | @Test 152 | public void getOwlNamedIndividualTest() throws BrainException { 153 | OWLNamedIndividual owlClassA = brain.addNamedIndividual("a"); 154 | OWLNamedIndividual owlClassAprime = brain.getOWLNamedIndividual("a"); 155 | assertEquals(owlClassA, owlClassAprime); 156 | } 157 | 158 | @Test(expected = NonExistingEntityException.class) 159 | public void getNonExsistingOwlNamedIndividualTest() throws NonExistingEntityException { 160 | brain.getOWLNamedIndividual("a"); 161 | } 162 | 163 | @Test 164 | public void individualAssertion() throws BrainException { 165 | Brain brain = new Brain(); 166 | brain.addNamedIndividual("Joe"); 167 | brain.addClass("Human"); 168 | brain.addClass("Scientist"); 169 | brain.type("Human and Scientist", "Joe"); 170 | brain.getInstances("Human", false); 171 | assertEquals(1, brain.getInstances("Human", false).size()); 172 | } 173 | 174 | @Test 175 | public void objectPropertyAssertionTest() throws BrainException { 176 | Brain brain = new Brain(); 177 | brain.addNamedIndividual("joe"); 178 | brain.addNamedIndividual("mary"); 179 | brain.addObjectProperty("knows"); 180 | brain.objectPropertyAssertion("joe", "knows", "mary"); 181 | List instances = brain.getInstances("knows value mary", false); 182 | assertEquals(1, instances.size()); 183 | brain.transitive("knows"); 184 | brain.addNamedIndividual("bob"); 185 | brain.objectPropertyAssertion("bob", "knows", "joe"); 186 | List instances2 = brain.getInstances("knows value mary", false); 187 | assertEquals(2, instances2.size()); 188 | } 189 | 190 | @Test 191 | public void objectPropertyTest() throws BrainException { 192 | brain.addObjectProperty("part-of"); 193 | assertEquals(true, brain.getOntology().containsObjectPropertyInSignature(IRI.create(brain.getPrefixManager().getDefaultPrefix() + "part-of"))); 194 | } 195 | 196 | @Test(expected = ExistingObjectPropertyException.class) 197 | public void redundantObjectPropertyTest() throws BrainException { 198 | brain.addObjectProperty("part-of"); 199 | assertEquals(true, brain.getOntology().containsObjectPropertyInSignature(IRI.create(brain.getPrefixManager().getDefaultPrefix() + "part-of"))); 200 | brain.addObjectProperty("part-of"); 201 | } 202 | 203 | @Test 204 | public void addExternalObjectPropertyTest() throws BrainException{ 205 | brain.addObjectProperty("http://www.example.org/part-of"); 206 | assertEquals(true, brain.getOntology().containsObjectPropertyInSignature(IRI.create("http://www.example.org/part-of"))); 207 | assertNotNull(brain.getOWLObjectProperty("part-of")); 208 | } 209 | 210 | @Test 211 | public void transitivePropertyTest() throws BrainException { 212 | brain.addObjectProperty("part-of"); 213 | brain.transitive("part-of"); 214 | brain.save("src/test/resources/output.owl"); 215 | } 216 | 217 | @Test(expected = ObjectPropertyExpressionException.class) 218 | public void unknownTransitivePropertyTest() throws BrainException { 219 | brain.addObjectProperty("part-of"); 220 | brain.transitive("regulates"); 221 | } 222 | 223 | @Test 224 | public void reflexivePropertyTest() throws BrainException { 225 | brain.addObjectProperty("part-of"); 226 | brain.reflexive("part-of"); 227 | brain.save("src/test/resources/output.owl"); 228 | } 229 | 230 | @Test 231 | public void dataPropertyTest() throws BrainException { 232 | brain.addDataProperty("has-age"); 233 | assertEquals(true, brain.getOntology().containsDataPropertyInSignature(IRI.create(brain.getPrefixManager().getDefaultPrefix() + "has-age"))); 234 | brain.save("src/test/resources/output.owl"); 235 | } 236 | 237 | @Test(expected = ExistingDataPropertyException.class) 238 | public void existingDataPropertyTest() throws BrainException { 239 | brain.addDataProperty("has-age"); 240 | brain.addDataProperty("has-age"); 241 | } 242 | 243 | @Test 244 | public void addExternalDataPropertyTest() throws BrainException{ 245 | brain.addDataProperty("http://www.example.org/has-age"); 246 | assertEquals(true, brain.getOntology().containsDataPropertyInSignature(IRI.create("http://www.example.org/has-age"))); 247 | assertNotNull(brain.getOWLDataProperty("has-age")); 248 | } 249 | 250 | @Test 251 | public void functionalDataPropertyTest() throws BrainException { 252 | brain.addDataProperty("has-age"); 253 | brain.functional("has-age"); 254 | } 255 | 256 | @Test(expected = DataPropertyExpressionException.class) 257 | public void functionalDataPropertyAssertionTest() throws BrainException{ 258 | brain.addObjectProperty("part-of"); 259 | brain.addDataProperty("has-age"); 260 | brain.functional("part-of"); 261 | } 262 | 263 | @Test 264 | public void domainTest() throws BrainException{ 265 | brain.addClass("A"); 266 | brain.addObjectProperty("part-of"); 267 | brain.domain("part-of", "A"); 268 | } 269 | 270 | @Test 271 | public void complexDomainTest() throws BrainException{ 272 | brain.addClass("A"); 273 | brain.addClass("B"); 274 | brain.addDataProperty("has-age"); 275 | brain.addObjectProperty("part-of"); 276 | brain.domain("has-age", "A and B"); 277 | brain.domain("part-of", "A and B"); 278 | brain.save("src/test/resources/output.owl"); 279 | } 280 | 281 | @Test 282 | public void rangeObjectPropertyTest() throws BrainException{ 283 | brain.addClass("A"); 284 | brain.addObjectProperty("part-of"); 285 | brain.range("part-of", "A"); 286 | } 287 | 288 | @Test 289 | public void rangeDataPropertyTest() throws BrainException { 290 | brain.addClass("A"); 291 | brain.addDataProperty("has-age"); 292 | brain.range("has-age", brain.INTEGER); 293 | brain.save("src/test/resources/output.owl"); 294 | } 295 | 296 | @Test 297 | public void equivalentObjectPropertiesTest() throws BrainException { 298 | brain.addObjectProperty("part-of"); 299 | brain.addObjectProperty("is-part-of"); 300 | brain.equivalentProperties("part-of", "is-part-of"); 301 | } 302 | 303 | @Test(expected = BrainException.class) 304 | public void wrongEquivalentObjectPropertiesTest() throws BrainException { 305 | brain.addObjectProperty("part-of"); 306 | brain.addObjectProperty("is-part-of"); 307 | brain.equivalentProperties("part-of", "isEEE-part-of"); 308 | } 309 | 310 | @Test 311 | public void equivalentDataPropertiesTest() throws BrainException { 312 | brain.addDataProperty("has-age"); 313 | brain.addDataProperty("number-of-years"); 314 | brain.equivalentProperties("has-age", "number-of-years"); 315 | } 316 | 317 | @Test(expected = BrainException.class) 318 | public void mixedEquivalentPropertiesTest() throws BrainException { 319 | brain.addDataProperty("has-age"); 320 | brain.addObjectProperty("part-of"); 321 | brain.equivalentProperties("has-age", "part-of"); 322 | } 323 | 324 | @Test 325 | public void subObjectPropertyTest() throws BrainException{ 326 | brain.addObjectProperty("positively-regulates"); 327 | brain.addObjectProperty("regulates"); 328 | brain.subPropertyOf("positively-regulates", "regulates"); 329 | brain.save("src/test/resources/output.owl"); 330 | } 331 | 332 | @Test 333 | public void subDataPropertyTest() throws BrainException{ 334 | brain.addDataProperty("positively-regulates"); 335 | brain.addDataProperty("regulates"); 336 | brain.subPropertyOf("positively-regulates", "regulates"); 337 | brain.save("src/test/resources/output.owl"); 338 | } 339 | 340 | @Test(expected = BrainException.class) 341 | public void corruptedSubPropertyAseertionTest() throws BrainException{ 342 | brain.addDataProperty("positively-regulates"); 343 | brain.addObjectProperty("regulates"); 344 | brain.subPropertyOf("positively-regulates", "regulates"); 345 | brain.save("src/test/resources/output.owl"); 346 | } 347 | 348 | @Test 349 | public void ChainedPropertiesTest() throws BrainException { 350 | brain.addObjectProperty("part-of"); 351 | brain.addObjectProperty("regulates"); 352 | brain.chain("regulates o part-of", "regulates"); 353 | brain.save("src/test/resources/output.owl"); 354 | } 355 | 356 | @Test(expected = ObjectPropertyExpressionException.class) 357 | public void wrongChainedPropertiesTest() throws BrainException { 358 | brain.addObjectProperty("part-of"); 359 | brain.addObjectProperty("regulates"); 360 | brain.chain("regulated o part-of", "regulates"); 361 | brain.save("src/test/resources/output.owl"); 362 | } 363 | 364 | @Test 365 | public void labelTest() throws BrainException { 366 | brain.addClass("A"); 367 | brain.label("A", "this is the content of the label"); 368 | brain.save("src/test/resources/output.owl"); 369 | } 370 | 371 | @Test 372 | public void externalAnnotationTest() throws BrainException { 373 | brain.addAnnotationProperty("http://example.org/definition"); 374 | brain.addClass("A"); 375 | brain.annotation("A", "definition", "this is the definition of the entity"); 376 | } 377 | 378 | @Test 379 | public void annotationTest() throws BrainException { 380 | brain.addAnnotationProperty("definition"); 381 | brain.addClass("A"); 382 | brain.annotation("A", "definition", "this is the definition of the entity"); 383 | brain.save("src/test/resources/output.owl"); 384 | } 385 | 386 | @Test(expected = NonExistingEntityException.class) 387 | public void wrongAnnotationTest() throws BrainException { 388 | brain.addAnnotationProperty("definition"); 389 | brain.annotation("A", "definitionCorrupted", "this is the definition of the entity"); 390 | } 391 | 392 | @Test 393 | public void builtInAnnotationTest() throws BrainException { 394 | brain.addClass("A"); 395 | brain.comment("A", "his is a comment"); 396 | brain.seeAlso("A", "see also there"); 397 | brain.isDefinedBy("A", "is defined by that"); 398 | brain.save("src/test/resources/output.owl"); 399 | } 400 | 401 | @Test 402 | public void prefixTest() throws BrainException { 403 | Brain brain = new Brain("http://www.example.com/", "http://www.example.com/example.owl"); 404 | brain.addClass("A"); 405 | brain.addClass("http://www.example.com/B"); 406 | brain.save("src/test/resources/prefix.owl"); 407 | } 408 | 409 | @Test 410 | public void contructorFromOntologyTest() throws OWLOntologyCreationException, ClassExpressionException, NewOntologyException, ExistingEntityException, BadPrefixException{ 411 | OWLOntologyManager man = OWLManager.createOWLOntologyManager(); 412 | OWLOntology ontology = man.loadOntologyFromOntologyDocument(new File("src/test/resources/dev.owl")); 413 | Brain brain = new Brain(ontology); 414 | List subClasses = brain.getSubClasses("G", false); 415 | brain.sleep(); 416 | assertEquals(3, subClasses.size()); 417 | } 418 | 419 | @Test 420 | public void addClassBivalentHandling() throws BrainException { 421 | Brain brain = new Brain(); 422 | brain.addClass("http://www.example.org/A"); 423 | brain.addClass("B"); 424 | brain.subClassOf("A", "B"); 425 | brain.prefix("http://www.example.org/", "example"); 426 | brain.save("src/test/resources/prefix.owl"); 427 | } 428 | 429 | 430 | } 431 | -------------------------------------------------------------------------------- /src/main/java/uk/ac/ebi/brain/core/Brain.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright [2013] EMBL - European Bioinformatics Institute 3 | * Licensed under the Apache License, Version 2.0 (the 4 | * "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of 6 | * the License at 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 | * KIND, either express or implied. See the License for the 12 | * specific language governing permissions and limitations 13 | * under the License. 14 | */ 15 | package uk.ac.ebi.brain.core; 16 | 17 | import java.io.File; 18 | import java.net.*; 19 | import java.util.*; 20 | 21 | import org.apache.log4j.Level; 22 | import org.apache.log4j.Logger; 23 | import org.coode.owlapi.manchesterowlsyntax.*; 24 | import org.semanticweb.elk.owlapi.*; 25 | import org.semanticweb.elk.reasoner.config.ReasonerConfiguration; 26 | import org.semanticweb.owlapi.apibinding.OWLManager; 27 | import org.semanticweb.owlapi.expression.*; 28 | import org.semanticweb.owlapi.model.*; 29 | import org.semanticweb.owlapi.profiles.*; 30 | import org.semanticweb.owlapi.reasoner.*; 31 | import org.semanticweb.owlapi.util.*; 32 | import org.semanticweb.owlapi.vocab.PrefixOWLOntologyFormat; 33 | 34 | import uk.ac.ebi.brain.error.*; 35 | 36 | /** 37 | * Facade class to manipulate the OWL-API. 38 | * The documentation is available at https://github.com/loopasam/Brain/wiki 39 | * @author Samuel Croset 40 | */ 41 | public class Brain { 42 | 43 | private OWLOntology ontology; 44 | private OWLReasoner reasoner; 45 | private OWLOntologyManager manager; 46 | private OWLDataFactory factory; 47 | private OWLReasonerFactory reasonerFactory; 48 | private BidirectionalShortFormProvider bidiShortFormProvider; 49 | private OWLEntityChecker entityChecker; 50 | private DefaultPrefixManager prefixManager; 51 | public OWLDatatype INTEGER; 52 | public OWLDatatype FLOAT; 53 | public OWLDatatype BOOLEAN; 54 | public static final String DEFAULT_PREFIX = "brain#"; 55 | private boolean isClassified; 56 | private ElkReasonerConfiguration configuration; 57 | 58 | public OWLOntology getOntology() { 59 | return ontology; 60 | } 61 | 62 | @Deprecated 63 | public void setOntology(OWLOntology ontology) { 64 | this.ontology = ontology; 65 | } 66 | public OWLReasoner getReasoner() { 67 | return reasoner; 68 | } 69 | 70 | @Deprecated 71 | public void setReasoner(OWLReasoner reasoner) { 72 | this.reasoner = reasoner; 73 | } 74 | public OWLOntologyManager getManager() { 75 | return manager; 76 | } 77 | 78 | @Deprecated 79 | public void setManager(OWLOntologyManager manager) { 80 | this.manager = manager; 81 | } 82 | public OWLDataFactory getFactory() { 83 | return factory; 84 | } 85 | 86 | @Deprecated 87 | public void setFactory(OWLDataFactory factory) { 88 | this.factory = factory; 89 | } 90 | public OWLReasonerFactory getReasonerFactory() { 91 | return reasonerFactory; 92 | } 93 | 94 | @Deprecated 95 | public void setReasonerFactory(OWLReasonerFactory reasonerFactory) { 96 | this.reasonerFactory = reasonerFactory; 97 | } 98 | public BidirectionalShortFormProvider getBidiShortFormProvider() { 99 | return bidiShortFormProvider; 100 | } 101 | 102 | @Deprecated 103 | public void setBidiShortFormProvider(BidirectionalShortFormProvider bidiShortFormProvider) { 104 | this.bidiShortFormProvider = bidiShortFormProvider; 105 | } 106 | public OWLEntityChecker getEntityChecker() { 107 | return entityChecker; 108 | } 109 | 110 | @Deprecated 111 | public void setEntityChecker(OWLEntityChecker entityChecker) { 112 | this.entityChecker = entityChecker; 113 | } 114 | 115 | @Deprecated 116 | public void setPrefixManager(DefaultPrefixManager prefixManager) { 117 | this.prefixManager = prefixManager; 118 | } 119 | public PrefixManager getPrefixManager() { 120 | return prefixManager; 121 | } 122 | 123 | @Deprecated 124 | public void setClassified(boolean isClassified) { 125 | this.isClassified = isClassified; 126 | } 127 | public boolean isClassified() { 128 | return isClassified; 129 | } 130 | 131 | /** 132 | * Creates a Brain instance with the specified prefix and ontology IRI. 133 | * The OWL entities created will inherit from this prefix. 134 | * The number of working threads for reasoning is set to the maximum (default). 135 | * @param prefix 136 | * @param ontologyIri 137 | * @throws BadPrefixException 138 | * @throws NewOntologyException 139 | */ 140 | public Brain(String prefix, String ontologyIri) throws NewOntologyException, BadPrefixException { 141 | this(prefix, ontologyIri, -1); 142 | } 143 | 144 | /** 145 | * Creates a Brain instance with the a default prefix () and ontology IRI (). 146 | * The number of working threads for reasoning is set to the maximum (default). 147 | * @throws BadPrefixException 148 | * @throws NewOntologyException 149 | */ 150 | public Brain() throws NewOntologyException, BadPrefixException { 151 | this(DEFAULT_PREFIX, "brain.owl", -1); 152 | } 153 | 154 | /** 155 | * Creates a Brain instance from an ontology object. 156 | * This method is useful for people with a pre-loaded ontology in memory and willing 157 | * to quickly create a Brain instance from it. 158 | * The default prefix () and ontology IRI () are used. 159 | * The number of working threads for reasoning is set to the maximum (default). 160 | * @param ontology 161 | * @throws BadPrefixException 162 | * @throws NewOntologyException 163 | */ 164 | public Brain(OWLOntology ontology) throws NewOntologyException { 165 | 166 | this.prefixManager = new DefaultPrefixManager(DEFAULT_PREFIX); 167 | 168 | this.manager = ontology.getOWLOntologyManager(); 169 | this.ontology = ontology; 170 | 171 | this.factory = this.manager.getOWLDataFactory(); 172 | 173 | ShortFormProvider shortFormProvider = new SimpleShortFormProvider(); 174 | Set importsClosure = this.ontology.getImportsClosure(); 175 | this.bidiShortFormProvider = new BidirectionalShortFormProviderAdapter(this.manager, importsClosure, shortFormProvider); 176 | this.entityChecker = new ShortFormEntityChecker(this.bidiShortFormProvider); 177 | 178 | Logger.getLogger("org.semanticweb.elk").setLevel(Level.OFF); 179 | 180 | this.reasonerFactory = new ElkReasonerFactory(); 181 | this.reasoner = this.getReasonerFactory().createReasoner(this.ontology); 182 | 183 | this.isClassified = false; 184 | 185 | this.INTEGER = this.factory.getIntegerOWLDatatype(); 186 | this.FLOAT = this.factory.getFloatOWLDatatype(); 187 | this.BOOLEAN = this.factory.getBooleanOWLDatatype(); 188 | update(); 189 | try { 190 | this.addClass("http://www.w3.org/2002/07/owl#Thing"); 191 | } catch (BrainException e) { 192 | //If the class throws an error it's no big deal. 193 | //It means that the top class was already present in the loaded 194 | //ontology 195 | } 196 | } 197 | 198 | 199 | /** 200 | * Creates a Brain instance with the specified prefix and ontology IRI. 201 | * The OWL entities created will inherit from this prefix. 202 | * The number of working threads for reasoning is set to the passed value (numberOfWorkers parameter). 203 | * @param prefix 204 | * @param ontologyIri 205 | * @param numberOfWorkers 206 | * @throws BadPrefixException 207 | * @throws NewOntologyException 208 | */ 209 | public Brain(String prefix, String ontologyIri, int numberOfWorkers) throws NewOntologyException, BadPrefixException { 210 | 211 | if(!prefix.equals(DEFAULT_PREFIX)){ 212 | try { 213 | @SuppressWarnings("unused") 214 | URL url = new URL(prefix); 215 | } catch (MalformedURLException e) { 216 | throw new BadPrefixException(e); 217 | } 218 | if(!(prefix.endsWith("#") || prefix.endsWith("/"))){ 219 | throw new BadPrefixException("The separator symbol must either be '/' or '#'"); 220 | } 221 | } 222 | 223 | this.prefixManager = new DefaultPrefixManager(prefix); 224 | this.manager = OWLManager.createOWLOntologyManager(); 225 | this.factory = manager.getOWLDataFactory(); 226 | try { 227 | this.ontology = manager.createOntology(IRI.create(ontologyIri)); 228 | } catch (OWLOntologyCreationException e) { 229 | throw new NewOntologyException(e); 230 | } 231 | 232 | ShortFormProvider shortFormProvider = new SimpleShortFormProvider(); 233 | Set importsClosure = this.ontology.getImportsClosure(); 234 | this.bidiShortFormProvider = new BidirectionalShortFormProviderAdapter(this.manager, importsClosure, shortFormProvider); 235 | this.entityChecker = new ShortFormEntityChecker(this.bidiShortFormProvider); 236 | 237 | Logger.getLogger("org.semanticweb.elk").setLevel(Level.OFF); 238 | 239 | if(numberOfWorkers != -1){ 240 | this.configuration = new ElkReasonerConfiguration(); 241 | this.configuration.getElkConfiguration().setParameter(ReasonerConfiguration.NUM_OF_WORKING_THREADS, Integer.toString(numberOfWorkers)); 242 | this.reasonerFactory = new ElkReasonerFactory(); 243 | this.reasoner = this.getReasonerFactory().createReasoner(this.ontology, this.configuration); 244 | }else{ 245 | this.reasonerFactory = new ElkReasonerFactory(); 246 | this.reasoner = this.getReasonerFactory().createReasoner(this.ontology); 247 | } 248 | 249 | this.isClassified = false; 250 | 251 | this.INTEGER = this.factory.getIntegerOWLDatatype(); 252 | this.FLOAT = this.factory.getFloatOWLDatatype(); 253 | this.BOOLEAN = this.factory.getBooleanOWLDatatype(); 254 | update(); 255 | try { 256 | this.addClass("http://www.w3.org/2002/07/owl#Thing"); 257 | } catch (BrainException e) { 258 | e.printStackTrace(); 259 | } 260 | } 261 | 262 | /** 263 | * Declare a prefix mapping. Will be used when the ontology is serialized 264 | * (.owl file). Example of usage: 265 | * 266 | *
 267 | 	 * {@code
 268 | 	 * Brain brain = new Brain();
 269 | 	 * brain.prefix("http://www.example.org/", "example");
 270 | 	 * }
 271 | 	 * 
272 | *
273 | * @param longForm 274 | * @param abbreviation 275 | */ 276 | public void prefix(String longForm, String abbreviation) { 277 | this.prefixManager.setPrefix(abbreviation + ":", longForm); 278 | } 279 | 280 | /** 281 | * Add an OWL class to the ontology. An OWLClass 282 | * object is returned. The input value can either be 283 | * the short form of the class or the full IRI. Examples of input 284 | * parameter: "http://example.org/MyClass" or "MyClass". 285 | * @param className 286 | * @return owlClass 287 | * @throws ExistingClassException 288 | * @throws BadNameException 289 | */ 290 | public OWLClass addClass(String className) throws BadNameException, ExistingClassException { 291 | if(isExternalEntity(className)){ 292 | return addExternalClass(className); 293 | }else{ 294 | try { 295 | this.getOWLClass(className); 296 | throw new ExistingClassException("The class '"+ className +"' already exists."); 297 | } catch (NonExistingClassException e) { 298 | validate(className); 299 | OWLClass owlClass = null; 300 | try{ 301 | owlClass = this.factory.getOWLClass(className, this.prefixManager); 302 | } catch(RuntimeException re) { 303 | throw new BadNameException("The prefix you are using is not recognized. Use either no prefix or a valid URI. More info: " + re); 304 | } 305 | declare(owlClass); 306 | update(); 307 | return owlClass; 308 | } 309 | } 310 | } 311 | 312 | /** 313 | * Add an OWL class to the ontology. The class refers to an external class (external ontology). 314 | * @param className 315 | * @return owlClass 316 | * @throws ExistingClassException 317 | * @throws BadNameException 318 | */ 319 | private OWLClass addExternalClass(String className) throws ExistingClassException, BadNameException { 320 | if(this.ontology.containsClassInSignature(IRI.create(className))){ 321 | throw new ExistingClassException("The class already '"+ className +"' already exists."); 322 | } 323 | validateExternalEntity(className); 324 | OWLClass owlClass = this.factory.getOWLClass(IRI.create(className)); 325 | SimpleShortFormProvider shortFormProvider = new SimpleShortFormProvider(); 326 | String sf = shortFormProvider.getShortForm(owlClass); 327 | try { 328 | this.getOWLClass(sf); 329 | removeExternalClass(className); 330 | throw new ExistingClassException("A class as already the short form '"+ sf +"'. A class name as to be unique."); 331 | } catch (NonExistingClassException e) { 332 | declare(owlClass); 333 | update(); 334 | return owlClass; 335 | } 336 | } 337 | 338 | /** 339 | * Check whether an entity is an external entity (URL present) or not. 340 | * @param entityName 341 | * @return isExternalEntity 342 | */ 343 | private boolean isExternalEntity(String entityName) { 344 | try { 345 | new URL(entityName); 346 | return true; 347 | } catch (MalformedURLException e) { 348 | return false; 349 | } 350 | } 351 | 352 | /** 353 | * Remove an OWL class from the ontology. The class refers to an external class (external ontology). 354 | * @param externalClass 355 | */ 356 | private void removeExternalClass(String externalClass) { 357 | OWLEntityRemover remover = new OWLEntityRemover(this.manager, Collections.singleton(this.ontology)); 358 | OWLClass owlClassToRemove = this.factory.getOWLClass(IRI.create(externalClass)); 359 | owlClassToRemove.accept(remover); 360 | this.manager.applyChanges(remover.getChanges()); 361 | remover.reset(); 362 | update(); 363 | } 364 | 365 | /** 366 | * Declare (register) an OWL entity in the ontology. 367 | * @param owlEntity 368 | */ 369 | private void declare(OWLEntity owlEntity) { 370 | OWLDeclarationAxiom declarationAxiom = this.factory.getOWLDeclarationAxiom(owlEntity); 371 | manager.addAxiom(this.ontology, declarationAxiom); 372 | } 373 | 374 | /** 375 | * Validate an OWL entity. 376 | * @param entityName 377 | * @throws BadNameException 378 | */ 379 | private void validate(String entityName) throws BadNameException { 380 | URL url; 381 | String prefix = null; 382 | if(this.prefixManager.getDefaultPrefix().equals(DEFAULT_PREFIX)){ 383 | prefix = "http://localhost/"; 384 | }else{ 385 | prefix = this.prefixManager.getDefaultPrefix(); 386 | } 387 | try { 388 | url = new URL(prefix + entityName); 389 | url.toURI(); 390 | } catch (MalformedURLException e) { 391 | throw new BadNameException(e); 392 | } catch (URISyntaxException e) { 393 | throw new BadNameException("'"+entityName+"' is not valid valid name for an OWL entity. Use only characters that are valid for a URI (no space, etc...)."); 394 | } 395 | } 396 | 397 | /** 398 | * Add an OWL named individual to the ontology. An OWLNamedIndividual 399 | * object is returned. The input value can either be 400 | * the short form of the property or the full IRI. Examples of input 401 | * parameter: "http://example.org/MyIndividual" or "MyIndividual". 402 | * @param individualName 403 | * @return owlNamedIndividual 404 | * @throws BadNameException 405 | * @throws ExistingNamedIndividualException 406 | */ 407 | public OWLNamedIndividual addNamedIndividual(String individualName) throws BadNameException, ExistingNamedIndividualException { 408 | if(isExternalEntity(individualName)){ 409 | return addExternalIndividual(individualName); 410 | }else{ 411 | try { 412 | this.getOWLNamedIndividual(individualName); 413 | throw new ExistingNamedIndividualException("The named individual '"+ individualName +"' already exists."); 414 | } catch (NonExistingNamedIndividualException e) { 415 | validate(individualName); 416 | OWLNamedIndividual owlNamedIndividual = null; 417 | try{ 418 | owlNamedIndividual = this.factory.getOWLNamedIndividual(individualName, this.prefixManager); 419 | } catch(RuntimeException re) { 420 | throw new BadNameException("The prefix you are using is not recognized. Use either no prefix or a valid URI. More info: " + re); 421 | } 422 | declare(owlNamedIndividual); 423 | update(); 424 | return owlNamedIndividual; 425 | } 426 | } 427 | } 428 | 429 | /** 430 | * Add an external named individual to the ontology. 431 | * @param individualName 432 | * @return owlNamedIndividual 433 | * @throws ExistingNamedIndividualException 434 | * @throws BadNameException 435 | */ 436 | private OWLNamedIndividual addExternalIndividual(String individualName) throws ExistingNamedIndividualException, BadNameException { 437 | if(this.ontology.containsIndividualInSignature(IRI.create(individualName))){ 438 | throw new ExistingNamedIndividualException("The named individual '"+ individualName +"' already exists."); 439 | } 440 | validateExternalEntity(individualName); 441 | OWLNamedIndividual owlNamedIndividual = this.factory.getOWLNamedIndividual(IRI.create(individualName)); 442 | SimpleShortFormProvider shortFormProvider = new SimpleShortFormProvider(); 443 | String sf = shortFormProvider.getShortForm(owlNamedIndividual); 444 | try { 445 | this.getOWLNamedIndividual(sf); 446 | removeExternalNamedIndividual(individualName); 447 | throw new ExistingNamedIndividualException("A named individual as already the short form '"+ sf +"'. The short form cannot be re-used name as to be unique."); 448 | } catch (NonExistingNamedIndividualException e) { 449 | declare(owlNamedIndividual); 450 | update(); 451 | return owlNamedIndividual; 452 | } 453 | } 454 | 455 | /** 456 | * Remove an external OWL named individual from the ontology. 457 | * @param externalIndividual 458 | */ 459 | private void removeExternalNamedIndividual(String externalIndividual) { 460 | OWLEntityRemover remover = new OWLEntityRemover(this.manager, Collections.singleton(this.ontology)); 461 | OWLNamedIndividual owlNamedIndividualToRemove = this.factory.getOWLNamedIndividual(IRI.create(externalIndividual)); 462 | owlNamedIndividualToRemove.accept(remover); 463 | this.manager.applyChanges(remover.getChanges()); 464 | remover.reset(); 465 | update(); 466 | } 467 | 468 | /** 469 | * Validate an external OWL entity. 470 | * @param entityIri 471 | * @throws BadNameException 472 | */ 473 | private void validateExternalEntity(String entityIri) throws BadNameException { 474 | URL url; 475 | try { 476 | url = new URL(entityIri); 477 | url.toURI(); 478 | } catch (MalformedURLException e) { 479 | throw new BadNameException(e); 480 | } catch (URISyntaxException e) { 481 | throw new BadNameException("'"+entityIri+"' is not valid valid name for an OWL entity. Use only characters that are valid for a URI (no space, etc...)."); 482 | } 483 | } 484 | 485 | /** 486 | * Add the axiom to the ontology. 487 | * @param owlAxiom 488 | */ 489 | private void addAxiom(OWLAxiom owlAxiom) { 490 | AddAxiom addAx = new AddAxiom(this.ontology, owlAxiom); 491 | this.manager.applyChange(addAx); 492 | update(); 493 | } 494 | 495 | /** 496 | * Add an OWL object property to the ontology. An OWLObjectProperty 497 | * object is returned. The input value can either be 498 | * the short form of the property or the full IRI. Examples of input 499 | * parameter: "http://example.org/MyProperty" or "MyProperty". 500 | * @param objectPropertyName 501 | * @return owlObjectProperty 502 | * @throws ExistingObjectPropertyException 503 | * @throws BadNameException 504 | */ 505 | public OWLObjectProperty addObjectProperty(String objectPropertyName) throws ExistingObjectPropertyException, BadNameException { 506 | if(isExternalEntity(objectPropertyName)){ 507 | return addExternalObjectProperty(objectPropertyName); 508 | }else{ 509 | try { 510 | this.getOWLObjectProperty(objectPropertyName); 511 | throw new ExistingObjectPropertyException("The object property '"+ objectPropertyName +"' already exists."); 512 | } catch (NonExistingObjectPropertyException e) { 513 | validate(objectPropertyName); 514 | OWLObjectProperty owlObjectProperty = null; 515 | try{ 516 | owlObjectProperty = this.factory.getOWLObjectProperty(objectPropertyName, this.prefixManager); 517 | } catch(RuntimeException re) { 518 | throw new BadNameException("The prefix you are using is not recognized. Use either no prefix or a valid URI. More info: " + re); 519 | } 520 | declare(owlObjectProperty); 521 | update(); 522 | return owlObjectProperty; 523 | } 524 | } 525 | } 526 | 527 | /** 528 | * Add an external OWL object property to the ontology. 529 | * @param objectPropertyName 530 | * @return owlObjectProperty 531 | * @throws ExistingObjectPropertyException 532 | * @throws BadNameException 533 | */ 534 | private OWLObjectProperty addExternalObjectProperty(String objectPropertyName) throws BadNameException, ExistingObjectPropertyException { 535 | if(this.ontology.containsObjectPropertyInSignature(IRI.create(objectPropertyName))){ 536 | throw new ExistingObjectPropertyException("The object property already '"+ objectPropertyName +"' already exists."); 537 | } 538 | validateExternalEntity(objectPropertyName); 539 | OWLObjectProperty owlObjectProperty = this.factory.getOWLObjectProperty(IRI.create(objectPropertyName)); 540 | SimpleShortFormProvider shortFormProvider = new SimpleShortFormProvider(); 541 | String sf = shortFormProvider.getShortForm(owlObjectProperty); 542 | try { 543 | this.getOWLObjectProperty(sf); 544 | removeExternalObjectProperty(objectPropertyName); 545 | throw new ExistingObjectPropertyException("An object property as already the short form '"+ sf +"'. The short form cannot be re-used name as to be unique."); 546 | } catch (NonExistingObjectPropertyException e) { 547 | declare(owlObjectProperty); 548 | update(); 549 | return owlObjectProperty; 550 | } 551 | } 552 | 553 | /** 554 | * Remove an external OWL object property to the ontology. 555 | * @param externalObjectProperty 556 | */ 557 | private void removeExternalObjectProperty(String externalObjectProperty) { 558 | OWLEntityRemover remover = new OWLEntityRemover(this.manager, Collections.singleton(this.ontology)); 559 | OWLObjectProperty owlObjectPropertyToRemove = this.factory.getOWLObjectProperty(IRI.create(externalObjectProperty)); 560 | owlObjectPropertyToRemove.accept(remover); 561 | this.manager.applyChanges(remover.getChanges()); 562 | remover.reset(); 563 | update(); 564 | } 565 | 566 | /** 567 | * Add an OWL data property to the ontology. An OWLDataProperty 568 | * object is returned. The input value can either be 569 | * the short form of the property or the full IRI. Examples of input 570 | * parameter: "http://example.org/MyProperty" or "MyProperty". 571 | * @param dataPropertyName 572 | * @return owlDataProperty 573 | * @throws ExistingDataPropertyException 574 | * @throws BadNameException 575 | */ 576 | public OWLDataProperty addDataProperty(String dataPropertyName) throws ExistingDataPropertyException, BadNameException { 577 | if(isExternalEntity(dataPropertyName)){ 578 | return addExternalDataProperty(dataPropertyName); 579 | }else{ 580 | try { 581 | this.getOWLDataProperty(dataPropertyName); 582 | throw new ExistingDataPropertyException("The data property '"+ dataPropertyName +"' already exists."); 583 | } catch (NonExistingDataPropertyException e) { 584 | validate(dataPropertyName); 585 | OWLDataProperty owlDataProperty = null; 586 | try{ 587 | owlDataProperty = this.factory.getOWLDataProperty(dataPropertyName, this.prefixManager); 588 | } catch(RuntimeException re) { 589 | throw new BadNameException("The prefix you are using is not recognized. Use either no prefix or a valid URI. More info: " + re); 590 | } 591 | declare(owlDataProperty); 592 | update(); 593 | return owlDataProperty; 594 | } 595 | } 596 | } 597 | 598 | /** 599 | * Add an external OWL data property to the ontology. 600 | * @param dataPropertyName 601 | * @return owlDataProperty 602 | * @throws ExistingDataPropertyException 603 | * @throws BadNameException 604 | */ 605 | private OWLDataProperty addExternalDataProperty(String dataPropertyName) throws BadNameException, ExistingDataPropertyException { 606 | if(this.ontology.containsDataPropertyInSignature(IRI.create(dataPropertyName))){ 607 | throw new ExistingDataPropertyException("The data property already '"+ dataPropertyName +"' already exists."); 608 | } 609 | validateExternalEntity(dataPropertyName); 610 | OWLDataProperty owlDataProperty = this.factory.getOWLDataProperty(IRI.create(dataPropertyName)); 611 | SimpleShortFormProvider shortFormProvider = new SimpleShortFormProvider(); 612 | String sf = shortFormProvider.getShortForm(owlDataProperty); 613 | try { 614 | this.getOWLDataProperty(sf); 615 | removeExternalDataProperty(dataPropertyName); 616 | throw new ExistingDataPropertyException("An entity as already the short form '"+ sf +"'. The short form cannot be re-used name as to be unique."); 617 | } catch (NonExistingDataPropertyException e) { 618 | declare(owlDataProperty); 619 | update(); 620 | return owlDataProperty; 621 | } 622 | } 623 | 624 | /** 625 | * Remove an external OWL data property to the ontology. 626 | * @param externalDataProperty 627 | */ 628 | private void removeExternalDataProperty(String externalDataProperty) { 629 | OWLEntityRemover remover = new OWLEntityRemover(this.manager, Collections.singleton(this.ontology)); 630 | OWLDataProperty owlDataPropertyToRemove = this.factory.getOWLDataProperty(IRI.create(externalDataProperty)); 631 | owlDataPropertyToRemove.accept(remover); 632 | this.manager.applyChanges(remover.getChanges()); 633 | remover.reset(); 634 | update(); 635 | } 636 | 637 | /** 638 | * Add an OWL annotation property to the ontology. An OWLAnnotationProperty 639 | * object is returned. The input value can either be 640 | * the short form of the property or the full IRI. Examples of input 641 | * parameter: "http://example.org/MyProperty" or "MyProperty". 642 | * @param annotationProperty 643 | * @return owlAnnotationProperty 644 | * @throws ExistingDataPropertyException 645 | * @throws BadNameException 646 | */ 647 | public OWLAnnotationProperty addAnnotationProperty(String annotationProperty) throws ExistingAnnotationPropertyException, BadNameException { 648 | if(isExternalEntity(annotationProperty)){ 649 | return addExternalAnnotationProperty(annotationProperty); 650 | }else{ 651 | try { 652 | this.getOWLAnnotationProperty(annotationProperty); 653 | throw new ExistingAnnotationPropertyException("The annotation property '"+ annotationProperty +"' already exists."); 654 | } catch (NonExistingAnnotationPropertyException e) { 655 | validate(annotationProperty); 656 | OWLAnnotationProperty owlAnnotationProperty = null; 657 | try{ 658 | owlAnnotationProperty = this.factory.getOWLAnnotationProperty(annotationProperty, this.prefixManager); 659 | } catch(RuntimeException re) { 660 | throw new BadNameException("The prefix you are using is not recognized. Use either no prefix or a valid URI. More info: " + re); 661 | } 662 | declare(owlAnnotationProperty); 663 | update(); 664 | return owlAnnotationProperty; 665 | } 666 | } 667 | } 668 | 669 | /** 670 | * Add an external OWL annotation property to the ontology. 671 | * @param annotationPropertyName 672 | * @return owlAnnotationProperty 673 | * @throws ExistingAnnotationPropertyException 674 | * @throws BadNameException 675 | */ 676 | private OWLAnnotationProperty addExternalAnnotationProperty(String annotationPropertyName) throws BadNameException, ExistingAnnotationPropertyException { 677 | if(this.ontology.containsAnnotationPropertyInSignature(IRI.create(annotationPropertyName))){ 678 | throw new ExistingAnnotationPropertyException("The annotation property already '"+ annotationPropertyName +"' already exists."); 679 | } 680 | validateExternalEntity(annotationPropertyName); 681 | OWLAnnotationProperty owlAnnotationProperty = this.factory.getOWLAnnotationProperty(IRI.create(annotationPropertyName)); 682 | SimpleShortFormProvider shortFormProvider = new SimpleShortFormProvider(); 683 | String sf = shortFormProvider.getShortForm(owlAnnotationProperty); 684 | try { 685 | this.getOWLAnnotationProperty(sf); 686 | removeExternalAnnotationProperty(annotationPropertyName); 687 | throw new ExistingAnnotationPropertyException("An entity as already the short form '"+ sf +"'. The short form cannot be re-used name as to be unique."); 688 | } catch (NonExistingAnnotationPropertyException e) { 689 | declare(owlAnnotationProperty); 690 | update(); 691 | return owlAnnotationProperty; 692 | } 693 | } 694 | 695 | /** 696 | * Remove an external OWL annotation property to the ontology. 697 | * @param externalAnnotationProperty 698 | */ 699 | private void removeExternalAnnotationProperty(String externalAnnotationProperty) { 700 | OWLEntityRemover remover = new OWLEntityRemover(this.manager, Collections.singleton(this.ontology)); 701 | OWLAnnotationProperty owlAnnotationPropertyToRemove = this.factory.getOWLAnnotationProperty(IRI.create(externalAnnotationProperty)); 702 | owlAnnotationPropertyToRemove.accept(remover); 703 | this.manager.applyChanges(remover.getChanges()); 704 | remover.reset(); 705 | update(); 706 | } 707 | 708 | /** 709 | * Set the reasoner is the not classified state and flush it. 710 | * A classification will have to be redone. 711 | */ 712 | private void update() { 713 | this.reasoner.flush(); 714 | this.isClassified = false; 715 | } 716 | 717 | /** 718 | * Returns the OWL class corresponding to the input string (short form). 719 | * @param className 720 | * @return owlClass 721 | * @throws NonExistingClassException 722 | */ 723 | public OWLClass getOWLClass(String className) throws NonExistingClassException { 724 | OWLEntity entity = this.bidiShortFormProvider.getEntity(className); 725 | if(entity != null && entity.isOWLClass()){ 726 | return (OWLClass) entity; 727 | }else{ 728 | throw new NonExistingClassException("The entity '"+ className +"' does not exist or is not an OWL class."); 729 | } 730 | } 731 | 732 | /** 733 | * Returns the named individual corresponding to the input string (short form). 734 | * @param individualName 735 | * @return namedIndividual 736 | * @throws NonExistingNamedIndividualException 737 | */ 738 | public OWLNamedIndividual getOWLNamedIndividual(String individualName) throws NonExistingNamedIndividualException { 739 | OWLEntity entity = this.bidiShortFormProvider.getEntity(individualName); 740 | if(entity != null && entity.isOWLNamedIndividual()){ 741 | return (OWLNamedIndividual) entity; 742 | }else{ 743 | throw new NonExistingNamedIndividualException("The entity '"+ individualName +"' does not exist or is not an OWL named individual."); 744 | } 745 | } 746 | 747 | /** 748 | * Returns the OWL annotation property corresponding to the input string (short form). 749 | * @param propertyName 750 | * @return owlAnnotationProperty 751 | * @throws NonExistingAnnotationPropertyException 752 | */ 753 | public OWLAnnotationProperty getOWLAnnotationProperty(String propertyName) throws NonExistingAnnotationPropertyException { 754 | OWLEntity entity = this.bidiShortFormProvider.getEntity(propertyName); 755 | if(entity != null && entity.isOWLAnnotationProperty()){ 756 | return (OWLAnnotationProperty) entity; 757 | }else{ 758 | throw new NonExistingAnnotationPropertyException("The annotation property '"+ propertyName +"' does not exist."); 759 | } 760 | } 761 | 762 | /** 763 | * Returns the OWL object property corresponding to the input string (short form). 764 | * @param objectPropertyName 765 | * @return owlObjectProperty 766 | * @throws NonExistingObjectPropertyException 767 | */ 768 | public OWLObjectProperty getOWLObjectProperty(String objectPropertyName) throws NonExistingObjectPropertyException { 769 | OWLEntity entity = this.bidiShortFormProvider.getEntity(objectPropertyName); 770 | if(entity != null && entity.isOWLObjectProperty()){ 771 | return (OWLObjectProperty) entity; 772 | }else{ 773 | throw new NonExistingObjectPropertyException("The object property '"+ objectPropertyName +"' does not exist."); 774 | } 775 | } 776 | 777 | /** 778 | * Returns the OWL data property corresponding to the input string (short form). 779 | * @param dataPropertyName 780 | * @return owlDataProperty 781 | * @throws NonExistingDataPropertyException 782 | */ 783 | public OWLDataProperty getOWLDataProperty(String dataPropertyName) throws NonExistingDataPropertyException { 784 | OWLEntity entity = this.bidiShortFormProvider.getEntity(dataPropertyName); 785 | if(entity != null && entity.isOWLDataProperty()){ 786 | return (OWLDataProperty) entity; 787 | }else{ 788 | throw new NonExistingDataPropertyException("The data property '"+ dataPropertyName +"' does not exist."); 789 | } 790 | } 791 | 792 | /** 793 | * Declare a subClassOf axiom. 794 | * @param subClass 795 | * @param superClass 796 | * @throws ClassExpressionException 797 | */ 798 | public void subClassOf(String subClass, String superClass) throws ClassExpressionException { 799 | OWLClassExpression subClassExpression = parseClassExpression(subClass); 800 | OWLClassExpression superClassExpression = parseClassExpression(superClass); 801 | OWLSubClassOfAxiom subClassAxiom = this.factory.getOWLSubClassOfAxiom(subClassExpression, superClassExpression); 802 | addAxiom(subClassAxiom); 803 | } 804 | 805 | /** 806 | * Declare an equivalence axiom. 807 | * @param class1 808 | * @param class2 809 | * @throws ClassExpressionException 810 | */ 811 | public void equivalentClasses(String class1, String class2) throws ClassExpressionException { 812 | OWLClassExpression classExpression1 = parseClassExpression(class1); 813 | OWLClassExpression classExpression2 = parseClassExpression(class2); 814 | OWLEquivalentClassesAxiom equivalentClassAxiom = this.factory.getOWLEquivalentClassesAxiom(classExpression1, classExpression2); 815 | addAxiom(equivalentClassAxiom); 816 | } 817 | 818 | /** 819 | * Declare an disjoint axiom. 820 | * @param class1 821 | * @param class2 822 | * @throws ClassExpressionException 823 | */ 824 | public void disjointClasses(String class1, String class2) throws ClassExpressionException { 825 | OWLClassExpression classExpression1 = parseClassExpression(class1); 826 | OWLClassExpression classExpression2 = parseClassExpression(class2); 827 | OWLDisjointClassesAxiom disjointClassAxiom = this.factory.getOWLDisjointClassesAxiom(classExpression1, classExpression2); 828 | addAxiom(disjointClassAxiom); 829 | } 830 | 831 | /** 832 | * Assert a type to an individual (class assertion). 833 | * @param classExpression 834 | * @param individualName 835 | * @throws ClassExpressionException 836 | * @throws NamedIndividualException 837 | */ 838 | public void type(String classExpression, String individualName) throws ClassExpressionException, NamedIndividualException { 839 | OWLClassExpression owlClassExpression = parseClassExpression(classExpression); 840 | OWLNamedIndividual owlIndividual = parseNamedIndividual(individualName); 841 | OWLClassAssertionAxiom axiom = this.factory.getOWLClassAssertionAxiom(owlClassExpression, owlIndividual); 842 | addAxiom(axiom); 843 | } 844 | 845 | /** 846 | * Assert a relation between two individuals (triple or fact). 847 | * @param subjectIndividual 848 | * @param objectProperty 849 | * @param objectIndividual 850 | * @throws ObjectPropertyExpressionException 851 | * @throws NamedIndividualException 852 | */ 853 | public void objectPropertyAssertion(String subjectIndividual, String objectProperty, String objectIndividual) throws ObjectPropertyExpressionException, NamedIndividualException { 854 | OWLObjectPropertyExpression owlObjectProperty = parseObjectPropertyExpression(objectProperty); 855 | OWLNamedIndividual owlSubjectIndividual = parseNamedIndividual(subjectIndividual); 856 | OWLNamedIndividual owlObjectIndividual = parseNamedIndividual(objectIndividual); 857 | OWLObjectPropertyAssertionAxiom axiom = this.factory.getOWLObjectPropertyAssertionAxiom(owlObjectProperty, owlSubjectIndividual, owlObjectIndividual); 858 | addAxiom(axiom); 859 | } 860 | 861 | //TODO implement axiom same individual once supported by ELK 862 | //TODO implement axiom different individual once supported by ELK 863 | //TODO implement axiom data property assertion once supported by ELK 864 | //TODO implement axiom negative object property assertion once supported by ELK 865 | //TODO implement axiom negative data property assertion once supported by ELK 866 | 867 | 868 | /** 869 | * Declare an transitive axiom. 870 | * @param propertyExpression 871 | * @throws ObjectPropertyExpressionException 872 | */ 873 | public void transitive(String propertyExpression) throws ObjectPropertyExpressionException { 874 | OWLObjectPropertyExpression expression = parseObjectPropertyExpression(propertyExpression); 875 | OWLTransitiveObjectPropertyAxiom axiom = this.factory.getOWLTransitiveObjectPropertyAxiom(expression); 876 | addAxiom(axiom); 877 | } 878 | 879 | /** 880 | * Declare an reflexive axiom. 881 | * @param propertyExpression 882 | * @throws ObjectPropertyExpressionException 883 | */ 884 | public void reflexive(String propertyExpression) throws ObjectPropertyExpressionException { 885 | OWLObjectPropertyExpression expression = parseObjectPropertyExpression(propertyExpression); 886 | OWLReflexiveObjectPropertyAxiom axiom = this.factory.getOWLReflexiveObjectPropertyAxiom(expression); 887 | addAxiom(axiom); 888 | } 889 | 890 | /** 891 | * Declare an functional axiom. 892 | * @param propertyExpression 893 | * @throws DataPropertyExpressionException 894 | */ 895 | public void functional(String propertyExpression) throws DataPropertyExpressionException { 896 | OWLDataPropertyExpression expression = parseDataPropertyExpression(propertyExpression); 897 | OWLFunctionalDataPropertyAxiom axiom = this.factory.getOWLFunctionalDataPropertyAxiom(expression); 898 | addAxiom(axiom); 899 | } 900 | 901 | /** 902 | * Declare an domain axiom. 903 | * @param propertyExpression 904 | * @param classExpression 905 | * @throws ClassExpressionException 906 | * @throws NonExistingEntityException 907 | */ 908 | public void domain(String propertyExpression, String classExpression) throws ClassExpressionException, NonExistingEntityException{ 909 | OWLClassExpression domainExpression = parseClassExpression(classExpression); 910 | try { 911 | OWLObjectPropertyExpression owlPropertyExpression = parseObjectPropertyExpression(propertyExpression); 912 | OWLObjectPropertyDomainAxiom axiom = this.factory.getOWLObjectPropertyDomainAxiom(owlPropertyExpression, domainExpression); 913 | addAxiom(axiom); 914 | } catch (ObjectPropertyExpressionException e) { 915 | OWLDataPropertyExpression owlPropertyExpression; 916 | try { 917 | owlPropertyExpression = parseDataPropertyExpression(propertyExpression); 918 | OWLDataPropertyDomainAxiom axiom = this.factory.getOWLDataPropertyDomainAxiom(owlPropertyExpression, domainExpression); 919 | addAxiom(axiom); 920 | } catch (DataPropertyExpressionException e1) { 921 | throw new NonExistingEntityException("The property '"+propertyExpression+"' does not exist."); 922 | } 923 | } 924 | } 925 | 926 | /** 927 | * Declare an range axiom. 928 | * @param propertyExpression 929 | * @param classExpression 930 | * @throws ClassExpressionException 931 | * @throws ObjectPropertyExpressionException 932 | */ 933 | public void range(String propertyExpression, String classExpression) throws ClassExpressionException, ObjectPropertyExpressionException { 934 | OWLObjectPropertyExpression owlPropertyExpression = parseObjectPropertyExpression(propertyExpression); 935 | OWLClassExpression rangeExpression = parseClassExpression(classExpression); 936 | OWLObjectPropertyRangeAxiom axiom = this.factory.getOWLObjectPropertyRangeAxiom(owlPropertyExpression, rangeExpression); 937 | addAxiom(axiom); 938 | } 939 | 940 | /** 941 | * Declare an range axiom. 942 | * @param propertyExpression 943 | * @param dataType 944 | * @throws DataPropertyExpressionException 945 | */ 946 | public void range(String propertyExpression, OWLDatatype dataType) throws DataPropertyExpressionException { 947 | OWLDataPropertyExpression owlPropertyExpression = parseDataPropertyExpression(propertyExpression); 948 | OWLDataPropertyRangeAxiom axiom = this.factory.getOWLDataPropertyRangeAxiom(owlPropertyExpression, dataType); 949 | addAxiom(axiom); 950 | } 951 | 952 | /** 953 | * Declare an equivalentProperties axiom. 954 | * @param property1 955 | * @param property2 956 | * @throws BrainException 957 | */ 958 | public void equivalentProperties(String property1, String property2) throws BrainException { 959 | try { 960 | OWLObjectPropertyExpression owlProperty1 = parseObjectPropertyExpression(property1); 961 | OWLObjectPropertyExpression owlProperty2 = parseObjectPropertyExpression(property2); 962 | OWLEquivalentObjectPropertiesAxiom axiom = this.factory.getOWLEquivalentObjectPropertiesAxiom(owlProperty1, owlProperty2); 963 | addAxiom(axiom); 964 | } catch (ObjectPropertyExpressionException e) { 965 | try{ 966 | OWLDataPropertyExpression owlProperty1 = parseDataPropertyExpression(property1); 967 | OWLDataPropertyExpression owlProperty2 = parseDataPropertyExpression(property2); 968 | OWLEquivalentDataPropertiesAxiom axiom = this.factory.getOWLEquivalentDataPropertiesAxiom(owlProperty1, owlProperty2); 969 | addAxiom(axiom); 970 | } catch (DataPropertyExpressionException e1) { 971 | throw new BrainException("One of the properties ('"+property1+"' or '"+property2+"') does not exist or the properties have different types."); 972 | } 973 | } 974 | } 975 | 976 | /** 977 | * Declare an subPropertyOf axiom. 978 | * @param subProperty 979 | * @param superProperty 980 | * @throws BrainException 981 | */ 982 | public void subPropertyOf(String subProperty, String superProperty) throws BrainException { 983 | try{ 984 | OWLObjectPropertyExpression subPropertyExpression = parseObjectPropertyExpression(subProperty); 985 | OWLObjectPropertyExpression superPropertyExpression = parseObjectPropertyExpression(superProperty); 986 | OWLSubObjectPropertyOfAxiom subPropertyAxiom = this.factory.getOWLSubObjectPropertyOfAxiom(subPropertyExpression, superPropertyExpression); 987 | addAxiom(subPropertyAxiom); 988 | } catch(ObjectPropertyExpressionException e){ 989 | try{ 990 | OWLDataPropertyExpression subPropertyExpression = parseDataPropertyExpression(subProperty); 991 | OWLDataPropertyExpression superPropertyExpression = parseDataPropertyExpression(superProperty); 992 | OWLSubDataPropertyOfAxiom subPropertyAxiom = this.factory.getOWLSubDataPropertyOfAxiom(subPropertyExpression, superPropertyExpression); 993 | addAxiom(subPropertyAxiom); 994 | }catch (DataPropertyExpressionException e1) { 995 | throw new BrainException("One of the properties ('"+subProperty+"' or '"+superProperty+"') does not exist or the properties have different types."); 996 | } 997 | } 998 | } 999 | 1000 | /** 1001 | * Declare an chain axiom. 1002 | * @param chain 1003 | * @param superProperty 1004 | * @throws ObjectPropertyExpressionException 1005 | */ 1006 | public void chain(String chain, String superProperty) throws ObjectPropertyExpressionException { 1007 | List chainExpression = parseObjectPropertyChain(chain); 1008 | OWLObjectPropertyExpression superPropertyExpression = parseObjectPropertyExpression(superProperty); 1009 | OWLSubPropertyChainOfAxiom axiom = this.factory.getOWLSubPropertyChainOfAxiom(chainExpression, superPropertyExpression); 1010 | addAxiom(axiom); 1011 | } 1012 | 1013 | /** 1014 | * Declare an annotation axiom. 1015 | * @param entity 1016 | * @param annotationProperty 1017 | * @param content 1018 | * @throws NonExistingEntityException 1019 | */ 1020 | public void annotation(String entity, String annotationProperty, String content) throws NonExistingEntityException { 1021 | if(this.bidiShortFormProvider.getEntity(entity) != null){ 1022 | OWLEntity owlEntity = this.bidiShortFormProvider.getEntity(entity); 1023 | OWLAnnotationProperty owlAnnotationProperty = this.getOWLAnnotationProperty(annotationProperty); 1024 | OWLAnnotation labelAnnotation = this.factory.getOWLAnnotation(owlAnnotationProperty, this.factory.getOWLLiteral(content)); 1025 | OWLAxiom axiom = this.factory.getOWLAnnotationAssertionAxiom(owlEntity.getIRI(), labelAnnotation); 1026 | addAxiom(axiom); 1027 | }else{ 1028 | throw new NonExistingEntityException("The entity '"+entity+"' does not exist."); 1029 | } 1030 | } 1031 | 1032 | /** 1033 | * Declare an annotation axiom. 1034 | * @param entity 1035 | * @param annotationProperty 1036 | * @param content 1037 | * @throws NonExistingEntityException 1038 | */ 1039 | private void annotation(String entity, OWLAnnotationProperty annotationProperty, String content) throws NonExistingEntityException { 1040 | if(this.bidiShortFormProvider.getEntity(entity) != null){ 1041 | OWLEntity owlEntity = this.bidiShortFormProvider.getEntity(entity); 1042 | OWLAnnotation labelAnnotation = this.factory.getOWLAnnotation(annotationProperty, this.factory.getOWLLiteral(content)); 1043 | OWLAxiom axiom = this.factory.getOWLAnnotationAssertionAxiom(owlEntity.getIRI(), labelAnnotation); 1044 | addAxiom(axiom); 1045 | }else{ 1046 | throw new NonExistingEntityException("The entity '"+entity+"' does not exist."); 1047 | } 1048 | } 1049 | 1050 | /** 1051 | * Retrieve the content of an annotation. 1052 | * @param entity 1053 | * @param annotationProperty 1054 | * @throws NonExistingEntityException 1055 | * @return content 1056 | */ 1057 | private String getAnnotation(String entity, OWLAnnotationProperty annotationProperty) throws NonExistingEntityException { 1058 | if(this.bidiShortFormProvider.getEntity(entity) != null){ 1059 | OWLEntity owlEntity = this.bidiShortFormProvider.getEntity(entity); 1060 | for (OWLAnnotation annotation : owlEntity.getAnnotations(this.ontology, annotationProperty)) { 1061 | if (annotation.getValue() instanceof OWLLiteral) { 1062 | OWLLiteral val = (OWLLiteral) annotation.getValue(); 1063 | return val.getLiteral(); 1064 | } 1065 | } 1066 | }else{ 1067 | throw new NonExistingEntityException("The entity '"+entity+"' does not exist."); 1068 | } 1069 | throw new NonExistingEntityException("The entity '"+entity+"' has no annotation of this type attached to it."); 1070 | } 1071 | 1072 | /** 1073 | * Retrieve the content of an annotation. 1074 | * @param entity 1075 | * @param annotationProperty 1076 | * @throws NonExistingEntityException 1077 | * @return content 1078 | */ 1079 | public String getAnnotation(String entity, String annotationProperty) throws NonExistingEntityException { 1080 | OWLAnnotationProperty owlAnnotationProperty = this.getOWLAnnotationProperty(annotationProperty); 1081 | return this.getAnnotation(entity, owlAnnotationProperty); 1082 | } 1083 | 1084 | /** 1085 | * Retrieve the list of values for an annotation. 1086 | * @param entity 1087 | * @param annotationProperty 1088 | * @throws NonExistingEntityException 1089 | * @return content 1090 | */ 1091 | private List getAnnotations(String entity, OWLAnnotationProperty annotationProperty) throws NonExistingEntityException { 1092 | if(this.bidiShortFormProvider.getEntity(entity) != null){ 1093 | OWLEntity owlEntity = this.bidiShortFormProvider.getEntity(entity); 1094 | List annotations = new ArrayList(); 1095 | Set owlAnnotations = owlEntity.getAnnotations(this.ontology, annotationProperty); 1096 | if(owlAnnotations.size() <= 0){ 1097 | throw new NonExistingEntityException("The entity '"+entity+"' has no annotation of this type attached to it."); 1098 | } 1099 | for (OWLAnnotation annotation : owlAnnotations) { 1100 | if (annotation.getValue() instanceof OWLLiteral) { 1101 | OWLLiteral val = (OWLLiteral) annotation.getValue(); 1102 | annotations.add(val.getLiteral()); 1103 | } 1104 | } 1105 | return annotations; 1106 | }else{ 1107 | throw new NonExistingEntityException("The entity '"+entity+"' does not exist."); 1108 | } 1109 | } 1110 | 1111 | /** 1112 | * Retrieve the list of values for an annotation. 1113 | * @param entity 1114 | * @param annotationProperty 1115 | * @throws NonExistingEntityException 1116 | * @return content 1117 | */ 1118 | public List getAnnotations(String entity, String annotationProperty) throws NonExistingEntityException { 1119 | OWLAnnotationProperty owlAnnotationProperty = this.getOWLAnnotationProperty(annotationProperty); 1120 | return this.getAnnotations(entity, owlAnnotationProperty); 1121 | } 1122 | 1123 | /** 1124 | * Set the content of the label. 1125 | * @param entity 1126 | * @param label 1127 | * @throws NonExistingEntityException 1128 | */ 1129 | public void label(String entity, String label) throws NonExistingEntityException { 1130 | this.annotation(entity, this.factory.getRDFSLabel(), label); 1131 | } 1132 | 1133 | /** 1134 | * Get the content of the label. 1135 | * @param entity 1136 | * @throws NonExistingEntityException 1137 | * @return label 1138 | */ 1139 | public String getLabel(String entity) throws NonExistingEntityException { 1140 | return this.getAnnotation(entity, this.factory.getRDFSLabel()); 1141 | } 1142 | 1143 | /** 1144 | * Set the content of the comment. 1145 | * @param entity 1146 | * @param comment 1147 | * @throws NonExistingEntityException 1148 | */ 1149 | public void comment(String entity, String comment) throws NonExistingEntityException { 1150 | this.annotation(entity, this.factory.getRDFSComment(), comment); 1151 | } 1152 | 1153 | /** 1154 | * Get the content of the comment. 1155 | * @param entity 1156 | * @throws NonExistingEntityException 1157 | * @return comment 1158 | */ 1159 | public String getComment(String entity) throws NonExistingEntityException { 1160 | return this.getAnnotation(entity, this.factory.getRDFSComment()); 1161 | } 1162 | 1163 | /** 1164 | * Set the content of rdfs:isDefinedBy. 1165 | * @param entity 1166 | * @param isDefinedBy 1167 | * @throws NonExistingEntityException 1168 | */ 1169 | public void isDefinedBy(String entity, String isDefinedBy) throws NonExistingEntityException { 1170 | this.annotation(entity, this.factory.getRDFSIsDefinedBy(), isDefinedBy); 1171 | } 1172 | 1173 | /** 1174 | * Get the content of the rdfs:isDefinedBy. 1175 | * @param entity 1176 | * @throws NonExistingEntityException 1177 | * @return rdfs:isDefinedBy 1178 | */ 1179 | public String getIsDefinedBy(String entity) throws NonExistingEntityException { 1180 | return this.getAnnotation(entity, this.factory.getRDFSIsDefinedBy()); 1181 | } 1182 | 1183 | /** 1184 | * Set the content of rdfs:seeAlso. 1185 | * @param entity 1186 | * @param seeAlso 1187 | * @throws NonExistingEntityException 1188 | */ 1189 | public void seeAlso(String entity, String seeAlso) throws NonExistingEntityException { 1190 | this.annotation(entity, this.factory.getRDFSSeeAlso(), seeAlso); 1191 | } 1192 | 1193 | /** 1194 | * Get the content of the rdfs:seeAlso. 1195 | * @param entity 1196 | * @throws NonExistingEntityException 1197 | * @return rdfs:isDefinedBy 1198 | */ 1199 | public String getSeeAlso(String entity) throws NonExistingEntityException { 1200 | return this.getAnnotation(entity, this.factory.getRDFSSeeAlso()); 1201 | } 1202 | 1203 | /** 1204 | * Converts a string into an OWLClassExpression. If a problem is encountered, an error is thrown. 1205 | * @param expression 1206 | * @return owlClassExpression 1207 | * @throws ClassExpressionException 1208 | */ 1209 | private OWLClassExpression parseClassExpression(String expression) throws ClassExpressionException { 1210 | OWLClassExpression owlClassExpression = null; 1211 | ManchesterOWLSyntaxEditorParser parser = getParser(expression); 1212 | try { 1213 | owlClassExpression = parser.parseClassExpression(); 1214 | } catch (ParserException e) { 1215 | throw new ClassExpressionException(e); 1216 | } 1217 | return owlClassExpression; 1218 | } 1219 | 1220 | 1221 | /** 1222 | * Converts a string representing an OWL class expression with labels into 1223 | * an OWLClassExpression object. If a problem is 1224 | * encountered, an error is thrown. 1225 | * @param labelClassExpression 1226 | * @return owlClassExpression 1227 | * @throws ClassExpressionException 1228 | */ 1229 | public OWLClassExpression parseLabelClassExpression(String labelClassExpression) throws ClassExpressionException { 1230 | OWLClassExpression owlClassExpression = null; 1231 | ManchesterOWLSyntaxEditorParser parser = getLabelParser(labelClassExpression); 1232 | try { 1233 | owlClassExpression = parser.parseClassExpression(); 1234 | } catch (ParserException e) { 1235 | throw new ClassExpressionException(e); 1236 | } 1237 | return owlClassExpression; 1238 | } 1239 | 1240 | 1241 | /** 1242 | * Converts a string into an OWLObjectPropertyExpression. If a problem is encountered, an error is thrown. 1243 | * @param objectPropertyExpression 1244 | * @return owlObjectPropertyExpression 1245 | * @throws ObjectPropertyExpressionException 1246 | */ 1247 | private OWLObjectPropertyExpression parseObjectPropertyExpression(String objectPropertyExpression) throws ObjectPropertyExpressionException { 1248 | OWLObjectPropertyExpression owlObjectPropertyExpression = null; 1249 | ManchesterOWLSyntaxEditorParser parser = getParser(objectPropertyExpression); 1250 | try { 1251 | owlObjectPropertyExpression = parser.parseObjectPropertyExpression(); 1252 | } catch (ParserException e) { 1253 | throw new ObjectPropertyExpressionException(e); 1254 | } 1255 | return owlObjectPropertyExpression; 1256 | } 1257 | 1258 | /** 1259 | * Converts a string into an OWLDataPropertyExpression. If a problem is encountered, an error is thrown. 1260 | * @param dataPropertyExpression 1261 | * @return owlDataPropertyExpression 1262 | * @throws DataPropertyExpressionException 1263 | */ 1264 | private OWLDataPropertyExpression parseDataPropertyExpression(String dataPropertyExpression) throws DataPropertyExpressionException { 1265 | OWLDataPropertyExpression owlDataPropertyExpression = null; 1266 | ManchesterOWLSyntaxEditorParser parser = getParser(dataPropertyExpression); 1267 | try { 1268 | owlDataPropertyExpression = parser.parseDataPropertyExpression(); 1269 | } catch (ParserException e) { 1270 | throw new DataPropertyExpressionException(e); 1271 | } 1272 | return owlDataPropertyExpression; 1273 | } 1274 | 1275 | /** 1276 | * Converts a string into a list of OWLObjectPropertyExpression. If a problem is encountered, an error is thrown. 1277 | * @param chainExpression 1278 | * @return owlObjectPropertyExpressions 1279 | * @throws ObjectPropertyExpressionException 1280 | */ 1281 | private List parseObjectPropertyChain(String chainExpression) throws ObjectPropertyExpressionException { 1282 | List owlObjectPropertyExpressions = null; 1283 | ManchesterOWLSyntaxEditorParser parser = getParser(chainExpression); 1284 | try { 1285 | owlObjectPropertyExpressions = parser.parseObjectPropertyChain(); 1286 | } catch (ParserException e) { 1287 | throw new ObjectPropertyExpressionException(e); 1288 | } 1289 | return owlObjectPropertyExpressions; 1290 | } 1291 | 1292 | /** 1293 | * Converts a string into an OWLNamedIndividual. If a problem is encountered, an error is thrown. 1294 | * @param namedIndividual 1295 | * @return owlNamedIndividual 1296 | * @throws NamedIndividualException 1297 | */ 1298 | private OWLNamedIndividual parseNamedIndividual(String namedIndividual) throws NamedIndividualException { 1299 | OWLNamedIndividual owlNamedIndividual = null; 1300 | ManchesterOWLSyntaxEditorParser parser = getParser(namedIndividual); 1301 | try { 1302 | owlNamedIndividual = (OWLNamedIndividual) parser.parseIndividual(); 1303 | } catch (ParserException e) { 1304 | throw new NamedIndividualException(e); 1305 | } 1306 | return owlNamedIndividual; 1307 | } 1308 | 1309 | /** 1310 | * Instantiate a new Manchester syntax parser. 1311 | * @param expression 1312 | * @return parser 1313 | */ 1314 | private ManchesterOWLSyntaxEditorParser getParser(String expression) { 1315 | ManchesterOWLSyntaxEditorParser parser = new ManchesterOWLSyntaxEditorParser(this.factory, expression); 1316 | parser.setDefaultOntology(this.ontology); 1317 | parser.setOWLEntityChecker(this.entityChecker); 1318 | return parser; 1319 | } 1320 | 1321 | /** 1322 | * Instantiate a new Manchester syntax parser for labels. 1323 | * @param labelClassExpression 1324 | * @return parser 1325 | */ 1326 | private ManchesterOWLSyntaxEditorParser getLabelParser(String labelClassExpression) { 1327 | List rdfsLabels = Arrays.asList(this.factory.getRDFSLabel()); 1328 | ShortFormProvider sfp = new AnnotationValueShortFormProvider(rdfsLabels, 1329 | Collections.> emptyMap(), this.manager); 1330 | BidirectionalShortFormProvider bidiLabel = new BidirectionalShortFormProviderAdapter(this.manager.getOntologies(), sfp); 1331 | ShortFormEntityChecker labelChecker = new ShortFormEntityChecker(bidiLabel); 1332 | ManchesterOWLSyntaxEditorParser parser = new ManchesterOWLSyntaxEditorParser(this.factory, labelClassExpression); 1333 | parser.setOWLEntityChecker(labelChecker); 1334 | parser.setDefaultOntology(this.ontology); 1335 | return parser; 1336 | } 1337 | 1338 | 1339 | /** 1340 | * Save the ontology at the specified location. 1341 | * The ontology is serialized using Manchester syntax. 1342 | * @param path 1343 | * @throws StorageException 1344 | */ 1345 | public void save(String path) throws StorageException { 1346 | File file = new File(path); 1347 | OWLOntologyFormat format = this.manager.getOntologyFormat(this.ontology); 1348 | ManchesterOWLSyntaxOntologyFormat manSyntaxFormat = new ManchesterOWLSyntaxOntologyFormat(); 1349 | if (format.isPrefixOWLOntologyFormat()) { 1350 | manSyntaxFormat.copyPrefixesFrom(this.prefixManager); 1351 | } 1352 | try { 1353 | this.manager.saveOntology(this.ontology, manSyntaxFormat, IRI.create(file)); 1354 | } catch (OWLOntologyStorageException e) { 1355 | throw new StorageException(e); 1356 | } 1357 | } 1358 | 1359 | /** 1360 | * Load an external ontology from it's IRI or from a local file 1361 | * and merge it with the current ontology. 1362 | * @param pathToOntology 1363 | * @throws NewOntologyException 1364 | * @throws ExistingEntityException 1365 | */ 1366 | public void learn(String pathToOntology) throws NewOntologyException, ExistingEntityException { 1367 | OWLOntologyManager newManager = OWLManager.createOWLOntologyManager(); 1368 | OWLOntology newOnto; 1369 | if(isExternalEntity(pathToOntology)){ 1370 | IRI iriOnto = IRI.create(pathToOntology); 1371 | try { 1372 | newOnto = newManager.loadOntologyFromOntologyDocument(iriOnto); 1373 | } catch (OWLOntologyCreationException e) { 1374 | throw new NewOntologyException(e); 1375 | } 1376 | }else{ 1377 | File file = new File(pathToOntology); 1378 | try { 1379 | newOnto = newManager.loadOntologyFromOntologyDocument(file); 1380 | } catch (OWLOntologyCreationException e) { 1381 | throw new NewOntologyException(e); 1382 | } 1383 | } 1384 | 1385 | SimpleShortFormProvider sf = new SimpleShortFormProvider(); 1386 | Set importsClosure = newOnto.getImportsClosure(); 1387 | BidirectionalShortFormProviderAdapter newBidiShortFormProvider = new BidirectionalShortFormProviderAdapter(newManager, importsClosure, sf); 1388 | 1389 | //For each of the new shortForms, checks if it exists already in the existing one. 1390 | //throws an error if the same entity was found, otherwise carries on. 1391 | for (String shortFromNewOnto : newBidiShortFormProvider.getShortForms()) { 1392 | 1393 | if(this.bidiShortFormProvider.getEntity(shortFromNewOnto) != null){ 1394 | 1395 | //If the short form is already known, can be problematic, because multiple short forms can 1396 | //refer to the same entity. The code below is made to identify this. 1397 | 1398 | OWLEntity existingEntity = this.bidiShortFormProvider.getEntity(shortFromNewOnto); 1399 | OWLEntity newEntity = newBidiShortFormProvider.getEntity(shortFromNewOnto); 1400 | 1401 | boolean identicalEntities = false; 1402 | boolean identicalType = false; 1403 | boolean identicalIri = false; 1404 | 1405 | //Compares the URI of the entities 1406 | if(newEntity.getIRI().equals(existingEntity.getIRI())){ 1407 | identicalIri = true; 1408 | } 1409 | 1410 | //Compares their types 1411 | if(newEntity.getEntityType().equals(existingEntity.getEntityType())){ 1412 | identicalType = true; 1413 | if(identicalIri){ 1414 | //If they have the same type and same name, therefore they are the same entities. 1415 | identicalEntities = true; 1416 | } 1417 | } 1418 | 1419 | if(!shortFromNewOnto.equals("Thing") && !identicalEntities){ 1420 | //A problem was identified: the new short form is not unique and refers in a different already existing entity. 1421 | String message = "Inside the file you are trying to load (" + pathToOntology + "), there's an entity with the short form '"+ shortFromNewOnto + "' (derived from " + 1422 | newEntity.getIRI() + ") which already exists in the kwnoledge base.\n" + 1423 | "This short form currently refers to the existing entity '" + existingEntity.getIRI() + "'.\n" + 1424 | " Please make sure short forms are unique, otherwise you will have ambiguity problems when" + 1425 | " running queries later on."; 1426 | 1427 | if(!identicalType && identicalIri){ 1428 | message += " These two entities have the IRI (" + existingEntity.getIRI() + "), but different types. " + 1429 | "The type of the already existing one is '" + existingEntity.getEntityType() + "' and the type" + 1430 | " of the one to be learned is '" + newEntity.getEntityType() + "'."; 1431 | } 1432 | 1433 | throw new ExistingEntityException(message); 1434 | } 1435 | } 1436 | } 1437 | 1438 | //Keep the prefix information if present 1439 | OWLOntologyFormat format = newManager.getOntologyFormat(newOnto); 1440 | if (format.isPrefixOWLOntologyFormat()) { 1441 | PrefixOWLOntologyFormat newPrefixesFormat = format.asPrefixOWLOntologyFormat(); 1442 | Set newPrefixes = newPrefixesFormat.getPrefixNames(); 1443 | for (String prefix : newPrefixes) { 1444 | if(!this.prefixManager.containsPrefixMapping(prefix)){ 1445 | prefix(newPrefixesFormat.getPrefix(prefix), prefix.replaceAll(":", "")); 1446 | } 1447 | } 1448 | } 1449 | 1450 | //Transfer all the axioms from the old ontology into the new one 1451 | //Bottleneck 1452 | this.manager.addAxioms(this.ontology, newOnto.getAxioms()); 1453 | update(); 1454 | } 1455 | 1456 | /** 1457 | * Load an external ontology from it's IRI or from a local file 1458 | * and merge it with the current ontology. In case the shortform is already used, Brain 1459 | * re-uses the one already existing (first-come, first-served strategy). Brain will try do 1460 | * disambiguate shortform as much as possible, however, because this process is hacky by nature, the solution 1461 | * might not be exactly accurate - so double check the results! 1462 | * @param pathToOntology 1463 | * @throws NewOntologyException 1464 | * @throws ExistingEntityException 1465 | */ 1466 | public void learnAndDisambiguate(String pathToOntology) throws NewOntologyException, ExistingEntityException { 1467 | OWLOntologyManager newManager = OWLManager.createOWLOntologyManager(); 1468 | OWLOntology newOnto; 1469 | if(isExternalEntity(pathToOntology)){ 1470 | IRI iriOnto = IRI.create(pathToOntology); 1471 | try { 1472 | newOnto = newManager.loadOntologyFromOntologyDocument(iriOnto); 1473 | } catch (OWLOntologyCreationException e) { 1474 | throw new NewOntologyException(e); 1475 | } 1476 | }else{ 1477 | File file = new File(pathToOntology); 1478 | try { 1479 | newOnto = newManager.loadOntologyFromOntologyDocument(file); 1480 | } catch (OWLOntologyCreationException e) { 1481 | throw new NewOntologyException(e); 1482 | } 1483 | } 1484 | 1485 | SimpleShortFormProvider sf = new SimpleShortFormProvider(); 1486 | Set importsClosure = newOnto.getImportsClosure(); 1487 | BidirectionalShortFormProviderAdapter newBidiShortFormProvider = new BidirectionalShortFormProviderAdapter(newManager, importsClosure, sf); 1488 | 1489 | //For each of the new shortForms, checks if it exists already in the existing one. 1490 | //throws an error if the same entity was found, otherwise carries on. 1491 | for (String shortFromNewOnto : newBidiShortFormProvider.getShortForms()) { 1492 | 1493 | //check if the shortform is already known, to identify the problematic cases. 1494 | if(this.bidiShortFormProvider.getEntity(shortFromNewOnto) != null){ 1495 | 1496 | //If the short form is already known, can be problematic, because multiple short forms can 1497 | //refer to the same entity. This method tries to disambiguate the shortform using 1498 | //a first-come first-served strategy: if an identical shortform is already detected, then it 1499 | //uses the already existing version rather than the new one, and prints a warning to the user. 1500 | 1501 | OWLEntity existingEntity = this.bidiShortFormProvider.getEntity(shortFromNewOnto); 1502 | OWLEntity newEntity = newBidiShortFormProvider.getEntity(shortFromNewOnto); 1503 | 1504 | boolean identicalEntities = false; 1505 | boolean identicalType = false; 1506 | boolean identicalIri = false; 1507 | 1508 | //Compares the URI of the entities 1509 | if(newEntity.getIRI().equals(existingEntity.getIRI())){ 1510 | identicalIri = true; 1511 | } 1512 | 1513 | //Compares their types 1514 | if(newEntity.getEntityType().equals(existingEntity.getEntityType())){ 1515 | identicalType = true; 1516 | if(identicalIri){ 1517 | //If they have the same type and same name, therefore they are the same entities. 1518 | //No problem, just carries on with the checking. 1519 | identicalEntities = true; 1520 | } 1521 | } 1522 | 1523 | if(!shortFromNewOnto.equals("Thing") && !identicalEntities){ 1524 | //Should be an error in this case, impossible to disambiguate 1525 | if(!identicalType && identicalIri){ 1526 | throw new ExistingEntityException(" These two entities (" + existingEntity.getIRI() + ") have the same IRI, but different types. " + 1527 | "The type of the already existing one is '" + existingEntity.getEntityType() + "' and the type" + 1528 | " of the one to be learned is '" + newEntity.getEntityType() + "'."); 1529 | }else{ 1530 | //Attempt to disambiguate, the entities have the same shortform and types, but different IRIs 1531 | //Covert all the new IRIs to the existing one 1532 | Set ontologies = new HashSet(); 1533 | ontologies.add(newOnto); 1534 | OWLEntityRenamer renamer = new OWLEntityRenamer(newManager, ontologies); 1535 | IRI existingIri = existingEntity.getIRI(); 1536 | IRI newIri = newEntity.getIRI(); 1537 | //Replace the IRI of the conflicting new entity with the IRI already existing in the KB. 1538 | List changes = renamer.changeIRI(newIri, existingIri); 1539 | //Applies the changes to the ontology 1540 | newManager.applyChanges(changes); 1541 | } 1542 | } 1543 | } 1544 | } 1545 | 1546 | //Keep the prefix information if present 1547 | OWLOntologyFormat format = newManager.getOntologyFormat(newOnto); 1548 | if (format.isPrefixOWLOntologyFormat()) { 1549 | PrefixOWLOntologyFormat newPrefixesFormat = format.asPrefixOWLOntologyFormat(); 1550 | Set newPrefixes = newPrefixesFormat.getPrefixNames(); 1551 | for (String prefix : newPrefixes) { 1552 | if(!this.prefixManager.containsPrefixMapping(prefix)){ 1553 | prefix(newPrefixesFormat.getPrefix(prefix), prefix.replaceAll(":", "")); 1554 | } 1555 | } 1556 | } 1557 | 1558 | //Transfer all the axioms from the old ontology into the new one 1559 | //Bottleneck 1560 | this.manager.addAxioms(this.ontology, newOnto.getAxioms()); 1561 | update(); 1562 | } 1563 | 1564 | 1565 | /** 1566 | * Load another brain object. The axioms are extracted 1567 | * and merged with the current one. 1568 | * 1569 | * @throws NewOntologyException 1570 | * @throws ExistingEntityException 1571 | */ 1572 | public void learn(Brain brainToLearn) throws ExistingEntityException { 1573 | 1574 | for (String shortFromNewOnto : brainToLearn.getBidiShortFormProvider().getShortForms()) { 1575 | if(this.bidiShortFormProvider.getEntity(shortFromNewOnto) != null){ 1576 | OWLEntity existingEntity = this.bidiShortFormProvider.getEntity(shortFromNewOnto); 1577 | OWLEntity newEntity = brainToLearn.getBidiShortFormProvider().getEntity(shortFromNewOnto); 1578 | boolean identicalEntities = false; 1579 | if(newEntity.getIRI().equals(existingEntity.getIRI()) && newEntity.getEntityType().equals(existingEntity.getEntityType())){ 1580 | identicalEntities = true; 1581 | } 1582 | 1583 | if(!shortFromNewOnto.equals("Thing") && !identicalEntities){ 1584 | throw new ExistingEntityException("The entity '"+shortFromNewOnto+"' already exists and is of different type or has a" + 1585 | "different prefix."); 1586 | } 1587 | } 1588 | } 1589 | 1590 | //Keep the prefix information if present 1591 | for (String prefix : brainToLearn.getPrefixManager().getPrefixNames()) { 1592 | if(!this.prefixManager.containsPrefixMapping(prefix)){ 1593 | prefix(brainToLearn.getPrefixManager().getPrefix(prefix), prefix.replaceAll(":", "")); 1594 | } 1595 | } 1596 | 1597 | this.manager.addAxioms(this.ontology, brainToLearn.getOntology().getAxioms()); 1598 | update(); 1599 | } 1600 | 1601 | 1602 | /** 1603 | * Classify the ontology. It is usually the most expensive 1604 | * operation, so use it carefully! 1605 | */ 1606 | public void classify() { 1607 | //Maybe this is performance bottleneck. Elk implements as in 0.3.2 an lazy way to trigger the 1608 | //reasoning. Inside Brain the idea is to handle the reasoning separately, so we have to trigger the 1609 | //reclassification for each type. 1610 | //this.reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY); 1611 | //this.reasoner.precomputeInferences(InferenceType.CLASS_ASSERTIONS); 1612 | this.reasoner.precomputeInferences(); 1613 | this.isClassified = true; 1614 | } 1615 | 1616 | /** 1617 | * Test whether the ontology is following an OWL 2 EL profile or not. 1618 | * @return hasElProfile 1619 | */ 1620 | public boolean hasElProfile() { 1621 | OWL2ELProfile profile = new OWL2ELProfile(); 1622 | OWLProfileReport report = profile.checkOntology(this.ontology); 1623 | if(report.getViolations().size() == 0){ 1624 | return true; 1625 | }else{ 1626 | return false; 1627 | } 1628 | } 1629 | 1630 | /** 1631 | * Returns the list of violations of the OWL 2 EL profile. 1632 | * @return violations 1633 | */ 1634 | public List getElProfileViolations() { 1635 | OWL2ELProfile profile = new OWL2ELProfile(); 1636 | OWLProfileReport report = profile.checkOntology(this.ontology); 1637 | ArrayList violations = new ArrayList(); 1638 | if(report.getViolations().size() != 0){ 1639 | for (OWLProfileViolation violation : report.getViolations()) { 1640 | violations.add(violation.toString()); 1641 | } 1642 | } 1643 | return violations; 1644 | } 1645 | 1646 | /** 1647 | * Returns the list of subclasses from the expression. 1648 | * The second parameter is a flag telling whether only the direct classes 1649 | * should be returned. The method is synchronized in order 1650 | * to avoid concurrency problems that could arise in threaded 1651 | * environment (such as a web server with a shared Brain instance). 1652 | * @param classExpression 1653 | * @param direct 1654 | * @return subClasses 1655 | * @throws ClassExpressionException 1656 | */ 1657 | public synchronized List getSubClasses(String classExpression, boolean direct) throws ClassExpressionException { 1658 | OWLClassExpression owlClassExpression = parseClassExpression(classExpression); 1659 | return getSubClasses(owlClassExpression, direct); 1660 | } 1661 | 1662 | /** 1663 | * Returns the list of subclasses from the OWL expression. 1664 | * The second parameter is a flag telling whether only the direct classes 1665 | * should be returned. 1666 | * @param owlClassExpression 1667 | * @param direct 1668 | * @return subClasses 1669 | */ 1670 | private List getSubClasses(OWLClassExpression owlClassExpression, boolean direct) { 1671 | Set subClasses = null; 1672 | //Can be simplified once Elk would have implemented a better way to deal with anonymous classes 1673 | if(owlClassExpression.isAnonymous()){ 1674 | OWLClass anonymousClass = getTemporaryAnonymousClass(owlClassExpression); 1675 | this.classify(); 1676 | subClasses = this.reasoner.getSubClasses(anonymousClass, direct).getFlattened(); 1677 | removeTemporaryAnonymousClass(anonymousClass); 1678 | }else{ 1679 | if(!this.isClassified){ 1680 | this.classify(); 1681 | } 1682 | subClasses = this.reasoner.getSubClasses(owlClassExpression, direct).getFlattened(); 1683 | } 1684 | return sortClasses(subClasses); 1685 | } 1686 | 1687 | 1688 | /** 1689 | * Returns the list of subclasses from the label expression. 1690 | * The second parameter is a flag telling whether only the direct classes 1691 | * should be returned. The method is synchronized in order 1692 | * to avoid concurrency problems that could arise in threaded 1693 | * environment (such as a web server with a shared Brain instance). 1694 | * @param labelClassExpression 1695 | * @param direct 1696 | * @return subClasses 1697 | * @throws ClassExpressionException 1698 | */ 1699 | public synchronized List getSubClassesFromLabel(String labelClassExpression, boolean direct) throws ClassExpressionException { 1700 | OWLClassExpression owlClassExpression = parseLabelClassExpression(labelClassExpression); 1701 | return getSubClasses(owlClassExpression, direct); 1702 | } 1703 | 1704 | /** 1705 | * Returns the list of super classes from the expression. 1706 | * The second parameter is a flag telling whether only the direct classes 1707 | * should be returned. The method is synchronized in order 1708 | * to avoid concurrency problems that could arise in threaded 1709 | * environment (such as a web server with a shared Brain instance). 1710 | * @param classExpression 1711 | * @param direct 1712 | * @return superClasses 1713 | * @throws ClassExpressionException 1714 | */ 1715 | public synchronized List getSuperClasses(String classExpression, boolean direct) throws ClassExpressionException { 1716 | OWLClassExpression owlClassExpression = parseClassExpression(classExpression); 1717 | return getSuperClasses(owlClassExpression, direct); 1718 | } 1719 | 1720 | /** 1721 | * Returns the list of super classes from the OWL expression. 1722 | * The second parameter is a flag telling whether only the direct classes 1723 | * should be returned. 1724 | * @param owlClassExpression 1725 | * @param direct 1726 | * @return subClasses 1727 | */ 1728 | private List getSuperClasses(OWLClassExpression owlClassExpression, boolean direct) { 1729 | Set superClasses = null; 1730 | //Can be simplified once Elk would have implemented a better way to deal with anonymous classes 1731 | if(owlClassExpression.isAnonymous()){ 1732 | OWLClass anonymousClass = getTemporaryAnonymousClass(owlClassExpression); 1733 | this.classify(); 1734 | superClasses = this.reasoner.getSuperClasses(anonymousClass, direct).getFlattened(); 1735 | removeTemporaryAnonymousClass(anonymousClass); 1736 | }else{ 1737 | if(!this.isClassified){ 1738 | this.classify(); 1739 | } 1740 | superClasses = this.reasoner.getSuperClasses(owlClassExpression, direct).getFlattened(); 1741 | } 1742 | return sortClasses(superClasses); 1743 | } 1744 | 1745 | 1746 | /** 1747 | * Returns the list of super classes from the label expression. 1748 | * The second parameter is a flag telling whether only the direct classes 1749 | * should be returned. The method is synchronized in order 1750 | * to avoid concurrency problems that could arise in threaded 1751 | * environment (such as a web server with a shared Brain instance). 1752 | * @param labelClassExpression 1753 | * @param direct 1754 | * @return superClasses 1755 | * @throws ClassExpressionException 1756 | */ 1757 | public synchronized List getSuperClassesFromLabel(String labelClassExpression, boolean direct) throws ClassExpressionException { 1758 | OWLClassExpression owlClassExpression = parseLabelClassExpression(labelClassExpression); 1759 | return getSuperClasses(owlClassExpression, direct); 1760 | } 1761 | 1762 | 1763 | /** 1764 | * Retrieves the named equivalent classes corresponding to the class expression. 1765 | * The method is synchronized in order to avoid concurrency problems 1766 | * that could arise in threaded 1767 | * environment (such as a web server with a shared Brain instance). 1768 | * @return equivalentClasses 1769 | * @throws ClassExpressionException 1770 | * @throws ClassExpressionException 1771 | */ 1772 | public synchronized List getEquivalentClasses(String classExpression) throws ClassExpressionException { 1773 | OWLClassExpression owlClassExpression = parseClassExpression(classExpression); 1774 | return getEquivalentClasses(owlClassExpression); 1775 | } 1776 | 1777 | 1778 | /** 1779 | * Returns the list of equivalent classes from the OWL expression. 1780 | * should be returned. 1781 | * @param owlClassExpression 1782 | * @return equivalentClasses 1783 | */ 1784 | private List getEquivalentClasses(OWLClassExpression owlClassExpression) { 1785 | Set equivalentClasses = null; 1786 | //Can be simplified once Elk would have implemented a better way to deal with anonymous classes 1787 | if(owlClassExpression.isAnonymous()){ 1788 | OWLClass anonymousClass = getTemporaryAnonymousClass(owlClassExpression); 1789 | this.classify(); 1790 | equivalentClasses = this.reasoner.getEquivalentClasses(anonymousClass).getEntitiesMinus(anonymousClass); 1791 | removeTemporaryAnonymousClass(anonymousClass); 1792 | }else{ 1793 | if(!this.isClassified){ 1794 | this.classify(); 1795 | } 1796 | equivalentClasses = this.reasoner.getEquivalentClasses(owlClassExpression).getEntitiesMinus((OWLClass) owlClassExpression); 1797 | } 1798 | return sortClasses(equivalentClasses); 1799 | } 1800 | 1801 | /** 1802 | * Retrieves the named equivalent classes corresponding to the label class expression. 1803 | * The method is synchronized in order to avoid concurrency problems 1804 | * that could arise in threaded 1805 | * environment (such as a web server with a shared Brain instance). 1806 | * @return equivalentClasses 1807 | * @throws ClassExpressionException 1808 | * @throws ClassExpressionException 1809 | */ 1810 | public synchronized List getEquivalentClassesFromLabel(String labelClassExpression) throws ClassExpressionException { 1811 | OWLClassExpression owlClassExpression = parseLabelClassExpression(labelClassExpression); 1812 | return getEquivalentClasses(owlClassExpression); 1813 | } 1814 | 1815 | /** 1816 | * Removes any class. Should be used to remove temporary classes used for expressions only at the moment. 1817 | * @param anonymousClass 1818 | */ 1819 | private void removeTemporaryAnonymousClass(OWLClass anonymousClass) { 1820 | OWLEntityRemover remover = new OWLEntityRemover(this.manager, Collections.singleton(this.ontology)); 1821 | anonymousClass.accept(remover); 1822 | this.manager.applyChanges(remover.getChanges()); 1823 | remover.reset(); 1824 | update(); 1825 | } 1826 | 1827 | /** 1828 | * Add a temporary class to the ontology based on the expression. Helpful because Elk doesn't support 1829 | * anonymous queries at the moment. To be removed in the future. 1830 | * @param classExpression 1831 | */ 1832 | private OWLClass getTemporaryAnonymousClass(OWLClassExpression classExpression) { 1833 | IRI anonymousIri = IRI.create("temp"); 1834 | int counter = 0; 1835 | while (this.ontology.containsClassInSignature(anonymousIri)) { 1836 | anonymousIri = IRI.create("temp" + counter); 1837 | counter++; 1838 | } 1839 | OWLClass anonymousClass = this.factory.getOWLClass(anonymousIri); 1840 | OWLEquivalentClassesAxiom equivalenceAxiom = this.factory.getOWLEquivalentClassesAxiom(anonymousClass, classExpression); 1841 | addAxiom(equivalenceAxiom); 1842 | this.reasoner.flush(); 1843 | return anonymousClass; 1844 | } 1845 | 1846 | /** 1847 | * Sort the classes based on their short forms. 1848 | * @param classes 1849 | * @return listClasses 1850 | */ 1851 | private List sortClasses(Set classes) { 1852 | List listClasses = new ArrayList(); 1853 | for (OWLClass owlClass : classes) { 1854 | if(!owlClass.isOWLNothing()){ 1855 | listClasses.add(this.bidiShortFormProvider.getShortForm(owlClass)); 1856 | } 1857 | } 1858 | Collections.sort(listClasses); 1859 | return listClasses; 1860 | } 1861 | 1862 | /** 1863 | * Sort the individuals based on their short forms. 1864 | * @param individuals 1865 | * @return listIndividuals 1866 | */ 1867 | private List sortInstances(Set individuals) { 1868 | List listIndividuals = new ArrayList(); 1869 | for (OWLNamedIndividual owlIndividual : individuals) { 1870 | listIndividuals.add(this.bidiShortFormProvider.getShortForm(owlIndividual)); 1871 | } 1872 | Collections.sort(listIndividuals); 1873 | return listIndividuals; 1874 | } 1875 | 1876 | /** 1877 | * Returns the list of individuals from the expression. 1878 | * The second parameter is a flag telling whether only the individuals 1879 | * should be returned. The method is synchronized in order 1880 | * to avoid concurrency problems that could arise in threaded 1881 | * environment (such as a web server with a shared Brain instance). 1882 | * @param classExpression 1883 | * @param direct 1884 | * @return individuals 1885 | * @throws ClassExpressionException 1886 | */ 1887 | public synchronized List getInstances(String classExpression, boolean direct) throws ClassExpressionException { 1888 | OWLClassExpression owlClassExpression = parseClassExpression(classExpression); 1889 | return getInstances(owlClassExpression, direct); 1890 | } 1891 | 1892 | /** 1893 | * Returns the list of named individuals from the OWL expression. 1894 | * The second parameter is a flag telling whether only the direct named individuals 1895 | * should be returned. 1896 | * @param owlClassExpression 1897 | * @param direct 1898 | * @return instances 1899 | */ 1900 | private List getInstances(OWLClassExpression owlClassExpression, boolean direct) { 1901 | Set instances = null; 1902 | //Can be simplified once Elk would have implemented a better way to deal with anonymous classes 1903 | if(owlClassExpression.isAnonymous()){ 1904 | OWLClass anonymousClass = getTemporaryAnonymousClass(owlClassExpression); 1905 | this.classify(); 1906 | instances = this.reasoner.getInstances(anonymousClass, direct).getFlattened(); 1907 | removeTemporaryAnonymousClass(anonymousClass); 1908 | }else{ 1909 | if(!this.isClassified){ 1910 | this.classify(); 1911 | } 1912 | instances = this.reasoner.getInstances(owlClassExpression, direct).getFlattened(); 1913 | } 1914 | return sortInstances(instances); 1915 | } 1916 | 1917 | 1918 | /** 1919 | * Returns the list of instances (named individuals) from the label expression. 1920 | * The second parameter is a flag telling whether only the direct individuals 1921 | * should be returned. The method is synchronized in order 1922 | * to avoid concurrency problems that could arise in threaded 1923 | * environment (such as a web server with a shared Brain instance). 1924 | * @param labelClassExpression 1925 | * @param direct 1926 | * @return instances 1927 | * @throws ClassExpressionException 1928 | */ 1929 | public synchronized List getInstancesFromLabel(String labelClassExpression, boolean direct) throws ClassExpressionException { 1930 | OWLClassExpression owlClassExpression = parseLabelClassExpression(labelClassExpression); 1931 | return getInstances(owlClassExpression, direct); 1932 | } 1933 | 1934 | 1935 | /** 1936 | * Test whether an OWLClass is the subclass of an other. 1937 | * @param presumedSubClass 1938 | * @param presumedSuperClass 1939 | * @param direct 1940 | * @return isASubClass 1941 | * @throws ClassExpressionException 1942 | */ 1943 | public boolean isSubClass(String presumedSubClass, String presumedSuperClass, boolean direct) throws ClassExpressionException { 1944 | List subClasses = this.getSubClasses(presumedSuperClass, direct); 1945 | boolean contained = subClasses.contains(presumedSubClass); 1946 | return contained; 1947 | } 1948 | 1949 | /** 1950 | * Test whether an OWLClass is the super class of an other. 1951 | * @param presumedSuperClass 1952 | * @param presumedSubClass 1953 | * @param direct 1954 | * @return isASuperClass 1955 | * @throws ClassExpressionException 1956 | */ 1957 | public boolean isSuperClass(String presumedSuperClass, String presumedSubClass, boolean direct) throws ClassExpressionException { 1958 | List superClasses = this.getSubClasses(presumedSuperClass, direct); 1959 | boolean contained = superClasses.contains(presumedSubClass); 1960 | return contained; 1961 | } 1962 | 1963 | /** 1964 | * Returns the unsatisfiable classes (aka no possible instances). 1965 | * @return unsatisfiableClasses 1966 | */ 1967 | public List getUnsatisfiableClasses() { 1968 | if(!this.isClassified){ 1969 | this.classify(); 1970 | } 1971 | Set unsatisfiableClasses = this.reasoner.getUnsatisfiableClasses().getEntitiesMinusBottom(); 1972 | return sortClasses(unsatisfiableClasses); 1973 | } 1974 | 1975 | /** 1976 | * Free the resources used by the reasoner. 1977 | * Once this method is called, the reasoner is destroyed and replaced 1978 | * by a fresh one. Re-classification is needed before re-querying. 1979 | */ 1980 | public void sleep() { 1981 | this.reasoner.flush(); 1982 | this.reasoner.dispose(); 1983 | if(this.configuration == null){ 1984 | this.reasoner = this.getReasonerFactory().createReasoner(this.ontology); 1985 | }else{ 1986 | this.reasoner = this.getReasonerFactory().createReasoner(this.ontology, this.configuration); 1987 | } 1988 | this.isClassified = false; 1989 | } 1990 | 1991 | /** 1992 | * Checks if the class (short form) is already inside the brain. Useful while 1993 | * parsing in order to avoid errors. 1994 | * @return whether the class is known or not. 1995 | */ 1996 | public boolean knowsClass(String owlClass) { 1997 | try { 1998 | this.getOWLClass(owlClass); 1999 | return true; 2000 | } catch (NonExistingClassException e) { 2001 | return false; 2002 | } 2003 | } 2004 | 2005 | /** 2006 | * Checks if the object property (short form) is already inside the brain. Useful while 2007 | * parsing in order to avoid errors. 2008 | * @return whether the class is known or not. 2009 | */ 2010 | public boolean knowsObjectProperty(String owlObjectProperty) { 2011 | try { 2012 | this.getOWLObjectProperty(owlObjectProperty); 2013 | return true; 2014 | }catch(NonExistingObjectPropertyException e){ 2015 | return false; 2016 | } 2017 | } 2018 | 2019 | /** 2020 | * Checks if the data property is already inside the brain. Useful while 2021 | * parsing in order to avoid errors. 2022 | * @return whether the class is known or not. 2023 | */ 2024 | public boolean knowsDataProperty(String owlDataProperty) { 2025 | try { 2026 | this.getOWLDataProperty(owlDataProperty); 2027 | return true; 2028 | }catch(NonExistingDataPropertyException e){ 2029 | return false; 2030 | } 2031 | } 2032 | 2033 | /** 2034 | * Checks if the annotation property is already inside the brain. Useful while 2035 | * parsing in order to avoid errors. 2036 | * @return whether the class is known or not. 2037 | */ 2038 | public boolean knowsAnnotationProperty(String owlAnnotationProperty) { 2039 | try { 2040 | this.getOWLAnnotationProperty(owlAnnotationProperty); 2041 | return true; 2042 | }catch(NonExistingAnnotationPropertyException e){ 2043 | return false; 2044 | } 2045 | } 2046 | 2047 | /** 2048 | * Calculates the Jaccard index between two classes of the ontology. 2049 | * The super classes of each classes are compared... 2050 | * http://en.wikipedia.org/wiki/Jaccard_index 2051 | * http://webwhompers.com/set-theory.html 2052 | * @return The Jaccard index 2053 | * @throws ClassExpressionException 2054 | */ 2055 | //TODO finish the doc 2056 | public float getJaccardSimilarityIndex(String class1, String class2) throws ClassExpressionException { 2057 | float intersection = 0; 2058 | List superClasses1 = this.getSuperClasses(class1, false); 2059 | superClasses1.add(class1); 2060 | List superClasses2 = this.getSuperClasses(class2, false); 2061 | superClasses2.add(class2); 2062 | 2063 | float sizeSet1 = 0; 2064 | float sizeSet2 = superClasses2.size(); 2065 | for (String superClass1 : superClasses1) { 2066 | if(superClasses2.contains(superClass1)){ 2067 | intersection++; 2068 | sizeSet2--; 2069 | }else{ 2070 | sizeSet1++; 2071 | } 2072 | } 2073 | 2074 | float union = intersection + sizeSet2 + sizeSet1; 2075 | float index = intersection/union; 2076 | return index; 2077 | } 2078 | 2079 | //TODO doc 2080 | //Groupwise similarity coefficient 2081 | public float getJaccardSimilarityIndex(List classes1, List classes2) throws ClassExpressionException { 2082 | List allClasses1 = new ArrayList(); 2083 | List allClasses2 = new ArrayList(); 2084 | 2085 | for (String class1 : classes1) { 2086 | List superClasses1 = this.getSuperClasses(class1, false); 2087 | superClasses1.add(class1); 2088 | for (String superClass1 : superClasses1) { 2089 | if(!allClasses1.contains(superClass1)){ 2090 | allClasses1.add(superClass1); 2091 | } 2092 | } 2093 | } 2094 | 2095 | for (String class2 : classes2) { 2096 | List superClasses2 = this.getSuperClasses(class2, false); 2097 | superClasses2.add(class2); 2098 | for (String superClass2 : superClasses2) { 2099 | if(!allClasses2.contains(superClass2)){ 2100 | allClasses2.add(superClass2); 2101 | } 2102 | } 2103 | } 2104 | 2105 | float intersection = 0; 2106 | float sizeSet1 = 0; 2107 | float sizeSet2 = allClasses2.size(); 2108 | for (String superClass1 : allClasses1) { 2109 | if(allClasses2.contains(superClass1)){ 2110 | intersection++; 2111 | sizeSet2--; 2112 | }else{ 2113 | sizeSet1++; 2114 | } 2115 | } 2116 | 2117 | float union = intersection + sizeSet2 + sizeSet1; 2118 | float index = intersection/union; 2119 | return index; 2120 | } 2121 | 2122 | } 2123 | --------------------------------------------------------------------------------