Package com.hp.jena.ymris.translations

Source Code of com.hp.jena.ymris.translations.AbsRule

/* 
  (c) Copyright 2009 Hewlett-Packard Development Company, LP
  [See end of file]
  $Id$
*/

package com.hp.jena.ymris.translations;

import java.util.*;

import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.reasoner.TriplePattern;
import com.hp.hpl.jena.reasoner.rulesys.ClauseEntry;
import com.hp.hpl.jena.reasoner.rulesys.Functor;
import com.hp.hpl.jena.reasoner.rulesys.Rule;
import com.hp.hpl.jena.shared.PrefixMapping;
import com.hp.hpl.jena.vocabulary.ReasonerVocabulary;

public class AbsRule
    {
    public final String name;
    public final boolean isBackwards;
    public final List<Term> premises;
    public final List<Expression> filters;
    public final List<Term> conclusion;
    public final List<Expression> actions;
    public final List<AbsRule> backwards;
   
    public static final PrefixMapping prefixes = PrefixMapping.Factory.create()
            .setNsPrefixes( PrefixMapping.Extended )
            .setNsPrefix( "rb", ReasonerVocabulary.getRBNamespace() )
    //        .setNsPrefix( "uhj", "urn:x-hp-jena:" )
            ;
   
    public AbsRule
        ( String name
        , boolean isBackwards
        , List<Term> premises
        , List<Expression> filters
        , List<Term> conclusion
        , List<Expression> actions
        , List<AbsRule> backwards
        )
        {
        this.name = name;
        this.isBackwards = isBackwards;
        this.premises = premises;
        this.filters = filters;
        this.conclusion = conclusion;
        this.actions = actions;
        this.backwards = backwards;
        }
   
    public AbsRule( Expression action )
        {
        this
            (
            "[hide]",
            true,
            new ArrayList<Term>(),
            new ArrayList<Expression>(),
            new ArrayList<Term>(),
            list( action ),
            new ArrayList<AbsRule>()
            );
        }

    private static List<Expression> list( Expression... action )
        { return Arrays.asList( action ); }

    public static AbsRule create
        ( String name
        , boolean isBackwards
        , List<Term> premises
        , List<Expression> filters
        , List<Term> conclusion
        , List<Expression> actions
        , List<AbsRule> backwards
        )
        { return new AbsRule( name, isBackwards, premises, filters, conclusion, actions, backwards ); }

    public static AbsRule translateRule( Rule r )
        {
        String name = r.getName();
        boolean isBackwards = r.isBackward();
        List<Expression> filters = new ArrayList<Expression>();
        List<Expression> actions = new ArrayList<Expression>();
        List<AbsRule> backwards = new ArrayList<AbsRule>();
        List<Term> premises = AbsRule.translateClauses( filters, backwards, r.getBody() );
        List<Term> conclusion = AbsRule.translateClauses( actions, backwards, r.getHead() );
        return create( name, isBackwards, premises, filters, conclusion, actions, backwards );
        }

    public static List<Term> translateClauses( List<Expression> expressions, List<AbsRule> backwards, ClauseEntry [] clause )
        {
        List<Term> result = new ArrayList<Term>();
        for (ClauseEntry e: clause) translateClause( result, backwards, expressions, e );
        return result;
        }

    public static void translateClause( List<Term> terms, List<AbsRule> backwards, List<Expression> expressions, ClauseEntry c )
        {
        if (c instanceof TriplePattern)
          {
          TriplePattern tp = (TriplePattern) c;
          Item s = AbsRule.toItem( tp.getSubject() );
          Item p = AbsRule.toItem( tp.getPredicate() );
          Item o = AbsRule.toItem( tp.getObject() );
          terms.add( Term.create( s, p, o ) );
          }
        else if (c instanceof Functor)
          {
          Functor f = (Functor) c;
          if (f.getName().equals( "hide" ))
              backwards.add( new AbsRule( AbsRule.asCondition( f ) ) );
          else
              expressions.add( AbsRule.asCondition( f ) );
          }
        else if (c instanceof com.hp.hpl.jena.reasoner.rulesys.Rule)
          {
          backwards.add( translateRule( (com.hp.hpl.jena.reasoner.rulesys.Rule) c ) );
          }
        else
            System.err.println( "cannot translate ClauseEntry " + c );
        }

    public static Item toItem( Node n )
        { return Item.create( n ); }

    public static Expression asCondition( final Functor f )
        { return new Expression( f.getName(), f.getArgs() ); }

    public static Action actionFor( final String name )
      {
      return new Action()
          {
          public boolean eval( Node[] args )
              {
              System.err.println( "implementing " + name + " as \\args. false." );
              return false;
              }
          };
      }
   
    }

/*
    (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.translations.AbsRule

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.