From: <ku...@us...> - 2008-04-07 07:48:13
|
Revision: 769 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=769&view=rev Author: kurzum Date: 2008-04-07 00:48:07 -0700 (Mon, 07 Apr 2008) Log Message: ----------- script and classes for closing ontologies Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/examples/KRK.java trunk/src/dl-learner/org/dllearner/utilities/OntologyCloser.java Added Paths: ----------- trunk/src/dl-learner/org/dllearner/utilities/CloseOntology.java trunk/src/dl-learner/org/dllearner/utilities/OntologyCloserOWLAPI.java trunk/src/dl-learner/org/dllearner/utilities/SimpleClock.java Modified: trunk/src/dl-learner/org/dllearner/examples/KRK.java =================================================================== --- trunk/src/dl-learner/org/dllearner/examples/KRK.java 2008-04-07 07:46:21 UTC (rev 768) +++ trunk/src/dl-learner/org/dllearner/examples/KRK.java 2008-04-07 07:48:07 UTC (rev 769) @@ -7,6 +7,7 @@ import java.io.FileReader; import java.io.IOException; import java.net.URI; +import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.SortedSet; @@ -50,9 +51,16 @@ // // turn off to not write the owl, needs about 30 seconds or more static boolean writeOWL = true; - static boolean writeClosedOWL = true ; static boolean useTransitivity = false; + static boolean writeExampleSets = true; + static boolean writeConciseOWLAllDifferent = false; + + static boolean closeKB=true; + static boolean closeConcise= true && closeKB; + static boolean writeClosedOWL = true && closeKB; + static boolean verifySomeConcepts = false && closeKB; + static boolean useInverse = false; // dependent, love and marriage, horse and carriage @@ -216,53 +224,116 @@ finishBackgroundForRoles(); System.out.println("Finished Background"); // WRITE - writeExampleSets(); - writeConciseOWLAllDifferent(); + if(writeExampleSets)writeExampleSets(); + if(writeConciseOWLAllDifferent)writeConciseOWLAllDifferent(); + if (writeOWL)writeOWLFile("test.owl"); - - if (writeOWL) { - writeOWLFile("test.owl"); + OntologyCloser oc = new OntologyCloser(kb); + String kbaddition= "_Closed"; + if(closeKB) { + + if(closeConcise) { + oc.applyNumberRestrictionsNamed(); + kbaddition = "_CloseConcise"; + } + else oc.applyNumberRestrictions(); } - if(writeClosedOWL) { - OntologyCloser oc = new OntologyCloser(kb); - oc.applyNumberRestrictions(); - - 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)"; - //conceptStr = "ALL \"http://www.test.de/test#hasPiece\".\"http://www.test.de/test#WKing\""; - //conceptStr = "EXISTS \"http://www.test.de/test#hasPiece\".EXISTS \"http://www.test.de/test#hasLowerRankThan\".(\"http://www.test.de/test#WRook\" AND ALL \"http://www.test.de/test#fileDistanceLessThan1\".\"http://www.test.de/test#WKing\")"; - - conceptStr = "\"http://www.test.de/test#WRook\""; + if (verifySomeConcepts) verifySomeConcepts(oc); + if (writeClosedOWL) writeOWLFile("test"+kbaddition+".owl"); - oc.verifyConcept(conceptStr); - conceptStr = "ALL \"http://www.test.de/test#fileDistanceLessThan1\"." + - "\"http://www.test.de/test#WKing\" "; - - oc.verifyConcept(conceptStr); - conceptStr = "(\"http://www.test.de/test#WRook\" "+ - " AND " + - " ALL \"http://www.test.de/test#fileDistanceLessThan1\"." + - "\"http://www.test.de/test#WKing\") "; - - oc.verifyConcept(conceptStr); - conceptStr = "EXISTS \"http://www.test.de/test#hasLowerRankThan\"."+ - "(\"http://www.test.de/test#WRook\""+ - "AND ALL \"http://www.test.de/test#fileDistanceLessThan1\".\"http://www.test.de/test#WKing\") "; - - - oc.verifyConcept(conceptStr); - //writeOWLFile("test_Closed.owl"); - } - } catch (Exception e) { e.printStackTrace(); } System.out.println("Done"); }// end main + + protected static void verifySomeConcepts(OntologyCloser oc) { + + ArrayList<String> test=new ArrayList<String>(); + + //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)"; + //conceptStr = "ALL \"http://www.test.de/test#hasPiece\".\"http://www.test.de/test#WKing\""; + //conceptStr = "EXISTS \"http://www.test.de/test#hasPiece\".EXISTS \"http://www.test.de/test#hasLowerRankThan\".(\"http://www.test.de/test#WRook\" AND ALL \"http://www.test.de/test#fileDistanceLessThan1\".\"http://www.test.de/test#WKing\")"; + + + test.add("\"http://www.test.de/test#WRook\""); + /*test.add("ALL \"http://www.test.de/test#fileDistanceLessThan1\"." + + "\"http://www.test.de/test#WKing\""); + /*test.add("(\"http://www.test.de/test#WRook\" "+ + " AND " + + " ALL \"http://www.test.de/test#fileDistanceLessThan1\"." + + "\"http://www.test.de/test#WKing\") ");*/ + /*test.add("(\"http://www.test.de/test#Piece\" "+ + " AND " + + " ALL \"http://www.test.de/test#fileDistanceLessThan8\"." + + "\"http://www.test.de/test#Piece\") "); + test.add("(\"http://www.test.de/test#Piece\" "+ + " AND ( " + + " ALL \"http://www.test.de/test#fileDistanceLessThan8\"." + + "\"http://www.test.de/test#Piece\" " + + " AND " + + " EXISTS \"http://www.test.de/test#fileDistanceLessThan8\"." + + "\"http://www.test.de/test#Piece\"))") ; + test.add("(\"http://www.test.de/test#Piece\" "+ + " AND ( " + + " ALL \"http://www.test.de/test#fileDistanceLessThan7\"." + + "\"http://www.test.de/test#Piece\" " + + " AND " + + " EXISTS \"http://www.test.de/test#fileDistanceLessThan7\"." + + "\"http://www.test.de/test#Piece\"))") ; + test.add("(\"http://www.test.de/test#Piece\" "+ + " AND ( " + + " ALL \"http://www.test.de/test#fileDistanceLessThan5\"." + + "\"http://www.test.de/test#Piece\" " + + " AND " + + " EXISTS \"http://www.test.de/test#fileDistanceLessThan5\"." + + "\"http://www.test.de/test#Piece\"))") ;*/ + test.add("(\"http://www.test.de/test#Piece\" "+ + " AND ( " + + " ALL \"http://www.test.de/test#fileDistanceLessThan3\"." + + "\"http://www.test.de/test#Piece\" " + + " AND " + + " EXISTS \"http://www.test.de/test#fileDistanceLessThan3\"." + + "\"http://www.test.de/test#Piece\"))") ; + test.add("(\"http://www.test.de/test#BKing\" "+ + " AND ( " + + " ALL \"http://www.test.de/test#fileDistanceLessThan3\"." + + "\"http://www.test.de/test#Piece\" " + + " AND " + + " EXISTS \"http://www.test.de/test#fileDistanceLessThan3\"." + + "\"http://www.test.de/test#Piece\"))") ; + test.add("(\"http://www.test.de/test#BKing\" "+ + " AND ( " + + " ALL \"http://www.test.de/test#fileDistanceLessThan2\"." + + "\"http://www.test.de/test#Piece\" " + + " AND " + + " EXISTS \"http://www.test.de/test#fileDistanceLessThan2\"." + + "\"http://www.test.de/test#Piece\"))") ; + + /*test.add("(\"http://www.test.de/test#Piece\" "+ + " AND " + + " ALL \"http://www.test.de/test#fileDistanceLessThan1\"." + + "\"http://www.test.de/test#WKing\") ");*/ + + for (int i = 0; i < test.size(); i++) { + String conceptStr = test.get(i); + oc.verifyConcept(conceptStr); + } + + + /*conceptStr = "EXISTS \"http://www.test.de/test#hasLowerRankThan\"."+ + "(\"http://www.test.de/test#WRook\""+ + "AND ALL \"http://www.test.de/test#fileDistanceLessThan1\".\"http://www.test.de/test#WKing\") "; + + */ + System.out.println(); + } + static void makeDistanceRoles(KRKPiece A, KRKPiece B) { int Fdist = A.getFileDistance(B); int Rdist = A.getRankDistance(B); Added: trunk/src/dl-learner/org/dllearner/utilities/CloseOntology.java =================================================================== --- trunk/src/dl-learner/org/dllearner/utilities/CloseOntology.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/utilities/CloseOntology.java 2008-04-07 07:48:07 UTC (rev 769) @@ -0,0 +1,69 @@ +package org.dllearner.utilities; + +import java.io.File; +import java.net.URI; +import java.util.HashSet; +import java.util.Set; + +import org.dllearner.core.KnowledgeSource; +import org.dllearner.kb.OWLFile; +import org.dllearner.reasoning.OWLAPIReasoner; + +public class CloseOntology { + + /** + * @param args + */ + public static void main(String[] args) { + String ontopath="examples/krkworking/test.owl"; + ontopath = args[0]; + File file = new File(ontopath); + + //System.out.println(file.getAbsolutePath()); + System.out.println(file.toURI()); + + + try{ + URI inputURI = file.toURI(); + URI outputURI; + OWLFile owlFile=new OWLFile(); + owlFile.setURL(inputURI.toURL()); + + Set<KnowledgeSource> ks = new HashSet<KnowledgeSource>(); + ks.add(owlFile); + OWLAPIReasoner owlapireasoner = new OWLAPIReasoner(ks); + owlapireasoner.init(); + OntologyCloserOWLAPI oc= new OntologyCloserOWLAPI(owlapireasoner); + oc.testForTransitiveProperties(true); + String ending = ontopath.substring(ontopath.lastIndexOf(".") + 1); + /*ontopath=ontopath.replace("."+ending, "_test."+ending); + file=new File(ontopath); + URI outputURI = file.toURI(); + oc.writeOWLFile(outputURI);*/ + System.out.println("Attempting to close"); + oc.applyNumberRestrictionsConcise(); + System.out.println("Finished, preparing output"); + //String ending = ontopath.substring(ontopath.lastIndexOf(".") + 1); + ontopath=ontopath.replace("."+ending, "_closedConcise."+ending); + file=new File(ontopath); + outputURI = file.toURI(); + oc.writeOWLFile(outputURI); + + //ontopath=ontopath.replace("_closed."+ending, "_ClosedConcise."+ending); + //file=new File(ontopath); + //outputURI = file.toURI(); + //oc.writeOWLFile(outputURI); + + + //System.out.println(ontopath); + + /*manager.saveOntology(ontology, new OWLXMLOntologyFormat(), + physicalURI2); + + manager.removeOntology(ontology.getURI()); + */ + + }catch (Exception e) {e.printStackTrace();} + } + +} Modified: trunk/src/dl-learner/org/dllearner/utilities/OntologyCloser.java =================================================================== --- trunk/src/dl-learner/org/dllearner/utilities/OntologyCloser.java 2008-04-07 07:46:21 UTC (rev 768) +++ trunk/src/dl-learner/org/dllearner/utilities/OntologyCloser.java 2008-04-07 07:48:07 UTC (rev 769) @@ -2,16 +2,21 @@ import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedList; import java.util.Map; import java.util.Set; import java.util.SortedSet; +import java.util.TreeSet; 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.EquivalentClassesAxiom; import org.dllearner.core.owl.Individual; +import org.dllearner.core.owl.Intersection; import org.dllearner.core.owl.KB; +import org.dllearner.core.owl.NamedClass; import org.dllearner.core.owl.ObjectExactCardinalityRestriction; import org.dllearner.core.owl.ObjectProperty; import org.dllearner.core.owl.PropertyAxiom; @@ -19,22 +24,31 @@ import org.dllearner.kb.KBFile; import org.dllearner.parser.KBParser; import org.dllearner.reasoning.OWLAPIReasoner; +import org.semanticweb.owl.model.OWLOntology; public class OntologyCloser { KB kb; KBFile kbFile; - Set<KnowledgeSource> ks; + OWLOntology onto; + OWLAPIReasoner reasoner; + boolean isKB = true; + + // Set<KnowledgeSource> ks; ReasoningService rs; + HashMap<Individual, Set<ObjectExactCardinalityRestriction>> indToRestr; + HashMap<Individual, Set<Description>> indToNamedClass; + HashSet<Description> classes; public OntologyCloser(KB kb) { super(); this.kb = kb; this.kbFile = new KBFile(this.kb); - this.ks = new HashSet<KnowledgeSource>(); - this.ks.add(this.kbFile); + Set<KnowledgeSource> ks = new HashSet<KnowledgeSource>(); + ks.add(this.kbFile); OWLAPIReasoner owlapi = new OWLAPIReasoner(ks); owlapi.init(); - + this.indToRestr = new HashMap<Individual, Set<ObjectExactCardinalityRestriction>>(); + this.classes = new HashSet<Description>(); this.rs = new ReasoningService(owlapi); } @@ -46,17 +60,38 @@ 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")) { + testForTransitiveProperties(true); - 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()); + } + + + + /** + * counts the number of roles used by each individual and assigns + * ExactCardinalityRestriction + */ + public void applyNumberRestrictionsConcise() { + Set<ObjectProperty> allRoles = this.rs.getAtomicRoles(); + // Set<Individual> allind = this.rs.getIndividuals(); + testForTransitiveProperties(true); + for (ObjectProperty oneRole : allRoles) { // System.out.println(oneRole.getClass()); @@ -67,39 +102,165 @@ if (fillers.size() > 0) { ObjectExactCardinalityRestriction oecr = new ObjectExactCardinalityRestriction( fillers.size(), oneRole, new Thing()); - kb.addABoxAxiom(new ClassAssertionAxiom(oecr, oneInd)); + // indToRestr.put(oneInd,) + collectExObjRestrForInd(oneInd, oecr); } } + // + + }// end for + + // Intersection intersection = null; + LinkedList<Description> ll = new LinkedList<Description>(); + Set<ObjectExactCardinalityRestriction> s = null; + + for (Individual oneInd : indToRestr.keySet()) { + s = indToRestr.get(oneInd); + for (ObjectExactCardinalityRestriction oecr : s) { + ll.add(oecr); + } + kb.addABoxAxiom(new ClassAssertionAxiom(new Intersection(ll), + oneInd)); + s = null; + ll = new LinkedList<Description>(); } + + // + // System.out.println("good ontology? " + rs.isSatisfiable()); } + + + /** + * counts the number of roles used by each individual and assigns + * ExactCardinalityRestriction + */ + public void applyNumberRestrictionsNamed() { + Set<ObjectProperty> allRoles = this.rs.getAtomicRoles(); + // Set<Individual> allind = this.rs.getIndividuals(); + testForTransitiveProperties(true); + + 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()); + // indToRestr.put(oneInd,) + //make Description + Description d = new NamedClass(oneRole+"Exact"+fillers.size()+"gen"); + d.addChild(oecr); + kb.addTBoxAxiom(new EquivalentClassesAxiom(d,oecr)); + //System.out.println(d.toManchesterSyntaxString("", new HashMap<String, String>())); + kb.addABoxAxiom(new ClassAssertionAxiom(d,oneInd)); + //collectExObjRestrForInd(oneInd, oecr); + + } + + // + + }// end for + + // Intersection intersection = null; + /* + LinkedList<Description> ll = new LinkedList<Description>(); + Set<ObjectExactCardinalityRestriction> s = null; + + for (Individual oneInd : indToRestr.keySet()) { + s = indToRestr.get(oneInd); + for (ObjectExactCardinalityRestriction oecr : s) { + ll.add(oecr); + } + kb.addABoxAxiom(new ClassAssertionAxiom(new Intersection(ll), + oneInd)); + s = null; + ll = new LinkedList<Description>(); + }*/ + + // + + // System.out.println("good ontology? " + rs.isSatisfiable()); + + } + + public boolean testForTransitiveProperties(boolean printflag) { + boolean retval = false; + Set<PropertyAxiom> ax = kb.getRbox(); + for (PropertyAxiom propertyAxiom : ax) { + if (propertyAxiom.getClass().getSimpleName().equals( + "TransitiveObjectPropertyAxiom")) { + retval = true; + if (printflag) { + System.out + .println("WARNING transitive object property can't be used in cardinality restriction\n" + + propertyAxiom.toString()); + } + } + } + + return retval; + } + public static void closeKB(KB kb) { new OntologyCloser(kb).applyNumberRestrictions(); } - public void verifyConcept(String conceptStr) { + public SortedSet<Individual> verifyConcept(String conceptStr) { Description d; + SimpleClock sc = new SimpleClock(); StringBuffer sb = new StringBuffer(); sb.append(conceptStr); conceptStr = sb.toString(); + SortedSet<Individual> ind = new TreeSet<Individual>(); try { d = KBParser.parseConcept(conceptStr); - System.out.println(d.toManchesterSyntaxString("", new HashMap<String,String>())); - System.out.println(d.toString()); - System.out.println("Starting retrieval"); - SortedSet<Individual> ind = this.rs.retrieval(d); + System.out.println("\n*******************\nStarting retrieval"); + System.out.println(d.toManchesterSyntaxString("", + new HashMap<String, String>())); + // System.out.println(d.toString()); + sc.setTime(); + this.rs.retrieval(d); + System.out.println("retrieved: " + ind.size() + " instances"); + sc.printAndSet(); + for (Individual individual : ind) { + System.out.print(individual + "|"); + } - //for (Individual individual : ind) { - //System.out.println(individual + ""); - //} - } catch (Exception e) { e.printStackTrace(); } + return ind; } + private boolean collectExObjRestrForInd(Individual ind, + ObjectExactCardinalityRestriction oecr) { + Set<ObjectExactCardinalityRestriction> s = indToRestr.get(ind); + if (s == null) { + indToRestr.put(ind, + new HashSet<ObjectExactCardinalityRestriction>()); + s = indToRestr.get(ind); + } + return s.add(oecr); + } + + @SuppressWarnings("unused") + private boolean collectDescriptionForInd(Individual ind, + Description d) { + Set<Description> s = indToNamedClass.get(ind); + if (s == null) { + indToNamedClass.put(ind, + new HashSet<Description>()); + s = indToNamedClass.get(ind); + } + return s.add(d); + } + } Added: trunk/src/dl-learner/org/dllearner/utilities/OntologyCloserOWLAPI.java =================================================================== --- trunk/src/dl-learner/org/dllearner/utilities/OntologyCloserOWLAPI.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/utilities/OntologyCloserOWLAPI.java 2008-04-07 07:48:07 UTC (rev 769) @@ -0,0 +1,264 @@ +package org.dllearner.utilities; + +import java.net.URI; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; + +import org.dllearner.core.ReasoningService; +import org.dllearner.core.owl.Description; +import org.dllearner.core.owl.Individual; +import org.dllearner.core.owl.ObjectProperty; +import org.dllearner.parser.KBParser; +import org.dllearner.reasoning.OWLAPIReasoner; +import org.semanticweb.owl.apibinding.OWLManager; +import org.semanticweb.owl.io.OWLXMLOntologyFormat; +import org.semanticweb.owl.model.AddAxiom; +import org.semanticweb.owl.model.OWLAxiom; +import org.semanticweb.owl.model.OWLDataFactory; +import org.semanticweb.owl.model.OWLDescription; +import org.semanticweb.owl.model.OWLOntology; +import org.semanticweb.owl.model.OWLOntologyManager; +import org.semanticweb.owl.model.OWLPropertyAxiom; + +import uk.ac.manchester.cs.owl.OWLObjectExactCardinalityRestrictionImpl; +import uk.ac.manchester.cs.owl.OWLObjectIntersectionOfImpl; + +public class OntologyCloserOWLAPI { + + OWLOntology onto; + OWLAPIReasoner reasoner; + //Set<KnowledgeSource> ks; + ReasoningService rs; + HashMap<Individual,Set<OWLObjectExactCardinalityRestrictionImpl>> indToRestr; + OWLDataFactory factory; + OWLOntologyManager manager; + public int numberOfStatementsChanged = 0; + + public OntologyCloserOWLAPI(OWLAPIReasoner reasoner) { + //this.onto = reasoner.getOWLAPIOntologies().get(0); + this.reasoner = reasoner; + this.indToRestr = new HashMap<Individual,Set<OWLObjectExactCardinalityRestrictionImpl>> (); + this.rs = new ReasoningService(reasoner); + this.manager = OWLManager.createOWLOntologyManager(); + this.factory = manager.getOWLDataFactory(); + + this.onto = reasoner.getOWLAPIOntologies().get(0); + //URI ontologyURI = URI.create(reasoner.getBaseURI()); + //URI physicalURI = URI.create(owlFile); + //SimpleURIMapper mapper = new SimpleURIMapper(ontologyURI, physicalURI); + //manager.addURIMapper(mapper); + /*try{ + this.onto = manager.loadOntologyFromPhysicalURI(physicalURI); + }catch (Exception e) {e.printStackTrace();} + //manager.getOntologies().add(this.onto); + System.out.println(manager.getOntologies()); + System.out.println(this.onto);*/ + //reasoner = new org.mindswap.pellet.owlapi.Reasoner(manager); + //System.out.println(this.rs.getAtomicConcepts()); + + } + + + /** + * counts the number of roles used by each individual and assigns + * ExactCardinalityRestriction + */ + public void applyNumberRestrictions() { + System.out.println("applyNRES"); + Set<ObjectProperty> allRoles = this.rs.getAtomicRoles(); + System.out.println("found: "+allRoles.size()+" roles"); + // Set<Individual> allind = this.rs.getIndividuals(); + //testForTransitiveProperties(true); + + + + 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) { + OWLObjectExactCardinalityRestrictionImpl oecr = + new OWLObjectExactCardinalityRestrictionImpl( + factory, + factory.getOWLObjectProperty(URI.create(oneRole.getName())), + fillers.size(), + factory.getOWLThing() + ); + //System.out.println(oneRole.getName()+"||"+fillers.size()+"||"+oneInd); + /*ObjectExactCardinalityRestriction oecr = new ObjectExactCardinalityRestriction( + fillers.size(), oneRole, new Thing());*/ + //kb.addABoxAxiom(new ClassAssertionAxiom(oecr, oneInd)); + OWLAxiom axiom = factory.getOWLClassAssertionAxiom( + factory.getOWLIndividual(URI.create(oneInd.getName())) + , oecr); + AddAxiom addAxiom = new AddAxiom(this.onto, axiom); + try{ + manager.applyChange(addAxiom); + }catch (Exception e) {e.printStackTrace();} + } + } + + } + // System.out.println("good ontology? " + rs.isSatisfiable()); + + } + + /** + * counts the number of roles used by each individual and assigns + * ExactCardinalityRestriction + */ + public void applyNumberRestrictionsConcise() { + + Set<ObjectProperty> allRoles = this.rs.getAtomicRoles(); + // Set<Individual> allind = this.rs.getIndividuals(); + // testForTransitiveProperties(true); + + 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) { + OWLObjectExactCardinalityRestrictionImpl oecr = + new OWLObjectExactCardinalityRestrictionImpl( + factory, + factory.getOWLObjectProperty(URI.create(oneRole.getName())), + fillers.size(), + factory.getOWLThing() + ); + //indToRestr.put(oneInd,) + + collectExObjRestrForInd(oneInd, oecr); + } + } + + // + + }//end for + + //Intersection intersection = null; + Set<OWLDescription> target = new HashSet<OWLDescription>(); + Set<OWLObjectExactCardinalityRestrictionImpl> s = null; + + for (Individual oneInd : indToRestr.keySet()) { + s = indToRestr.get(oneInd); + for (OWLObjectExactCardinalityRestrictionImpl oecr : s) { + target.add(oecr); + } + OWLObjectIntersectionOfImpl intersection = + new OWLObjectIntersectionOfImpl(this.factory,target); + //kb.addABoxAxiom(new ClassAssertionAxiom(new OWLIntersection(ll), oneInd)); + + + s = null; + target = new HashSet<OWLDescription>(); + + OWLAxiom axiom = factory.getOWLClassAssertionAxiom( + factory.getOWLIndividual(URI.create(oneInd.getName())) + , intersection); + AddAxiom addAxiom = new AddAxiom(this.onto, axiom); + try{ + manager.applyChange(addAxiom); + numberOfStatementsChanged++; + }catch (Exception e) {e.printStackTrace();} + + }//end for + + + + // + + // System.out.println("good ontology? " + rs.isSatisfiable()); + + } + + + public boolean testForTransitiveProperties(boolean printflag) { + + boolean retval = false; + + Set<OWLPropertyAxiom> ax = onto.getObjectPropertyAxioms(); + for (OWLPropertyAxiom propertyAxiom : ax) { + //System.out.println(propertyAxiom.getClass().getSimpleName()); + if (propertyAxiom.getClass().getSimpleName().equals( + "OWLTransitiveObjectPropertyAxiomImpl")) { + retval = true; + if(printflag) { + System.out.println( + "WARNING transitive object property can't be used in cardinality restriction\n" + + propertyAxiom.toString()+"but I'm ignoring it"); + } + } + } + if(printflag) { + System.out.println( + "No transitive Properties found"); + } + return retval; + } + +/* + public static void closeKB(KB kb) { + new OntologyCloserOWLAPI(kb).applyNumberRestrictions(); + } +*/ + + public SortedSet<Individual> verifyConcept(String conceptStr) { + + Description d; + SimpleClock sc=new SimpleClock(); + StringBuffer sb = new StringBuffer(); + sb.append(conceptStr); + conceptStr = sb.toString(); + SortedSet<Individual> ind = new TreeSet<Individual>(); + try { + d = KBParser.parseConcept(conceptStr); + System.out.println("\n*******************\nStarting retrieval"); + System.out.println(d.toManchesterSyntaxString("", new HashMap<String,String>())); + //System.out.println(d.toString()); + sc.setTime(); + this.rs.retrieval(d); + + System.out.println("retrieved: " + ind.size() + " instances"); + sc.printAndSet(); + for (Individual individual : ind) { + System.out.print(individual + "|"); + } + + + + } catch (Exception e) { + e.printStackTrace(); + } + return ind; + } + + public void writeOWLFile(URI filename){ + try{ + manager.saveOntology(this.onto, new OWLXMLOntologyFormat(), + filename); + }catch (Exception e) {e.printStackTrace();} + } + + + private boolean collectExObjRestrForInd(Individual ind, OWLObjectExactCardinalityRestrictionImpl oecr){ + Set<OWLObjectExactCardinalityRestrictionImpl> s = indToRestr.get(ind); + + if(s==null){ + + indToRestr.put(ind, new HashSet<OWLObjectExactCardinalityRestrictionImpl>()); + s = indToRestr.get(ind); + } + return s.add(oecr); + } + +} Added: trunk/src/dl-learner/org/dllearner/utilities/SimpleClock.java =================================================================== --- trunk/src/dl-learner/org/dllearner/utilities/SimpleClock.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/utilities/SimpleClock.java 2008-04-07 07:48:07 UTC (rev 769) @@ -0,0 +1,20 @@ +package org.dllearner.utilities; + +public class SimpleClock { + private long time; + + public SimpleClock() { + time=System.currentTimeMillis(); + } + + public void printAndSet() { + long now=System.currentTimeMillis(); + System.out.println("needed "+(now-time)+" ms"); + time=now; + } + + public void setTime() { + time=System.currentTimeMillis(); + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |