001    package org.dllearner.examples;
002    
003    import java.io.BufferedReader;
004    import java.io.File;
005    import java.io.FileNotFoundException;
006    import java.io.FileOutputStream;
007    import java.io.FileReader;
008    import java.io.FileWriter;
009    import java.io.IOException;
010    import java.net.URI;
011    import java.util.ArrayList;
012    import java.util.HashMap;
013    import java.util.Iterator;
014    import java.util.SortedSet;
015    import java.util.StringTokenizer;
016    import java.util.TreeSet;
017    
018    import org.dllearner.core.owl.ClassAssertionAxiom;
019    import org.dllearner.core.owl.DatatypeProperty;
020    import org.dllearner.core.owl.Description;
021    import org.dllearner.core.owl.Individual;
022    import org.dllearner.core.owl.InverseObjectPropertyAxiom;
023    import org.dllearner.core.owl.KB;
024    import org.dllearner.core.owl.NamedClass;
025    import org.dllearner.core.owl.ObjectProperty;
026    import org.dllearner.core.owl.ObjectPropertyAssertion;
027    import org.dllearner.core.owl.ObjectPropertyDomainAxiom;
028    import org.dllearner.core.owl.ObjectPropertyRangeAxiom;
029    import org.dllearner.core.owl.SubClassAxiom;
030    import org.dllearner.core.owl.SubObjectPropertyAxiom;
031    import org.dllearner.core.owl.SymmetricObjectPropertyAxiom;
032    import org.dllearner.core.owl.TransitiveObjectPropertyAxiom;
033    import org.dllearner.reasoning.OWLAPIReasoner;
034    import org.dllearner.utilities.owl.OntologyCloser;
035    
036    /*
037     * Structure
038     * 
039     * 
040     * 
041     * 
042     * */
043    
044    public class KRK {
045    
046            // REMEMBER
047            // FILES are letters
048            // RANKS are numbers
049    
050            // FLAGS
051            // 
052            // turn off to not write the owl, needs about 30 seconds or more
053            static boolean writeOWL = true;
054            static boolean writeKB = false;
055            static boolean useTransitivity = false;
056            static boolean useHigherThan = false;
057            static boolean writeExampleSets = false;
058            static boolean writeConciseOWLAllDifferent = false;
059            
060            
061            static boolean closeKB=false;
062            static boolean closeConcise= true && closeKB;
063            static boolean writeClosedOWL = true && closeKB;
064            static boolean verifySomeConcepts = false && closeKB;
065            
066    
067            static boolean useInverse = false;
068            // dependent, love and marriage, horse and carriage
069            static boolean useTripleSubProps = useInverse && false;
070            
071            
072            static String workingDir = "examples/krk";
073            //static String fileIn = workingDir+"/krkopt_no_draw.data";
074            static String fileIn = workingDir+"/krkopt.data";
075            static String owlfilename = "complete_nodraw.owl";
076            
077    
078            static URI ontologyURI = URI.create("http://dl-learner.org/krk");
079            // static SortedSet<String> fileSet = new TreeSet<String>();
080            static SortedSet<String> allInstances = new TreeSet<String>();
081            static SortedSet<String> classSet = new TreeSet<String>();
082            static SortedSet<String> symmetricRoleSet = new TreeSet<String>();
083    
084            static HashMap<String, SortedSet<String>> classToInd = new HashMap<String, SortedSet<String>>();
085    
086            // static LinkedList<String> words;
087            static KB kb;
088    
089            static NamedClass Game = getAtomicConcept("Game");
090            static NamedClass WKing = getAtomicConcept("WKing");
091            static NamedClass WRook = getAtomicConcept("WRook");
092            static NamedClass BKing = getAtomicConcept("BKing");
093            // had to rename, too much similarity to java.io.File
094            static NamedClass FileData = getAtomicConcept("File");
095            static NamedClass Rank = getAtomicConcept("Rank");
096            static NamedClass Piece = getAtomicConcept("Piece");
097    
098            static ObjectProperty hasPiece = getRole("hasPiece");
099            static ObjectProperty hasWKing = getRole("hasWKing");
100            static ObjectProperty hasWRook = getRole("hasWRook");
101            static ObjectProperty hasBKing = getRole("hasBKing");
102    
103            static ObjectProperty hasPieceInv = getRole("hasGame");
104            static ObjectProperty hasWKingInv = getRole("hasWKingInv");
105            static ObjectProperty hasWRookInv = getRole("hasWRookInv");
106            static ObjectProperty hasBKingInv = getRole("hasBKingInv");
107    
108            static ObjectProperty rankLessThan = getRole("hasLowerRankThan");
109            static ObjectProperty fileLessThan = getRole("hasLowerFileThan");
110            
111            static ObjectProperty rankHigherThan = getRole("hasHigherRankThan");
112            static ObjectProperty fileHigherThan = getRole("hasHigherFileThan");
113    
114            // static ObjectProperty lessThan = getRole("strictLessThan");
115    
116            // static ObjectProperty hasRankInv = getRole("hasRankInv");
117            // static ObjectProperty hasFileInv = getRole("hasFileInv");
118            //
119            // static ObjectProperty lessThanInv = getRole("strictLessThanInv");
120    
121            // static HashMap<String,SortedSet<String>> classToInd;
122            /**
123             * @param args
124             */
125            public static void main(String[] args) {
126                    System.out.println("Start");
127                    workingDir = workingDir + File.separator;
128                    if (!new File(workingDir).exists()) {
129                            System.out.println("Created directory: " + workingDir + " : " + new File(workingDir).mkdir()
130                                                            + ".");
131                    }
132                    
133                    // Datei �ffnen
134                    BufferedReader in = null;
135                    try {
136                            in = new BufferedReader(new FileReader(fileIn));
137                    } catch (FileNotFoundException e) {
138                            e.printStackTrace();
139                    }
140    
141                    // classToInd = new HashMap<String,SortedSet<String>>();
142                    init();
143    
144                    Individual gameind;
145                    Individual wkingind;
146                    Individual wrookind;
147                    Individual bkingind;
148                    
149                    
150                    
151                    
152                    
153                    
154    
155                    try {
156                            String line = "";
157                            String[] ar = new String[6];
158    //                      String currentclass="";
159    
160                            int x = 0;
161                            while ((line = in.readLine()) != null) {
162                                    x++;
163                                    if (x % 3000 == 0)
164                                            System.out.println("Currently at line" + x);
165                                    ar = tokenize(line);
166    
167    //                              currentclass = ar[6];
168                                    
169                                    gameind = getIndividual("game" + x);
170                                    wkingind = getIndividual("wking_" + ar[0] + ar[1] + "_" + x);
171                                    wrookind = getIndividual("wrook_" + ar[2] + ar[3] + "_" + x);
172                                    bkingind = getIndividual("bking_" + ar[4] + ar[5] + "_" + x);
173                                    
174                                    
175                                    
176                                    allInstances.add(gameind+"");
177                                    allInstances.add(wkingind+"");
178                                    allInstances.add(wrookind+"");
179                                    allInstances.add(bkingind+"");
180    
181                                    // if (x == 1)
182                                    // currentClass = ar[6];
183    
184                                    // save it for examplegeneration
185                                    addToHM(ar[6], gameind.getName());
186                                    // .add(gameind.getName());
187                                    classSet.add(ar[6]);
188    
189                                    // CLASSES
190                                    kb.addABoxAxiom(new ClassAssertionAxiom(Game, gameind));
191                                    kb.addABoxAxiom(new ClassAssertionAxiom(
192                                                    getAtomicConcept(ar[6]), gameind));
193                                    kb.addABoxAxiom(new ClassAssertionAxiom(WKing, wkingind));
194                                    kb.addABoxAxiom(new ClassAssertionAxiom(WRook, wrookind));
195                                    kb.addABoxAxiom(new ClassAssertionAxiom(BKing, bkingind));
196    
197                                    
198                                    /**Files and Ranks***/
199                                    // FILES are letters
200                                    // RANKS are numbers
201                                    
202                                    //WKing
203                                    kb.addABoxAxiom(new ClassAssertionAxiom(getAtomicConcept("File"+ar[0].toUpperCase()), wkingind));
204                                    kb.addABoxAxiom(new ClassAssertionAxiom(getAtomicConcept("Rank"+ar[1]) , wkingind));
205                                    
206                                    //WRook
207                                    kb.addABoxAxiom(new ClassAssertionAxiom(getAtomicConcept("File"+ar[2].toUpperCase()), wrookind));
208                                    kb.addABoxAxiom(new ClassAssertionAxiom(getAtomicConcept("Rank"+ar[3])  , wrookind));
209                                    
210                                    //BKing
211                                    kb.addABoxAxiom(new ClassAssertionAxiom(getAtomicConcept("File"+ar[4].toUpperCase()), bkingind));
212                                    kb.addABoxAxiom(new ClassAssertionAxiom(getAtomicConcept("Rank"+ar[5]), bkingind));
213                                    
214                                    
215                                    
216                                    // PROPERTIES
217                                    kb.addABoxAxiom(new ObjectPropertyAssertion(hasPiece, gameind,
218                                                    wkingind));
219                                    kb.addABoxAxiom(new ObjectPropertyAssertion(hasPiece, gameind,
220                                                    wrookind));
221                                    kb.addABoxAxiom(new ObjectPropertyAssertion(hasPiece, gameind,
222                                                    bkingind));
223    
224                                    // labels
225    
226                                    KRKPiece WKingPiece = new KRKPiece(wkingind, ar[0], Integer
227                                                    .parseInt(ar[1]));
228                                    KRKPiece WRookPiece = new KRKPiece(wrookind, ar[2], Integer
229                                                    .parseInt(ar[3]));
230                                    KRKPiece BKingPiece = new KRKPiece(bkingind, ar[4], Integer
231                                                    .parseInt(ar[5]));
232    
233                                    makeDistanceRoles(WKingPiece, WRookPiece);
234                                    makeDistanceRoles(WKingPiece, BKingPiece);
235                                    makeDistanceRoles(WRookPiece, BKingPiece);
236    
237                                    // FILERANK
238                                    /*
239                                     * kb.addABoxAxiom(new ObjectPropertyAssertion(hasFile,
240                                     * wkingind, getIndividual(ar[0]))); kb.addABoxAxiom(new
241                                     * ObjectPropertyAssertion(hasRank, wkingind, getIndividual("r" +
242                                     * ar[1])));
243                                     * 
244                                     * kb.addABoxAxiom(new ObjectPropertyAssertion(hasFile,
245                                     * wrookind, getIndividual(ar[2]))); kb.addABoxAxiom(new
246                                     * ObjectPropertyAssertion(hasRank, wrookind, getIndividual("r" +
247                                     * ar[3])));
248                                     * 
249                                     * kb.addABoxAxiom(new ObjectPropertyAssertion(hasFile,
250                                     * bkingind, getIndividual(ar[4]))); kb.addABoxAxiom(new
251                                     * ObjectPropertyAssertion(hasRank, bkingind, getIndividual("r" +
252                                     * ar[5])));
253                                     */
254    
255                            }// endWhile
256    
257                            finishBackgroundForRoles();
258                            System.out.println("Finished Background");
259                            // WRITE
260                            if(writeExampleSets)writeExampleSets();
261                            if(writeConciseOWLAllDifferent)writeConciseOWLAllDifferent();
262                            if (writeOWL)writeOWLFile(owlfilename);
263                            if(writeKB)writeKBFile("test.kb");
264                                    
265                            OntologyCloser oc = null;
266                            String kbaddition= "_Closed";
267                            if(closeKB) {
268                                    oc= new OntologyCloser(kb);
269                                    if(closeConcise) {
270                                            oc.applyNumberRestrictionsConcise();
271                                            kbaddition = "_CloseConcise";
272                                    }
273                                    else oc.applyNumberRestrictions();
274                            }
275                            
276                    
277                            if (verifySomeConcepts) {
278                                    
279                                    oc.updateReasoner();
280                                    verifySomeConcepts(oc);
281                            }
282                            if (writeClosedOWL) writeOWLFile("test"+kbaddition+".owl");
283    
284                    } catch (Exception e) {
285                            e.printStackTrace();
286                    }
287                    System.out.println("Done");
288            }// end main
289            
290            
291            
292            static void makeOntology(){
293                    
294                    
295                    
296            }
297            
298            
299            protected static void verifySomeConcepts(OntologyCloser oc) {
300                    
301                    ArrayList<String> test=new ArrayList<String>();
302                            
303                    for (int i = 0; i < test.size(); i++) {
304                            String conceptStr = test.get(i);
305                            oc.verifyConcept(conceptStr);
306                    }
307                    
308                    
309                    System.out.println();
310            }
311    
312            static void makeDistanceRoles(KRKPiece A, KRKPiece B) {
313                    int Fdist = A.getFileDistance(B);
314                    int Rdist = A.getRankDistance(B);
315                    String rdistance = "rankDistance";
316                    String fdistance = "fileDistance";
317    
318                    kb.addABoxAxiom(new ObjectPropertyAssertion(getRole(rdistance + Rdist),
319                                    A.id, B.id));
320                    symmetricRoleSet.add(rdistance + Rdist);
321                    kb.addABoxAxiom(new ObjectPropertyAssertion(getRole(fdistance + Fdist),
322                                    A.id, B.id));
323                    symmetricRoleSet.add(fdistance + Fdist);
324                    
325                    
326    
327                    if (A.meHasLowerFileThan(B)){
328                            kb.addABoxAxiom(new ObjectPropertyAssertion(fileLessThan, A.id,
329                                            B.id));
330                            if(useHigherThan)kb.addABoxAxiom(new ObjectPropertyAssertion(fileHigherThan, B.id,
331                                            A.id));
332                            
333                    }
334                    // 18:00
335                    else if (B.meHasLowerFileThan(A)){
336                            kb.addABoxAxiom(new ObjectPropertyAssertion(fileLessThan, B.id,
337                                            A.id));
338                            if(useHigherThan)kb.addABoxAxiom(new ObjectPropertyAssertion(fileHigherThan, A.id,
339                                            B.id));
340                    }
341    
342                    if (A.meHasLowerRankThan(B)){
343                            kb.addABoxAxiom(new ObjectPropertyAssertion(rankLessThan, A.id,
344                                            B.id));
345                            if(useHigherThan)kb.addABoxAxiom(new ObjectPropertyAssertion(rankHigherThan, B.id,
346                                            A.id));
347                    }
348                    else if (B.meHasLowerRankThan(A)){
349                            kb.addABoxAxiom(new ObjectPropertyAssertion(rankLessThan, B.id,
350                                            A.id));
351                            if(useHigherThan)kb.addABoxAxiom(new ObjectPropertyAssertion(rankHigherThan, A.id,
352                                            B.id));
353                            
354                    }
355            }
356    
357            public static void init() {
358                    kb = new KB();
359                    initClassHierarchy();
360                    initDisJointClasses();
361    
362                    initClassesForRankAndFile();
363                    initDomainRangeForRoles();
364            }
365    
366            public static void initDisJointClasses() {
367                    SortedSet<Description> DisJointClasses1 = new TreeSet<Description>();
368                    DisJointClasses1.add(Piece);
369                    // DisJointClasses1.add(Rank);
370                    // DisJointClasses1.add(File);
371                    DisJointClasses1.add(Game);
372                    //kb.addTBoxAxiom(new DisjointClassesAxiom(DisJointClasses1));
373    
374                    SortedSet<Description> DisJointClasses2 = new TreeSet<Description>();
375                    DisJointClasses2 = new TreeSet<Description>();
376                    DisJointClasses2.add(WKing);
377                    DisJointClasses2.add(WRook);
378                    DisJointClasses2.add(BKing);
379                    // DisJointClasses2.add(Rank);
380                    // DisJointClasses2.add(File);
381                    // DisJointClasses2.add(Game);
382    
383                    //kb.addTBoxAxiom(new DisjointClassesAxiom(DisJointClasses2));
384            }
385    
386            public static void initClassHierarchy() {
387    
388                    // all sub of piece
389                    kb.addTBoxAxiom(new SubClassAxiom(WKing, Piece));
390                    kb.addTBoxAxiom(new SubClassAxiom(WRook, Piece));
391                    kb.addTBoxAxiom(new SubClassAxiom(BKing, Piece));
392                    
393                    String[] letters=new String[]{"FileA","FileB","FileC","FileD","FileE","FileF","FileG","FileH"};
394                    String[] numbers=new String[8];
395                    for (int i = 0; i < numbers.length; i++) {
396                            numbers[i]="Rank"+i;
397                    }
398                    //System.out.println(numbers);
399                    
400                    for (int i = 0; i < numbers.length; i++) {
401                            kb.addTBoxAxiom(new SubClassAxiom(getAtomicConcept(letters[i]),Piece));
402                            kb.addTBoxAxiom(new SubClassAxiom(getAtomicConcept(letters[i]),Piece));
403                    }
404                    
405            }
406    
407            static void initClassesForRankAndFile() {
408    
409                    /*
410                     * fileSet.add("a"); fileSet.add("b"); fileSet.add("c");
411                     * fileSet.add("d"); fileSet.add("e"); fileSet.add("f");
412                     * fileSet.add("g"); fileSet.add("h"); for (int count = 1; count < 9;
413                     * count++) { rankSet.add("r" + count); }
414                     * 
415                     * for (String oneFile : fileSet) { kb.addTBoxAxiom(new
416                     * SubClassAxiom(getAtomicConcept(oneFile.toUpperCase()),File));
417                     * kb.addABoxAxiom(new
418                     * ClassAssertionAxiom(getAtomicConcept(oneFile.toUpperCase()),getIndividual(oneFile))); }
419                     * for (String oneRank : rankSet) { kb.addTBoxAxiom(new
420                     * SubClassAxiom(getAtomicConcept(oneRank.toUpperCase()),Rank));
421                     * kb.addABoxAxiom(new
422                     * ClassAssertionAxiom(getAtomicConcept(oneRank.toUpperCase()),getIndividual(oneRank))); }
423                     */
424    
425            }// end init
426    
427            static void initDomainRangeForRoles() {
428    
429                    kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(hasPiece, Game));
430                    kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(hasPiece, Piece));
431    
432                    if (useTripleSubProps) {
433                            kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(hasWKing, Game));
434                            kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(hasWKing, WKing));
435    
436                            kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(hasWRook, Game));
437                            kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(hasWRook, WRook));
438    
439                            kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(hasBKing, Game));
440                            kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(hasBKing, BKing));
441                    }
442    
443                    if (useInverse) {
444                            kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(hasPieceInv, Piece));
445                            kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(hasPieceInv, Game));
446    
447                            kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(hasWKingInv, WKing));
448                            kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(hasWKingInv, Game));
449    
450                            kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(hasWRookInv, WRook));
451                            kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(hasWRookInv, Game));
452    
453                            kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(hasBKingInv, BKing));
454                            kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(hasBKingInv, Game));
455                    }
456    
457                    kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(rankLessThan, Piece));
458                    kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(rankLessThan, Piece));
459                    kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(fileLessThan, Piece));
460                    kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(fileLessThan, Piece));
461                    
462                    if(useHigherThan) {
463                            kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(rankHigherThan, Piece));
464                            kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(rankHigherThan, Piece));
465                            kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(fileHigherThan, Piece));
466                            kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(fileHigherThan, Piece));
467                    }
468            }
469    
470            static void finishBackgroundForRoles() {
471    
472                    if (useTransitivity) {
473                            kb.addRBoxAxiom(new TransitiveObjectPropertyAxiom(rankLessThan));
474                            kb.addRBoxAxiom(new TransitiveObjectPropertyAxiom(fileLessThan));
475                            if(useHigherThan) { 
476                                    kb.addRBoxAxiom(new TransitiveObjectPropertyAxiom(rankHigherThan));
477                                    kb.addRBoxAxiom(new TransitiveObjectPropertyAxiom(fileHigherThan));
478                            }
479                            
480                    }
481                    if (useInverse)
482                    // INVERSE
483                    {
484                            kb.addRBoxAxiom(new InverseObjectPropertyAxiom(hasPiece,
485                                            hasPieceInv));
486                            kb.addRBoxAxiom(new InverseObjectPropertyAxiom(hasWKing,
487                                            hasWKingInv));
488                            kb.addRBoxAxiom(new InverseObjectPropertyAxiom(hasWRook,
489                                            hasWRookInv));
490                            kb.addRBoxAxiom(new InverseObjectPropertyAxiom(hasBKing,
491                                            hasBKingInv));
492                    }
493    
494                    if (useTripleSubProps) {
495                            kb.addRBoxAxiom(new SubObjectPropertyAxiom(hasWKing, hasPiece));
496                            kb.addRBoxAxiom(new SubObjectPropertyAxiom(hasWRook, hasPiece));
497                            kb.addRBoxAxiom(new SubObjectPropertyAxiom(hasBKing, hasPiece));
498                    }
499    
500                    for (String oneRole : symmetricRoleSet) {
501                            kb.addRBoxAxiom(new SymmetricObjectPropertyAxiom(getRole(oneRole)));
502                            kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(getRole(oneRole),
503                                            Piece));
504                            kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(getRole(oneRole),
505                                            Piece));
506                    }
507    
508                    for (int i = 8; i > 0; i--) {
509                            kb.addRBoxAxiom(new SubObjectPropertyAxiom(
510                                            getRole("rankDistanceLessThan" + (i - 1)),
511                                            getRole("rankDistanceLessThan" + i)));
512                            kb.addRBoxAxiom(new SubObjectPropertyAxiom(
513                                            getRole("fileDistanceLessThan" + (i - 1)),
514                                            getRole("fileDistanceLessThan" + i)));
515    
516                            kb.addRBoxAxiom(new SubObjectPropertyAxiom(getRole("rankDistance"
517                                            + (i - 1)), getRole("rankDistanceLessThan" + i)));
518                            kb.addRBoxAxiom(new SubObjectPropertyAxiom(getRole("fileDistance"
519                                            + (i - 1)), getRole("fileDistanceLessThan" + i)));
520    
521                            kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(
522                                            getRole("rankDistanceLessThan" + i), Piece));
523                            kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(
524                                            getRole("rankDistanceLessThan" + i), Piece));
525    
526                            kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(
527                                            getRole("fileDistanceLessThan" + i), Piece));
528                            kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(
529                                            getRole("fileDistanceLessThan" + i), Piece));
530                    }
531    
532                    // kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(rankLessThan, Piece));
533                    // kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(fileLessThan, Piece));
534            }
535    
536            /*
537             * static void initBackgroundForRankAndFileRoles() {
538             * 
539             * kb.addRBoxAxiom(new TransitiveObjectPropertyAxiom(lessThan));
540             * kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(lessThan, new Union(Rank,
541             * File)));
542             * 
543             * kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(lessThan, new Union(Rank,
544             * File))); kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(hasRank, Piece));
545             * kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(hasRank, Rank));
546             * kb.addRBoxAxiom(new ObjectPropertyDomainAxiom(hasFile, Piece));
547             * kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(hasFile, File));
548             * 
549             * 
550             * 
551             * kb.addRBoxAxiom(new InverseObjectPropertyAxiom(hasRank,hasRankInv));
552             * kb.addRBoxAxiom(new InverseObjectPropertyAxiom(hasFile,hasFileInv));
553             * kb.addRBoxAxiom(new InverseObjectPropertyAxiom(lessThan,lessThanInv)); //
554             * assigning lessthan to file Iterator<String> it = fileSet.iterator();
555             * Individual current = getIndividual(it.next()); Individual next; while
556             * (it.hasNext()) { next = getIndividual(it.next()); kb .addABoxAxiom(new
557             * ObjectPropertyAssertion(lessThan, current, next)); current = next; } //
558             * assigning lessthan to rank it = rankSet.iterator(); current =
559             * getIndividual(it.next()); next = null; while (it.hasNext()) { next =
560             * getIndividual(it.next()); kb .addABoxAxiom(new
561             * ObjectPropertyAssertion(lessThan, current, next)); current = next; } //
562             * new PropertyRangeAxiom(rank, (PropertyRange) nc[5]);
563             * 
564             * String str = "hasDistanceOf"; String str2 = "hasDistanceLessThan"; //
565             * ObjectProperty tmp; String[] rankarray = new String[8]; String[]
566             * filearray = new String[8]; rankSet.toArray(rankarray);
567             * fileSet.toArray(filearray); // assigning has DistanceOf to Rank and File
568             * for (int count = 0; count < filearray.length; count++) { for (int inner =
569             * count + 1, dist = 1; inner < filearray.length; inner++, dist++) { //
570             * ObjectProperty op = getRole(str+inner);
571             * kb.addABoxAxiom(getRoleAssertion(str + dist, rankarray[count],
572             * rankarray[inner])); kb.addABoxAxiom(getRoleAssertion(str + dist,
573             * filearray[count], filearray[inner])); } kb.addRBoxAxiom(new
574             * ObjectPropertyDomainAxiom(getRole(str+(count+1)), new Union(Rank,File)));
575             * kb.addRBoxAxiom(new ObjectPropertyRangeAxiom(getRole(str+(count+1)), new
576             * Union(Rank,File))); kb.addRBoxAxiom(new
577             * ObjectPropertyDomainAxiom(getRole(str2+(count+1)), new
578             * Union(Rank,File))); kb.addRBoxAxiom(new
579             * ObjectPropertyRangeAxiom(getRole(str2+(count+1)), new Union(Rank,File))); } //
580             * make it symmetric + subproperty of for (int count = 1; count < 8;
581             * count++) { kb.addRBoxAxiom(new SymmetricObjectPropertyAxiom(getRole(str +
582             * count))); kb.addRBoxAxiom(new SubObjectPropertyAxiom(getRole(str +
583             * count), getRole(str2 + (count + 1)))); kb.addRBoxAxiom(new
584             * SubObjectPropertyAxiom(getRole(str2 + count), getRole(str2 + (count +
585             * 1)))); } }
586             */
587            public static String[] tokenize(String s) {
588                    StringTokenizer st = new StringTokenizer(s, ",");
589    
590                    String tmp = "";
591                    String[] ret = new String[7];
592                    int x = 0;
593                    while (st.hasMoreTokens()) {
594                            tmp = st.nextToken();
595                            if (x == 6)
596                                    tmp = tmp.toUpperCase();
597                            ret[x] = tmp;
598                            x++;
599                    }
600                    return ret;
601    
602            }
603    
604            protected static void writeExampleSets() {
605                    StringBuffer collect1 = new StringBuffer();
606                    StringBuffer collect2 = new StringBuffer();
607                    System.out.println("start writing sets");
608    
609                    for (String keys : classToInd.keySet()) {
610                            System.out.println(keys);
611                            SortedSet<String> tmpset = classToInd.get(keys);
612                            for (String individuals : tmpset) {
613                                    collect1.append("+\"" + individuals + "\"\n");
614                                    collect2.append("-\"" + individuals + "\"\n");
615                            }
616    
617                            writeToFile(workingDir+"/examples_for_" + keys + ".txt", collect1
618                                            + "\n\n" + collect2 + "\n");
619                            collect1 = new StringBuffer();
620                            collect2 = new StringBuffer();
621                    }
622                    // System.out.println("Sets written");
623                    collect1 = new StringBuffer();
624                    collect2 = new StringBuffer();
625                    for (String key : classToInd.keySet()) {
626    
627                            SortedSet<String> tmpset = classToInd.get(key);
628    
629                            if (key.equals("ZERO")) {
630                                    collect1.append("/**" + key + "**/\n");
631                                    for (String individuals : tmpset) {
632                                            collect1.append("+\"" + individuals + "\"\n");
633                                    }
634    
635                                    continue;
636                            } else {
637                                    collect2.append("/**" + key + "**/\n");
638                                    for (String individuals : tmpset) {
639                                            collect2.append("-\"" + individuals + "\"\n");
640                                    }
641                            }
642    
643                    }
644                    writeToFile(workingDir+"/examples_for_ZERO_and_Rest.txt", collect1
645                                    + "\n\n" + collect2 + "\n");
646                    System.out.println("Example sets written");
647            }
648            
649    
650            protected static void writeConciseOWLAllDifferent() {
651                    StringBuffer collect = new StringBuffer();
652                    System.out.println("start writing OWLAllDifferent");
653                    collect.append("<owl:AllDifferent>\n" +
654                                    "<owl:distinctMembers rdf:parseType=\"Collection\">\n");
655                    
656                    for (String inst : allInstances) {
657                            collect.append("<owl:Thing rdf:about=\"" + inst + "\" />\n");     
658                    }
659                    collect.append("</owl:distinctMembers>"+
660                            "</owl:AllDifferent>");
661                    
662                    writeToFile(workingDir+"/owlAllDifferent.txt", collect.toString());
663               
664            }
665    
666            
667            
668    
669            protected static void writeOWLFile(String filename) {
670    
671                    Iterator<String> it = classSet.iterator();
672                    System.out.println("import(\"" + filename + "\");");
673                    String collect = "refinement.ignoredConcepts={";
674                    while (it.hasNext()) {
675                            String tmp = (String) it.next();
676                            collect += "\n\"" + getAtomicConcept(tmp).getName() + "\",";
677                    }
678                    collect = collect.substring(0, collect.length() - 1);
679                    System.out.println(collect + "};");
680    
681                    System.out.println("Writing owl");
682                    File owlfile = new File(workingDir+"/" + filename);
683                    // System.out.println(kb.toString("http://www.test.de/test", new
684                    // HashMap<String, String>()));
685                    OWLAPIReasoner.exportKBToOWL(owlfile, kb, ontologyURI);
686    
687            }
688            
689            protected static void writeKBFile(String filename) {
690    
691                    System.out.println("Writing kb");
692                    try{
693                    FileWriter fw = new FileWriter(workingDir+"/" + filename,false); 
694                    fw.write(kb.toKBSyntaxString(ontologyURI.toString(), null));
695                    fw.flush();
696                    }catch (Exception e) {e.printStackTrace();}
697                    System.out.println("done writing kb");
698            
699    
700            }
701    
702            protected static Individual getIndividual(String name) {
703                    return new Individual(ontologyURI + "#" + name);
704            }
705    
706            protected static ObjectProperty getRole(String name) {
707                    return new ObjectProperty(ontologyURI + "#" + name);
708            }
709    
710            @SuppressWarnings("unused")
711            protected static DatatypeProperty getDatatypeProperty(String name) {
712                    return new DatatypeProperty(ontologyURI + "#" + name);
713            }
714    
715            protected static NamedClass getAtomicConcept(String name) {
716                    return new NamedClass(ontologyURI + "#" + name);
717            }
718    
719            @SuppressWarnings("unused")
720            protected static String getURI(String name) {
721                    return ontologyURI + "#" + name;
722            }
723    
724            @SuppressWarnings("unused")
725            protected static ClassAssertionAxiom getConceptAssertion(String concept,
726                            String i) {
727                    Individual ind = getIndividual(i);
728                    NamedClass c = getAtomicConcept(concept);
729                    return new ClassAssertionAxiom(c, ind);
730            }
731    
732            @SuppressWarnings("unused")
733            protected static ObjectPropertyAssertion getRoleAssertion(String role,
734                            String i1, String i2) {
735                    Individual ind1 = getIndividual(i1);
736                    Individual ind2 = getIndividual(i2);
737                    ObjectProperty ar = getRole(role);
738                    return new ObjectPropertyAssertion(ar, ind1, ind2);
739            }
740    
741            protected static void writeToFile(String filename, String content) {
742                    // create the file we want to use
743                    File file = new File(filename);
744    
745                    try {
746                            file.createNewFile();
747                            FileOutputStream fos = new FileOutputStream(filename, false);
748                            // ObjectOutputStream o = new ObjectOutputStream(fos);
749                            fos.write(content.getBytes());
750                            fos.flush();
751                            fos.close();
752                    } catch (IOException e) {
753                            e.printStackTrace();
754                    }
755            }
756    
757            protected static void addToHM(String className, String ind) {
758                    if (classToInd.get(className) == null)
759                            classToInd.put(className, new TreeSet<String>());
760    
761                    classToInd.get(className).add(ind);
762    
763            }
764    
765    }