11#include " network/synapse.h"
2-
3- Synapse::Synapse (SynapseDefinition* type) : type(type), synapseId(0 ), input(nullptr ), output(nullptr ), propagable(false ) {}
4-
5- Synapse::Synapse (SynapseDefinition* type, Neuron* input, Neuron* output) : type(type), synapseId(0 ), input(new NeuronReference(input, RefType::SYNAPSE_IN)), output(new NeuronReference(output, RefType::SYNAPSE_OUT)), propagable(false ) {}
2+ #include " network/synapse_definition.h"
3+ #include < iostream>
4+ #include < stdexcept>
5+ #include < limits>
6+
7+ // Need to add the missing SynapseDefinition* type member to Synapse class
8+ // This will be a temporary fix until the proper header update is done
9+
10+ Synapse::Synapse (SynapseDefinition* type) : Obj(type), synapseId(0 ), input(nullptr ), output(nullptr ), propagable(false ) {}
11+
12+ Synapse::Synapse (SynapseDefinition* type, Neuron* input, Neuron* output)
13+ : Obj(type), synapseId(0 ),
14+ input(new NeuronReference(input, RefType::SYNAPSE_IN)),
15+ output(new NeuronReference(output, RefType::SYNAPSE_OUT)),
16+ propagable(false )
17+ {
18+ link (input->getModel (), input, output);
19+ }
20+
21+ std::vector<Obj*> Synapse::followManyRelation (Relation* rel) {
22+ if (rel->getRelationId () == SynapseDefinition::LINK.getRelationId ()) {
23+ // Return links - this would need implementation but returning empty for now
24+ return std::vector<Obj*>();
25+ } else {
26+ throw std::runtime_error (" Invalid Relation" );
27+ }
28+ }
29+
30+ Obj* Synapse::followSingleRelation (Relation* rel) {
31+ if (rel->getRelationId () == SynapseDefinition::SELF.getRelationId ()) {
32+ return this ;
33+ } else if (rel->getRelationId () == SynapseDefinition::INPUT.getRelationId ()) {
34+ return getInput ();
35+ } else if (rel->getRelationId () == SynapseDefinition::OUTPUT.getRelationId ()) {
36+ return getOutput ();
37+ } else {
38+ throw std::runtime_error (" Invalid Relation" );
39+ }
40+ }
641
742int Synapse::getSynapseId () const {
843 return synapseId;
@@ -13,12 +48,27 @@ void Synapse::setSynapseId(int synapseId) {
1348}
1449
1550std::map<BSType*, BindingSignal*> Synapse::transitionForward (const std::map<BSType*, BindingSignal*>& inputBindingSignals) {
16- // Implementation for transitioning forward
17- return std::map<BSType*, BindingSignal*>(); // Placeholder
51+ std::map<BSType*, BindingSignal*> outputTransitions;
52+ auto transitions = static_cast <SynapseDefinition*>(getType ())->getTransition ();
53+
54+ for (auto t : transitions) {
55+ auto it = inputBindingSignals.find (t->from ());
56+ if (it != inputBindingSignals.end ()) {
57+ outputTransitions[t->to ()] = it->second ;
58+ }
59+ }
60+
61+ return outputTransitions;
1862}
1963
2064Synapse* Synapse::setPropagable (Model* m, bool propagable) {
65+ if (this ->propagable != propagable) {
66+ input->getNeuron (m)->setModified ();
67+ }
68+
69+ getInput (m)->updatePropagable (output->getNeuron (m), propagable);
2170 this ->propagable = propagable;
71+
2272 return this ;
2373}
2474
@@ -27,7 +77,10 @@ bool Synapse::isPropagable() const {
2777}
2878
2979void Synapse::setModified (Model* m) {
30- // Implementation for setting modified
80+ Neuron* n = getStoredAt ()->getNeuron (m, this );
81+ if (n != nullptr ) {
82+ n->setModified ();
83+ }
3184}
3285
3386void Synapse::setInput (Neuron* n) {
@@ -39,34 +92,48 @@ void Synapse::setOutput(Neuron* n) {
3992}
4093
4194Synapse* Synapse::link (Model* m, Neuron* input, Neuron* output) {
42- // Implementation for linking
95+ synapseId = output->getNewSynapseId ();
96+
97+ setInput (input);
98+ setOutput (output);
99+
100+ link (m);
101+
43102 return this ;
44103}
45104
46105void Synapse::link (Model* m) {
47- // Implementation for linking
106+ input->getNeuron (m)->addOutputSynapse (this );
107+ output->getNeuron (m)->addInputSynapse (this );
48108}
49109
50110void Synapse::unlinkInput (Model* m) {
51- // Implementation for unlinking input
111+ getInput (m)-> removeOutputSynapse ( this );
52112}
53113
54114void Synapse::unlinkOutput (Model* m) {
55- // Implementation for unlinking output
115+ getOutput (m)-> removeInputSynapse ( this );
56116}
57117
58118Link* Synapse::createLink (Activation* input, Activation* output) {
59- // Implementation for creating a link
60- return nullptr ; // Placeholder
119+ return createLink (input, transitionForward (input->getBindingSignals ()), output);
61120}
62121
63122Link* Synapse::createLink (Activation* input, const std::map<BSType*, BindingSignal*>& bindingSignals, Activation* output) {
64- // Implementation for creating a link with binding signals
65- return nullptr ; // Placeholder
123+ if (output->hasConflictingBindingSignals (bindingSignals)) {
124+ return nullptr ;
125+ } else if (output->hasNewBindingSignals (bindingSignals)) {
126+ output = output->branch (bindingSignals);
127+ output->linkIncoming (input);
128+ }
129+
130+ return static_cast <SynapseDefinition*>(getType ())
131+ ->getLink ()
132+ ->instantiate (this , input, output);
66133}
67134
68135Direction* Synapse::getStoredAt () const {
69- return nullptr ; // Placeholder
136+ return static_cast <SynapseDefinition*>( getType ())-> getStoredAt ();
70137}
71138
72139NeuronReference* Synapse::getInputRef () const {
@@ -78,33 +145,67 @@ NeuronReference* Synapse::getOutputRef() const {
78145}
79146
80147Neuron* Synapse::getInput () const {
81- return input->getRawNeuron ();
148+ if (!output || !output->getRawNeuron ()) {
149+ return nullptr ;
150+ }
151+ return getInput (output->getRawNeuron ()->getModel ());
82152}
83153
84154Neuron* Synapse::getInput (Model* m) const {
155+ if (!input) {
156+ return nullptr ;
157+ }
85158 return input->getNeuron (m);
86159}
87160
88161Neuron* Synapse::getOutput () const {
89- return output->getRawNeuron ();
162+ if (!input || !input->getRawNeuron ()) {
163+ return nullptr ;
164+ }
165+ return getOutput (input->getRawNeuron ()->getModel ());
90166}
91167
92168Neuron* Synapse::getOutput (Model* m) const {
169+ if (!output) {
170+ return nullptr ;
171+ }
93172 return output->getNeuron (m);
94173}
95174
175+ Timestamp Synapse::getCreated () const {
176+ // We would need MIN Timestamp defined somewhere
177+ return Timestamp (0 ); // Using 0 as MIN for now
178+ }
179+
180+ Timestamp Synapse::getFired () const {
181+ // We would need MAX Timestamp defined somewhere
182+ return Timestamp (std::numeric_limits<long >::max ()); // Using max long as MAX for now
183+ }
184+
96185void Synapse::deleteSynapse (Model* m) {
97- // Implementation for deleting a synapse
186+ std::cout << " Delete synapse: " << toString () << std::endl;
187+
188+ if (input) {
189+ getInput (m)->removeOutputSynapse (this );
190+ }
191+ if (output) {
192+ getOutput (m)->removeInputSynapse (this );
193+ }
98194}
99195
100196Queue* Synapse::getQueue () const {
101- return nullptr ; // Placeholder
197+ return nullptr ; // Not implemented yet
102198}
103199
104200std::string Synapse::toString () const {
105- return " Synapse: " + std::to_string (synapseId);
201+ return getType ()->getName () +
202+ " in:[" + (input ? input->toKeyString () : " X" ) + " ] " +
203+ " --> " +
204+ " out:[" + (output ? output->toKeyString () : " X" ) + " ])" ;
106205}
107206
108207std::string Synapse::toKeyString () const {
109- return " SynapseKey: " + std::to_string (synapseId);
110- }
208+ return (input ? input->toKeyString () : " X" ) +
209+ " --> " +
210+ (output ? output->toKeyString () : " X" );
211+ }
0 commit comments