📄 cmpi_wql2dnf.cpp
字号:
//%2006//////////////////////////////////////////////////////////////////////////// Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development// Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.// Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation, The Open Group.// Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; Symantec Corporation; The Open Group.//// Permission is hereby granted, free of charge, to any person obtaining a copy// of this software and associated documentation files (the "Software"), to// deal in the Software without restriction, including without limitation the// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or// sell copies of the Software, and to permit persons to whom the Software is// furnished to do so, subject to the following conditions:// // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN// ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.////==============================================================================//// Author: Markus Mueller (sedgewick_de@yahoo.de)//// Modified By: Adrian Schuur, schuur@de.ibm.com// Konrad Rzeszutek, konradr@us.ibm.com////%/////////////////////////////////////////////////////////////////////////////#include "CMPI_Version.h"#include <Pegasus/Common/Stack.h>#include <Pegasus/WQL/WQLParser.h>#include <Pegasus/WQL/WQLSelectStatementRep.h>#include "CMPI_Wql2Dnf.h"PEGASUS_USING_STD;PEGASUS_NAMESPACE_BEGIN//// Terminal element methods //void term_el_WQL::negate(void){ switch (op) { case WQL_EQ: op = WQL_NE; break; case WQL_NE: op = WQL_EQ; break; case WQL_LT: op = WQL_GE; break; case WQL_LE: op = WQL_GT; break; case WQL_GT: op = WQL_LE; break; case WQL_GE: op = WQL_LT; break; default: break; }};/*String opnd2string(const WQLOperand &o) { switch (o.getType()) { case WQLOperand::PROPERTY_NAME: return o.getPropertyName(); case WQLOperand::STRING_VALUE: return o.getStringValue(); case WQLOperand::INTEGER_VALUE: return Formatter::format("$0",o.getIntegerValue()); case WQLOperand::DOUBLE_VALUE: return Formatter::format("$0",o.getDoubleValue()); case WQLOperand::BOOLEAN_VALUE: return Formatter::format("$0",o.getBooleanValue()); default: ; } return "NULL_VALUE";}CMPIPredOp mapOperation(WQLOperation op) { static CMPIPredOp ops[]={(CMPIPredOp)0,(CMPIPredOp)0,(CMPIPredOp)0, CMPI_PredOp_Equals, CMPI_PredOp_NotEquals, CMPI_PredOp_LessThan, CMPI_PredOp_LessThanOrEquals, CMPI_PredOp_GreaterThan, CMPI_PredOp_GreaterThanOrEquals, (CMPIPredOp)0,(CMPIPredOp)0,(CMPIPredOp)0,(CMPIPredOp)0,(CMPIPredOp)0,(CMPIPredOp)0}; return ops[(int)op];}CMPIType mapType(WQLOperand::Type typ) { switch (typ) { case WQLOperand::PROPERTY_NAME: return CMPI_nameString; case WQLOperand::STRING_VALUE: return CMPI_charString; case WQLOperand::INTEGER_VALUE: return CMPI_integerString; case WQLOperand::DOUBLE_VALUE: return CMPI_realString; case WQLOperand::BOOLEAN_VALUE: return CMPI_booleanString; case WQLOperand::NULL_VALUE: return CMPI_null; } return CMPI_null;}int term_el::toStrings(CMPIType &typ, CMPIPredOp &opr, String &o1, String &o2) const { opr=mapOperation(op); o1=opnd2string(opn1); o2=opnd2string(opn2); if (opn1.getType()==WQLOperand::PROPERTY_NAME) typ=mapType(opn2.getType()); else typ=mapType(opn1.getType()); return 0;}*/CMPIPredOp mapOperation(WQLOperation op) { static CMPIPredOp ops[]={(CMPIPredOp)0,(CMPIPredOp)0,(CMPIPredOp)0, CMPI_PredOp_Equals, CMPI_PredOp_NotEquals, CMPI_PredOp_LessThan, CMPI_PredOp_LessThanOrEquals, CMPI_PredOp_GreaterThan, CMPI_PredOp_GreaterThanOrEquals, (CMPIPredOp)0,(CMPIPredOp)0,(CMPIPredOp)0,(CMPIPredOp)0,(CMPIPredOp)0,(CMPIPredOp)0}; return ops[(int)op];}CMPIType mapType(WQLOperand::Type typ) { switch (typ) { case WQLOperand::PROPERTY_NAME: return CMPI_nameString; case WQLOperand::STRING_VALUE: return CMPI_charString; case WQLOperand::INTEGER_VALUE: return CMPI_integerString; case WQLOperand::DOUBLE_VALUE: return CMPI_realString; case WQLOperand::BOOLEAN_VALUE: return CMPI_booleanString; case WQLOperand::NULL_VALUE: return CMPI_null; } return CMPI_null;}//// Helper function copied from WQLSelectStatement// template<class T>inline static Boolean _Compare(const T& x, const T& y, WQLOperation op){ switch (op) { case WQL_EQ: return x == y; case WQL_NE: return x != y; case WQL_LT: return x < y; case WQL_LE: return x <= y; case WQL_GT: return x > y; case WQL_GE: return x >= y; default: PEGASUS_ASSERT(0); } return false;}static bool operator==(const WQLOperand& x, const WQLOperand& y){ if (x.getType()==y.getType()) switch (x.getType()) { case WQLOperand::PROPERTY_NAME: return x.getPropertyName()==y.getPropertyName(); case WQLOperand::INTEGER_VALUE: return x.getIntegerValue()==y.getIntegerValue(); case WQLOperand::DOUBLE_VALUE: return x.getDoubleValue()==y.getDoubleValue(); case WQLOperand::BOOLEAN_VALUE: return x.getBooleanValue()==y.getBooleanValue(); case WQLOperand::STRING_VALUE: return x.getStringValue()==y.getStringValue(); case WQLOperand::NULL_VALUE: return true; } return false;}static bool operator==(const term_el_WQL& x, const term_el_WQL& y){ return x.op == y.op && x.opn1 == y.opn1 && x.opn2 == y.opn2;}static void addIfNotExists(TableauRow_WQL &tr, const term_el_WQL& el){ for (int i=0,m=tr.size(); i<m; i++) { if (tr[i]==el) return; } tr.append(el);}static Boolean _Evaluate( const WQLOperand& lhs, const WQLOperand& rhs, WQLOperation op){ switch (lhs.getType()) { case WQLOperand::NULL_VALUE: { // This cannot happen since expressions of the form // OPERAND OPERATOR NULL are converted to unary form. // For example: "count IS NULL" is treated as a unary // operation in which IS_NULL is the unary operation // and count is the the unary operand. PEGASUS_ASSERT(0); break; } case WQLOperand::INTEGER_VALUE: { return _Compare( lhs.getIntegerValue(), rhs.getIntegerValue(), op); } case WQLOperand::DOUBLE_VALUE: { return _Compare( lhs.getDoubleValue(), rhs.getDoubleValue(), op); } case WQLOperand::BOOLEAN_VALUE: { return _Compare( lhs.getBooleanValue(), rhs.getBooleanValue(), op); } case WQLOperand::STRING_VALUE: { return _Compare( lhs.getStringValue(), rhs.getStringValue(), op); } default: PEGASUS_ASSERT(0); } return false;}//// WQL Compiler methods// CMPI_Wql2Dnf::CMPI_Wql2Dnf(const String &condition, const String &pref) { WQLSelectStatement wqs; WQLParser::parse(pref+condition,wqs); eval_heap.reserveCapacity(16); terminal_heap.reserveCapacity(16); _tableau.clear(); compile(&wqs);}CMPI_Wql2Dnf::CMPI_Wql2Dnf() { eval_heap.reserveCapacity(16); terminal_heap.reserveCapacity(16); _tableau.clear();}CMPI_Wql2Dnf::CMPI_Wql2Dnf(const WQLSelectStatement & wqs){ eval_heap.reserveCapacity(16); terminal_heap.reserveCapacity(16); _tableau.clear(); compile(&wqs);}CMPI_Wql2Dnf::CMPI_Wql2Dnf(const WQLSelectStatement * wqs){ eval_heap.reserveCapacity(16); terminal_heap.reserveCapacity(16); _tableau.clear(); compile(wqs);}CMPI_Wql2Dnf::~CMPI_Wql2Dnf() {}void CMPI_Wql2Dnf::compile(const WQLSelectStatement * wqs){ if (!wqs->hasWhereClause()) return; _tableau.clear(); _buildEvalHeap(wqs); _pushNOTDown(); _factoring(); Array<CMPI_stack_el> disj; _gatherDisj(disj); if (disj.size() == 0) if (terminal_heap.size() > 0) // point to the remaining terminal element disj.append(CMPI_stack_el(0,true)); for (Uint32 i=0, n =disj.size(); i< n; i++) { TableauRow_WQL tr; Array<CMPI_stack_el> conj; if (!disj[i].is_terminal) { _gatherConj(conj, disj[i]); for( Uint32 j=0, m = conj.size(); j < m; j++) addIfNotExists(tr,terminal_heap[conj[j].opn]);// tr.append(terminal_heap[conj[j].opn]); } else addIfNotExists(tr,terminal_heap[disj[i].opn]);// tr.append(terminal_heap[disj[i].opn]); _tableau.append(tr); } eval_heap.clear(); //print(); //printTableau(); //_sortTableau(); _populateTableau();}Boolean CMPI_Wql2Dnf::evaluate(WQLPropertySource * source) const{ Boolean b = false; WQLOperand lhs, rhs; for(Uint32 i=0,n = _tableau.size(); i < n; i++) { TableauRow_WQL tr = _tableau[i]; for(Uint32 j=0,m = tr.size(); j < m; j++) { lhs = tr[j].opn1; CMPI_Wql2Dnf::_ResolveProperty(lhs,source); rhs = tr[j].opn2; CMPI_Wql2Dnf::_ResolveProperty(rhs,source); if (rhs.getType() != lhs.getType()) throw TypeMismatchException(); if (!_Evaluate(lhs, rhs, tr[j].op)) { b = false; break; } else b = true; } if (b) return true; } return false;}void CMPI_Wql2Dnf::print(void){for (Uint32 i=0, n=eval_heap.size();i < n;i++) { WQLOperation wop = eval_heap[i].op; if (wop == WQL_IS_TRUE) continue; cout << "Eval element " << i << ": "; if (eval_heap[i].is_terminal1) cout << "T("; else cout << "E("; cout << eval_heap[i].opn1 << ") "; cout << WQLOperationToString(eval_heap[i].op); if (eval_heap[i].is_terminal2) cout << " T("; else cout << " E("; cout << eval_heap[i].opn2 << ")" << endl;}for (Uint32 i=0, n=terminal_heap.size();i < n;i++) { cout << "Terminal expression " << i << ": "; cout << terminal_heap[i].opn1.toString() << " "; cout << WQLOperationToString(terminal_heap[i].op) << " " << terminal_heap[i].opn2.toString() << endl;}}void CMPI_Wql2Dnf::printTableau(void){ for(Uint32 i=0,n = _tableau.size(); i < n; i++) { cout << "Tableau " << i << endl; TableauRow_WQL tr = _tableau[i]; for(Uint32 j=0,m = tr.size(); j < m; j++) { cout << tr[j].opn1.toString() << " "; cout << WQLOperationToString(tr[j].op) << " " << tr[j].opn2.toString() << endl; } }}StringWQL2String(const WQLOperand &o) { switch (o.getType()) { case WQLOperand::PROPERTY_NAME: return o.getPropertyName(); case WQLOperand::STRING_VALUE: return o.getStringValue(); case WQLOperand::INTEGER_VALUE: return Formatter::format("$0",o.getIntegerValue()); case WQLOperand::DOUBLE_VALUE: return Formatter::format("$0",o.getDoubleValue()); case WQLOperand::BOOLEAN_VALUE: return Formatter::format("$0",o.getBooleanValue()); default: ; } return "NULL_VALUE";}CMPIPredOpWQL2PredOp(const WQLOperation &op) { static CMPIPredOp ops[]={(CMPIPredOp)0,(CMPIPredOp)0,(CMPIPredOp)0, CMPI_PredOp_Equals, CMPI_PredOp_NotEquals, CMPI_PredOp_LessThan, CMPI_PredOp_LessThanOrEquals, CMPI_PredOp_GreaterThan, CMPI_PredOp_GreaterThanOrEquals, (CMPIPredOp)0,(CMPIPredOp)0,(CMPIPredOp)0,(CMPIPredOp)0,(CMPIPredOp)0,(CMPIPredOp)0};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -