• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

Nepomuk-Core

  • sources
  • kde-4.12
  • kdelibs
  • nepomuk-core
  • libnepomukcore
  • query
negationterm.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the Nepomuk KDE project.
3  Copyright (C) 2009-2012 Sebastian Trueg <trueg@kde.org>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) version 3, or any
9  later version accepted by the membership of KDE e.V. (or its
10  successor approved by the membership of KDE e.V.), which shall
11  act as a proxy defined in Section 6 of version 3 of the license.
12 
13  This library 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 GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include "negationterm.h"
23 #include "negationterm_p.h"
24 #include "querybuilderdata_p.h"
25 #include "andterm.h"
26 #include "andterm_p.h"
27 #include "comparisonterm.h"
28 
29 QString Nepomuk2::Query::NegationTermPrivate::toSparqlGraphPattern( const QString& resourceVarName, const TermPrivate* parentTerm, const QString &additionalFilters, QueryBuilderData *qbd ) const
30 {
31  //
32  // A small optimization: we can negate filters very easily
33  //
34  if(m_subTerm.isComparisonTerm() && m_subTerm.toComparisonTerm().comparator() == ComparisonTerm::Regexp) {
35  QString term = m_subTerm.d_ptr->toSparqlGraphPattern( resourceVarName, parentTerm, additionalFilters, qbd );
36  const int pos = term.indexOf(QLatin1String("FILTER"));
37  term.insert(pos+7, QLatin1Char('!'));
38  return term;
39  }
40  else {
41  //
42  // A negation is expressed via a filter. Since filters can only work on a "real" graph pattern
43  // we need to make sure that such a pattern exists. This can be done by searching one in a
44  // surrounding AndTerm.
45  //
46  // Why is that enough?
47  // Nested AndTerms are flattened before the SPARQL query is constructed in Query. Thus, an AndTerm can
48  // only be embedded in an OrTerm or as a child term to either a ComparisonTerm or an OptionalTerm.
49  // In both cases we need a real pattern inside the AndTerm.
50  //
51  // We use a type pattern for performance reasons. Thus, we assume that each resource has a type. This
52  // is not perfect but much faster than using a wildcard for the property. And in the end all Nepomuk
53  // resources should have a properly defined type.
54  //
55  // FIXME: remove the type pattern. Instead perform optimization before which copies the negation into all unions
56  bool haveRealTerm = false;
57  if( parentTerm && parentTerm->m_type == Term::And ) {
58  haveRealTerm = static_cast<const AndTermPrivate*>(parentTerm)->hasRealPattern();
59  }
60 
61  QString term;
62  if( !haveRealTerm ) {
63  term += QString::fromLatin1("%1 a %2 . ")
64  .arg( resourceVarName, qbd->uniqueVarName() );
65  }
66 
67  term += QString( "FILTER NOT EXISTS { %1 } . " )
68  .arg( m_subTerm.d_ptr->toSparqlGraphPattern( resourceVarName, this, QString(), qbd ) );
69 
70  term += additionalFilters;
71 
72  return term;
73  }
74 }
75 
76 
77 Nepomuk2::Query::NegationTerm::NegationTerm()
78  : SimpleTerm( new NegationTermPrivate() )
79 {
80 }
81 
82 
83 Nepomuk2::Query::NegationTerm::NegationTerm( const NegationTerm& term )
84  : SimpleTerm( term )
85 {
86 }
87 
88 
89 Nepomuk2::Query::NegationTerm::~NegationTerm()
90 {
91 }
92 
93 
94 Nepomuk2::Query::NegationTerm& Nepomuk2::Query::NegationTerm::operator=( const NegationTerm& term )
95 {
96  d_ptr = term.d_ptr;
97  return *this;
98 }
99 
100 
101 Nepomuk2::Query::Term Nepomuk2::Query::NegationTerm::negateTerm( const Term& term )
102 {
103  if ( term.isNegationTerm() ) {
104  return term.toNegationTerm().subTerm();
105  }
106  else {
107  NegationTerm nt;
108  nt.setSubTerm( term );
109  return nt;
110  }
111 }
andterm.h
Nepomuk2::Query::NegationTerm::~NegationTerm
~NegationTerm()
Destructor.
Definition: negationterm.cpp:89
Nepomuk2::Query::Term
The base class for all term types.
Definition: term.h:64
Nepomuk2::Query::SimpleTerm::setSubTerm
void setSubTerm(const Term &term)
Set the sub term to match against.
Definition: simpleterm.cpp:59
Nepomuk2::Query::NegationTerm::negateTerm
static Term negateTerm(const Term &term)
Negate term.
Definition: negationterm.cpp:101
Nepomuk2::Query::SimpleTerm::subTerm
Term subTerm() const
The sub term to match against.
Definition: simpleterm.cpp:52
negationterm.h
Nepomuk2::Query::Term::isNegationTerm
bool isNegationTerm() const
Definition: term.cpp:175
Nepomuk2::Query::NegationTerm
Negate an arbitrary term.
Definition: negationterm.h:47
Nepomuk2::Query::SimpleTerm
Abstract base class for NegationTerm and ComparisonTerm which maintains one sub-term.
Definition: simpleterm.h:41
comparisonterm.h
Nepomuk2::Query::NegationTerm::NegationTerm
NegationTerm()
Default constructor: creates an invalid negation term.
Definition: negationterm.cpp:77
Nepomuk2::Query::NegationTerm::operator=
NegationTerm & operator=(const NegationTerm &term)
Assignment operator.
Definition: negationterm.cpp:94
Nepomuk2::Query::Term::toNegationTerm
NegationTerm toNegationTerm() const
Interpret this term as a NegationTerm.
Definition: term.cpp:230
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:08 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Nepomuk-Core

Skip menu "Nepomuk-Core"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal