Skip to content

Commit

Permalink
[#231] Improve logs readability
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgenii Grigorev authored and blcham committed Jul 15, 2024
1 parent 9a02c94 commit cf87630
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ private void checkConstraints(Model model, QuerySolution bindings, List<Resource
// TemplateCall templateCall = SPINFactory.asTemplateCall(queryRes);
// }

Query query = ARQFactory.get().createQuery(spinQuery);
Query query = QueryUtils.createQuery(spinQuery);

QueryExecution execution = QueryExecutionFactory.create(query, model, bindings);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cz.cvut.spipes.engine.ExecutionContext;
import cz.cvut.spipes.engine.ExecutionContextFactory;
import cz.cvut.spipes.engine.VariablesBinding;
import cz.cvut.spipes.util.QueryUtils;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
Expand All @@ -26,7 +27,7 @@ public class BindBySelectModule extends AbstractModule {
@Override
public ExecutionContext executeSelf() {

Query query = ARQFactory.get().createQuery(selectQuery);
Query query = QueryUtils.createQuery(selectQuery);

QuerySolution inputBindings = executionContext.getVariablesBinding().asQuerySolution();

Expand Down
15 changes: 14 additions & 1 deletion s-pipes-core/src/main/java/cz/cvut/spipes/util/QueryUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cz.cvut.spipes.util;


import org.apache.commons.lang3.StringUtils;
import org.apache.jena.query.*;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.RDFNode;
Expand All @@ -12,6 +13,7 @@
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class QueryUtils {
Expand Down Expand Up @@ -183,7 +185,18 @@ public static Query createQuery(org.topbraid.spin.model.Query spinQuery) {
return ARQFactory.get().createQuery(spinQuery);
} catch (QueryParseException e) {
String query = ARQFactory.get().createCommandString(spinQuery);
LOG.error("Parse error [1] occurred in query [2].\n[1] ERROR:\n{}\n[2] QUERY:\n{}", e.getMessage(), query);
String msg = e.getMessage();
Pattern pattern = Pattern.compile("line\\s+(\\d+)");
Matcher matcher = pattern.matcher(msg);
matcher.find();
String numberAfterLine = matcher.group(1);
int wrongLineNumber = Integer.valueOf(numberAfterLine); //e.g
// etLine() returns wrong number for some reason
int beginningOfTheWrongLine = StringUtils.ordinalIndexOf(query, "\n", wrongLineNumber - 1);
int endOfTheWrongLine = StringUtils.ordinalIndexOf(query, "\n", wrongLineNumber);
String wrongLine = query.substring(beginningOfTheWrongLine, endOfTheWrongLine);
LOG.error("Parse error [1] occurred in lines [2].\n[1] ERROR:\n{}\n[2] LINE:\n{}",
msg, wrongLine);
throw e;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cz.cvut.sforms.Vocabulary;
import cz.cvut.sforms.model.Question;
import cz.cvut.spipes.util.QueryUtils;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.rdf.model.*;
Expand Down Expand Up @@ -53,7 +54,7 @@ public static Query parse(Question q, Model m) {
}

private static <T extends org.topbraid.spin.model.Query> String getFromQuery(Resource r, Class<T> resClass) {
Query q = ARQFactory.get().createQuery(r.as(resClass));
Query q = QueryUtils.createQuery(r.as(resClass));
Model m = r.getModel();
PrefixMapping mapping = new PrefixMappingImpl();
mapping.setNsPrefixes(m.getNsPrefixMap());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public ExecutionContext executeSelf() {
String queryStr = spinConstructRes.getProperty(SP.text).getLiteral().getString();
query = QueryFactory.create(substituteQueryMarkers(count, queryStr));
} else {
query = ARQFactory.get().createQuery(spinConstructRes);
query = QueryUtils.createQuery(spinConstructRes);
}

Model queriedModel = QueryUtils.execConstruct(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public ExecutionContext executeSelf() {
String queryStr = spinConstructRes.getProperty(SP.text).getLiteral().getString();
query = QueryFactory.create(substituteQueryMarkers(count, queryStr));
} else {
query = ARQFactory.get().createQuery(spinConstructRes);
query = QueryUtils.createQuery(spinConstructRes);
}

Model constructedModel = QueryUtils.execConstruct(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import cz.cvut.spipes.recursion.CombinedQueryTemplateRecursionProvider;
import cz.cvut.spipes.recursion.QueryTemplateRecursionProvider;
import cz.cvut.spipes.recursion.ScrollableCursorProvider;
import cz.cvut.spipes.util.QueryUtils;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
Expand Down Expand Up @@ -74,7 +75,7 @@ public void setSelectQuery(Select selectQuery) {
}

private ResultSet getSelectQueryResultSet() {
Query query = ARQFactory.get().createQuery(selectQuery);
Query query = QueryUtils.createQuery(selectQuery);

QuerySolution inputBindings = executionContext.getVariablesBinding().asQuerySolution();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void setSelectQuery(Select selectQuery) {
}

public void initializeQuery() {
Query query = ARQFactory.get().createQuery(selectQuery);
Query query = QueryUtils.createQuery(selectQuery);

QuerySolution inputBindings = executionContext.getVariablesBinding().asQuerySolution();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import cz.cvut.spipes.modules.annotations.SPipesModule;
import cz.cvut.spipes.registry.StreamResource;
import cz.cvut.spipes.registry.StreamResourceRegistry;
import cz.cvut.spipes.util.QueryUtils;
import org.apache.jena.ext.com.google.common.io.Files;
import org.apache.jena.query.Query;
import org.apache.jena.rdf.model.Model;
Expand Down Expand Up @@ -102,7 +103,7 @@ public ExecutionContext executeSelf() {
for (Resource constructQueryRes : constructQueries) {
Construct spinConstructRes = constructQueryRes.as(Construct.class);

Query query = ARQFactory.get().createQuery(spinConstructRes);
Query query = QueryUtils.createQuery(spinConstructRes);

try {
// save string query to temporary file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import cz.cvut.spipes.engine.ExecutionContext;
import cz.cvut.spipes.modules.annotations.SPipesModule;
import cz.cvut.spipes.modules.constants.Termit;
import cz.cvut.spipes.util.QueryUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
Expand Down Expand Up @@ -91,7 +92,7 @@ ExecutionContext executeSelf() {
return executionContext;
}

Query query = ARQFactory.get().createQuery(selectQuery);
Query query = QueryUtils.createQuery(selectQuery);
try (QueryExecution queryExecution = QueryExecutionFactory.create(query, inputModel)) {
ResultSet resultSet = queryExecution.execSelect();
List<RDFNode> listOfObjects = new ArrayList<>();
Expand Down

0 comments on commit cf87630

Please sign in to comment.