/** initialise this personality with the bindings from _other_ */ public Personality( Personality<T> other ) { this(); this.add( other ); }
/** create a new Personality copying this one; the _types_ state is copied, not shared. */ public Personality<T> copy() { return new Personality<>( this ); }
/** * For debugging purposes, list the standard personalities on the given * output writer. * * @param writer A printwriter to list the personalities mapping to */ static public void listPersonalities( PrintWriter writer ) { for ( Map.Entry<Class<? extends RDFNode>, Implementation> e : model.getMap().entrySet() ) { writer.println( "personality key " + e.getKey().getName() + " -> value " + e.getValue() ); } writer.flush(); } }
public void testSimple() { Graph g = Factory.createGraphMem(); Personality<RDFNode> ours = BuiltinPersonalities.model.copy().add( Example.class, Example.factory ); EnhGraph eg = new EnhGraph( g, ours ); Node n = NodeFactory.createURI( "spoo:bar" ); EnhNode eNode = new EnhNode( NodeFactory.createURI( "spoo:bar" ), eg ); EnhNode eBlank = new EnhNode( NodeFactory.createBlankNode(), eg ); assertTrue( "URI node can be an Example", eNode.supports( Example.class ) ); assertFalse( "Blank node cannot be an Example", eBlank.supports( Example.class ) ); }
/** Answer an new enhanced node object that presents <i>this</i> in a way which satisfies type <code>t</code>. The new object is linked into this object's sibling ring. If the node cannot be converted, throw an UnsupportedPolymorphismException. */ @Override protected <X extends RDFNode> X convertTo( Class<X> t ) { EnhGraph eg = getGraph(); if (eg == null) throw new UnsupportedPolymorphismException( this, false, t ); Implementation imp = getPersonality().getImplementation( t ); if (imp == null) throw new UnsupportedPolymorphismException( this, true, t ); EnhNode result = imp.wrap( asNode(), eg ); this.addView( result ); return t.cast( result ); }
/** * Answer an enhanced node that wraps the given node and conforms to the given * interface type. * * @param n A node (assumed to be in this graph) * @param interf A type denoting the enhanced facet desired * @return An enhanced node */ public <X extends RDFNode> X getNodeAs( Node n, Class<X> interf ) { // We use a cache to avoid reconstructing the same Node too many times. EnhNode eh = (EnhNode) enhNodes.getIfPresent( n ); if ( eh == null ) { // not in the cache, so build a new one X constructed = personality.newInstance(interf, n, this) ; enhNodes.put(n, constructed) ; return constructed ; } else return eh.viewAs( interf ); }
public void testAlreadyLinkedViewException() { Graph g = Factory.createGraphMem(); Personality<RDFNode> ours = BuiltinPersonalities.model.copy().add( Example.class, Example.factory ); EnhGraph eg = new EnhGraph( g, ours ); Node n = NodeCreateUtils.create( "spoo:bar" ); EnhNode eNode = new Example( n, eg ); EnhNode multiplexed = new Example( n, eg ); multiplexed.as( Property.class ); eNode.viewAs( Example.class ); try { eNode.addView( multiplexed ); fail( "should raise an AlreadyLinkedViewException " ); } catch (AlreadyLinkedViewException e) {} }
/** Answer an new enhanced node object that presents <i>this</i> in a way which satisfies type <code>t</code>. The new object is linked into this object's sibling ring. If the node cannot be converted, throw an UnsupportedPolymorphismException. */ @Override protected <X extends RDFNode> X convertTo( Class<X> t ) { EnhGraph eg = getGraph(); if (eg == null) throw new UnsupportedPolymorphismException( this, false, t ); Implementation imp = getPersonality().getImplementation( t ); if (imp == null) throw new UnsupportedPolymorphismException( this, true, t ); EnhNode result = imp.wrap( asNode(), eg ); this.addView( result ); return t.cast( result ); }
/** * Answer an enhanced node that wraps the given node and conforms to the given * interface type. * * @param n A node (assumed to be in this graph) * @param interf A type denoting the enhanced facet desired * @return An enhanced node */ public <X extends RDFNode> X getNodeAs( Node n, Class<X> interf ) { // We use a cache to avoid reconstructing the same Node too many times. EnhNode eh = (EnhNode) enhNodes.getIfPresent( n ); if ( eh == null ) { // not in the cache, so build a new one X constructed = personality.newInstance(interf, n, this) ; enhNodes.put(n, constructed) ; return constructed ; } else return eh.viewAs( interf ); }
/** initialise this personality with the bindings from _other_ */ public Personality( Personality<T> other ) { this(); this.add( other ); }
public void testSimple() { Graph g = Factory.createGraphMem(); Personality<RDFNode> ours = BuiltinPersonalities.model.copy().add( Example.class, Example.factory ); EnhGraph eg = new EnhGraph( g, ours ); Node n = NodeFactory.createURI( "spoo:bar" ); EnhNode eNode = new EnhNode( NodeFactory.createURI( "spoo:bar" ), eg ); EnhNode eBlank = new EnhNode( NodeFactory.createBlankNode(), eg ); assertTrue( "URI node can be an Example", eNode.supports( Example.class ) ); assertFalse( "Blank node cannot be an Example", eBlank.supports( Example.class ) ); }
/** create a new Personality copying this one; the _types_ state is copied, not shared. */ public Personality<T> copy() { return new Personality<>( this ); }
/** answer true iff this enhanced node can support the class <code>t</code>, ie it is already a value <code>t</code> or it can be reimplemented as a <code>t</code> via the graph's personality's implementation. If this node has no graph, answer false. */ @Override protected <X extends RDFNode> boolean canSupport( Class<X> t ) { if (alreadyHasView( t )) return true; if (getGraph() == null) return false; Implementation imp = getPersonality().getImplementation( t ); return imp == null ? false : imp.canWrap( asNode(), getGraph() ); }
/** * For debugging purposes, list the standard personalities on the given * output writer. * * @param writer A printwriter to list the personalities mapping to */ static public void listPersonalities( PrintWriter writer ) { for ( Map.Entry<Class<? extends RDFNode>, Implementation> e : model.getMap().entrySet() ) { writer.println( "personality key " + e.getKey().getName() + " -> value " + e.getValue() ); } writer.flush(); } }
public static void init(Personality<RDFNode> p) { p.add(TriplesMap.class, new SimpleImplementation(TriplesMapImpl::new)); p.add(LogicalTable.class, new SimpleImplementation(LogicalTableImpl::new)); p.add(PredicateObjectMap.class, new SimpleImplementation(PredicateObjectMapImpl::new)); p.add(GraphMap.class, new SimpleImplementation(GraphMapImpl::new)); p.add(SubjectMap.class, new SimpleImplementation(SubjectMapImpl::new)); p.add(PredicateMap.class, new SimpleImplementation(PredicateMapImpl::new)); p.add(ObjectMap.class, new SimpleImplementation(ObjectMapImpl::new)); p.add(TermMap.class, new SimpleImplementation(TermMapImpl::new)); } }
public void testAlreadyLinkedViewException() { Graph g = Factory.createGraphMem(); Personality<RDFNode> ours = BuiltinPersonalities.model.copy().add( Example.class, Example.factory ); EnhGraph eg = new EnhGraph( g, ours ); Node n = NodeCreateUtils.create( "spoo:bar" ); EnhNode eNode = new Example( n, eg ); EnhNode multiplexed = new Example( n, eg ); multiplexed.as( Property.class ); eNode.viewAs( Example.class ); try { eNode.addView( multiplexed ); fail( "should raise an AlreadyLinkedViewException " ); } catch (AlreadyLinkedViewException e) {} }
public void testNullPointerTrapInCanSupport() { EnhGraph eg = new EnhGraph( Factory.createGraphMem(), new Personality<RDFNode>() ); Node n = NodeCreateUtils.create( "eh:something" ); EnhNode en = new EnhNode( n, eg ); assertFalse( en.canAs( Property.class ) ); }
/** answer true iff this enhanced node can support the class <code>t</code>, ie it is already a value <code>t</code> or it can be reimplemented as a <code>t</code> via the graph's personality's implementation. If this node has no graph, answer false. */ @Override protected <X extends RDFNode> boolean canSupport( Class<X> t ) { if (alreadyHasView( t )) return true; if (getGraph() == null) return false; Implementation imp = getPersonality().getImplementation( t ); return imp == null ? false : imp.canWrap( asNode(), getGraph() ); }
public static void init(Personality<RDFNode> p) { p.add(TriplesMap.class, new SimpleImplementation(TriplesMapImpl::new)); p.add(LogicalTable.class, new SimpleImplementation(LogicalTableImpl::new)); p.add(PredicateObjectMap.class, new SimpleImplementation(PredicateObjectMapImpl::new)); p.add(GraphMap.class, new SimpleImplementation(GraphMapImpl::new)); p.add(SubjectMap.class, new SimpleImplementation(SubjectMapImpl::new)); p.add(PredicateMap.class, new SimpleImplementation(PredicateMapImpl::new)); p.add(ObjectMap.class, new SimpleImplementation(ObjectMapImpl::new)); p.add(TermMap.class, new SimpleImplementation(TermMapImpl::new)); } }
public void testNullPointerTrapInCanSupport() { EnhGraph eg = new EnhGraph( Factory.createGraphMem(), new Personality<RDFNode>() ); Node n = NodeCreateUtils.create( "eh:something" ); EnhNode en = new EnhNode( n, eg ); assertFalse( en.canAs( Property.class ) ); }