Package com.hp.jena.ymris.deploy.functions

Source Code of com.hp.jena.ymris.deploy.functions.Jena2Builtins$YmrisBound

/* 
  (c) Copyright 2009 Hewlett-Packard Development Company, LP
  [See end of file]
  $Id$
*/
package com.hp.jena.ymris.deploy.functions;

import static com.hp.jena.ymris.yast.expr.Expr.*;

import java.util.*;

import com.hp.hpl.jena.graph.*;
import com.hp.hpl.jena.reasoner.rulesys.*;
import com.hp.hpl.jena.reasoner.rulesys.builtins.*;


import com.hp.jena.ymris.deploy.reasoner.DirectInferenceGraph;
import com.hp.jena.ymris.yast.expr.*;

public class Jena2Builtins implements HowTo
    {
    protected final RuleContext ruleContext;
   
    public Jena2Builtins( RuleContext ruleContext )
        {
        this.ruleContext = ruleContext;
    //
        register( new Equal() );
        register( new NotEqual() );
        register( new NotLiteral() );
        register( new IsLiteral() );
        register( new IsBNode() );
        register( new NotBNode() );
        register( new LE() );
        register( new LessThan() );
        register( new ListContains() );
        register( new GreaterThan() );
        register( new GE() );
        register( new NotFunctor() );
        register( new IsFunctor() );
        register( new NotDType() );
        register( new IsDType() );
        register( new NoValue() );
    //
        register( new YmrisHide() );
        register( new YmrisBound() );
        register( new YmrisUnbound() );
    //
        register( new Min() );
        register( new ListEqual() );
    //
        register( new ListMapAsSubject() );
        register( new AssertDisjointPairs() );
        register( new MakeTemp() );
        register( new CountLiteralValues() );
        register( new ListNotContains() );
    //
//        register( new Print() );
//        register( new AddOne() );
//        register( new Remove() );
//        register( new Drop() );
//        register( new Sum() );
//        register( new Difference() );
//        register( new Product() );
//        register( new Quotient() );
//        register( new Max() );
//        register( new ListLength() );
//        register( new ListEntry() );
//        register( new ListNotEqual() );
//        register( new ListMapAsObject() );
//        register( new MakeInstance() );
//        register( new Table() );
//        register( new TableAll() );
//        register( new StrConcat() );
//        register( new UriConcat() );
//        register( new Regex() );
//        register( new Now() );
        }
   

    public class YmrisBound extends BaseBuiltin
        {
        @Override public String getName()
            { return "bound"; }   
       
        @Override public boolean bodyCall( Node[] args, int length, RuleContext context )
            { return true; }
        }

    public class YmrisUnbound extends BaseBuiltin
        {
        @Override public String getName()
            { return "unbound"; }   
       
        @Override public boolean bodyCall( Node[] args, int length, RuleContext context )
            { return false; }
        }
   
    public class YmrisHide extends BaseBuiltin
        {
        @Override public String getName()
            { return "hide"; }   
       
        @Override public boolean bodyCall( Node[] args, int length, RuleContext context )
            {
            headAction( args, length, context );
            return true;  
            }

       @Override public void headAction( Node[] args, int length, RuleContext context )
           {
           DirectInferenceGraph ng = (DirectInferenceGraph) context.getGraph();
           for (int i = 0; i < length; i += 1) ng.hide( args[i] );
           }
        }

    protected final Map<String, Builtin> builtins = new HashMap<String, Builtin>();
   
    protected final Map<String, Builtin> builtinsByURI = new HashMap<String, Builtin>();
   
    private void register( BaseBuiltin b )
        {
        builtins.put( b.getName(), b );
        builtins.put( "jena2:" + b.getName(), b );
        builtinsByURI.put( b.getURI(), b );
        }

    @Override public Operation operationNamed( String f )
        {
        if (f.equals( "=" ))
            {
            return new Operation()
                {
                @Override public Node evalValue( Node [] values )
                    {
                    Node l = values[0], r = values[1];
                    return l.equals( r ) ? Expr.NODE_TRUE : Expr.NODE_FALSE;
                    }
                };
            }
        else if (f.equals( ">" ))
            {
            final GreaterThan gt = new GreaterThan();
            return new Operation()
                {
                @Override public Node evalValue( Node [] values )
                    {
                    Node l = values[0], r = values[1];
                    return gt.bodyCall( new Node[] {l, r}, 2, ruleContext )? NODE_TRUE : NODE_FALSE;
                    }
                };
            }
        else if (f.equals( "jena2:newAnon" ))
            return new Operation()
                {               
                @Override public Node evalValue( Node [] values )
                    {
                    return Node.createAnon(); // TODO context sensitivity
                    }
                };
        Builtin b = builtins.get( f );
        if (b == null) b = builtinsByURI.get( f );
        if (b == null)
            throw new RuntimeException( "don't understand function '" + f + "'" );
        else
            return builtinToRelation( b );
        }

    public Operation builtinToRelation( final Builtin b )
        {
        return new Operation()
            {
            public boolean evalBool( Node[] values )
                { return b.bodyCall( values, values.length, ruleContext ); }
           
            @Override public Node evalValue( Node [] values )
                {
                try
                    { return evalBool( values ) ? NODE_TRUE : NODE_FALSE; }
                catch (RuntimeException e)
                    {
                    b.headAction( values, values.length, ruleContext );
                    return Node.createURI( "eh:/void" );
                    }
                }
            };
        }

    @Override public Graph getGraph()
        { return ruleContext.getGraph().getRawGraph(); }
    }

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

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.

    3. The name of the author may not be used to endorse or promote products
       derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ 
TOP

Related Classes of com.hp.jena.ymris.deploy.functions.Jena2Builtins$YmrisBound

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.