Example #1
0
        public void StartLateStatement(ContextControllerStatementDesc statement)
        {
            var ids = ContextManager.ContextPartitionIdService.Ids;
            foreach (var cpid in ids) {
                var partitionKeys = ContextManager.ContextPartitionIdService.GetPartitionKeys(cpid);

                // create context properties bean
                var contextBean = ContextManagerUtil.BuildContextProperties(
                    cpid,
                    partitionKeys,
                    ContextManager.ContextDefinition,
                    AgentInstanceContextCreate.StatementContext);

                // create filter proxies
                Supplier<IDictionary<FilterSpecActivatable, FilterValueSetParam[][]>> generator = () =>
                    ContextManagerUtil.ComputeAddendumForStatement(
                        statement,
                        ContextManager.Statements,
                        ContextManager.ContextDefinition.ControllerFactories,
                        partitionKeys,
                        AgentInstanceContextCreate);
                AgentInstanceFilterProxy proxy = new AgentInstanceFilterProxyImpl(generator);

                // start
                AgentInstanceUtil.StartStatement(
                    ContextManager.StatementContextCreate.StatementContextRuntimeServices,
                    cpid,
                    statement,
                    contextBean,
                    proxy);
            }
        }
Example #2
0
        public ContextPartitionInstantiationResult ContextPartitionInstantiate(
            IntSeqKey controllerPathId,
            int subpathId,
            ContextController originator,
            EventBean optionalTriggeringEvent,
            IDictionary<string, object> optionalPatternForInclusiveEval,
            object[] parentPartitionKeys,
            object partitionKey)
        {
            // detect non-leaf
            var controllerEnv = originator.Factory.FactoryEnv;
            if (controllerPathId.Length != controllerEnv.NestingLevel - 1) {
                throw new IllegalStateException("Unexpected controller path");
            }

            if (parentPartitionKeys.Length != controllerEnv.NestingLevel - 1) {
                throw new IllegalStateException("Unexpected partition key size");
            }

            var nestingLevel = controllerEnv.NestingLevel; // starts at 1 for root
            if (nestingLevel < ContextControllers.Length) {
                // next sub-sontext
                var nextContext = ContextControllers[nestingLevel];

                // add a partition key
                var nestedPartitionKeys = AddPartitionKey(nestingLevel, parentPartitionKeys, partitionKey);

                // now post-initialize, this may actually call back
                var childPath = controllerPathId.AddToEnd(subpathId);
                nextContext.Activate(
                    childPath,
                    nestedPartitionKeys,
                    optionalTriggeringEvent,
                    optionalPatternForInclusiveEval);

                return new ContextPartitionInstantiationResult(subpathId, Collections.GetEmptyList<AgentInstance>());
            }

            // assign context id
            var allPartitionKeys = CollectionUtil.AddValue(parentPartitionKeys, partitionKey);
            var assignedContextId = ContextManager.ContextPartitionIdService.AllocateId(allPartitionKeys);

            // build built-in context properties
            var contextBean = ContextManagerUtil.BuildContextProperties(
                assignedContextId,
                allPartitionKeys,
                ContextManager.ContextDefinition,
                AgentInstanceContextCreate.StatementContext);

            // handle leaf creation
            IList<AgentInstance> startedInstances = new List<AgentInstance>(2);
            foreach (var statementEntry in ContextManager.Statements) {
                var statementDesc = statementEntry.Value;

                Supplier<IDictionary<FilterSpecActivatable, FilterValueSetParam[][]>> generator = () =>
                    ContextManagerUtil.ComputeAddendumForStatement(
                        statementDesc,
                        ContextManager.Statements,
                        ContextManager.ContextDefinition.ControllerFactories,
                        allPartitionKeys,
                        AgentInstanceContextCreate);
                
                AgentInstanceFilterProxy proxy = new AgentInstanceFilterProxyImpl(generator);

                var agentInstance = AgentInstanceUtil.StartStatement(
                    ContextManager.StatementContextCreate.StatementContextRuntimeServices,
                    assignedContextId,
                    statementDesc,
                    contextBean,
                    proxy);
                startedInstances.Add(agentInstance);
            }

            // for all new contexts: evaluate this event for this statement
            if (optionalTriggeringEvent != null || optionalPatternForInclusiveEval != null) {
                // comment-in: log.info("Thread " + Thread.currentThread().getId() + " event " + optionalTriggeringEvent.getUnderlying() + " evaluateEventForStatement assignedContextId=" + assignedContextId);
                AgentInstanceUtil.EvaluateEventForStatement(
                    optionalTriggeringEvent,
                    optionalPatternForInclusiveEval,
                    startedInstances,
                    AgentInstanceContextCreate);
            }

            if (ContextManager.ListenersMayNull != null) {
                var identifier = ContextManager.GetContextPartitionIdentifier(allPartitionKeys);
                ContextStateEventUtil.DispatchPartition(
                    ContextManager.ListenersMayNull,
                    () => new ContextStateEventContextPartitionAllocated(
                        AgentInstanceContextCreate.RuntimeURI,
                        ContextManager.ContextRuntimeDescriptor.ContextDeploymentId,
                        ContextManager.ContextDefinition.ContextName,
                        assignedContextId,
                        identifier),
                    (
                        listener,
                        context) => listener.OnContextPartitionAllocated(context));
            }

            return new ContextPartitionInstantiationResult(assignedContextId, startedInstances);
        }