mupsfinder.java

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

JAVA
943
字号
			this.writeLogFile();
		}		
	}
	
	public void callReasoners(OWLOntology ont, OWLClass cla) throws Exception {

		Set sos = new HashSet(); //TODO check size / return
		// run all tests by calling reasoners
		// write ontology locally :(
		this.writeOntology(ont);
		
		List MUPS = new ArrayList();		
		
		// ** RACER
//		useRACER = true;
//		useKAON = false;
//		System.out.println("calling RACER");
//		sos = this.getBlackBoxSOS(ont, cla);
//		System.out.println("sos size: "+sos.size());
//		MUPS.add(sos);
//		if (allMUPS) HSTMUPS(sos, ont, cla, MUPS, new ArrayList(), new HashSet(), new HashSet());
//		System.out.println("time: "+(testTimer.getTotal()+racerTime)+" mups size: "+MUPS);
//		logFile += " "+String.valueOf(testTimer.getTotal()+racerTime) + " ("+MUPS.size()+")";
//		racerTime = 0; //***** key
		
		// ** KAON2
//		useKAON = true;
//		useRACER = false;
//		System.out.println("calling KAON2");
//		sos = this.getBlackBoxSOS(ont, cla);
//		System.out.println("sos size: "+sos.size());
//		MUPS = new ArrayList();
//		MUPS.add(sos);
//		if (allMUPS) HSTMUPS(sos, ont, cla, MUPS, new ArrayList(), new HashSet(), new HashSet());
//		System.out.println("time: "+testTimer.getTotal()+" mups size: "+MUPS);
//		logFile += " "+testTimer.getTotal() + " ("+MUPS.size()+")";
		
		// ** Pellet 
		useRACER = false;
		useKAON = false;
		System.out.println("calling Pellet");
		sos = this.getBlackBoxSOS(ont, cla);
		System.out.println("sos size: "+sos.size());
		MUPS = new ArrayList();
		MUPS.add(sos);
		if (allMUPS) HSTMUPS(sos, ont, cla, MUPS, new ArrayList(), new HashSet(), new HashSet());
		System.out.println("time: "+testTimer.getTotal()+" mups size: "+MUPS);
		logFile += " "+testTimer.getTotal() + " ("+MUPS.size()+")";
		
	}
	
	public Set getBlackBoxSOS(OWLOntology ont, OWLClass cla) throws Exception {
		
		Set sos = new HashSet();
		
		// reset all maps and set limit
		axiomLimit = 40;
		usageMap.clear();
		axiomMap.clear();
		signatureMap.clear();
		
		testTimer = new Timer("total");
		testTimer.start();
		
		// add axioms related to class 
		Set axioms = swoopModel.getAxioms(cla, ont);
		axiomMap.put(cla.getURI(), axioms);
		OWLOntBuilder ob = new OWLOntBuilder();
		ob.addAxiom = true; // add axiom mode set to true
		
		// add all base axioms to testOnt via ob
		for (Iterator iter = axioms.iterator(); iter.hasNext();) {
			OWLObject axiom = (OWLObject) iter.next();
			axiom.accept(ob);
		}
		
		// toggle a variable that considers entity usage while expanding axiom set
		boolean expandMore = false;
		
		testTimer.stop();
		
		// add linked references iteratively
		while (checkSatisfiability(ob, cla)) {
			
			testTimer.start();
			
			Set newAxioms = this.expandAxiomSet(axioms, ont, expandMore);
//			System.out.println("Size of axioms: "+axioms.size());
			
			// add axioms from latest to testOnt
			for (Iterator it = newAxioms.iterator(); it.hasNext();) {
				OWLObject axiom = (OWLObject) it.next(); 
				axiom.accept(ob);					
			}
			
			if (newAxioms.isEmpty() && expandMore) {
				System.out.println("ERROR: Could not find axioms responsible for error!");
				testTimer.stop();
				return sos; 
			}
			else if (newAxioms.isEmpty()) {
				expandMore = true;
			}
//			System.out.println(axioms);
			testTimer.stop();
		}
		
		// now axioms contains cla unsatisfiable
		// remove one axiom at a time and if it turns satisfiable, add it to sos
//		System.out.println("Found concept unsatisfiable: #axioms = "+axioms.size());
		
		testTimer.start();
		
		// fast pruning 
		List axiomList = new ArrayList(axioms);
		int pruneWindow = 10;
		if (axiomList.size()>pruneWindow) {
			axioms.clear();
			int parts = axiomList.size() / pruneWindow;
			for (int part=0; part<parts; part++) {
				for (int i=part*pruneWindow; i<part*pruneWindow+pruneWindow; i++) {
					ob.addAxiom = false;
					((OWLObject)axiomList.get(i)).accept(ob);
				}
				testTimer.stop();
				if (checkSatisfiability(ob, cla)) {
					testTimer.start();
					for (int i=part*pruneWindow; i<part*pruneWindow+pruneWindow; i++) {
						axioms.add(axiomList.get(i));
						ob.addAxiom = true;
						((OWLObject)axiomList.get(i)).accept(ob);
					}
					testTimer.stop();
				}
				testTimer.start();
			}
			if (axiomList.size()>parts*pruneWindow) {
				// add remaining from list to axioms
				for (int i=parts*pruneWindow; i<axiomList.size(); i++) {
					axioms.add(axiomList.get(i));
				}
			}
		}
		
		// slow pruning
		for (Iterator iter = new HashSet(axioms).iterator(); iter.hasNext();) {
			OWLObject axiom = (OWLObject) iter.next();
			axioms.remove(axiom);
			ob.addAxiom = false;
			axiom.accept(ob);
			testTimer.stop();
			if (checkSatisfiability(ob, cla)) {
				testTimer.start();
				sos.add(axiom);
				axioms.add(axiom);
				ob.addAxiom = true;
				axiom.accept(ob);
				testTimer.stop();
			}
			testTimer.start();
		}
		
		testTimer.stop();
		return sos;
	}
	
	private boolean checkSatisfiability(OWLOntBuilder ob, OWLDescription clazz) throws Exception {
		
		// check satisfiability of clazz in ont in ob
		OWLOntology newOnt = ob.currentOnt;
		boolean sat = false;
		boolean checkConsistency = false;
		
		if (clazz == null) {
			checkConsistency = true;			
		}
		else {
			// check if clazz is not present at all
			if (clazz instanceof OWLClass && newOnt.getClass(((OWLClass) clazz).getURI())==null) return true;
			clazz = ob.visitDescription(clazz);
		}
        // create new instance of pellet and check sat. of clazz    
		PelletReasoner newPellet = new PelletReasoner();
		newPellet.setOntology(newOnt, false);
		testTimer.start();
		if (checkConsistency) sat = newPellet.isConsistent();
		else sat = newPellet.isConsistent(clazz);
		testTimer.stop();		        
		return sat;
	}
	
	public Set expandAxiomSet(Set axioms, OWLOntology ont, boolean expandMore) {
		
		try {
			Set newEntities = new HashSet();
			
			for (Iterator iter = new HashSet(axioms).iterator(); iter.hasNext();) {
				OWLObject axiom = (OWLObject) iter.next();
				if (signatureMap.containsKey(axiom)) {
					newEntities.addAll((HashSet) signatureMap.get(axiom));
				}
				else {
					Set ents = swoopModel.getAxiomSignature(axiom, ont);
					signatureMap.put(axiom, ents);
					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) {
		
		// key step - make a backup of onto
		OWLOntology backup = swoopModel.cloneOntology(onto);
		
		try {
			for (Iterator iter = mups.iterator(); iter.hasNext();) {
				
				// reset ontology 
				OWLOntology copyOnt = swoopModel.cloneOntology(backup);
				
				testTimer.start();
				
				OWLObject axiom = (OWLObject) iter.next();
				currPath.add(axiom);
//				System.out.println(axiom);
				
				// **** remove axiom from copyOnt *****
				if (axiom instanceof OWLDisjointClassesAxiom) {
					OWLDisjointClassesAxiom dis = (OWLDisjointClassesAxiom) axiom;
					Set disSet = dis.getDisjointClasses();
					Set newDisSet = new HashSet();
					for (Iterator iter2 = disSet.iterator(); iter2.hasNext();) {

⌨️ 快捷键说明

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