From: <ku...@us...> - 2008-04-02 15:39:36
|
Revision: 754 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=754&view=rev Author: kurzum Date: 2008-04-02 08:39:32 -0700 (Wed, 02 Apr 2008) Log Message: ----------- working ontology closer, I put it here, because I wasn't sure where and how to integrate it, code could be better Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/examples/KRK.java Added Paths: ----------- trunk/src/dl-learner/org/dllearner/examples/OntologyCloser.java Modified: trunk/src/dl-learner/org/dllearner/examples/KRK.java =================================================================== --- trunk/src/dl-learner/org/dllearner/examples/KRK.java 2008-04-02 15:37:21 UTC (rev 753) +++ trunk/src/dl-learner/org/dllearner/examples/KRK.java 2008-04-02 15:39:32 UTC (rev 754) @@ -47,6 +47,11 @@ // FLAGS // + static boolean writeOWL = true; + static boolean writeClosedOWL = true ; + static boolean useTransitivity = false; + + static boolean useInverse = false; // dependent, love and marriage, horse and carriage static boolean useTripleSubProps = useInverse && false; @@ -97,7 +102,6 @@ public static void main(String[] args) { System.out.println("Start"); // turn off to not write the owl, needs about 30 seconds or more - boolean writeOWL = true; // classToInd = new HashMap<String,SortedSet<String>>(); init(); @@ -125,7 +129,8 @@ int x = 0; while ((line = in.readLine()) != null) { x++; - if(x % 3000 == 0 ) System.out.println("Currently at line"+x); + if (x % 3000 == 0) + System.out.println("Currently at line" + x); ar = tokenize(line); gameind = getIndividual("game" + x); @@ -194,8 +199,20 @@ System.out.println("Finished Background"); // WRITE writeExampleSets(); - if (writeOWL) + + + if (writeOWL) { writeOWLFile("test.owl"); + } + if(writeClosedOWL) { + String conceptStr = "ALL \"http://www.test.de/test#hasPiece\".(EXISTS \"http://www.test.de/test#fileDistanceLessThan6\".((NOT \"http://www.test.de/test#WKing\") AND ALL \"http://www.test.de/test#rankDistance1\".(\"http://www.test.de/test#WKing\" AND ALL \"http://www.test.de/test#fileDistanceLessThan2\".\"http://www.test.de/test#BKing\" AND ALL \"http://www.test.de/test#hasLowerFileThan\".\"http://www.test.de/test#WKing\")) AND ALL \"http://www.test.de/test#fileDistance1\".\"http://www.test.de/test#WRook\")"; + //conceptStr = "ALL http://www.test.de/test#hasPiece.(EXISTS http://www.test.de/test#fileDistanceLessThan6.((NOT http://www.test.de/test#WKing) AND ALL http://www.test.de/test#rankDistance1.(http://www.test.de/test#WKing AND ALL http://www.test.de/test#fileDistanceLessThan2.http://www.test.de/test#WKing AND ALL http://www.test.de/test#hasLowerFileThan.http://www.test.de/test#WKing)) AND ALL http://www.test.de/test#fileDistance1.http://www.test.de/test#WRook)"; + //conceptStr = "ALL hasPiece.(EXISTS fileDistanceLessThan6.((NOT WKing) AND ALL rankDistance1.(WKing AND ALL fileDistanceLessThan2.WKing AND ALL hasLowerFileThan.WKing)) AND ALL fileDistance1.WRook)"; + OntologyCloser oc = new OntologyCloser(kb); + oc.applyNumberRestrictions(); + oc.verifyConcept(conceptStr); + writeOWLFile("test_Closed.owl"); + } } catch (Exception e) { e.printStackTrace(); @@ -329,9 +346,10 @@ static void finishBackgroundForRoles() { - kb.addRBoxAxiom(new TransitiveObjectPropertyAxiom(rankLessThan)); - kb.addRBoxAxiom(new TransitiveObjectPropertyAxiom(fileLessThan)); - + if (useTransitivity) { + kb.addRBoxAxiom(new TransitiveObjectPropertyAxiom(rankLessThan)); + kb.addRBoxAxiom(new TransitiveObjectPropertyAxiom(fileLessThan)); + } if (useInverse) // INVERSE { @@ -456,15 +474,15 @@ } protected static void writeExampleSets() { - StringBuffer collect1 = new StringBuffer(); + StringBuffer collect1 = new StringBuffer(); StringBuffer collect2 = new StringBuffer(); System.out.println("start writing sets"); - + for (String keys : classToInd.keySet()) { System.out.println(keys); SortedSet<String> tmpset = classToInd.get(keys); for (String individuals : tmpset) { - collect1.append( "+\"" + individuals + "\"\n"); + collect1.append("+\"" + individuals + "\"\n"); collect2.append("-\"" + individuals + "\"\n"); } @@ -473,7 +491,7 @@ collect1 = new StringBuffer(); collect2 = new StringBuffer(); } - //System.out.println("Sets written"); + // System.out.println("Sets written"); collect1 = new StringBuffer(); collect2 = new StringBuffer(); for (String key : classToInd.keySet()) { @@ -481,16 +499,16 @@ SortedSet<String> tmpset = classToInd.get(key); if (key.equals("ZERO")) { - collect1.append( "/**" + key + "**/\n"); + collect1.append("/**" + key + "**/\n"); for (String individuals : tmpset) { - collect1.append( "+\"" + individuals + "\"\n"); + collect1.append("+\"" + individuals + "\"\n"); } continue; } else { - collect2.append( "/**" + key + "**/\n"); + collect2.append("/**" + key + "**/\n"); for (String individuals : tmpset) { - collect2.append( "-\"" + individuals + "\"\n"); + collect2.append("-\"" + individuals + "\"\n"); } } Added: trunk/src/dl-learner/org/dllearner/examples/OntologyCloser.java =================================================================== --- trunk/src/dl-learner/org/dllearner/examples/OntologyCloser.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/examples/OntologyCloser.java 2008-04-02 15:39:32 UTC (rev 754) @@ -0,0 +1,102 @@ +package org.dllearner.examples; + +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.SortedSet; + +import org.dllearner.core.KnowledgeSource; +import org.dllearner.core.ReasoningService; +import org.dllearner.core.owl.ClassAssertionAxiom; +import org.dllearner.core.owl.Description; +import org.dllearner.core.owl.Individual; +import org.dllearner.core.owl.KB; +import org.dllearner.core.owl.ObjectExactCardinalityRestriction; +import org.dllearner.core.owl.ObjectProperty; +import org.dllearner.core.owl.PropertyAxiom; +import org.dllearner.core.owl.Thing; +import org.dllearner.kb.KBFile; +import org.dllearner.parser.KBParser; +import org.dllearner.reasoning.OWLAPIReasoner; + +public class OntologyCloser { + KB kb; + KBFile kbFile; + Set<KnowledgeSource> ks; + ReasoningService rs; + + public OntologyCloser(KB kb) { + super(); + this.kb = kb; + this.kbFile = new KBFile(this.kb); + this.ks = new HashSet<KnowledgeSource>(); + this.ks.add(this.kbFile); + OWLAPIReasoner owlapi = new OWLAPIReasoner(ks); + owlapi.init(); + + this.rs = new ReasoningService(owlapi); + + } + + /** + * counts the number of roles used by each individual and assigns + * ExactCardinalityRestriction + */ + public void applyNumberRestrictions() { + Set<ObjectProperty> allRoles = this.rs.getAtomicRoles(); + // Set<Individual> allind = this.rs.getIndividuals(); + Set<PropertyAxiom> ax = kb.getRbox(); + for (PropertyAxiom propertyAxiom : ax) { + if (propertyAxiom.getClass().getSimpleName().equals( + "TransitiveObjectPropertyAxiom")) { + + System.out + .println("WARNING transitive object property can't be used in cardinality restriction\n" + + propertyAxiom.toString()); + } + } + + for (ObjectProperty oneRole : allRoles) { + + // System.out.println(oneRole.getClass()); + Map<Individual, SortedSet<Individual>> allRoleMembers = this.rs + .getRoleMembers(oneRole); + for (Individual oneInd : allRoleMembers.keySet()) { + SortedSet<Individual> fillers = allRoleMembers.get(oneInd); + if (fillers.size() > 0) { + ObjectExactCardinalityRestriction oecr = new ObjectExactCardinalityRestriction( + fillers.size(), oneRole, new Thing()); + kb.addABoxAxiom(new ClassAssertionAxiom(oecr, oneInd)); + } + } + + } + // System.out.println("good ontology? " + rs.isSatisfiable()); + + } + + public static void closeKB(KB kb) { + new OntologyCloser(kb).applyNumberRestrictions(); + } + + public void verifyConcept(String conceptStr) { + Description d; + StringBuffer sb = new StringBuffer(); + sb.append(conceptStr); + conceptStr = sb.toString(); + try { + d = KBParser.parseConcept(conceptStr); + System.out.println("Starting retrieval"); + SortedSet<Individual> ind = this.rs.retrieval(d); + System.out.println("retrieved: " + ind.size() + " instances"); + + for (Individual individual : ind) { + System.out.println(individual + ""); + } + + } catch (Exception e) { + e.printStackTrace(); + } + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ku...@us...> - 2008-04-16 07:51:48
|
Revision: 791 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=791&view=rev Author: kurzum Date: 2008-04-16 00:51:44 -0700 (Wed, 16 Apr 2008) Log Message: ----------- small fix Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/examples/KRKModular.java trunk/src/dl-learner/org/dllearner/examples/KRKOntologyTBox.java Modified: trunk/src/dl-learner/org/dllearner/examples/KRKModular.java =================================================================== --- trunk/src/dl-learner/org/dllearner/examples/KRKModular.java 2008-04-16 06:07:17 UTC (rev 790) +++ trunk/src/dl-learner/org/dllearner/examples/KRKModular.java 2008-04-16 07:51:44 UTC (rev 791) @@ -72,6 +72,7 @@ static String workingDir = "examples/krk/"; static String allData = workingDir+"krkopt_no_draw.data"; + //static String allData = workingDir+"krkopt_original_dataset.data"; static URI ontologyURI = KRKOntologyTBox.ontologyURI; @@ -84,7 +85,7 @@ static HashMap<Individual, String> indToClass = new HashMap<Individual, String>(); static Set<ReasonerComponent> allReasoners = new HashSet<ReasonerComponent>(); - static int negativeExamplesAdded = 300; + static int negativeExamplesAdded = 200; // static LinkedList<String> words; public KB kb; @@ -301,7 +302,7 @@ String line = ""; int x = 0; while ((line = in.readLine()) != null) { - if (x % 10000 == 0) + if (x % 1000 == 0) {sc.print("Currently at line " + x+" : ");} km.addOneLineToKBinit(x, line); if(x%1000==0 && x!=0){ @@ -309,12 +310,13 @@ allReasoners.add(km.reasoner); km = new KRKModular(); } - if(x==26000)break; + //if(x==200)break; x++; }// endWhile km.initReasonerFact(); allReasoners.add(km.reasoner); + km.writeOWLFile(); km = null; @@ -466,16 +468,16 @@ // RANKS are numbers //WKing - kb.addABoxAxiom(new ClassAssertionAxiom(getAtomicConcept(ar[0].toUpperCase()), wkingind)); - kb.addABoxAxiom(new ClassAssertionAxiom(getAtomicConcept("F"+ar[1]) , wkingind)); + kb.addABoxAxiom(new ClassAssertionAxiom(getAtomicConcept("File"+ar[0].toUpperCase()), wkingind)); + kb.addABoxAxiom(new ClassAssertionAxiom(getAtomicConcept("Rank"+ar[1]) , wkingind)); //WRook - kb.addABoxAxiom(new ClassAssertionAxiom(getAtomicConcept(ar[2].toUpperCase()), wrookind)); - kb.addABoxAxiom(new ClassAssertionAxiom(getAtomicConcept("F"+ar[3]) , wrookind)); + kb.addABoxAxiom(new ClassAssertionAxiom(getAtomicConcept("File"+ar[2].toUpperCase()), wrookind)); + kb.addABoxAxiom(new ClassAssertionAxiom(getAtomicConcept("Rank"+ar[3]) , wrookind)); //BKing - kb.addABoxAxiom(new ClassAssertionAxiom(getAtomicConcept(ar[4].toUpperCase()), bkingind)); - kb.addABoxAxiom(new ClassAssertionAxiom(getAtomicConcept("F"+ar[5]), bkingind)); + kb.addABoxAxiom(new ClassAssertionAxiom(getAtomicConcept("File"+ar[4].toUpperCase()), bkingind)); + kb.addABoxAxiom(new ClassAssertionAxiom(getAtomicConcept("Rank"+ar[5]), bkingind)); Modified: trunk/src/dl-learner/org/dllearner/examples/KRKOntologyTBox.java =================================================================== --- trunk/src/dl-learner/org/dllearner/examples/KRKOntologyTBox.java 2008-04-16 06:07:17 UTC (rev 790) +++ trunk/src/dl-learner/org/dllearner/examples/KRKOntologyTBox.java 2008-04-16 07:51:44 UTC (rev 791) @@ -101,10 +101,10 @@ kb.addTBoxAxiom(new SubClassAxiom(WRook, Piece)); kb.addTBoxAxiom(new SubClassAxiom(BKing, Piece)); - String[] letters=new String[]{"A","B","C","D","E","F","G","H"}; + String[] letters=new String[]{"FileA","FileB","FileC","FileD","FileE","FileF","FileG","FileH"}; String[] numbers=new String[8]; for (int i = 0; i < numbers.length; i++) { - numbers[i]="F"+(i+1); + numbers[i]="Rank"+(i+1); } //System.out.println(numbers); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ku...@us...> - 2008-04-17 13:02:40
|
Revision: 797 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=797&view=rev Author: kurzum Date: 2008-04-17 06:02:17 -0700 (Thu, 17 Apr 2008) Log Message: ----------- script for generating all ontologies 0 to 16 Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/examples/KRK.java trunk/src/dl-learner/org/dllearner/examples/KRKModular.java Modified: trunk/src/dl-learner/org/dllearner/examples/KRK.java =================================================================== --- trunk/src/dl-learner/org/dllearner/examples/KRK.java 2008-04-17 06:24:07 UTC (rev 796) +++ trunk/src/dl-learner/org/dllearner/examples/KRK.java 2008-04-17 13:02:17 UTC (rev 797) @@ -70,8 +70,8 @@ static String workingDir = "examples/krk"; - static String fileIn = workingDir+"/krkopt_no_draw.data"; - //static String fileIn = workingDir+"/krkopt.data"; + //static String fileIn = workingDir+"/krkopt_no_draw.data"; + static String fileIn = workingDir+"/krkopt.data"; static String owlfilename = "complete_nodraw.owl"; Modified: trunk/src/dl-learner/org/dllearner/examples/KRKModular.java =================================================================== --- trunk/src/dl-learner/org/dllearner/examples/KRKModular.java 2008-04-17 06:24:07 UTC (rev 796) +++ trunk/src/dl-learner/org/dllearner/examples/KRKModular.java 2008-04-17 13:02:17 UTC (rev 797) @@ -9,6 +9,7 @@ import java.net.URI; import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedList; import java.util.Random; import java.util.Set; import java.util.SortedSet; @@ -70,7 +71,7 @@ //static boolean useTripleSubProps = useInverse && false; - static String workingDir = "examples/krk/"; + static String workingDir = "examples/krkrecognizer/"; static String allData = workingDir+"krkopt_no_draw.data"; //static String allData = workingDir+"krkopt_original_dataset.data"; @@ -102,15 +103,111 @@ " : " + new File(workingDir).mkdir()+ "."); } } + + public static void main(String[] args) { + main1(args); + //main2(args); + + } + + + public static void main1(String[] args) { + init(); + initAllInstancesWithoutReasoners(); + System.out.println("initializing finished"); + //String currentClass = "ZERO"; + LinkedList<String> ll=new LinkedList<String>(); + + ll.add("DRAW"); + ll.add("ZERO"); + ll.add("ONE"); + ll.add("TWO"); + ll.add("THREE"); + ll.add("FOUR"); + ll.add("FIVE"); + ll.add("SIX"); + ll.add("SEVEN"); + ll.add("EIGHT"); + ll.add("NINE"); + ll.add("TEN"); + ll.add("ELEVEN"); + ll.add("TWELVE"); + ll.add("THIRTEEN"); + ll.add("FOURTEEN"); + ll.add("FIFTEEN"); + ll.add("SIXTEEN"); + + String skript=""; + + for (int i = 0; i < ll.size(); i++) { + System.out.println("progress "+i+" of "+ll.size()); + String currentClass=ll.get(i); + SortedSet<Individual> allPos = classToInd.get(currentClass); + if(classToInd.get(currentClass)==null)continue; + if(currentClass.equals("SIXTEEN"))continue; + classToInd.remove(currentClass); + SortedSet<Individual> neg = new TreeSet<Individual>(); + for (SortedSet<Individual> set : classToInd.values()) { + neg.addAll(set); + } + SortedSet<Integer> lines = getLines(allPos, neg); + KB kb = getKB(lines); + KRKModular km=new KRKModular(kb); + //starting reasone + //km.initReasonerFact(); + String filename=""; + if(i==0) filename="KRK_recognizerDRAW"; + else filename="KRK_recognizer"+(i-1); + km.writeOWLFile(filename+".owl"); + + StringBuffer buf= new StringBuffer(); + buf.append("\nimport(\""+filename+".owl"+"\");\n\n"); + + buf.append("refexamples.ignoredConcepts={\n"); + buf.append("\""+ontologyURI+"#"+currentClass+"\""); + for (String str : classToInd.keySet()) { + buf.append(",\n"); + buf.append("\""+ontologyURI+"#"+str+"\""); + } + + buf.append("};\n\n"); + buf.append("algorithm = refexamples;\n"+ + "reasoner=fastInstanceChecker;\n"+ + "refexamples.useAllConstructor = false;\n"+ + "refexamples.useExistsConstructor = true;\n"+ + "refexamples.useCardinalityRestrictions = false;\n"+ + "refexamples.useNegation = false;\n\n\n"); + + for (Individual ind : allPos) { + buf.append("+\""+ind+"\"\n"); + } + buf.append("\n\n\n"); + for (Individual ind : neg) { + buf.append("-\""+ind+"\"\n"); + } + + writeToFile(workingDir+filename+".conf", buf.toString()); + skript+= "./dllearner "+workingDir+filename+".conf >> "+workingDir+filename+"result.txt\n"; + } + System.out.println(skript); + writeToFile(workingDir+"skript.sh", skript); + + + + } + + /** * @param args */ - public static void main(String[] args) { + public static void main2(String[] args) { init(); initAllInstancesAndReasoners(); System.out.println("initializing finished"); String currentClass = "ZERO"; SortedSet<Individual> allPos = classToInd.get(currentClass); + + //if(allPos.size()<400)negativeExamplesAdded = allPos.size(); //else negativeExamplesAdded = 400; SortedSet<Individual> tmp = new TreeSet<Individual>(); @@ -121,19 +218,18 @@ SortedSet<Individual> neg = getNegativeExamples(currentClass, tmp, negativeExamplesAdded); SortedSet<Integer> lines = getLines(allPos, neg); KB kb = getKB(lines); + Description d=null; + try{ + d = KBParser.parseConcept("EXISTS \"http://dl-learner.org/krk#hasPiece\".(EXISTS \"http://dl-learner.org/krk#hasLowerRankThan\".(EXISTS \"http://dl-learner.org/krk#fileDistanceLessThan1\".(\"http://dl-learner.org/krk#BKing\" AND EXISTS \"http://dl-learner.org/krk#rankDistanceLessThan2\".(\"http://dl-learner.org/krk#WKing\" OR EXISTS \"http://dl-learner.org/krk#rankDistanceLessThan1\".EXISTS \"http://dl-learner.org/krk#rankDistanceLessThan3\".\"http://dl-learner.org/krk#WKing\")) AND (\"http://dl-learner.org/krk#FileA\" OR \"http://dl-learner.org/krk#WKing\")) AND (\"http://dl-learner.org/krk#FileC\" OR (\"http://dl-learner.org/krk#BKing\" AND \"http://dl-learner.org/krk#FileD\")))"); + }catch (Exception e) {e.printStackTrace();} + while (true){ - /*for (String set : classToInd.keySet()) { - for (Individual individual : classToInd.get(set)) { - if(indToClass.get(individual)==null)System.out.println(indToClass.get(individual)); - }}*/ - - - - Description d= learn(kb, allPos, neg); - SortedSet<Individual> result = retrieveAll(d); + + SortedSet<Individual> result = retrieveAll(d); + System.out.println(result); System.out.println("still left: " + (result.size()-allPos.size())); if(verify(currentClass, result)) { System.out.println("Correct solution: "+ d.toKBSyntaxString(ontologyURI+"#", null)); @@ -141,36 +237,11 @@ neg.addAll(getNegativeExamples(currentClass, result, negativeExamplesAdded)); lines = getLines(allPos, neg); kb = getKB(lines); + d= learn(kb, allPos, neg); - } - //System.out.println(allPos); - //System.out.println(neg); - - //cm. - - - /*Map<Class<? extends Component>, String> componentPrefixMapping = Start.createComponentPrefixMapping(); - ComponentManager cm = ComponentManager.getInstance(); - - Set<KnowledgeSource> sources = new HashSet<KnowledgeSource>(); - KnowledgeSource ks = cm.knowledgeSource(KBFile.class); - sources.add(ks); - cm.applyConfigEntry(ks, "url", null); - ks.init(); - */ - - - - - //verifyConcept("EXISTS hasPiece.EXISTS fileDistance0.TOP"); - //verifyConcept("(EXISTS \"http://dl-learner.org/krk#hasPiece\".EXISTS \"http://dl-learner.org/krk#rankDistanceLessThan2\".(\"http://dl-learner.org/krk#BKing\" AND EXISTS \"http://dl-learner.org/krk#fileDistanceLessThan1\".(\"http://dl-learner.org/krk#A\" OR \"http://dl-learner.org/krk#F3\")) AND EXISTS \"http://dl-learner.org/krk#hasPiece\".(\"http://dl-learner.org/krk#WKing\" AND ((\"http://dl-learner.org/krk#C\" AND EXISTS \"http://dl-learner.org/krk#hasLowerRankThan\".\"http://dl-learner.org/krk#A\") OR (\"http://dl-learner.org/krk#F3\" AND EXISTS \"http://dl-learner.org/krk#rankDistance2\".\"http://dl-learner.org/krk#WRook\"))))"); - //verifyConcept("EXISTS hasPiece.TOP"); - - //.writeOWLFile("test.owl"); - } static Description learn(KB kb, SortedSet<Individual> pos,SortedSet<Individual> neg){ @@ -282,6 +353,9 @@ public KRKModular() { this.kb = makeOntologyTBox(); } + public KRKModular(KB kb) { + this.kb = kb; + } public KRKModular(String concept) { this.kb = makeOntologyTBox(concept); @@ -346,8 +420,41 @@ } + public static void initAllInstancesWithoutReasoners(){ + // Datei \xF6ffnen + BufferedReader in = null; + try { + in = new BufferedReader(new FileReader(allData)); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + SimpleClock sc= new SimpleClock(); + KRKModular km =null; + km = new KRKModular(); + try { + String line = ""; + int x = 0; + while ((line = in.readLine()) != null) { + if (x % 1000 == 0) + {sc.print("Currently at line " + x+" : ");} + km.addOneLineToKBinit(x, line); + + //if(x==200)break; + x++; + }// endWhile + km = null; + sc.printAndSet("initialization finished"); + + + } catch (Exception e) { + e.printStackTrace(); + } + + } + + public static SortedSet<Individual> retrieveAll(String concept){ Description d = null; try{ @@ -774,4 +881,5 @@ return null; }*/ + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |