001    package org.dllearner.examples;
002    
003    import java.net.URI;
004    import java.util.SortedSet;
005    import java.util.TreeSet;
006    
007    import org.dllearner.core.owl.ClassAssertionAxiom;
008    import org.dllearner.core.owl.DatatypeProperty;
009    import org.dllearner.core.owl.Description;
010    import org.dllearner.core.owl.DisjointClassesAxiom;
011    import org.dllearner.core.owl.EquivalentClassesAxiom;
012    import org.dllearner.core.owl.Individual;
013    import org.dllearner.core.owl.KB;
014    import org.dllearner.core.owl.NamedClass;
015    import org.dllearner.core.owl.ObjectProperty;
016    import org.dllearner.core.owl.ObjectPropertyAssertion;
017    import org.dllearner.core.owl.ObjectPropertyDomainAxiom;
018    import org.dllearner.core.owl.ObjectPropertyRangeAxiom;
019    import org.dllearner.core.owl.SubClassAxiom;
020    import org.dllearner.core.owl.SubObjectPropertyAxiom;
021    import org.dllearner.core.owl.SymmetricObjectPropertyAxiom;
022    import org.dllearner.parser.KBParser;
023    
024    public class KRKOntologyTBox {
025            
026            private KB kb;
027            static URI ontologyURI = URI.create("http://dl-learner.org/krk");
028            
029            static boolean useHigherThan=false;
030            static boolean useInverse = false;
031            
032            static NamedClass Game = getAtomicConcept("Game");
033            static NamedClass WKing = getAtomicConcept("WKing");
034            static NamedClass WRook = getAtomicConcept("WRook");
035            static NamedClass BKing = getAtomicConcept("BKing");
036            // had to rename, too much similarity to java.io.File
037            static NamedClass FileData = getAtomicConcept("File");
038            static NamedClass Rank = getAtomicConcept("Rank");
039            static NamedClass Piece = getAtomicConcept("Piece");
040    
041            static ObjectProperty hasPiece = getRole("hasPiece");
042            static ObjectProperty hasWKing = getRole("hasWKing");
043            static ObjectProperty hasWRook = getRole("hasWRook");
044            static ObjectProperty hasBKing = getRole("hasBKing");
045    
046            static ObjectProperty hasPieceInv = getRole("hasGame");
047            static ObjectProperty hasWKingInv = getRole("hasWKingInv");
048            static ObjectProperty hasWRookInv = getRole("hasWRookInv");
049            static ObjectProperty hasBKingInv = getRole("hasBKingInv");
050    
051            static ObjectProperty rankLessThan = getRole("hasLowerRankThan");
052            static ObjectProperty fileLessThan = getRole("hasLowerFileThan");
053            
054            static ObjectProperty rankHigherThan = getRole("hasHigherRankThan");
055            static ObjectProperty fileHigherThan = getRole("hasHigherFileThan");
056    
057                    
058            /*public        KRKOntologyTBox(KB kbin){
059                    this.kb=kbin;
060                    
061            }*/
062            
063            public  KRKOntologyTBox(){
064                    this.kb=new KB();
065                    initOntologyTBox();
066                    
067                    
068            }
069            
070            public void addConcept(String concept){
071                    try{
072                    //make Description
073                    Description d = KBParser.parseConcept(concept,ontologyURI.toString()+"#");
074                    //d.addChild(oecr);
075                    kb.addTBoxAxiom(new EquivalentClassesAxiom(getAtomicConcept("test"),d));
076                    
077                    }catch (Exception e) {e.printStackTrace();}
078            }
079            
080            public void initOntologyTBox(){
081                    
082                    //DISJOINTCLASSES
083                    SortedSet<Description> DisJointClasses1 = new TreeSet<Description>();
084                    DisJointClasses1.add(Piece);
085                    DisJointClasses1.add(Game);
086                    kb.addTBoxAxiom(new DisjointClassesAxiom(DisJointClasses1));
087    
088                    SortedSet<Description> DisJointClasses2 = new TreeSet<Description>();
089                    DisJointClasses2 = new TreeSet<Description>();
090                    DisJointClasses2.add(WKing);
091                    DisJointClasses2.add(WRook);
092                    DisJointClasses2.add(BKing);
093                    // DisJointClasses2.add(Rank);
094                    // DisJointClasses2.add(File);
095                    // DisJointClasses2.add(Game);
096                    kb.addTBoxAxiom(new DisjointClassesAxiom(DisJointClasses2));
097                    
098                    /**CLASSES***/
099                    // all sub of piece
100                    kb.addTBoxAxiom(new SubClassAxiom(WKing, Piece));
101                    kb.addTBoxAxiom(new SubClassAxiom(WRook, Piece));
102                    kb.addTBoxAxiom(new SubClassAxiom(BKing, Piece));
103                    
104                    String[] letters=new String[]{"FileA","FileB","FileC","FileD","FileE","FileF","FileG","FileH"};
105                    String[] numbers=new String[8];
106                    for (int i = 0; i < numbers.length; i++) {
107                            numbers[i]="Rank"+(i+1);
108                    }
109                    //System.out.println(numbers);
110                    
111                    for (int i = 0; i < numbers.length; i++) {
112                            kb.addTBoxAxiom(new SubClassAxiom(getAtomicConcept(letters[i]),Piece));
113                            kb.addTBoxAxiom(new SubClassAxiom(getAtomicConcept(numbers[i]),Piece));
114                    }
115                    
116                    
117                    kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(hasPiece, Game));
118                    kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(hasPiece, Piece));
119    
120                    
121                    if (useInverse) {
122                            kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(hasPieceInv, Piece));
123                            kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(hasPieceInv, Game));
124    
125                            kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(hasWKingInv, WKing));
126                            kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(hasWKingInv, Game));
127    
128                            kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(hasWRookInv, WRook));
129                            kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(hasWRookInv, Game));
130    
131                            kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(hasBKingInv, BKing));
132                            kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(hasBKingInv, Game));
133                    }
134    
135                    kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(rankLessThan, Piece));
136                    kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(rankLessThan, Piece));
137                    kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(fileLessThan, Piece));
138                    kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(fileLessThan, Piece));
139                    
140                    if(useHigherThan) {
141                            kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(rankHigherThan, Piece));
142                            kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(rankHigherThan, Piece));
143                            kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(fileHigherThan, Piece));
144                            kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(fileHigherThan, Piece));
145                    }
146                    
147                    finishBackgroundForRoles();
148                    
149                    //return this.kb;
150                    
151            }
152            
153            
154            public void finishBackgroundForRoles() {
155    
156                    for (int i = 8; i > 0; i--) {
157                            
158                            
159                            ObjectProperty rankDistance = getRole("rankDistance"+(i-1));
160                            ObjectProperty fileDistance = getRole("fileDistance"+(i-1));
161                            kb.addRBoxAxiom(new SymmetricObjectPropertyAxiom(rankDistance));
162                            kb.addRBoxAxiom(new SymmetricObjectPropertyAxiom(fileDistance));
163                            kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(rankDistance,
164                                            KRKOntologyTBox.Piece));
165                            kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(rankDistance,
166                                            KRKOntologyTBox.Piece));
167                            kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(fileDistance,
168                                            KRKOntologyTBox.Piece));
169                            kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(fileDistance,
170                                            KRKOntologyTBox.Piece));
171                            
172                            
173            
174                            kb.addRBoxAxiom(new SubObjectPropertyAxiom(
175                                            getRole("rankDistance" + (i - 1)), 
176                                            getRole("rankDistanceLessThan" + i)));
177                            kb.addRBoxAxiom(new SubObjectPropertyAxiom(
178                                            getRole("fileDistance" + (i - 1)), 
179                                            getRole("fileDistanceLessThan" + i)));
180    
181                            kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(
182                                            getRole("rankDistanceLessThan" + i), KRKOntologyTBox.Piece));
183                            kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(
184                                            getRole("rankDistanceLessThan" + i), KRKOntologyTBox.Piece));
185    
186                            kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(
187                                            getRole("fileDistanceLessThan" + i), KRKOntologyTBox.Piece));
188                            kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(
189                                            getRole("fileDistanceLessThan" + i), KRKOntologyTBox.Piece));
190                            
191                            
192                            if(i==1) continue;
193                            
194                            kb.addRBoxAxiom(new SubObjectPropertyAxiom(
195                                            getRole("rankDistanceLessThan" + (i - 1)),
196                                            getRole("rankDistanceLessThan" + i)));
197                            kb.addRBoxAxiom(new SubObjectPropertyAxiom(
198                                            getRole("fileDistanceLessThan" + (i - 1)),
199                                            getRole("fileDistanceLessThan" + i)));
200                            
201                            
202                    }
203    
204                    
205            }
206            
207            
208            protected static Individual getIndividual(String name) {
209                    return new Individual(ontologyURI + "#" + name);
210            }
211    
212            protected static ObjectProperty getRole(String name) {
213                    return new ObjectProperty(ontologyURI + "#" + name);
214            }
215    
216            @SuppressWarnings("unused")
217            protected static DatatypeProperty getDatatypeProperty(String name) {
218                    return new DatatypeProperty(ontologyURI + "#" + name);
219            }
220    
221            protected static NamedClass getAtomicConcept(String name) {
222                    return new NamedClass(ontologyURI + "#" + name);
223            }
224    
225            @SuppressWarnings("unused")
226            protected static String getURI(String name) {
227                    return ontologyURI + "#" + name;
228            }
229    
230            @SuppressWarnings("unused")
231            protected static ClassAssertionAxiom getConceptAssertion(String concept,
232                            String instance) {
233                    Individual ind = getIndividual(instance);
234                    NamedClass c = getAtomicConcept(concept);
235                    return new ClassAssertionAxiom(c, ind);
236            }
237    
238            @SuppressWarnings("unused")
239            protected static ObjectPropertyAssertion getRoleAssertion(String role,
240                            String i1, String i2) {
241                    Individual ind1 = getIndividual(i1);
242                    Individual ind2 = getIndividual(i2);
243                    ObjectProperty ar = getRole(role);
244                    return new ObjectPropertyAssertion(ar, ind1, ind2);
245            }
246    
247            public KB getKb() {
248                    return kb;
249            }
250            
251    }