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

it.tidalwave.openrdf.elmo.ElmoOwlUtils Maven / Gradle / Ivy

The newest version!
/***********************************************************************************************************************
 *
 * blueMarine Semantic - open source media workflow
 * Copyright (C) 2008-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
 *
 ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ***********************************************************************************************************************
 *
 * WWW: http://bluemarine.tidalwave.it
 * SCM: https://kenai.com/hg/bluemarine~semantic-src
 *
 **********************************************************************************************************************/
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.tidalwave.openrdf.elmo;

import it.tidalwave.util.logging.Logger;
import java.util.List;
import javax.annotation.Nonnull;
import org.openide.util.Lookup;
import org.openrdf.concepts.owl.Thing;
import org.openrdf.elmo.ElmoQuery;
import org.openrdf.elmo.Entity;
import org.openrdf.elmo.sesame.SesameManager;
import org.openrdf.repository.contextaware.ContextAwareConnection;

/**
 *
 * @author fritz
 */
public final class ElmoOwlUtils
  {
    private static final String CLASS = ElmoOwlUtils.class.getName();
    private static final Logger logger = Logger.getLogger(CLASS);

    private ElmoOwlUtils()
      {
      }

    private static final String QUERY ="PREFIX owl: \n" +
                                       "SELECT ?entity WHERE \n" +
                                       "  {\n" +
                                       "    ?entity a ?type.\n" +
                                       "    { ?id owl:sameAs ?entity } UNION { ?entity owl:sameAs ?id }\n" +
                                       "  }";

    /***************************************************************************
     *
     * @deprecated  Use ElmoOWLThing instead
     *
     **************************************************************************/
    public static  T findSame (@Nonnull final Thing object,
                                  @Nonnull final Class clazz)
      {
        logger.fine("findSame(%s (%s), %s)", object, ((Entity)object).getQName(), clazz);

        final ClassLoader saveClassLoader = Thread.currentThread().getContextClassLoader(); // Don't know if needed
        Thread.currentThread().setContextClassLoader(Lookup.getDefault().lookup(ClassLoader.class));
        final SesameManager em = getSesameManager();
        final ContextAwareConnection connection = em.getConnection();
        final boolean includeInferredSave = connection.isIncludeInferred();

        try
          {
            connection.setIncludeInferred(true);
            final ElmoQuery query = em.createQuery(QUERY).setType("type", clazz).setQName("id", ((Entity)object).getQName());
            final List resultList = query.getResultList();
            final T result = resultList.isEmpty() ? null : (T) resultList.get(0);
            logger.finest(">>>> returning %s", result);
            return result;
          }
//        catch (MalformedQueryException e)
//          {
//            throw new RuntimeException(e);
//          }
//        catch (RepositoryException e)
//          {
//            throw new RuntimeException(e);
//          }
        finally
          {
            connection.setIncludeInferred(includeInferredSave);
            Thread.currentThread().setContextClassLoader(saveClassLoader);
          }
      }

    private static SesameManager getSesameManager()
      {
        return (SesameManager)ElmoManagerThreadLocal.get();
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy