Class Rule

Class Documentation

class Rule

Represents a rule used in a rule-based system.

This class encapsulates the attributes and methods needed to handle rules which consist of antecedents and the conditions associated with them.

Public Functions

Rule() = default
Rule(const std::vector<Antecedent> &antecedents, const std::vector<int> &coveredSamples, int out_class, double fidelity, double accuracy, double confidence)

Constructs a Rule object.

Parameters:
  • antecedents – Vector of antecedents to insert inside a rule.

  • coveredSamples – Vector of integers containing the covered samples IDs.

  • outClass – Integer indicating which class is targetted by the rule.

  • fidelity – Double indicating the fidelity of the rule.

  • accuracy – Double indicating the accuracy of the rule.

  • confidence – Double indicating the confidence of the rule.

inline void setOutputClass(int value)

Sets the output class of the rule.

Parameters:

value – The new output class.

inline void setCoveringSize(int value)

Sets the covering size of the rule.

Parameters:

value – The new covering size.

inline void setFidelity(double value)

Sets the fidelity of the rule.

Parameters:

value – The new fidelity value.

inline void setAccuracy(double value)

Sets the accuracy of the rule.

Parameters:

value – The new accuracy value.

inline void setConfidence(double value)

Sets the confidence of the rule.

Parameters:

value – The new confidence value.

inline void setAntecedents(const std::vector<Antecedent> &values)

Sets the antecedents of the rule.

Parameters:

values – The new antecedents.

inline void setCoveredSamples(const std::vector<int> &values)

Sets the covered samples of the rule and updates the covering size.

Parameters:

values – The new covered samples.

inline std::vector<Antecedent> getAntecedents() const

Gets the antecedents of the rule.

Returns:

The antecedents of the rule.

inline int getNbAntecedents() const

Gets the number of antecedents in the rule.

Returns:

The number of antecedents.

inline std::vector<int> getCoveredSamples() const

Gets the covered samples of the rule.

Returns:

The covered samples.

inline int getCoveringSize() const

Gets the covering size of the rule.

Returns:

The covering size.

inline int getOutputClass() const

Gets the output class of the rule.

Returns:

The output class.

inline double getFidelity() const

Gets the fidelity of the rule.

Returns:

The fidelity.

inline double getAccuracy() const

Gets the accuracy of the rule.

Returns:

The accuracy.

inline double getConfidence() const

Gets the confidence of the rule.

Returns:

The confidence.

inline void addAntecedent(Antecedent value)

Adds an antecedent to the rule.

Parameters:

value – The antecedent to add.

inline void addCoveredSample(int value)

Adds a covered sample to the rule and updates the covering size.

Parameters:

value – The sample to add.

std::string toString(const std::vector<std::string> &attributes, const std::vector<std::string> &classes) const

Builds a string presenting every element and value contained by a given rule.

Parameters:
  • attributes – Optional vector of strings containing all attributes names, useful to print attribute names instead of integers.

  • classes – Optional vector of strings containing all class names, useful to print class names instead of an integer.

Returns:

A string representation of the rule.

bool isEqual(const Rule &other) const

Compares a rule with another to determine whether they’re identical.

Parameters:

other – Other rule for comparison.

Returns:

Returns true if they’re identical, false otherwise.

Public Static Functions

static std::vector<Rule> fromJsonFile(const std::string &filename, float &decisionThreshold, int &positiveClassIndex)

Parses a JSON file and returns a vector of the parsed rules.

JSON rule format must be like this example: { “rules”: [ { “antecedents”: [ { “attribute”: 0, “inequality”: true, “value”: 0.7231 }, { “attribute”: 3, “inequality”: false, “value”: 0.0031 }, … ], “coveredSamples”: [1,2,3, …], “outputClass”: 0, “fidelity”: 1, “accuracy”: 0.63, “confidence”: 0.8 }, {…} ] }

Parameters:
  • filename – Path of the JSON file to be parsed.

  • decisionThreshold – Reference to a float where the decision threshold will be stored.

  • positiveClassIndex – Reference to an int where the positive class index will be stored.

Returns:

A vector of parsed rules.

static void toJsonFile(const std::string &filename, const std::vector<Rule> &rules, float threshold, int positiveIndex)

Writes a vector of rules into a JSON file.

Parameters:
  • filename – Name of the file to be written.

  • rules – Vector of rules to be written.

  • threshold – Decision threshold.

  • positiveIndex – Positive class index.