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

Source Code of com.hp.jena.ymris.deploy.functions.LightFunctions$Eq

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

import java.util.Set;

import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
import com.hp.hpl.jena.graph.Graph;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.impl.LiteralLabel;
import com.hp.jena.ymris.yast.expr.*;

/**
    A HowTo with simple-minded implementations of the Ymris functions, adequate
    for testing.
*/
public class LightFunctions implements HowTo
    {
    @Override public Operation operationNamed( String op )
        {
        if (op.equals( "=" )) return new Eq();
        if (op.equals( "+" )) return new Add();
        if (op.equals( "-" )) return new Neg();
        if (op.equals( "before" )) return new Before();
        if (op.equals( "eh:/my#before" )) return new Before();
        if (op.equals( "valof" )) return new Valof();
        if (op.equals( "count" )) return new Count();
        throw new RuntimeException( "don't understand infix operator '" + op + "'" );
        }
   
    static class Valof implements Operation
        {
        public Valof()
            {}
           
        @Override public Node evalValue( Node [] values )
            { return values[0]; }
        }
   
    static class Count implements Operation
        {
        public Count()
            {}
       
        @Override public Node evalValue( Node [] values )
            {
            Node toCount = values[0];
            if (toCount == null) throw new RuntimeException( "Value not bound" );
            if (!toCount.isLiteral()) throw new RuntimeException( "Expected a value, not " + toCount );
            Set<?> elements = (Set<?>) toCount.getLiteralValue();
            return Node.createLiteral( "" + elements.size(), "", XSDDatatype.XSDinteger );
            }
        }
   
    static class Before implements Operation
        {
        public Before()
            {}
           
        public Node evalBool( Node lValue, Node rValue )
            {
            return lValue.toString().compareTo( rValue.toString() ) < 0
                ? Expr.NODE_TRUE
                : Expr.NODE_FALSE
                ;
            }
       
        @Override public Node evalValue( Node[] values )
            {
            if (values.length == 2) return evalBool( values[0], values[1] );
            throw new RuntimeException( "unexpected calling Before.evalBool(Node[])" );
            }
        }
   
    public static class Add implements Operation
        {
        public Add()
            {}
           
        @Override public Node evalValue( Node [] values )
            {
            Node l = values[0], r = values[1];
            Integer lv = (Integer) l.getLiteralValue();
            Integer rv = (Integer) r.getLiteralValue();
            LiteralLabel ll = new LiteralLabel( lv + rv, "", XSDDatatype.XSDinteger );
            return Node.createLiteral( ll );
            }
        }
   
    public static class Neg implements Operation
        {
        public Neg()
            {}
       
        @Override public Node evalValue( Node [] values )
            {
            Node x = values[0];
            Integer xv = (Integer) x.getLiteralValue();
            LiteralLabel ll = new LiteralLabel( -xv, "", XSDDatatype.XSDinteger );
            return Node.createLiteral( ll );
            }
        }

    static class Eq implements Operation
        {
        public Eq()
            {}

        @Override public Node evalValue( Node[] values )
            { return values[0].equals( values[1] ) ? Expr.NODE_TRUE : Expr.NODE_FALSE; }
        }

    @Override public Graph getGraph()
        { throw new RuntimeException( "OOPS" ); }
    }


/*
    (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.LightFunctions$Eq

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.