/*
(c) Copyright 2008, 2009 Hewlett-Packard Development Company, LP
All rights reserved.
$Id$
*/
/*
(c) Copyright 2008 Hewlett-Packard Development Company, LP
All rights reserved.
$Id$
*/
package com.hp.jena.rules.retelike.impl.scratch;
import java.util.*;
import com.hp.jena.graph.Node;
import com.hp.jena.graph.NodeFactory;
import com.hp.jena.graph.Node_Literal;
import com.hp.jena.rules.ast.AllElement;
import com.hp.jena.rules.ast.Element;
import com.hp.jena.rules.ast.Expr;
import com.hp.jena.rules.ast.RuleBody;
import com.hp.jena.rules.ast.Term;
import com.hp.jena.rules.retelike.impl.*;
import com.hp.jena.ymris.util.*;
public class PremiseTranslator
{
protected final CompileTerms compileTerms;
public PremiseTranslator( CompileTerms compileTerms )
{ this.compileTerms = compileTerms; }
void translate
( Set<Binder> binders, Seq<Term> premises, Consumer<Entries> target )
{
if (premises.size() == 0)
{
translateAxiom( binders, target );
}
else if (premises.size() == 1)
{
binders.add( binderForPremise( premises.get( 0 ), target ) );
}
else
{
JoinedBindingConsumers j = new JoinedBindingConsumers( target );
translate( binders, premises.leftPart(), j.L );
translate( binders, premises.rightPart(), j.R );
}
}
private void translateAxiom( Set<Binder> binders, Consumer<Entries> target )
{
// System.err.println( ">> translate: unpremised rule being compiled." );
binders.add
(
new Binder( target )
{
boolean fired = false;
@Override public void start()
{
if (fired)
System.err.println( ">> (suppressing second or subsequent firing" );
else
{
// System.err.println( ">> translate: unpremised rule firing @start." );
consumer.consume( new Entries() );
fired = true;
}
}
}
);
}
private Binder binderForPremise( Term premise, Consumer<Entries> cb )
{
return new Binder( compileTerms.createTripleTerm( premise ), cb );
}
public void translateAlls( Set<Binder> binders, Seq<AllElement> alls, final Consumer<Entries> conclusion )
{
if (alls.size() == 0)
{}
else if (alls.size() == 1)
{
FlattenElements visitor = new FlattenElements();
AllElement element = alls.get( 0 );
final String name = element.getName();
RuleBody rb = element.getRuleBody();
Element premises = rb.getPremises(); premises.visit( visitor );
List<Term> c = rb.getConclusions();
List<Expr> actions = rb.getActions();
Consumer<Entries> intermediateConclusion = new Consumer<Entries>()
{
Set<Node> collection = new HashSet<Node>();
public void consume( Entries item )
{
System.out.println( ">> intermediate consumer for " + name + " with " + item );
Node n = item.get( NodeFactory.createVariable( name ) );
System.out.println( ">> adding " + n );
collection.add( n );
}
public void finish()
{
System.out.println( ">> HO HO A SIGNAL" );
Entries item = new Entries();
Node aggregate = literalAggregate( collection );
item.put( NodeFactory.createVariable( name ), aggregate );
conclusion.consume( item );
}
public void start()
{}
private Node literalAggregate( final Set<Node> collection )
{
return new Node_Literal( "<some set>", "", null )
{
@Override public Object getLiteralValue() { return collection ; }
};
}
};
translate( binders, visitor.premises, intermediateConclusion );
// Binder b = binderForPremise( element, conclusion );
// binders.add( b );
// throw new UnsupportedOperationException( "not finished translateAlls" );
}
else
{
JoinedBindingConsumers j = new JoinedBindingConsumers( conclusion );
translateAlls( binders, alls.leftPart(), j.L );
translateAlls( binders, alls.rightPart(), j.R );
}
}
}
/*
(c) Copyright 2008, 2009 Hewlett-Packard Development Company, LP
All rights reserved.
$Id$
*/