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.algorithms.qtl.util;
020
021import org.dllearner.core.StringRenderer;
022import org.dllearner.core.StringRenderer.Rendering;
023import org.semanticweb.owlapi.model.*;
024import org.semanticweb.owlapi.util.DefaultPrefixManager;
025import org.semanticweb.owlapi.vocab.OWLFacet;
026import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl;
027
028import javax.annotation.Nonnull;
029import java.util.Collections;
030import java.util.HashSet;
031import java.util.Set;
032
033/**
034 * @author Lorenz Buehmann
035 *
036 */
037public class ClassExpressionLiteralCombination implements OWLClassExpressionVisitorEx<Set<OWLClassExpression>>, OWLDataRangeVisitorEx<Set<OWLDataRange>>{
038        
039        private OWLDataFactory df =  new OWLDataFactoryImpl();
040
041        /* (non-Javadoc)
042         * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLClass)
043         */
044        @Nonnull
045        @Override
046        public Set<OWLClassExpression> visit(@Nonnull OWLClass ce) {
047                return Collections.singleton(ce);
048        }
049
050        /* (non-Javadoc)
051         * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectIntersectionOf)
052         */
053        @Nonnull
054        @Override
055        public Set<OWLClassExpression> visit(@Nonnull OWLObjectIntersectionOf ce) {
056                Set<OWLClassExpression> expressions = new HashSet<>();
057                Set<Set<OWLClassExpression>> combinations = new HashSet<>();
058                for (int i = 0; i < ce.getOperands().size(); i++) {
059                        Set<OWLClassExpression> tmp = new HashSet<>();
060                        combinations.add(tmp);
061                }
062                for (OWLClassExpression operand : ce.getOperands()) {
063                        Set<Set<OWLClassExpression>> combinationsTmp = new HashSet<>();
064                        Set<OWLClassExpression> newOperands = operand.accept(this);
065                        for (Set<OWLClassExpression> set : combinations) {
066                                for (OWLClassExpression newOp : newOperands) {
067                                        Set<OWLClassExpression> tmp = new HashSet<>();
068                                        tmp.addAll(set);
069                                        tmp.add(newOp);
070                                        combinationsTmp.add(tmp);
071                                }
072                        }
073                        combinations = combinationsTmp;
074                }
075                for (Set<OWLClassExpression> combination : combinations) {
076                        expressions.add(df.getOWLObjectIntersectionOf(combination));
077                }
078                return expressions;
079        }
080
081        /* (non-Javadoc)
082         * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectUnionOf)
083         */
084        @Nonnull
085        @Override
086        public Set<OWLClassExpression> visit(@Nonnull OWLObjectUnionOf ce) {
087                return Collections.singleton(ce);
088        }
089
090        /* (non-Javadoc)
091         * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectComplementOf)
092         */
093        @Nonnull
094        @Override
095        public Set<OWLClassExpression> visit(@Nonnull OWLObjectComplementOf ce) {
096                return Collections.singleton(ce);
097        }
098
099        /* (non-Javadoc)
100         * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom)
101         */
102        @Nonnull
103        @Override
104        public Set<OWLClassExpression> visit(@Nonnull OWLObjectSomeValuesFrom ce) {
105                Set<OWLClassExpression> expressions = new HashSet<>();
106                Set<OWLClassExpression> newFillers = ce.getFiller().accept(this);
107                for (OWLClassExpression newFiller : newFillers) {
108                        expressions.add(df.getOWLObjectSomeValuesFrom(ce.getProperty(), newFiller));
109                }
110                return expressions;
111        }
112
113        /* (non-Javadoc)
114         * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectAllValuesFrom)
115         */
116        @Nonnull
117        @Override
118        public Set<OWLClassExpression> visit(@Nonnull OWLObjectAllValuesFrom ce) {
119                return Collections.singleton(ce);
120        }
121
122        /* (non-Javadoc)
123         * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectHasValue)
124         */
125        @Nonnull
126        @Override
127        public Set<OWLClassExpression> visit(@Nonnull OWLObjectHasValue ce) {
128                return Collections.singleton(ce);
129        }
130
131        /* (non-Javadoc)
132         * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectMinCardinality)
133         */
134        @Nonnull
135        @Override
136        public Set<OWLClassExpression> visit(@Nonnull OWLObjectMinCardinality ce) {
137                return Collections.singleton(ce);
138        }
139
140        /* (non-Javadoc)
141         * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectExactCardinality)
142         */
143        @Nonnull
144        @Override
145        public Set<OWLClassExpression> visit(@Nonnull OWLObjectExactCardinality ce) {
146                return Collections.singleton(ce);
147        }
148
149        /* (non-Javadoc)
150         * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectMaxCardinality)
151         */
152        @Nonnull
153        @Override
154        public Set<OWLClassExpression> visit(@Nonnull OWLObjectMaxCardinality ce) {
155                return Collections.singleton(ce);
156        }
157
158        /* (non-Javadoc)
159         * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectHasSelf)
160         */
161        @Nonnull
162        @Override
163        public Set<OWLClassExpression> visit(@Nonnull OWLObjectHasSelf ce) {
164                return Collections.singleton(ce);
165        }
166
167        /* (non-Javadoc)
168         * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLObjectOneOf)
169         */
170        @Nonnull
171        @Override
172        public Set<OWLClassExpression> visit(@Nonnull OWLObjectOneOf ce) {
173                return Collections.singleton(ce);
174        }
175
176        /* (non-Javadoc)
177         * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLDataSomeValuesFrom)
178         */
179        @Nonnull
180        @Override
181        public Set<OWLClassExpression> visit(@Nonnull OWLDataSomeValuesFrom ce) {
182                Set<OWLClassExpression> expressions = new HashSet<>();
183                Set<OWLDataRange> newDataRanges = ce.getFiller().accept(this);
184                for (OWLDataRange newDataRange : newDataRanges) {
185                        expressions.add(df.getOWLDataSomeValuesFrom(ce.getProperty(), newDataRange));
186                }
187                return expressions;
188        }
189
190        /* (non-Javadoc)
191         * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLDataAllValuesFrom)
192         */
193        @Nonnull
194        @Override
195        public Set<OWLClassExpression> visit(@Nonnull OWLDataAllValuesFrom ce) {
196                return Collections.singleton(ce);
197        }
198
199        /* (non-Javadoc)
200         * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLDataHasValue)
201         */
202        @Nonnull
203        @Override
204        public Set<OWLClassExpression> visit(@Nonnull OWLDataHasValue ce) {
205                return Collections.singleton(ce);
206        }
207
208        /* (non-Javadoc)
209         * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLDataMinCardinality)
210         */
211        @Nonnull
212        @Override
213        public Set<OWLClassExpression> visit(@Nonnull OWLDataMinCardinality ce) {
214                return Collections.singleton(ce);
215        }
216
217        /* (non-Javadoc)
218         * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLDataExactCardinality)
219         */
220        @Nonnull
221        @Override
222        public Set<OWLClassExpression> visit(@Nonnull OWLDataExactCardinality ce) {
223                return Collections.singleton(ce);
224        }
225
226        /* (non-Javadoc)
227         * @see org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx#visit(org.semanticweb.owlapi.model.OWLDataMaxCardinality)
228         */
229        @Nonnull
230        @Override
231        public Set<OWLClassExpression> visit(@Nonnull OWLDataMaxCardinality ce) {
232                return Collections.singleton(ce);
233        }
234
235        /* (non-Javadoc)
236         * @see org.semanticweb.owlapi.model.OWLDataRangeVisitorEx#visit(org.semanticweb.owlapi.model.OWLDatatype)
237         */
238        @Nonnull
239        @Override
240        public Set<OWLDataRange> visit(@Nonnull OWLDatatype dr) {
241                return Collections.singleton(dr);
242        }
243
244        /* (non-Javadoc)
245         * @see org.semanticweb.owlapi.model.OWLDataRangeVisitorEx#visit(org.semanticweb.owlapi.model.OWLDataOneOf)
246         */
247        @Nonnull
248        @Override
249        public Set<OWLDataRange> visit(@Nonnull OWLDataOneOf dr) {
250                return Collections.singleton(dr);
251        }
252
253        /* (non-Javadoc)
254         * @see org.semanticweb.owlapi.model.OWLDataRangeVisitorEx#visit(org.semanticweb.owlapi.model.OWLDataComplementOf)
255         */
256        @Nonnull
257        @Override
258        public Set<OWLDataRange> visit(@Nonnull OWLDataComplementOf dr) {
259                return Collections.singleton(dr);
260        }
261
262        /* (non-Javadoc)
263         * @see org.semanticweb.owlapi.model.OWLDataRangeVisitorEx#visit(org.semanticweb.owlapi.model.OWLDataIntersectionOf)
264         */
265        @Nonnull
266        @Override
267        public Set<OWLDataRange> visit(@Nonnull OWLDataIntersectionOf dr) {
268                return Collections.singleton(dr);
269        }
270
271        /* (non-Javadoc)
272         * @see org.semanticweb.owlapi.model.OWLDataRangeVisitorEx#visit(org.semanticweb.owlapi.model.OWLDataUnionOf)
273         */
274        @Nonnull
275        @Override
276        public Set<OWLDataRange> visit(@Nonnull OWLDataUnionOf dr) {
277                return Collections.singleton(dr);
278        }
279
280        /* (non-Javadoc)
281         * @see org.semanticweb.owlapi.model.OWLDataRangeVisitorEx#visit(org.semanticweb.owlapi.model.OWLDatatypeRestriction)
282         */
283        @Nonnull
284        @Override
285        public Set<OWLDataRange> visit(@Nonnull OWLDatatypeRestriction dr) {
286                Set<OWLDataRange> dataRanges = new HashSet<>();
287                Set<OWLFacetRestriction> facetRestrictions = dr.getFacetRestrictions();
288                OWLLiteral min = null;
289                OWLLiteral max = null;
290                for (OWLFacetRestriction facetRestriction : facetRestrictions) {
291                        OWLFacet facet = facetRestriction.getFacet();
292                        switch (facet) {
293                                case MIN_INCLUSIVE:
294                                        min = facetRestriction.getFacetValue();
295                                        break;
296                                case MAX_INCLUSIVE:
297                                        max = facetRestriction.getFacetValue();
298                                        break;
299                                default:
300                                        throw new IllegalArgumentException("Facet not allowed for transformation.");
301                        }
302                }
303//              dataRanges.add(dr);
304                dataRanges.add(df.getOWLDatatypeRestriction(dr.getDatatype(), df.getOWLFacetRestriction(OWLFacet.MIN_INCLUSIVE, min)));
305                dataRanges.add(df.getOWLDatatypeRestriction(dr.getDatatype(), df.getOWLFacetRestriction(OWLFacet.MAX_INCLUSIVE, max)));
306//              dataRanges.add(dr.getDatatype());
307                return dataRanges;
308        }
309        
310        
311        public static void main(String[] args) throws Exception {
312                StringRenderer.setRenderer(Rendering.MANCHESTER_SYNTAX);
313                OWLDataFactoryImpl df = new OWLDataFactoryImpl();
314                PrefixManager pm = new DefaultPrefixManager();
315                pm.setDefaultPrefix(":");
316                OWLClass A = df.getOWLClass("A", pm );
317                OWLDataProperty s = df.getOWLDataProperty("s", pm);
318                OWLDataProperty t = df.getOWLDataProperty("t", pm);
319                OWLDatatypeRestriction dr1 = df.getOWLDatatypeMinMaxInclusiveRestriction(1.0, 2.0);
320                OWLDatatypeRestriction dr2 = df.getOWLDatatypeMinMaxInclusiveRestriction(15, 100);
321                OWLClassExpression ce = df.getOWLObjectIntersectionOf(A,
322                                df.getOWLDataSomeValuesFrom(s, dr1),
323                                df.getOWLDataSomeValuesFrom(t, dr2)                                             
324                                );
325                Set<OWLClassExpression> expressions = ce.accept(new ClassExpressionLiteralCombination());
326                for (OWLClassExpression expr : expressions) {
327                        System.out.println(expr);
328                }
329        }
330
331}