/*
(c) Copyright 2008 Hewlett-Packard Development Company, LP
All rights reserved.
$Id$
*/
package com.hp.jena.rules.ast;
import java.util.List;
import com.hp.jena.util.BrokenException;
public class Item
{
protected final String uri;
Item( String uri ) { this.uri = uri; }
public static Item createURI( String uri )
{ return new URI( uri.replace( "<", "" ).replace( ">", "" ) ); }
public String getURI() { return uri; }
@Override public String toString() { return uri; }
static class URI extends Item
{
URI( String uri )
{ super( uri ); }
@Override public boolean isURI()
{ return true; }
@Override public <T> T visit( Visitor<T> v )
{ return v.visitURI( uri ); }
}
static class Var extends Item
{
Var( String name ) { super( name ); }
@Override public boolean isVariable() { return true; }
@Override public String getVarName() { return uri; }
@Override public <T> T visit( Visitor<T> v )
{ return v.visitVariable( uri ); }
}
static class Blank extends Item
{
static int count = 1000;
Blank() { super( "_:B" + ++count ); }
@Override public boolean isBlank()
{ return true; }
@Override public <T> T visit( Visitor<T> v )
{ return v.visitBNode( uri ); }
}
static class ExplicitBlank extends Item
{
static int count = 1000;
ExplicitBlank( String label ) { super( label ); }
@Override public boolean isExplicitBlank()
{ return true; }
@Override public <T> T visit( Visitor<T> v )
{ return v.visitExplicitBNode( uri ); }
}
public static class Name extends Item
{
Name( String uri )
{ super( uri ); }
}
public static class Functor extends Item
{
protected final String name;
protected final List<Expr> args;
Functor( String name, List<Expr> args )
{ super( name ); this.name = name; this.args = args; }
@Override public boolean isFunctor()
{ return true; }
@Override public List<Expr> getFunctorArgs()
{ return args; }
@Override public String getFunctorName()
{ return name; }
@Override public String toString()
{ return name + args; }
@Override public <T> T visit( Visitor<T> v )
{ return v.visitFunctor( this ); }
}
static class WrappedExpr extends Item
{
protected final Expr expr;
WrappedExpr( Expr expr )
{ super( expr.f ); this.expr = expr; }
@Override public Expr getExpr()
{ return expr; }
@Override public boolean isExpr()
{ return true; }
@Override public <T> T visit( Visitor<T> v )
{ return v.visitExpr( expr ); }
}
static class Literal extends Item
{
protected final String spelling;
protected final String language;
protected final String type;
Literal( boolean positive, String spelling, String type )
{ this( (positive ? spelling : "-" + spelling), null, type ); }
Literal( String spelling, String language, String type )
{ super( spelling )
; this.spelling = spelling
; this.language = language
; this.type = type
; }
@Override public boolean isLiteral()
{ return true; }
@Override public String toString()
{
return spelling
+ (language == null ? "" : "@" + language)
+ (type == null ? "" : "^^" + type)
;
}
@Override public String getLiteralSpelling()
{ return spelling; }
@Override public String getLiteralLanguage()
{ return language; }
@Override public String getLiteralType()
{ return type; }
@Override public <T> T visit( Visitor<T> v )
{
if (language != null) return v.visitTaggedLiteral( spelling, language );
if (type != null) return v.visitTypedLiteral( spelling, type );
return v.visitPlainLiteral( spelling );
}
}
public static Item createVariable( String name )
{ return new Item.Var( name ); }
public static Item createBNode()
{ return new Blank(); }
public static Item createExplicitBNode( String string )
{ return new ExplicitBlank( string ); }
public static Item createPlainLiteral( String string )
{ return new Literal( string, null, null ); }
public static Item createTaggedLiteral( String spelling, String lang )
{ return new Literal( spelling, lang, null ); }
public static Item createTypedLiteral( String spelling, String type )
{ return new Literal( spelling, null, type ); }
public boolean isURI()
{ return false; }
public boolean isVariable()
{ return false; }
public boolean isLiteral()
{ return false; }
public boolean isBlank()
{ return false; }
public boolean isExplicitBlank()
{ return false; }
public boolean isFunctor()
{ return false; }
public boolean isExpr()
{ return false; }
public Expr getExpr()
{ throw new UnsupportedOperationException( "cannot get expr from this" ); }
public String getVarName()
{ throw new UnsupportedOperationException( "cannot get variable name from this" ); }
public String getLiteralSpelling()
{ throw new UnsupportedOperationException( "cannot get literal spelling from this" ); }
public String getLiteralLanguage()
{ throw new UnsupportedOperationException( "cannot get literal language from this" ); }
public String getLiteralType()
{ throw new UnsupportedOperationException( "cannot get literal type from this" ); }
public List<Expr> getFunctorArgs()
{ throw new UnsupportedOperationException( "cannot get functor args from this" ); }
public String getFunctorName()
{ throw new UnsupportedOperationException( "cannot get functor name from this" ); }
public static Item createFunctor( String name, List<Expr> args )
{ return new Functor( name, args ); }
public static Item createFunctor( Item name, List<Expr> args )
{ return new Functor( name.getURI(), args ); }
public static Item createExpr( Expr expr )
{ return new Item.WrappedExpr( expr ); }
public interface Visitor<Result>
{
public Result visitURI( String uri );
public Result visitVariable( String name );
public Result visitBNode( String label );
public Result visitExplicitBNode( String label );
public Result visitPlainLiteral( String spelling );
public Result visitFunctor( Functor f );
public Result visitTypedLiteral( String spelling, String type );
public Result visitTaggedLiteral( String spelling, String tag );
public Result visitExpr( Expr e );
}
public static class VisitorBoom<T> implements Visitor<T>
{
public T visitURI( String uri )
{ throw new BrokenException( "VisitorBoom can't visit a URI" ); }
public T visitVariable( String name )
{ throw new BrokenException( "VisitorBoom can't visit a variable" ); }
public T visitBNode( String label )
{ throw new BrokenException( "VisitorBoom can't visit a bnode" ); }
public T visitExplicitBNode( String label )
{ throw new BrokenException( "VisitorBoom can't visit an explicit bnode" ); }
public T visitPlainLiteral( String spelling )
{ throw new BrokenException( "VisitorBoom can't visit a plain literal" ); }
public T visitFunctor( Functor f )
{ throw new BrokenException( "VisitorBoom can't visit a functor" ); }
public T visitTaggedLiteral( String spelling, String tag )
{ throw new BrokenException( "VisitorBoom can't visit a tagged literal" ); }
public T visitTypedLiteral( String spelling, String type )
{ throw new BrokenException( "VisitorBoom can't visit a typed literal" ); }
public T visitExpr( Expr e )
{ throw new BrokenException( "VisitorBoom can't visit a wrapped expression" ); }
}
public <T> T visit( Visitor<T> iv )
{ throw new BrokenException( "cannot visit this " + this.getClass().getName() + ": " + this ); }
}