Search in sources :

Example 1 with CSVParser

use of org.apache.jena.atlas.csv.CSVParser in project jena by apache.

the class AbstractPropertyTableBuilderTest method testFillPropertyTable.

@Test
public void testFillPropertyTable() {
    CSVParser iterator = csv("a,b\nc,d\ne,f");
    PropertyTableBuilder.fillPropertyTable(table, iterator, csvFilePath);
    Assert.assertEquals(3, table.getColumns().size());
    containsColumn(PropertyTableBuilder.CSV_ROW_NODE);
    containsColumn("a");
    containsColumn("b");
    Assert.assertEquals(2, table.getAllRows().size());
    containsValue(0, "a", "c");
    containsValue(0, "b", "d");
    containsValue(1, "a", "e");
    containsValue(1, "b", "f");
}
Also used : CSVParser(org.apache.jena.atlas.csv.CSVParser) BaseTest(org.apache.jena.propertytable.BaseTest) Test(org.junit.Test)

Example 2 with CSVParser

use of org.apache.jena.atlas.csv.CSVParser in project jena by apache.

the class AbstractPropertyTableBuilderTest method testIrregularTable3.

@Test
public void testIrregularTable3() {
    CSVParser iterator = csv("a,b\n,d\ne,f");
    PropertyTableBuilder.fillPropertyTable(table, iterator, csvFilePath);
    Assert.assertEquals(3, table.getColumns().size());
    containsColumn(PropertyTableBuilder.CSV_ROW_NODE);
    containsColumn("a");
    containsColumn("b");
    Assert.assertEquals(2, table.getAllRows().size());
    nullValue(0, "a");
    containsValue(0, "b", "d");
    containsValue(1, "a", "e");
    containsValue(1, "b", "f");
}
Also used : CSVParser(org.apache.jena.atlas.csv.CSVParser) BaseTest(org.apache.jena.propertytable.BaseTest) Test(org.junit.Test)

Example 3 with CSVParser

use of org.apache.jena.atlas.csv.CSVParser in project jena by apache.

the class AbstractPropertyTableBuilderTest method testIrregularTable1.

@Test
public void testIrregularTable1() {
    CSVParser iterator = csv("a,b\nc\ne,f");
    PropertyTableBuilder.fillPropertyTable(table, iterator, csvFilePath);
    Assert.assertEquals(3, table.getColumns().size());
    containsColumn(PropertyTableBuilder.CSV_ROW_NODE);
    containsColumn("a");
    containsColumn("b");
    Assert.assertEquals(2, table.getAllRows().size());
    containsValue(0, "a", "c");
    nullValue(0, "b");
    containsValue(1, "a", "e");
    containsValue(1, "b", "f");
}
Also used : CSVParser(org.apache.jena.atlas.csv.CSVParser) BaseTest(org.apache.jena.propertytable.BaseTest) Test(org.junit.Test)

Example 4 with CSVParser

use of org.apache.jena.atlas.csv.CSVParser in project jena by apache.

the class ReaderRIOTCSV method parse.

public void parse() {
    sink.start();
    CSVParser parser = (input != null) ? CSVParser.create(input) : CSVParser.create(reader);
    ArrayList<Node> predicates = new ArrayList<>();
    int rowNum = 0;
    for (List<String> row : parser) {
        if (rowNum == 0) {
            for (String column : row) {
                String uri = IRIResolver.resolveString(filename) + "#" + toSafeLocalname(column);
                Node predicate = this.maker.createURI(uri, rowNum, 0);
                predicates.add(predicate);
            }
        } else {
            //Node subject = this.profile.createBlankNode(null, -1, -1);
            Node subject = calculateSubject(rowNum, filename);
            Node predicateRow = this.maker.createURI(CSV_ROW, -1, -1);
            Node objectRow = this.maker.createTypedLiteral((rowNum + ""), XSDDatatype.XSDinteger, rowNum, 0);
            sink.triple(this.maker.createTriple(subject, predicateRow, objectRow, rowNum, 0));
            for (int col = 0; col < row.size() && col < predicates.size(); col++) {
                Node predicate = predicates.get(col);
                String columnValue = row.get(col).trim();
                if ("".equals(columnValue)) {
                    continue;
                }
                Node o;
                try {
                    // Try for a double.
                    Double.parseDouble(columnValue);
                    o = NodeFactory.createLiteral(columnValue, XSDDatatype.XSDdouble);
                } catch (Exception e) {
                    o = NodeFactory.createLiteral(columnValue);
                }
                sink.triple(this.maker.createTriple(subject, predicate, o, rowNum, col));
            }
        }
        rowNum++;
    }
    sink.finish();
}
Also used : CSVParser(org.apache.jena.atlas.csv.CSVParser) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList)

Example 5 with CSVParser

use of org.apache.jena.atlas.csv.CSVParser in project jena by apache.

the class ReaderRIOTCSV method parse.

public void parse() {
    sink.start();
    CSVParser parser = (input != null) ? CSVParser.create(input) : CSVParser.create(reader);
    ArrayList<Node> predicates = new ArrayList<>();
    int rowNum = 0;
    for (List<String> row : parser) {
        if (rowNum == 0) {
            for (String column : row) {
                String uri = IRIResolver.resolveString(filename) + "#" + toSafeLocalname(column);
                Node predicate = this.profile.createURI(uri, rowNum, 0);
                predicates.add(predicate);
            }
        } else {
            //Node subject = this.profile.createBlankNode(null, -1, -1);
            Node subject = calculateSubject(rowNum, filename);
            Node predicateRow = this.profile.createURI(CSV_ROW, -1, -1);
            Node objectRow = this.profile.createTypedLiteral((rowNum + ""), XSDDatatype.XSDinteger, rowNum, 0);
            sink.triple(this.profile.createTriple(subject, predicateRow, objectRow, rowNum, 0));
            for (int col = 0; col < row.size() && col < predicates.size(); col++) {
                Node predicate = predicates.get(col);
                String columnValue = row.get(col).trim();
                if ("".equals(columnValue)) {
                    continue;
                }
                Node o;
                try {
                    // Try for a double.
                    Double.parseDouble(columnValue);
                    o = NodeFactory.createLiteral(columnValue, XSDDatatype.XSDdouble);
                } catch (Exception e) {
                    o = NodeFactory.createLiteral(columnValue);
                }
                sink.triple(this.profile.createTriple(subject, predicate, o, rowNum, col));
            }
        }
        rowNum++;
    }
    sink.finish();
}
Also used : CSVParser(org.apache.jena.atlas.csv.CSVParser) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList)

Aggregations

CSVParser (org.apache.jena.atlas.csv.CSVParser)10 BaseTest (org.apache.jena.propertytable.BaseTest)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 Node (org.apache.jena.graph.Node)3 Var (org.apache.jena.sparql.core.Var)2 InputStream (java.io.InputStream)1 List (java.util.List)1 Function (java.util.function.Function)1 ARQException (org.apache.jena.sparql.ARQException)1 ResultSetStream (org.apache.jena.sparql.engine.ResultSetStream)1 Binding (org.apache.jena.sparql.engine.binding.Binding)1 BindingMap (org.apache.jena.sparql.engine.binding.BindingMap)1