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

org.coode.parsers.EntityFinderImpl Maven / Gradle / Ivy

package org.coode.parsers;

import static org.coode.oppl.utils.ArgCheck.checkNotNull;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLDataProperty;
import org.semanticweb.owlapi.model.OWLDatatype;
import org.semanticweb.owlapi.model.OWLEntity;
import org.semanticweb.owlapi.model.OWLIndividual;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLObjectProperty;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyManager;

/** Author: Matthew Horridge
* The University Of Manchester
* Medical Informatics Group
* Date: 16-May-2006
*
*
* [email protected]
* www.cs.man.ac.uk/~horridgm
*
*/ public class EntityFinderImpl implements EntityFinder { private final OWLEntityRenderingCache renderingCache; private final OWLOntologyManager manager; private final boolean useRegularExpressions; private static final String WILDCARD = "*"; /** @param mngr * mngr * @param renderingCache * renderingCache * @param useRegularExpressions * useRegularExpressions */ public EntityFinderImpl(OWLOntologyManager mngr, OWLEntityRenderingCache renderingCache, boolean useRegularExpressions) { this.renderingCache = checkNotNull(renderingCache, "renderingCache"); manager = checkNotNull(mngr, "mngr"); this.useRegularExpressions = useRegularExpressions; } @Override public Set getMatchingOWLClasses(String match) { return this.getEntities(match, OWLClass.class, useRegularExpressions); } @Override public Set getMatchingOWLClasses(String match, boolean fullRegExp) { return this.getEntities(match, OWLClass.class, fullRegExp); } @Override public Set getMatchingOWLObjectProperties(String match) { return this.getEntities(match, OWLObjectProperty.class, useRegularExpressions); } @Override public Set getMatchingOWLObjectProperties(String match, boolean fullRegExp) { return this.getEntities(match, OWLObjectProperty.class, fullRegExp); } @Override public Set getMatchingOWLDataProperties(String match) { return this.getEntities(match, OWLDataProperty.class, useRegularExpressions); } @Override public Set getMatchingOWLDataProperties(String match, boolean fullRegExp) { return this.getEntities(match, OWLDataProperty.class, fullRegExp); } @Override public Set getMatchingOWLIndividuals(String match) { return this.getEntities(match, OWLNamedIndividual.class, useRegularExpressions); } @Override public Set getMatchingOWLIndividuals(String match, boolean fullRegExp) { return this.getEntities(match, OWLNamedIndividual.class, fullRegExp); } @Override public Set getMatchingOWLDataTypes(String match) { return this.getEntities(match, OWLDatatype.class, useRegularExpressions); } @Override public Set getMatchingOWLDataTypes(String match, boolean fullRegExp) { return this.getEntities(match, OWLDatatype.class, fullRegExp); } @Override public Set getEntities(String match) { return this.getEntities(match, OWLEntity.class, useRegularExpressions); } @Override public Set getEntities(String match, boolean fullRegExp) { return this.getEntities(match, OWLEntity.class, fullRegExp); } private Set getEntities(String match, Class type, boolean fullRegExp) { if (match.length() == 0) { return Collections.emptySet(); } if (fullRegExp) { return this.doRegExpSearch(match, type); } else { return this.doWildcardSearch(match, type); } } private Set doRegExpSearch(String match, Class type) { Set results = new HashSet(); try { Pattern pattern = Pattern.compile(match); for (String rendering : this.getRenderings(type)) { Matcher m = pattern.matcher(rendering); if (m.find()) { T ent = this.getEntity(rendering, type); if (ent != null) { results.add(ent); } } } } catch (Exception e) { e.printStackTrace(); } return results; } /* * @@TODO fix wildcard searching - it does not handle the usecases correctly * eg A*B will not work, and endsWith is implemented the same as contains * (probably right but this should not be implemented separately) */ private Set doWildcardSearch(String _match, Class type) { String match = _match; Set results = new HashSet(); if (match.equals(WILDCARD)) { results = this.getAllEntities(type); } else { SimpleWildCardMatcher matcher; if (match.startsWith(WILDCARD)) { if (match.length() > 1 && match.endsWith(WILDCARD)) { // Contains matcher = new SimpleWildCardMatcher() { @Override public boolean matches(String rendering, String s) { return rendering.indexOf(s) != -1; } }; match = match.substring(1, match.length() - 1); } else { // Ends with matcher = new SimpleWildCardMatcher() { @Override public boolean matches(String rendering, String s) { return rendering.indexOf(s) != -1; } }; match = match.substring(1, match.length()); } } else { // Starts with if (match.endsWith(WILDCARD) && match.length() > 1) { match = match.substring(0, match.length() - 1); } // @@TODO handle matches exactly? matcher = new SimpleWildCardMatcher() { @Override public boolean matches(String rendering, String s) { return rendering.startsWith(s) || rendering.startsWith("'" + s); } }; } if (match.trim().length() != 0) { match = match.toLowerCase(); for (String rendering : this.getRenderings(type)) { if (rendering.length() > 0 && matcher.matches(rendering.toLowerCase(), match)) { results.add(this.getEntity(rendering, type)); } } } } return results; } @SuppressWarnings("unchecked") private Set getAllEntities(Class type) { Set entities = new HashSet(); for (OWLOntology ont : manager.getOntologies()) { if (type.equals(OWLClass.class)) { entities.addAll((Set) ont.getClassesInSignature()); } else if (type.equals(OWLObjectProperty.class)) { entities.addAll((Set) ont.getObjectPropertiesInSignature()); } else if (type.equals(OWLDataProperty.class)) { entities.addAll((Set) ont.getDataPropertiesInSignature()); } else if (type.equals(OWLIndividual.class)) { entities.addAll((Set) ont.getIndividualsInSignature()); } else if (type.equals(OWLDatatype.class)) { entities.addAll((Set) ont.getDatatypesInSignature()); } } return entities; } @SuppressWarnings("unchecked") private T getEntity(String rendering, Class type) { if (type.equals(OWLClass.class)) { return (T) renderingCache.getOWLClass(rendering); } else if (type.equals(OWLObjectProperty.class)) { return (T) renderingCache.getOWLObjectProperty(rendering); } else if (type.equals(OWLDataProperty.class)) { return (T) renderingCache.getOWLDataProperty(rendering); } else if (type.equals(OWLIndividual.class)) { return (T) renderingCache.getOWLIndividual(rendering); } else if (type.equals(OWLDatatype.class)) { return (T) renderingCache.getOWLDataType(rendering); } else { return (T) renderingCache.getOWLEntity(rendering); } } private Set getRenderings(Class type) { if (type.equals(OWLClass.class)) { return renderingCache.getOWLClassRenderings(); } else if (type.equals(OWLObjectProperty.class)) { return renderingCache.getOWLObjectPropertyRenderings(); } else if (type.equals(OWLDataProperty.class)) { return renderingCache.getOWLDataPropertyRenderings(); } else if (type.equals(OWLIndividual.class)) { return renderingCache.getOWLIndividualRenderings(); } else if (type.equals(OWLDatatype.class)) { return renderingCache.getOWLDatatypeRenderings(); } else { return renderingCache.getOWLEntityRenderings(); } } private interface SimpleWildCardMatcher { boolean matches(String rendering, String s); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy