org.topbraid.spin.model
Class SPINFactory

java.lang.Object
  extended by org.topbraid.spin.model.SPINFactory

public class SPINFactory
extends java.lang.Object

The singleton that is used to convert plain Jena objects into SPIN API resources, and to do corresponding tests.

Author:
Holger Knublauch

Constructor Summary
SPINFactory()
           
 
Method Summary
static Element asElement(Resource resource)
          Checks whether a given Resource represents a SPARQL element, and returns an instance of a subclass of Element if so.
static RDFNode asExpression(RDFNode node)
          Returns the most specific Java instance for a given RDFNode.
static Function asFunction(Resource resource)
          Converts a given Resource into a Function instance.
static FunctionCall asFunctionCall(Resource resource)
          Checks if a given Resource might represent a Function call, and if yes returns the resource as Function.
static Query asQuery(Resource resource)
          Checks if a given Resource is a SPIN query, and returns an instance of a subclass of Query if so.
static Template asTemplate(Resource resource)
          Converts a given Resource into a Template instance.
static TemplateCall asTemplateCall(RDFNode node)
          Checks whether a given RDFNode can be cast into TemplateCall, and returns it as a TemplateCall instance if so.
static TriplePattern asTriplePattern(RDFNode node)
          Checks whether a given RDFNode can be converted into a TriplePattern, and if yes, returns an instance of TriplePattern.
static Variable asVariable(RDFNode node)
          Checks whether a given RDFNode can be cast into a Variable and - if yes - converts it into an instance of Variable.
static Argument createArgument(Model model, Property argProperty, Resource argType, boolean optional)
          Creates an spl:Argument with a given property and value type.
static Ask createAsk(Model model, ElementList where)
          Creates an Ask query for a given WHERE clause.
static Attribute createAttribute(Model model, Property argProperty, Resource argType, java.lang.Integer minCount, java.lang.Integer maxCount)
          Creates a new spl:Attribute as a blank node in a given Model.
static ElementList createElementList(Model model, Element[] elements)
          Creates a new ElementList in a given Model.
static ElementList createElementList(Model model, java.util.Iterator<Element> elements)
          Creates a new ElementList in a given Model.
static Filter createFilter(Model model, RDFNode expression)
          Creates a Filter from a given expression.
static FunctionCall createFunctionCall(Model model, Resource function)
          Creates a new Function call, which is basically an instance of the function's class.
static Let createLet(Model model, Variable variable, RDFNode expression)
          Creates a Let in a given Model as a blank node.
static NamedGraph createNamedGraph(Model model, Resource graphNameNode, RDFList elements)
          Creates a new NamedGraph element as a blank node in a given Model.
static Optional createOptional(Model model, ElementList elements)
          Creates a new Optional as a blank node in a given Model.
static TemplateCall createTemplateCall(Model model, Resource template)
          Creates a new TemplateCall as a blank node instance of a given template.
static TriplePath createTriplePath(Model model, Resource subject, Resource path, RDFNode object)
          Creates a new TriplePath as a blank node in a given Model.
static TriplePattern createTriplePattern(Model model, Resource subject, Resource predicate, RDFNode object)
          Creates a new TriplePattern as a blank node in a given Model.
static Union createUnion(Model model, ElementList elements)
          Creates a new UNION element as a blank node in a given Model.
static Variable createVariable(Model model, java.lang.String varName)
          Creates a new Variable as a blank node in a given Model.
static Attribute getAttribute(Resource cls, Property property)
          Gets an spl:Attribute defined for a given property on a given class.
static boolean isAbstract(Resource module)
          Checks whether a given module has been declared abstract using spin:abstract
static boolean isElementList(Resource resource)
          Checks if a given Resource can be cast into an ElementList.
static boolean isModuleInstance(Resource resource)
          Checks if a given Resource is an instance of a class that has type spin:Module (or its subclasses such as spin:Function).
static boolean isQueryProperty(Property predicate)
          Checks if a given Property is spin:query or a sub-property of it.
static boolean isTemplateCall(RDFNode node)
          Checks whether a given RDFNode is a TemplateCall.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SPINFactory

public SPINFactory()
Method Detail

asElement

public static Element asElement(Resource resource)
Checks whether a given Resource represents a SPARQL element, and returns an instance of a subclass of Element if so.

Parameters:
resource - the Resource to check
Returns:
Resource as an Element or null if resource is not an element

asExpression

public static RDFNode asExpression(RDFNode node)
Returns the most specific Java instance for a given RDFNode. If the node is a function call, it will be returned as instance of Function. If it's a Variable, the Variable will be returned. Otherwise the node itself will be returned.

Parameters:
node - the node to cast
Returns:
node or node as a Function or Variable

asFunction

public static Function asFunction(Resource resource)
Converts a given Resource into a Function instance. No other tests are done.

Parameters:
resource - the Resource to convert
Returns:
the Function

asFunctionCall

public static FunctionCall asFunctionCall(Resource resource)
Checks if a given Resource might represent a Function call, and if yes returns the resource as Function. The condition here is fairly general: a function call must be a blank node with an rdf:type triple where the type triple's object is a URI resource. It is generally assumed that this function is called after other options have been exhausted. For example, in order to test whether a resource is a variable or a function call, the variable test must be done first as it is more specific than these test conditions

Parameters:
resource - the Resource to test
Returns:
resource as a Function or null if resource cannot be cast

asQuery

public static Query asQuery(Resource resource)
Checks if a given Resource is a SPIN query, and returns an instance of a subclass of Query if so.

Parameters:
resource - the Resource to test
Returns:
resource as a Query or null

asTemplate

public static Template asTemplate(Resource resource)
Converts a given Resource into a Template instance. No other tests are done.

Parameters:
resource - the Resource to convert
Returns:
the Template

asTemplateCall

public static TemplateCall asTemplateCall(RDFNode node)
Checks whether a given RDFNode can be cast into TemplateCall, and returns it as a TemplateCall instance if so.

Parameters:
node - the node to convert
Returns:
an instance of TemplateCall or null

asTriplePattern

public static TriplePattern asTriplePattern(RDFNode node)
Checks whether a given RDFNode can be converted into a TriplePattern, and if yes, returns an instance of TriplePattern.

Parameters:
node - the node to test
Returns:
node as TriplePattern or null

asVariable

public static Variable asVariable(RDFNode node)
Checks whether a given RDFNode can be cast into a Variable and - if yes - converts it into an instance of Variable. The Resource must have a value for spin:varName.

Parameters:
node - the node to check
Returns:
resource as a Variable or null

createArgument

public static Argument createArgument(Model model,
                                      Property argProperty,
                                      Resource argType,
                                      boolean optional)
Creates an spl:Argument with a given property and value type. The new Argument resource will be a blank node in a given Model.

Parameters:
model - the Model
argProperty - the property or null
argType - the value type or null
optional - true if the Argument shall be optional
Returns:
the new Argument

createAttribute

public static Attribute createAttribute(Model model,
                                        Property argProperty,
                                        Resource argType,
                                        java.lang.Integer minCount,
                                        java.lang.Integer maxCount)
Creates a new spl:Attribute as a blank node in a given Model.

Parameters:
model - the Model to create the attribute in
argProperty - the predicate or null
argType - the value type or null
minCount - the minimum cardinality or null
maxCount - the maximum cardinality or null
Returns:
a new Attribute

createAsk

public static Ask createAsk(Model model,
                            ElementList where)
Creates an Ask query for a given WHERE clause.

Parameters:
model - the Model to create the Ask (blank node) in
where - the elements of the WHERE clause
Returns:
the new Ask query

createElementList

public static ElementList createElementList(Model model,
                                            Element[] elements)
Creates a new ElementList in a given Model.

Parameters:
model - the Model to create the ElementList in
elements - the elements (may be empty)
Returns:
a new ElementList (may be rdf:nil)

createElementList

public static ElementList createElementList(Model model,
                                            java.util.Iterator<Element> elements)
Creates a new ElementList in a given Model.

Parameters:
model - the Model to create the ElementList in
elements - the elements (may be empty)
Returns:
a new ElementList (may be rdf:nil)

createFilter

public static Filter createFilter(Model model,
                                  RDFNode expression)
Creates a Filter from a given expression.

Parameters:
model - the Model to create the (blank node) Filter in
expression - the expression node (not null)
Returns:
a new Filter

createFunctionCall

public static FunctionCall createFunctionCall(Model model,
                                              Resource function)
Creates a new Function call, which is basically an instance of the function's class.

Parameters:
model - the Model to create the function call in
function - the function class (must be a URI resource)
Returns:
a new instance of function

createLet

public static Let createLet(Model model,
                            Variable variable,
                            RDFNode expression)
Creates a Let in a given Model as a blank node.

Parameters:
model - the Model to create the Let in
variable - the Variable to assign
expression - the expression on the right hand side
Returns:
a new Let instance

createNamedGraph

public static NamedGraph createNamedGraph(Model model,
                                          Resource graphNameNode,
                                          RDFList elements)
Creates a new NamedGraph element as a blank node in a given Model.

Parameters:
model - the Model to generate the NamedGraph in
graphNameNode - the URI resource of the graph name
elements - the elements in the NamedGraph
Returns:
a new NamedGraph

createOptional

public static Optional createOptional(Model model,
                                      ElementList elements)
Creates a new Optional as a blank node in a given Model.

Parameters:
model - the Model to create the OPTIONAL in
elements - the elements of the OPTIONAL
Returns:
a new Optional

createTemplateCall

public static TemplateCall createTemplateCall(Model model,
                                              Resource template)
Creates a new TemplateCall as a blank node instance of a given template.

Parameters:
model - the Model to create a template call in
template - the template class
Returns:
the new TemplateCall or null

createTriplePath

public static TriplePath createTriplePath(Model model,
                                          Resource subject,
                                          Resource path,
                                          RDFNode object)
Creates a new TriplePath as a blank node in a given Model.

Parameters:
model - the Model to create the path in
subject - the subject (not null)
path - the path (not null)
object - the object (not null)
Returns:
a new TriplePath

createTriplePattern

public static TriplePattern createTriplePattern(Model model,
                                                Resource subject,
                                                Resource predicate,
                                                RDFNode object)
Creates a new TriplePattern as a blank node in a given Model.

Parameters:
model - the Model to create the pattern in
subject - the subject (not null)
predicate - the predicate (not null)
object - the object (not null)
Returns:
a new TriplePattern

createUnion

public static Union createUnion(Model model,
                                ElementList elements)
Creates a new UNION element as a blank node in a given Model.

Parameters:
model - the Model to create the Union in
elements - the elements
Returns:
a new Union

createVariable

public static Variable createVariable(Model model,
                                      java.lang.String varName)
Creates a new Variable as a blank node in a given Model.

Parameters:
model - the Model
varName - the name of the variable
Returns:
the Variable

getAttribute

public static Attribute getAttribute(Resource cls,
                                     Property property)
Gets an spl:Attribute defined for a given property on a given class. The spl:Attribute must be a direct spin:constraint on the class.

Parameters:
cls - the class
property - the property
Returns:
the Attribute or null if none is found

isAbstract

public static boolean isAbstract(Resource module)
Checks whether a given module has been declared abstract using spin:abstract
Parameters:
module - the module to test
Returns:
true if abstract

isElementList

public static boolean isElementList(Resource resource)
Checks if a given Resource can be cast into an ElementList. It must be either rdf:nil or an rdf:List where the first list item is an element using asElement().

Parameters:
resource - the resource to test
Returns:
true if resource is an element list

isModuleInstance

public static boolean isModuleInstance(Resource resource)
Checks if a given Resource is an instance of a class that has type spin:Module (or its subclasses such as spin:Function).

Parameters:
resource - the Resource to check
Returns:
true if resource is a Module

isQueryProperty

public static boolean isQueryProperty(Property predicate)
Checks if a given Property is spin:query or a sub-property of it.

Parameters:
predicate - the Property to test
Returns:
true if predicate is a query property

isTemplateCall

public static boolean isTemplateCall(RDFNode node)
Checks whether a given RDFNode is a TemplateCall. The condition for this is stricter than for asTemplateCall as the node also must have a valid template assigned to it, i.e. the type of the node must be an instance of spin:Template.

Parameters:
node - the RDFNode to check
Returns:
true if node is a TemplateCall