示例#1
0
  public NLGElement getNLFromSingleClause(Element e) {
    if (e instanceof ElementPathBlock) {
      ElementPathBlock epb = (ElementPathBlock) e;
      List<Triple> triples = new ArrayList<Triple>();

      // get all triples. We assume that the depth of union is always 1
      for (TriplePath tp : epb.getPattern().getList()) {
        Triple t = tp.asTriple();
        triples.add(t);
      }
      return getNLForTripleList(triples, "and");
    } // if clause is union clause then we generate or statements
    else if (e instanceof ElementUnion) {
      CoordinatedPhraseElement cpe;
      // cast to union
      ElementUnion union = (ElementUnion) e;
      List<Triple> triples = new ArrayList<Triple>();

      // get all triples. We assume that the depth of union is always 1
      for (Element atom : union.getElements()) {
        ElementPathBlock epb = ((ElementPathBlock) (((ElementGroup) atom).getElements().get(0)));
        if (!epb.isEmpty()) {
          Triple t = epb.getPattern().get(0).asTriple();
          triples.add(t);
        }
      }
      return getNLForTripleList(triples, "or");
    } // if it's a filter
    else if (e instanceof ElementFilter) {
      SPhraseSpec p = nlgFactory.createClause();
      ElementFilter filter = (ElementFilter) e;
      Expr expr = filter.getExpr();
      return getNLFromSingleExpression(expr);
    }
    return null;
  }
    /* @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!");
      }
    }