Package com.hp.jena.rules.retelike.impl.scratch

Source Code of com.hp.jena.rules.retelike.impl.scratch.EngineExecContext

/*
  (c) Copyright 2008, 2009 Hewlett-Packard Development Company, LP
   All rights reserved.
   $Id$
*/


/**
*
*/
package com.hp.jena.rules.retelike.impl.scratch;

import java.util.*;

import com.hp.jena.datatypes.xsd.*;
import com.hp.jena.graph.*;
import com.hp.jena.graph.basicpattern.BasicGraphPattern;
import com.hp.jena.iterator.*;
import com.hp.jena.rules.retelike.impl.ExecContext;
import com.hp.jena.vocabulary.RDF;

public class EngineExecContext implements ExecContext
    {
    private final Graph result;
    private final Graph m;
   
    private final Set<Node> hidden;
   
    Transform<Triple, Node> getObject = new Transform<Triple, Node>()
        {
        public Node convert( Triple item )
            { return item.getObject(); }
        };   
       
    public EngineExecContext( Graph result, Graph m )
        { this( result, m, new HashSet<Node>() ); }
   
    public EngineExecContext( Graph result, Graph m, Set<Node> hidden )
        {
        this.result = result;
        this.m = m;
        this.hidden = hidden;
        }
   
    public boolean contains( Node s, Node p, Node o )
        { return m.contains( s, p, o ) || result.contains( s, p, o ); }
   
    public boolean baseContains( List<Triple> triples )
        {
        BasicGraphPattern p = new BasicGraphPattern();
        for (Triple t: triples) p.add( t );
        return m.getQueryHandler().prepare( p ).executeBindings().hasNext();
        }

    public boolean listEquals( Node L, Node R )
        {
        while (true)
            {
            if (L.equals( RDF.nil )) return R.equals( RDF.nil );
            if (R.equals( RDF.nil )) return false;
            Node lFirst = propertyValue( L, RDF.first );
            Node rFirst = propertyValue( R, RDF.first );
            if (!lFirst.sameValueAs( rFirst )) return false;
            L = propertyValue( L, RDF.rest );
            R = propertyValue( R, RDF.rest );
            }
        }
   
    Iter<Triple> find( Node S, Node P, Node O )
        {
        return m.find( S, P, O ).append( result.find( S, P, O ) );
        }
   
    Node propertyValue( Node S, Node P )
        {
        return find( S, P, Node.ANY ).next().getObject();
        }
   
    public boolean listContains( Node lv, Node ev )
        {
        while (true)
            {
            if (lv.equals( RDF.nil )) return false;
            if (ev.equals( propertyValue( lv, RDF.first ) )) return true;
            lv = propertyValue( lv, RDF.rest );
            }
        }
   
    public int countDistinctValues( Node xv, Node pv )
        {
        List<Node> values = find( xv, pv, Node.ANY ).map( getObject ).toList();
        for (int i = 0; i < values.size(); i += 1)
            {
            Node current = values.get( i );
            for (int j = i + 1; j < values.size(); j += 1)
                {
                Node candidate = values.get( j );
                if (candidate.sameValueAs( current )) values.remove( j );
                }
            }
        return values.size();
        }
   
    public Node createBNode()
        { return NodeFactory.createBlankNode(); }

    public void hide( Node toHide )
        {
        hidden.add( toHide );
//        if (toHide.isIRI()) System.err.println( ">> hiding " + toHide );
        }

    public void stripHiddenFrom( Graph toStrip )
        { stripHiddenFrom( toStrip, hidden ); }
   
    public static void stripHiddenFrom( Graph toStrip, Set<Node> hidden )
        {
//        System.err.println( ">> stripHiddenFrom: hidden = " + hidden );
//        System.err.println( ">> ... toStrip size initially " + toStrip.size() );
        Set<Triple> toHide = new HashSet<Triple>();
        for (Triple t: toStrip.find( Triple.ANY )) if (containsHiddenNode( hidden, t )) toHide.add( t );
//        System.err.println( ">> ... toHide contains " + toHide.size() + " elements" );       
        for (Triple t: toHide) toStrip.delete( t );
//        System.err.println( ">> ... toStrip size now " + toStrip.size() );
        }

    private static boolean containsHiddenNode( Set<Node> hidden, Triple t )
        {
        return
            hidden.contains( t.getSubject() )
            || hidden.contains( t.getPredicate() )
            || hidden.contains( t.getObject() )
            || isFunctor( t.getObject() )
            ;
        }

    private static boolean isFunctor( Node n )
        {
        return n instanceof Functor;
        }

    //    private final String now = new GregorianCalendar().getTime().toString();
    private final String now = new XSDDateTime( new GregorianCalendar() ).timeLexicalForm();
   
    public Node now()
        {
        return NodeFactory.createTypedLiteral( now, XSDDatatype.XSDdateTime );
        }
    }/*
  (c) Copyright 2008, 2009 Hewlett-Packard Development Company, LP
   All rights reserved.
   $Id$
*/ 
TOP

Related Classes of com.hp.jena.rules.retelike.impl.scratch.EngineExecContext

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.