~ubuntu-branches/ubuntu/trusty/rygel/trusty

« back to all changes in this revision

Viewing changes to src/plugins/tracker/rygel-tracker-logical-filter.vala

  • Committer: Package Import Robot
  • Author(s): Andreas Henriksson
  • Date: 2011-12-16 15:21:25 UTC
  • mfrom: (14.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20111216152125-qgn31dkfmhouhrf0
Upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2010 MediaNet Inh.
 
3
 *
 
4
 * Author: Sunil Mohan Adapa <sunil@medhas.org>
 
5
 *
 
6
 * This file is part of Rygel.
 
7
 *
 
8
 * Rygel is free software; you can redistribute it and/or modify
 
9
 * it under the terms of the GNU Lesser General Public License as published by
 
10
 * the Free Software Foundation; either version 2 of the License, or
 
11
 * (at your option) any later version.
 
12
 *
 
13
 * Rygel is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 * GNU Lesser General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU Lesser General Public License
 
19
 * along with this program; if not, write to the Free Software Foundation,
 
20
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
21
 */
 
22
 
 
23
/**
 
24
 * Logical expressions in SPARQL query filter
 
25
 */
 
26
public class Rygel.Tracker.LogicalFilter : Object, QueryFilter {
 
27
    public enum Operator {
 
28
        AND,
 
29
        OR,
 
30
        NOT
 
31
    }
 
32
 
 
33
    public Operator op;
 
34
    public QueryFilter operand1;
 
35
    public QueryFilter operand2;
 
36
 
 
37
    public LogicalFilter (Operator     op,
 
38
                          QueryFilter  operand1,
 
39
                          QueryFilter? operand2 = null) {
 
40
        this.op = op;
 
41
        this.operand1 = operand1;
 
42
        this.operand2 = operand2;
 
43
    }
 
44
 
 
45
    /**
 
46
     * Creates a simplified version of this expressions if it involves boolean
 
47
     * constants.
 
48
     */
 
49
    public QueryFilter simplify () {
 
50
        if (!(this.operand1 is BooleanFilter ||
 
51
              this.operand2 is BooleanFilter)) {
 
52
            return this;
 
53
        }
 
54
 
 
55
        if (this.op == Operator.NOT && this.operand1 is BooleanFilter) {
 
56
            var bool_filter = this.operand1 as BooleanFilter;
 
57
 
 
58
            bool_filter.value = !(bool_filter.value);
 
59
 
 
60
            return this.operand1;
 
61
        }
 
62
 
 
63
        BooleanFilter bool_filter;
 
64
        QueryFilter operand;
 
65
 
 
66
        if (this.operand1 is BooleanFilter) {
 
67
            bool_filter = this.operand1 as BooleanFilter;
 
68
            operand = this.operand2;
 
69
        } else {
 
70
            bool_filter = this.operand2 as BooleanFilter;
 
71
            operand = this.operand1;
 
72
        }
 
73
 
 
74
        if ((bool_filter.value && this.op == Operator.OR) ||
 
75
            (!(bool_filter.value) && this.op == Operator.AND)) {
 
76
            return bool_filter;
 
77
        } else {
 
78
            return operand;
 
79
        }
 
80
    }
 
81
 
 
82
    public string to_string () {
 
83
        string str = "(" + this.operand1.to_string () + ")";
 
84
 
 
85
        if (this.op == Operator.NOT) {
 
86
            return "!" + str;
 
87
        }
 
88
 
 
89
        switch (this.op) {
 
90
        case Operator.AND:
 
91
            str += " && ";
 
92
 
 
93
            break;
 
94
        case Operator.OR:
 
95
            str += " || ";
 
96
 
 
97
            break;
 
98
        default:
 
99
            assert_not_reached ();
 
100
        }
 
101
 
 
102
        str += "(" + this.operand2.to_string () + ")";
 
103
 
 
104
        return str;
 
105
    }
 
106
}