test.java

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

JAVA
940
字号
				remove.add(new RemoveEntity(ont, cla, null));			
			}
			
			// call key method now
			this.callReasoners(ont, cla);
			
			// remove temp changes
			for (Iterator iter2 = remove.iterator(); iter2.hasNext();) {
				OntologyChange oc = (OntologyChange) iter2.next();
				oc.accept((ChangeVisitor) ont);
			}
			
			logFile += NEWLINE;
			
			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);
//		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();
		
		// ** Pellet 
		useRACER = false;
		useKAON = false;
		System.out.println("calling Pellet");
		List explList = new ArrayList();
		if (this.useTableau) explList = this.getTableauSOS(ont, cla);
		else 
			explList = this.getBlackBoxSOS(ont, cla);
		long time = Long.parseLong(explList.get(0).toString());
		sos = (HashSet) explList.get(1);
		System.out.println("sos time: "+time+" #axioms: "+sos.size());
		logFile += " "+time;
//		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: "+allMUPSTimer.getTotal()+" #mups: "+MUPS.size()+MUPS);
		logFile += " "+allMUPSTimer.getTotal()+" MUPS: "+MUPS.size();
		
		// useTableau
//		List explList2 = this.getTableauSOS(ont, cla);
//		long time2 = Long.parseLong(explList2.get(0).toString());
//		Set expl = (HashSet) explList2.get(1);
//		System.out.println("hybrid time: "+time2+" mups size: "+expl.size());
//		logFile += " "+time2;
		
//		Reasoner pellet = new Reasoner();
//		Timer normal = new Timer("Normal");
//		pellet.setOntology(ont);
//		normal.start();
//		pellet.isConsistent(cla);
//		normal.stop();
//		logFile += " "+normal.getTotal();
//		System.out.println("normal time: "+normal.getTotal());
	}
	
	public List getBlackBoxSOS(OWLOntology ont, OWLClass cla) throws Exception {
		
		List expl = new ArrayList();
		
		// reset all maps and set limit
		axiomLimit = 40;
		usageMap.clear();
		axiomMap.clear();
		signatureMap.clear();
		
		Timer blackTime = new Timer("total");
		blackTime.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();
				expl.add("0");
				expl.add(new HashSet());
				return expl; 
			}
			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
		Set sos = new HashSet(axioms);
		for (Iterator iter = axioms.iterator(); iter.hasNext();) {
			OWLObject axiom = (OWLObject) iter.next();
			sos.remove(axiom);
			boolean sat = false;
			sat = this.checkSatisfiability(sos, cla);
			if (sat) {
				sos.add(axiom);					
			}
		}
//		for (Iterator iter = axioms.iterator(); iter.hasNext();) {
//			OWLObject axiom = (OWLObject) iter.next();
//			ob.addAxiom = false;
//			axiom.accept(ob);
////			testTimer.stop();
//			if (checkSatisfiability(ob, cla)) {
////				testTimer.start();
//				sos.add(axiom);
//				ob.addAxiom = true;
//				axiom.accept(ob);
////				testTimer.stop();
//			}
////			testTimer.start();
//		}
		
		blackTime.stop();
		expl.add(String.valueOf(blackTime.getTotal()));
		expl.add(sos);
		return expl;
	}
	
	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);
		}
		
		// use reasoner to check class consistency
		String fname = "file:///C:/Docume~1/UMD/MyDocu~1/Semant~1/SWOOP/test.owl";
//		if (useRACER) {
//			// communicate with Racer using JRacer API
//			try {
//				// write ontology locally!
//				this.writeOntology(newOnt);
//				
//				// communicate with RACER
//				RacerServer racer = new RacerServer("localhost",8088);
//				racer.openConnection();
////				racer.send("(logging-on)");
//				racer.send("(owl-read-document \""+fname+"\")");
//				String response = "";
//				PrintStream temp = System.out;
//				System.setOut(new PrintStream(new FileOutputStream(new File("time.txt"))));
//			    if (checkConsistency) {
//					response = racer.send("(time (abox-consistent-p))");
//				}
//				else {
//					response = racer.send("(time (concept-satisfiable? "+"|"+((OWLClass) clazz).getURI().toString()+"|))");					
//				}
//			    BufferedReader in = new BufferedReader(new FileReader(new File("time.txt")));
//		        String line = null, hack = "";
//		        while (( line = in.readLine()) != null) {
//			       hack += line;
//			    }
//			    System.setOut(temp);
//			    int spos = hack.lastIndexOf("took");
//			    int epos = hack.lastIndexOf("seconds");
//			    if (spos!=-1 && epos!=-1) {
//			   	   String time = hack.substring(spos+4, epos);
//				   racerTime += (long) Float.parseFloat(time)*1000;				   
//			    }
//				sat = (response.equalsIgnoreCase("nil")) ? false : true;
//			}
//			catch (Exception ex) {System.out.println("No RACER: "+ex.getMessage());}
//		}
		if (useKAON) {
			// write ontology!
			this.writeOntology(newOnt);
			
			// communicate with KAON2 using API
			KAON2Connection connection = KAON2Manager.newConnection();
			DefaultOntologyResolver resolver = new DefaultOntologyResolver();
			resolver.registerReplacement(newOnt.getLogicalURI().toString(), fname); //"http://kaon2.semanticweb.org/example1","file:src/ex1/example1.xml");
	        connection.setOntologyResolver(resolver);
	        // read in ontology
	        try {
	        	Ontology ontology = connection.openOntology(newOnt.getLogicalURI().toString(), new HashMap());
	        	org.semanticweb.kaon2.api.owl.elements.OWLClass cla = KAON2Manager.factory().owlClass(((OWLClass) clazz).getURI().toString());
		        org.semanticweb.kaon2.api.reasoner.Reasoner reasoner = ontology.createReasoner();
		        if (checkConsistency) sat = reasoner.isSatisfiable();
				else sat = reasoner.isSatisfiable(cla);
				
	        }
	        catch (Exception ex) {System.out.println("No KAON: "+ex.getMessage());}	        		    	        
		}
		else {
			// create new instance of pellet and check sat. of clazz
//			PelletReasoner pellet = new PelletReasoner();
//			pellet.setOntology(newOnt, false);
			Reasoner pellet = new Reasoner();
			pellet.setOntology(newOnt);
			if (checkConsistency) sat = pellet.isConsistent();
			else sat = pellet.isConsistent(clazz);					        
		}		
		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);

⌨️ 快捷键说明

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