org.coode.oppl.similarity.SymbolBasedHashFunction Maven / Gradle / Ivy
package org.coode.oppl.similarity;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.coode.oppl.utils.OWLObjectExtractor;
import org.coode.oppl.utils.PrimeNumbersUtils;
import org.semanticweb.owlapi.model.OWLEntity;
import org.semanticweb.owlapi.model.OWLLiteral;
import org.semanticweb.owlapi.model.OWLObject;
/** @author Luigi Iannone */
public class SymbolBasedHashFunction implements HashFunction {
private final Map entityHashCodes = new HashMap();
private final Map owlConstantHashCodes = new HashMap();
private int maxPrime = 1;
@Override
public int getHashCode(OWLObject owlObject) {
Set entities = OWLObjectExtractor.getAllOWLEntities(owlObject);
int toReturn = 1;
for (OWLEntity owlEntity : entities) {
toReturn = toReturn * this.createHashCode(owlEntity);
}
Set allOWLConstants = OWLObjectExtractor.getAllOWLLiterals(owlObject);
for (OWLLiteral owlConstant : allOWLConstants) {
toReturn = toReturn * this.createHashCode(owlConstant);
}
return toReturn;
}
protected int createHashCode(OWLLiteral constant) {
return this.createHashCode(constant, owlConstantHashCodes);
}
protected int createHashCode(OWLEntity owlEntity) {
return this.createHashCode(owlEntity, entityHashCodes);
}
private int createHashCode(O key, Map map) {
int toReturn = PrimeNumbersUtils.getNextPrime(getMaxPrime());
map.put(key, toReturn);
setMaxPrime(toReturn);
return toReturn;
}
/** @return the maxPrime */
public int getMaxPrime() {
return maxPrime;
}
/** @param maxPrime
* the maxPrime to set */
private void setMaxPrime(int maxPrime) {
this.maxPrime = maxPrime;
}
}