Add OR operator to cps-path
steps to be followed :
- Antlr grammer file changes should be made to recognise OR
- Implement the code logic in order to add OR functionality
- Add test cases
- Update documentation
- Demo to team
Here following are the antlr changes which made to recognise the OR
So,before there is and condition which is applied with leaf condition ,similarly we have added or to the antlr
path :cps-path-parser/src/main/antlr4/org/onap/cps/cpspath/parser/antlr4/CpsPath.g4
listElementRef : OB leafCondition ( ( KW_AND | KW_OR) leafCondition)* CB
multipleLeafConditions : OB leafCondition (( KW_AND | KW_OR) leafCondition)* CB
to-do:
Need to implement the logic for OR Operation
But there are few doubts regarding this to be discussed
References
CPS-1273
Query data node using cps-path
In query of data node, when we can query using <,>,>=,<= comparative operators . Also we can combine “AND/OR” conditions with comparative operators . Below are the some examples
Comparative Operators Condition
# | cps-path | Output |
---|
1 | Using "<" condition cps-path : //books[@price<15] |
Code Block |
---|
title | Json Response |
---|
collapse | true |
---|
| [
{
"book-store:books": {
"lang": "English",
"price": 10,
"title": "Matilda",
"authors": [
"Roald Dahl"
],
"editions": [
1988,
2000
]
}
},
{
"book-store:books": {
"lang": "English",
"price": 14,
"title": "The Light Fantastic",
"authors": [
"Terry Pratchett"
],
"editions": [
1986
]
}
},
{
"book-store:books": {
"lang": "English",
"price": 12,
"title": "The Colour of Magic",
"authors": [
"Terry Pratchett"
],
"editions": [
1983
]
}
},
{
"book-store:books": {
"lang": "English",
"price": 13,
"title": "Good Omens",
"authors": [
"Terry Pratchett",
"Neil Gaiman"
],
"editions": [
2006
]
}
},
{
"book-store:books": {
"lang": "N/A",
"price": 11,
"title": "Logarithm tables",
"authors": [
"Joe Bloggs"
],
"editions": [
2009
]
}
}
] | |
---|
2 | Using "<" with OR condition cps-path : //books[@price<10 or @lang="German"] |
Code Block |
---|
title | Json Response |
---|
collapse | true |
---|
| [
{
"book-store:books": {
"lang": "German",
"price": 39,
"title": "Debian GNU/Linux",
"authors": [
"Peter H. Ganten",
"Wulf Alex"
],
"editions": [
2013,
2021,
2007
]
}
}
] | |
---|
3 | Using ">" with AND condition cps-path : //books[@price>13 and @title="A Book with No Language"] |
Code Block |
---|
title | Json Response |
---|
collapse | true |
---|
| [
{
"book-store:books": {
"lang": "",
"price": 20,
"title": "A Book with No Language",
"authors": [
"Joe Bloggs"
],
"editions": [
2023
]
}
}
] |
|
---|
4 | Using ">=" with combination of OR/AND condition cps-path : //books[@price>=13 or @lang="Spanish" and @title="Good Omens"] |
Code Block |
---|
title | Json Response |
---|
collapse | true |
---|
| [
{
"book-store:books": {
"lang": "English",
"price": 15,
"title": "The Gruffalo",
"authors": [
"Julia Donaldson"
],
"editions": [
1999
]
}
},
{
"book-store:books": {
"lang": "English",
"price": 15,
"title": "Annihilation",
"authors": [
"Jeff VanderMeer"
],
"editions": [
2014
]
}
},
{
"book-store:books": {
"lang": "English",
"price": 14,
"title": "The Light Fantastic",
"authors": [
"Terry Pratchett"
],
"editions": [
1986
]
}
},
{
"book-store:books": {
"lang": "English",
"price": 13,
"title": "Good Omens",
"authors": [
"Terry Pratchett",
"Neil Gaiman"
],
"editions": [
2006
]
}
},
{
"book-store:books": {
"lang": "",
"price": 20,
"title": "A Book with No Language",
"authors": [
"Joe Bloggs"
],
"editions": [
2023
]
}
},
{
"book-store:books": {
"lang": "German",
"price": 39,
"title": "Debian GNU/Linux",
"authors": [
"Peter H. Ganten",
"Wulf Alex"
],
"editions": [
2013,
2021,
2007
]
}
}
] |
|
---|
5 | Using "<=" with combination of AND/OR condition cps-path : //books[@price<=15 and @title="Annihilation" or @lang="Spanish"] |
Code Block |
---|
title | Json Response |
---|
collapse | true |
---|
| [
{
"book-store:books": {
"lang": "English",
"price": 15,
"title": "Annihilation",
"authors": [
"Jeff VanderMeer"
],
"editions": [
2014
]
}
}
] |
|
---|
Implementation of Comparative Operator
1.Update antlr parser to recognize <,>,<=,>= in leaf-condition
2.Implement required (native) query
3.Add Integration tests for comparative operators
4.Update documentation
5.demo to team
Query used : SELECT * FROM fragment WHERE anchor_id = :anchorId AND xpath ~ :xpathRegex AND ((attributes ->> 'price')\:\:int > '13' and attributes @> '{"title":"A Book with No Language"}')
Limitations
1.Using comparative operators with string values will lead to an error at runtime. This error can't be validated earlier as the datatype is unknown until the execution phase.