/interpreter/branches/data-driven/src/edu/vub/crime_at/symbiosis/NATCurrentContext.java
http://ambienttalk.googlecode.com/ · Java · 94 lines · 64 code · 20 blank · 10 comment · 2 complexity · d54595bd460795033f7c072b6e47e98d MD5 · raw file
- /**
- *
- */
- package edu.vub.crime_at.symbiosis;
- import edu.vub.at.eval.Evaluator;
- import edu.vub.at.exceptions.InterpreterException;
- import edu.vub.at.objects.ATContext;
- import edu.vub.at.objects.ATMessage;
- import edu.vub.at.objects.ATObject;
- import edu.vub.at.objects.ATTable;
- import edu.vub.at.objects.coercion.NativeTypeTags;
- import edu.vub.at.objects.grammar.ATSymbol;
- import edu.vub.at.objects.mirrors.PrimitiveMethod;
- import edu.vub.at.objects.natives.NATNumber;
- import edu.vub.at.objects.natives.NATObject;
- import edu.vub.at.objects.natives.NATTable;
- import edu.vub.at.objects.natives.grammar.AGSymbol;
- import edu.vub.at.signals.Signal;
- import edu.vub.crime.grammar.GenericFact;
- import edu.vub.crime.grammar.attributes.datastructures.Pair;
- import edu.vub.crime.parser.EvaluationVisitor;
- /**
- * @author smostinc
- *
- */
- public class NATCurrentContext extends NATObject {
- protected static final ATSymbol _PUBLISH_ = AGSymbol.jAlloc("publish");
- protected static final ATTable _ARGS_ = NATTable.of(AGSymbol.jAlloc("theObject"), AGSymbol.jAlloc("theExportPattern"));
-
- public NATCurrentContext() throws InterpreterException {
- meta_addMethod(new PrimitiveMethod(_PUBLISH_, _ARGS_, NATTable.of(NativeTypeTags._MUTATOR_)) {
- public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
- ATObject theObject = arguments.base_at(NATNumber.ONE);
- ATObject thePattern = arguments.base_at(NATNumber.atValue(2));
-
- final GenericFact theCrimeFact = DataMapping.createPublicFact(thePattern);
-
- theCrimeFact.addMetaData(new Pair("wrappedObject", DataMapping.toCrime(theObject)));
-
- theCrimeFact.accept(new EvaluationVisitor(RuleEngine.getContextEngine(), true));
-
- // test whether the publication is nested inside a reactive activation or inside a rule body
- // if this is the case, register a listener to automatically remove the fact when the context
- // becomes invalid (i.e. the reactive value changes or the activation is cancelled).
- final Signal theEncapsulator = Evaluator.getDataflowEngine().getEncapsulatingSignal();
- if(theEncapsulator != null) {
- theEncapsulator.registerNestedDependency(new Signal() {
- public int dependencyHeight() {
- return theEncapsulator.dependencyHeight() + 1;
- }
- public void unhook() {
- theCrimeFact.accept(new EvaluationVisitor(RuleEngine.getContextEngine(), false));
- }
- public void updateDependencyHeight(int newDependencyHeight) {
- throw new RuntimeException("Illegal Operation");
- }
- public boolean addDependent(Signal newDependent) {
- throw new RuntimeException("Illegal Operation");
- }
- public boolean performUpdate(Signal theProgenitor, ATMessage theUpdate) {
- throw new RuntimeException("Illegal Operation");
- }
- public void registerNestedDependency(Signal newlyCreatedSignal) {
- throw new RuntimeException("Illegal Operation");
- }
- public boolean removeDependent(Signal anExistingDependent) {
- throw new RuntimeException("Illegal Operation");
- }
- public void scheduleUpdate(Signal theProgenitor, ATMessage theUpdate) {}
- public void unhookNestedDependencies() {
- throw new RuntimeException("Illegal Operation");
- }
- });
- }
- return new NATCrimePublication(thePattern, theCrimeFact);
- }
- });
- }
-
- }