Exemplo n.º 1
0
    /* @see com.hp.hpl.jena.sparql.syntax.ElementVisitorBase#visit(com.hp.hpl.jena.sparql.syntax.ElementFilter) */
    @Override
    public void visit(final ElementFilter elementfilter) {
      final Expr expr = elementfilter.getExpr();
      final BuiltinAtom atom;
      try {
        if (expr instanceof Expression) // RDQL
        {
          final Expression e = (Expression) expr;
          final SWRLDataObject arg1 = makeDataObject(e.getArg(0));
          final SWRLDataObject arg2 = makeDataObject(e.getArg(1));

          if (e instanceof Q_Equal) atom = swrlFactory.createEqual(arg1, arg2);
          else if (e instanceof Q_NotEqual) atom = swrlFactory.createNotEqual(arg1, arg2);
          else if (e instanceof Q_GreaterThan) atom = swrlFactory.createGreaterThan(arg1, arg2);
          else if (e instanceof Q_GreaterThanOrEqual)
            atom = swrlFactory.createGreaterThanOrEqual(arg1, arg2);
          else if (e instanceof Q_LessThan) atom = swrlFactory.createLessThan(arg1, arg2);
          else if (e instanceof Q_LessThanOrEqual)
            atom = swrlFactory.createLessThanOrEqual(arg1, arg2);
          else
            throw new IllegalArgumentException(
                "Unsupported constraint expression " + e + " used in RDQL query.");
        } else if (expr.isFunction()) // SPARQL
        {
          final ExprFunction f = (ExprFunction) expr;
          final SWRLDataObject arg1 = makeDataObject(f.getArg(0));
          final SWRLDataObject arg2 = makeDataObject(f.getArg(1));

          if (f instanceof E_Equals) atom = swrlFactory.createEqual(arg1, arg2);
          else if (f instanceof E_NotEquals) atom = swrlFactory.createNotEqual(arg1, arg2);
          else if (f instanceof E_GreaterThan) atom = swrlFactory.createGreaterThan(arg1, arg2);
          else if (f instanceof E_GreaterThanOrEqual)
            atom = swrlFactory.createGreaterThanOrEqual(arg1, arg2);
          else if (f instanceof E_LessThan) atom = swrlFactory.createLessThan(arg1, arg2);
          else if (f instanceof E_LessThanOrEqual)
            atom = swrlFactory.createLessThanOrEqual(arg1, arg2);
          else
            throw new IllegalArgumentException(
                "Unsupported constraint (filter) " + f + " used in SPARQL query.");
        } else return;
        atoms = atoms.cons(atom);
      } catch (final URISyntaxException e) {
        throw new IllegalArgumentException(e.getInput() + " is not a valid URI!");
      }
    }
Exemplo n.º 2
0
    private SWRLDataObject makeDataObject(final Expression expr) throws URISyntaxException {
      if (expr.isVariable()) {
        return swrlFactory.createDataVariable(new URI(varNS + expr.getName()));
      } else if (expr.isConstant()) {
        OWLDataValue value = null;
        if (expr instanceof ParsedLiteral) {
          final ParsedLiteral lit = (ParsedLiteral) expr;
          if (lit.isInt()) value = owlModel.createDataValue(Long.valueOf(lit.getInt()));
          else if (lit.isDouble())
            value = owlModel.createDataValue(Double.valueOf(lit.getDouble()));
          else if (lit.isBoolean())
            value = owlModel.createDataValue(Boolean.valueOf(lit.getBoolean()));
          else if (lit.isString()) value = owlModel.createDataValue(lit.getString());
          else if (lit.isURI()) value = owlModel.createDataValue(URI.create(lit.getURI()));
          else /* if (lit.isNode()) */ throw new NotImplementedException();
        } else value = owlModel.createDataValue(expr.getValue());

        return swrlFactory.wrapDataValue(value);
      } else {
        throw new IllegalArgumentException(
            "Nested constraint expressions near " + expr + " can not be transformed to SWRL atom.");
      }
    }