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. |