/**
     * Adds a handlesMimetypes restriction to the query.
     *
     * <p>Note that all mimetypes added using the methods {@link #addHandlesMimetype(String...)},
     * {@link #addHandlesMimetypeWildcard(String...)}, {@link #addHandlesMimetypes(String, String)}
     * and {@link #addHandlesMimetypesWildcard(String, String)} will be concatenated using UNION.
     *
     * @param leftMimetype the left mimetype
     * @param rightMimetype the right mimetype
     * @return this query
     */
    public ComponentQuery addHandlesMimetypes(String leftMimetype, String rightMimetype) {
      if (leftMimetype != null
          && !leftMimetype.isEmpty()
          && rightMimetype != null
          && !rightMimetype.isEmpty()) {
        Node node = NodeFactory.createAnon();
        ElementGroup group = new ElementGroup();
        group.addTriplePattern(
            new Triple(wfNode, NodeFactory.createURI(ONTOLOGY_IRI + "handlesMimetypes"), node));
        group.addTriplePattern(
            new Triple(
                node,
                NodeFactory.createURI(TYPE_IRI),
                NodeFactory.createURI(ONTOLOGY_IRI + "AcceptedMimetypes")));
        group.addTriplePattern(
            new Triple(
                node,
                NodeFactory.createURI(ONTOLOGY_IRI + "handlesLeftMimetype"),
                NodeFactory.createLiteral(leftMimetype)));
        group.addTriplePattern(
            new Triple(
                node,
                NodeFactory.createURI(ONTOLOGY_IRI + "handlesRightMimetype"),
                NodeFactory.createLiteral(rightMimetype)));

        handlesMimetypes.addElement(group);
      }
      return this;
    }
Example #2
0
  @Test
  public void testQueryAddValuesInQueryPattern() {
    List<Var> header = vars("a", "b");
    Binding b1 = binding(header, "1", "2");
    Binding b2 = binding(header, "3", "4");

    Query q = QueryFactory.create("SELECT * {}");
    ElementGroup group = new ElementGroup();
    ElementData table = new ElementData();
    table.add(Var.alloc("a"));
    table.add(Var.alloc("b"));
    table.add(b1);
    table.add(b2);
    group.addElement(q.getQueryPattern());
    group.addElement(table);
    q.setQueryPattern(group);
    ResultSet rs = QueryExecutionFactory.create(q, ModelFactory.createDefaultModel()).execSelect();

    assertEquals(Arrays.asList(new String[] {"a", "b"}), rs.getResultVars());
    assertTrue(rs.hasNext());
    assertEquals(b1, rs.nextBinding());
    assertTrue(rs.hasNext());
    assertEquals(b2, rs.nextBinding());
    assertFalse(rs.hasNext());
  }
Example #3
0
  public static Model exec(Model model, final Table table, Query query) throws IOException {
    OntModel inferencedModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
    ElementData tableElementData =
        new ElementData() {
          @Override
          public Table getTable() {
            return table;
          }
        };
    for (Var var : table.getVars()) {
      tableElementData.add(var);
    }
    ElementGroup elementGroup = new ElementGroup();
    elementGroup.addElement(tableElementData);
    if (query.getQueryPattern() instanceof ElementGroup) {
      for (Element element : ((ElementGroup) query.getQueryPattern()).getElements()) {
        elementGroup.addElement(element);
      }
    } else {
      elementGroup.addElement(query.getQueryPattern());
    }
    query.setQueryPattern(elementGroup);

    //        QueryExecution ex = QueryExecutionFactory.create(query, model);
    QueryExecution ex = ARQFactory.get().createQueryExecution(query, model);
    if (query.isConstructType()) {
      ex.execConstruct(inferencedModel);
    } else {
      inferencedModel.add(ex.execSelect().getResourceModel());
    }
    return inferencedModel;
  }
 /** Finishes the handles mimetype filter */
 private void finishHandlesMimetypes() {
   if (handlesMimetypes != null && !handlesMimetypes.getElements().isEmpty()) {
     if (handlesMimetypes.getElements().size() > 1) {
       query.addElement(handlesMimetypes);
     } else {
       query.addElement(handlesMimetypes.getElements().get(0));
     }
   }
 }
 /** Finishes the source migration path filter. */
 private void finishMigrationPathSource() {
   if (migrationPathSource != null && !migrationPathSource.getElements().isEmpty()) {
     if (migrationPathSource.getElements().size() > 1) {
       query.addElement(migrationPathSource);
     } else {
       query.addElement(migrationPathSource.getElements().get(0));
     }
   }
 }
Example #6
0
 /**
  * Fetches all elements of the optional, i.e., of the OPTIONAL clause. query
  *
  * @param query Input query
  * @return List of elements from the OPTIONAL clause if there is one, else null
  */
 private static List<Element> getOptionalElements(Query query) {
   ElementGroup elt = (ElementGroup) query.getQueryPattern();
   for (int i = 0; i < elt.getElements().size(); i++) {
     Element e = elt.getElements().get(i);
     if (e instanceof ElementOptional) {
       return ((ElementGroup) ((ElementOptional) e).getOptionalElement()).getElements();
     }
   }
   return new ArrayList<Element>();
 }
Example #7
0
 /**
  * Fetches all elements of the query body, i.e., of the WHERE clause of a query
  *
  * @param query Input query
  * @return List of elements from the WHERE clause
  */
 private static List<Element> getWhereElements(Query query) {
   List<Element> result = new ArrayList<Element>();
   ElementGroup elt = (ElementGroup) query.getQueryPattern();
   for (int i = 0; i < elt.getElements().size(); i++) {
     Element e = elt.getElements().get(i);
     if (!(e instanceof ElementOptional)) {
       result.add(e);
     }
   }
   return result;
 }
 /**
  * Adds an output port type restriction to the query.
  *
  * @param provides the port type
  * @return this query
  */
 public ComponentQuery addOutputPort(String provides) {
   if (provides != null && !provides.isEmpty()) {
     Node node = NodeFactory.createAnon();
     ElementGroup group = new ElementGroup();
     group.addTriplePattern(
         new Triple(wfNode, NodeFactory.createURI(WFDESC_IRI + "hasOutput"), node));
     group.addTriplePattern(
         new Triple(
             node,
             NodeFactory.createURI(ONTOLOGY_IRI + "provides"),
             NodeFactory.createURI(provides)));
     query.addElement(group);
   }
   return this;
 }
 /** Finishes the dependency label filter. */
 private void finishDependencyLabelFilter() {
   if (dependencyLabelPattern != null && !dependencyLabelPattern.isEmpty()) {
     Node processNode = NodeFactory.createAnon();
     Node installationNode = NodeFactory.createAnon();
     Node dependencyNode = NodeFactory.createAnon();
     Node dependencyLabel = NodeFactory.createVariable("dependencyLabel");
     ElementGroup group = new ElementGroup();
     group.addTriplePattern(
         new Triple(wfNode, NodeFactory.createURI(WFDESC_IRI + "hasSubProcess"), processNode));
     group.addTriplePattern(
         new Triple(
             processNode,
             NodeFactory.createURI(ONTOLOGY_IRI + "requiresInstallation"),
             installationNode));
     group.addTriplePattern(
         new Triple(
             installationNode,
             NodeFactory.createURI(ONTOLOGY_IRI + "dependsOn"),
             dependencyNode));
     group.addTriplePattern(
         new Triple(dependencyNode, NodeFactory.createURI(SKOS_LABEL), dependencyLabel));
     query.addElement(group);
     ElementFilter filter =
         new ElementFilter(
             new E_StrContains(
                 new ExprVar(dependencyLabel), new NodeValueString(dependencyLabelPattern)));
     query.addElementFilter(filter);
   }
 }
  protected ElementGroup getWhereClauseWithBlankNodeFilter(Triple triplePattern) {

    Node s = triplePattern.getSubject();
    Node o = triplePattern.getObject();

    ElementGroup whereClause = new ElementGroup();
    whereClause.addTriplePattern(triplePattern);
    if (s.isVariable()) {
      whereClause.addElementFilter(
          new ElementFilter(new E_LogicalNot(new E_IsBlank(new ExprVar(s)))));
    }
    if (o.isVariable()) {
      whereClause.addElementFilter(
          new ElementFilter(new E_LogicalNot(new E_IsBlank(new ExprVar(o)))));
    }

    return whereClause;
  }
 /**
  * Adds a handlesMimetype restriction to the query.
  *
  * <p>Note that all mimetypes added using the methods {@link #addHandlesMimetype(String...)},
  * {@link #addHandlesMimetypeWildcard(String...)}, {@link #addHandlesMimetypes(String, String)}
  * and {@link #addHandlesMimetypesWildcard(String, String)} will be concatenated using UNION.
  *
  * @param mimetypes the mimetypes
  * @return this query
  */
 public ComponentQuery addHandlesMimetype(String... mimetypes) {
   if (mimetypes != null && mimetypes.length > 0) {
     ElementGroup elements = new ElementGroup();
     Set<String> mimeset = new HashSet<String>();
     Collections.addAll(mimeset, mimetypes);
     for (String mimetype : mimeset) {
       if (mimetype != null) {
         elements.addTriplePattern(
             new Triple(
                 wfNode,
                 NodeFactory.createURI(ONTOLOGY_IRI + "handlesMimetype"),
                 NodeFactory.createLiteral(mimetype)));
       }
     }
     handlesMimetypes.addElement(elements);
   }
   return this;
 }
 /**
  * Adds a migration path source restriction to the query.
  *
  * <p>Note that all mimetypes added using the methods {@link #addMigrationPath(String)}, {@link
  * #addMigrationPathWildcard(String)} will be concatenated using UNION.
  *
  * @param sourceMimetype the source mimetype
  * @return this query
  */
 public ComponentQuery addMigrationPath(String sourceMimetype) {
   if (sourceMimetype != null && !sourceMimetype.isEmpty()) {
     Node node = NodeFactory.createAnon();
     ElementGroup group = new ElementGroup();
     group.addTriplePattern(
         new Triple(wfNode, NodeFactory.createURI(ONTOLOGY_IRI + "migrates"), node));
     group.addTriplePattern(
         new Triple(
             node,
             NodeFactory.createURI(TYPE_IRI),
             NodeFactory.createURI(ONTOLOGY_IRI + "MigrationPath")));
     group.addTriplePattern(
         new Triple(
             node,
             NodeFactory.createURI(ONTOLOGY_IRI + "sourceMimetype"),
             NodeFactory.createLiteral(sourceMimetype)));
     migrationPathSource.addElement(group);
   }
   return this;
 }
 /**
  * Adds a profile restriction to the query.
  *
  * @param profile the profile
  * @return this query
  */
 public ComponentQuery addProfile(String profile) {
   if (profile != null && !profile.isEmpty()) {
     Triple t =
         new Triple(
             wfNode,
             NodeFactory.createURI(ONTOLOGY_IRI + "fits"),
             NodeFactory.createURI(profile));
     query.addTriplePattern(t);
   }
   return this;
 }
 /**
  * Adds an environment restriction to the query.
  *
  * @param environment the environment
  * @return this query
  */
 public ComponentQuery addInstallationEnvironment(String environment) {
   if (environment != null && !environment.isEmpty()) {
     Node processNode = NodeFactory.createAnon();
     Node installationNode = NodeFactory.createAnon();
     ElementGroup group = new ElementGroup();
     group.addTriplePattern(
         new Triple(wfNode, NodeFactory.createURI(WFDESC_IRI + "hasSubProcess"), processNode));
     group.addTriplePattern(
         new Triple(
             processNode,
             NodeFactory.createURI(ONTOLOGY_IRI + "requiresInstallation"),
             installationNode));
     group.addTriplePattern(
         new Triple(
             installationNode,
             NodeFactory.createURI(ONTOLOGY_IRI + "hasEnvironment"),
             NodeFactory.createURI(environment)));
     query.addElement(group);
   }
   return this;
 }
 /**
  * Adds a measure output port restriction to the query.
  *
  * @param relatedObject the object related to the measures
  * @param measure the measure
  * @return this query
  */
 public ComponentQuery addMeasureOutputPort(String relatedObject, String measure) {
   if (measure != null && !measure.isEmpty()) {
     Node node = NodeFactory.createAnon();
     ElementGroup group = new ElementGroup();
     group.addTriplePattern(
         new Triple(wfNode, NodeFactory.createURI(WFDESC_IRI + "hasOutput"), node));
     if (relatedObject != null && !relatedObject.isEmpty()) {
       group.addTriplePattern(
           new Triple(
               node,
               NodeFactory.createURI(ONTOLOGY_IRI + "relatesTo"),
               NodeFactory.createURI(ONTOLOGY_IRI + relatedObject)));
     }
     group.addTriplePattern(
         new Triple(
             node,
             NodeFactory.createURI(ONTOLOGY_IRI + "provides"),
             NodeFactory.createURI(measure)));
     query.addElement(group);
   }
   return this;
 }
 /** Finishes the migration path to filter. */
 private void finishMigrationPathFilter() {
   if (migrationPathTargetPattern != null && !migrationPathTargetPattern.isEmpty()) {
     Node migrationPath = NodeFactory.createAnon();
     Node toMimetype = NodeFactory.createVariable("migrationPathTarget");
     ElementGroup group = new ElementGroup();
     group.addTriplePattern(
         new Triple(wfNode, NodeFactory.createURI(ONTOLOGY_IRI + "migrates"), migrationPath));
     group.addTriplePattern(
         new Triple(
             migrationPath,
             NodeFactory.createURI(TYPE_IRI),
             NodeFactory.createURI(ONTOLOGY_IRI + "MigrationPath")));
     group.addTriplePattern(
         new Triple(
             migrationPath, NodeFactory.createURI(ONTOLOGY_IRI + "targetMimetype"), toMimetype));
     query.addElement(group);
     ElementFilter filter =
         new ElementFilter(
             new E_StrContains(
                 new ExprVar(toMimetype), new NodeValueString(migrationPathTargetPattern)));
     query.addElementFilter(filter);
   }
 }
 /**
  * Adds an element to the query.
  *
  * @param element the element to add
  * @return this query
  */
 public ComponentQuery addElement(Element element) {
   query.addElement(element);
   return this;
 }
 /* @see com.hp.hpl.jena.sparql.syntax.ElementVisitorBase#visit(com.hp.hpl.jena.sparql.syntax.ElementGroup) */
 @Override
 public void visit(final ElementGroup elementgroup) {
   for (final Element e : elementgroup.getElements()) {
     e.visit(this);
   }
 }