Solid::Predicate Class

class Solid::Predicate

This class implements predicates for devices. More...

Header: #include <Solid/Predicate>
CMake: find_package(KF6 REQUIRED COMPONENTS Solid)
target_link_libraries(mytarget PRIVATE KF6::Solid)

Public Types

enum ComparisonOperator { Equals, Mask }
enum Type { PropertyCheck, Conjunction, Disjunction, InterfaceCheck }

Public Functions

Predicate()
Predicate(const QString &ifaceName)
Predicate(const Solid::DeviceInterface::Type &ifaceType)
Predicate(const QString &ifaceName, const QString &property, const QVariant &value, Solid::Predicate::ComparisonOperator compOperator = Equals)
Predicate(const Solid::DeviceInterface::Type &ifaceType, const QString &property, const QVariant &value, Solid::Predicate::ComparisonOperator compOperator = Equals)
(since 4.4) Solid::Predicate::ComparisonOperator comparisonOperator() const
(since 4.4) Solid::Predicate firstOperand() const
(since 4.4) Solid::DeviceInterface::Type interfaceType() const
bool isValid() const
bool matches(const Solid::Device &device) const
(since 4.4) QVariant matchingValue() const
(since 4.4) QString propertyName() const
(since 4.4) Solid::Predicate secondOperand() const
QString toString() const
(since 4.4) Solid::Predicate::Type type() const
QSet<Solid::DeviceInterface::Type> usedTypes() const
Solid::Predicate operator&(const Solid::Predicate &other)
Solid::Predicate &operator&=(const Solid::Predicate &other)
Solid::Predicate operator|(const Solid::Predicate &other)
Solid::Predicate &operator|=(const Solid::Predicate &other)

Static Public Members

Solid::Predicate fromString(const QString &predicate)

Detailed Description

A predicate is a logical condition that a given device can match or not. It's a constraint about the value a property must have in a given device interface, or any combination (conjunction, disjunction) of such constraints.

A predicate can be:

  • a single comparison, or
  • a conjunction ("AND") of exactly two predicates, or
  • a disjunction ("OR") of exactly two predicates.

Since these can be nested, it is possible to express "a StorageVolume that is not ignored AND that StorageVolume contains a FileSystem AND that StorageVolume is removable". Since conjunctions use exactly two predicates (and this expression has three), square brackets are used to group the nested predicates when writing them out in full:

[ [ StorageVolume.ignored == false AND StorageVolume.usage == 'FileSystem' ]
  AND StorageVolume.removable == true ]

Predicates can be constructed programmatically by creating single comparisons with the Predicate constructor, and then building conjunctions with operator& and disjunctions with operator|.

Predicates can be constructed from a string by calling fromString() which parses the given string and returns a predicate. If there are any errors in parsing the string, an empty predicate is returned; use isValid() to detect whether that is the case.

The string language is described exactly in predicate_parser.y, but boils down to:

  • a single comparison is written as <interface>.<property> == <value>
  • a single bitmask check is written as <interface>.<property> & <value>
  • a conjunction is written as [ <predicate> AND <predicate> ]
  • a disjunction is written as [ <predicate> OR <predicate> ]

Note the mandatory use of [ and ] around conjunctions and disjunctions.

Member Type Documentation

enum Predicate::ComparisonOperator

The comparison operator which can be used for matching within the predicate.

ConstantValueDescription
Solid::Predicate::Equals0the property and the value will match for strict equality
Solid::Predicate::Mask1the property and the value will match if the bitmasking is not null

enum Predicate::Type

The predicate type which controls how the predicate is handled

ConstantValueDescription
Solid::Predicate::PropertyCheck0the predicate contains a comparison that needs to be matched using a ComparisonOperator
Solid::Predicate::Conjunction1the two contained predicates need to be true for this predicate to be true
Solid::Predicate::Disjunction2either of the two contained predicates may be true for this predicate to be true
Solid::Predicate::InterfaceCheck3the device type is compared

Member Function Documentation

Predicate::Predicate()

Constructs an invalid predicate.

[explicit] Predicate::Predicate(const QString &ifaceName)

Constructs a predicate matching devices being of a particular device interface

ifaceName the name of the device interface the device must have

[explicit] Predicate::Predicate(const Solid::DeviceInterface::Type &ifaceType)

Constructs a predicate matching devices being of a particular device interface

ifaceType the device interface the device must have

Predicate::Predicate(const QString &ifaceName, const QString &property, const QVariant &value, Solid::Predicate::ComparisonOperator compOperator = Equals)

Constructs a predicate matching the value of a property in a given device interface.

ifaceName the name of the device interface the device must have

property the property name of the device interface

value the value the property must have to make the device match

compOperator the operator to apply between the property and the value when matching

Predicate::Predicate(const Solid::DeviceInterface::Type &ifaceType, const QString &property, const QVariant &value, Solid::Predicate::ComparisonOperator compOperator = Equals)

Constructs a predicate matching the value of a property in a given device interface.

ifaceType the device interface type the device must have

property the property name of the device interface

value the value the property must have to make the device match

compOperator the operator to apply between the property and the value when matching

[since 4.4] Solid::Predicate::ComparisonOperator Predicate::comparisonOperator() const

Retrieves the comparison operator used to compare a property's value

Note: This is only valid for Conjunction and Disjunction types Returns the comparison operator used

This function was introduced in 4.4.

[since 4.4] Solid::Predicate Predicate::firstOperand() const

A smaller, inner predicate which is the first to appear and is compared with the second one

Note: This is only valid for Conjunction and Disjunction types Returns The predicate used for the first operand

This function was introduced in 4.4.

[static] Solid::Predicate Predicate::fromString(const QString &predicate)

Converts a string to a predicate.

predicate the string to convert

Returns a new valid predicate if the given string is syntactically correct, Predicate() otherwise

[since 4.4] Solid::DeviceInterface::Type Predicate::interfaceType() const

Retrieves the interface type.

Note: This is only valid for InterfaceCheck and PropertyCheck types Returns a device interface type used by the predicate

This function was introduced in 4.4.

bool Predicate::isValid() const

Indicates if the predicate is valid.

Predicate() is the only invalid predicate.

Returns true if the predicate is valid, false otherwise

bool Predicate::matches(const Solid::Device &device) const

Checks if a device matches the predicate.

device the device to match against the predicate

Returns true if the given device matches the predicate, false otherwise

[since 4.4] QVariant Predicate::matchingValue() const

Retrieves the value used when comparing a devices property to see if it matches the predicate

Note: This is only valid for the PropertyCheck type Returns the value used

This function was introduced in 4.4.

[since 4.4] QString Predicate::propertyName() const

Retrieves the property name used when retrieving the value to compare against

Note: This is only valid for the PropertyCheck type Returns a property name

This function was introduced in 4.4.

[since 4.4] Solid::Predicate Predicate::secondOperand() const

A smaller, inner predicate which is the second to appear and is compared with the first one

Note: This is only valid for Conjunction and Disjunction types Returns The predicate used for the second operand

This function was introduced in 4.4.

QString Predicate::toString() const

Converts the predicate to its string form.

Returns a string representation of the predicate

[since 4.4] Solid::Predicate::Type Predicate::type() const

Retrieves the predicate type, used to determine how to handle the predicate

Returns the predicate type

This function was introduced in 4.4.

QSet<Solid::DeviceInterface::Type> Predicate::usedTypes() const

Retrieves the device interface types used in this predicate.

Returns all the device interface types used in this predicate

Solid::Predicate Predicate::operator&(const Solid::Predicate &other)

'And' operator.

other the second operand

Returns a new 'and' predicate having 'this' and 'other' as operands

Solid::Predicate &Predicate::operator&=(const Solid::Predicate &other)

'AndEquals' operator.

other the second operand

Assigns to 'this' a new 'and' predicate having 'this' and 'other' as operands

Solid::Predicate Predicate::operator|(const Solid::Predicate &other)

'Or' operator.

other the second operand

Returns a new 'or' predicate having 'this' and 'other' as operands

Solid::Predicate &Predicate::operator|=(const Solid::Predicate &other)

'OrEquals' operator.

other the second operand

Assigns to 'this' a new 'or' predicate having 'this' and 'other' as operands