You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by jp...@apache.org on 2014/07/17 09:24:21 UTC

svn commit: r1611267 - in /jena/Experimental/jena-csv/src: main/java/com/hp/hpl/jena/propertytable/impl/ test/java/com/hp/hpl/jena/propertytable/impl/

Author: jpz6311whu
Date: Thu Jul 17 07:24:21 2014
New Revision: 1611267

URL: http://svn.apache.org/r1611267
Log:
Add the implementation of PropertyTable with a two dimension array

Added:
    jena/Experimental/jena-csv/src/main/java/com/hp/hpl/jena/propertytable/impl/PropertyTableArrayImpl.java   (with props)
    jena/Experimental/jena-csv/src/main/java/com/hp/hpl/jena/propertytable/impl/PropertyTableHashMapImpl.java
      - copied, changed from r1608984, jena/Experimental/jena-csv/src/main/java/com/hp/hpl/jena/propertytable/impl/PropertyTableImpl.java
Removed:
    jena/Experimental/jena-csv/src/main/java/com/hp/hpl/jena/propertytable/impl/PropertyTableImpl.java
Modified:
    jena/Experimental/jena-csv/src/main/java/com/hp/hpl/jena/propertytable/impl/GraphCSV.java
    jena/Experimental/jena-csv/src/main/java/com/hp/hpl/jena/propertytable/impl/PropertyTableBuilder.java
    jena/Experimental/jena-csv/src/test/java/com/hp/hpl/jena/propertytable/impl/GraphCSVTest.java

Modified: jena/Experimental/jena-csv/src/main/java/com/hp/hpl/jena/propertytable/impl/GraphCSV.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-csv/src/main/java/com/hp/hpl/jena/propertytable/impl/GraphCSV.java?rev=1611267&r1=1611266&r2=1611267&view=diff
==============================================================================
--- jena/Experimental/jena-csv/src/main/java/com/hp/hpl/jena/propertytable/impl/GraphCSV.java (original)
+++ jena/Experimental/jena-csv/src/main/java/com/hp/hpl/jena/propertytable/impl/GraphCSV.java Thu Jul 17 07:24:21 2014
@@ -18,9 +18,36 @@
 
 package com.hp.hpl.jena.propertytable.impl;
 
+import com.hp.hpl.jena.propertytable.PropertyTable;
+
 public class GraphCSV extends GraphPropertyTable {
 	
-	public GraphCSV(String csvFilePath){
-		super(PropertyTableBuilder.buildPropetyTableFromCSVFile(csvFilePath));
+	public static GraphCSV createHashMapImpl( String csvFilePath ){
+		return new GraphCSVHashMapImpl(csvFilePath);
+	}
+	
+	public static GraphCSV createArrayImpl( String csvFilePath ){
+		return new GraphCSVArrayImpl(csvFilePath);
+	}
+	
+	protected GraphCSV (PropertyTable table) {
+		super(table);
+	}
+	
+	public GraphCSV ( String csvFilePath ){
+		super(PropertyTableBuilder.buildPropetyTableArrayImplFromCsv(csvFilePath));
+	}
+}
+
+
+class GraphCSVHashMapImpl extends GraphCSV{
+	protected GraphCSVHashMapImpl(String csvFilePath){
+		super(PropertyTableBuilder.buildPropetyTableHashMapImplFromCsv(csvFilePath));
+	}
+}
+
+class GraphCSVArrayImpl extends GraphCSV{
+	protected GraphCSVArrayImpl(String csvFilePath){
+		super(PropertyTableBuilder.buildPropetyTableArrayImplFromCsv(csvFilePath));
 	}
 }

Added: jena/Experimental/jena-csv/src/main/java/com/hp/hpl/jena/propertytable/impl/PropertyTableArrayImpl.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-csv/src/main/java/com/hp/hpl/jena/propertytable/impl/PropertyTableArrayImpl.java?rev=1611267&view=auto
==============================================================================
--- jena/Experimental/jena-csv/src/main/java/com/hp/hpl/jena/propertytable/impl/PropertyTableArrayImpl.java (added)
+++ jena/Experimental/jena-csv/src/main/java/com/hp/hpl/jena/propertytable/impl/PropertyTableArrayImpl.java Thu Jul 17 07:24:21 2014
@@ -0,0 +1,272 @@
+package com.hp.hpl.jena.propertytable.impl;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.jena.atlas.iterator.Iter;
+import org.apache.jena.atlas.iterator.IteratorConcat;
+
+import com.hp.hpl.jena.graph.Node;
+import com.hp.hpl.jena.graph.Triple;
+import com.hp.hpl.jena.propertytable.Column;
+import com.hp.hpl.jena.propertytable.PropertyTable;
+import com.hp.hpl.jena.propertytable.Row;
+import com.hp.hpl.jena.util.iterator.ExtendedIterator;
+import com.hp.hpl.jena.util.iterator.WrappedIterator;
+
+public class PropertyTableArrayImpl implements PropertyTable {
+	
+	private final List<Node> rowList;
+	private final List<Node> columnList;
+
+	private final Map<Node, Integer> rowKeyToIndex;
+	private final Map<Node, Integer> columnKeyToIndex;
+	private final Node[][] array;
+	private final int rowNum;
+	private final int columnNum;
+	
+	public PropertyTableArrayImpl(int rowNum, int columnNum){
+		rowList = new ArrayList<Node>(rowNum);
+		columnList = new ArrayList<Node>(columnNum);
+		rowKeyToIndex = new HashMap<Node, Integer>();
+		columnKeyToIndex = new HashMap<Node, Integer>();
+		this.rowNum = rowNum;
+		this.columnNum = columnNum;
+		array = new Node [rowNum][columnNum];
+	}
+
+	@Override
+	public ExtendedIterator<Triple> getTripleIterator(Column column, Node value) {
+		if (column == null || column.getColumnKey() == null)
+			throw new NullPointerException("column is null");
+		
+		if (value == null){
+			throw new NullPointerException("value is null");
+		}
+		
+		ArrayList<Triple> triples = new ArrayList<Triple>();
+		
+		Node p = column.getColumnKey();
+		Integer columnIndex = this.columnKeyToIndex.get(p);
+		if (columnIndex != null){
+			for(int rowIndex=0; rowIndex< rowList.size();rowIndex++){
+				if ( value.equals( this.get(rowIndex, columnIndex))){
+					triples.add(Triple.create(rowList.get(rowIndex), p, value));
+				}
+			}
+		}
+		return WrappedIterator.create(triples.iterator());
+	}
+
+	@Override
+	public ExtendedIterator<Triple> getTripleIterator(Column column) {
+		
+		if (column == null || column.getColumnKey() == null)
+			throw new NullPointerException("column is null");
+		
+		ArrayList<Triple> triples = new ArrayList<Triple>();
+		
+		Node p = column.getColumnKey();
+		Integer columnIndex = this.columnKeyToIndex.get(p);
+		if (columnIndex != null){
+			for(int rowIndex=0; rowIndex< rowList.size();rowIndex++){
+				triples.add(Triple.create(rowList.get(rowIndex), p, this.get(rowIndex, columnIndex)));
+			}
+		}
+		return WrappedIterator.create(triples.iterator());
+	}
+
+	@Override
+	public ExtendedIterator<Triple> getTripleIterator(Node value) {
+		if (value == null)
+			throw new NullPointerException("value is null");
+		
+		IteratorConcat<Triple> iter = new IteratorConcat<Triple>();
+		for (Column column : this.getColumns()) {
+			ExtendedIterator<Triple> eIter = getTripleIterator(column,value);
+			iter.add(eIter);
+		}
+		return WrappedIterator.create(Iter.distinct(iter));
+	}
+
+	@Override
+	public ExtendedIterator<Triple> getTripleIterator(Row row) {
+		if (row == null || row.getRowKey() == null)
+			throw new NullPointerException("row is null");
+		
+		ArrayList<Triple> triples = new ArrayList<Triple>();
+	    Integer rowIndex = this.rowKeyToIndex.get(row.getRowKey());
+
+		if (rowIndex != null){
+			for(int columnIndex=0; columnIndex < columnList.size(); columnIndex++){
+				triples.add(Triple.create( row.getRowKey(), columnList.get(columnIndex), this.get(rowIndex, columnIndex)));
+			}
+		}
+		return WrappedIterator.create(triples.iterator());
+	}
+
+	@Override
+	public ExtendedIterator<Triple> getTripleIterator() {
+
+		IteratorConcat<Triple> iter = new IteratorConcat<Triple>();
+		for (Column column : getColumns()) {
+			iter.add(getTripleIterator(column));
+		}
+		return WrappedIterator.create(Iter.distinct(iter));
+	}
+
+	@Override
+	public Collection<Column> getColumns() {
+		Collection<Column> columns = new ArrayList<Column>();
+		for(Node p: columnKeyToIndex.keySet() ){
+			columns.add(new ColumnImpl(this, p));
+		}
+		return columns;
+	}
+
+	@Override
+	public Column getColumn(Node p) {
+		if (p == null)
+			throw new NullPointerException("column name is null");
+	    Integer columnIndex = columnKeyToIndex.get(p);
+	    return (columnIndex == null)
+	        ? null : new ColumnImpl(this, p);
+	}
+
+	@Override
+	public void createColumn(Node p) {
+		if (p == null)
+			throw new NullPointerException("column name is null");
+
+		if (columnKeyToIndex.containsKey(p))
+			throw new IllegalArgumentException("column already exists: '"
+					+ p.toString());
+		
+		if (columnList.size()>= columnNum)
+			throw new IllegalArgumentException("cannot create new column for max column count: " + columnNum);
+		
+		columnList.add(p);
+		columnKeyToIndex.put(p, columnList.indexOf(p));
+	}
+
+	@Override
+	public Row getRow(Node s) {
+		if (s == null)
+			throw new NullPointerException("subject node is null");
+		
+		Integer rowIndex = rowKeyToIndex.get(s);
+		return (rowIndex == null) ? null : new InternalRow(rowIndex);
+	}
+
+	@Override
+	public Row createRow(Node s) {
+		Row row = this.getRow(s);
+		if (row != null)
+			return row;
+
+		if (rowList.size()>= rowNum)
+			throw new IllegalArgumentException("cannot create new row for max row count: " + rowNum);
+		
+		rowList.add(s);
+		int rowIndex = rowList.indexOf(s);
+		rowKeyToIndex.put(s, rowIndex);		
+		
+		return new InternalRow(rowIndex);
+	}
+	
+	private void set(int rowIndex, int columnIndex, Node value) {
+		
+		if (rowIndex >= rowList.size())
+			throw new IllegalArgumentException("row index out of bound: " + rowList.size());
+		if (columnIndex >= columnList.size())
+			throw new IllegalArgumentException("column index out of bound: " + columnList.size());
+		array[rowIndex][columnIndex] = value;
+	}
+	
+	public Node get(int rowIndex, int columnIndex) {
+	    if (rowIndex >= rowList.size())
+			throw new IllegalArgumentException("row index out of bound: " + rowList.size());
+		if (columnIndex >= columnList.size())
+			throw new IllegalArgumentException("column index out of bound: " + columnList.size());
+		return array[rowIndex][columnIndex];
+    }
+
+	@Override
+	public ExtendedIterator<Row> getRowIterator() {
+		ArrayList<Row> rows = new ArrayList<Row>();
+		for (int rowIndex=0;rowIndex<rowList.size();rowIndex++){
+			rows.add( new InternalRow(rowIndex));
+		}
+		return WrappedIterator.create(rows.iterator());
+	}
+	
+	private final class InternalRow implements Row {
+
+	    final int rowIndex;
+
+	    InternalRow(int rowIndex) {
+	      this.rowIndex = rowIndex;
+	    }
+		
+		@Override
+		public PropertyTable getTable() {
+			return PropertyTableArrayImpl.this;
+		}
+
+		@Override
+		public void setValue(Column column, Node value) {
+			if (column == null || column.getColumnKey() == null)
+				throw new NullPointerException("column is null");
+			
+		    Integer columnIndex = columnKeyToIndex.get(column.getColumnKey());
+		    if (columnIndex == null)
+		    	throw new IllegalArgumentException("column index does not exist: " + column.getColumnKey());
+
+		    set(rowIndex, columnIndex, value);
+			
+		}
+
+		@Override
+		public Node getValue(Column column) {
+			if (column == null)
+				throw new NullPointerException("column is null");
+			return this.getValue(column.getColumnKey());
+		}
+
+		@Override
+		public Node getValue(Node columnKey) {
+			if (columnKey == null)
+				throw new NullPointerException("column key is null");
+			
+		    Integer columnIndex = columnKeyToIndex.get(columnKey);
+		    if (columnIndex == null)
+		    	throw new IllegalArgumentException("column index does not exist: " + columnKey);
+		    
+		    return get(rowIndex, columnIndex);
+		}
+
+		@Override
+		public Node getRowKey() {
+			return rowList.get(rowIndex);
+		}
+
+		@Override
+		public ExtendedIterator<Triple> getTripleIterator() {
+			ArrayList<Triple> triples = new ArrayList<Triple>();
+			for (int columnIndex=0;columnIndex<columnList.size();columnIndex++) {
+				triples.add(Triple.create(getRowKey(), columnList.get(columnIndex), get(rowIndex, columnIndex)));
+			}
+			return WrappedIterator.create(triples.iterator());
+		}
+
+		@Override
+		public Collection<Column> getColumns() {
+			return PropertyTableArrayImpl.this.getColumns();
+		}
+		
+	}
+
+}

Propchange: jena/Experimental/jena-csv/src/main/java/com/hp/hpl/jena/propertytable/impl/PropertyTableArrayImpl.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jena/Experimental/jena-csv/src/main/java/com/hp/hpl/jena/propertytable/impl/PropertyTableBuilder.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-csv/src/main/java/com/hp/hpl/jena/propertytable/impl/PropertyTableBuilder.java?rev=1611267&r1=1611266&r2=1611267&view=diff
==============================================================================
--- jena/Experimental/jena-csv/src/main/java/com/hp/hpl/jena/propertytable/impl/PropertyTableBuilder.java (original)
+++ jena/Experimental/jena-csv/src/main/java/com/hp/hpl/jena/propertytable/impl/PropertyTableBuilder.java Thu Jul 17 07:24:21 2014
@@ -30,17 +30,46 @@ import com.hp.hpl.jena.graph.Node;
 import com.hp.hpl.jena.graph.NodeFactory;
 import com.hp.hpl.jena.propertytable.PropertyTable;
 import com.hp.hpl.jena.propertytable.Row;
-import com.hp.hpl.jena.util.FileUtils;
 
 public class PropertyTableBuilder {
 
 	public static Node CSV_ROW_NODE = NodeFactory.createURI(LangCSV.CSV_ROW);
-
-	public static PropertyTable buildPropetyTableFromCSVFile(String csvFilePath) {
-
+	
+	public static PropertyTable buildPropetyTableHashMapImplFromCsv(String csvFilePath) {		
+		PropertyTable table = new PropertyTableHashMapImpl();
+		return fillPropertyTable(table, csvFilePath);
+	}
+	
+	public static PropertyTable buildPropetyTableArrayImplFromCsv(String csvFilePath) {
+		PropertyTable table = createEmptyPropertyTableArrayImpl(csvFilePath);
+		return fillPropertyTable(table, csvFilePath);
+	}
+	
+	private static PropertyTable createEmptyPropertyTableArrayImpl (String csvFilePath) {
 		CSVParser parser = CSVParser.create(csvFilePath);
-		PropertyTableImpl table = new PropertyTableImpl();
+		List<String> rowLine = null;
+		int rowNum = 0;
+		int columnNum = 0;
+		
+		while ((rowLine = parser.parse1()) != null) {
+			if (rowNum == 0) {
+				columnNum = rowLine.size();
+			}
+			rowNum++;
+		}
+		if (rowNum!=0 && columnNum!=0){
+			return new PropertyTableArrayImpl(rowNum, columnNum+1);
+		} else {
+			return null;
+		}
+	}
 
+	
+	private static PropertyTable fillPropertyTable(PropertyTable table, String csvFilePath ){
+		if (table == null){
+			return null;
+		}
+		CSVParser parser = CSVParser.create(csvFilePath);
 		List<String> rowLine = null;
 		ArrayList<Node> predicates = new ArrayList<Node>();
 		int rowNum = 0;

Copied: jena/Experimental/jena-csv/src/main/java/com/hp/hpl/jena/propertytable/impl/PropertyTableHashMapImpl.java (from r1608984, jena/Experimental/jena-csv/src/main/java/com/hp/hpl/jena/propertytable/impl/PropertyTableImpl.java)
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-csv/src/main/java/com/hp/hpl/jena/propertytable/impl/PropertyTableHashMapImpl.java?p2=jena/Experimental/jena-csv/src/main/java/com/hp/hpl/jena/propertytable/impl/PropertyTableHashMapImpl.java&p1=jena/Experimental/jena-csv/src/main/java/com/hp/hpl/jena/propertytable/impl/PropertyTableImpl.java&r1=1608984&r2=1611267&rev=1611267&view=diff
==============================================================================
--- jena/Experimental/jena-csv/src/main/java/com/hp/hpl/jena/propertytable/impl/PropertyTableImpl.java (original)
+++ jena/Experimental/jena-csv/src/main/java/com/hp/hpl/jena/propertytable/impl/PropertyTableHashMapImpl.java Thu Jul 17 07:24:21 2014
@@ -44,7 +44,7 @@ import com.hp.hpl.jena.util.iterator.Wra
  * It contains PSO and POS indexes.
  * 
  */
-public class PropertyTableImpl implements PropertyTable {
+public class PropertyTableHashMapImpl implements PropertyTable {
 
 	private Map<Node, Column> columnIndex; // Maps property Node key to Column
 	private List<Column> columnList; // Stores the list of columns in the table
@@ -63,7 +63,7 @@ public class PropertyTableImpl implement
 																	// Node)
 																	// pairs
 
-	PropertyTableImpl() {
+	PropertyTableHashMapImpl() {
 		columnIndex = new HashMap<Node, Column>();
 		columnList = new ArrayList<Column>();
 		rowIndex = new HashMap<Node, Row>();
@@ -87,6 +87,10 @@ public class PropertyTableImpl implement
 	public ExtendedIterator<Triple> getTripleIterator(Column column) {
 		
 		// use PSO index directly (fast)
+		
+		if (column == null || column.getColumnKey() == null)
+			throw new NullPointerException("column is null");
+		
 		ArrayList<Triple> triples = new ArrayList<Triple>();
 		Map<Node, Node> values = valueIndex.get(column.getColumnKey());
 
@@ -102,9 +106,13 @@ public class PropertyTableImpl implement
 	public ExtendedIterator<Triple> getTripleIterator(Node value) {
 		
 		// use POS index ( O(n), n= column count )
+		
+		if (value == null)
+			throw new NullPointerException("value is null");
+		
 		IteratorConcat<Triple> iter = new IteratorConcat<Triple>();
 		for (Column column : this.getColumns()) {
-			ExtendedIterator<Triple> eIter = getTripleIterator(column);
+			ExtendedIterator<Triple> eIter = getTripleIterator(column,value);
 			iter.add(eIter);
 		}
 		return WrappedIterator.create(Iter.distinct(iter));
@@ -114,6 +122,14 @@ public class PropertyTableImpl implement
 	public ExtendedIterator<Triple> getTripleIterator(Column column, Node value) {
 		
 		// use POS index directly (fast)
+		
+		if (column == null || column.getColumnKey() == null)
+			throw new NullPointerException("column is null");
+		
+		if (value == null)
+			throw new NullPointerException("value is null");
+		
+		
 		Node p = column.getColumnKey();
 		final SetMultimap<Node, Node> valueToSubjectMap = valueReverseIndex
 				.get(p);
@@ -129,6 +145,10 @@ public class PropertyTableImpl implement
 	@Override
 	public ExtendedIterator<Triple> getTripleIterator(Row row) {
 		// use PSO index ( O(n), n= column count )
+		
+		if (row == null || row.getRowKey() == null)
+			throw new NullPointerException("row is null");
+		
 		ArrayList<Triple> triples = new ArrayList<Triple>();
 		for (Column column : getColumns()) {
 			Node value = row.getValue(column);
@@ -144,13 +164,15 @@ public class PropertyTableImpl implement
 
 	@Override
 	public Column getColumn(Node p) {
+		if (p == null)
+			throw new NullPointerException("column node is null");
 		return columnIndex.get(p);
 	}
 
 	@Override
 	public void createColumn(Node p) {
 		if (p == null)
-			throw new NullPointerException("column name is null");
+			throw new NullPointerException("column node is null");
 
 		if (columnIndex.containsKey(p))
 			throw new IllegalArgumentException("column already exists: '"
@@ -270,7 +292,7 @@ public class PropertyTableImpl implement
 
 		@Override
 		public PropertyTable getTable() {
-			return PropertyTableImpl.this;
+			return PropertyTableHashMapImpl.this;
 		}
 
 		@Override
@@ -281,7 +303,7 @@ public class PropertyTableImpl implement
 		@Override
 		public Collection<Column> getColumns() {
 			// TODO Auto-generated method stub
-			return PropertyTableImpl.this.getColumns();
+			return PropertyTableHashMapImpl.this.getColumns();
 		}
 
 		@Override

Modified: jena/Experimental/jena-csv/src/test/java/com/hp/hpl/jena/propertytable/impl/GraphCSVTest.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-csv/src/test/java/com/hp/hpl/jena/propertytable/impl/GraphCSVTest.java?rev=1611267&r1=1611266&r2=1611267&view=diff
==============================================================================
--- jena/Experimental/jena-csv/src/test/java/com/hp/hpl/jena/propertytable/impl/GraphCSVTest.java (original)
+++ jena/Experimental/jena-csv/src/test/java/com/hp/hpl/jena/propertytable/impl/GraphCSVTest.java Thu Jul 17 07:24:21 2014
@@ -34,10 +34,10 @@ import com.hp.hpl.jena.sparql.algebra.Al
 import com.hp.hpl.jena.sparql.algebra.Op;
 import com.hp.hpl.jena.sparql.engine.main.StageBuilder;
 import com.hp.hpl.jena.sparql.engine.main.StageGenerator;
+import com.hp.hpl.jena.util.PrintUtil;
 
 public class GraphCSVTest extends Assert {
 	
-	@Ignore
 	@Test
 	public void testGraphCSV() throws Exception {
 		String file = "src/test/resources/test.csv";