test.java

来自「Semantic Web Ontology Editor」· Java 代码 · 共 940 行 · 第 1/3 页

JAVA
940
字号
					newEntities.addAll(ents);					
				}
				
				if (expandMore) {
					// expand entity set to include usage entities
					for (Iterator iter2 = new HashSet(newEntities).iterator(); iter2.hasNext();) {
						Set usage = new HashSet();
						OWLEntity ent = (OWLEntity) iter2.next();
						if (ent==null || ent.getURI()==null) {
							continue;
						}
					
						// check in local cache first before using OntologyHelper
						if (usageMap.containsKey(ent.getURI())) {
							usage = (HashSet) usageMap.get(ent.getURI());
						}
						else {	
							usage = OntologyHelper.entityUsage(ont, ent);
							usageMap.put(ent.getURI(), usage);
						}
						// only add entities because axioms are returned
						for (Iterator it = usage.iterator(); it.hasNext();) {
							Object e = it.next();
							if (e instanceof OWLEntity) newEntities.add(e);
							else if (e instanceof OWLObject) {
								if (signatureMap.containsKey(e)) {
									newEntities.addAll((HashSet) signatureMap.get(e));
								}
								else {
									Set ents = swoopModel.getAxiomSignature((OWLObject) e, ont);
									signatureMap.put(e, ents);
									newEntities.addAll(ents);					
								}
							}
						}
					}
				}
			}
			
			// get axioms for all newEntities either from local cache or from swoopModel
			Set newAxioms = new HashSet();
			for (Iterator iter2 = newEntities.iterator(); iter2.hasNext();) {
				OWLEntity ent = (OWLEntity) iter2.next();
				if (ent==null || ent.getURI()==null) {
					continue;
				}
				if (axiomMap.containsKey(ent.getURI())) {
					newAxioms.addAll((HashSet) axiomMap.get(ent.getURI()));
				}
				else {
					Set ax = swoopModel.getAxioms(ent, ont);
					axiomMap.put(ent.getURI(), ax);
					newAxioms.addAll(ax);
				}								
			}
			
			if (axioms.containsAll(newAxioms)) {
				return new HashSet();
			}
			else {
				// determine latest axioms
				Set before = new HashSet(axioms);
				Set latest = new HashSet(axioms);
				latest.addAll(newAxioms);
				latest.removeAll(before);
//				// set a limit on axioms to be added
				if (latest.size()>axiomLimit) {
					// only let limited entities remain in latest
					Set copyLatest = new HashSet(latest);
					latest.clear();
					for (int ctr = 0; ctr < axiomLimit; ctr++) {
						Object ax = copyLatest.iterator().next();
						latest.add(ax);
						copyLatest.remove(ax);
					}
					axiomLimit *= 1.25; // slowly increase axiom limit
				}
				newAxioms = latest;
				axioms.addAll(newAxioms);
				return newAxioms;
			}
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		return new HashSet();
	}
	
	public void HSTMUPS(Set mups, OWLOntology onto, OWLClass cla, List MUPS, List explStr, Set satPaths, Set currPath) {
		
		OWLOntBuilder ob = new OWLOntBuilder(onto);
		
		try {
			for (Iterator iter = mups.iterator(); iter.hasNext();) {
				
				allMUPSTimer.start();
				
				// remove some axiom in current MUPS node (mups) from ontology
				// .. add axiom to currPath
				OWLObject axiom = (OWLObject) iter.next();
				ob.addAxiom = false;
				axiom.accept(ob);
				currPath.add(axiom);
				
				// get class in copyOnt
				cla = ob.currentOnt.getClass(cla.getURI());
				
				// early path termination
				boolean earlyTermination = false;
				for (Iterator i=satPaths.iterator(); i.hasNext();) {
					Set satPath = (HashSet) i.next();
					if (satPath.containsAll(currPath)) {
//						System.out.println("EARLY PATH TERMINATION!");
						earlyTermination = true;
						break;
					}
				}
				
				if (!earlyTermination) { 
					// check if there is a new mups of class
					Set newMUPS = new HashSet();
					String expl = "";
					if (allMUPSTimer.isStarted()) allMUPSTimer.stop();
					if (useTableau) {
						// use tableau tracing
						List explList = this.getTableauSOS(ob.currentOnt, cla);
						newMUPS = new HashSet((HashSet) explList.get(1));
					}
					else {
						// use black box
						List explList = this.getBlackBoxSOS(ob.currentOnt, cla);
						newMUPS = new HashSet((HashSet) explList.get(1));
					}
					allMUPSTimer.start();
					if (!newMUPS.isEmpty()) {
						if (!MUPS.contains(newMUPS)) { 
							// print explanation for new MUPS
							MUPS.add(newMUPS);
							explStr.add(expl);
//							System.out.println("FOUND NEW MUPS - MUPS COUNT: "+MUPS.size());
							// recurse!						
							HSTMUPS(newMUPS, ob.currentOnt, cla, MUPS, explStr, satPaths, currPath);
						}
					}
					else {
						satPaths.add(new HashSet(currPath));
					}
				}
				
				// retrace one step
				// .. reinsert axiom into ontology and move up one node in HST
				currPath.remove(axiom);
				ob.addAxiom = true;
				axiom.accept(ob);
				
				if (allMUPSTimer.isStarted()) allMUPSTimer.stop();
			}
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		finally {
			if (allMUPSTimer.isStarted()) allMUPSTimer.stop();			
		}
	}
	
	public void cleanLog() throws Exception {
		BufferedReader in = new BufferedReader(new FileReader(new File("debugEvalLog.txt")));
		String line = null; 
		String newLog = "";
		swoopModel.setShowQNames(false);
	    while (( line = in.readLine()) != null) {
	     	 if (line.indexOf("(")!=-1) {  
			 	String token1 = line.substring(line.indexOf("(")+1, line.indexOf(","));
			 	String token2 = line.substring(line.indexOf(",")+1, line.indexOf(")"));
			 	line = line.replace(token1, swoopModel.shortForm(new URI(token1)));
			 	line = line.replace(token2, swoopModel.shortForm(new URI(token2)));
	     	 }
	     	 newLog += line + NEWLINE;
	    }
	    logFile = newLog;
	    System.out.print("Cleant file..");
	    this.writeLogFile();
	}
	
	public List getTableauSOS(OWLOntology ont, OWLDescription clazz) {
		List explSOS = new ArrayList();
		try {
			Reasoner pelletDebug = new Reasoner();
			pelletDebug.setOntology(ont);
			pelletDebug.getKB().setDoExplanation(true);
			pelletDebug.getKB().doDependencyTracking = true;
			boolean consistent = true;
			Timer glassTime = new Timer("Glass");
		    glassTime.start();
			if (clazz!=null) consistent = pelletDebug.isConsistent(clazz);
			else consistent = pelletDebug.isConsistent();
			glassTime.stop();
			if (consistent) {
				// no SOS cos ABox is consistent!
//				System.out.println("No SOS since ABox is consistent");
				explSOS.add("0");
				explSOS.add(new HashSet()); // empty SOS
				return explSOS;
			}
			Set explanationSet = pelletDebug.getKB().getExplanationSet();
			// prune the axioms in case there are additional axioms
			Set prunedSet = new HashSet(explanationSet);
			
//			OWLOntBuilder ob = new OWLOntBuilder();
//			ob.addAxiom = true;
//			for (Iterator iter = explanationSet.iterator(); iter.hasNext();) {
//				OWLObject axiom = (OWLObject) iter.next();
//				axiom.accept(ob);
//			}
			
			glassTime.start();
			for (Iterator iter = explanationSet.iterator(); iter.hasNext();) {
				OWLObject axiom = (OWLObject) iter.next();
				prunedSet.remove(axiom);
				boolean sat = false;
				sat = this.checkSatisfiability(prunedSet, clazz);
				if (sat) {
					prunedSet.add(axiom);					
				}
			}
			glassTime.stop();
//			System.out.println("Output/Min "+explanationSet.size()+"/"+prunedSet.size());
			explanationSet = prunedSet;
			// end of pruning
			
			explSOS.add(String.valueOf(glassTime.getTotal()));
			explSOS.add(explanationSet);			
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		return explSOS;
	}
	
	public boolean checkSatisfiability(Set axioms, OWLDescription clazz) {
		// create a new ontology with axioms
		// and check satisfiability of clazz
		boolean sat = false;
		try {
			OWLOntBuilder ontBuilder = new OWLOntBuilder();
			
			// create new ontology using axioms
			// use OWLOntBuilder to build a new ontology given axioms
			for (Iterator iter = axioms.iterator(); iter.hasNext();) {
				OWLObject obj = (OWLObject) iter.next();
				obj.accept(ontBuilder);
			}
			
			// if clazz is not in ontology, return true
			OWLOntology newOnt = ontBuilder.currentOnt;
			if (clazz!=null && clazz instanceof OWLClass && newOnt.getClass(((OWLClass) clazz).getURI())==null) return true;
			else if (clazz!=null) {
				// get clazz in newOnt
				clazz = ontBuilder.visitDescription(clazz);				
			}
			
			// create new instance of pellet and check sat. of clazz
//			Reasoner pelletDebug = new Reasoner();
//			pelletDebug.getKB().setDoExplanation(true);
//			pelletDebug.getKB().doDependencyTracking = true;
//			pelletDebug.setOntology(newOnt);			
			PelletReasoner newPellet = new PelletReasoner();
			newPellet.setOntology(newOnt, false);
			if (clazz!=null) sat = newPellet.isConsistent(clazz);
			else sat = newPellet.isConsistent();
		}
		catch (Exception ex) {	
			System.out.println(ex.getMessage()); // clazz (description) may not be in ontology!
//			ex.printStackTrace();
			return true;
		}
		
		return sat;
	}
	
	public void removeOnt() throws Exception {
		if (entTest.isEmpty()) this.loadMap();
		entTest.remove(new URI("http://sweet.jpl.nasa.gov/ontology/earthrealm.owl"));
		this.writeTestEntsMap();
	}
	
	public static void main(String[] args) {
		try {
			test t = new test();
			
			//*** part 1: run through test ontologies, collect interesting entailments, 
			// i.e., unsat classes and inferred subsumption/instantiation
			// and store them in a map - entTest..write to file "debugTestEnts.map"			
			t.init();
//			 t.removeOnt();
			t.viewEntailmentMap();
			
			//*** part 2: read debugTestEnts map, for each ontology, select 20 entailments randomly
			// and run through various reasoners, collect timing information and store in logFile
			// write to "debugEvalLog.txt"
			t.runTests();			
			t.cleanLog();
			
//			System.out.println(t.logFile);
		} 
		catch (Exception e) {
			e.printStackTrace();
		}		
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?