Пример #1
0
  /**
   * Resolve a constraint against an RDF/XML document.
   *
   * <p>Resolution is by filtration of a URL stream, and thus very slow.
   */
  public Resolution resolve(Constraint constraint) throws QueryException {
    if (logger.isDebugEnabled()) {
      logger.debug("Resolve " + constraint);
    }

    // check the model
    ConstraintElement modelElement = constraint.getModel();
    if (modelElement instanceof Variable) {
      if (logger.isDebugEnabled()) logger.debug("Ignoring solutions for " + constraint);
      return new EmptyResolution(constraint, false);
    } else if (!(modelElement instanceof LocalNode)) {
      throw new QueryException("Failed to localize Lucene Graph before resolution " + constraint);
    }

    /* temporary hack because $_from is not resolved before transformation occurs, and hence
     * no LuceneConstraint's are created when doing ... from <lucene-model> where ... .
     */
    if (!(constraint instanceof LuceneConstraint)) {
      constraint = new LuceneConstraint(constraint);
    }

    // generate the tuples
    try {
      FullTextStringIndex stringIndex =
          getFullTextStringIndex(((LocalNode) modelElement).getValue());
      return new FullTextStringIndexTuples(
          stringIndex, (LuceneConstraint) constraint, resolverSession);
    } catch (IOException ioe) {
      throw new QueryException("Failed to open string index", ioe);
    } catch (FullTextStringIndexException ef) {
      throw new QueryException("Query failed against string index\n" + new StackTrace(ef));
    } catch (TuplesException te) {
      throw new QueryException("Failed to query string index", te);
    }
  }
Пример #2
0
  /**
   * Resolve a constraint against an RDF/XML document.
   *
   * <p>Resolution is by filtration of a URL stream, and thus very slow.
   */
  public Resolution resolve(Constraint constraint) throws QueryException {
    if (logger.isDebugEnabled()) {
      logger.debug("Resolve " + constraint);
    }

    // Validate "constraint" parameter
    if (constraint == null) {
      throw new IllegalArgumentException("Null \"constraint\" parameter");
    }

    if (!(constraint.getModel() instanceof LocalNode)) {
      if (logger.isDebugEnabled()) logger.debug("Ignoring solutions for " + constraint);
      return new EmptyResolution(constraint, false);
    }

    if (!(constraint.getElement(1) instanceof LocalNode)
        || !(constraint.getElement(2) instanceof LocalNode)) {
      throw new QueryException(
          "Prefix resolver can only be used for fixed prefixes: " + constraint);
    }

    try {

      long property = ((LocalNode) constraint.getElement(1)).getValue();
      LocalNode object = (LocalNode) constraint.getElement(2);
      Node prefixNode = resolverSession.globalize(object.getValue());

      // check the constraint for consistency
      if ((property != mulgaraPrefix && property != mulgaraStringPrefix)
          || !(prefixNode instanceof Literal || prefixNode instanceof URIReference)) {
        logger.debug("property = " + property + ", mulgaraPrefix = " + mulgaraPrefix);
        logger.debug("element(2): " + prefixNode + " [" + prefixNode.getClass().getName() + "]");
        throw new QueryException(
            "Prefix resolver can only be used for prefix constraints: " + constraint);
      }

      if (logger.isDebugEnabled()) {
        logger.debug(
            "Evaluating " + constraint.getElement(0) + " has prefix " + constraint.getElement(2));
      }

      ConstraintElement node = constraint.getElement(0);
      assert node != null;

      Tuples tuples;

      if (node instanceof Variable) {

        // convert the prefix into a string pool object
        SPObjectFactory spoFact = SPObjectFactoryImpl.getInstance();
        SPObject startPrefixObj = getStartObject(spoFact, prefixNode, property);
        SPObject endPrefixObj = getEndObject(spoFact, prefixNode, property);

        // get the extents of the prefix from the string pool
        tuples = resolverSession.findStringPoolRange(startPrefixObj, true, endPrefixObj, false);
        assert tuples != null;
        // rename variables away from subject, predicate and object
        tuples.renameVariables(constraint);

        long resultSize;
        try {
          // Get the size of the final result.
          resultSize = tuples.getRowCount();
        } catch (TuplesException e) {
          throw new QueryException("Unable to build result", e);
        }

        if (logger.isDebugEnabled()) {
          try {
            logger.debug(
                "tuples size = " + tuples.getRowCount() + " (should be " + resultSize + ")");
          } catch (TuplesException e) {
            logger.debug("Error getting the length of the tuples object");
          }
        }

        return new TuplesWrapperResolution(tuples, constraint);

      } else { // if (node instanceof Variable)

        // node must therefore be an instanceof LocalNode
        // we can shortcut the process here
        assert node instanceof LocalNode;
        LocalNode n = (LocalNode) node;

        // get the node out of the string pool
        SPObject spo = resolverSession.findStringPoolObject(n.getValue());

        // check that the node exists
        if (spo == null) {
          tuples = TuplesOperations.empty();
        } else {

          // see if the node starts with the required prefix

          String prefix;
          // extract the string from the literal
          if (prefixNode instanceof Literal) {
            prefix = ((Literal) prefixNode).getLexicalForm();
          } else {
            prefix = ((URIReference) prefixNode).getURI().toString();
          }
          if (spo.getLexicalForm().startsWith(prefix)) {
            tuples = TuplesOperations.unconstrained();
          } else {
            tuples = TuplesOperations.empty();
          }
        }
      }

      // convert the tuples to a resolution
      return new TuplesWrapperResolution(tuples, constraint);

    } catch (GlobalizeException ge) {
      throw new QueryException("Couldn't convert internal data into a string", ge);
    } catch (StringPoolException e) {
      throw new QueryException("Couldn't query constraint", e);
    }
  }