Package com.hp.jena.ymris.yast

Source Code of com.hp.jena.ymris.yast.YFunctor

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

import java.io.PrintStream;
import java.util.*;

import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.reasoner.rulesys.Functor;
import com.hp.hpl.jena.reasoner.rulesys.Functor.FunctorDatatype;
import com.hp.hpl.jena.shared.PrefixMapping;
import com.hp.jena.ymris.deploy.local.*;

public class YFunctor extends YNode
    {
    protected final String f;
    public final List<? extends YNode> args;
   
    public YFunctor( String f, List<? extends YNode> args )
        { this.f = f; this.args = args; }
   
    @Override public String toString()
        { return "<" + f + args + ">"; }
   
    @Override public void prettyPrint( PrintStream pout )
        {
        pout.print( f );
        pout.print( "(" );
        String gap = "";
        for (YNode arg: args)
            { pout.print( gap ); arg.prettyPrint( pout ); gap = " "; }
        pout.print( ")" );
        }
   
    @Override public void addVariables( Set<YVar> vars )
        { for (YNode arg: args) arg.addVariables( vars ); }
   
    @Override public YNode apply( PrefixMapping pm )
        {
        List<YNode> newArgs = new ArrayList<YNode>();
        for (YNode n: args) newArgs.add( n.apply( pm ) );
        return new YFunctor( f, newArgs );
        }
   
    @Override public Match matchBy( final TranslationContext tc )
        {
        return new Match()
            {
            @Override public boolean match( Node toMatch, Node[] b )
                {
                return
                    toMatch.isLiteral()
                    && toMatch.getLiteralDatatype().equals( FunctorDatatype.theFunctorDatatype )
                    && matchAll( (Functor) toMatch.getLiteralValue(), b );
                }
           
            private boolean matchAll( Functor toMatch, Node[] b )
                {
                int index = 0;
                Node [] fargs = toMatch.getArgs();
                if (f.equals( toMatch.getName() ) && toMatch.getArgLength() == args.size())
                    {
                    for (YNode arg: args)
                        {
                        Node farg = fargs[index++];
                        if (arg instanceof YVar)
                            {
                            int offset = tc.varOffset( (YVar) arg );
                            if (b[offset] == null)
                                b[offset] = farg;
                            else if (!b[offset].equals( farg ))
                                return false;
                            }
                        else
                            {
                            if (!farg.equals( arg.asNode() )) return false;
                            }
                        }
                    return true;
                    }
                return false;
                }
            };
        }
   
    @Override public Subst substBy( final TranslationContext c )
        {
        return new Subst()
            {
            @Override public Node subst( Node[] b )
                {
                List<Node> mappedArgs = new ArrayList<Node>();
                for (YNode arg: args) mappedArgs.add( subst( arg, b ) );
                Functor functor = new Functor( f, mappedArgs );
                return Node.createUncachedLiteral( functor, "", FunctorDatatype.theFunctorDatatype );
                }

            private Node subst( YNode arg, Node[] b )
                {
                if (arg instanceof YVar)
                    {
                    int offset = c.varOffset( (YVar) arg );
                    return b[offset];
                    }
                else
                    return arg.asNode();
                }
            };
        }

    @Override public <T> T visit( YNodeVisitor<T> v )
        { throw new UnsupportedOperationException( "TBD" ); }
    }

/*
    (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.yast.YFunctor

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.