sparqlrdfsemantic-webgraphdbdata-tracing

Is there a SPARQL Query to Trace different Process steps of one Workflow into one row


I use GraphDB to store different production steps. The productionsteps are typified by their steps such as A,B,C,D. Process steps that belong together are connected by the object property ":hasUpstreamProduktionsnummer".

The data contains many different workflows. Where also a few production steps are missing. So, a Workflow can end with an instance of class B or start with an instance of Class C.

To visualize the workflow, I need to create a table that contains all production steps in one row and keeping a free space for the missing. For example:

A1  B1  C1
    B2  C2
A3  B3  
A4      
        C4

My approach was :hasUpstreamProduktionsnummer as transtiv and the following:

    select distinct ?A ?B ?C where {
    
    {   ?A a :A.
    ?A :hasUpstreamProduktionsnummer ?AUP.}
Optional
    { ?B owl:sameAs ?AUP.
    ?B a :B.}
Optional
    { ?C owl:sameAs ?AUP.
    ?C a :C. }
}
  

The query will result the following for Workflow 1:

    A1      C1
    A1  B1  
    A1  

Is there any option to query them in one row?

Is there any write the query more efficient? In my case I would need to write the same down for B and C as a starting point.

Data:

@base <http://BLB.de/Daten/Produktionsdaten/>
@prefix : <http://BLB.de/Daten/Produktionsdaten/> .
<A1> a :A;
:hasUpstreamProduktionsnummer <B1>.
<B1> a :B;
:hasUpstreamProduktionsnummer <C1>.
<C1> a :C.
<B2> a :B;
:hasUpstreamProduktionsnummer <C2>.
<C2> a :C.
<A3> a :A;
:hasUpstreamProduktionsnummer <B3>.
<B3> a :B.
<A4> a :A.
<C4> a :C

Thank you for helping in advance!


Solution

  • Problem solved by UninformedUser with SPARQL Elements Optional, Union and filter not exists in the following Query:

    prefix : <http://BLB.de/Daten/Produktionsdaten/>  
    select distinct ?A ?B ?C where {  
    { ?A a :A. 
    OPTIONAL {?A :hasUpstreamProduktionsnummer ?B . 
    ?B a :B 
    Optional{?B :hasUpstreamProduktionsnummer ?C . 
    ?C a :C. }} }
    UNION { ?B a :B 
    Optional{?B :hasUpstreamProduktionsnummer ?C . ?C a :C. } 
    filter not exists {?A :hasUpstreamProduktionsnummer ?B} } 
    UNION { ?C a :C.  filter not exists {?B :hasUpstreamProduktionsnummer ?C} } }