working on RPN generation

This commit is contained in:
Jörg Prante 2023-03-22 17:37:57 +01:00
parent 23b2188159
commit 155f0adcd4
8 changed files with 98 additions and 18 deletions

View file

@ -1,5 +1,5 @@
group = org.xbib group = org.xbib
name = z3950 name = z3950
version = 5.0.2 version = 5.0.3
org.gradle.warning.mode = ALL org.gradle.warning.mode = ALL

View file

@ -79,9 +79,7 @@ public class SearchOperation extends AbstractOperation<SearchResponse, SearchReq
dbs[n].value.value = new ASN1GeneralString(databases.get(n)); dbs[n].value.value = new ASN1GeneralString(databases.get(n));
} }
search.databaseNames = dbs; search.databaseNames = dbs;
if (logger.isLoggable(Level.FINE)) { logger.log(Level.FINER, search.toString());
logger.log(Level.FINE, search.toString());
}
write(search); write(search);
SearchResponse response = read(); SearchResponse response = read();
if (response != null) { if (response != null) {

View file

@ -7,24 +7,37 @@ public class Expression extends Node {
private final String op; private final String op;
private final Query q1; private final Query query1;
private final Query q2; private final Query query2;
public Expression(String op, Query q1, Query q2) { public Expression(String op, Query query1, Query query2) {
this.op = op; this.op = op;
this.q1 = q1; this.query1 = query1;
this.q2 = q2; this.query2 = query2;
} }
@Override @Override
public void accept(Visitor visitor) { public void accept(Visitor visitor) {
q2.accept(visitor); query2.accept(visitor);
q1.accept(visitor); query1.accept(visitor);
visitor.visit(this); visitor.visit(this);
} }
public String getOperator() { public String getOperator() {
return op; return op;
} }
public Query getQuery1() {
return query1;
}
public Query getQuery2() {
return query2;
}
@Override
public String toString() {
return "[Expression: op=" + op + ",query1=" + query1 + ",query2=" + query2 + "]";
}
} }

View file

@ -47,6 +47,7 @@ public class PQFRPNGenerator implements Visitor {
if (any instanceof RPNStructure) { if (any instanceof RPNStructure) {
rpnQuery.rpn = (RPNStructure) any; rpnQuery.rpn = (RPNStructure) any;
} else if (any instanceof ASN1OctetString) { } else if (any instanceof ASN1OctetString) {
// TODO
logger.log(Level.INFO, "found ASN1OctetString = " + ((ASN1OctetString) any).get()); logger.log(Level.INFO, "found ASN1OctetString = " + ((ASN1OctetString) any).get());
} }
if (pqf.getAttrSet() == null) { if (pqf.getAttrSet() == null) {
@ -64,7 +65,13 @@ public class PQFRPNGenerator implements Visitor {
Operand operand = new Operand(); Operand operand = new Operand();
operand.attrTerm = new AttributesPlusTerm(); operand.attrTerm = new AttributesPlusTerm();
operand.attrTerm.term = new org.xbib.z3950.common.v3.Term(); operand.attrTerm.term = new org.xbib.z3950.common.v3.Term();
operand.attrTerm.term.c_general = new ASN1OctetString(query.getTerm().getValue()); Term term = query.getTerm();
if (term != null) {
Logger.getLogger("").log(Level.INFO, "query = " + query + " term = " + term + " value = " + term.getValue());
operand.attrTerm.term.c_general = new ASN1OctetString(term.getValue());
} else {
operand.attrTerm.term.c_null = new ASN1Null();
}
Stack<AttributeElement> attrs = new Stack<>(); Stack<AttributeElement> attrs = new Stack<>();
ASN1Any any = !result.isEmpty() && result.peek() instanceof AttributeElement ? result.pop() : null; ASN1Any any = !result.isEmpty() && result.peek() instanceof AttributeElement ? result.pop() : null;
while (any != null) { while (any != null) {
@ -75,10 +82,9 @@ public class PQFRPNGenerator implements Visitor {
operand.attrTerm.attributes.value = attrs.toArray(new AttributeElement[0]); operand.attrTerm.attributes.value = attrs.toArray(new AttributeElement[0]);
RPNStructure rpn = new RPNStructure(); RPNStructure rpn = new RPNStructure();
rpn.c_op = operand; rpn.c_op = operand;
if (attrs.size() > 0) { Logger.getLogger("").log(Level.INFO, "push rpn = " + rpn);
result.push(rpn); result.push(rpn);
} }
}
@Override @Override
public void visit(Expression expr) { public void visit(Expression expr) {
@ -95,8 +101,21 @@ public class PQFRPNGenerator implements Visitor {
if ("@not".equals(op)) { if ("@not".equals(op)) {
rpn.c_rpnRpnOp.s_op.andNotOp = new ASN1Null(); rpn.c_rpnRpnOp.s_op.andNotOp = new ASN1Null();
} }
rpn.c_rpnRpnOp.s_rpn1 = (RPNStructure) result.pop(); ASN1Any rpn1 = result.pop();
rpn.c_rpnRpnOp.s_rpn2 = (RPNStructure) result.pop(); if (rpn1 instanceof RPNStructure) {
Logger.getLogger("test").log(Level.INFO, "rpn1 = " + rpn1);
rpn.c_rpnRpnOp.s_rpn1 = (RPNStructure) rpn1;
} else {
Logger.getLogger("test").log(Level.INFO, "debug rpn1: " + rpn1);
}
ASN1Any rpn2 = result.pop();
if (rpn2 instanceof RPNStructure) {
Logger.getLogger("test").log(Level.INFO, "rpn2 = " + rpn2);
rpn.c_rpnRpnOp.s_rpn2 = (RPNStructure) rpn2;
} else {
Logger.getLogger("test").log(Level.INFO, "debug rpn2: " + rpn2);
}
Logger.getLogger("test").log(Level.INFO, "visit expr: rpn = " + rpn);
result.push(rpn); result.push(rpn);
} }
@ -111,6 +130,7 @@ public class PQFRPNGenerator implements Visitor {
@Override @Override
public void visit(Term term) { public void visit(Term term) {
Logger.getLogger("test").log(Level.INFO, "push term = " + term);
result.push(new ASN1OctetString(term.getValue())); result.push(new ASN1OctetString(term.getValue()));
} }

View file

@ -8,11 +8,17 @@ import java.util.LinkedList;
public class Query extends Node { public class Query extends Node {
private String attrschema; private String attrschema;
private final LinkedList<AttrStr> attrspec = new LinkedList<>(); private final LinkedList<AttrStr> attrspec = new LinkedList<>();
private Query querystruct; private Query querystruct;
private Setname setname; private Setname setname;
private Term term; private Term term;
private Expression expr; private Expression expr;
private PQF pqf; private PQF pqf;
// ATTR CHARSTRING1 attrstr querystruct // ATTR CHARSTRING1 attrstr querystruct
@ -91,6 +97,9 @@ public class Query extends Node {
@Override @Override
public String toString() { public String toString() {
if (expr != null) {
return "[Query: expr=" + expr + "]";
}
return "[Query: term=" + term + " attrschema=" + attrschema + " setname=" + setname + return "[Query: term=" + term + " attrschema=" + attrschema + " setname=" + setname +
" querystruct=" + querystruct + "]"; " querystruct=" + querystruct + "]";
} }

View file

@ -32,7 +32,7 @@ class PQFParserTest {
} }
} }
assertEquals(errors, 0); assertEquals(errors, 0);
assertEquals(ok, 19); assertEquals(ok, 20);
} }
/** /**

View file

@ -0,0 +1,39 @@
package org.xbib.z3950.common.pqf;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.xbib.z3950.common.v3.RPNQuery;
import java.io.StringReader;
import java.util.logging.Level;
import java.util.logging.Logger;
class PQFTest {
private static final Logger logger = Logger.getLogger(PQFTest.class.getName());
@Test
@Disabled
void testParser() throws SyntaxException {
String q = "@and @attr 1=4 linux @attr 1=2 sybex";
PQFParser parser = new PQFParser(new StringReader(q));
parser.parse();
PQF pqf = parser.getResult();
logger.log(Level.INFO, "pqf = " + pqf.toString());
}
@Test
void testRPN() {
String q = "@and @attr 1=4 linux @attr 1=2 sybex";
logger.log(Level.INFO, "rpn = " + createRPNQueryFromPQF(q));
}
private RPNQuery createRPNQueryFromPQF(String query) {
PQFRPNGenerator generator = new PQFRPNGenerator();
org.xbib.z3950.common.pqf.PQFParser parser = new org.xbib.z3950.common.pqf.PQFParser(new StringReader(query));
parser.parse();
parser.getResult().accept(generator);
return generator.getResult();
}
}

View file

@ -17,3 +17,4 @@ dylan
@or @and bob dylan @set Result-1 @or @and bob dylan @set Result-1
@attr 4=1 @and @attr 1=1 "bob dylan" @attr 1=4 "slow train coming" @attr 4=1 @and @attr 1=1 "bob dylan" @attr 1=4 "slow train coming"
@and @attr 2=4 @attr gils 1=2038 -114 @attr 2=2 @attr gils 1=2039 -109 @and @attr 2=4 @attr gils 1=2038 -114 @attr 2=2 @attr gils 1=2039 -109
@and @attr 1=4 @attr 2=3 @attr 3=3 @attr 4=1 @attr 5=1 @attr 6=1 "linux" @attr 1=2 @attr 2=3 @attr 3=3 @attr 4=1 @attr 5=1 @attr 6=1 "sybex"