All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.protege.owl.diff.present.Changes Maven / Gradle / Ivy

Go to download

Engine for calculating differences between two OWL ontologies. Used by the OWL Difference plug-in for the Protege Desktop ontology editor.

The newest version!
package org.protege.owl.diff.present;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.SortedSet;
import java.util.Map.Entry;
import java.util.TreeSet;

import org.protege.owl.diff.align.OwlDiffMap;
import org.protege.owl.diff.util.GetAxiomSourceVisitor;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLEntity;

public class Changes {
    private Collection unmatchedSourceAxiomsWithNoSubject = new HashSet();
    private Collection unmatchedTargetAxiomsWithNoSubject = new HashSet();
    private SortedSet entityBasedDiffs  = new TreeSet();
    private Map sourceDiffMap = new HashMap();
    private Map targetDiffMap = new HashMap();
    private AxiomDescribesEntitiesDetector sourceEntitiesDetector;
    private AxiomDescribesEntitiesDetector targetEntitiesDetector;

    private OwlDiffMap diffMap;
    
    public Changes(OwlDiffMap diffMap) {
        this.diffMap = diffMap;
        sourceEntitiesDetector = new GetAxiomSourceVisitor(diffMap.getSourceOntology(), diffMap.getOWLDataFactory());
        targetEntitiesDetector = new GetAxiomSourceVisitor(diffMap.getTargetOntology(), diffMap.getOWLDataFactory());
        initialiseDiffs();
    }

    public SortedSet getEntityBasedDiffs() {
        return entityBasedDiffs;
    }
    
    public Map getSourceDiffMap() {
		return sourceDiffMap;
	}
    
    public Map getTargetDiffMap() {
		return targetDiffMap;
	}
    
    public OwlDiffMap getRawDiffMap() {
		return diffMap;
	}
    
    public Collection getUnmatchedSourceAxiomsWithNoSubject() {
        return unmatchedSourceAxiomsWithNoSubject;
    }

    public Collection getUnmatchedTargetAxiomsWithNoSubject() {
        return unmatchedTargetAxiomsWithNoSubject;
    }
    
    public boolean containsMatch(MatchedAxiom match) {
    	if (match.getSourceAxiom() != null) {
    		Collection subjects = sourceEntitiesDetector.getSources(match.getSourceAxiom());
    		if (!subjects.isEmpty()) {
    			OWLEntity entity = subjects.iterator().next();
    			EntityBasedDiff entityDiff = sourceDiffMap.get(entity);
    			return entityDiff != null && entityDiff.getAxiomMatches().contains(match);
    		}
    	}
    	if (match.getTargetAxiom() != null) {
    		Collection subjects = sourceEntitiesDetector.getSources(match.getTargetAxiom());
    		if (!subjects.isEmpty()) {
    			OWLEntity entity = subjects.iterator().next();
    			EntityBasedDiff entityDiff = targetDiffMap.get(entity);
    			return entityDiff != null && entityDiff.getAxiomMatches().contains(match);
    		}
    	}
    	return (match.getDescription().equals(MatchedAxiom.AXIOM_DELETED) && unmatchedSourceAxiomsWithNoSubject.contains(match.getSourceAxiom())) 
    	          || (match.getDescription().equals(MatchedAxiom.AXIOM_ADDED) && unmatchedTargetAxiomsWithNoSubject.contains(match.getTargetAxiom()));
    }
    
    public void addMatch(MatchedAxiom match) {
    	if (match.getSourceAxiom() != null) {
    		OWLAxiom axiom = match.getSourceAxiom();
    		Collection subjects = sourceEntitiesDetector.getSources(axiom);
    		for (OWLEntity e : subjects) {
    			EntityBasedDiff diff = sourceDiffMap.get(e);
    			if (diff == null) {
    				diff = new EntityBasedDiff();
    				diff.setSourceEntity(e);
    				diff.setTargetEntity(e);
    				sourceDiffMap.put(e, diff);
    				targetDiffMap.put(e, diff);
    				entityBasedDiffs.add(diff);
    			}
    			diff.addMatch(match);
    		}
    		if (subjects.isEmpty()) {
    			unmatchedSourceAxiomsWithNoSubject.add(axiom);
    		}
    	}
    	if (match.getTargetAxiom() != null) {
    		OWLAxiom axiom = match.getTargetAxiom();
    		Collection subjects = targetEntitiesDetector.getSources(axiom);
    		for (OWLEntity e : subjects) {
    			EntityBasedDiff diff = targetDiffMap.get(e);
    			if (diff == null) {
    				diff = new EntityBasedDiff();
    				diff.setSourceEntity(e);
    				diff.setTargetEntity(e);
    				sourceDiffMap.put(e, diff);
    				targetDiffMap.put(e, diff);
    				entityBasedDiffs.add(diff);
    			}
    			diff.addMatch(match);
    		}
    		if (subjects.isEmpty()) {
    			unmatchedTargetAxiomsWithNoSubject.add(axiom);
    		}
    	}
    }
    
    public void removeMatch(MatchedAxiom match) {
    	if (match.getSourceAxiom() != null) {
    		OWLAxiom axiom = match.getSourceAxiom();
    		Collection subjects = sourceEntitiesDetector.getSources(axiom);
    		for (OWLEntity e : subjects) {
    			EntityBasedDiff diff = sourceDiffMap.get(e);
    			if (diff != null) {
    				diff.removeMatch(match);
    			}
    		}
    		if (subjects.isEmpty()) {
    			unmatchedSourceAxiomsWithNoSubject.remove(axiom);
    		}
    	}
    	if (match.getTargetAxiom() != null) {
    		OWLAxiom axiom = match.getTargetAxiom();
    		Collection subjects = targetEntitiesDetector.getSources(axiom);
    		for (OWLEntity e : subjects) {
    			EntityBasedDiff diff = targetDiffMap.get(e);
    			if (diff != null) {
    				diff.removeMatch(match);
    			}
    		}
    		if (subjects.isEmpty()) {
    			unmatchedTargetAxiomsWithNoSubject.remove(axiom);
    		}
    	}
    }

    private void initialiseDiffs() {
        for (OWLEntity source : diffMap.getUnmatchedSourceEntities()) {
            EntityBasedDiff d = new EntityBasedDiff();
            d.setSourceEntity(source);
            sourceDiffMap.put(source, d);
            entityBasedDiffs.add(d);
        }
        for (Entry entry : diffMap.getEntityMap().entrySet()) {
            OWLEntity source = entry.getKey();
            OWLEntity target = entry.getValue();
            if (!source.equals(target)) {
            	EntityBasedDiff d = new EntityBasedDiff();
            	d.setSourceEntity(source);
            	d.setTargetEntity(target);
            	sourceDiffMap.put(source, d);
            	targetDiffMap.put(target, d);
            	entityBasedDiffs.add(d);
            }
        }
        for (OWLEntity target : diffMap.getUnmatchedTargetEntities()) {
            EntityBasedDiff d = new EntityBasedDiff();
            d.setTargetEntity(target);
            targetDiffMap.put(target, d);
            entityBasedDiffs.add(d);
        }
        for (OWLAxiom axiom : diffMap.getUnmatchedSourceAxioms()) {
        	addMatch(new MatchedAxiom(axiom, null, MatchedAxiom.AXIOM_DELETED));
        }
        for (OWLAxiom axiom : diffMap.getUnmatchedTargetAxioms()) {
        	addMatch(new MatchedAxiom(null, axiom, MatchedAxiom.AXIOM_ADDED));
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy