Skip to main content

Query

Struct Query 

Source
pub struct Query<'a> { /* private fields */ }
Expand description

This represents a query that can be performed on an AnnotationStore via AnnotationStore::query() to obtain anything in the store. A query can be formulated in STAMQL, a dedicated query language (via Query::parse(), or it can be instantiated programmatically via Query::new().

A query consists of a query type (QueryType), a result type (subset of Type), a variable to bind to (optional), and zero or more constraints (Constraint), and optionally a subquery.

§Examples

select all occurrences of the text “fly”

let query = Query::parse("SELECT TEXT WHERE
                                 TEXT \"fly\";")?;

the same query as above but constructed directly instead of via STAMQL, this is always more performant as it bypasses the parsing stage. It does not affect the runtime performance of the query evaluation itself though:

let query = Query::new(QueryType::Select, Some(Type::TextSelection), None)
                  .with_constraint(Constraint::Text("fly", TextMode::Exact));

select all annotations that targets the text “fly”

let query = Query::parse("SELECT ANNOTATION WHERE
                                 TEXT \"fly\";")?;

select all annotations with data ‘part-of-speech’ and value ‘noun’ (ad-hoc vocab!), bind the result to a variable

let query = Query::parse("SELECT ANNOTATION ?noun WHERE
                                 DATA \"myset\" \"part-of-speech\" = \"noun\";")?;

the same query as above but constructed programmatically:

let query = Query::new(QueryType::Select, Some(Type::Annotation), Some("noun"))
                  .with_constraint(Constraint::KeyValue {
                           set: "myset",
                           key: "fly",
                           operator: DataOperator::Equals("noun".into()),
                           qualifier: SelectionQualifier::Normal
                  });

select all annotations that have a part-of-speech annotation (regardless of the value)

let query = Query::parse("SELECT ANNOTATION ?pos WHERE
                                 DATA \"myset\" \"part-of-speech\";")?;

select all annotations with data ‘part-of-speech’ made by a certain annotator (ad-hoc vocab!)

let query = Query::parse("SELECT ANNOTATION WHERE
                             DATA \"myset\" \"part-of-speech\" = \"noun\";
                             DATA \"myset\" \"annotator\" = \"John Doe\";")?;

select sentences with a particular annotated text in it, as formulated via a subquery

let query = Query::parse("SELECT TEXT ?sentence WHERE
                                 DATA \"myset\" \"type\" = \"sentence\";
                                   {
                                    SELECT TEXT ?fly WHERE
                                        RELATION ?sentence EMBEDS;
                                        DATA \"myset\" \"part-of-speech\" = \"noun\";
                                        TEXT \"fly\";
                                   }")?;

Implementations§

Source§

impl<'a> Query<'a>

Source

pub fn new( querytype: QueryType, resulttype: Option<Type>, name: Option<&'a str>, ) -> Self

Instantiate a new query. See the top-level documentation of Query for examples.

Source

pub fn select_by_path(&self, querypath: QueryPathRef<'_>) -> Option<&Query<'a>>

Selects this query or a specific subquery anywhere in its tree based on a given query path. A querypath is a list of indices selecting subqueries, e.g. [0,0,1] for main query, first subquery, then second subquery). The first item (main query) is always 0 Return None when the path does not resolve.

Source

pub fn with_qualifier(self, qualifier: QueryQualifier) -> Self

Set a qualifier for the query

Source

pub fn with_constraint(self, constraint: Constraint<'a>) -> Self

Add a constraint to the query

Source

pub fn constrain(&mut self, constraint: Constraint<'a>) -> &mut Self

Add a constraint to the query

Source

pub fn with_subquery(self, query: Query<'a>) -> Self

Set the subquery for this query

Source

pub fn with_name(self, name: &'a str) -> Self

Source

pub fn has_subqueries(&self) -> bool

Does this query have subqueries?

Source

pub fn subqueries_len(&self) -> usize

Returns the number of subqueries

Source

pub fn subqueries(&self) -> impl Iterator<Item = &Query<'a>>

Returns an iterator over the direct subqueries (i.e. non-recursively) Use queries() for recursion.

Source

pub fn qualifier(&self) -> QueryQualifier

Return the qualifier for the query

Source

pub fn querypaths(&self) -> Vec<QueryPath>

Return all querypaths (relative to the current query as root!), including the one referencing self This should only be run on a root query

Source

pub fn queries(&self) -> impl Iterator<Item = (QueryPath, &Query<'a>)>

Returns an iterator over self and all subqueries (i.e. recursively) Note: this always returns self as first element

Source

pub fn names(&self) -> Vec<&'a str>

Returns all variable names occur that occur in this query, including in all possible subqueries.

Source

pub fn constraints<'s>(&'s self) -> Iter<'s, Constraint<'a>>

Iterates over all constraints in the Query

Source

pub fn attributes<'s>(&'s self) -> Iter<'s, &'a str>

Returns all attributes for this query

Source

pub fn constraints_with_attributes( &self, ) -> impl Iterator<Item = (&Constraint<'a>, &Vec<&'a str>)>

Iterates over all constraints and their attributes in the Query

Source

pub fn iter<'s>(&'s self) -> Iter<'s, Constraint<'a>>

Iterates over all constraints in the Query Alias for constraints(),

Source

pub fn assignments<'s>(&'s self) -> Iter<'s, Assignment<'a>>

Iterates over all assignments in the Query

Source

pub fn name(&self) -> Option<&'a str>

Returns the variable name of the Query, the ? prefix STAMQL uses is never included.

Source

pub fn querytype(&self) -> QueryType

Returns the type of the query

Source

pub fn resulttype(&self) -> Option<Type>

Returns the type of the results that this query produces

Source

pub fn resulttype_as_str(&self) -> Option<&'static str>

Returns the type of the results that this query produces, as a STAMQL keyword.

Source

pub fn parse(querystring: &'a str) -> Result<(Self, &'a str), StamError>

Parses a query formulated in STAMQL. Returns the Query if successful, it can subsequently by passed to [AnnotationStore.query()] or a StamError::QuerySyntaxError if the query is not valid. See the documentation on Query itself for examples.

Source

pub fn with_annotationvar( self, name: impl Into<String>, annotation: &ResultItem<'_, Annotation>, ) -> Self

Bind a variable, the name should not include the ? prefix STAMQL uses. This is a context variable that will be available to the query, but will not be propagated to the results.

Source

pub fn bind_annotationvar( &mut self, name: impl Into<String>, annotation: &ResultItem<'_, Annotation>, )

Bind a variable, the name should not include the ? prefix STAMQL uses. This is a context variable that will be available to the query, but will not be propagated to the results.

Source

pub fn with_datavar( self, name: impl Into<String>, data: &ResultItem<'_, AnnotationData>, ) -> Self

Bind a variable, the name should not include the ? prefix STAMQL uses. This is a context variable that will be available to the query, but will not be propagated to the results.

Source

pub fn bind_datavar( &mut self, name: impl Into<String>, data: &ResultItem<'_, AnnotationData>, )

Bind a variable, the name should not include the ? prefix STAMQL uses. This is a context variable that will be available to the query, but will not be propagated to the results.

Source

pub fn with_keyvar( self, name: impl Into<String>, key: &ResultItem<'_, DataKey>, ) -> Self

Bind a variable, the name should not include the ? prefix STAMQL uses. This is a context variable that will be available to the query, but will not be propagated to the results.

Source

pub fn bind_keyvar( &mut self, name: impl Into<String>, key: &ResultItem<'_, DataKey>, )

Bind a variable, the name should not include the ? prefix STAMQL uses.

Source

pub fn with_substorevar( self, name: impl Into<String>, substore: &ResultItem<'_, AnnotationSubStore>, ) -> Self

Bind a variable, the name should not include the ? prefix STAMQL uses. This is a context variable that will be available to the query, but will not be propagated to the results.

Source

pub fn bind_substorevar( &mut self, name: impl Into<String>, substore: &ResultItem<'_, AnnotationSubStore>, )

Bind a variable, the name should not include the ? prefix STAMQL uses.

Source

pub fn with_textvar( self, name: impl Into<String>, textselection: &ResultTextSelection<'_>, ) -> Self

Bind a variable, the name should not include the ? prefix STAMQL uses. This is a context variable that will be available to the query, but will not be propagated to the results.

Source

pub fn bind_textvar( &mut self, name: impl Into<String>, textselection: &ResultTextSelection<'_>, )

Bind a variable, the name should not include the ? prefix STAMQL uses. This is a context variable that will be available to the query, but will not be propagated to the results.

Source

pub fn with_resourcevar( self, name: impl Into<String>, resource: &ResultItem<'_, TextResource>, ) -> Self

Bind a variable, the name should not include the ? prefix STAMQL uses. This is a context variable that will be available to the query, but will not be propagated to the results.

Source

pub fn bind_resourcevar( &mut self, name: impl Into<String>, resource: &ResultItem<'_, TextResource>, )

Bind a variable, the name should not include the ? prefix STAMQL uses. This is a context variable that will be available to the query, but will not be propagated to the results.

Source

pub fn with_datasetvar( self, name: impl Into<String>, dataset: &ResultItem<'_, AnnotationDataSet>, ) -> Self

Bind a variable, the name should not include the ? prefix STAMQL uses. This is a context variable that will be available to the query, but will not be propagated to the results.

Source

pub fn bind_datasetvar( &mut self, name: impl Into<String>, dataset: &ResultItem<'_, AnnotationDataSet>, )

Bind a variable, the name should not include the ? prefix STAMQL uses. This is a context variable that will be available to the query, but will not be propagated to the results.

Source

pub fn bind_from_result( &mut self, varname: impl Into<String>, resultitem: &QueryResultItem<'_>, )

Bind any variable from a QueryResultItem as a context variable in this query

Source

pub fn to_string(&self) -> Result<String, StamError>

Serialize the query to a STAMQL String

Trait Implementations§

Source§

impl<'a> Clone for Query<'a>

Source§

fn clone(&self) -> Query<'a>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Query<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> TryFrom<&'a str> for Query<'a>

Source§

type Error = StamError

The type returned in the event of a conversion error.
Source§

fn try_from(querystring: &'a str) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl<'a> Freeze for Query<'a>

§

impl<'a> RefUnwindSafe for Query<'a>

§

impl<'a> Send for Query<'a>

§

impl<'a> Sync for Query<'a>

§

impl<'a> Unpin for Query<'a>

§

impl<'a> UnsafeUnpin for Query<'a>

§

impl<'a> UnwindSafe for Query<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V