001package conexp.fx.core.util; 002 003import java.io.File; 004 005/* 006 * #%L 007 * Concept Explorer FX 008 * %% 009 * Copyright (C) 2010 - 2019 Francesco Kriegel 010 * %% 011 * This program is free software: you can redistribute it and/or modify 012 * it under the terms of the GNU General Public License as 013 * published by the Free Software Foundation, either version 3 of the 014 * License, or (at your option) any later version. 015 * 016 * This program is distributed in the hope that it will be useful, 017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 019 * GNU General Public License for more details. 020 * 021 * You should have received a copy of the GNU General Public 022 * License along with this program. If not, see 023 * <http://www.gnu.org/licenses/gpl-3.0.html>. 024 * #L% 025 */ 026 027import java.util.Iterator; 028 029import org.semanticweb.owlapi.model.OWLAxiom; 030import org.semanticweb.owlapi.model.OWLClass; 031import org.semanticweb.owlapi.model.OWLClassExpression; 032import org.semanticweb.owlapi.model.OWLEquivalentClassesAxiom; 033import org.semanticweb.owlapi.model.OWLNamedIndividual; 034import org.semanticweb.owlapi.model.OWLObjectAllValuesFrom; 035import org.semanticweb.owlapi.model.OWLObjectComplementOf; 036import org.semanticweb.owlapi.model.OWLObjectExactCardinality; 037import org.semanticweb.owlapi.model.OWLObjectHasSelf; 038import org.semanticweb.owlapi.model.OWLObjectIntersectionOf; 039import org.semanticweb.owlapi.model.OWLObjectMaxCardinality; 040import org.semanticweb.owlapi.model.OWLObjectMinCardinality; 041import org.semanticweb.owlapi.model.OWLObjectPropertyExpression; 042import org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom; 043import org.semanticweb.owlapi.model.OWLObjectUnionOf; 044import org.semanticweb.owlapi.model.OWLSubClassOfAxiom; 045import org.semanticweb.owlapi.model.OWLSubObjectPropertyOfAxiom; 046import org.semanticweb.owlapi.model.OWLSubPropertyChainOfAxiom; 047import org.ujmp.core.booleanmatrix.BooleanMatrix; 048 049import com.google.common.base.Function; 050 051import conexp.fx.core.collections.setlist.SetList; 052import conexp.fx.core.collections.setlist.SetLists; 053import conexp.fx.core.context.MatrixContext; 054import conexp.fx.core.exporter.CXTExporter; 055 056public class OWLUtil { 057 058 public static final String toString(final OWLClassExpression classExpression) { 059 if (classExpression.isOWLNothing()) { 060 return UnicodeSymbols.BOT; 061 } 062 if (classExpression.isOWLThing()) { 063 return UnicodeSymbols.TOP; 064 } 065 final StringBuilder s = new StringBuilder(); 066 if (classExpression instanceof OWLClass) { 067 final OWLClass c = (OWLClass) classExpression; 068 s.append(c.toString().substring(1, c.toString().length() - 1)); 069 } else if (classExpression instanceof OWLObjectComplementOf) { 070 final OWLObjectComplementOf c = (OWLObjectComplementOf) classExpression; 071 s.append(UnicodeSymbols.NEG); 072 s.append(toString(c.getOperand())); 073 } else if (classExpression instanceof OWLObjectIntersectionOf) { 074 final OWLObjectIntersectionOf c = (OWLObjectIntersectionOf) classExpression; 075 if (c.getOperands().isEmpty()) 076 return UnicodeSymbols.TOP; 077 if (c.getOperands().size() == 1) 078 return toString(c.getOperands().iterator().next()); 079 s.append("("); 080 final Iterator<OWLClassExpression> i = c.getOperands().iterator(); 081 s.append(toString(i.next())); 082 while (i.hasNext()) 083 s.append(UnicodeSymbols.SQCAP + toString(i.next())); 084 s.append(")"); 085 } else if (classExpression instanceof OWLObjectUnionOf) { 086 final OWLObjectUnionOf c = (OWLObjectUnionOf) classExpression; 087 if (c.getOperands().isEmpty()) 088 return UnicodeSymbols.BOT; 089 if (c.getOperands().size() == 1) 090 return toString(c.getOperands().iterator().next()); 091 s.append("("); 092 final Iterator<OWLClassExpression> i = c.getOperands().iterator(); 093 s.append(toString(i.next())); 094 while (i.hasNext()) 095 s.append(UnicodeSymbols.SQCUP + toString(i.next())); 096 s.append(")"); 097 } else if (classExpression instanceof OWLObjectSomeValuesFrom) { 098 final OWLObjectSomeValuesFrom c = (OWLObjectSomeValuesFrom) classExpression; 099 s.append(UnicodeSymbols.EXISTS); 100 s.append(toString(c.getProperty())); 101 s.append("."); 102 s.append(toString(c.getFiller())); 103 } else if (classExpression instanceof OWLObjectAllValuesFrom) { 104 final OWLObjectAllValuesFrom c = (OWLObjectAllValuesFrom) classExpression; 105 s.append(UnicodeSymbols.FORALL); 106 s.append(toString(c.getProperty())); 107 s.append("."); 108 s.append(toString(c.getFiller())); 109 } else if (classExpression instanceof OWLObjectHasSelf) { 110 final OWLObjectHasSelf c = (OWLObjectHasSelf) classExpression; 111 s.append(UnicodeSymbols.EXISTS); 112 s.append(toString(c.getProperty())); 113 s.append("."); 114 s.append("Self"); 115 } else if (classExpression instanceof OWLObjectMinCardinality) { 116 final OWLObjectMinCardinality c = (OWLObjectMinCardinality) classExpression; 117 s.append(UnicodeSymbols.GEQ); 118 s.append(c.getCardinality()); 119 s.append("."); 120 s.append(toString(c.getProperty())); 121 s.append("."); 122 s.append(toString(c.getFiller())); 123 } else if (classExpression instanceof OWLObjectMaxCardinality) { 124 final OWLObjectMaxCardinality c = (OWLObjectMaxCardinality) classExpression; 125 s.append(UnicodeSymbols.LEQ); 126 s.append(c.getCardinality()); 127 s.append("."); 128 s.append(toString(c.getProperty())); 129 s.append("."); 130 s.append(toString(c.getFiller())); 131 } else if (classExpression instanceof OWLObjectExactCardinality) { 132 final OWLObjectExactCardinality c = (OWLObjectExactCardinality) classExpression; 133 s.append("="); 134 s.append(c.getCardinality()); 135 s.append("."); 136 s.append(toString(c.getProperty())); 137 s.append("."); 138 s.append(toString(c.getFiller())); 139 } else { 140 s.append(classExpression.toString()); 141 } 142 return s.toString(); 143 } 144 145 public static final String toString(final OWLObjectPropertyExpression propertyExpression) { 146 return propertyExpression.toString().substring(1, propertyExpression.toString().length() - 1); 147 } 148 149 public static final String toString(final OWLAxiom axiom) { 150 final StringBuilder s = new StringBuilder(); 151 if (axiom instanceof OWLSubClassOfAxiom) { 152 final OWLSubClassOfAxiom a = (OWLSubClassOfAxiom) axiom; 153 s.append(toString(a.getSubClass())); 154 s.append(UnicodeSymbols.SQSUBSETEQ); 155 s.append(toString(a.getSuperClass())); 156 } else if (axiom instanceof OWLEquivalentClassesAxiom) { 157 final OWLEquivalentClassesAxiom a = (OWLEquivalentClassesAxiom) axiom; 158 final Iterator<OWLClassExpression> i = a.getClassExpressions().iterator(); 159 if (i.hasNext()) { 160 s.append(toString(i.next())); 161 while (i.hasNext()) 162 s.append(UnicodeSymbols.EQUIV + toString(i.next())); 163 } 164 } else if (axiom instanceof OWLSubObjectPropertyOfAxiom) { 165 final OWLSubObjectPropertyOfAxiom a = (OWLSubObjectPropertyOfAxiom) axiom; 166 s.append(toString(a.getSubProperty())); 167 s.append(UnicodeSymbols.SQSUBSETEQ); 168 s.append(toString(a.getSuperProperty())); 169 } else if (axiom instanceof OWLSubPropertyChainOfAxiom) { 170 final OWLSubPropertyChainOfAxiom a = (OWLSubPropertyChainOfAxiom) axiom; 171 final Iterator<OWLObjectPropertyExpression> i = a.getPropertyChain().iterator(); 172 if (i.hasNext()) 173 s.append(toString(i.next())); 174 while (i.hasNext()) { 175 s.append(UnicodeSymbols.CIRC); 176 s.append(toString(i.next())); 177 } 178 s.append(UnicodeSymbols.SQSUBSETEQ); 179 s.append(toString(a.getSuperProperty())); 180 } else { 181 s.append(axiom.toString()); 182 } 183 return s.toString(); 184 } 185 186 public static final String toLaTeX(final OWLClassExpression clazz) { 187 final StringBuilder str = new StringBuilder(); 188 if (clazz instanceof OWLObjectSomeValuesFrom) { 189 str.append("("); 190 OWLObjectSomeValuesFrom exrest = (OWLObjectSomeValuesFrom) clazz; 191 String r = exrest.getProperty().toString(); 192 r = r.substring(r.indexOf("#") + 1); 193 r = r.substring(0, r.length() - 1); 194 final OWLClassExpression c = exrest.getFiller(); 195 str.append("\\exists "); 196 str.append(r + "."); 197 str.append(toLaTeX(c)); 198 str.append(")"); 199 } else if (clazz instanceof OWLObjectComplementOf) { 200 str.append("\\neg("); 201 str.append(toLaTeX(((OWLObjectComplementOf) clazz).getOperand())); 202 str.append(")"); 203 } else if (clazz instanceof OWLObjectAllValuesFrom) { 204 final OWLObjectAllValuesFrom vr = (OWLObjectAllValuesFrom) clazz; 205 str.append("(\\forall "); 206 str.append(vr.getProperty().toString() + "."); 207 str.append(toLaTeX(vr.getFiller()) + ")"); 208 } else if (clazz instanceof OWLObjectMinCardinality) { 209 final OWLObjectMinCardinality qgr = (OWLObjectMinCardinality) clazz; 210 str.append("(\\geq "); 211 str.append(qgr.getCardinality()); 212 str.append(" " + qgr.getProperty() + "."); 213 str.append(toLaTeX(qgr.getFiller()) + ")"); 214 } else if (clazz instanceof OWLObjectMaxCardinality) { 215 final OWLObjectMaxCardinality qlr = (OWLObjectMaxCardinality) clazz; 216 str.append("(\\leq "); 217 str.append(qlr.getCardinality()); 218 str.append(" " + qlr.getProperty() + "."); 219 str.append(toLaTeX(qlr.getFiller()) + ")"); 220 } else if (clazz instanceof OWLObjectExactCardinality) { 221 final OWLObjectExactCardinality qer = (OWLObjectExactCardinality) clazz; 222 str.append("(= "); 223 str.append(qer.getCardinality()); 224 str.append(" " + qer.getProperty() + "."); 225 str.append(toLaTeX(qer.getFiller()) + ")"); 226 } else if (clazz instanceof OWLObjectHasSelf) { 227 final OWLObjectHasSelf sr = (OWLObjectHasSelf) clazz; 228 str.append("\\exists "); 229 str.append(sr.getProperty() + "."); 230 str.append("\\mathrm{Self}"); 231 } else if (clazz instanceof OWLObjectIntersectionOf) { 232 str.append("("); 233 OWLObjectIntersectionOf conj = (OWLObjectIntersectionOf) clazz; 234 if (conj.asConjunctSet().size() == 1) 235 str.append(toLaTeX(conj.asConjunctSet().iterator().next())); 236 boolean first = true; 237 for (OWLClassExpression c : conj.asConjunctSet()) { 238 if (first) 239 first = false; 240 else 241 str.append("\\cap"); 242 str.append(toLaTeX(c)); 243 } 244 str.append(")"); 245 } else if (clazz instanceof OWLClass) { 246 OWLClass c = (OWLClass) clazz; 247 String string = c.toString(); 248 string = string.substring(string.indexOf("#") + 1); 249 str.append(string.substring(0, string.length() - 1)); 250 } 251 return str.toString(); 252 } 253 254 public static final MatrixContext<String, String> 255 toLaTeXContext(final MatrixContext<OWLNamedIndividual, OWLClassExpression> context) { 256 final BooleanMatrix matrix = context.matrix(); 257 final SetList<String> domain = SetLists.transform(context.rowHeads(), new Function<OWLNamedIndividual, String>() { 258 259 @Override 260 public final String apply(final OWLNamedIndividual input) { 261 String string = input.toString(); 262 string = string.substring(string.indexOf("#") + 1); 263 return string.substring(0, string.length() - 1); 264 } 265 }); 266 final SetList<String> codomain = SetLists.transform(context.colHeads(), new Function<OWLClassExpression, String>() { 267 268 @Override 269 public final String apply(final OWLClassExpression input) { 270 return OWLUtil.toLaTeX(input); 271 } 272 }); 273 final MatrixContext<String, String> latexContext = 274 new MatrixContext<String, String>(domain, codomain, matrix, false); 275 return latexContext; 276 } 277 278 public static final void 279 exportLaTeXContext(final MatrixContext<OWLNamedIndividual, OWLClassExpression> context, final File outputFile) { 280 CXTExporter.<String, String> export(toLaTeXContext(context), outputFile); 281 } 282}