/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

  1. /**
  2. *
  3. */
  4. package edu.vub.crime_at.symbiosis;
  5. import edu.vub.at.eval.Evaluator;
  6. import edu.vub.at.exceptions.InterpreterException;
  7. import edu.vub.at.objects.ATContext;
  8. import edu.vub.at.objects.ATMessage;
  9. import edu.vub.at.objects.ATObject;
  10. import edu.vub.at.objects.ATTable;
  11. import edu.vub.at.objects.coercion.NativeTypeTags;
  12. import edu.vub.at.objects.grammar.ATSymbol;
  13. import edu.vub.at.objects.mirrors.PrimitiveMethod;
  14. import edu.vub.at.objects.natives.NATNumber;
  15. import edu.vub.at.objects.natives.NATObject;
  16. import edu.vub.at.objects.natives.NATTable;
  17. import edu.vub.at.objects.natives.grammar.AGSymbol;
  18. import edu.vub.at.signals.Signal;
  19. import edu.vub.crime.grammar.GenericFact;
  20. import edu.vub.crime.grammar.attributes.datastructures.Pair;
  21. import edu.vub.crime.parser.EvaluationVisitor;
  22. /**
  23. * @author smostinc
  24. *
  25. */
  26. public class NATCurrentContext extends NATObject {
  27. protected static final ATSymbol _PUBLISH_ = AGSymbol.jAlloc("publish");
  28. protected static final ATTable _ARGS_ = NATTable.of(AGSymbol.jAlloc("theObject"), AGSymbol.jAlloc("theExportPattern"));
  29. public NATCurrentContext() throws InterpreterException {
  30. meta_addMethod(new PrimitiveMethod(_PUBLISH_, _ARGS_, NATTable.of(NativeTypeTags._MUTATOR_)) {
  31. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  32. ATObject theObject = arguments.base_at(NATNumber.ONE);
  33. ATObject thePattern = arguments.base_at(NATNumber.atValue(2));
  34. final GenericFact theCrimeFact = DataMapping.createPublicFact(thePattern);
  35. theCrimeFact.addMetaData(new Pair("wrappedObject", DataMapping.toCrime(theObject)));
  36. theCrimeFact.accept(new EvaluationVisitor(RuleEngine.getContextEngine(), true));
  37. // test whether the publication is nested inside a reactive activation or inside a rule body
  38. // if this is the case, register a listener to automatically remove the fact when the context
  39. // becomes invalid (i.e. the reactive value changes or the activation is cancelled).
  40. final Signal theEncapsulator = Evaluator.getDataflowEngine().getEncapsulatingSignal();
  41. if(theEncapsulator != null) {
  42. theEncapsulator.registerNestedDependency(new Signal() {
  43. public int dependencyHeight() {
  44. return theEncapsulator.dependencyHeight() + 1;
  45. }
  46. public void unhook() {
  47. theCrimeFact.accept(new EvaluationVisitor(RuleEngine.getContextEngine(), false));
  48. }
  49. public void updateDependencyHeight(int newDependencyHeight) {
  50. throw new RuntimeException("Illegal Operation");
  51. }
  52. public boolean addDependent(Signal newDependent) {
  53. throw new RuntimeException("Illegal Operation");
  54. }
  55. public boolean performUpdate(Signal theProgenitor, ATMessage theUpdate) {
  56. throw new RuntimeException("Illegal Operation");
  57. }
  58. public void registerNestedDependency(Signal newlyCreatedSignal) {
  59. throw new RuntimeException("Illegal Operation");
  60. }
  61. public boolean removeDependent(Signal anExistingDependent) {
  62. throw new RuntimeException("Illegal Operation");
  63. }
  64. public void scheduleUpdate(Signal theProgenitor, ATMessage theUpdate) {}
  65. public void unhookNestedDependencies() {
  66. throw new RuntimeException("Illegal Operation");
  67. }
  68. });
  69. }
  70. return new NATCrimePublication(thePattern, theCrimeFact);
  71. }
  72. });
  73. }
  74. }