private static MultiMap<Variable, Variable> surelyDifferent(DefaultRule rule) {
   Clause body = rule.antecedent();
   Clause head = rule.consequent();
   final MultiMap<Variable, Variable> different = new MultiMap<Variable, Variable>();
   for (Literal literal :
       Sugar.union(
           body.getLiteralsByPredicate(SpecialBinaryPredicates.NEQ),
           body.getLiteralsByPredicate(SpecialBinaryPredicates.GT),
           body.getLiteralsByPredicate(SpecialBinaryPredicates.LT),
           body.getLiteralsByPredicate(SpecialVarargPredicates.ALLDIFF),
           head.getLiteralsByPredicate(SpecialBinaryPredicates.NEQ),
           head.getLiteralsByPredicate(SpecialBinaryPredicates.GT),
           head.getLiteralsByPredicate(SpecialBinaryPredicates.LT),
           head.getLiteralsByPredicate(SpecialVarargPredicates.ALLDIFF))) {
     for (Term a : literal.terms()) {
       if (a instanceof Variable) {
         Variable v1 = (Variable) a;
         for (Term b : literal.terms()) {
           if (b instanceof Variable) {
             Variable v2 = (Variable) b;
             if (v1 != v2) {
               different.put(v1, v2);
             }
           }
         }
       }
     }
   }
   return different;
 }
  @Override
  public Expression<K> applyInternal(And<K> input) {

    for (Expression<K> expr : input.expressions) {
      if (expr instanceof Literal) {
        Literal l = (Literal) expr;

        if (l.getValue()) {
          return copyWithoutTrue(input);
        } else {
          return Literal.getFalse();
        }
      }

      //  succeed immediately if require something or its opposite
      if (expr instanceof Not) {
        Expression<K> notChild = ((Not<K>) expr).getE();
        for (Expression<K> child : input.expressions) {
          if (child.equals(notChild)) {
            return Literal.getFalse();
          }
        }
      }
    }

    return input;
  }
 public static Set<DefaultRule> selectNonisomorphicDefaultRules(
     Iterable<DefaultRule> defaultRules) {
   List<Clause> candidates = new ArrayList<Clause>();
   for (DefaultRule rule : defaultRules) {
     DefaultRule preprocessed = preprocess(rule);
     candidates.add(
         new Clause(
             Sugar.<Literal>iterable(
                 preprocessed.antecedent().literals(), preprocessed.consequent().literals())));
   }
   Matching m = new Matching();
   Set<DefaultRule> retVal = new HashSet<DefaultRule>();
   for (Clause c : m.nonisomorphic(candidates)) {
     List<Literal> head = new ArrayList<Literal>();
     List<Literal> body = new ArrayList<Literal>();
     for (Literal l : c.literals()) {
       Literal newLiteral =
           new Literal(
               l.predicate().substring(l.predicate().indexOf(":") + 1), l.isNegated(), l.arity());
       for (int i = 0; i < l.arity(); i++) {
         newLiteral.set(l.get(i), i);
       }
       if (l.predicate().startsWith("antecedent:")
           || l.predicate().startsWith(SymmetricPredicates.PREFIX + "antecedent:")) {
         body.add(newLiteral);
       } else {
         head.add(newLiteral);
       }
     }
     retVal.add(new DefaultRule(new Clause(body), new Clause(head)));
   }
   return retVal;
 }
  /**
   * Query SPARQL endpoint with a SELECT query
   *
   * @param qExec QueryExecution encapsulating the query
   * @return model retrieved by querying the endpoint
   */
  private Model getSelectModel(QueryExecution qExec) {
    Model model = ModelFactory.createDefaultModel();
    Graph graph = model.getGraph();
    ResultSet results = qExec.execSelect();

    while (results.hasNext()) {
      QuerySolution sol = results.next();
      String subject;
      String predicate;
      RDFNode object;

      try {
        subject = sol.getResource("s").toString();
        predicate = sol.getResource("p").toString();
        object = sol.get("o");
      } catch (NoSuchElementException e) {
        logger.error("SELECT query does not return a (?s ?p ?o) Triple");
        continue;
      }

      Node objNode;
      if (object.isLiteral()) {
        Literal obj = object.asLiteral();
        objNode = NodeFactory.createLiteral(obj.getString(), obj.getDatatype());
      } else {
        objNode = NodeFactory.createLiteral(object.toString());
      }

      graph.add(
          new Triple(NodeFactory.createURI(subject), NodeFactory.createURI(predicate), objNode));
    }

    return model;
  }
Example #5
0
 /** Return a list of all tests of the given type, according to the current filters */
 public List<Resource> findTestsOfType(Resource testType) {
   ArrayList<Resource> result = new ArrayList<>();
   StmtIterator si = testDefinitions.listStatements(null, RDF.type, testType);
   while (si.hasNext()) {
     Resource test = si.nextStatement().getSubject();
     boolean accept = true;
     // Check test status
     Literal status = (Literal) test.getProperty(RDFTest.status).getObject();
     if (approvedOnly) {
       accept = status.getString().equals(STATUS_FLAGS[0]);
     } else {
       accept = false;
       for (String STATUS_FLAG : STATUS_FLAGS) {
         if (status.getString().equals(STATUS_FLAG)) {
           accept = true;
           break;
         }
       }
     }
     // Check for blocked tests
     for (String BLOCKED_TEST : BLOCKED_TESTS) {
       if (BLOCKED_TEST.equals(test.toString())) {
         accept = false;
       }
     }
     // End of filter tests
     if (accept) {
       result.add(test);
     }
   }
   return result;
 }
 public static Literal allDiffLiteral(Clause c) {
   Literal l = new Literal(SpecialVarargPredicates.ALLDIFF, c.variables().size());
   int i = 0;
   for (Variable v : c.variables()) {
     l.set(v, i++);
   }
   return l;
 }
 public void flushCache() {
   super.flushCache();
   constant_computed = false;
   constant_value = null;
   type_computed = false;
   type_value = null;
 }
      @Override
      public Query apply(Function input, Context context) throws IOException {
        Tuple<Reference, Literal> tuple = prepare(input);
        if (tuple == null) {
          return null;
        }
        String field = tuple.v1().info().ident().columnIdent().fqn();
        Literal literal = tuple.v2();
        CollectionType dataType = ((CollectionType) literal.valueType());

        Set values = (Set) literal.value();
        DataType innerType = dataType.innerType();
        BytesRef[] terms = getBytesRefs(values, TermBuilder.forType(innerType));
        TermsFilter termsFilter = new TermsFilter(field, terms);
        return new FilteredQuery(Queries.newMatchAllQuery(), termsFilter);
      }
 private TermsFilter termsFilter(String columnName, Literal arrayLiteral) {
   TermsFilter termsFilter;
   Object values = arrayLiteral.value();
   if (values instanceof Collection) {
     termsFilter =
         new TermsFilter(
             columnName,
             getBytesRefs((Collection) values, TermBuilder.forType(arrayLiteral.valueType())));
   } else {
     termsFilter =
         new TermsFilter(
             columnName,
             getBytesRefs((Object[]) values, TermBuilder.forType(arrayLiteral.valueType())));
   }
   return termsFilter;
 }
Example #10
0
 @Override
 protected Query applyArrayReference(
     Reference arrayReference, Literal literal, Context context) throws IOException {
   QueryBuilderHelper builder =
       QueryBuilderHelper.forType(((CollectionType) arrayReference.valueType()).innerType());
   return builder.eq(arrayReference.ident().columnIdent().fqn(), literal.value());
 }
Example #11
0
 @Nullable
 private Function rewriteAndValidateFields(Function function, Context context) {
   if (function.arguments().size() == 2) {
     Symbol left = function.arguments().get(0);
     Symbol right = function.arguments().get(1);
     if (left.symbolType() == SymbolType.REFERENCE && right.symbolType().isValueSymbol()) {
       Reference ref = (Reference) left;
       if (ref.info().ident().columnIdent().equals(DocSysColumns.ID)) {
         function.setArgument(
             0,
             new Reference(DocSysColumns.forTable(ref.ident().tableIdent(), DocSysColumns.UID)));
         function.setArgument(
             1,
             Literal.newLiteral(
                 Uid.createUid(
                     Constants.DEFAULT_MAPPING_TYPE, ValueSymbolVisitor.STRING.process(right))));
       } else {
         String unsupportedMessage =
             context.unsupportedMessage(ref.info().ident().columnIdent().name());
         if (unsupportedMessage != null) {
           throw new UnsupportedFeatureException(unsupportedMessage);
         }
       }
     }
   }
   return function;
 }
  private Literal getOptionalObjectLiteral(
      Model model, Resource subject, URI predicate, String lang) {
    Set<Value> objects = GraphUtil.getObjects(model, subject, predicate);

    Literal result = null;

    for (Value nextValue : objects) {
      if (nextValue instanceof Literal) {
        final Literal literal = (Literal) nextValue;
        if (result == null || (lang != null && lang.equals(literal.getLanguage()))) {
          result = literal;
        }
      }
    }
    return result;
  }
Example #13
0
  /**
   * Used to write Resources for a Subject. Resources will use "ObjectNode" method.
   *
   * @param predicate PredicateNode
   * @param object Literal
   * @param writer PrintWriter
   * @throws GraphException
   */
  protected void writeStatement(
      Graph graph, SubjectNode subject, PredicateNode predicate, Literal object, PrintWriter writer)
      throws GraphException {

    // determine if the Literal has a datatype
    URI datatype = object.getDatatypeURI();

    // Get the lexical form of the literal
    String literalObject = object.getLexicalForm();

    // Create the StringBuffer to hold the resultant string
    StringBuffer buffer = new StringBuffer();

    // Escape the XML string
    StringUtil.quoteAV(literalObject, buffer);

    if (datatype != null) {

      // write as:  <predicateURI rdf:datatype="datatype">"Literal value"
      //           </predicateURI>
      writer.println(
          "    <"
              + this.getURI(predicate)
              + " "
              + RDF_PREFIX
              + ":datatype=\""
              + datatype
              + "\">"
              + buffer.toString()
              + "</"
              + this.getURI(predicate)
              + ">");
    } else {

      // write as:  <predicateURI>"Literal value"</predicateURI>
      writer.println(
          "    <"
              + this.getURI(predicate)
              + ">"
              + buffer.toString()
              + "</"
              + this.getURI(predicate)
              + ">");
    }
  }
Example #14
0
 @Override
 protected Query applyArrayReference(
     Reference arrayReference, Literal literal, Context context) throws IOException {
   // 1 < ANY (array_col) --> array_col > 1
   return rangeQuery.toQuery(
       arrayReference,
       ((CollectionType) arrayReference.valueType()).innerType(),
       literal.value());
 }
Example #15
0
 @Override
 protected Query applyArrayReference(
     Reference arrayReference, Literal literal, Context context) throws IOException {
   String notLike =
       negateWildcard(convertWildcardToRegex(BytesRefs.toString(literal.value())));
   return new RegexpQuery(
       new Term(arrayReference.info().ident().columnIdent().fqn(), notLike),
       RegexpFlag.COMPLEMENT.value());
 }
  private Expression<K> copyWithoutTrue(And<K> input) {
    List<Expression<K>> copy = Lists.newArrayList();
    for (Expression<K> expr : input.expressions) {
      if (expr instanceof Literal) {
        Literal l = (Literal) expr;

        if (l.getValue()) {
          continue;
        }
      }
      copy.add(expr);
    }

    if (copy.isEmpty()) {
      return Literal.getTrue();
    }

    return And.of(copy);
  }
Example #17
0
 @Override
 protected Query applyArrayLiteral(Reference reference, Literal arrayLiteral, Context context)
     throws IOException {
   // col like ANY (['a', 'b']) --> or(like(col, 'a'), like(col, 'b'))
   BooleanQuery booleanQuery = new BooleanQuery();
   booleanQuery.setMinimumNumberShouldMatch(1);
   for (Object value : toIterable(arrayLiteral.value())) {
     booleanQuery.add(
         likeQuery.toQuery(reference, value, context), BooleanClause.Occur.SHOULD);
   }
   return booleanQuery;
 }
Example #18
0
      @Override
      public Query apply(Function input, Context context) {
        Tuple<Reference, Literal> tuple = super.prepare(input);
        if (tuple == null) {
          return null;
        }
        Reference reference = tuple.v1();
        Literal literal = tuple.v2();
        String columnName = reference.info().ident().columnIdent().fqn();
        if (DataTypes.isCollectionType(reference.valueType())
            && DataTypes.isCollectionType(literal.valueType())) {

          // create boolean filter with term filters to pre-filter the result before applying the
          // functionQuery.
          BooleanFilter boolTermsFilter = new BooleanFilter();
          DataType type = literal.valueType();
          while (DataTypes.isCollectionType(type)) {
            type = ((CollectionType) type).innerType();
          }
          QueryBuilderHelper builder = QueryBuilderHelper.forType(type);
          Object value = literal.value();
          buildTermsQuery(boolTermsFilter, value, columnName, builder);

          if (boolTermsFilter.clauses().isEmpty()) {
            // all values are null...
            return genericFunctionQuery(input, context);
          }

          // wrap boolTermsFilter and genericFunction filter in an additional BooleanFilter to
          // control the ordering of the filters
          // termsFilter is applied first
          // afterwards the more expensive genericFunctionFilter
          BooleanFilter filterClauses = new BooleanFilter();
          filterClauses.add(boolTermsFilter, BooleanClause.Occur.MUST);
          filterClauses.add(genericFunctionFilter(input, context), BooleanClause.Occur.MUST);
          return new FilteredQuery(Queries.newMatchAllQuery(), filterClauses);
        }
        QueryBuilderHelper builder = QueryBuilderHelper.forType(tuple.v1().valueType());
        return builder.eq(columnName, tuple.v2().value());
      }
Example #19
0
 @Override
 protected Query applyArrayLiteral(Reference reference, Literal arrayLiteral, Context context)
     throws IOException {
   // col < ANY ([1,2,3]) --> or(col<1, col<2, col<3)
   BooleanQuery booleanQuery = new BooleanQuery();
   booleanQuery.setMinimumNumberShouldMatch(1);
   for (Object value : toIterable(arrayLiteral.value())) {
     booleanQuery.add(
         inverseRangeQuery.toQuery(reference, reference.valueType(), value),
         BooleanClause.Occur.SHOULD);
   }
   return booleanQuery;
 }
Example #20
0
 @Test
 public void testInfoStamp() {
   String versionString = "E3.14159", commentString = "gloopSmurfale";
   String resourceString =
       "_x eye:assumed 'ABC'; _x eye:checked 'DEF'; _x eye:version '%v'; _x eye:comment '%c'"
           .replaceAll("%v", versionString)
           .replaceAll("%c", commentString);
   InfoStamp i = new InfoStamp(resourceInModel(resourceString));
   Calendar now = Calendar.getInstance();
   Resource root = i.stamp(now);
   Model stamp = root.getModel();
   Literal dateLiteral = ModelFactory.createDefaultModel().createTypedLiteral(now);
   String dateString = "'" + dateLiteral.getLexicalForm() + "'" + dateLiteral.getDatatypeURI();
   String expectedFormat =
       "[eye:assumed 'ABC' & eye:checked 'DEF' & eye:dated <date>"
           + " & eye:comment '<comment>' & eye:version '<version>']";
   String expectedString =
       expectedFormat
           .replaceAll("<date>", dateString)
           .replaceAll("<version>", versionString)
           .replaceAll("<comment>", commentString);
   Model expected = model(expectedString);
   assertIsoModels(expected, stamp);
 }
Example #21
0
      @Override
      protected Query applyArrayLiteral(Reference reference, Literal arrayLiteral, Context context)
          throws IOException {
        //  col != ANY ([1,2,3]) --> not(col=1 and col=2 and col=3)
        String columnName = reference.info().ident().columnIdent().fqn();
        QueryBuilderHelper builder = QueryBuilderHelper.forType(reference.valueType());
        BooleanFilter filter = new BooleanFilter();

        BooleanFilter notFilter = new BooleanFilter();
        for (Object value : toIterable(arrayLiteral.value())) {
          notFilter.add(builder.eqFilter(columnName, value), BooleanClause.Occur.MUST);
        }
        filter.add(notFilter, BooleanClause.Occur.MUST_NOT);

        return new FilteredQuery(Queries.newMatchAllQuery(), filter);
      }
Example #22
0
      @Override
      protected Query applyArrayReference(
          Reference arrayReference, Literal literal, Context context) throws IOException {
        // 1 != any ( col ) -->  gt 1 or lt 1
        String columnName = arrayReference.info().ident().columnIdent().fqn();
        Object value = literal.value();

        QueryBuilderHelper builder = QueryBuilderHelper.forType(arrayReference.valueType());
        BooleanQuery query = new BooleanQuery();
        query.setMinimumNumberShouldMatch(1);
        query.add(
            builder.rangeQuery(columnName, value, null, false, false), BooleanClause.Occur.SHOULD);
        query.add(
            builder.rangeQuery(columnName, null, value, false, false), BooleanClause.Occur.SHOULD);
        return query;
      }
Example #23
0
      @Override
      protected Query applyArrayLiteral(Reference reference, Literal arrayLiteral, Context context)
          throws IOException {
        // col not like ANY (['a', 'b']) --> not(and(like(col, 'a'), like(col, 'b')))
        BooleanQuery query = new BooleanQuery();
        BooleanQuery notQuery = new BooleanQuery();

        String columnName = reference.ident().columnIdent().fqn();
        QueryBuilderHelper builder = QueryBuilderHelper.forType(reference.valueType());
        for (Object value : toIterable(arrayLiteral.value())) {
          notQuery.add(
              builder.like(columnName, value, context.indexCache.filter()),
              BooleanClause.Occur.MUST);
        }
        query.add(notQuery, BooleanClause.Occur.MUST_NOT);
        return query;
      }
 private static Clause preprocessClause(Clause c, String prefix) {
   List<Literal> newLiterals = new ArrayList<Literal>();
   int specialID = 0;
   for (Literal l : c.literals()) {
     String predicate = l.predicate();
     if (predicate.equals(SpecialBinaryPredicates.NEQ)
         || predicate.equals(SpecialBinaryPredicates.EQ)
         || predicate.equals(SpecialVarargPredicates.ALLDIFF)) {
       predicate = SymmetricPredicates.PREFIX + prefix + predicate;
     } else {
       predicate = prefix + predicate;
     }
     Literal newLit = new Literal(predicate, l.isNegated(), l.arity());
     for (int i = 0; i < l.arity(); i++) {
       newLit.set(l.get(i), i);
     }
     newLiterals.add(newLit);
   }
   return new Clause(newLiterals);
 }
 public void flushCollectionCache() {
   super.flushCollectionCache();
 }
  public HashMap<String, Properties> generateResourceBundle(String baseName)
      throws GenerationException {
    // be sure to have at least the default URI constant settings
    if (uriGeneration == null) {
      uriGeneration = GenerationSetting.createDefault(caseFormat, "", "");
    }
    Pattern pattern = Pattern.compile(Pattern.quote(getPrefix()) + "(.+)");
    HashMap<String, URI> splitUris = new HashMap<>();
    for (Resource nextSubject : model.subjects()) {
      if (nextSubject instanceof URI) {
        Matcher matcher = pattern.matcher(nextSubject.stringValue());
        if (matcher.find()) {
          String k = matcher.group(1);
          splitUris.put(k, (URI) nextSubject);
        }
      }
    }

    List<String> keys = new ArrayList<>();
    keys.addAll(splitUris.keySet());
    Collections.sort(keys, String.CASE_INSENSITIVE_ORDER);

    HashMap<String, Properties> bundles = new HashMap<>();
    // Default we have for sure
    bundles.put(baseName, new Properties());
    for (String key : keys) {
      final URI resource = splitUris.get(key);
      //
      String nextKey = cleanKey(doCaseFormatting(key, uriGeneration.getCaseFormat()));

      for (URI p : LABEL_PROPERTIES) {
        for (Value v : GraphUtil.getObjects(model, resource, p)) {
          if (v instanceof Literal) {
            final Literal lit = (Literal) v;
            final String lang = lit.getLanguage();
            final Properties bundle;
            if (lang == null) {
              bundle = bundles.get(baseName);
            } else if (bundles.containsKey(baseName + "_" + lang)) {
              bundle = bundles.get(baseName + "_" + lang);
            } else {
              bundle = new Properties();
              bundles.put(baseName + "_" + lang, bundle);
            }

            if (!bundle.containsKey(nextKey + ".label")) {
              bundle.put(nextKey + ".label", lit.getLabel().replaceAll("\\s+", " "));
            }
          }
        }
      }

      for (URI p : COMMENT_PROPERTIES) {
        for (Value v : GraphUtil.getObjects(model, resource, p)) {
          if (v instanceof Literal) {
            final Literal lit = (Literal) v;
            final String lang = lit.getLanguage();
            final Properties bundle;
            if (lang == null) {
              bundle = bundles.get(baseName);
            } else if (bundles.containsKey(baseName + "_" + lang)) {
              bundle = bundles.get(baseName + "_" + lang);
            } else {
              bundle = new Properties();
              bundles.put(baseName + "_" + lang, bundle);
            }

            if (!bundle.containsKey(nextKey + ".comment")) {
              bundle.put(nextKey + ".comment", lit.getLabel().replaceAll("\\s+", " "));
            }
          }
        }
      }
    }

    if (getPreferredLanguage() != null) {
      log.debug("completing default Bundle with preferred language {}", getPreferredLanguage());
      final Properties defaultBundle = bundles.get(baseName);
      final Properties prefBundle = bundles.get(baseName + "_" + getPreferredLanguage());
      if (prefBundle != null) {
        for (Entry<Object, Object> key : prefBundle.entrySet()) {
          String nextKey = (String) key.getKey();
          if (!defaultBundle.containsKey(nextKey)) {
            log.trace("copying {} from {} to default Bundle", nextKey, getPreferredLanguage());
            defaultBundle.setProperty(nextKey, (String) key.getValue());
          }
        }
      } else {
        log.warn("No Bundle data found for preferred language {}", getPreferredLanguage());
      }
    }
    return bundles;
  }
  public void generate(String className, PrintWriter out)
      throws IOException, GraphUtilException, GenerationException {
    // be sure to have at least the uri constants generated
    if (stringGeneration == null && uriGeneration == null) {
      uriGeneration = GenerationSetting.createDefault(caseFormat, "", "");
      // throw new GenerationException("no generation settings present, please set explicitly");
    }
    log.trace("classname: {}", className);
    if (StringUtils.isBlank(name)) {
      name = className;
    }
    if (StringUtils.isBlank(prefix)) {
      throw new GenerationException("could not detect prefix, please set explicitly");
    } else {
      log.debug("prefix: {}", prefix);
    }

    Pattern pattern = Pattern.compile(Pattern.quote(getPrefix()) + "(.+)");
    ConcurrentMap<String, URI> splitUris = new ConcurrentHashMap<>();
    for (Resource nextSubject : model.subjects()) {
      if (nextSubject instanceof URI) {
        Matcher matcher = pattern.matcher(nextSubject.stringValue());
        if (matcher.find()) {
          String k = matcher.group(1);
          URI putIfAbsent = splitUris.putIfAbsent(k, (URI) nextSubject);
          if (putIfAbsent != null) {
            log.warn(
                "Conflicting keys found: uri={} key={} existing={}",
                nextSubject.stringValue(),
                k,
                putIfAbsent);
          }
        }
      }
    }

    // print

    // package is optional
    if (StringUtils.isNotBlank(packageName)) {
      out.printf("package %s;%n%n", getPackageName());
    }
    // imports
    out.println("import org.openrdf.model.URI;");
    out.println("import org.openrdf.model.ValueFactory;");
    out.println("import org.openrdf.model.impl.ValueFactoryImpl;");
    out.println();

    final URI pfx = new URIImpl(prefix);
    Literal oTitle =
        getFirstExistingObjectLiteral(model, pfx, getPreferredLanguage(), LABEL_PROPERTIES);
    Literal oDescr =
        getFirstExistingObjectLiteral(model, pfx, getPreferredLanguage(), COMMENT_PROPERTIES);
    Set<Value> oSeeAlso = model.filter(pfx, RDFS.SEEALSO, null).objects();

    // class JavaDoc
    out.println("/**");
    if (oTitle != null) {
      out.printf(
          " * %s.%n",
          WordUtils.wrap(oTitle.getLabel().replaceAll("\\s+", " "), 70, "\n * ", false));
      out.println(" * <p>");
    }
    if (oDescr != null) {
      out.printf(
          " * %s.%n",
          WordUtils.wrap(oDescr.getLabel().replaceAll("\\s+", " "), 70, "\n * ", false));
      out.println(" * <p>");
    }
    out.printf(" * Namespace %s.%n", name);
    out.printf(" * Prefix: {@code <%s>}%n", prefix);
    if (!oSeeAlso.isEmpty()) {
      out.println(" *");
      for (Value s : oSeeAlso) {
        if (s instanceof URI) {
          out.printf(" * @see <a href=\"%s\">%s</a>%n", s.stringValue(), s.stringValue());
        }
      }
    }
    out.println(" */");
    // class Definition
    out.printf("public class %s {%n", className);
    out.println();

    // constants
    out.printf(getIndent(1) + "/** {@code %s} **/%n", prefix);
    out.printf(getIndent(1) + "public static final String NAMESPACE = \"%s\";%n", prefix);
    out.println();
    out.printf(getIndent(1) + "/** {@code %s} **/%n", name.toLowerCase());
    out.printf(getIndent(1) + "public static final String PREFIX = \"%s\";%n", name.toLowerCase());
    out.println();

    List<String> keys = new ArrayList<>();
    keys.addAll(splitUris.keySet());
    Collections.sort(keys, String.CASE_INSENSITIVE_ORDER);
    // do the string constant generation (if set)
    if (stringGeneration != null) {
      for (String key : keys) {
        final Literal comment =
            getFirstExistingObjectLiteral(
                model, splitUris.get(key), getPreferredLanguage(), COMMENT_PROPERTIES);
        final Literal label =
            getFirstExistingObjectLiteral(
                model, splitUris.get(key), getPreferredLanguage(), LABEL_PROPERTIES);

        out.println(getIndent(1) + "/**");
        if (label != null) {
          out.printf(getIndent(1) + " * %s%n", label.getLabel());
          out.println(getIndent(1) + " * <p>");
        }
        out.printf(getIndent(1) + " * {@code %s}.%n", splitUris.get(key).stringValue());
        if (comment != null) {
          out.println(getIndent(1) + " * <p>");
          out.printf(
              getIndent(1) + " * %s%n",
              WordUtils.wrap(
                  comment.getLabel().replaceAll("\\s+", " "),
                  70,
                  "\n" + getIndent(1) + " * ",
                  false));
        }
        out.println(getIndent(1) + " *");
        out.printf(getIndent(1) + " * @see <a href=\"%s\">%s</a>%n", splitUris.get(key), key);
        out.println(getIndent(1) + " */");

        final String nextKey =
            cleanKey(
                // NOTE: CONSTANT PREFIX and constant SUFFIX are NOT part of caseFormatting
                String.format(
                    "%s%s%s",
                    StringUtils.defaultString(stringGeneration.getConstantPrefix()),
                    doCaseFormatting(key, stringGeneration.getCaseFormat()),
                    StringUtils.defaultString(stringGeneration.getConstantSuffix())));
        checkField(className, nextKey);
        out.printf(
            getIndent(1) + "public static final String %s = %s.NAMESPACE + \"%s\";%n",
            nextKey,
            className,
            key);
        out.println();
      }
    }
    // do the entire uri constant generation
    if (uriGeneration != null) {
      for (String key : keys) {
        Literal comment =
            getFirstExistingObjectLiteral(
                model, splitUris.get(key), getPreferredLanguage(), COMMENT_PROPERTIES);
        Literal label =
            getFirstExistingObjectLiteral(
                model, splitUris.get(key), getPreferredLanguage(), LABEL_PROPERTIES);

        out.println(getIndent(1) + "/**");
        if (label != null) {
          out.printf(getIndent(1) + " * %s%n", label.getLabel());
          out.println(getIndent(1) + " * <p>");
        }
        out.printf(getIndent(1) + " * {@code %s}.%n", splitUris.get(key).stringValue());
        if (comment != null) {
          out.println(getIndent(1) + " * <p>");
          out.printf(
              getIndent(1) + " * %s%n",
              WordUtils.wrap(
                  comment.getLabel().replaceAll("\\s+", " "),
                  70,
                  "\n" + getIndent(1) + " * ",
                  false));
        }
        out.println(getIndent(1) + " *");
        out.printf(getIndent(1) + " * @see <a href=\"%s\">%s</a>%n", splitUris.get(key), key);
        out.println(getIndent(1) + " */");
        final String nextKey =
            cleanKey(
                // NOTE: CONSTANT PREFIX and constant SUFFIX are NOT part of caseFormatting
                String.format(
                    "%s%s%s",
                    StringUtils.defaultString(uriGeneration.getConstantPrefix()),
                    doCaseFormatting(key, uriGeneration.getCaseFormat()),
                    StringUtils.defaultString(uriGeneration.getConstantSuffix())));

        // String nextKey = cleanKey(doCaseFormatting(key, uriGeneration.getCaseFormat()));
        checkField(className, nextKey);
        out.printf(getIndent(1) + "public static final URI %s;%n", nextKey);
        out.println();
      }
      // static init
      out.println(getIndent(1) + "static {");
      out.printf(getIndent(2) + "ValueFactory factory = ValueFactoryImpl.getInstance();%n");
      out.println();
      for (String key : keys) {
        final String nextKey =
            cleanKey(
                // NOTE: CONSTANT PREFIX and constant SUFFIX are NOT part of caseFormatting
                String.format(
                    "%s%s%s",
                    StringUtils.defaultString(uriGeneration.getConstantPrefix()),
                    doCaseFormatting(key, uriGeneration.getCaseFormat()),
                    StringUtils.defaultString(uriGeneration.getConstantSuffix())));
        out.printf(
            getIndent(2) + "%s = factory.createURI(%s.NAMESPACE, \"%s\");%n",
            nextKey,
            className,
            key);
      }
      out.println(getIndent(1) + "}");
      out.println();
    }

    // private contructor to avoid instances
    out.printf(getIndent(1) + "private %s() {%n", className);
    out.println(getIndent(2) + "//static access only");
    out.println(getIndent(1) + "}");
    out.println();

    // class end
    out.println("}");
    out.flush();
  }
Example #28
0
 @Override
 protected Query applyArrayReference(
     Reference arrayReference, Literal literal, Context context) throws IOException {
   return likeQuery.toQuery(arrayReference, literal.value(), context);
 }
Example #29
0
  /**
   * Generates code for a Java expression satisfying the {@link org.eigenbase.runtime.TupleIter}
   * interface. The generated code allocates a {@link org.eigenbase.runtime.CalcTupleIter} with a
   * dynamic {@link org.eigenbase.runtime.TupleIter#fetchNext()} method. If the "abort on error"
   * flag is false, or an error handling tag is specified, then fetchNext is written to handle row
   * errors.
   *
   * <p>Row errors are handled by wrapping expressions that can fail with a try/catch block. A
   * caught RuntimeException is then published to an "connection variable." In the event that errors
   * can overflow, an "error buffering" flag allows them to be posted again on the next iteration of
   * fetchNext.
   *
   * @param implementor an object that implements relations as Java code
   * @param rel the relation to be implemented
   * @param childExp the implemented child of the relation
   * @param varInputRow the Java variable to use for the input row
   * @param inputRowType the rel data type of the input row
   * @param outputRowType the rel data type of the output row
   * @param program the rex program to implemented by the relation
   * @param tag an error handling tag
   * @return a Java expression satisfying the TupleIter interface
   */
  public static Expression implementAbstractTupleIter(
      JavaRelImplementor implementor,
      JavaRel rel,
      Expression childExp,
      Variable varInputRow,
      final RelDataType inputRowType,
      final RelDataType outputRowType,
      RexProgram program,
      String tag) {
    MemberDeclarationList memberList = new MemberDeclarationList();

    // Perform error recovery if continuing on errors or if
    // an error handling tag has been specified
    boolean errorRecovery = !abortOnError || (tag != null);

    // Error buffering should not be enabled unless error recovery is
    assert !errorBuffering || errorRecovery;

    // Allow backwards compatibility until all Farrago extensions are
    // satisfied with the new error handling semantics. The new semantics
    // include:
    //   (1) cast input object to input row object outside of try block,
    //         should be fine, at least for base Farrago
    //   (2) maintain a columnIndex counter to better locate of error,
    //         at the cost of a few cycles
    //   (3) publish errors to the runtime context. FarragoRuntimeContext
    //         now supports this API
    boolean backwardsCompatible = true;
    if (tag != null) {
      backwardsCompatible = false;
    }

    RelDataTypeFactory typeFactory = implementor.getTypeFactory();
    OJClass outputRowClass = OJUtil.typeToOJClass(outputRowType, typeFactory);
    OJClass inputRowClass = OJUtil.typeToOJClass(inputRowType, typeFactory);

    Variable varOutputRow = implementor.newVariable();

    FieldDeclaration inputRowVarDecl =
        new FieldDeclaration(
            new ModifierList(ModifierList.PRIVATE),
            TypeName.forOJClass(inputRowClass),
            varInputRow.toString(),
            null);

    FieldDeclaration outputRowVarDecl =
        new FieldDeclaration(
            new ModifierList(ModifierList.PRIVATE),
            TypeName.forOJClass(outputRowClass),
            varOutputRow.toString(),
            new AllocationExpression(outputRowClass, new ExpressionList()));

    // The method body for fetchNext, a main target of code generation
    StatementList nextMethodBody = new StatementList();

    // First, post an error if it overflowed the previous time
    //     if (pendingError) {
    //         rc = handleRowError(...);
    //         if (rc instanceof NoDataReason) {
    //             return rc;
    //         }
    //         pendingError = false;
    //     }
    if (errorBuffering) {
      // add to next method body...
    }

    // Most of fetchNext falls within a while() block. The while block
    // allows us to try multiple input rows against a filter condition
    // before returning a single row.
    //     while (true) {
    //         Object varInputObj = inputIterator.fetchNext();
    //         if (varInputObj instanceof TupleIter.NoDataReason) {
    //             return varInputObj;
    //         }
    //         varInputRow = (InputRowClass) varInputObj;
    //         int columnIndex = 0;
    //         [calculation statements]
    //     }
    StatementList whileBody = new StatementList();

    Variable varInputObj = implementor.newVariable();

    whileBody.add(
        new VariableDeclaration(
            OJUtil.typeNameForClass(Object.class),
            varInputObj.toString(),
            new MethodCall(new FieldAccess("inputIterator"), "fetchNext", new ExpressionList())));

    StatementList ifNoDataReasonBody = new StatementList();

    whileBody.add(
        new IfStatement(
            new InstanceofExpression(
                varInputObj, OJUtil.typeNameForClass(TupleIter.NoDataReason.class)),
            ifNoDataReasonBody));

    ifNoDataReasonBody.add(new ReturnStatement(varInputObj));

    // Push up the row declaration for new error handling so that the
    // input row is available to the error handler
    if (!backwardsCompatible) {
      whileBody.add(assignInputRow(inputRowClass, varInputRow, varInputObj));
    }

    Variable varColumnIndex = null;
    if (errorRecovery && !backwardsCompatible) {
      // NOTE jvs 7-Oct-2006:  Declare varColumnIndex as a member
      // (rather than a local) in case in the future we want
      // to decompose complex expressions into helper methods.
      varColumnIndex = implementor.newVariable();
      FieldDeclaration varColumnIndexDecl =
          new FieldDeclaration(
              new ModifierList(ModifierList.PRIVATE),
              OJUtil.typeNameForClass(int.class),
              varColumnIndex.toString(),
              null);
      memberList.add(varColumnIndexDecl);
      whileBody.add(
          new ExpressionStatement(
              new AssignmentExpression(
                  varColumnIndex, AssignmentExpression.EQUALS, Literal.makeLiteral(0))));
    }

    // Calculator (projection, filtering) statements are later appended
    // to calcStmts. Typically, this target will be the while list itself.
    StatementList calcStmts;
    if (!errorRecovery) {
      calcStmts = whileBody;
    } else {
      // For error recovery, we wrap the calc statements
      // (e.g., everything but the code that reads rows from the
      // inputIterator) in a try/catch that publishes exceptions.

      calcStmts = new StatementList();

      // try { /* calcStmts */ }
      // catch(RuntimeException ex) {
      //     Object rc = connection.handleRowError(...);
      //     [buffer error if necessary]
      // }
      StatementList catchStmts = new StatementList();

      if (backwardsCompatible) {
        catchStmts.add(
            new ExpressionStatement(
                new MethodCall(
                    new MethodCall(
                        OJUtil.typeNameForClass(EigenbaseTrace.class), "getStatementTracer", null),
                    "log",
                    new ExpressionList(
                        new FieldAccess(OJUtil.typeNameForClass(Level.class), "WARNING"),
                        Literal.makeLiteral("java calc exception"),
                        new FieldAccess("ex")))));
      } else {
        Variable varRc = implementor.newVariable();
        ExpressionList handleRowErrorArgs =
            new ExpressionList(varInputRow, new FieldAccess("ex"), varColumnIndex);
        handleRowErrorArgs.add(Literal.makeLiteral(tag));
        catchStmts.add(
            new VariableDeclaration(
                OJUtil.typeNameForClass(Object.class),
                varRc.toString(),
                new MethodCall(
                    implementor.getConnectionVariable(), "handleRowError", handleRowErrorArgs)));

        // Buffer an error if it overflowed
        //     if (rc instanceof NoDataReason) {
        //         pendingError = true;
        //         [save error state]
        //         return rc;
        //     }
        if (errorBuffering) {
          // add to catch statements...
        }
      }

      CatchList catchList =
          new CatchList(
              new CatchBlock(
                  new Parameter(OJUtil.typeNameForClass(RuntimeException.class), "ex"),
                  catchStmts));

      TryStatement tryStmt = new TryStatement(calcStmts, catchList);

      whileBody.add(tryStmt);
    }

    if (backwardsCompatible) {
      calcStmts.add(assignInputRow(inputRowClass, varInputRow, varInputObj));
    }

    StatementList condBody;
    RexToOJTranslator translator = implementor.newStmtTranslator(rel, calcStmts, memberList);
    try {
      translator.pushProgram(program);
      if (program.getCondition() != null) {
        // TODO jvs 8-Oct-2006:  move condition to its own
        // method if big, as below for project exprs.
        condBody = new StatementList();
        RexNode rexIsTrue =
            rel.getCluster()
                .getRexBuilder()
                .makeCall(SqlStdOperatorTable.isTrueOperator, program.getCondition());
        Expression conditionExp = translator.translateRexNode(rexIsTrue);
        calcStmts.add(new IfStatement(conditionExp, condBody));
      } else {
        condBody = calcStmts;
      }

      RelDataTypeField[] fields = outputRowType.getFields();
      final List<RexLocalRef> projectRefList = program.getProjectList();
      int i = -1;
      for (RexLocalRef rhs : projectRefList) {

        // NOTE jvs 14-Sept-2006:  Put complicated project expressions
        // into their own method, otherwise a big select list can easily
        // blow the 64K Java limit on method bytecode size.  Make
        // methods private final in the hopes that they will get inlined
        // JIT.  For now we decide "complicated" based on the size of
        // the generated Java parse tree. A big enough select list of
        // simple expressions could still blow the limit, so we may need
        // to group them together, sub-divide, etc.

        StatementList projMethodBody = new StatementList();

        if (errorRecovery && !backwardsCompatible) {
          projMethodBody.add(
              new ExpressionStatement(
                  new UnaryExpression(varColumnIndex, UnaryExpression.POST_INCREMENT)));
        }
        ++i;

        RexToOJTranslator projTranslator = translator.push(projMethodBody);
        String javaFieldName = Util.toJavaId(fields[i].getName(), i);
        Expression lhs = new FieldAccess(varOutputRow, javaFieldName);
        projTranslator.translateAssignment(fields[i], lhs, rhs);

        int complexity = OJUtil.countParseTreeNodes(projMethodBody);

        // REVIEW: HCP 5/18/2011
        // The projMethod should be checked
        // for causing possible compiler errors caused by the use of
        // variables declared in other projMethods.  Also the
        // local declaration of variabled used by other proj methods
        // should also be checked.

        // Fixing for backswing integration 14270
        // TODO: check if abstracting this method body will cause
        // a compiler error
        if (true) {
          // No method needed; just append.
          condBody.addAll(projMethodBody);
          continue;
        }

        // Need a separate method.

        String projMethodName = "calc_" + varOutputRow.toString() + "_f_" + i;
        MemberDeclaration projMethodDecl =
            new MethodDeclaration(
                new ModifierList(ModifierList.PRIVATE | ModifierList.FINAL),
                TypeName.forOJClass(OJSystem.VOID),
                projMethodName,
                new ParameterList(),
                null,
                projMethodBody);
        memberList.add(projMethodDecl);
        condBody.add(new ExpressionStatement(new MethodCall(projMethodName, new ExpressionList())));
      }
    } finally {
      translator.popProgram(program);
    }

    condBody.add(new ReturnStatement(varOutputRow));

    WhileStatement whileStmt = new WhileStatement(Literal.makeLiteral(true), whileBody);

    nextMethodBody.add(whileStmt);

    MemberDeclaration fetchNextMethodDecl =
        new MethodDeclaration(
            new ModifierList(ModifierList.PUBLIC),
            OJUtil.typeNameForClass(Object.class),
            "fetchNext",
            new ParameterList(),
            null,
            nextMethodBody);

    // The restart() method should reset variables used to buffer errors
    //     pendingError = false
    if (errorBuffering) {
      // declare refinement of restart() and add to member list...
    }

    memberList.add(inputRowVarDecl);
    memberList.add(outputRowVarDecl);
    memberList.add(fetchNextMethodDecl);
    Expression newTupleIterExp =
        new AllocationExpression(
            OJUtil.typeNameForClass(CalcTupleIter.class), new ExpressionList(childExp), memberList);

    return newTupleIterExp;
  }