segmentation.java
来自「Semantic Web Ontology Editor」· Java 代码 · 共 1,418 行 · 第 1/4 页
JAVA
1,418 行
if (DEBUG)
System.out.println("Calling the Reasoner");
if(reasoner.isEquivalentClass(first, second)){
if (DEBUG)
System.out.println("DONE Calling the Reasoner");
if(DEBUG)
System.out.println("The EQUIVALENTCLASSES axiom is local w.r.t. the external signature");
return true;
}
else{
if(DEBUG)
System.out.println("The EQUIVALENTCLASSES axiom is NOT local w.r.t. the external signature");
return false;
}
}
else
return true;
}
if (ax instanceof OWLDisjointClassesAxiom){
Set disjclasses = ((OWLDisjointClassesAxiom)axiom).getDisjointClasses();
OWLDataFactory df = source.getOWLDataFactory();
OWLDescription conjunction = df.getOWLAnd(disjclasses);
if (DEBUG)
System.out.println("Calling the Reasoner");
if(reasoner.isEquivalentClass(conjunction,this.nothing)){
if (DEBUG)
System.out.println("DONE Calling the Reasoner");
if(DEBUG)
System.out.println("The DISJOINTCLASSES axiom is local w.r.t. the external signature");
return true;
}
else{
if(DEBUG)
System.out.println("The DISJOINTCLASSES axiom is NOT local w.r.t. the external signature");
return false;
}
}
if(DEBUG)
System.out.println("Something WRONG");
return true;
}
public OWLOntology getOntologyFromAxioms(Set s, URI uri) throws URISyntaxException, OWLException{
OWLOntology module;
OWLOntBuilder builder = new OWLOntBuilder(uri);
builder.buildOntologyFromAxioms(s);
module = builder.getCurrentOntology();
return module;
}
//Returns the collection of Axioms in an ontology
private Set getAxiomsInOntology(OWLOntology ont) throws OWLException{
Set result = new HashSet();
AxiomCollector coll = new AxiomCollector(ont);
result = coll.axiomize(ont);
return result;
}
public Set expandSignature(OWLEntity cl, Set processed, Map sigToAxioms, Map axSignature, Map moduleMap) throws Exception{
int niterations = 0;
Set toDo = new HashSet();
toDo.addAll(processed);
Set newSig = new HashSet();
newSig.addAll(processed);
while(!newSig.isEmpty()){
processed.addAll(toDo);
toDo = new HashSet();
toDo.addAll(newSig);
newSig = new HashSet();
//*******************************
newSig.addAll(updateSignature(cl, processed,toDo, sigToAxioms, axSignature, moduleMap));
//********************************
niterations++;
processed.addAll(toDo);
}
if(DEBUG)
System.out.println("Times going through all axioms" + niterations );
return processed;
}
public Set updateSignature(OWLEntity cl, Set processed, Set toDo, Map sigToAxioms, Map axSignature, Map moduleMap) throws Exception{
int avoidedTests = 0;
Set newSig = new HashSet();
Set axioms = new HashSet();
boolean changed = false;
//Expand toDo list
Set toDoAux = new HashSet();
toDoAux.addAll(toDo);
//This is just an optimization
Iterator k = toDoAux.iterator();
while(k.hasNext()){
OWLEntity ent2 = (OWLEntity)k.next();
if(moduleMap.containsKey(ent2)){
Set auxi = (Set)moduleMap.get(ent2);
toDo.addAll(auxi);
}
}
//
Set allSig = new HashSet();
allSig.addAll(toDo);
allSig.addAll(processed);
// Iterate over toDO list
Iterator iter = toDo.iterator();
while(iter.hasNext()){
OWLEntity ent = (OWLEntity)iter.next();
Set aux = new HashSet();
aux.addAll((Set)sigToAxioms.get(ent));
axioms.addAll(aux);
}
if(DEBUG){
System.out.println("Number of axioms we iterate over: " + ": " +axioms.size());
System.out.println("Size of processed signature: " + processed.size());
}
Iterator it = axioms.iterator();
while(it.hasNext()){
OWLObject ax = (OWLObject)it.next();
Set sigAxiom = new HashSet();
//We retrieve the signature of the axiom
sigAxiom.addAll((Set)axSignature.get(ax));
if(!allSig.containsAll(sigAxiom)){
if(ax instanceof OWLClassAxiom){
nlocalityChecks++;
if(!checkLocalitySyntax((OWLClassAxiom)ax, allSig)){
newSig.addAll(sigAxiom);
//
//Set aux = (Set)classToModule.get(cl);
//aux.add(ax);
//classToModule.put(cl,aux);
//
changed = true;
}
}
if(ax instanceof OWLPropertyAxiom){
nlocalityChecks++;
if(!checkLocality((OWLPropertyAxiom)ax, allSig)){
newSig.addAll(sigAxiom);
//
//Set aux = (Set)classToModule.get(cl);
//aux.add(ax);
//classToModule.put(cl,aux);
//
changed = true;
}
}
}
else
avoidedTests++;
}
newSig.removeAll(processed);
newSig.removeAll(toDo);
if(DEBUG)
System.out.println("Avoided locality tests: " + avoidedTests);
return newSig;
}
//Creates a Map:
// Key: Concept names in the ontology
// Value: Set of axioms that mention that concept
private Map signatureToAxioms(Set allAxioms, Map axToSignature) throws OWLException{
Map result = new HashMap();
SignatureCollector col = new SignatureCollector(allAxioms);
result = col.buildSignatureToAxiom(axToSignature);
return result;
}
public Map getClassToModule(){
return classToModule;
}
//Creates a map from axioms in the ontology to their signature
private Map axiomsToSignature(Set allAxioms) throws OWLException{
Map result = new HashMap();
SignatureCollector col = new SignatureCollector(allAxioms);
return col.buildSignatureMap(allAxioms);
/*
Map map = new HashMap();
Iterator iter = allAxioms.iterator();
while(iter.hasNext()){
OWLObject axiom = (OWLObject)iter.next();
Set sig = getAxiomSignature(axiom, source);
map.put(axiom, sig);
}
return map;
*/
}
//Having the signature dependencies for an entity, returns its module as a collection
//of axioms.
public Set getModuleFromSignature(Set sig, Map axSignature){
Set result = new HashSet();
Iterator iter = axSignature.keySet().iterator();
while(iter.hasNext()){
OWLObject axiom = (OWLObject)iter.next();
Set sigAxiom = new HashSet();
sigAxiom = (Set)axSignature.get(axiom);
if(sig.containsAll(sigAxiom)){
result.add(axiom);
}
}
return result;
}
//Returns the Set of Axioms in the Module. This method is currently
//used only in the SWOOP UI, but not in the classification algorithm.
public OWLOntology getModule(Set allAxioms, Set si, Map axSignature, Map sigToAxioms, URI uriOntology, OWLClass cl) throws Exception{
ShortFormProvider shortFormProvider = new DefaultShortFormProvider();
//
String name = "";
Iterator i = si.iterator();
while(i.hasNext()){
OWLEntity e = (OWLEntity)i.next();
name = name + shortFormProvider.shortForm(e.getURI()) + "_";
}
URI uriModule = new URI("http://" + shortFormProvider.shortForm(uriOntology) + "-" + name +".owl");
//
/*
URI uriClass = cl.getURI();
URI uriModule= new URI("http://" + shortFormProvider.shortForm(uriOntology) + "-" + shortFormProvider.shortForm(uriClass) +".owl");
*/
//Get signature of the module
Map moduleMap = new HashMap();
Set sigModule = new HashSet();
sigModule.addAll(expandSignature(cl,si,sigToAxioms, axSignature, moduleMap));
//
Set axiomsInModule = new HashSet();
axiomsInModule = this.getModuleFromSignature(sigModule,axSignature);
OWLOntology module = this.getOntologyFromAxioms(axiomsInModule, uriModule);
return module;
}
// returns the entities in the signature of the axiom (Taken from SwoopModel)
public Set getAxiomSignature(OWLObject axiom, OWLOntology ont) {
Set entities = new HashSet();
try {
OWLOntBuilder ob = new OWLOntBuilder();
axiom.accept(ob);
OWLOntology temp = ob.currentOnt;
for (Iterator iter2=temp.getClasses().iterator(); iter2.hasNext();) {
OWLClass cla = (OWLClass) iter2.next();
entities.add(ont.getClass(cla.getURI()));
}
for (Iterator iter2=temp.getDataProperties().iterator(); iter2.hasNext();) {
OWLDataProperty prop = (OWLDataProperty) iter2.next();
entities.add(ont.getDataProperty(prop.getURI()));
}
for (Iterator iter2=temp.getObjectProperties().iterator(); iter2.hasNext();) {
OWLObjectProperty prop = (OWLObjectProperty) iter2.next();
entities.add(ont.getObjectProperty(prop.getURI()));
}
for (Iterator iter2=temp.getIndividuals().iterator(); iter2.hasNext();) {
OWLIndividual ind = (OWLIndividual) iter2.next();
entities.add(ont.getIndividual(ind.getURI()));
}
}
catch (Exception ex) {
ex.printStackTrace();
}
return entities;
}
public boolean saveOntologyToDisk (OWLOntology ont, String path) throws FileNotFoundException, IOException, OWLException{
File ontFile = new File(path);
OutputStream fileStream = new FileOutputStream(ontFile);
Writer writer = new OutputStreamWriter(fileStream, Charset
.forName("UTF-8"));
CorrectedRDFRenderer rend = new CorrectedRDFRenderer();
StringWriter st = new StringWriter();
rend.renderOntology(ont, st);
writer.write(st.toString());
writer.close();
return true;
}
//This method prunes the modules that are redundant for
//classification
public Map pruneModules(Map signatureTable) throws OWLException {
Set toRemove = new HashSet();
Map auxMap = new HashMap();
//Generate a copy of the Map
auxMap.putAll(signatureTable);
Iterator iter = signatureTable.keySet().iterator();
while(iter.hasNext()){
OWLEntity ent = (OWLEntity)iter.next();
boolean changed = false;
Iterator it = auxMap.keySet().iterator();
while(it.hasNext() &&!changed){
OWLEntity entAux = (OWLEntity)it.next();
if(!entAux.equals(ent)){
Set sig = (Set)auxMap.get(entAux);
if(sig.contains(ent)){
toRemove.add(ent);
System.out.println("Pruning module for " + ent.getURI().toString() );
changed = true;
}
}
}
}
System.out.println("Modules to remove: " +toRemove.size());
Iterator i = toRemove.iterator();
while(i.hasNext()){
OWLEntity ent = (OWLEntity)i.next();
signatureTable.remove(ent);
}
return signatureTable;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?