Java Code Examples for org.apache.jena.system.Txn#executeRead()

The following examples show how to use org.apache.jena.system.Txn#executeRead() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestRDFChangesCancel.java    From rdf-delta with Apache License 2.0 6 votes vote down vote up
@Test public void changeSuppressEmptyCommit_4() {
    Quad q = SSE.parseQuad("(_ :s :p 'object')");
    Triple t = SSE.parseTriple("(:t :p 'object')");

    Txn.executeRead(dsg,   ()->{});
    testCounters(counter.summary(), 0, 0);

    Txn.executeWrite(dsg,  ()->{dsg.add(q);});
    testCounters(counter.summary(), 1, 0);

    Txn.executeWrite(dsg,  ()->{dsg.getDefaultGraph().add(t);});
    testCounters(counter.summary(), 2, 0);

    Txn.executeWrite(dsg,  ()->{dsg.getDefaultGraph().getPrefixMapping().setNsPrefix("", "http://example/");});
    testCounters(counter.summary(), 2, 1);

    Txn.executeWrite(dsg,  ()->{});
    testCounters(counter.summary(), 2, 1);
}
 
Example 2
Source File: TestRestart.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
private static void test(Id dsRef, DeltaClient dClient, int numQuads) {
    try(DeltaConnection dConn = dClient.get(dsRef)) {
        DatasetGraph dsg  = dConn.getDatasetGraph();
        Txn.executeRead(dsg, ()->{
            assertEquals(numQuads, Iter.count(dsg.find()));
        });
    }
}
 
Example 3
Source File: TestRDFChangesCancel.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
@Test public void changeSuppressEmptyCommit_1() {
    Txn.executeRead(dsg, ()->{});

    PatchSummary s1 = counter.summary();
    assertEquals(0, s1.getCountTxnBegin());
    assertEquals(0, s1.getCountTxnCommit());
    assertEquals(0, s1.getCountTxnAbort());
}
 
Example 4
Source File: SimpleCyLoader.java    From rdf2neo with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * <p>Uses the {@link #getCyNodeLoader()} and {@link #getCyRelationLoader()} to map data from an RDF/TDB
 * data source into Cypher entities and load them into a pre-configured Neo4j server.</p> 
 * 
 * <p>
 * opts [ 0 ] = true, means process Cypher nodes.<br/>
 * opts [ 1 ] = true, means process Cypher relations.<br/>
 * opts [ 2 ] = true, means process Cypher indices (if {@link #getCypherIndexer()} is defined).<br/>
 * </p>
 * 
 * <p>I opts is null, all the three operations above are run in the same sequence.</p>
 *  
 * <p>Nodes are always loaded before relations, indices are created the end of all other operations 
 * this simplify relation DDL creation commands.</p>
 * 
 */
@Override
public void load ( String tdbPath, Object... opts )
{		
	try
	{
		RdfDataManager rdfMgr = this.getRdfDataManager ();

		rdfDataManager.open ( tdbPath );
		Dataset ds = rdfMgr.getDataSet ();
		
		String[] nameStr = { StringUtils.trimToEmpty ( this.getName () ) };
		if ( !nameStr [ 0 ].isEmpty () ) nameStr [ 0 ] ="[" + nameStr [ 0 ] + "] ";
		
		Txn.executeRead ( ds, () -> 
			log.info ( "{}Sending {} RDF triples to Cypher", nameStr [ 0 ], ds.getDefaultModel ().size () )
		);
		
		// Nodes
		boolean doNodes = opts != null && opts.length > 0 ? (Boolean) opts [ 0 ] : true;
		if ( doNodes ) this.getCyNodeLoader ().process ( rdfMgr, opts );

		// Relations
		boolean doRels = opts != null && opts.length > 1 ? (Boolean) opts [ 1 ] : true;
		if ( doRels ) this.getCyRelationLoader ().process ( rdfMgr, opts );

		
		// User-defined indices
		boolean doIdx = opts != null && opts.length > 2 ? (Boolean) opts [ 2 ] : true;

		if ( doIdx ) {
			CypherIndexer indexer = this.getCypherIndexer ();
			if ( indexer != null ) indexer.index ();
		}
		
		log.info ( "{}RDF-Cypher conversion finished", nameStr [ 0 ] );
	}
	catch ( Exception ex ) {
		throw new RuntimeException ( "Error while running the RDF/Cypher loader:" + ex.getMessage (), ex );
	}
}
 
Example 5
Source File: CypherHandlersIT.java    From rdf2neo with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Tests {@link CyRelationLoadingHandler} to see if relations are mapped from RDF and loaded into Neo4J.
 */
@Test
public void testRelations () throws Exception
{
	try (	
		Driver neoDriver = GraphDatabase.driver( "bolt://127.0.0.1:7687", AuthTokens.basic ( "neo4j", "test" ) );
		RdfDataManager rdfMgr = new RdfDataManager ( RdfDataManagerTest.TDB_PATH );
	)
	{
		CyRelationLoadingHandler handler = new CyRelationLoadingHandler ();
		Neo4jDataManager neoMgr = new Neo4jDataManager ( neoDriver );

		handler.setRdfDataManager ( rdfMgr );
		handler.setNeo4jDataManager ( neoMgr );
		handler.setRelationTypesSparql ( RdfDataManagerTest.SPARQL_REL_TYPES );
		handler.setRelationPropsSparql ( RdfDataManagerTest.SPARQL_REL_PROPS  );

		Set<QuerySolution> relSparqlRows = new HashSet<> ();
		Dataset dataSet = rdfMgr.getDataSet ();
		Txn.executeRead ( dataSet,  () ->
			SparqlUtils.select ( RdfDataManagerTest.SPARQL_REL_TYPES, rdfMgr.getDataSet ().getDefaultModel () )
				.forEachRemaining ( row -> relSparqlRows.add ( row ) )
		);

		handler.accept ( relSparqlRows );

		// Verify
		
		CypherTester tester = new CypherTester ( neoMgr );
		
		Assert.assertTrue (
			"Wrong count for relations",
			tester.ask ( "MATCH ()-[r]->() RETURN COUNT ( r ) = 3" )
		);

		Assert.assertTrue (
			"Wrong count for {1 relatedTo 2}!",
			tester.ask ( 
				"MATCH p = (:TestNode{ iri:$iri1 })-[:relatedTo]->(:TestNode{ iri:$iri2 }) RETURN COUNT ( p ) = 1",
				"iri1", iri ( "ex:1" ), "iri2", iri ( "ex:2" )
			)
		);
		
		Assert.assertTrue (
			"Wrong count for {3 derivedFrom 1}!",
			tester.ask ( 
				"MATCH p = (:SuperTestNode{ iri:$iri1 })-[:derivedFrom]->(:TestNode{ iri:$iri2 }) RETURN COUNT ( p ) = 1",
				"iri1", iri ( "ex:3" ), "iri2", iri ( "ex:1" )
			)
		);

		Assert.assertTrue (
			"Wrong count for {3 derivedFrom 1}!",
			tester.ask ( 
				"MATCH p = (:SuperTestNode{ iri:$iri1 })-[:derivedFrom]->(:TestNode{ iri:$iri2 }) RETURN COUNT ( p ) = 1",
				"iri1", iri ( "ex:3" ), "iri2", iri ( "ex:1" )
			)
		);

		Assert.assertTrue (
			"Wrong count for {3 derivedFrom 1}!",
			tester.ask ( 
				"MATCH p = (:SuperTestNode{ iri:$iri1 })-[:derivedFrom]->(:TestNode{ iri:$iri2 }) RETURN COUNT ( p ) = 1",
				"iri1", iri ( "ex:3" ), "iri2", iri ( "ex:1" )
			)
		);
		
		
		Assert.assertTrue (
			"reified relation, wrong property value for 'note'!",
			tester.compare (
				// Test against the Cypher result
				notesv -> {
					List<Object> notes = notesv.asList ();
					if ( notes == null || notes.isEmpty () ) return false;

					// notes collection is sorted, then compared to the sorted values in the reference
					return notes
						.stream ()
						.sorted ()
						.collect ( Collectors.toList () )
						.equals ( 
							Arrays.asList ( new String[] { "Another Note", "Reified Relation" } )
						);
				},
				// the relation containing .note
				"MATCH (:TestNode{ iri:$iri1 })-[r:relatedTo]->(:AdditionalLabel{ iri:$iri2 })\n"
				+ "RETURN r.note\n",
				"iri1", iri ( "ex:2" ), "iri2", iri ( "ex:3" )
			)
		);
	} // try
}
 
Example 6
Source File: TestManagedDatasetBuilder.java    From rdf-delta with Apache License 2.0 4 votes vote down vote up
@Test public void buildManaged_2_sameZone() {
    Zone zone = Zone.connect(Location.mem());
    DatasetGraph dsg1 = ManagedDatasetBuilder.create()
        .deltaLink(deltaLink)
        .logName("ABC")
        .zone(zone)
        .syncPolicy(SyncPolicy.TXN_RW)
        .storageType(LocalStorageType.MEM)
        .build();
    // Rebuild from with same zone. Should get the same underlying storage database.
    // i.e. same Zone item.

    DeltaLink deltaLink2 = DeltaLinkLocal.connect(localServer);
    DatasetGraph dsg2 = ManagedDatasetBuilder.create()
        .deltaLink(deltaLink2)
        .logName("ABC")
        .zone(zone)
        .syncPolicy(SyncPolicy.TXN_RW)
        .storageType(LocalStorageType.MEM)
        .build();

    assertNotSame(dsg1, dsg2);
    DeltaConnection conn1 = (DeltaConnection)(dsg1.getContext().get(symDeltaConnection));
    DeltaConnection conn2 = (DeltaConnection)(dsg2.getContext().get(symDeltaConnection));
    // Same zone, same underlying storage client-side
    assertSame(conn1.getStorage(), conn2.getStorage());

    Quad q1 = SSE.parseQuad("(:g :s :p 1)");
    Quad q2 = SSE.parseQuad("(:g :s :p 2)");
    Txn.executeWrite(dsg1, ()->dsg1.add(q1));
    Txn.executeRead(dsg2, ()->assertTrue(dsg2.contains(q1)));

    try ( DeltaConnection c = conn1 ) {
        DatasetGraph dsg  = c.getDatasetGraph();
        Txn.executeRead(  c.getDatasetGraph(), ()->assertTrue(c.getDatasetGraph().contains(q1)) );
        Txn.executeWrite( c.getDatasetGraph(), ()->c.getDatasetGraph().add(q2) );
    }
    try ( DeltaConnection c = conn2 ) {
        Txn.executeRead(c.getDatasetGraph(), ()->assertTrue(c.getDatasetGraph().contains(q2)));
    }
}
 
Example 7
Source File: TestManagedDatasetBuilder.java    From rdf-delta with Apache License 2.0 4 votes vote down vote up
@Test public void buildManaged_3_differentZones() {
    // Different zones, same local server.
    Zone zone1 = Zone.connect(Location.mem());
    Zone zone2 = Zone.connect(Location.mem());
    DatasetGraph dsg1 = ManagedDatasetBuilder.create()
        .deltaLink(deltaLink)
        .logName("BCD")
        .zone(zone1)
        .syncPolicy(SyncPolicy.TXN_RW)
        .storageType(LocalStorageType.MEM)
        .build();
    // Different zones.

    DeltaLink deltaLink2 = DeltaLinkLocal.connect(localServer);
    DatasetGraph dsg2 = ManagedDatasetBuilder.create()
        .deltaLink(deltaLink2)
        .logName("BCD")
        .zone(zone2)
        .syncPolicy(SyncPolicy.TXN_RW)
        .storageType(LocalStorageType.MEM)
        .build();

    Quad q1 = SSE.parseQuad("(:g :s :p 1)");
    Quad q2 = SSE.parseQuad("(:g :s :p 2)");
    Txn.executeWrite(dsg1, ()->dsg1.add(q1));
    Txn.executeRead(dsg2, ()->assertTrue(dsg2.contains(q1)));

    DeltaConnection conn1 = (DeltaConnection)(dsg1.getContext().get(symDeltaConnection));
    DeltaConnection conn2 = (DeltaConnection)(dsg2.getContext().get(symDeltaConnection));

    assertNotSame(conn1.getStorage(), conn2.getStorage());

    try ( DeltaConnection c = conn1 ) {
        DatasetGraph dsg  = c.getDatasetGraph();
        Txn.executeRead(  c.getDatasetGraph(), ()->assertTrue(dsg2.contains(q1)) );
        Txn.executeWrite( c.getDatasetGraph(), ()->c.getDatasetGraph().add(q2) );
    }
    try ( DeltaConnection c = conn2 ) {
        Txn.executeRead(c.getDatasetGraph(), ()->assertTrue(dsg2.contains(q2)));
    }
}
 
Example 8
Source File: TestManagedDatasetBuilder2.java    From rdf-delta with Apache License 2.0 4 votes vote down vote up
@Test public void buildManaged_persistentZone() {
    // Restart.
    Location loc = Location.create(ZONE_DIR);
    Zone zone = Zone.connect(loc);

    Quad q1 = SSE.parseQuad("(:g :s :p 1)");
    Quad q2 = SSE.parseQuad("(:g :s :p 2)");

    {
        DatasetGraph dsg = ManagedDatasetBuilder.create()
            .deltaLink(deltaLink)
            .logName("ABD")
            .zone(zone)
            .syncPolicy(SyncPolicy.TXN_RW)
            .storageType(LocalStorageType.TDB2)
            .build();

        DeltaConnection conn = (DeltaConnection)(dsg.getContext().get(symDeltaConnection));
        Txn.executeWrite(dsg, ()->dsg.add(q1));
        Txn.executeRead(  conn.getDatasetGraph(), ()->assertTrue(conn.getDatasetGraph().contains(q1)) );
    }

    // Same zone
    Zone.clearZoneCache();
    zone = Zone.connect(loc);
    // Storage should be recovered from the on-disk state.

    {
        DatasetGraph dsg1 = ManagedDatasetBuilder.create()
            .deltaLink(deltaLink)
            .logName("ABD")
            .zone(zone)
            .syncPolicy(SyncPolicy.TXN_RW)
            //.storageType(LocalStorageType.TDB2) // Storage required. Should detect [FIXME]
            .build();
        DatasetGraph dsg2 = ManagedDatasetBuilder.create()
            .deltaLink(deltaLink)
            .logName("ABD")
            .zone(zone)
            .syncPolicy(SyncPolicy.TXN_RW)
            //.storageType(LocalStorageType.TDB) // Wrong storage - does not matter; dsg1 setup choice applies
            .build();
        DeltaConnection conn1 = (DeltaConnection)(dsg1.getContext().get(symDeltaConnection));
        DeltaConnection conn2 = (DeltaConnection)(dsg2.getContext().get(symDeltaConnection));

        Txn.executeRead(conn1.getDatasetGraph(), ()->assertTrue(conn1.getDatasetGraph().contains(q1)) );
        Txn.executeWrite(conn2.getDatasetGraph(), ()->conn2.getDatasetGraph().add(q2));
        Txn.executeRead(conn1.getDatasetGraph(), ()->assertTrue(conn1.getDatasetGraph().contains(q2)) );
    }
}