Skip to content

Commit e328519

Browse files
author
Olaf Hartig
committed
added TriplePatternElement as a generic, API-independent interface tp represent elements of triple patterns; plus a generic factory for possible TriplePatternElement objects
1 parent f4ad030 commit e328519

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.linkeddatafragments.fragments.tpf;
2+
3+
/**
4+
* Represents an element of a triple pattern (i.e., subject, predicate, object).
5+
*
6+
* @param <TermType> type for representing RDF terms in triple patterns
7+
* @param <VarType> type for representing specific variables in triple patterns
8+
*
9+
* @author <a href="http://olafhartig.de">Olaf Hartig</a>
10+
*/
11+
public interface TriplePatternElement<TermType,VarType>
12+
{
13+
/**
14+
* Returns true if this element is a variable (named or unnamed).
15+
*/
16+
boolean isVariable();
17+
18+
/**
19+
* Returns true if this element is a specific variable, and false if either
20+
* it is not a variable but an RDF term or it is some variable that is not
21+
* specified. The latter (unspecified variables) is possible because when
22+
* a client requests a triple pattern fragment, it may omit triple pattern
23+
* related parameters.
24+
*
25+
* If this element is a specific variable (that is, this method returns
26+
* true), this specific variable can be obtained by {@link #asVariable()}.
27+
*/
28+
boolean isSpecificVariable();
29+
30+
/**
31+
* Returns a representation of this element as a specific variable (assuming
32+
* it is a specific variable).
33+
*
34+
* @throws UnsupportedOperationException
35+
* If this element is not a specific variable (i.e.,
36+
* if {@link #isSpecificVariable()} returns false).
37+
*/
38+
VarType asVariable() throws UnsupportedOperationException;
39+
40+
/**
41+
* Returns a representation of this element as an RDF term (assuming it is
42+
* an RDF term and not a variable).
43+
*
44+
* @throws UnsupportedOperationException
45+
* If this element is not an RDF term but a variable
46+
* (i.e., if {@link #isVariable()} returns true).
47+
*/
48+
TermType asTerm() throws UnsupportedOperationException;
49+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.linkeddatafragments.fragments.tpf;
2+
3+
/**
4+
* A factory for {@link TriplePatternElement}s.
5+
*
6+
* @param <TermType> type for representing RDF terms in triple patterns
7+
* @param <VarType> type for representing specific variables in triple patterns
8+
*
9+
* @author <a href="http://olafhartig.de">Olaf Hartig</a>
10+
*/
11+
public class TriplePatternElementFactory<TermType,VarType>
12+
{
13+
public TriplePatternElement<TermType,VarType> createUnspecifiedVariable() {
14+
return new UnspecifiedVariable<TermType,VarType>();
15+
}
16+
17+
public TriplePatternElement<TermType,VarType> createSpecificVariable(
18+
final VarType variable ) {
19+
return new SpecificVariable<TermType,VarType>( variable );
20+
}
21+
public TriplePatternElement<TermType,VarType> createRDFTerm(
22+
final TermType term ) {
23+
return new RDFTerm<TermType,VarType>( term );
24+
}
25+
26+
static abstract public class Variable<TermType,VarType>
27+
implements TriplePatternElement<TermType,VarType>
28+
{
29+
public boolean isVariable() { return true; }
30+
public TermType asTerm() { throw new UnsupportedOperationException(); }
31+
}
32+
33+
static public class UnspecifiedVariable<TermType,VarType>
34+
extends Variable<TermType,VarType>
35+
{
36+
public boolean isSpecificVariable() { return false; }
37+
public VarType asVariable() { throw new UnsupportedOperationException(); }
38+
public String toString() { return "UnspecifiedVariable"; }
39+
}
40+
41+
static public class SpecificVariable<TermType,VarType>
42+
extends Variable<TermType,VarType>
43+
{
44+
protected final VarType v;
45+
public SpecificVariable( final VarType variable ) { v = variable; }
46+
public boolean isSpecificVariable() { return true; }
47+
public VarType asVariable() { return v; }
48+
public String toString() { return "SpecificVariable(" + v.toString() + ")"; }
49+
}
50+
51+
static public class RDFTerm<TermType,VarType>
52+
implements TriplePatternElement<TermType,VarType>
53+
{
54+
protected final TermType t;
55+
public RDFTerm( final TermType term ) { t = term; }
56+
public boolean isVariable() { return false; }
57+
public boolean isSpecificVariable() { return false; }
58+
public VarType asVariable() { throw new UnsupportedOperationException(); }
59+
public TermType asTerm() { return t; }
60+
public String toString() { return "RDFTerm(" + t.toString() + ")(type: " + t.getClass().getSimpleName() + ")"; }
61+
}
62+
63+
}

0 commit comments

Comments
 (0)