Java Code Examples for org.apache.jena.sparql.expr.NodeValue#isLiteral()

The following examples show how to use org.apache.jena.sparql.expr.NodeValue#isLiteral() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FUN_GeoJSONGeometry.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
@Override
public NodeValue exec(NodeValue json) {
    if (!json.isLiteral()) {
        LOG.debug("The argument should be a literal. Got" + json);
        throw new ExprEvalException("The argument should be a literal. Got" + json);
    }
    String s = json.getNode().getLiteralLexicalForm();
    Feature feature = gson.fromJson(s, Feature.class);
    return getNodeValue(feature.geometry());
}
 
Example 2
Source File: FUN_regex.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
@Override
public NodeValue exec(NodeValue stringValue, NodeValue regex, NodeValue locationV) {
    if (!stringValue.isLiteral()) {
        LOG.debug("First argument must be a literal, got: " + stringValue);
        throw new ExprEvalException("First argument must be a literal, got: " + stringValue);
    }
    String string = stringValue.asNode().getLiteralLexicalForm();

    if (!regex.isString()) {
        LOG.debug("Second argument must be a string, got: " + regex);
        throw new ExprEvalException("Second argument must be a string, got: " + regex);
    }
    String regexString = regex.asString();
    Pattern pattern;
    try {
        pattern = Pattern.compile(regexString, Pattern.MULTILINE);
    } catch(Exception ex) {
        LOG.debug("Exception while compiling regex string " + regexString, ex);
        throw new ExprEvalException("Exception while compiling regex string " + regexString, ex);
    }

    if (!locationV.isInteger()) {
        LOG.debug("Third argument must be an integer, got: " + locationV);
        throw new ExprEvalException("Third argument must be an integer, got: " + locationV);
    }

    int location = locationV.getInteger().intValue();

    Matcher matcher = pattern.matcher(string);

    if (matcher.find()) {
    	return new NodeValueString(matcher.group(location));
    }
    throw new ExprEvalException("The regex did not match on " +stringValue + " ( regex was " + regex);
}
 
Example 3
Source File: TemplatePlan.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
private String getExprEval(Expr expr, Binding binding, Context context, FunctionEnv env) {
	NodeValue nv = null;
	try {
		nv = expr.eval(binding, env);
	} catch (ExprEvalException ex) {
		LOG.debug("Could not evaluate expression " + expr, ex);
	}
	if (nv == null) {
		return "";
	}
	if (!nv.isLiteral()) {
		LOG.debug("Expression did not evaluate as a literal " + expr + ", got :" + nv);
	}
	return nv.asNode().getLiteralLexicalForm();
}
 
Example 4
Source File: CustomAggregate.java    From xcurator with Apache License 2.0 5 votes vote down vote up
/** Function called on each row in a group */
@Override
public void accumulate(Binding binding, FunctionEnv functionEnv) {
    ExprList exprList = agg.getExprList() ;
    for(Expr expr: exprList) {
        try {
            NodeValue nv = expr.eval(binding, functionEnv) ;
            // Evaluation succeeded.
            if ( nv.isLiteral())
                count ++ ;
        } catch (ExprEvalException ex) {}
    }
}
 
Example 5
Source File: ST_Call_Template.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
/**
   *
   * @param binding
   * @param args
   * @param uri
   * @param env
   * @return
   */
  @Override
  public NodeValue exec(
          final Binding binding,
          final ExprList args,
          final String uri,
          final FunctionEnv env) {
      if (args == null) {
          throw new ARQInternalErrorException("FunctionBase: Null args list");
      }
      if (args.size() < 1) {
          throw new ExprEvalException("Expecting at least one argument");
      }
      NodeValue queryNode = args.get(0).eval(binding, env);
      if (!(queryNode.isIRI() || queryNode.isLiteral() && SPARQLExt.MEDIA_TYPE_URI.equals(queryNode.getDatatypeURI()))) {
          throw new ExprEvalException("Name of sub query "
                  + "should be a URI or a literal with datatype " + SPARQLExt.MEDIA_TYPE_URI + ". Got: " + queryNode);
      }
      if (queryNode.isLiteral() && args.size() > 1) {
          throw new ExprEvalException("Expecting at most one argument when first argument is a literal.");
      }

      final Context context = env.getContext();
      final QueryExecutor queryExecutor = ContextUtils.getQueryExecutor(context);
      try(ByteArrayOutputStream baos = new ByteArrayOutputStream();
      		IndentedWriter writer = new IndentedWriter(baos);) {
      	Context newContext = ContextUtils.fork(context).setTemplateOutput(writer).fork();
          if (queryNode.isIRI()) {
              String queryName = queryNode.asNode().getURI();
              List<List<Node>> callParameters = new ArrayList<>();
              callParameters.add(EvalUtils.eval(args.subList(1, args.size()), binding, env));
              queryExecutor.execTemplateFromName(queryName, callParameters, newContext);
              return new NodeValueString(new String(baos.toByteArray()));
          }
          String queryString = queryNode.asNode().getLiteralLexicalForm();
      	queryExecutor.execTemplateFromString(queryString, binding, newContext);
          String result = new String(baos.toByteArray());
       writer.close(); // need to flush. One before the other isn't important. boas.close is the only variant that doesn't work.
          return new NodeValueString(result);
      } catch (IOException ex) {
      	throw new ExprEvalException(ex);
}
  }
 
Example 6
Source File: FUN_Select_Call_Template.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
/**
   *
   * @param binding
   * @param args
   * @param uri
   * @param env
   * @return
   */
  @Override
  public NodeValue exec(
          final Binding binding,
          final ExprList args,
          final String uri,
          final FunctionEnv env) {
      if (args == null) {
          throw new ARQInternalErrorException("FunctionBase: Null args list");
      }
      if (args.size() < 2) {
          throw new ExprEvalException("Expecting at least two arguments");
      }
      NodeValue selectQueryNode = null;
      if(args.get(0) != null) {
      	selectQueryNode = args.get(0).eval(binding, env);
          if (!(selectQueryNode.isLiteral() && SPARQLExt.MEDIA_TYPE_URI.equals(selectQueryNode.getDatatypeURI()))) {
              throw new ExprEvalException("First argument must be a literal with datatype " + SPARQLExt.MEDIA_TYPE_URI + ". Got: " + selectQueryNode);
          }
      }
      NodeValue templateQueryNode = args.get(1).eval(binding, env);
      if (!(templateQueryNode.isIRI())) {
          throw new ExprEvalException("Second argument must be a URI. Got: " + templateQueryNode);
      }
      ExprList callArgs = args.subList(2, args.size());

      final Context context = env.getContext();
      final QueryExecutor queryExecutor = ContextUtils.getQueryExecutor(context);

      final List<List<Node>> callParameters = new ArrayList<>();
      if(selectQueryNode != null) {
       RootPlan selectPlan = queryExecutor.getPlanFromString(selectQueryNode.asString(), null);
       List<Binding> bindings = new ArrayList<>();
       bindings.add(binding);
       ResultSet resultSet = selectPlan.execSelect(bindings, context);
       
       for(;resultSet.hasNext();) {
           Binding newBinding = resultSet.nextBinding();
           callParameters.add(EvalUtils.eval(callArgs, newBinding, env));
       }
      } else {
          callParameters.add(EvalUtils.eval(callArgs, binding, env));
      }
      String templateQueryName = templateQueryNode.asNode().getURI();
      try(ByteArrayOutputStream baos = new ByteArrayOutputStream();
      		IndentedWriter writer = new IndentedWriter(baos);) {
       Context newContext = ContextUtils.fork(context).setTemplateOutput(writer).fork();
       queryExecutor.execTemplateFromName(templateQueryName, callParameters, newContext);
       String result = new String(baos.toByteArray());
       writer.close(); // need to flush. One before the other isn't important. boas.close is the only variant that doesn't work.
       return new NodeValueString(result);
      } catch (IOException ex) {
      	throw new ExprEvalException(ex);
}

  }