Package org.openrdf.query.parser.sparql

Source Code of org.openrdf.query.parser.sparql.GraphPattern

/*
* Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
*
* Licensed under the Aduna BSD-style license.
*/
package org.openrdf.query.parser.sparql;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.openrdf.query.algebra.Filter;
import org.openrdf.query.algebra.Join;
import org.openrdf.query.algebra.LeftJoin;
import org.openrdf.query.algebra.SingletonSet;
import org.openrdf.query.algebra.StatementPattern;
import org.openrdf.query.algebra.TupleExpr;
import org.openrdf.query.algebra.ValueExpr;
import org.openrdf.query.algebra.Var;

/**
* A graph pattern consisting of (required and optional) tuple expressions and
* boolean constraints.
*
* @author Arjohn Kampman
*/
class GraphPattern {

  /**
   * The context of this graph pattern.
   */
  private Var contextVar;

  /**
   * The StatementPattern-scope of this graph pattern.
   */
  private StatementPattern.Scope spScope = StatementPattern.Scope.DEFAULT_CONTEXTS;

  /**
   * The required tuple expressions in this graph pattern.
   */
  private List<TupleExpr> requiredTEs = new ArrayList<TupleExpr>();

  /**
   * The optional tuple expressions in this graph pattern.
   */
  private List<TupleExpr> optionalTEs = new ArrayList<TupleExpr>();

  /**
   * The boolean constraints in this graph pattern.
   */
  private List<ValueExpr> constraints = new ArrayList<ValueExpr>();

  /**
   * Creates a new graph pattern.
   */
  public GraphPattern() {
  }

  /**
   * Creates a new graph pattern that inherits the context and scope from a
   * parent graph pattern.
   */
  public GraphPattern(GraphPattern parent) {
    contextVar = parent.contextVar;
    spScope = parent.spScope;
  }

  public void setContextVar(Var contextVar) {
    this.contextVar = contextVar;
  }

  public Var getContextVar() {
    return contextVar;
  }

  public void setStatementPatternScope(StatementPattern.Scope spScope) {
    this.spScope = spScope;
  }

  public StatementPattern.Scope getStatementPatternScope() {
    return spScope;
  }

  public void addRequiredTE(TupleExpr te) {
    requiredTEs.add(te);
  }

  public void addRequiredSP(Var subjVar, Var predVar, Var objVar) {
    addRequiredTE(new StatementPattern(spScope, subjVar, predVar, objVar, contextVar));
  }

  public List<TupleExpr> getRequiredTEs() {
    return Collections.unmodifiableList(requiredTEs);
  }

  public void addOptionalTE(TupleExpr te) {
    optionalTEs.add(te);
  }

  public List<TupleExpr> getOptionalTEs() {
    return Collections.unmodifiableList(optionalTEs);
  }

  public void addConstraint(ValueExpr constraint) {
    constraints.add(constraint);
  }

  public void addConstraints(Collection<ValueExpr> constraints) {
    this.constraints.addAll(constraints);
  }

  public List<ValueExpr> getConstraints() {
    return Collections.unmodifiableList(constraints);
  }

  public List<ValueExpr> removeAllConstraints() {
    List<ValueExpr> constraints = this.constraints;
    this.constraints = new ArrayList<ValueExpr>();
    return constraints;
  }

  /**
   * Removes all tuple expressions and constraints.
   */
  public void clear() {
    requiredTEs.clear();
    optionalTEs.clear();
    constraints.clear();
  }

  /**
   * Builds a combined tuple expression from the tuple expressions and
   * constraints in this graph pattern.
   *
   * @return A tuple expression for this graph pattern.
   */
  public TupleExpr buildTupleExpr() {
    TupleExpr result;

    if (requiredTEs.isEmpty()) {
      result = new SingletonSet();
    }
    else if (requiredTEs.size() == 1) {
      result = requiredTEs.get(0);
    }
    else {
      result = new Join(requiredTEs);
    }

    for (TupleExpr optTE : optionalTEs) {
      result = new LeftJoin(result, optTE);
    }

    for (ValueExpr constraint : constraints) {
      result = new Filter(result, constraint);
    }

    return result;
  }
}
TOP

Related Classes of org.openrdf.query.parser.sparql.GraphPattern

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.