Example usage for org.hibernate.criterion Restrictions sqlRestriction

List of usage examples for org.hibernate.criterion Restrictions sqlRestriction

Introduction

In this page you can find the example usage for org.hibernate.criterion Restrictions sqlRestriction.

Prototype

public static Criterion sqlRestriction(String sql) 

Source Link

Document

Apply a constraint expressed in SQL with no JDBC parameters.

Usage

From source file:de.tudarmstadt.ukp.lmf.api.Uby.java

License:Apache License

/**
 * Consumes a synset offset (in WordNet terminology) and a part-of-speech. Returns a
 * {@link List} of all {@link Sense} instances which are derived from the WordNets synset,
 * identified by the consumed arguments.
 *
 * @param partOfSpeech/*www  . j a  v a  2 s.com*/
 *            a string describing part of speech of the senses to be returned.
 *            <p>
 *            Valid values are: <list>
 *            <li>"noun"</li>
 *            <li>"verb"</li>
 *            <li>"adverb"</li>
 *            <li>"adjective"</li> </list>
 *
 * @param offset
 *            string representation of the WordNets synset offset i.e. "14469014"
 *
 * @return senses derived from the WordNets synset, described by the consumed arguments
 *         <p>
 *         This method returns an empty list if the database accessed by this {@link Uby}
 *         instance does not contain the specified sense.
 *
 * @throws UbyInvalidArgumentException
 *             if the specified part of speech is not valid or one of consumed arguments is null
 *
 * @since 0.2.0
 */
@Deprecated
public List<Sense> wordNetSenses(String partOfSpeech, String offset) throws IllegalArgumentException {

    if (partOfSpeech == null) {
        throw new IllegalArgumentException("partOfSpeech is null");
    }

    if (offset == null) {
        throw new IllegalArgumentException("offset is null");
    }

    String refId = "[POS: noun] ";
    if (partOfSpeech.equals("adjective")) {
        refId = refId.replaceAll("noun", "adjective");
    } else if (partOfSpeech.equals("adverb")) {
        refId = refId.replaceAll("noun", "adverb");
    } else if (partOfSpeech.equals("verb")) {
        refId = refId.replaceAll("noun", "verb");
    } else if (!partOfSpeech.equals("noun")) {
        throw new IllegalArgumentException("\"" + partOfSpeech + "\""
                + " is not a valid part of speech. Only \"noun\", \"verb\", \"adverb\" or \"adjective\" are allowed");
    }

    refId = refId + offset;

    /*
     * This direct query avoids the joining huge table done by using normal hibernate, while we just need the ID
     */
    String sqlQueryString = "SELECT synsetId FROM MonolingualExternalRef WHERE externalReference = '"
            + refId.trim() + "'";
    SQLQuery query = session.createSQLQuery(sqlQueryString);
    String ss_id = (String) query.uniqueResult();
    if (ss_id == null) {
        return new ArrayList<Sense>(0);
    }

    Criteria criteria = session.createCriteria(Sense.class);
    criteria = criteria.add(Restrictions.sqlRestriction("synsetId='" + ss_id.trim() + "'"));
    @SuppressWarnings("unchecked")
    List<Sense> result = criteria.list();
    return result;
}

From source file:de.tudarmstadt.ukp.lmf.api.Uby.java

License:Apache License

/**
 * Consumes a sense key (in WordNet terminology) and a part-of-speech. Returns a {@link Sense}
 * instance which is derived from the WordNets sense, identified by the consumed arguments.
 *
 * @param partOfSpeech//from  w  w w  . j a  v a2 s . c  o m
 *            a string describing part of speech of the sense to be returned.
 *            <p>
 *            Valid values are: <list>
 *            <li>"noun"</li>
 *            <li>"verb"</li>
 *            <li>"adverb"</li>
 *            <li>"adjective"</li> </list>
 *
 * @param senseKey
 *            string representation of the WordNets identifier of a sense i.e. "enter%2:33:00::"
 *
 * @return UBY-LMF sense derived from the WordNets word, described by the consumed arguments
 *         <p>
 *         This method returns null if the database accessed by this {@link Uby} instance does
 *         not contain the specified sense.
 *
 * @throws UbyInvalidArgumentException
 *             if the specified part of speech is not valid or one of the consumed arguments is
 *             null
 *
 * @since 0.2.0
 */
@Deprecated
public Sense wordNetSense(String partOfSpeech, String senseKey) throws IllegalArgumentException {

    if (partOfSpeech == null) {
        throw new IllegalArgumentException("partOfSpeech is null");
    }

    if (senseKey == null) {
        throw new IllegalArgumentException("senseKey is null");
    }

    String refId = "[POS: noun] ";
    if (partOfSpeech.equals("adjective")) {
        refId = refId.replaceAll("noun", "adjective");
    } else if (partOfSpeech.equals("adverb")) {
        refId = refId.replaceAll("noun", "adverb");
    } else if (partOfSpeech.equals("verb")) {
        refId = refId.replaceAll("noun", "verb");
    } else if (!partOfSpeech.equals("noun")) {
        throw new IllegalArgumentException("\"" + partOfSpeech + "\""
                + " is not a valid part of speech. Only \"noun\", \"verb\", \"adverb\" or \"adjective\" are allowed");
    }

    refId = refId + senseKey;

    /*
     * This direct query avoids the joining huge table done by using normal hibernate, while we just need the ID
     */
    String sqlQueryString = "SELECT senseId FROM MonolingualExternalRef WHERE externalReference = '"
            + refId.trim() + "'";
    SQLQuery query = session.createSQLQuery(sqlQueryString);
    String ss_id = (String) query.uniqueResult();
    if (ss_id == null) {
        return null;
    }

    Criteria criteria = session.createCriteria(Sense.class);
    criteria = criteria.add(Restrictions.sqlRestriction("senseId='" + ss_id.trim() + "'"));

    Sense result = (Sense) criteria.uniqueResult();
    return result;
}

From source file:de.tudarmstadt.ukp.lmf.api.Uby.java

License:Apache License

/**
 * Consumes a unique identifier of a {@link Sense} instance and returns a {@link List} of all
 * {@link SemanticLabel} instances associated to the specified sense.
 *
 * @param senseId//w  w w. j  a va 2 s. c  om
 *            a unique identifier of the sense for which semantic labels should be returned
 * @return a list of all semantic labels of the specified sense or an empty list if a sense with
 *         such identifier does not exist or the sense does not have any associated semantic
 *         labels
 */
public List<SemanticLabel> getSemanticLabelsbySenseId(String senseId) {
    Criteria criteria = session.createCriteria(SemanticLabel.class);
    criteria = criteria.add(Restrictions.sqlRestriction("senseId='" + senseId + "'"));
    @SuppressWarnings("unchecked")
    List<SemanticLabel> result = criteria.list();
    return result;
}

From source file:de.tudarmstadt.ukp.lmf.api.Uby.java

License:Apache License

/**
 * Consumes a unique identifier of a {@link Sense} instance and returns a {@link List} of all
 * {@link SemanticLabel} instances associated to the specified sense. The returned semantic
 * labels are filtered by the specified type.
 *
 *
 * @param senseId/*  w w w  . j av  a  2 s . c  om*/
 *            a unique identifier of the sense for which semantic labels should be returned
 * @param type
 *            returned semantic labels must have this type
 * @return a list of all semantic labels of the specified sense filtered by the type or an empty
 *         list if the database accessed by this {@link Uby} instance does not contain any
 *         semantic labels matching the criteria
 */
public List<SemanticLabel> getSemanticLabelsbySenseIdbyType(String senseId, String type) {
    Criteria criteria = session.createCriteria(SemanticLabel.class);
    criteria = criteria.add(Restrictions.sqlRestriction("senseId='" + senseId + "' and type ='" + type + "'"));
    @SuppressWarnings("unchecked")
    List<SemanticLabel> result = criteria.list();
    return result;
}

From source file:de.tudarmstadt.ukp.lmf.api.Uby.java

License:Apache License

/**
 * Return the semantic predicate with the given Id
 *
 * @param predicateId/*from   ww  w. j  a  v a2s .  c  o m*/
 *            the id of the predicate
 * @return semantic predicate
 */
public SemanticPredicate getSemanticPredicateById(String predicateId) {
    Criteria criteria = session.createCriteria(SemanticPredicate.class);
    criteria = criteria.add(Restrictions.sqlRestriction("semanticPredicateId='" + predicateId + "'"));
    return (SemanticPredicate) criteria.uniqueResult();

}

From source file:de.tudarmstadt.ukp.lmf.api.Uby.java

License:Apache License

/**
 * Returns a {@link List} of all {@link SemanticPredicate} instances in the
 * database accessed by this {@link Uby} instance, optionally filtered by
 * {@link Lexicon}./*w ww.  j  a v  a  2s.  c  o m*/
 *
 * @param lexicon
 *            if not null, all returned semantic predicates will belong to
 *            the specified lexicon
 * @return list of semantic predicates which matches the criteria or an
 *         empty list if none of the semantic predicates matches the
 *         criteria
 */
public List<SemanticPredicate> getSemanticPredicates(Lexicon lexicon) {
    Criteria criteria = session.createCriteria(SemanticPredicate.class);
    if (lexicon != null) {
        String lexId = lexicon.getId();
        criteria = criteria.add(Restrictions.sqlRestriction("lexiconId='" + lexId + "'"));
    }
    @SuppressWarnings("unchecked")
    List<SemanticPredicate> result = criteria.list();
    return result;
}

From source file:de.tudarmstadt.ukp.lmf.api.Uby.java

License:Apache License

/**
 * Return an {@link Iterator} over {@link SemanticPredicate} instances,
 * optionally filtered by a {@link Lexicon}.
 *
 * @param lexicon//from  w  w  w .  ja v  a 2  s  .  c  o  m
 *            if not null, the iterator will only be for semantic predicates
 *            of the specified lexicon
 *
 * @return iterator over the semantic predicates in the specified lexicon.<br>
 *         If the specified lexicon is null, this method returns an iterator
 *         over all semantic predicates in the {@link LexicalResource},
 *         accessed by this {@link Uby} instance.
 */
public Iterator<SemanticPredicate> getSemanticPredicateIterator(Lexicon lexicon) {
    DetachedCriteria criteria = DetachedCriteria.forClass(SemanticPredicate.class);
    if (lexicon != null) {
        String lexId = lexicon.getId();
        criteria = criteria.add(Restrictions.sqlRestriction("lexiconId='" + lexId + "'"));
    }
    CriteriaIterator<SemanticPredicate> predicateIterator = new CriteriaIterator<SemanticPredicate>(criteria,
            sessionFactory, 500);
    return predicateIterator;
}

From source file:de.tudarmstadt.ukp.lmf.api.Uby.java

License:Apache License

/**
 * Returns the {@link SemanticArgument} instance with the specified unique identifier.
 *
 * @param argumentId/* w w  w . jav  a2 s.  com*/
 *            the unique identifier of the semantic argument to be returned
 *
 * @return semantic argument with the specified unique identifier, contained in the database
 *         accessed by this {@link Uby} instance.<br>
 *         If a semantic argument with the specified identifier does not exist, this method
 *         return null.
 */
public SemanticArgument getSemanticArgumentById(String argumentId) {
    Criteria criteria = session.createCriteria(SemanticArgument.class);
    criteria = criteria.add(Restrictions.sqlRestriction("semanticArgumentId='" + argumentId + "'"));
    return (SemanticArgument) criteria.uniqueResult();
}

From source file:de.tudarmstadt.ukp.lmf.api.Uby.java

License:Apache License

/**
 * Returns a {@link List} of all {@link SemanticPredicate} instances in the
 * database that are associated with a {@link Sense} with the given senseId
 * @param senseId//from  w  w w .  j  a  v a  2  s.c om
 *       UBY sense Id string
 * @return list of semantic predicates which matches the criteria or an empty list
 */
public List<SemanticPredicate> getSemanticPredicatesBySenseId(String senseId) {
    Criteria criteria = session.createCriteria(PredicativeRepresentation.class);
    criteria = criteria.add(Restrictions.sqlRestriction("senseId='" + senseId + "'"));
    @SuppressWarnings("unchecked")
    List<PredicativeRepresentation> representations = criteria.list();
    List<SemanticPredicate> result = new ArrayList<>();
    for (PredicativeRepresentation predicative : representations) {
        result.add(predicative.getPredicate());
    }
    return result;
}

From source file:de.tudarmstadt.ukp.lmf.api.Uby.java

License:Apache License

/**
 * Returns a {@link List} of all {@link Sense} instances in the
 * database that are associated with a {@link SemanticPredicate} with the given semanticPredicateId
 * @param semanticPredicateid/* w  w  w . j av a2  s  .  com*/
 *       UBY semanticPredicateId string
 * @return list of senses which matches the criteria or an empty list
 */
public List<Sense> getSensesBySemanticPredicateId(String semanticPredicateId) {
    Criteria criteria = session.createCriteria(PredicativeRepresentation.class);
    criteria = criteria.add(Restrictions.sqlRestriction("predicate='" + semanticPredicateId + "'"));
    @SuppressWarnings("unchecked")
    List<PredicativeRepresentation> representations = criteria.list();
    List<Sense> result = new ArrayList<>();
    for (PredicativeRepresentation predicative : representations) {
        result.add(predicative.getSense());
    }
    return result;
}