001 /**
002 * Copyright (C) 2007-2011, 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 */
019
020 package org.dllearner.reasoning;
021
022 import java.net.URI;
023 import java.util.Set;
024
025 import org.dllearner.core.owl.ObjectAllRestriction;
026 import org.dllearner.core.owl.NamedClass;
027 import org.dllearner.core.owl.Axiom;
028 import org.dllearner.core.owl.Nothing;
029 import org.dllearner.core.owl.Description;
030 import org.dllearner.core.owl.ClassAssertionAxiom;
031 import org.dllearner.core.owl.EquivalentClassesAxiom;
032 import org.dllearner.core.owl.ObjectSomeRestriction;
033 import org.dllearner.core.owl.FunctionalObjectPropertyAxiom;
034 import org.dllearner.core.owl.SubClassAxiom;
035 import org.dllearner.core.owl.Individual;
036 import org.dllearner.core.owl.KB;
037 import org.dllearner.core.owl.Intersection;
038 import org.dllearner.core.owl.Union;
039 import org.dllearner.core.owl.Negation;
040 import org.dllearner.core.owl.ObjectProperty;
041 import org.dllearner.core.owl.ObjectPropertyAssertion;
042 import org.dllearner.core.owl.ObjectPropertyExpression;
043 import org.dllearner.core.owl.ObjectPropertyInverse;
044 import org.dllearner.core.owl.ObjectQuantorRestriction;
045 import org.dllearner.core.owl.SubObjectPropertyAxiom;
046 import org.dllearner.core.owl.SymmetricObjectPropertyAxiom;
047 import org.dllearner.core.owl.Thing;
048 import org.dllearner.core.owl.TransitiveObjectPropertyAxiom;
049
050 /**
051 * Methods for converting internal representation to DIG and
052 * vice versa.
053 *
054 * @author jl
055 *
056 */
057 public class DIGConverter {
058
059 public static StringBuilder getDIGString(KB kb, URI kbURI) {
060 StringBuilder sb = new StringBuilder();
061 sb.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
062 sb.append("<tells xmlns=\"http://dl.kr.org/dig/2003/02/lang\" " +
063 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
064 "xsi:schemaLocation=\"http://dl.kr.org/dig/2003/02/lang\n" +
065 "http://dl-web.man.ac.uk/dig/2003/02/dig.xsd\" uri=\""+kbURI+"\">");
066 sb.append(getDIGString(kb));
067 sb.append("</tells>");
068 return sb;
069 }
070
071 public static StringBuilder getDIGString(KB kb) {
072 StringBuilder sb = new StringBuilder();
073
074 // erstmal alle Konzepte, Rollen und Individuen definieren
075 Set<NamedClass> atomicConcepts = kb.findAllAtomicConcepts();
076 Set<ObjectProperty> atomicRoles = kb.findAllAtomicRoles();
077 Set<Individual> individuals = kb.findAllIndividuals();
078
079 for(NamedClass ac : atomicConcepts)
080 sb.append("<defconcept name=\""+ac.getName()+"\"/>");
081
082 for(ObjectProperty ar : atomicRoles)
083 sb.append("<defrole name=\""+ar.getName()+"\"/>");
084
085 for(Individual individual : individuals)
086 sb.append("<defindividual name=\""+individual.getName()+"\"/>");
087
088 for(Axiom axiom : kb.getAbox()) {
089 sb.append(getDIGString(axiom));
090 }
091 for(Axiom axiom : kb.getRbox()) {
092 sb.append(getDIGString(axiom));
093 }
094 for(Axiom axiom : kb.getTbox()) {
095 sb.append(getDIGString(axiom));
096 }
097 return sb;
098 }
099
100 public static StringBuilder getDIGString(Axiom axiom) {
101 StringBuilder sb = new StringBuilder();
102
103 // ABox
104 if(axiom instanceof ObjectPropertyAssertion)
105 sb.append("<related>"+ "<individual name=\"" + ((ObjectPropertyAssertion)axiom).getIndividual1().getName() + "\"/>"
106 + getDIGString(((ObjectPropertyAssertion)axiom).getRole()) +
107 "<individual name=\"" + ((ObjectPropertyAssertion)axiom).getIndividual2().getName() + "\"/></related>\n");
108 else if(axiom instanceof ClassAssertionAxiom)
109 sb.append("<instanceof>"+ "<individual name=\"" + ((ClassAssertionAxiom)axiom).getIndividual().getName() + "\"/>" +
110 getDIGString(((ClassAssertionAxiom)axiom).getConcept()) + "</instanceof>\n");
111 // RBox
112 else if(axiom instanceof SymmetricObjectPropertyAxiom) {
113 // wird nicht direkt in DIG 1.1 unterstützt
114 // ich modelliere es hier, indem ich sage, dass eine Rolle gleich ihrem
115 // Inversen ist
116 sb.append("<equalr>");
117 sb.append("<ratom name=\""+((SymmetricObjectPropertyAxiom)axiom).getRole().getName()+"\" />\n");
118 sb.append("<inverse><ratom name=\""+((SymmetricObjectPropertyAxiom)axiom).getRole().getName()+"\" /></inverse>\n");
119 sb.append("</equalr>");
120 } else if(axiom instanceof TransitiveObjectPropertyAxiom)
121 sb.append("<transitive>"+ getDIGString(((TransitiveObjectPropertyAxiom)axiom).getRole()) + "</transitive>\n");
122 else if(axiom instanceof FunctionalObjectPropertyAxiom)
123 sb.append("<functional>"+ getDIGString(((FunctionalObjectPropertyAxiom)axiom).getRole()) + "</functional>\n");
124 else if(axiom instanceof SubObjectPropertyAxiom)
125 sb.append("<impliesr>"+ getDIGString(((SubObjectPropertyAxiom)axiom).getSubRole()) +
126 getDIGString(((SubObjectPropertyAxiom)axiom).getRole()) + "</impliesr>\n");
127 // TBox
128 else if(axiom instanceof EquivalentClassesAxiom)
129 sb.append("<equalc>"+ getDIGString(((EquivalentClassesAxiom)axiom).getConcept1()) +
130 getDIGString(((EquivalentClassesAxiom)axiom).getConcept2()) + "</equalc>\n");
131 else if(axiom instanceof SubClassAxiom)
132 sb.append("<impliesc>"+ getDIGString(((SubClassAxiom)axiom).getSubConcept()) +
133 getDIGString(((SubClassAxiom)axiom).getSuperConcept()) + "</impliesc>\n");
134 else
135 throw new RuntimeException();
136
137 return sb;
138 }
139
140 public static StringBuilder getDIGString(Description concept) {
141 StringBuilder sb = new StringBuilder();
142
143 if(concept instanceof Thing)
144 sb.append("<top/>");
145 else if(concept instanceof Nothing)
146 sb.append("<bottom/>");
147 else if(concept instanceof NamedClass)
148 sb.append("<catom name=\""+((NamedClass)concept).getName()+"\"/>");
149 else if(concept instanceof Negation)
150 sb.append("<not>");
151 else if(concept instanceof Intersection)
152 sb.append("<and>");
153 else if(concept instanceof Union)
154 sb.append("<or>");
155 else if(concept instanceof ObjectSomeRestriction)
156 sb.append("<some>"+getDIGString(((ObjectQuantorRestriction)concept).getRole()));
157 else if(concept instanceof ObjectAllRestriction)
158 sb.append("<all>"+getDIGString(((ObjectQuantorRestriction)concept).getRole()));
159 else
160 throw new RuntimeException();
161
162 for(Description child : concept.getChildren()) {
163 sb.append(getDIGString(child));
164 }
165
166 if(concept instanceof Negation)
167 sb.append("</not>");
168 else if(concept instanceof Intersection)
169 sb.append("</and>");
170 else if(concept instanceof Union)
171 sb.append("</or>");
172 else if(concept instanceof ObjectSomeRestriction)
173 sb.append("</some>");
174 else if(concept instanceof ObjectAllRestriction)
175 sb.append("</all>");
176
177 return sb;
178 }
179
180 public static StringBuilder getDIGString(ObjectPropertyExpression role) {
181 if(role instanceof ObjectProperty)
182 return new StringBuilder("<ratom name=\"" + ((ObjectProperty)role).getName() + "\"/>");
183 else if(role instanceof ObjectPropertyInverse)
184 return new StringBuilder("<inverse><ratom name=\"" + ((ObjectPropertyInverse)role).getName() + "\"/></inverse>");
185
186 throw new RuntimeException("Can only create DIG Strings for atomic and inverse roles, not for " + role + ".");
187 }
188
189
190 }