Exemple #1
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());
  }
Exemple #2
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 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));
     }
   }
 }
 /** 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));
     }
   }
 }
 /**
  * Adds an environment restriction to the query.
  *
  * @param environmentClass the environment class
  * @return this query
  */
 public ComponentQuery addInstallationEnvironmentType(String environmentClass) {
   if (environmentClass != null && !environmentClass.isEmpty()) {
     Node processNode = NodeFactory.createAnon();
     Node installationNode = NodeFactory.createAnon();
     Node environmentNode = 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"),
             environmentNode));
     group.addTriplePattern(
         new Triple(
             environmentNode,
             NodeFactory.createURI(TYPE_IRI),
             NodeFactory.createURI(environmentClass)));
     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);
   }
 }
 /**
  * Adds a migration path restriction to the query.
  *
  * @param sourceMimetype the source mimetype
  * @param targetMimetype the target mimetype
  * @return this query
  */
 public ComponentQuery addMigrationPath(String sourceMimetype, String targetMimetype) {
   if ((sourceMimetype != null && !sourceMimetype.isEmpty())
       || (targetMimetype != null && !targetMimetype.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")));
     if (sourceMimetype != null && !sourceMimetype.isEmpty()) {
       group.addTriplePattern(
           new Triple(
               node,
               NodeFactory.createURI(ONTOLOGY_IRI + "sourceMimetype"),
               NodeFactory.createLiteral(sourceMimetype)));
     }
     if (targetMimetype != null && !targetMimetype.isEmpty()) {
       group.addTriplePattern(
           new Triple(
               node,
               NodeFactory.createURI(ONTOLOGY_IRI + "targetMimetype"),
               NodeFactory.createLiteral(targetMimetype)));
     }
     query.addElement(group);
   }
   return this;
 }
 /**
  * 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;
 }
 /**
  * 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;
 }