コード例 #1
0
/**
 * Rebuild expression depending on action
 *
 * Example:
 *   $expressionTree = array(
 *     [0] => array(
 *       'id' => '0_92',
 *       'type' => 'operand',
 *       'operand' => '&',
 *       'elements' => array(
 *         [0] => array(
 *           'id' => '0_44',
 *           'type' => 'expression',
 *           'expression' => '{host1:system.cpu.util[,iowait].last(0)} > 50'
 *         ),
 *         [1] => array(
 *           'id' => '48_92',
 *           'type' => 'expression',
 *           'expression' => '{host2:system.cpu.util[,iowait].last(0)} > 50'
 *         )
 *       )
 *     )
 *   )
 *   $action = 'R'
 *   $expressionId = '48_92'
 *
 * Result:
 *   $expressionTree = array(
 *     [0] => array(
 *       'id' => '0_44',
 *       'type' => 'expression',
 *       'expression' => '{host1:system.cpu.util[,iowait].last(0)} > 50'
 *     )
 *   )
 *
 * @param array $expressionTree
 * @param string $expressionId  element identifier like "0_55"
 * @param string $action        one of &/|/r/R (AND/OR/replace/Remove)
 * @param string $newExpression expression for AND, OR or replace actions
 * @param string $operand       parameter only for recursive call
 *
 * @return bool                 returns true if element is found, false - otherwise
 */
function rebuildExpressionTree(array &$expressionTree, $expressionId, $action, $newExpression, $operand = null)
{
    foreach ($expressionTree as $key => $expression) {
        if ($expressionId == $expressionTree[$key]['id']) {
            switch ($action) {
                // AND and OR
                case '&':
                case '|':
                    switch ($expressionTree[$key]['type']) {
                        case 'operand':
                            if ($expressionTree[$key]['operand'] == $action) {
                                $expressionTree[$key]['elements'][] = array('expression' => $newExpression, 'type' => 'expression');
                            } else {
                                $element = array('type' => 'operand', 'operand' => $action, 'elements' => array($expressionTree[$key], array('expression' => $newExpression, 'type' => 'expression')));
                                $expressionTree[$key] = $element;
                            }
                            break;
                        case 'expression':
                            if (!$operand || $operand != $action) {
                                $element = array('type' => 'operand', 'operand' => $action, 'elements' => array($expressionTree[$key], array('expression' => $newExpression, 'type' => 'expression')));
                                $expressionTree[$key] = $element;
                            } else {
                                $expressionTree[] = array('expression' => $newExpression, 'type' => 'expression');
                            }
                            break;
                    }
                    break;
                    // replace
                // replace
                case 'r':
                    $expressionTree[$key]['expression'] = $newExpression;
                    if ($expressionTree[$key]['type'] == 'operand') {
                        $expressionTree[$key]['type'] = 'expression';
                        unset($expressionTree[$key]['operand'], $expressionTree[$key]['elements']);
                    }
                    break;
                    // remove
                // remove
                case 'R':
                    unset($expressionTree[$key]);
                    break;
            }
            return true;
        }
        if ($expressionTree[$key]['type'] == 'operand') {
            if (rebuildExpressionTree($expressionTree[$key]['elements'], $expressionId, $action, $newExpression, $expressionTree[$key]['operand'])) {
                return true;
            }
        }
    }
    return false;
}
コード例 #2
0
/**
 * Rebuild expression depending on action.
 *
 * Supported action values:
 * - and	- add an expression using "and";
 * - or		- add an expression using "or";
 * - r 		- replace;
 * - R		- remove.
 *
 * Example:
 *   $expressionTree = array(
 *     [0] => array(
 *       'id' => '0_94',
 *       'type' => 'operator',
 *       'operator' => 'and',
 *       'elements' => array(
 *         [0] => array(
 *           'id' => '0_44',
 *           'type' => 'expression',
 *           'expression' => '{host1:system.cpu.util[,iowait].last(0)} > 50'
 *         ),
 *         [1] => array(
 *           'id' => '50_94',
 *           'type' => 'expression',
 *           'expression' => '{host2:system.cpu.util[,iowait].last(0)} > 50'
 *         )
 *       )
 *     )
 *   )
 *   $action = 'R'
 *   $expressionId = '50_94'
 *
 * Result:
 *   $expressionTree = array(
 *     [0] => array(
 *       'id' => '0_44',
 *       'type' => 'expression',
 *       'expression' => '{host1:system.cpu.util[,iowait].last(0)} > 50'
 *     )
 *   )
 *
 * @param array 	$expressionTree
 * @param string 	$expressionId  		element identifier like "0_55"
 * @param string 	$action        		action to perform
 * @param string 	$newExpression 		expression for AND, OR or replace actions
 * @param string 	$operator       	parameter only for recursive call
 *
 * @return bool                 returns true if element is found, false - otherwise
 */
function rebuildExpressionTree(array &$expressionTree, $expressionId, $action, $newExpression, $operator = null)
{
    foreach ($expressionTree as $key => $expression) {
        if ($expressionId == $expressionTree[$key]['id']) {
            switch ($action) {
                case 'and':
                case 'or':
                    switch ($expressionTree[$key]['type']) {
                        case 'operator':
                            if ($expressionTree[$key]['operator'] == $action) {
                                $expressionTree[$key]['elements'][] = ['expression' => $newExpression, 'type' => 'expression'];
                            } else {
                                $element = ['type' => 'operator', 'operator' => $action, 'elements' => [$expressionTree[$key], ['expression' => $newExpression, 'type' => 'expression']]];
                                $expressionTree[$key] = $element;
                            }
                            break;
                        case 'expression':
                            if (!$operator || $operator != $action) {
                                $element = ['type' => 'operator', 'operator' => $action, 'elements' => [$expressionTree[$key], ['expression' => $newExpression, 'type' => 'expression']]];
                                $expressionTree[$key] = $element;
                            } else {
                                $expressionTree[] = ['expression' => $newExpression, 'type' => 'expression'];
                            }
                            break;
                    }
                    break;
                    // replace
                // replace
                case 'r':
                    $expressionTree[$key]['expression'] = $newExpression;
                    if ($expressionTree[$key]['type'] == 'operator') {
                        $expressionTree[$key]['type'] = 'expression';
                        unset($expressionTree[$key]['operator'], $expressionTree[$key]['elements']);
                    }
                    break;
                    // remove
                // remove
                case 'R':
                    unset($expressionTree[$key]);
                    break;
            }
            return true;
        }
        if ($expressionTree[$key]['type'] == 'operator') {
            if (rebuildExpressionTree($expressionTree[$key]['elements'], $expressionId, $action, $newExpression, $expressionTree[$key]['operator'])) {
                return true;
            }
        }
    }
    return false;
}