protected override void DoTraverse(Port rootPort, IScanVisitor visitor, bool ignoreHold)
 {
     List<Guid> signals = new List<Guid>();
     foreach(Signal input in rootPort.InputSignals)
         if(!TraverseSignal(signals, input, rootPort, ignoreHold, visitor))
             break; // finished
 }
        public override bool SupportsPort(Port port)
        {
            if(port == null)
                throw new ArgumentNullException("port");

            return port.BusCount == 0 && port.InputSignalCount == port.OutputSignalCount+1;
        }
        private bool TraverseSignal(List<Guid> signals, List<Guid> ports, List<Guid> buses, Signal signal, Port target, bool ignoreHold, IScanVisitor visitor)
        {
            if(signal == null)
                return true;

            bool again = true;
            if(!signals.Contains(signal.InstanceId))
            {
                again = false;
                signals.Add(signal.InstanceId);
            }

            if(visitor.EnterSignal(signal, target, again, target == null))
            {
                // LEAF SIGNAL?
                if(!signal.BehavesAsBeingDriven(ignoreHold))
                {
                    if(!visitor.VisitLeaf(signal, again))
                        return false; // finished
                }
                else // HANDLE PORT
                {
                    TraversePort(signals, ports, buses, signal.DrivenByPort, signal, ignoreHold, visitor);
                }
            }
            return visitor.LeaveSignal(signal, target, again, target == null);
        }
        public override ECategoryMembership IsMember(Port port)
        {
            ECategoryMembership childMembership;
            Entity entity = port.Entity;

            if(port.BusCount != 0)
                return ECategoryMembership.NotMember;

            childMembership = EvaluateJointChildMembership(port.InputSignals);
            if(childMembership == ECategoryMembership.NotMember)
                return ECategoryMembership.NotMember;

            if(entity.EqualsById(_additiveEntity))
            {
                if((_structure & EAlgebraicStructure.AdditiveClose) == EAlgebraicStructure.AdditiveClose && childMembership == ECategoryMembership.Member)
                    return ECategoryMembership.Member;
                else //ECategoryMembership.Unknown
                    return ECategoryMembership.Unknown;
            }
            else if(entity.EqualsById(_multiplicativeEntity))
            {
                if((_structure & EAlgebraicStructure.MultiplicativeClose) == EAlgebraicStructure.MultiplicativeClose && childMembership == ECategoryMembership.Member)
                    return ECategoryMembership.Member;
                else //ECategoryMembership.Unknown
                    return ECategoryMembership.Unknown;
            }
            else
                return ECategoryMembership.NotMember;
        }
 protected override void DoTraverse(Port rootPort, IScanVisitor visitor, bool ignoreHold)
 {
     List<Guid> signals = new List<Guid>();
     List<Guid> ports = new List<Guid>();
     List<Guid> buses = new List<Guid>();
     TraversePort(signals, ports, buses, rootPort, null, ignoreHold, visitor);
 }
 public override bool EnterPort(Port port, Signal parent, bool again, bool root)
 {
     if(again)
         return false;
     _ports.Add(port);
     return true;
 }
 public override bool EnterSignal(Signal signal, Port parent, bool again, bool root)
 {
     if(again)
         return false;
     _signals.Add(signal);
     return true;
 }
 public override bool FulfillsCondition(Signal output, Port port)
 {
     foreach(Condition c in _conditions)
         if(!c.FulfillsCondition(output, port))
             return false;
     return true;
 }
        public override bool FulfillsCondition(Signal output, Port port)
        {
            if(port == null)
                return false;

            return port.HasArchitectureLink && _match(port.CurrentArchitecture);
        }
 public void Traverse(Port rootPort, IScanVisitor visitor, bool ignoreHold)
 {
     //lock(??) {
     ScanStrategy strat = ProvideExecutableInstance();
     strat._activeScans++; //}
     try { strat.DoTraverse(rootPort, visitor, ignoreHold); }
     finally { strat._activeScans--; }
 }
 protected override void ReregisterArchitecture(Port oldPort, Port newPort)
 {
     this.inputSignals = newPort.InputSignals;
     this.outputSignals = newPort.OutputSignals;
     for(int i = 0; i < inputSignals.Count; i++)
         inputSignals[i].ValueChanged += CompoundArchitecture_SignalValueChanged;
     system.PushInputValueRange(inputSignals);
 }
 public override bool VisitLeaf(Port port, bool again)
 {
     // ## REMOVE PLANS ON UNCERTAIN PORTS WITHOUT CHILDS
     if(_plans[port.InstanceId] == ManipulationPlan.AlterIfChildsAltered
         || _plans[port.InstanceId] == ManipulationPlan.CloneIfChildsAltered)
         _plans[port.InstanceId] = ManipulationPlan.DontAlter;
     return base.VisitLeaf(port, again);
 }
 public override bool EnterPort(Port port, Signal parent, bool again, bool root)
 {
     // ## DIVE DEEPER ONLY ON UNCERTAIN PORTS
     if(_plans[port.InstanceId] != ManipulationPlan.AlterIfChildsAltered
         && _plans[port.InstanceId] != ManipulationPlan.CloneIfChildsAltered)
         return false;
     return base.EnterPort(port, parent, again, root);
 }
 public override bool EnterSignal(Signal signal, Port parent, bool again, bool root)
 {
     if(again)
         return false;
     if(_match(signal))
         return _action(signal);
     return true;
 }
        public SyncArchitecture(Port port)
            : base(_entityId, _entityId, false)
        {
            //System.Diagnostics.Debug.Assert(SupportsPort(port));
            SetPort(port);

            port.InputSignals[port.InputSignalCount-1].SignalValueChanged  += SyncArchitecture_SignalValueChanged;
        }
 public override bool VisitCycle(Port port, Signal target, Signal source)
 {
     // ## ON CYCLES, TEST WHETHER THE PORT DEPENDS ON ANY ALTERED PORTS AND ADOPT
     if(Scanner.ExistsPort(port, delegate(Port p) { return _plans[p.InstanceId] == ManipulationPlan.DoAlter; }, _ignoreHold))
         _plans[port.InstanceId] = ManipulationPlan.DoAlter;
     else
         _plans[port.InstanceId] = ManipulationPlan.DontAlter;
     return base.VisitCycle(port, target, source);
 }
 public Architecture InstantiateToPort(Port port)
 {
     foreach(ArchitectureItem item in _items)
     {
         if(item.PortSupport(port))
             return new GenericArchitecture(item.Id, _entityId, item.IsMathematicalOperator,port,item.PortSupport,item.InternalSignals(port),item.ProcessGenerator(port));
     }
     throw new MathNet.Symbolics.Backend.Exceptions.ArchitectureNotAvailableException(port);
 }
        /// <summary>Port Instance Constructor</summary>
        protected GenericMathOpArchitecture(MathIdentifier id, MathIdentifier entityId, Port port)
            : base(id, entityId, true)
        {
            if(port == null) throw new ArgumentNullException("port");
            //System.Diagnostics.Debug.Assert(SupportsPort(port));
            SetPort(port);

            for(int i = 0; i < port.OutputSignalCount; i++)
                port.OutputSignals[i].PostNewValue(UndefinedSymbol.Instance);
        }
 public override bool EnterPort(Port port, Signal parent, bool again, bool root)
 {
     _currentPath.Push(port);
     if(_source.Equals(port))
     {
         List<Port> p = new List<Port>(_currentPath);
         _paths.Add(p);
         return false;
     }
     return base.EnterPort(port, parent, again, root);
 }
 public override bool EnterSignal(Signal signal, Port parent, bool again, bool root)
 {
     _currentPath.Push(signal);
     if(_source.Equals(signal))
     {
         List<Signal> p = new List<Signal>(_currentPath);
         _paths.Add(p);
         return false;
     }
     return base.EnterSignal(signal, parent, again, root);
 }
 public ManipulationPlan EstimatePlan(Port port)
 {
     ITransformationTheorem trans;
     if(_transformations.TryLookupTheorem(port, out trans))
     {
         if(_configure != null)
             _configure(trans);
         return trans.EstimatePlan(port);
     }
     else
         return ManipulationPlan.CloneIfChildsAltered;
 }
 public IEnumerable<Signal> ManipulatePort(Port port, SignalSet manipulatedInputs, bool hasManipulatedInputs)
 {
     ITransformationTheorem trans;
     if(_transformations.TryLookupTheorem(port, out trans))
     {
         if(_configure != null)
             _configure(trans);
         return trans.ManipulatePort(port, manipulatedInputs, hasManipulatedInputs);
     }
     else
         throw new MathNet.Symbolics.Backend.Exceptions.TheoremMismatchException();
 }
 public override bool EnterSignal(Signal signal, Port parent, bool again, bool root)
 {
     if(again)
         return false;
     if(_match(signal))
     {
         _foundSignal = signal;
         _foundSignalTarget = parent;
         return false;
     }
     return true;
 }
 public override bool EnterPort(Port port, Signal parent, bool again, bool root)
 {
     if(again)
         return false;
     if(_match(port))
     {
         _foundPort = port;
         _foundPortTarget = parent;
         return false;
     }
     return true;
 }
        /// <summary>Port Instance Constructor</summary>
        protected GenericSimpleArchitecture(MathIdentifier entityId, bool isMathematicalOperator, Port port, int internalSignalCount)
            : base(entityId, entityId, isMathematicalOperator)
        {
            _internalSignals = new Signal[internalSignalCount];
            for(int i = 0; i < _internalSignals.Length; i++)
                _internalSignals[i] = new Signal(port.Context);

            //System.Diagnostics.Debug.Assert(SupportsPort(port));
            SetPort(port);
            _sensedSignals = new SignalSet();
            SenseSignals(port.InputSignals, _internalSignals, port.Buses, port.OutputSignals);
            Action(port.InputSignals, port.OutputSignals, _internalSignals, port.Buses);
        }
        public bool SupportsPort(Port port)
        {
            if(!port.Entity.EntityId.Equals(_entityId))
                return false;

            foreach(ArchitectureItem item in _items)
            {
                if(item.PortSupport(port))
                    return true;
            }

            return false;
        }
        public Match MatchFirst(Port port)
        {
            ReadOnlySignalSet inputs = port.InputSignals;
            if(_childAxis.Count != inputs.Count)
                return null;

            // Exact, Ordered Matching
            List<MatchCollection> list = new List<MatchCollection>(_childAxis.Count);
            for(int i = 0; i < _childAxis.Count; i++)
                list.Add(_childAxis[i].MatchAll(inputs[i], inputs[i].DrivenByPort, 3));

            return MatchCollection.CombineIntersectFirst(list);
        }
 private bool TraversePort(List<Guid> ports, Port port, Signal target, bool ignoreHold, IScanVisitor visitor)
 {
     if(ports.Contains(port.InstanceId))
         return true;
     ports.Add(port.InstanceId);
     if(visitor.EnterPort(port, target, false, target == null))
     {
         foreach(Signal input in port.InputSignals)
             if(input != null && input.BehavesAsBeingDriven(ignoreHold))
                 if(!TraversePort(ports, input.DrivenByPort, input, ignoreHold, visitor))
                     return false; // finished
     }
     return visitor.LeavePort(port, target, false, target == null);
 }
        public CompoundArchitecture(MathIdentifier id, MathIdentifier entityId, Port port, IMathSystem system)
            : base(id, entityId, false)
        {
            this.inputSignals = port.InputSignals;
            this.outputSignals = port.OutputSignals;
            this.system = system;
            this.system.OutputValueChanged += system_OutputValueChanged;

            SetPort(port);

            for(int i = 0; i < inputSignals.Count; i++)
                inputSignals[i].ValueChanged += CompoundArchitecture_SignalValueChanged;
            system.PushInputValueRange(inputSignals);
        }
 public bool RebindToPortIfSupported(Port newPort)
 {
     if(newPort == null) throw new ArgumentNullException("newPort");
     if(newPort.Equals(_port))
         return true;
     if(SupportsPort(newPort))
     {
         Port oldPort = _port;
         UnregisterArchitecture();
         _port = newPort;
         ReregisterArchitecture(oldPort, newPort);
         return true;
     }
     return false;
 }