Exemplo n.º 1
0
  /**
   * Deconstruct the node or list object argument and make a StrMatch The 'executionTime' flag
   * indciates whether this is for a build time static check, or for runtime execution.
   */
  private StrMatch objectToStruct(PropFuncArg argObject, boolean executionTime) {
    EntityDefinition docDef = textIndex.getDocDef();
    if (argObject.isNode()) {
      Node o = argObject.getArg();
      if (!o.isLiteral()) {
        if (executionTime) log.warn("Object to text query is not a literal");
        return null;
      }

      RDFDatatype dt = o.getLiteralDatatype();
      if (dt != null && dt != XSDDatatype.XSDstring) {
        log.warn("Object to text query is not a string");
        return null;
      }

      String qs = o.getLiteralLexicalForm();
      return new StrMatch(null, qs, -1, 0);
    }

    List<Node> list = argObject.getArgList();
    if (list.size() == 0 || list.size() > 3)
      throw new TextIndexException("Change in object list size");

    Node predicate = null;
    String field = null; // Do not prepend the field name - rely on default field
    int idx = 0;
    Node x = list.get(0);
    // Property?
    if (x.isURI()) {
      predicate = x;
      idx++;
      if (idx >= list.size())
        throw new TextIndexException("Property specificed but no query string : " + list);
      x = list.get(idx);
      field = docDef.getField(predicate);
      if (field == null) {
        log.warn("Predicate not indexed: " + predicate);
        return null;
      }
    }

    // String!
    if (!x.isLiteral()) {
      if (executionTime) log.warn("Text query string is not a literal " + list);
      return null;
    }

    if (x.getLiteralDatatype() != null && !x.getLiteralDatatype().equals(XSDDatatype.XSDstring)) {
      log.warn("Text query is not a string " + list);
      return null;
    }
    String queryString = x.getLiteralLexicalForm();
    idx++;

    int limit = -1;
    float score = 0;

    if (idx < list.size()) {
      // Limit?
      x = list.get(idx);
      idx++;
      if (!x.isLiteral()) {
        if (executionTime) log.warn("Text query limit is not an integer " + x);
        return null;
      }

      int v = NodeFactoryExtra.nodeToInt(x);
      limit = (v < 0) ? -1 : v;
    }

    String qs = queryString;
    if (field != null) qs = field + ":" + qs;

    return new StrMatch(predicate, qs, limit, score);
  }
Exemplo n.º 2
0
  /**
   * Instantiates a new sparql construct function.
   *
   * @param conf the graph with dataflow definition
   * @param confRoot the specific node in the graph representing the producer configuration
   * @param map the map to access the other defined producers
   * @see RDFProducer
   */
  public SparqlConstructFunction(Graph conf, Node confRoot, final ProducerMap map) {
    URI baseURI = URI.create(confRoot.getURI());

    Node inputNode = GraphUtils.getSingleValueOptProperty(conf, confRoot, DF.input.asNode());
    inputProducer =
        (inputNode != null) ? map.getProducer(inputNode) : EmptyGraphProducer.getInstance();

    Node queryAsInputNode = NodeFactory.createURI(baseURI.resolve("#query").toString());

    Node queryNode = GraphUtils.getSingleValueOptProperty(conf, confRoot, DF.configTxt.asNode());
    String queryTxt = (queryNode != null) ? queryNode.getLiteralLexicalForm() : null;

    Node configNode = GraphUtils.getSingleValueOptProperty(conf, confRoot, DF.config.asNode());
    RDFProducer configProducer = (configNode != null) ? map.getProducer(configNode) : null;

    Node configRootNode =
        GraphUtils.getSingleValueOptProperty(conf, confRoot, DF.configRoot.asNode());
    if (configRootNode == null) configRootNode = configNode;
    if (configRootNode == null && inputNode != null) {
      Iterator<Node> namedInputNodes =
          GraphUtils.getPropertyValues(conf, inputNode, DF.namedInput.asNode());
      while (namedInputNodes.hasNext()) {
        Node namedInputNode = namedInputNodes.next();
        if (conf.contains(namedInputNode, DF.id.asNode(), queryAsInputNode)) {
          configRootNode =
              GraphUtils.getSingleValueProperty(conf, namedInputNode, DF.input.asNode());
          break;
        }
      }
    }

    //		String configRootURI = (configRootNode != null && configRootNode.isURI()) ?
    // configRootNode.getURI() : null;

    queryProducer =
        new CoalesceQueryProducer(
            new GraphQueryProducer(
                new CoalesceGraphProducer(
                    configProducer, new SelectGraphProducer(inputProducer, queryAsInputNode)),
                configRootNode /*NodeFactory.createURI(baseURI.resolve("").toString())*/),
            new StringQueryProducer(queryTxt, baseURI.toString()));
    //		final Model confModel = ModelFactory.createModelForGraph(conf);
    //		Resource constructResource =
    //			confModel
    //					.getRDFNode(confRoot)
    //					.asResource()
    //					.getPropertyResourceValue(SPINX.config);
    //		Iterator<Producer> fromGraphProds =
    //			confModel
    //					.getRDFNode(confRoot)
    //					.asResource()
    //					.listProperties(SPINX.from)
    //					.mapWith(new Map1<Statement, Producer>() {
    //						@Override
    //						public Producer map1(Statement stmt) {
    //							return map.getProducer(stmt.getObject().asNode());
    //						}
    //					});
    // defaultProd = null;
    //		if (!fromGraphProds.hasNext()) {
    //			defaultProd = EmptyGraphProducer.getInstance();
    //		} else {
    //			defaultProd = fromGraphProds.next();
    //			if (fromGraphProds.hasNext()) {
    //				defaultProd =
    //					new UnionFunction(
    //							new ConcatenatedIterator<Producer>(
    //									new SingletonIterator<Producer>(defaultProd),
    //									fromGraphProds));
    //			}
    //		}
    //		Iterator<Node> fromNamedGraphNames =
    //			confModel
    //					.getRDFNode(confRoot)
    //					.asResource()
    //					.listProperties(SPINX.fromNamed)
    //					.mapWith(new Map1<Statement, Node>() {
    //						@Override
    //						public Node map1(Statement stmt) {
    //							return stmt.getObject().asNode();
    //						}
    //					});
    //		while (fromNamedGraphNames.hasNext()) {
    //			Node graphNames = fromNamedGraphNames.next();
    //			prodMap.put(graphNames, map.getProducer(graphNames));
    //		}

    //		query = QueryFactory.toQuery(conf, constructResource.asNode());
    //		if (query == null)
    //			throw new RuntimeException("Parsing Error");

    /*
    // debug
    Graph revEnginedGraph = SpinxFactory.fromQuery(query);
    System.out.println();
    System.out.println("**********************************");
    System.out.println("*** Reverse Engine query graph ***");
    System.out.println("**********************************");
    ModelFactory.createModelForGraph(revEnginedGraph).write(System.out,"N3");
    System.out.println("**********************************");
    System.out.println();
    */

    /*
    SPINFactory.asExpression(constructResource);
    query = ARQFactory.get().createQuery(SPINFactory.asQuery(constructResource));
    */
    /*
    Iterator<String> graphUris =
    	new ConcatenatedIterator<String>(
    			query.getGraphURIs().iterator(),
    			query.getNamedGraphURIs().iterator());
    while (graphUris.hasNext()) {
    	Node node = confModel.getResource(graphUris.next()).asNode();
    	prodMap.put(node, (GraphProducer) map.getProducer(node));
    }
    */
  }