Struct stam::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"),
                           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 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 subquery(&self) -> Option<&Query<'a>>

REturns the subquery (if any)

source

pub fn last_subquery(&self) -> &Query<'a>

Returns the last subquery (if any), otherwise returns this query itself

source

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

Iterates over all constraints 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<'a, 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<'a, 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<'a, 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<'a, 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<'a, 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<'a, DataKey> )

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

source

pub fn with_textvar( self, name: impl Into<String>, textselection: ResultTextSelection<'a> ) -> 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<'a> )

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<'a, 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<'a, 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<'a, 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<'a, 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 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 copy of the value. Read more
1.0.0 · 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>

§

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> RefUnwindSafe for Query<'a>

§

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

§

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

§

impl<'a> Unpin 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<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.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

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

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

§

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>,

§

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>,

§

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.
§

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

§

fn vzip(self) -> V