de.fuberlin.wiwiss.d2rq.find
Class TripleQuery

java.lang.Object
  extended byde.fuberlin.wiwiss.d2rq.find.TripleQuery

public class TripleQuery
extends java.lang.Object

Encapsulates a query for a triple. Three elements are necessary for constructing a TripleQuery: a PropertyBridge, a subject node, and an object node. Both nodes may be wildcard nodes ({{Node.ANY}}). The TripleQuery determines which database columns, database joins and database conditions must be used to find the triple, and builds Jena triples from SQL result rows. It also has logic to determine if it can be combined with other TripleQuery instances into a single SQL statement. TODO: The logic for removing unnecessary joins is ugly and probably the wrong place; especially getReplacedColumns() seems awkward

History:
08-03-2004: Initial version of this class.

Version:
V0.2
Author:
Richard Cyganiak

Field Summary
private  java.lang.String aTable
           
private  PropertyBridge bridge
           
private  java.util.Map columnValues
           
private  java.util.Set joins
           
private  java.util.Set objectColumns
           
private  NodeMaker objectMaker
           
private  NodeMaker predicateMaker
           
private  java.util.Map replacedColumns
           
private  java.util.Set selectColumns
           
private  java.util.Set subjectColumns
           
private  NodeMaker subjectMaker
           
 
Constructor Summary
TripleQuery(PropertyBridge bridge, com.hp.hpl.jena.graph.Node subject, com.hp.hpl.jena.graph.Node predicate, com.hp.hpl.jena.graph.Node object)
          Constructs a new TripleQuery.
 
Method Summary
private  void eliminateColumns(Join join, java.lang.String tableName)
          If we have determined that a table can be dropped, then we have to rewrite all references to columns in that table to the columns on the other side of the join.
private  java.util.Set getAllJoinTables()
           
 java.lang.String getATable()
          Returns the name of an arbitrary database table that is accessed by this query.
 java.util.Map getColumnValues()
           
 java.util.Set getConditions()
           
 Database getDatabase()
           
 java.util.Set getJoins()
           
 NodeMaker getNodeMaker(int i)
           
 PropertyBridge getPropertyBridge()
           
 java.util.Map getReplacedColumns()
          Returns a map from old Columns to new columns.
 java.util.Set getSelectColumns()
           
private  Join getSingleJoinReferencingTable(java.lang.String table)
          Gets a join that references a table, but only if there is exactly one join that references that table.
 boolean isCombinable(TripleQuery other)
          Checks if an other query can be combined with this one into a single SQL statement.
private  boolean isOptionalTable(java.lang.String table, Join join)
          Checks if a table can be dropped.
 com.hp.hpl.jena.graph.Triple makeTriple(java.lang.String[] row, java.util.Map columnNameNumberMap)
          Creates a triple from a database result row.
 boolean mightContainDuplicates()
           
private  void removeOptionalJoins()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

bridge

private PropertyBridge bridge

joins

private java.util.Set joins

columnValues

private java.util.Map columnValues

selectColumns

private java.util.Set selectColumns

aTable

private java.lang.String aTable

subjectColumns

private java.util.Set subjectColumns

objectColumns

private java.util.Set objectColumns

subjectMaker

private NodeMaker subjectMaker

predicateMaker

private NodeMaker predicateMaker

objectMaker

private NodeMaker objectMaker

replacedColumns

private java.util.Map replacedColumns
Constructor Detail

TripleQuery

public TripleQuery(PropertyBridge bridge,
                   com.hp.hpl.jena.graph.Node subject,
                   com.hp.hpl.jena.graph.Node predicate,
                   com.hp.hpl.jena.graph.Node object)
Constructs a new TripleQuery.

Parameters:
bridge - We look for triples matching this property bridge
subject - the subject node, may be {{Node.ANY}}
object - the object node, may be {{Node.ANY}}
Method Detail

getNodeMaker

public NodeMaker getNodeMaker(int i)

getPropertyBridge

public PropertyBridge getPropertyBridge()

getJoins

public java.util.Set getJoins()

getConditions

public java.util.Set getConditions()

getColumnValues

public java.util.Map getColumnValues()

getSelectColumns

public java.util.Set getSelectColumns()

getDatabase

public Database getDatabase()

mightContainDuplicates

public boolean mightContainDuplicates()

getATable

public java.lang.String getATable()
Returns the name of an arbitrary database table that is accessed by this query. Useful only when the query accesses only one table, that is, it has no joins.

Returns:
an arbitrary table that is accessed by this query

isCombinable

public boolean isCombinable(TripleQuery other)
Checks if an other query can be combined with this one into a single SQL statement. Two queries can be combined iff they access the same database and they contain exactly the same joins and WHERE clauses. If they both contain no joins, they must contain only columns from the same table.

Parameters:
other - a query to be compared with this one
Returns:
true if both are combinable

getReplacedColumns

public java.util.Map getReplacedColumns()
Returns a map from old Columns to new columns. The makeTriple(java.lang.String[], java.util.Map) method expects to find the old column names in the columnNameNumberMap, but the new columns are returned by the getSelectColumns() methods. Clients of this class must account for this when creating the columnNameNumberMap.

Returns:
a map from Columns to Columns

makeTriple

public com.hp.hpl.jena.graph.Triple makeTriple(java.lang.String[] row,
                                               java.util.Map columnNameNumberMap)
Creates a triple from a database result row.

Parameters:
row - a database result row
columnNameNumberMap - a map from column names to Integer indices into the row array
Returns:
a triple extracted from the row

removeOptionalJoins

private void removeOptionalJoins()

getAllJoinTables

private java.util.Set getAllJoinTables()

getSingleJoinReferencingTable

private Join getSingleJoinReferencingTable(java.lang.String table)
Gets a join that references a table, but only if there is exactly one join that references that table. If there is none or there are more than one, null will be returned. We use this to find joins that can be ignored. Joins can't be ignored if more than one join reference a particular table.


isOptionalTable

private boolean isOptionalTable(java.lang.String table,
                                Join join)
Checks if a table can be dropped. Only tables that are referenced in exactly one join can ever be dropped; this has to be checked before calling this method. A table can be dropped if the join covers exactly the columns that we need from this table, because in this case the same values are, by definition, present in the other table. There's an exception: If the join covers exactly all columns that we need from both tables that it connects, then we don't drop the table because of some reason I'm too tired to remember right now. Has something to do with 1:1 joins, I believe. If you read this, you may send me flame mail.

Parameters:
table - a table name
join - the only join referencing this table
Returns:
true if the join can be dropped

eliminateColumns

private void eliminateColumns(Join join,
                              java.lang.String tableName)
If we have determined that a table can be dropped, then we have to rewrite all references to columns in that table to the columns on the other side of the join.

Parameters:
join -
tableName -