예제 #1
0
 private SWRLIndividualObject makeIndividalObject(final Node node) throws URISyntaxException {
   if (node.isVariable()) {
     final SWRLIndividualVariable var =
         swrlFactory.createIndividualVariable(new URI(varNS + node.getName()));
     vars.put(node.toString(), var);
     return var;
   }
   final OWLIndividual ind = owlModel.createIndividual(null, new URI(node.getURI()));
   return swrlFactory.wrapIndividual(ind);
 }
예제 #2
0
 private SWRLDataObject makeDataObject(final Node node) throws URISyntaxException {
   if (node.isVariable()) {
     final SWRLDataVariable var =
         swrlFactory.createDataVariable(new URI(varNS + node.getName()));
     vars.put(node.toString(), var);
     return var;
   }
   final OWLDataValue value =
       new OWLDataValueImpl(new LiteralImpl(node, (EnhGraph) owlModel.getImplementation()));
   return swrlFactory.wrapDataValue(value);
 }
예제 #3
0
    /* @see com.hp.hpl.jena.sparql.syntax.ElementVisitorBase#visit(com.hp.hpl.jena.sparql.syntax.ElementTriplesBlock) */
    @Override
    public void visit(final ElementTriplesBlock elementtriplesblock) {
      for (final Triple t : elementtriplesblock.getPattern()) {
        try {
          if (t.getPredicate().isVariable())
            throw new IllegalArgumentException(
                "Variables cannot be used in predicate position in ABoxQuery");

          Atom atom = null;
          final String predURI = t.getPredicate().getURI();
          if (RDF.type.getURI().equals(predURI)) {
            if (t.getObject().isVariable())
              throw new IllegalArgumentException(
                  "Variables cannot be used as objects of rdf:type triples in ABoxQuery");

            final OWLClass c = owlModel.createClass(new URI(t.getObject().getURI()));
            final SWRLIndividualObject arg = makeIndividalObject(t.getSubject());

            atom = swrlFactory.createClassAtom(c, arg);
          } else {
            final OWLProperty p = owlModel.getProperty(new URI(predURI));

            if (p == null)
              throw new IllegalArgumentException(
                  predURI + " is unknown [Object|Datatype]Property in the backing OWL model.");
            else if (p.isDatatypeProperty()) {
              final OWLDataProperty dp = owlModel.createDataProperty(p.getURI());
              final SWRLIndividualObject arg1 = makeIndividalObject(t.getSubject());
              final SWRLDataObject arg2 = makeDataObject(t.getObject());

              atom = swrlFactory.createDataPropertyAtom(dp, arg1, arg2);
            } else if (p.isObjectProperty()) {
              final OWLObjectProperty op = owlModel.createObjectProperty(p.getURI());
              final SWRLIndividualObject arg1 = makeIndividalObject(t.getSubject());
              final SWRLIndividualObject arg2 = makeIndividalObject(t.getObject());

              atom = swrlFactory.createIndividualPropertyAtom(op, arg1, arg2);
            }
            // else it could be a annotation property, which are not relevant anyway
          }

          if (atom != null) atoms = atoms.cons(atom);
        } catch (final URISyntaxException e) {
          throw new IllegalArgumentException(
              e.getInput() + " appearing in the query string is not a valid URI!");
        }
      }
    }
예제 #4
0
 ElementVisitor(
     final OWLModel model, final ISWRLFactory swrlFactory, final Map<String, Variable> vars) {
   this.owlModel = model;
   this.swrlFactory = swrlFactory;
   this.vars = vars;
   this.atoms = swrlFactory.createList();
 }
예제 #5
0
    private SWRLDataObject makeDataObject(final Expr expr) throws URISyntaxException {
      if (expr.isVariable()) {
        return swrlFactory.createDataVariable(new URI(varNS + expr.getVarName()));
      } else if (expr.isConstant()) {
        final NodeValue nv = (NodeValue) expr;
        OWLDataValue value;
        if (nv.isInteger()) value = owlModel.createDataValue(nv.getInteger());
        else if (nv.isDouble()) value = owlModel.createDataValue(Double.valueOf(nv.getDouble()));
        else if (nv.isFloat()) value = owlModel.createDataValue(Float.valueOf(nv.getFloat()));
        else if (nv.isBoolean()) value = owlModel.createDataValue(Boolean.valueOf(nv.getBoolean()));
        else if (nv.isString()) value = owlModel.createDataValue(nv.getString());
        else if (nv.isDecimal()) value = owlModel.createDataValue(nv.getDecimal());
        else if (nv.hasDateTime()) value = owlModel.createDataValue(nv.getDateTime());
        else /* if (nv.isLiteral()) */ throw new NotImplementedException();

        return swrlFactory.wrapDataValue(value);
      } else {
        throw new IllegalArgumentException(
            "Nested constraint (filter) near " + expr + " can not be transformed to SWRL atom.");
      }
    }
예제 #6
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.");
      }
    }
예제 #7
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!");
      }
    }