Hi Andy.

The following CME (sorry it's messy) does what I want but are you saying it
might not based on something now or in the future..? :-)

I create a tdb dataset load some triples into the default graph and the
same triples into named graphs g1 and g2. I wrap the dataset in a
datasetgraphmap and overload getGraph to return a RDFS model created by
wrapping the super.getGraph if the graphNode equals g1.

This works and I get the following...


package JenaInf;

import com.hp.hpl.jena.graph.Graph;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.DatasetFactory;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ReadWrite;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.sparql.core.DatasetGraphMap;
import com.hp.hpl.jena.tdb.TDBFactory;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.vocabulary.RDFS;

public class TDBInf {

static final String graphNS = "http://example.org/graph#";;
static final Resource graphGraph = ResourceFactory.createResource(graphNS +
"Graph");
 static final String graphsNS = "http://example.org/graphs/";;
static final String g1URI = graphsNS + "g1";
static final String g2URI = graphsNS + "g2";
 static final String SELECT_ALL = "select * where {graph ?g {?s ?p ?o}}";
static final String CONSTRUCT_ALL = "construct where {?s ?p ?o}";
static final String SELECT_VIA_GRAPHS = String.format("select * where {{ ?g
a <%s> }. graph ?g {<%s> ?p ?o}}", graphGraph.getURI(), "
http://example.org/trek/Triton";);

static final Dataset dataset = TDBFactory.createDataset();
 static void init() {
dataset.begin(ReadWrite.WRITE);
FileManager.get().readModel(dataset.getDefaultModel(),
"/home/dick/git/Unit4/U4DaaS/test/JenaInf/Inftest.text", "Turtle");
FileManager.get().readModel(dataset.getNamedModel(g1URI),
"/home/dick/git/Unit4/U4DaaS/test/JenaInf/InfTestG1.text", "Turtle");
FileManager.get().readModel(dataset.getNamedModel(g2URI),
"/home/dick/git/Unit4/U4DaaS/test/JenaInf/InfTestG1.text", "Turtle");
dataset.commit();
}

static void select(Dataset dataset, String select) {
info(select);
if (dataset.supportsTransactions()) {
dataset.begin(ReadWrite.READ);
}
Query query = QueryFactory.create(select);
info(ResultSetFormatter.asText(QueryExecutionFactory.create(select,
dataset).execSelect()));
if (dataset.supportsTransactions()) {
dataset.end();
}
}

static void construct(Dataset dataset, String query) {
info(query);
dataset.begin(ReadWrite.READ);
QueryExecutionFactory.create(query,
dataset).execConstruct().write(System.out, "TTL");
dataset.end();
}
 static Dataset infWrap(Dataset dataset) {
dataset.begin(ReadWrite.READ);
DatasetGraphMap dgm = new DatasetGraphMap(dataset.asDatasetGraph()) {
@Override
public Graph getGraph(Node graphNode) {
Graph g = super.getGraph(graphNode);
if (g != null && graphNode.getURI().equals(g1URI)) {
g =
ModelFactory.createRDFSModel(ModelFactory.createModelForGraph(g)).getGraph();
}
return g;
}
};
dataset.end();
return DatasetFactory.create(dgm);
}
 /**
 * @param args
 */
public static void main(String[] args) {
init();
select(dataset, SELECT_VIA_GRAPHS);
select(infWrap(dataset), SELECT_VIA_GRAPHS);
}
 static void info(final Object text) {
System.out.println(String.format("%s", text));
}
}

inftest.text

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.

@prefix graph: <http://example.org/graph#>.

@prefix graphs: <http://example.org/graphs/>.

graph:Graph
rdf:type rdfs:Class.
graphs:g1
rdf:type graph:Graph.
graphs:g2
rdf:type graph:Graph.
graphs:g3
rdf:type graph:Graph.

inftestg1.text


On 19 June 2013 12:20, Andy Seaborne <[email protected]> wrote:

> On 18/06/13 18:22, Dick Murray wrote:
>
>> I'm looking for dynamic inference based on the select. Given a dataset
>> with
>> multiple named graphs I would like the ability to wrap specific named
>> graphs based on some form of filter when the select is processed.
>>
>
> The dataset being queried can not be manipulated during the query.
>
> Whether getGraph is called is evaluator dependent (it's not in TDB which
> works on quads).
>
> There is no guarantee a query is executed in a particular order.  It coudl
> do the GRAPH bit before the dft graph access.  Currently, that unlikely,
> but there is no guarantee.  Oh - and it may happen twice for rewritten
> queries (equality optimizations like to generate multiple more grounded
> accesses).
>
>
>  Given the dataset D which contains the named graphs G1, G2, G3 I would
>> like
>> G2 to be returned with RDFS inference if it is queried in a select. I have
>> achieved this by wrapping the graph as an InfModel and using a
>> DatasetGraphMap but this requires that the graph be known before the
>> select
>> is executed. What I'm trying to find (if it exists) is the point during
>> the
>> select processing when the graph is identified and used? Does this exist
>> in
>> a TDB Dataset or is it just a set of quads?
>>
>
> Such a point exists (OpExecutor.execute(OpGraph)) but because of
> optimization and/or converting to quads.
>
> A TDB datsegraph is a set of triples (dft graph) + a set of quads (named
> graphs).    Full quad based optimization isn't really done currently but it
> will be in future so any internal approach is going to be vulnerable to
> changes.
>
> I think you need a 2-phase approach.
>
> Phase-1 is setup - query the data and determine which graphs to add
> inference to.
>
> Phase-2 : Build a new datasetgraph and then query that for the real
> answers.
>
> Maybe that's what you are doing.  If you query dgm I'd expect to see the
> RDFS inferences but it does not show where you issue the query. Complete,
> minimal example?
>
>         Andy
>
>
>> Dick.
>>
>>
>> On 18 June 2013 16:01, Andy Seaborne <[email protected]> wrote:
>>
>>  Dick,
>>>
>>> I'm not completely sure what you're trying to do - a complete minimal
>>> example showing how they bits and pieces fit together would be good.  It
>>> seems to be querying the dataset without the inference graph.  I don't
>>> see
>>> where you query the dataset (and which one)
>>>
>>>  if (graphNode.getURI().equals(****types.getURI())) {
>>>>
>>>
>>> if (graphNode.equals(types.****asNode())  {
>>>
>>>
>>>
>>>
>>> On 18/06/13 14:22, Dick Murray wrote:
>>>
>>>  Hi.
>>>>
>>>> Is it possible to get at the graph i.e. the ?g (specifically the
>>>> returned
>>>> nodes) when the following query is executed?
>>>>
>>>>
>>> Yes - getGraph / getNamedModel depending on which level your working at.
>>>
>>>
>>>   SELECT  *
>>>
>>>> WHERE
>>>>     { { ?g 
>>>> <http://www.w3.org/1999/02/22-****rdf-syntax-ns#type<http://www.w3.org/1999/02/22-**rdf-syntax-ns#type>
>>>> <http://**www.w3.org/1999/02/22-rdf-**syntax-ns#type<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
>>>> >>
>>>> <
>>>> http://www.unit4.com/daas/****graph#Graph<http://www.unit4.com/daas/**graph#Graph>
>>>> <http://www.unit4.**com/daas/graph#Graph<http://www.unit4.com/daas/graph#Graph>
>>>> >>
>>>>
>>>> }
>>>>       GRAPH ?g
>>>>         { ?s ?p ?o }
>>>>     }
>>>>
>>>> When the result is instantiated I want to return the ?g as an RDFS
>>>> infmodel. Ideally I want to decide what to return based on the ?g. I've
>>>> traced the execSelect() and the ResultSetMem() but drew a blank as to
>>>> where
>>>> I can get at the ?g's!
>>>>
>>>>
>>> ResultSet.next().getResource("****g") ;
>>>     or
>>> ResultSet.nextBinding().get(****Var.alloc("g")) ;
>>>
>>>
>>>
>>>
>>>  The following allows me to wrap the returned graph but this is static
>>>> i.e.
>>>> I need to know the ?g's to generate the dgm to pass to the
>>>> QueryExecutionFactory.
>>>>
>>>> dataset.begin(ReadWrite.READ);
>>>> DatasetGraphMap dgm = new DatasetGraphMap(dataset.****asDatasetGraph())
>>>> {
>>>>
>>>>
>>>> @Override
>>>> public Graph getGraph(Node graphNode) {
>>>> Graph g = super.getGraph(graphNode);
>>>> if (graphNode.getURI().equals(****types.getURI())) {
>>>>
>>>> g = asRDFS(g);
>>>> }
>>>> return g;
>>>> }
>>>>    public Graph asRDFS(Graph g) {
>>>> return
>>>> ModelFactory.createRDFSModel(****ModelFactory.****
>>>> createModelForGraph(g)).**
>>>>
>>>> getGraph();
>>>> }
>>>>    };
>>>> Graph g = dgm.getGraph(types.asNode());
>>>> info(g.size());
>>>> dataset.end();
>>>>
>>>> For the following triples loaded in the default graph;
>>>>
>>>> @prefix rdf: 
>>>> <http://www.w3.org/1999/02/22-****rdf-syntax-ns#<http://www.w3.org/1999/02/22-**rdf-syntax-ns#>
>>>> <http://www.**w3.org/1999/02/22-rdf-syntax-**ns#<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
>>>> >
>>>>
>>>>> .
>>>>>
>>>> @prefix rdfs: 
>>>> <http://www.w3.org/2000/01/****rdf-schema#<http://www.w3.org/2000/01/**rdf-schema#>
>>>> <http://www.w3.org/**2000/01/rdf-schema#<http://www.w3.org/2000/01/rdf-schema#>
>>>> >
>>>>
>>>>> .
>>>>>
>>>>
>>>> @prefix graph: 
>>>> <http://www.unit4.com/daas/****graph#<http://www.unit4.com/daas/**graph#>
>>>> <http://www.unit4.com/**daas/graph# <http://www.unit4.com/daas/graph#>>
>>>>
>>>>> .
>>>>>
>>>>
>>>> @prefix graphs: 
>>>> <http://www.unit4.com/daas/****graphs/<http://www.unit4.com/daas/**graphs/>
>>>> <http://www.unit4.com/**daas/graphs/<http://www.unit4.com/daas/graphs/>
>>>> >
>>>>
>>>>  .
>>>>>
>>>>
>>>> graph:Graph
>>>> rdf:type rdfs:Class.
>>>> graphs:g1
>>>> rdf:type graph:Graph.
>>>>
>>>> and these loaded in a named graph <http://www.unit4.com/daas/****
>>>> graphs/g1 <http://www.unit4.com/daas/**graphs/g1><http://www.unit4.**
>>>> com/daas/graphs/g1 <http://www.unit4.com/daas/graphs/g1>>
>>>>
>>>>> ;
>>>>>
>>>>
>>>> @prefix rdf: 
>>>> <http://www.w3.org/1999/02/22-****rdf-syntax-ns#<http://www.w3.org/1999/02/22-**rdf-syntax-ns#>
>>>> <http://www.**w3.org/1999/02/22-rdf-syntax-**ns#<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
>>>> >
>>>>
>>>>> .
>>>>>
>>>> @prefix rdfs: 
>>>> <http://www.w3.org/2000/01/****rdf-schema#<http://www.w3.org/2000/01/**rdf-schema#>
>>>> <http://www.w3.org/**2000/01/rdf-schema#<http://www.w3.org/2000/01/rdf-schema#>
>>>> >
>>>>
>>>>> .
>>>>>
>>>>
>>>> @prefix graph: 
>>>> <http://www.unit4.com/daas/****graph#<http://www.unit4.com/daas/**graph#>
>>>> <http://www.unit4.com/**daas/graph# <http://www.unit4.com/daas/graph#>>
>>>>
>>>>> .
>>>>>
>>>>
>>>> @prefix graphs: 
>>>> <http://www.unit4.com/daas/****graphs/<http://www.unit4.com/daas/**graphs/>
>>>> <http://www.unit4.com/**daas/graphs/<http://www.unit4.com/daas/graphs/>
>>>> >
>>>>
>>>>  .
>>>>>
>>>>
>>>> graphs:g1
>>>> rdfs:label "Graph 1".
>>>>
>>>> A select returns;
>>>>
>>>> select * where {{ ?g a 
>>>> <http://www.unit4.com/daas/****graph#Graph<http://www.unit4.com/daas/**graph#Graph>
>>>> <http://www.unit4.**com/daas/graph#Graph<http://www.unit4.com/daas/graph#Graph>
>>>> >>
>>>>
>>>> }. graph ?g
>>>> {?s ?p ?o}}
>>>> ------------------------------****----------------------------**--**
>>>> ------------------------------****----------------------------**--**
>>>> --------------------
>>>> | g                                     | s
>>>>       | p                                            | o         |
>>>> ==============================****============================**==**
>>>> ==============================****============================**==**
>>>> ====================
>>>> | 
>>>> <http://www.unit4.com/daas/****graphs/g1<http://www.unit4.com/daas/**graphs/g1>
>>>> <http://www.unit4.**com/daas/graphs/g1<http://www.unit4.com/daas/graphs/g1>
>>>> >>
>>>> | <
>>>> http://www.unit4.com/daas/****graphs/g1<http://www.unit4.com/daas/**graphs/g1>
>>>> <http://www.unit4.**com/daas/graphs/g1<http://www.unit4.com/daas/graphs/g1>
>>>> >>
>>>> | <
>>>> http://www.w3.org/2000/01/rdf-****schema#label<http://www.w3.org/2000/01/rdf-**schema#label>
>>>> <http://www.w3.**org/2000/01/rdf-schema#label<http://www.w3.org/2000/01/rdf-schema#label>
>>>> >>
>>>> | "Graph 1" |
>>>> ------------------------------****----------------------------**--**
>>>> ------------------------------****----------------------------**--**
>>>>
>>>> --------------------
>>>>
>>>> What I want is for it to return about 40 more... :-)
>>>>
>>>>
>>>>
>>>
>>
>

Reply via email to