public ExpressionNode expand(ExpressionNode src) { if (!src.isSingleNode()) { NonLeafExpressionNode nlExp = (NonLeafExpressionNode) src; List<ExpressionNode> childExps = nlExp.getChildExps(); Operator outerOp = nlExp.getOperator(); if (isToBeExpanded(childExps)) { // Any of the child exp is a non leaf exp with & or | operator NonLeafExpressionNode newNode = new NonLeafExpressionNode(nlExp.getOperator()); for (ExpressionNode exp : childExps) { if (exp.isSingleNode()) { newNode.addChildExp(exp); } else { newNode.addChildExp(expand(exp)); } } nlExp = expandNonLeaf(newNode, outerOp); } return nlExp; } if (src instanceof NonLeafExpressionNode && ((NonLeafExpressionNode) src).getOperator() == Operator.NOT) { // Negate the exp return negate((NonLeafExpressionNode) src); } return src; }
throw new IOException(e); node = EXP_EXPANDER.expand(node); List<Tag> tags = new ArrayList<>(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
@Test public void testPositiveCases() throws Exception { ExpressionExpander expander = new ExpressionExpander(); ExpressionNode result = expander.expand(exp1); assertTrue(result instanceof NonLeafExpressionNode); NonLeafExpressionNode nlResult = (NonLeafExpressionNode) result; result = expander.expand(exp2); assertTrue(result instanceof NonLeafExpressionNode); nlResult = (NonLeafExpressionNode) result; result = expander.expand(exp3); assertTrue(result instanceof NonLeafExpressionNode); nlResult = (NonLeafExpressionNode) result; Operator.OR, new LeafExpressionNode("a"), new LeafExpressionNode("b")), new LeafExpressionNode("c")); result = expander.expand(exp4); assertTrue(result instanceof NonLeafExpressionNode); nlResult = (NonLeafExpressionNode) result; Operator.AND, new LeafExpressionNode("a"), new LeafExpressionNode("b")), new LeafExpressionNode("c")); result = expander.expand(exp5); assertTrue(result instanceof NonLeafExpressionNode); nlResult = (NonLeafExpressionNode) result; Operator.OR, new LeafExpressionNode("a"), new LeafExpressionNode("b")), new LeafExpressionNode("c"));
newNode = mergeChildNodes(newNode, outerOp, rightChild, (NonLeafExpressionNode) leftChild); } else if (leftChild.isSingleNode()) { newNode = mergeChildNodes(newNode, outerOp, leftChild, (NonLeafExpressionNode) rightChild); } else {
private ExpressionNode negate(NonLeafExpressionNode nlExp) { ExpressionNode notChild = nlExp.getChildExps().get(0); if (notChild instanceof LeafExpressionNode) { return nlExp; } NonLeafExpressionNode nlNotChild = (NonLeafExpressionNode) notChild; if (nlNotChild.getOperator() == Operator.NOT) { // negate the negate return nlNotChild.getChildExps().get(0); } Operator negateOp = nlNotChild.getOperator() == Operator.AND ? Operator.OR : Operator.AND; NonLeafExpressionNode newNode = new NonLeafExpressionNode(negateOp); for (ExpressionNode expNode : nlNotChild.getChildExps()) { NonLeafExpressionNode negateNode = new NonLeafExpressionNode(Operator.NOT); negateNode.addChildExp(expNode.deepClone()); newNode.addChildExp(expand(negateNode)); } return newNode; }
@Test public void testPositiveCases() throws Exception { ExpressionExpander expander = new ExpressionExpander(); ExpressionNode result = expander.expand(exp1); assertTrue(result instanceof NonLeafExpressionNode); NonLeafExpressionNode nlResult = (NonLeafExpressionNode) result; result = expander.expand(exp2); assertTrue(result instanceof NonLeafExpressionNode); nlResult = (NonLeafExpressionNode) result; result = expander.expand(exp3); assertTrue(result instanceof NonLeafExpressionNode); nlResult = (NonLeafExpressionNode) result; Operator.OR, new LeafExpressionNode("a"), new LeafExpressionNode("b")), new LeafExpressionNode("c")); result = expander.expand(exp4); assertTrue(result instanceof NonLeafExpressionNode); nlResult = (NonLeafExpressionNode) result; Operator.AND, new LeafExpressionNode("a"), new LeafExpressionNode("b")), new LeafExpressionNode("c")); result = expander.expand(exp5); assertTrue(result instanceof NonLeafExpressionNode); nlResult = (NonLeafExpressionNode) result; Operator.OR, new LeafExpressionNode("a"), new LeafExpressionNode("b")), new LeafExpressionNode("c"));
newNode = mergeChildNodes(newNode, outerOp, rightChild, (NonLeafExpressionNode) leftChild); } else if (leftChild.isSingleNode()) { newNode = mergeChildNodes(newNode, outerOp, leftChild, (NonLeafExpressionNode) rightChild); } else {
public ExpressionNode expand(ExpressionNode src) { if (!src.isSingleNode()) { NonLeafExpressionNode nlExp = (NonLeafExpressionNode) src; List<ExpressionNode> childExps = nlExp.getChildExps(); Operator outerOp = nlExp.getOperator(); if (isToBeExpanded(childExps)) { // Any of the child exp is a non leaf exp with & or | operator NonLeafExpressionNode newNode = new NonLeafExpressionNode(nlExp.getOperator()); for (ExpressionNode exp : childExps) { if (exp.isSingleNode()) { newNode.addChildExp(exp); } else { newNode.addChildExp(expand(exp)); } } nlExp = expandNonLeaf(newNode, outerOp); } return nlExp; } if (src instanceof NonLeafExpressionNode && ((NonLeafExpressionNode) src).getOperator() == Operator.NOT) { // Negate the exp return negate((NonLeafExpressionNode) src); } return src; }
@Override public List<Tag> createVisibilityExpTags(String visExpression, boolean withSerializationFormat, boolean checkAuths) throws IOException { ExpressionNode node = null; try { node = this.expressionParser.parse(visExpression); } catch (ParseException e) { throw new IOException(e); } node = this.expressionExpander.expand(node); List<Tag> tags = new ArrayList<>(); if (withSerializationFormat) { tags.add(STRING_SERIALIZATION_FORMAT_TAG); } if (node instanceof NonLeafExpressionNode && ((NonLeafExpressionNode) node).getOperator() == Operator.OR) { for (ExpressionNode child : ((NonLeafExpressionNode) node).getChildExps()) { tags.add(createTag(child)); } } else { tags.add(createTag(node)); } return tags; }
throw new IOException(e); node = EXP_EXPANDER.expand(node); List<Tag> tags = new ArrayList<Tag>(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
private ExpressionNode negate(NonLeafExpressionNode nlExp) { ExpressionNode notChild = nlExp.getChildExps().get(0); if (notChild instanceof LeafExpressionNode) { return nlExp; } NonLeafExpressionNode nlNotChild = (NonLeafExpressionNode) notChild; if (nlNotChild.getOperator() == Operator.NOT) { // negate the negate return nlNotChild.getChildExps().get(0); } Operator negateOp = nlNotChild.getOperator() == Operator.AND ? Operator.OR : Operator.AND; NonLeafExpressionNode newNode = new NonLeafExpressionNode(negateOp); for (ExpressionNode expNode : nlNotChild.getChildExps()) { NonLeafExpressionNode negateNode = new NonLeafExpressionNode(Operator.NOT); negateNode.addChildExp(expNode.deepClone()); newNode.addChildExp(expand(negateNode)); } return newNode; }
@Override public List<Tag> createVisibilityExpTags(String visExpression, boolean withSerializationFormat, boolean checkAuths) throws IOException { ExpressionNode node = null; try { node = this.expressionParser.parse(visExpression); } catch (ParseException e) { throw new IOException(e); } node = this.expressionExpander.expand(node); List<Tag> tags = new ArrayList<>(); if (withSerializationFormat) { tags.add(STRING_SERIALIZATION_FORMAT_TAG); } if (node instanceof NonLeafExpressionNode && ((NonLeafExpressionNode) node).getOperator() == Operator.OR) { for (ExpressionNode child : ((NonLeafExpressionNode) node).getChildExps()) { tags.add(createTag(child)); } } else { tags.add(createTag(node)); } return tags; }