[rygel] tracker: New classes to handle SPARQL query filters



commit 3d454754a3652f61d1ce71abe31443dedbcd46a4
Author: Sunil Mohan Adapa <sunil medhas org>
Date:   Thu Dec 9 01:10:25 2010 +0530

    tracker: New classes to handle SPARQL query filters
    
    These new set of classes represent the filter tree that can specified in a
    SPARQL query. They serialize to produce filter part of a SPARQL query.

 src/plugins/tracker/Makefile.am                    |    5 +
 .../tracker/rygel-tracker-boolean-filter.vala      |   36 +++++++
 .../tracker/rygel-tracker-bound-filter.vala        |   36 +++++++
 .../tracker/rygel-tracker-logical-filter.vala      |  106 ++++++++++++++++++++
 .../tracker/rygel-tracker-query-filter.vala        |   28 +++++
 .../tracker/rygel-tracker-regex-filter.vala        |   40 ++++++++
 6 files changed, 251 insertions(+), 0 deletions(-)
---
diff --git a/src/plugins/tracker/Makefile.am b/src/plugins/tracker/Makefile.am
index ccb1c1f..90a396b 100644
--- a/src/plugins/tracker/Makefile.am
+++ b/src/plugins/tracker/Makefile.am
@@ -27,6 +27,11 @@ librygel_tracker_la_SOURCES = \
 	rygel-tracker-insertion-query.vala \
 	rygel-tracker-query-triplet.vala \
 	rygel-tracker-query-triplets.vala \
+	rygel-tracker-query-filter.vala \
+	rygel-tracker-logical-filter.vala \
+	rygel-tracker-bound-filter.vala \
+	rygel-tracker-regex-filter.vala \
+	rygel-tracker-boolean-filter.vala \
 	rygel-tracker-item-factory.vala \
 	rygel-tracker-video-item-factory.vala \
 	rygel-tracker-music-item-factory.vala \
diff --git a/src/plugins/tracker/rygel-tracker-boolean-filter.vala b/src/plugins/tracker/rygel-tracker-boolean-filter.vala
new file mode 100644
index 0000000..a85eff5
--- /dev/null
+++ b/src/plugins/tracker/rygel-tracker-boolean-filter.vala
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2010 MediaNet Inh.
+ *
+ * Author: Sunil Mohan Adapa <sunil medhas org>
+ *
+ * This file is part of Rygel.
+ *
+ * Rygel is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Rygel is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+/**
+ * Boolean constants in SPARQL query filter
+ */
+public class Rygel.Tracker.BooleanFilter : Object, QueryFilter {
+    public bool value;
+
+    public BooleanFilter (bool value) {
+        this.value = value;
+    }
+
+    public string to_string () {
+        return this.value.to_string ();
+    }
+}
diff --git a/src/plugins/tracker/rygel-tracker-bound-filter.vala b/src/plugins/tracker/rygel-tracker-bound-filter.vala
new file mode 100644
index 0000000..a91c5d8
--- /dev/null
+++ b/src/plugins/tracker/rygel-tracker-bound-filter.vala
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2010 MediaNet Inh.
+ *
+ * Author: Sunil Mohan Adapa <sunil medhas org>
+ *
+ * This file is part of Rygel.
+ *
+ * Rygel is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Rygel is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+/**
+ * Bound expressions in SPARQL query filter
+ */
+public class Rygel.Tracker.BoundFilter : Object, QueryFilter {
+    public string variable;
+
+    public BoundFilter (string variable) {
+        this.variable = variable;
+    }
+
+    public string to_string () {
+        return "bound(" + variable + ")";
+    }
+}
diff --git a/src/plugins/tracker/rygel-tracker-logical-filter.vala b/src/plugins/tracker/rygel-tracker-logical-filter.vala
new file mode 100644
index 0000000..3006c08
--- /dev/null
+++ b/src/plugins/tracker/rygel-tracker-logical-filter.vala
@@ -0,0 +1,106 @@
+/*
+ * Copyright (C) 2010 MediaNet Inh.
+ *
+ * Author: Sunil Mohan Adapa <sunil medhas org>
+ *
+ * This file is part of Rygel.
+ *
+ * Rygel is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Rygel is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+/**
+ * Logical expressions in SPARQL query filter
+ */
+public class Rygel.Tracker.LogicalFilter : Object, QueryFilter {
+    public enum Operator {
+        AND,
+        OR,
+        NOT
+    }
+
+    public Operator op;
+    public QueryFilter operand1;
+    public QueryFilter operand2;
+
+    public LogicalFilter (Operator     op,
+                          QueryFilter  operand1,
+                          QueryFilter? operand2 = null) {
+        this.op = op;
+        this.operand1 = operand1;
+        this.operand2 = operand2;
+    }
+
+    /**
+     * Creates a simplified version of this expressions if it involves boolean
+     * constants.
+     */
+    public QueryFilter simplify () {
+        if (!(this.operand1 is BooleanFilter ||
+              this.operand2 is BooleanFilter)) {
+            return this;
+        }
+
+        if (this.op == Operator.NOT && this.operand1 is BooleanFilter) {
+            var bool_filter = this.operand1 as BooleanFilter;
+
+            bool_filter.value = !(bool_filter.value);
+
+            return this.operand1;
+        }
+
+        BooleanFilter bool_filter;
+        QueryFilter operand;
+
+        if (this.operand1 is BooleanFilter) {
+            bool_filter = this.operand1 as BooleanFilter;
+            operand = this.operand2;
+        } else {
+            bool_filter = this.operand2 as BooleanFilter;
+            operand = this.operand1;
+        }
+
+        if ((bool_filter.value && this.op == Operator.OR) ||
+            (!(bool_filter.value) && this.op == Operator.AND)) {
+            return bool_filter;
+        } else {
+            return operand;
+        }
+    }
+
+    public string to_string () {
+        string str = "(" + this.operand1.to_string () + ")";
+
+        if (this.op == Operator.NOT) {
+            return "!" + str;
+        }
+
+        switch (this.op) {
+        case Operator.AND:
+            str += " && ";
+
+            break;
+        case Operator.OR:
+            str += " || ";
+
+            break;
+        default:
+            assert_not_reached ();
+        }
+
+        str += "(" + this.operand2.to_string () + ")";
+
+        return str;
+    }
+}
diff --git a/src/plugins/tracker/rygel-tracker-query-filter.vala b/src/plugins/tracker/rygel-tracker-query-filter.vala
new file mode 100644
index 0000000..7c03890
--- /dev/null
+++ b/src/plugins/tracker/rygel-tracker-query-filter.vala
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2010 MediaNet Inh.
+ *
+ * Author: Sunil Mohan Adapa <sunil medhas org>
+ *
+ * This file is part of Rygel.
+ *
+ * Rygel is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Rygel is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+/**
+ * An expression in SPARQL query filter
+ */
+public interface Rygel.Tracker.QueryFilter : Object {
+    public abstract string to_string ();
+}
diff --git a/src/plugins/tracker/rygel-tracker-regex-filter.vala b/src/plugins/tracker/rygel-tracker-regex-filter.vala
new file mode 100644
index 0000000..a5638d9
--- /dev/null
+++ b/src/plugins/tracker/rygel-tracker-regex-filter.vala
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2010 MediaNet Inh.
+ *
+ * Author: Sunil Mohan Adapa <sunil medhas org>
+ *
+ * This file is part of Rygel.
+ *
+ * Rygel is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Rygel is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+/**
+ * Regexes in SPARQL query filter
+ */
+public class Rygel.Tracker.RegexFilter : Object, QueryFilter {
+    public string subject;
+    public string regex;
+    public string flags;
+
+    public RegexFilter (string subject, string regex, string flags) {
+        this.subject = subject;
+        this.regex = regex;
+        this.flags = flags;
+    }
+
+    public string to_string () {
+        return "regex(" + subject + ", \"" + regex + "\", \"" + flags + "\")";
+    }
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]