Commit f2fdfd25 authored by Iker Hurtado's avatar Iker Hurtado
Browse files

Make independent search by elements and by formulas. Add partial (pending of...

Make independent search by elements and by formulas. Add partial (pending of complete definition) support for the NOT operator and one level of parentheses

Little tested
parent d24ee075
Pipeline #92498 skipped with stage
...@@ -80,7 +80,7 @@ class MaterialNameBox{ ...@@ -80,7 +80,7 @@ class MaterialNameBox{
enableInput() { enableInput() {
this.materialNameField.element.disabled = false; this.materialNameField.element.disabled = false;
this.button.disabled = true; this.button.disabled = false;
} }
} }
......
...@@ -178,6 +178,7 @@ class NewSearchMod { ...@@ -178,6 +178,7 @@ class NewSearchMod {
this.elementTable.setClickListener(elementArray => { this.elementTable.setClickListener(elementArray => {
this.searchBox.addElements(elementArray); this.searchBox.addElements(elementArray);
this.addMatNameButton.disabled = true; // Not always necessary but it simplifies the code this.addMatNameButton.disabled = true; // Not always necessary but it simplifies the code
this.addFormulaButton.disabled = true;
}); });
this.elementTable.setDeselectListener(e => this.searchBox.removeElementORFormulaInSearchQuery(e)); this.elementTable.setDeselectListener(e => this.searchBox.removeElementORFormulaInSearchQuery(e));
...@@ -187,8 +188,9 @@ class NewSearchMod { ...@@ -187,8 +188,9 @@ class NewSearchMod {
if (formula.trim() !== ''){ if (formula.trim() !== ''){
this.searchBox.addTag(formula, 'F'); this.searchBox.addTag(formula, 'F');
//this.formulaBox.disable(true); //this.formulaBox.disable(true);
this.addElementButton.disabled = true;
this.addMatNameButton.disabled = true; this.addMatNameButton.disabled = true;
this.materialNameBox.disableInput(); // this.materialNameBox.disableInput(); It doesn't seem needed
} }
}); });
...@@ -199,8 +201,7 @@ class NewSearchMod { ...@@ -199,8 +201,7 @@ class NewSearchMod {
this.searchBox.addTag(name, 'MN');// this.addTag(name, 'MN'); this.searchBox.addTag(name, 'MN');// this.addTag(name, 'MN');
this.addElementButton.disabled = true; this.addElementButton.disabled = true;
this.addFormulaButton.disabled = true; this.addFormulaButton.disabled = true;
//TODO: are the following two lines needed, when the related buttons are already diasbled?? // this.formulaBox.disableInput(); It doesn't seem needed
this.formulaBox.disableInput();
this.materialNameBox.disableInput(); this.materialNameBox.disableInput();
} }
}); });
...@@ -871,7 +872,7 @@ class FormulaBox{ ...@@ -871,7 +872,7 @@ class FormulaBox{
enableInput() { enableInput() {
this.formulaTextField.disabled = false; this.formulaTextField.disabled = false;
this.formulaButton.disabled = true; this.formulaButton.disabled = false;
} }
/* /*
......
...@@ -84,6 +84,8 @@ class SearchBox{ ...@@ -84,6 +84,8 @@ class SearchBox{
getOptimadeQuery(allowOtherElements){ getOptimadeQuery(allowOtherElements){
console.log('getOptimadeQuery this.searchQuery', this.searchQuery)
if (this.searchQuery.length === 0 || if (this.searchQuery.length === 0 ||
!isQueryWellFormed(this.searchQuery)) return ''; !isQueryWellFormed(this.searchQuery)) return '';
...@@ -91,6 +93,37 @@ class SearchBox{ ...@@ -91,6 +93,37 @@ class SearchBox{
return `material_name="${this.searchQuery[0]}"`; return `material_name="${this.searchQuery[0]}"`;
}else{ // elements and formulas }else{ // elements and formulas
let searchQueryMod = [];
let parOptimadeQueries = [];
let openIndex = -1;
let parCounter = 0;
this.searchQuery.forEach( (item, i) => {
if ( this.searchQuery[i] === '(' ){
openIndex = i;
}
else if ( this.searchQuery[i] === ')' ){
searchQueryMod.push('('+parCounter);
const parSearchQuery = this.searchQuery.slice(openIndex+1, i);
//console.log('parSearchQuery: ',i, parSearchQuery, getOptimadeFromSearchQuery(allowOtherElements, parSearchQuery));
parOptimadeQueries.push(getOptimadeFromSearchQuery(allowOtherElements, parSearchQuery))
openIndex = -1;
parCounter++;
}else if (openIndex < 0) searchQueryMod.push(item);
});
// console.log('FFFFFFFFF searchQueryMod: ', searchQueryMod, parOptimadeQueries, getOptimadeFromSearchQuery(allowOtherElements, searchQueryMod, parOptimadeQueries));
//********* Not valid expressions
// return getOptimadeFromSearchQuery(allowOtherElements, this.searchQuery);
return getOptimadeFromSearchQuery(allowOtherElements, searchQueryMod, parOptimadeQueries);
}
function getOptimadeFromSearchQuery(allowOtherElements, searchQuery, optimadeSubqueries){
let optimadeQuery = ''; let optimadeQuery = '';
// Get the subqueries: parts of the query separated by ORs // Get the subqueries: parts of the query separated by ORs
...@@ -98,20 +131,18 @@ class SearchBox{ ...@@ -98,20 +131,18 @@ class SearchBox{
let currentSubquery = []; let currentSubquery = [];
subqueries.push(currentSubquery); subqueries.push(currentSubquery);
this.searchQuery.forEach( (item, i) => { searchQuery.forEach( (item, i) => {
if (this.searchQuery[i] === 'OR'){ if (searchQuery[i] === 'OR'){
currentSubquery = []; // new subquery currentSubquery = []; // new subquery
subqueries.push(currentSubquery); subqueries.push(currentSubquery);
}else }else
currentSubquery.push(item); currentSubquery.push(item);
}); });
console.log("getOptimadeQuery subqueries: ", subqueries);
console.log("getOptimadeQuery subqueries: ", subqueries) // Subqueries are either elements or formulas separated by ANDs
// Subqueries are either a formula or several elements separated by ANDs
subqueries.forEach( (subquery, i) => { subqueries.forEach( (subquery, i) => {
if (i > 0) optimadeQuery += ' OR ' if (i > 0) optimadeQuery += ' OR '
...@@ -119,7 +150,10 @@ class SearchBox{ ...@@ -119,7 +150,10 @@ class SearchBox{
if (allowOtherElements){ // Inclusive search if (allowOtherElements){ // Inclusive search
subquery.forEach( (item, i) => { // For every item in the subquery subquery.forEach( (item, i) => { // For every item in the subquery
if ( isElement(item) ){ if (item.charAt(0) === '('){
// console.log('OPPPPP', optimadeSubqueries[+item.charAt(1)])
optimadeQuery += `(${optimadeSubqueries[+item.charAt(1)]})`
}else if ( isElement(item) ){
optimadeQuery += `elements HAS "${item}"` optimadeQuery += `elements HAS "${item}"`
}else if ( item === 'AND' || item === 'NOT'){ }else if ( item === 'AND' || item === 'NOT'){
...@@ -133,7 +167,8 @@ class SearchBox{ ...@@ -133,7 +167,8 @@ class SearchBox{
}else{ // Exclusive search. For now only one formula or elements subqueries supported. Operator NOT doesn't make sense here }else{ // Exclusive search. For now only one formula or elements subqueries supported. Operator NOT doesn't make sense here
if (subquery.length === 1 && !isElement(subquery[0])){ // Only one formula // if (subquery.length === 1 && !isElement(subquery[0])){ // Only one formula
if (!isElement(subquery[0])){ // formula(s)
optimadeQuery += `formula="${subquery[0]}"`; optimadeQuery += `formula="${subquery[0]}"`;
}else{ // Several items, they all should be elements }else{ // Several items, they all should be elements
...@@ -142,11 +177,8 @@ class SearchBox{ ...@@ -142,11 +177,8 @@ class SearchBox{
} }
}); });
console.log('optimadeQuery: ', optimadeQuery)
return optimadeQuery; return optimadeQuery;
}
function isElement(item){ function isElement(item){
...@@ -166,9 +198,6 @@ class SearchBox{ ...@@ -166,9 +198,6 @@ class SearchBox{
return optSubquery; return optSubquery;
} }
}
function isQueryWellFormed(searchQuery){ function isQueryWellFormed(searchQuery){
const openingParIndex = searchQuery.indexOf('(') const openingParIndex = searchQuery.indexOf('(')
...@@ -178,6 +207,7 @@ class SearchBox{ ...@@ -178,6 +207,7 @@ class SearchBox{
// they are in right position // they are in right position
(openingParIndex >= 0 && openingParIndex < closingParIndex+1)) (openingParIndex >= 0 && openingParIndex < closingParIndex+1))
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment