segmentation.java

来自「Semantic Web Ontology Editor」· Java 代码 · 共 1,418 行 · 第 1/4 页

JAVA
1,418
字号
				 } catch (OWLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}	
			}
		
			
			
			
			
				if (desc instanceof OWLNot){
					try {
						if(this.isPositivelyLocal(((OWLNot)desc).getOperand(), foreign))
								return true;
						return false;
					} catch (OWLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				//All conjuncts must be non-local
				if (desc instanceof OWLAnd){
					Iterator iter;
					try {
						iter = ((OWLAnd)desc).getOperands().iterator();
						while(iter.hasNext()){
							OWLDescription conjunct = (OWLDescription)iter.next();
							if (!this.isNegativelyLocal(conjunct, foreign))
								return false;
								
						}
						return true;
					} catch (OWLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					
					}
				}
				
				//At least one of the disjuncts must be non-local
				if (desc instanceof OWLOr){
					Iterator iter;
					try {
						iter = ((OWLOr)desc).getOperands().iterator();
						while(iter.hasNext()){
							OWLDescription disjunct = (OWLDescription)iter.next();
							if (this.isNegativelyLocal(disjunct, foreign))
								return true;
				
						}
				
					} catch (OWLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
			return false;	
		}
		
		
		public Map getLargeModules(Map sigDependencies, double threshold){
			double maxSize = 5*threshold;
			Map result = new HashMap();
			Map largeModules = new HashMap();
			Iterator it = sigDependencies.keySet().iterator();
			//store large modules
			while(it.hasNext()){
				OWLEntity ent = (OWLEntity)it.next();
				HashSet sigAux = new HashSet();
				sigAux = (HashSet)sigDependencies.get(ent);
				if(sigAux.size()>threshold){
					largeModules.put(ent,sigAux);
				}
			}
			return largeModules;
		}
		
		/*
		public Map mergeLargeModules(Map largeModules, Map signatureDependencies, double maxSize){
			Map mergedLarge = new HashMap();
			Set toMerge = new HashSet();
			mergedLarge.putAll(largeModules);
			//Map result = new HashMap();
			boolean changed = false;
			int merges = 0;
			while(!changed){
			    Iterator j = largeModules.keySet().iterator();
				while(j.hasNext()){
					OWLEntity ent = (OWLEntity)j.next();
					Iterator k = largeModules.keySet().iterator();
					changed = false;
					while(k.hasNext() && !changed){
						OWLEntity ent2 = (OWLEntity)k.next();
						if(!ent2.equals(ent)){
							if(isMergeable(largeModules, ent, ent2, maxSize)){
								toMerge.add(ent2);
								merges++;
								mergedLarge = mergeDependencyEntries(mergedLarge,ent,ent2);
								changed = true;
							}
						}
					}
				 }
				}
				
				System.out.println("Merges Performed: " + merges);
				System.out.println("Remaining Large Modules :" + mergedLarge.keySet().size());
				
				
				Iterator k = toMerge.iterator();
				while(k.hasNext()){
					OWLEntity toDelete = (OWLEntity)k.next();
					signatureDependencies.remove(toDelete);
				}
				signatureDependencies.putAll(mergedLarge);
				return signatureDependencies;
		}
		
		
		public boolean isMergeable(Map sigDependencies, OWLEntity ent1, OWLEntity ent2, double maxSize){
			Set sig1 = new HashSet();
			Set sig2 = new HashSet();
			sig1 = (Set)sigDependencies.get(ent1);
			sig2 = (Set)sigDependencies.get(ent2);
			sig1.addAll(sig2);
			if(sig1.size()< maxSize)
				return true;
			
			return false;
		}
		
		public Map mergeDependencyEntries(Map sigDependencies, OWLEntity ent1, OWLEntity ent2){
			Map result = new HashMap();
			result.putAll(sigDependencies);
			Set sig1 = new HashSet();
			Set sig2 = new HashSet();
			sig1 = (Set)sigDependencies.get(ent1);
			sig2 = (Set)sigDependencies.get(ent2);
			sig1.addAll(sig2);
		    result.remove(ent1);
		    result.remove(ent2);
		    result.put(ent1,sig1);
		    return result;
			}
		
		*/
		
//		Input: Set of all axioms in the ontology.
	    //       Map from axioms in the ontology to signature
		//       Set of all classes in the ontology
		//Output: Map from class names to the signature of their module.
		public Map computeSignatureDependenciesOptimized(Set allAxioms, Map sigToAxioms, Map axSignature, Set allClasses, boolean save) throws Exception{
			int threshold = 200;
			Map result = new HashMap();
			Set alreadyProcessed = new HashSet();
			int countModules = 0;
			int largestModule = 0;
			int countLargeModules = 0;
			int countClasses = 0;
			int skippedClasses = 0;
			
			Iterator it = allClasses.iterator();
			while(it.hasNext()){
				Set sigModule = new HashSet();
				OWLClass cl = (OWLClass)it.next();
				
				if(!alreadyProcessed.contains(cl)){
					//
					//classToModule.put(cl, new HashSet());
					//
					countClasses++;
					if(DEBUG)
						System.out.println("Class: " + cl.getURI().toString());
					sigModule.add(cl);
					//***********************************
					sigModule.addAll(expandSignature(cl,sigModule,sigToAxioms, axSignature, result));
					//************************************
					if(sigModule.size()> largestModule){
						largestModule = sigModule.size();
					}
					
					countModules++;
					
					result.put(cl, sigModule);
					alreadyProcessed.addAll(sigModule);
					
					//if(DEBUG)
						System.out.println("Size: " + sigModule.size());
				
					//if(DEBUG)
						System.out.println(" NUMBER OF MODULES: " + countModules);
				}
				else{
					skippedClasses++;
				}
				//System.out.println("Classes Processed: " + alreadyProcessed.size());
				if(DEBUG){
					System.out.println("Classes Processed: " + alreadyProcessed.size());
					System.out.println("NUMBER OF LOCALITY CHECKS " + nlocalityChecks);
				}
				if(save){
					Set axiomsInModule = new HashSet();
					axiomsInModule =  this.getModuleFromSignature(sigModule,axSignature);
					ShortFormProvider shortFormProvider = new DefaultShortFormProvider();
					URI uriModule= new URI("http://" + shortFormProvider.shortForm(source.getURI()) + "-" + shortFormProvider.shortForm(cl.getURI()) +".owl" );
					System.out.println("Getting module");
					OWLOntology ont = this.getOntologyFromAxioms(axiomsInModule, uriModule);
					System.out.println("Module size (number of classes):" + ont.getClasses().size());
					System.out.println("Saving module");
					String path = "C:/ontologies/Snomed/" + shortFormProvider.shortForm(source.getURI()) + "-" + shortFormProvider.shortForm(cl.getURI()) +".owl";
					this.saveOntologyToDisk(ont,path);
				}
			}
			
			
			
			if(DEBUG){
				System.out.println("Classes Processed: " + countClasses);
				System.out.println("Classes skipped: " + skippedClasses);
				System.out.println("LARGEST module so far " + largestModule);
				System.out.println("NUMBER OF LARGE modules " + countLargeModules);
				System.out.println("NUMBER OF LOCALITY CHECKS " + nlocalityChecks);
			}
			return result;
		}
		
		
		
		public Set getModuleSignature(Set allAxioms, Set sigModule, Map axSignature) throws Exception{
				Set sigAux = new HashSet();
				sigAux.addAll(sigModule);
				Iterator iter = allAxioms.iterator();
				boolean changed = false;
				while(iter.hasNext()){
					OWLObject axiom = (OWLObject)iter.next(); 
					Set sigAxiom = new HashSet();
					//We retrieve the signature of the axiom
					if(axSignature.containsKey(axiom))
						sigAxiom = (Set)axSignature.get(axiom);
					else
						System.out.println("Error in signature");
			
					if(axiom instanceof OWLClassAxiom){
		            		if(!checkLocalitySyntax((OWLClassAxiom)axiom, sigModule)){
		            			sigModule.addAll(sigAxiom);
		            			changed = true;
		            	}
		            
		            }
		            if(axiom instanceof OWLPropertyAxiom){
		                if(!checkLocality((OWLPropertyAxiom)axiom, sigModule)){
		                	sigModule.addAll(sigAxiom);
	            			changed = true;
		            	}
		            }
				}
				if (changed){
					if(sigAux.size() == sigModule.size()){
						return sigModule;
					}
					else{
						if(DEBUG)
							System.out.println("I am repeating the big loop!");
						sigModule.addAll(getModuleSignature(allAxioms,sigModule,axSignature));
						
					}
			}
			return sigModule;
		}
		
		
	
		
		public boolean checkLocalitySyntax(OWLClassAxiom axiom, Set sig) throws Exception{
			
			if (axiom instanceof OWLSubClassAxiom){
				OWLDescription sup = ((OWLSubClassAxiom)axiom).getSuperClass();
				OWLDescription sub = ((OWLSubClassAxiom)axiom).getSubClass();
				
				boolean b1 = isPositivelyLocal(sub, sig);
				boolean b2 = isNegativelyLocal(sup, sig);
				
				if(b1 || b2){
					
					return true;
				}
				
			}
			if (axiom instanceof OWLEquivalentClassesAxiom){
				Set eqclasses = ((OWLEquivalentClassesAxiom)axiom).getEquivalentClasses();
				Iterator iter = eqclasses.iterator();
				if(eqclasses.size() == 2){
					OWLDescription first = (OWLDescription)iter.next();
					OWLDescription second = (OWLDescription)iter.next();
					if((isPositivelyLocal(first,sig) && isPositivelyLocal(second, sig)) || (isNegativelyLocal(first,sig) && isNegativelyLocal(second,sig) ) ){
						return true;
					}
					}
				}
			
		
			
			if(axiom instanceof OWLDisjointClassesAxiom){
				Set disjclasses = ((OWLDisjointClassesAxiom)axiom).getDisjointClasses();
				Iterator iter = disjclasses.iterator();
				while(iter.hasNext()){
					OWLDescription desc = (OWLDescription)iter.next();
					if(isPositivelyLocal(desc,sig)){
							return true;
					}
				}
			
			}
				
			return false;
		}
		
		
		public boolean checkLocality(OWLClassAxiom ax, Set sig) throws Exception{
			PelletReasoner reasoner = new PelletReasoner();
			if (DEBUG)
				System.out.println("Replacing axiom by Bottom");
			OWLClassAxiom axiom= replaceBottom(ax, sig);
		
			if (DEBUG)
				System.out.println("DONE Replacing axiom by Bottom");
			
			if (ax instanceof OWLSubClassAxiom){
					OWLDescription sup = ((OWLSubClassAxiom)axiom).getSuperClass();
					OWLDescription sub = ((OWLSubClassAxiom)axiom).getSubClass();
					if (DEBUG)
						System.out.println("Calling the Reasoner");
					if(reasoner.isSubClassOf(sub,sup)){
						if (DEBUG)
							System.out.println("DONE Calling the Reasoner");
						if(DEBUG)
							System.out.println("The SUBCLASS axiom is local w.r.t. the external signature");
						return true;
					}
					else{
						if(DEBUG)
							System.out.println("The SUBCLASS axiom is NOT local w.r.t. the external signature");
						return false;
					
					}
			}
			if (ax instanceof OWLEquivalentClassesAxiom){
				Set eqclasses = ((OWLEquivalentClassesAxiom)axiom).getEquivalentClasses();
				Iterator iter = eqclasses.iterator();
				if(eqclasses.size() == 2){
					OWLDescription first = (OWLDescription)iter.next();
					OWLDescription second = (OWLDescription)iter.next();
					

⌨️ 快捷键说明

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