001/** 002 * Copyright (C) 2007 - 2016, Jens Lehmann 003 * 004 * This file is part of DL-Learner. 005 * 006 * DL-Learner is free software; you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation; either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * DL-Learner is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 018 */ 019package org.dllearner.utilities.owl; 020 021import java.util.ArrayList; 022import java.util.Collections; 023import java.util.List; 024 025import org.dllearner.core.owl.OWLObjectIntersectionOfImplExt; 026import org.dllearner.core.owl.OWLObjectUnionOfImplExt; 027import org.semanticweb.owlapi.model.OWLClass; 028import org.semanticweb.owlapi.model.OWLClassExpression; 029import org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx; 030import org.semanticweb.owlapi.model.OWLDataAllValuesFrom; 031import org.semanticweb.owlapi.model.OWLDataExactCardinality; 032import org.semanticweb.owlapi.model.OWLDataFactory; 033import org.semanticweb.owlapi.model.OWLDataHasValue; 034import org.semanticweb.owlapi.model.OWLDataMaxCardinality; 035import org.semanticweb.owlapi.model.OWLDataMinCardinality; 036import org.semanticweb.owlapi.model.OWLDataSomeValuesFrom; 037import org.semanticweb.owlapi.model.OWLObjectAllValuesFrom; 038import org.semanticweb.owlapi.model.OWLObjectComplementOf; 039import org.semanticweb.owlapi.model.OWLObjectExactCardinality; 040import org.semanticweb.owlapi.model.OWLObjectHasSelf; 041import org.semanticweb.owlapi.model.OWLObjectHasValue; 042import org.semanticweb.owlapi.model.OWLObjectIntersectionOf; 043import org.semanticweb.owlapi.model.OWLObjectMaxCardinality; 044import org.semanticweb.owlapi.model.OWLObjectMinCardinality; 045import org.semanticweb.owlapi.model.OWLObjectOneOf; 046import org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom; 047import org.semanticweb.owlapi.model.OWLObjectUnionOf; 048 049/** 050 * Remove disjunctions in disjunctions and conjunctions in conjunctions. 051 * @author Lorenz Buehmann 052 * 053 */ 054public class OWLClassExpressionCleaner implements OWLClassExpressionVisitorEx<OWLClassExpression>{ 055 056 private OWLDataFactory df; 057 058 public OWLClassExpressionCleaner(OWLDataFactory df) { 059 this.df = df; 060 } 061 062 /* (non-Javadoc) 063 * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLClass) 064 */ 065 @Override 066 public OWLClassExpression visit(OWLClass ce) { 067 return ce; 068 } 069 070 /* (non-Javadoc) 071 * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectIntersectionOf) 072 */ 073 @Override 074 public OWLClassExpression visit(OWLObjectIntersectionOf ce) { 075 List<OWLClassExpression> operands = new ArrayList<>(); 076 for (OWLClassExpression operand : ce.getOperands()) { 077 OWLClassExpression newOperand = operand.accept(this); 078 if(newOperand instanceof OWLObjectIntersectionOf){ 079 operands.addAll(((OWLObjectIntersectionOf) newOperand).getOperandsAsList()); 080 } else { 081 operands.add(newOperand); 082 } 083 } 084 Collections.sort(operands); 085 return new OWLObjectIntersectionOfImplExt(operands); 086 } 087 088 /* (non-Javadoc) 089 * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectUnionOf) 090 */ 091 @Override 092 public OWLClassExpression visit(OWLObjectUnionOf ce) { 093 List<OWLClassExpression> operands = new ArrayList<>(); 094 for (OWLClassExpression operand : ce.getOperands()) { 095 OWLClassExpression newOperand = operand.accept(this); 096 if(newOperand instanceof OWLObjectUnionOf){ 097 operands.addAll(((OWLObjectUnionOf) newOperand).getOperandsAsList()); 098 } else { 099 operands.add(newOperand); 100 } 101 } 102 Collections.sort(operands); 103 return new OWLObjectUnionOfImplExt(operands); 104 } 105 106 /* (non-Javadoc) 107 * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectComplementOf) 108 */ 109 @Override 110 public OWLClassExpression visit(OWLObjectComplementOf ce) { 111 OWLClassExpression result = ce; 112 OWLClassExpression operand = ce.getOperand(); 113 if(operand.isAnonymous()){ 114 OWLClassExpression newOperand = operand.accept(this); 115 result = df.getOWLObjectComplementOf(newOperand); 116 } 117 return result; 118 } 119 120 /* (non-Javadoc) 121 * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom) 122 */ 123 @Override 124 public OWLClassExpression visit(OWLObjectSomeValuesFrom ce) { 125 OWLClassExpression result = ce; 126 OWLClassExpression operand = ce.getFiller(); 127 if(operand.isAnonymous()){ 128 OWLClassExpression newOperand = operand.accept(this); 129 result = df.getOWLObjectSomeValuesFrom(ce.getProperty(), newOperand); 130 } 131 return result; 132 } 133 134 /* (non-Javadoc) 135 * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectAllValuesFrom) 136 */ 137 @Override 138 public OWLClassExpression visit(OWLObjectAllValuesFrom ce) { 139 OWLClassExpression result = ce; 140 OWLClassExpression operand = ce.getFiller(); 141 if(operand.isAnonymous()){ 142 OWLClassExpression newOperand = operand.accept(this); 143 result = df.getOWLObjectAllValuesFrom(ce.getProperty(), newOperand); 144 } 145 return result; 146 } 147 148 /* (non-Javadoc) 149 * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectHasValue) 150 */ 151 @Override 152 public OWLClassExpression visit(OWLObjectHasValue ce) { 153 return ce; 154 } 155 156 /* (non-Javadoc) 157 * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectMinCardinality) 158 */ 159 @Override 160 public OWLClassExpression visit(OWLObjectMinCardinality ce) { 161 OWLClassExpression result = ce; 162 OWLClassExpression operand = ce.getFiller(); 163 if(operand.isAnonymous()){ 164 OWLClassExpression newOperand = operand.accept(this); 165 result = df.getOWLObjectMinCardinality(ce.getCardinality(), ce.getProperty(), newOperand); 166 } 167 return result; 168 } 169 170 /* (non-Javadoc) 171 * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectExactCardinality) 172 */ 173 @Override 174 public OWLClassExpression visit(OWLObjectExactCardinality ce) { 175 OWLClassExpression result = ce; 176 OWLClassExpression operand = ce.getFiller(); 177 if(operand.isAnonymous()){ 178 OWLClassExpression newOperand = operand.accept(this); 179 result = df.getOWLObjectExactCardinality(ce.getCardinality(), ce.getProperty(), newOperand); 180 } 181 return result; 182 } 183 184 /* (non-Javadoc) 185 * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectMaxCardinality) 186 */ 187 @Override 188 public OWLClassExpression visit(OWLObjectMaxCardinality ce) { 189 OWLClassExpression result = ce; 190 OWLClassExpression operand = ce.getFiller(); 191 if(operand.isAnonymous()){ 192 OWLClassExpression newOperand = operand.accept(this); 193 result = df.getOWLObjectMaxCardinality(ce.getCardinality(), ce.getProperty(), newOperand); 194 } 195 return result; 196 } 197 198 /* (non-Javadoc) 199 * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectHasSelf) 200 */ 201 @Override 202 public OWLClassExpression visit(OWLObjectHasSelf ce) { 203 return ce; 204 } 205 206 /* (non-Javadoc) 207 * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectOneOf) 208 */ 209 @Override 210 public OWLClassExpression visit(OWLObjectOneOf ce) { 211 return ce; 212 } 213 214 /* (non-Javadoc) 215 * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLDataSomeValuesFrom) 216 */ 217 @Override 218 public OWLClassExpression visit(OWLDataSomeValuesFrom ce) { 219 return ce; 220 } 221 222 /* (non-Javadoc) 223 * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLDataAllValuesFrom) 224 */ 225 @Override 226 public OWLClassExpression visit(OWLDataAllValuesFrom ce) { 227 return ce; 228 } 229 230 /* (non-Javadoc) 231 * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLDataHasValue) 232 */ 233 @Override 234 public OWLClassExpression visit(OWLDataHasValue ce) { 235 return ce; 236 } 237 238 /* (non-Javadoc) 239 * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLDataMinCardinality) 240 */ 241 @Override 242 public OWLClassExpression visit(OWLDataMinCardinality ce) { 243 return ce; 244 } 245 246 /* (non-Javadoc) 247 * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLDataExactCardinality) 248 */ 249 @Override 250 public OWLClassExpression visit(OWLDataExactCardinality ce) { 251 return ce; 252 } 253 254 /* (non-Javadoc) 255 * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLDataMaxCardinality) 256 */ 257 @Override 258 public OWLClassExpression visit(OWLDataMaxCardinality ce) { 259 return ce; 260 } 261 262}