├── .gitignore ├── LICENSE.txt ├── NOTICE.txt ├── README.md ├── core ├── pom.xml └── src │ ├── main │ └── java │ │ └── network │ │ └── aika │ │ ├── Config.java │ │ ├── Document.java │ │ ├── Element.java │ │ ├── Model.java │ │ ├── ModelProvider.java │ │ ├── activations │ │ ├── Activation.java │ │ ├── ActivationKey.java │ │ ├── ConjunctiveActivation.java │ │ ├── DisjunctiveActivation.java │ │ ├── InhibitoryActivation.java │ │ └── Link.java │ │ ├── bindingsignal │ │ ├── BSType.java │ │ ├── BindingSignal.java │ │ └── Transition.java │ │ ├── misc │ │ ├── direction │ │ │ ├── Direction.java │ │ │ ├── Input.java │ │ │ └── Output.java │ │ ├── exceptions │ │ │ ├── LockException.java │ │ │ ├── MissingNeuronException.java │ │ │ └── NeuronSerializationException.java │ │ ├── suspension │ │ │ ├── FSSuspensionCallback.java │ │ │ ├── InMemorySuspensionCallback.java │ │ │ └── SuspensionCallback.java │ │ └── utils │ │ │ ├── ReadWriteLock.java │ │ │ └── Utils.java │ │ ├── neurons │ │ ├── ConjunctiveSynapse.java │ │ ├── DisjunctiveSynapse.java │ │ ├── Neuron.java │ │ ├── NeuronReference.java │ │ ├── RefType.java │ │ └── Synapse.java │ │ ├── queue │ │ ├── ElementStep.java │ │ ├── Phase.java │ │ ├── keys │ │ │ └── FiredQueueKey.java │ │ └── steps │ │ │ ├── Fired.java │ │ │ └── Save.java │ │ └── typedefs │ │ ├── ActivationDefinition.java │ │ ├── EdgeDefinition.java │ │ ├── LinkDefinition.java │ │ ├── NeuronDefinition.java │ │ ├── NodeDefinition.java │ │ └── SynapseDefinition.java │ └── test │ ├── java │ └── network │ │ └── aika │ │ └── activations │ │ ├── AbstractActivationTest.java │ │ ├── ActivationTest.java │ │ ├── ConjunctiveActivationTest.java │ │ ├── MinimalNetworkTest.java │ │ ├── TestBSTypes.java │ │ ├── TestUtils.java │ │ └── model │ │ ├── TestActivationFunction.java │ │ ├── TestNeuronDef.java │ │ └── TestTypeModel.java │ └── resources │ └── logback.xml ├── debug ├── pom.xml └── src │ └── main │ └── java │ └── network │ └── aika │ └── debug │ ├── DumpNetwork.java │ ├── DumpTypes.java │ └── OutputConfig.java ├── fields ├── pom.xml └── src │ ├── main │ └── java │ │ └── network │ │ └── aika │ │ ├── exceptions │ │ └── TimeoutException.java │ │ ├── fields │ │ ├── AbstractFunctionDefinition.java │ │ ├── ActivationFunction.java │ │ ├── Addition.java │ │ ├── Division.java │ │ ├── EventListener.java │ │ ├── ExponentialFunction.java │ │ ├── FieldActivationFunction.java │ │ ├── IdentityFunction.java │ │ ├── InputField.java │ │ ├── InvertFunction.java │ │ ├── Multiplication.java │ │ ├── ScaleFunction.java │ │ ├── SoftmaxFields.java │ │ ├── Subtraction.java │ │ ├── SumField.java │ │ ├── ThresholdOperator.java │ │ ├── defs │ │ │ ├── FieldDefinition.java │ │ │ ├── FieldLinkDefinition.java │ │ │ ├── FieldLinkDefinitionInputSide.java │ │ │ ├── FieldLinkDefinitionOutputSide.java │ │ │ ├── FixedArgumentsFieldDefinition.java │ │ │ └── VariableArgumentsFieldDefinition.java │ │ ├── direction │ │ │ ├── Direction.java │ │ │ ├── Input.java │ │ │ └── Output.java │ │ ├── field │ │ │ ├── Field.java │ │ │ ├── FieldInput.java │ │ │ ├── FieldOutput.java │ │ │ ├── QueueInterceptor.java │ │ │ └── UpdateListener.java │ │ └── sign │ │ │ ├── Negative.java │ │ │ ├── Positive.java │ │ │ └── Sign.java │ │ ├── queue │ │ ├── ProcessingPhase.java │ │ ├── Queue.java │ │ ├── QueueProvider.java │ │ ├── Step.java │ │ ├── Timestamp.java │ │ ├── keys │ │ │ ├── FieldQueueKey.java │ │ │ └── QueueKey.java │ │ └── steps │ │ │ └── FieldUpdate.java │ │ ├── type │ │ ├── FlattenedType.java │ │ ├── FlattenedTypeRelation.java │ │ ├── Obj.java │ │ ├── ObjImpl.java │ │ ├── Type.java │ │ ├── TypeRegistry.java │ │ ├── TypeRegistryImpl.java │ │ └── relations │ │ │ ├── AbstractRelation.java │ │ │ ├── Relation.java │ │ │ ├── RelationMany.java │ │ │ ├── RelationOne.java │ │ │ └── RelationSelf.java │ │ └── utils │ │ ├── ApproximateComparisonValueUtil.java │ │ ├── ArrayUtils.java │ │ ├── FieldWritable.java │ │ ├── StringUtils.java │ │ ├── ToleranceUtils.java │ │ └── Writable.java │ └── test │ └── java │ └── network │ └── aika │ └── fields │ ├── hierarchy │ ├── InputSideOverloadingTest.java │ ├── MultipleInheritanceTest.java │ ├── OutputSideOverloadingTest.java │ └── TypeHierarchyTest.java │ ├── manyobjects │ ├── OneToManyRelationTest.java │ ├── TestObjectMany.java │ ├── TestObjectOne.java │ ├── TestTypeMany.java │ └── TestTypeOne.java │ ├── oneobject │ ├── AbstractTestWithObjects.java │ ├── DivisionTest.java │ ├── ExponentialFunctionTest.java │ ├── FieldInstantiationTest.java │ ├── FieldInstantiationWithObjectsTest.java │ ├── MultiplicationTest.java │ ├── ObjectInstantiationTest.java │ ├── SubtractionTest.java │ ├── TestObject.java │ └── TestType.java │ └── softmax │ ├── SoftmaxInputObj.java │ ├── SoftmaxInputType.java │ ├── SoftmaxNormObj.java │ ├── SoftmaxNormType.java │ ├── SoftmaxOutputObj.java │ ├── SoftmaxOutputType.java │ └── SoftmaxTest.java └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | # These are some examples of commonly ignored file patterns. 2 | # You should customize this list as applicable to your project. 3 | # Learn more about .gitignore: 4 | # https://www.atlassian.com/git/tutorials/saving-changes/gitignore 5 | 6 | # Node artifact files 7 | node_modules/ 8 | dist/ 9 | 10 | # Compiled Java class files 11 | *.class 12 | 13 | # Compiled Python bytecode 14 | *.py[cod] 15 | 16 | # Log files 17 | *.log 18 | 19 | # Package files 20 | *.jar 21 | 22 | # Maven 23 | target/ 24 | dist/ 25 | /.flattened-pom.xml 26 | 27 | # JetBrains IDE 28 | .idea/ 29 | 30 | # Unit test reports 31 | TEST*.xml 32 | 33 | # Generated by MacOS 34 | .DS_Store 35 | 36 | # Generated by Windows 37 | Thumbs.db 38 | 39 | # Applications 40 | *.app 41 | *.exe 42 | *.war 43 | 44 | # Large media files 45 | *.mp4 46 | *.tiff 47 | *.avi 48 | *.flv 49 | *.mov 50 | *.wmv 51 | 52 | -------------------------------------------------------------------------------- /NOTICE.txt: -------------------------------------------------------------------------------- 1 | AIKA 2 | 3 | Copyright 2015-2023 Lukas Molzberger 4 | -------------------------------------------------------------------------------- /core/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | aika-project 7 | network.aika 8 | ${revision} 9 | 10 | 4.0.0 11 | 12 | aika-core 13 | ${revision} 14 | 15 | 16 | 17 | network.aika 18 | aika-fields 19 | 20 | 21 | 22 | commons-io 23 | commons-io 24 | 25 | 26 | 27 | 28 | org.slf4j 29 | slf4j-api 30 | 31 | 32 | 33 | 34 | org.junit.jupiter 35 | junit-jupiter-engine 36 | test 37 | 38 | 39 | 40 | org.junit.jupiter 41 | junit-jupiter-params 42 | test 43 | 44 | 45 | 46 | org.junit.jupiter 47 | junit-jupiter-api 48 | test 49 | 50 | 51 | 52 | org.mockito 53 | mockito-junit-jupiter 54 | test 55 | 56 | 57 | 58 | org.mockito 59 | mockito-core 60 | test 61 | 62 | 63 | 64 | ch.qos.logback 65 | logback-core 66 | test 67 | 68 | 69 | 70 | ch.qos.logback 71 | logback-classic 72 | test 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /core/src/main/java/network/aika/Config.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika; 18 | 19 | /** 20 | * 21 | * @author Lukas Molzberger 22 | */ 23 | public class Config { 24 | private Double alpha = null; //0.99; 25 | 26 | private double learnRate; 27 | 28 | private boolean trainingEnabled; 29 | private boolean countingEnabled; 30 | 31 | private long neuronProviderRetention = 50; 32 | 33 | private Long timeout; 34 | 35 | 36 | public double getLearnRate() { 37 | return learnRate; 38 | } 39 | 40 | public Config setLearnRate(double learnRate) { 41 | this.learnRate = learnRate; 42 | return this; 43 | } 44 | public Double getAlpha() { 45 | return alpha; 46 | } 47 | 48 | public Config setAlpha(Double alpha) { 49 | this.alpha = alpha; 50 | return this; 51 | } 52 | 53 | public boolean isTrainingEnabled() { 54 | return trainingEnabled; 55 | } 56 | 57 | public Config setTrainingEnabled(boolean trainingEnabled) { 58 | this.trainingEnabled = trainingEnabled; 59 | return this; 60 | } 61 | 62 | public Config setCountingEnabled(boolean countingEnabled) { 63 | this.countingEnabled = countingEnabled; 64 | return this; 65 | } 66 | 67 | public boolean isCountingEnabled() { 68 | return countingEnabled; 69 | } 70 | 71 | public long getNeuronProviderRetention() { 72 | return neuronProviderRetention; 73 | } 74 | 75 | public Config setNeuronProviderRetention(long neuronProviderRetention) { 76 | this.neuronProviderRetention = neuronProviderRetention; 77 | return this; 78 | } 79 | 80 | public Long getTimeout() { 81 | return timeout; 82 | } 83 | 84 | public Config setTimeout(Long timeout) { 85 | this.timeout = timeout; 86 | return this; 87 | } 88 | 89 | public String toString() { 90 | return "Alpha: " + alpha + "\n" + 91 | "LearnRate" + learnRate + "\n\n"; 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /core/src/main/java/network/aika/Element.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika; 18 | 19 | 20 | import network.aika.queue.Timestamp; 21 | 22 | /** 23 | * An Element is either a node (Activation) or an edge (Link) in the Activation graph. 24 | * 25 | * @author Lukas Molzberger 26 | */ 27 | public interface Element { 28 | 29 | Timestamp getCreated(); 30 | 31 | Timestamp getFired(); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /core/src/main/java/network/aika/ModelProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika; 18 | 19 | /** 20 | * @author Lukas Molzberger 21 | */ 22 | public interface ModelProvider { 23 | 24 | Model getModel(); 25 | 26 | default Config getConfig() { 27 | return getModel().getConfig(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /core/src/main/java/network/aika/activations/ActivationKey.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.activations; 18 | 19 | /** 20 | * @author Lukas Molzberger 21 | */ 22 | public record ActivationKey(Long neuronId, Integer actId) { 23 | 24 | } 25 | -------------------------------------------------------------------------------- /core/src/main/java/network/aika/activations/ConjunctiveActivation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.activations; 18 | 19 | import network.aika.Document; 20 | import network.aika.bindingsignal.BindingSignal; 21 | import network.aika.neurons.Neuron; 22 | import network.aika.neurons.Synapse; 23 | import network.aika.typedefs.ActivationDefinition; 24 | import network.aika.bindingsignal.BSType; 25 | import network.aika.typedefs.SynapseDefinition; 26 | 27 | import java.util.Map; 28 | import java.util.NavigableMap; 29 | import java.util.TreeMap; 30 | import java.util.stream.Stream; 31 | 32 | /** 33 | * @author Lukas Molzberger 34 | */ 35 | public class ConjunctiveActivation extends Activation { 36 | 37 | protected NavigableMap inputLinks = new TreeMap<>(); 38 | 39 | public ConjunctiveActivation( 40 | ActivationDefinition t, 41 | Activation parent, 42 | Integer id, 43 | Neuron n, 44 | Document doc, 45 | Map bindingSignals 46 | ) { 47 | super(t, parent, id, n, doc, bindingSignals); 48 | } 49 | 50 | @Override 51 | public void linkIncoming(Activation excludedInputAct) { 52 | neuron 53 | .getInputSynapsesAsStream() 54 | .filter(s -> 55 | ((SynapseDefinition)s.getType()).isIncomingLinkingCandidate(getBindingSignals().keySet()) 56 | ) 57 | .forEach(s -> 58 | linkIncoming(s, excludedInputAct) 59 | ); 60 | } 61 | 62 | void linkIncoming(Synapse targetSyn, Activation excludedInputAct) { 63 | collectLinkingTargets(targetSyn.getInput(getModel())).stream() 64 | .filter(iAct -> iAct != excludedInputAct) 65 | .forEach(iAct -> 66 | targetSyn.createLink( 67 | iAct, 68 | this 69 | ) 70 | ); 71 | } 72 | 73 | @Override 74 | public void addInputLink(Link l) { 75 | Synapse syn = l.getSynapse(); 76 | assert inputLinks.get(syn.getSynapseId()) == null; 77 | inputLinks.put(syn.getSynapseId(), l); 78 | } 79 | 80 | @Override 81 | public Stream getInputLinks() { 82 | return inputLinks.values() 83 | .stream(); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /core/src/main/java/network/aika/activations/DisjunctiveActivation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.activations; 18 | 19 | import network.aika.Document; 20 | import network.aika.bindingsignal.BindingSignal; 21 | import network.aika.neurons.Neuron; 22 | import network.aika.typedefs.ActivationDefinition; 23 | import network.aika.bindingsignal.BSType; 24 | 25 | import java.util.Map; 26 | import java.util.NavigableMap; 27 | import java.util.TreeMap; 28 | import java.util.stream.Stream; 29 | 30 | /** 31 | * @author Lukas Molzberger 32 | */ 33 | public class DisjunctiveActivation extends Activation { 34 | 35 | protected NavigableMap inputLinks = new TreeMap<>(); 36 | 37 | public DisjunctiveActivation( 38 | ActivationDefinition t, 39 | Activation parent, 40 | Integer id, 41 | Neuron n, 42 | Document doc, 43 | Map bindingSignals 44 | ) { 45 | super(t, parent, id, n, doc, bindingSignals); 46 | } 47 | 48 | @Override 49 | public void addInputLink(Link l) { 50 | Activation iAct = l.getInput(); 51 | assert inputLinks.get(iAct.getId()) == null; 52 | inputLinks.put(iAct.getId(), l); 53 | } 54 | 55 | @Override 56 | public void linkIncoming(Activation excludedInputAct) { 57 | 58 | } 59 | 60 | @Override 61 | public Stream getInputLinks() { 62 | return inputLinks.values() 63 | .stream(); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /core/src/main/java/network/aika/bindingsignal/BSType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.bindingsignal; 18 | 19 | /** 20 | * 21 | * @author Lukas Molzberger 22 | */ 23 | public interface BSType { 24 | 25 | } 26 | -------------------------------------------------------------------------------- /core/src/main/java/network/aika/bindingsignal/BindingSignal.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.bindingsignal; 18 | 19 | import network.aika.Document; 20 | import network.aika.activations.Activation; 21 | import network.aika.activations.ActivationKey; 22 | import network.aika.neurons.Neuron; 23 | 24 | import java.util.*; 25 | import java.util.stream.Stream; 26 | 27 | /** 28 | * 29 | * @author Lukas Molzberger 30 | */ 31 | public class BindingSignal { 32 | 33 | private final int tokenId; 34 | 35 | private final Document doc; 36 | 37 | private final NavigableMap activations = new TreeMap<>( 38 | Comparator.comparingLong(ActivationKey::neuronId) 39 | .thenComparingInt(ActivationKey::actId) 40 | ); 41 | 42 | public BindingSignal(int tokenId, Document doc) { 43 | this.tokenId = tokenId; 44 | this.doc = doc; 45 | } 46 | 47 | public int getTokenId() { 48 | return tokenId; 49 | } 50 | 51 | public Document getDocument() { 52 | return doc; 53 | } 54 | 55 | public void addActivation(Activation act) { 56 | activations.put(act.getKey(), act); 57 | } 58 | 59 | public Stream getActivations(Neuron n) { 60 | return activations.subMap( 61 | new ActivationKey(n.getId(), Integer.MIN_VALUE), 62 | new ActivationKey(n.getId(), Integer.MAX_VALUE) 63 | ) 64 | .values() 65 | .stream(); 66 | } 67 | 68 | public Collection getActivations() { 69 | return activations.values(); 70 | } 71 | 72 | public String toString() { 73 | return "" + tokenId; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /core/src/main/java/network/aika/bindingsignal/Transition.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.bindingsignal; 18 | 19 | /** 20 | * 21 | * @author Lukas Molzberger 22 | */ 23 | public record Transition(BSType from, BSType to) { 24 | 25 | public static Transition of(BSType from, BSType to) { 26 | return new Transition(from, to); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /core/src/main/java/network/aika/misc/direction/Direction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.misc.direction; 18 | 19 | import network.aika.Model; 20 | import network.aika.activations.Activation; 21 | import network.aika.activations.Link; 22 | import network.aika.neurons.Neuron; 23 | import network.aika.neurons.Synapse; 24 | import network.aika.bindingsignal.BSType; 25 | import network.aika.bindingsignal.Transition; 26 | 27 | import java.io.DataInput; 28 | import java.io.DataOutput; 29 | import java.io.IOException; 30 | 31 | /** 32 | * 33 | * @author Lukas Molzberger 34 | */ 35 | public interface Direction { 36 | Direction INPUT = new Input(); 37 | Direction OUTPUT = new Output(); 38 | 39 | Direction invert(); 40 | 41 | I getInput(I from, I to); 42 | 43 | O getOutput(O from, O to); 44 | 45 | Neuron getNeuron(Model m, Synapse s); 46 | 47 | Activation getActivation(Link l); 48 | 49 | int getOrder(); 50 | 51 | BSType transition(BSType s, Transition[] t); 52 | 53 | void write(DataOutput out) throws IOException; 54 | 55 | static Direction read(DataInput in) throws IOException { 56 | return in.readBoolean() ? OUTPUT : INPUT; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /core/src/main/java/network/aika/misc/direction/Input.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.misc.direction; 18 | 19 | import network.aika.Model; 20 | import network.aika.activations.Activation; 21 | import network.aika.activations.Link; 22 | import network.aika.neurons.Neuron; 23 | import network.aika.neurons.Synapse; 24 | import network.aika.bindingsignal.BSType; 25 | import network.aika.bindingsignal.Transition; 26 | 27 | import java.io.DataOutput; 28 | import java.io.IOException; 29 | 30 | /** 31 | * 32 | * @author Lukas Molzberger 33 | */ 34 | public class Input implements Direction { 35 | 36 | @Override 37 | public Direction invert() { 38 | return OUTPUT; 39 | } 40 | 41 | @Override 42 | public I getInput(I from, I to) { 43 | return to; 44 | } 45 | 46 | @Override 47 | public O getOutput(O from, O to) { 48 | return from; 49 | } 50 | 51 | @Override 52 | public Neuron getNeuron(Model m, Synapse s) { 53 | return s.getInput(m); 54 | } 55 | 56 | @Override 57 | public Activation getActivation(Link l) { 58 | return l != null ? 59 | l.getInput() : 60 | null; 61 | } 62 | 63 | @Override 64 | public BSType transition(BSType s, Transition[] trns) { 65 | for(Transition t: trns) 66 | if(t.to() == s) 67 | return t.from(); 68 | 69 | return null; 70 | } 71 | 72 | @Override 73 | public int getOrder() { 74 | return -1; 75 | } 76 | 77 | @Override 78 | public void write(DataOutput out) throws IOException { 79 | out.writeBoolean(false); 80 | } 81 | 82 | public String toString() { 83 | return "INPUT"; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /core/src/main/java/network/aika/misc/direction/Output.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.misc.direction; 18 | 19 | import network.aika.Model; 20 | import network.aika.activations.Activation; 21 | import network.aika.activations.Link; 22 | import network.aika.neurons.Neuron; 23 | import network.aika.neurons.Synapse; 24 | import network.aika.bindingsignal.BSType; 25 | import network.aika.bindingsignal.Transition; 26 | 27 | import java.io.DataOutput; 28 | import java.io.IOException; 29 | 30 | /** 31 | * 32 | * @author Lukas Molzberger 33 | */ 34 | public class Output implements Direction { 35 | 36 | @Override 37 | public Direction invert() { 38 | return INPUT; 39 | } 40 | 41 | @Override 42 | public I getInput(I from, I to) { 43 | return from; 44 | } 45 | 46 | @Override 47 | public O getOutput(O from, O to) { 48 | return to; 49 | } 50 | 51 | @Override 52 | public Neuron getNeuron(Model m, Synapse s) { 53 | return s.getOutput(m); 54 | } 55 | 56 | @Override 57 | public Activation getActivation(Link l) { 58 | return l != null ? 59 | l.getOutput() : 60 | null; 61 | } 62 | 63 | @Override 64 | public BSType transition(BSType s, Transition[] trns) { 65 | for(Transition t: trns) 66 | if(t.from() == s) 67 | return t.to(); 68 | 69 | return null; 70 | } 71 | 72 | @Override 73 | public int getOrder() { 74 | return 1; 75 | } 76 | 77 | @Override 78 | public void write(DataOutput out) throws IOException { 79 | out.writeBoolean(true); 80 | } 81 | 82 | public String toString() { 83 | return "OUTPUT"; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /core/src/main/java/network/aika/misc/exceptions/LockException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.misc.exceptions; 18 | 19 | 20 | import static java.lang.String.format; 21 | 22 | /** 23 | * 24 | * @author Lukas Molzberger 25 | */ 26 | public class LockException extends RuntimeException { 27 | 28 | public LockException(Throwable cause) { 29 | super(format("Failed acquire or release lock"), cause); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /core/src/main/java/network/aika/misc/exceptions/MissingNeuronException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.misc.exceptions; 18 | 19 | import static java.lang.String.format; 20 | 21 | /** 22 | * 23 | * @author Lukas Molzberger 24 | */ 25 | public class MissingNeuronException extends RuntimeException { 26 | 27 | public MissingNeuronException(Long id, String modelLabel) { 28 | super(format("Neuron with id [%d] is missing in model label %s.", id, modelLabel)); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /core/src/main/java/network/aika/misc/exceptions/NeuronSerializationException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.misc.exceptions; 18 | 19 | 20 | import static java.lang.String.format; 21 | 22 | /** 23 | * 24 | * @author Lukas Molzberger 25 | */ 26 | public class NeuronSerializationException extends RuntimeException { 27 | 28 | public NeuronSerializationException(Long neuronId, Throwable cause) { 29 | super(format("Failed to serialize or deserialize the neuron [%d]", neuronId), cause); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /core/src/main/java/network/aika/misc/suspension/InMemorySuspensionCallback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.misc.suspension; 18 | 19 | import network.aika.Model; 20 | 21 | import java.io.IOException; 22 | import java.util.Map; 23 | import java.util.TreeMap; 24 | import java.util.concurrent.atomic.AtomicInteger; 25 | 26 | /** 27 | * 28 | * @author Lukas Molzberger 29 | */ 30 | public class InMemorySuspensionCallback implements SuspensionCallback { 31 | 32 | private final AtomicInteger currentId = new AtomicInteger(0); 33 | 34 | private final Map storage = new TreeMap<>(); 35 | private final Map tokenIds = new TreeMap<>(); 36 | 37 | @Override 38 | public void prepareNewModel() { 39 | 40 | } 41 | 42 | @Override 43 | public void open() throws IOException { 44 | 45 | } 46 | 47 | @Override 48 | public void close() { 49 | 50 | } 51 | 52 | @Override 53 | public long createId() { 54 | return currentId.addAndGet(1); 55 | } 56 | 57 | @Override 58 | public long getCurrentId() { 59 | return currentId.get(); 60 | } 61 | 62 | @Override 63 | public void store(Long id, byte[] data) { 64 | storage.put(id, data); 65 | } 66 | 67 | @Override 68 | public void remove(Long id) { 69 | storage.remove(id); 70 | } 71 | 72 | @Override 73 | public byte[] retrieve(Long id) { 74 | return storage.get(id); 75 | } 76 | 77 | @Override 78 | public Long getIdByTokenId(int tokenId) { 79 | return tokenIds.get(tokenId); 80 | } 81 | 82 | @Override 83 | public void putTokenId(int tokenId, Long id) { 84 | tokenIds.put(tokenId, id); 85 | } 86 | 87 | @Override 88 | public void removeTokenId(int tokenId) { 89 | tokenIds.remove(tokenId); 90 | } 91 | 92 | @Override 93 | public void loadIndex(Model m) { 94 | throw new UnsupportedOperationException(); 95 | } 96 | 97 | @Override 98 | public void saveIndex(Model m) { 99 | throw new UnsupportedOperationException(); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /core/src/main/java/network/aika/misc/suspension/SuspensionCallback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.misc.suspension; 18 | 19 | import network.aika.Model; 20 | 21 | import java.io.IOException; 22 | 23 | /** 24 | * 25 | * The suspension hook is used to suspend neurons to an external storage in order to reduce the memory footprint. 26 | * 27 | * !!! Important: When using the suspension hook, all references to a neuron need to occur through a 28 | * provider. Otherwise, the reference might be outdated. 29 | * 30 | * @author Lukas Molzberger 31 | */ 32 | public interface SuspensionCallback { 33 | 34 | void prepareNewModel() throws IOException; 35 | 36 | void open() throws IOException; 37 | 38 | void close() throws IOException; 39 | 40 | long createId(); 41 | 42 | long getCurrentId(); 43 | 44 | void store(Long id, byte[] data) throws IOException; 45 | 46 | void remove(Long id) throws IOException; 47 | 48 | byte[] retrieve(Long id) throws IOException; 49 | 50 | Long getIdByTokenId(int tokenId); 51 | 52 | void putTokenId(int tokenId, Long id); 53 | 54 | void removeTokenId(int tokenId); 55 | 56 | void loadIndex(Model m); 57 | 58 | void saveIndex(Model m) throws IOException; 59 | } 60 | -------------------------------------------------------------------------------- /core/src/main/java/network/aika/misc/utils/ReadWriteLock.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.misc.utils; 18 | 19 | import network.aika.misc.exceptions.LockException; 20 | 21 | /** 22 | * 23 | * @author Lukas Molzberger 24 | */ 25 | public class ReadWriteLock { 26 | 27 | private int readers = 0; 28 | private int writers = 0; 29 | private int writeRequests = 0; 30 | private long writerThreadId = -1; 31 | private int waitForReadLock = 0; 32 | private int waitForWriteLock = 0; 33 | 34 | private final Object writeLock = new Object(); 35 | 36 | public void acquireWriteLock() { 37 | try { 38 | synchronized (this) { 39 | writeRequests++; 40 | while (readers > 0) { 41 | wait(); 42 | } 43 | } 44 | 45 | long tid = Thread.currentThread().threadId(); 46 | synchronized (writeLock) { 47 | if(writerThreadId != tid) { 48 | waitForWriteLock++; 49 | while (writers > 0) { 50 | writeLock.wait(); 51 | } 52 | waitForWriteLock--; 53 | writerThreadId = tid; 54 | } 55 | writers++; 56 | } 57 | } catch(InterruptedException e) { 58 | throw new LockException(e); 59 | } 60 | } 61 | 62 | public synchronized void acquireReadLock() { 63 | try { 64 | waitForReadLock++; 65 | while (writeRequests > 0) { 66 | wait(); 67 | } 68 | waitForReadLock--; 69 | readers++; 70 | } catch(InterruptedException e) { 71 | throw new LockException(e); 72 | } 73 | } 74 | 75 | public void releaseWriteLock() { 76 | synchronized (writeLock) { 77 | writers--; 78 | if(writers == 0) { 79 | writerThreadId = -1; 80 | if(waitForWriteLock > 0) { 81 | writeLock.notify(); 82 | } 83 | } 84 | } 85 | synchronized(this) { 86 | writeRequests--; 87 | if(writeRequests == 0 && waitForReadLock > 0) { 88 | notifyAll(); 89 | } 90 | } 91 | } 92 | 93 | public synchronized void releaseReadLock() { 94 | readers--; 95 | if(writeRequests > 0 && readers == 0) { 96 | notifyAll(); 97 | } 98 | } 99 | } -------------------------------------------------------------------------------- /core/src/main/java/network/aika/misc/utils/Utils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.misc.utils; 18 | 19 | 20 | import network.aika.activations.Activation; 21 | 22 | /** 23 | * 24 | * @author Lukas Molzberger 25 | */ 26 | public class Utils { 27 | 28 | public static double TOLERANCE = 0.001; 29 | 30 | public static double[] add(double[] a, double[] b) { 31 | if(a == null) 32 | return b; 33 | if(b == null) 34 | return a; 35 | 36 | double[] r = new double[a.length]; 37 | for(int i = 0; i < r.length; i++) 38 | r[i] = a[i] + b[i]; 39 | return r; 40 | } 41 | 42 | public static double[] scale(double[] a, double s) { 43 | double[] r = new double[a.length]; 44 | for(int i = 0; i < r.length; i++) 45 | r[i] = a[i] * s; 46 | return r; 47 | } 48 | 49 | public static double sum(double[] a) { 50 | double sum = 0; 51 | for(int i = 0; i < a.length; i++) 52 | sum += a[i]; 53 | return sum; 54 | } 55 | 56 | public static boolean belowTolerance(Double tolerance, double[] x) { 57 | if(x == null) 58 | return true; 59 | 60 | if(tolerance == null) 61 | return false; 62 | 63 | return Math.abs(sum(x)) < tolerance; 64 | } 65 | 66 | public static boolean belowTolerance(Double tolerance, double x) { 67 | if(x == 0.0) 68 | return true; 69 | 70 | if(tolerance == null) 71 | return false; 72 | 73 | return Math.abs(x) < tolerance; 74 | } 75 | 76 | public static String depthToSpace(int depth) { 77 | StringBuilder sb = new StringBuilder(); 78 | for(int i = 0; i < depth; i++) 79 | sb.append(" "); 80 | return sb.toString(); 81 | } 82 | 83 | public static String idToString(Activation act) { 84 | return act != null ? "" + act.getId() : "--"; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /core/src/main/java/network/aika/neurons/ConjunctiveSynapse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.neurons; 18 | 19 | import network.aika.type.TypeRegistry; 20 | import network.aika.typedefs.SynapseDefinition; 21 | 22 | import java.io.DataInput; 23 | import java.io.DataOutput; 24 | import java.io.IOException; 25 | 26 | /** 27 | * 28 | * @author Lukas Molzberger 29 | */ 30 | public class ConjunctiveSynapse extends Synapse { 31 | 32 | public ConjunctiveSynapse(SynapseDefinition type) { 33 | super(type); 34 | } 35 | 36 | public ConjunctiveSynapse(SynapseDefinition type, Neuron input, Neuron output) { 37 | super(type, input, output); 38 | } 39 | 40 | 41 | @Override 42 | public void write(DataOutput out) throws IOException { 43 | super.write(out); 44 | 45 | out.writeBoolean(propagable); 46 | } 47 | 48 | @Override 49 | public void readFields(DataInput in, TypeRegistry tr) throws IOException { 50 | super.readFields(in, tr); 51 | 52 | propagable = in.readBoolean(); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /core/src/main/java/network/aika/neurons/DisjunctiveSynapse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.neurons; 18 | 19 | import network.aika.Model; 20 | import network.aika.typedefs.SynapseDefinition; 21 | 22 | /** 23 | * 24 | * @author Lukas Molzberger 25 | */ 26 | public class DisjunctiveSynapse extends Synapse { 27 | 28 | public DisjunctiveSynapse(SynapseDefinition type) { 29 | super(type); 30 | } 31 | 32 | public DisjunctiveSynapse(SynapseDefinition type, Neuron input, Neuron output) { 33 | super(type, input, output); 34 | 35 | propagable = true; 36 | } 37 | 38 | @Override 39 | public void link(Model m) { 40 | getInput(m).addOutputSynapse(this); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /core/src/main/java/network/aika/neurons/NeuronReference.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.neurons; 18 | 19 | import network.aika.Model; 20 | 21 | /** 22 | * 23 | * @author Lukas Molzberger 24 | */ 25 | public class NeuronReference { 26 | 27 | private final long id; 28 | private final RefType refType; 29 | 30 | private Neuron neuron; 31 | 32 | 33 | public NeuronReference(long neuronId, RefType refType) { 34 | this.id = neuronId; 35 | this.refType = refType; 36 | } 37 | 38 | public NeuronReference(Neuron n, RefType refType) { 39 | this.id = n.getId(); 40 | this.refType = refType; 41 | this.neuron = n; 42 | } 43 | 44 | public long getId() { 45 | return id; 46 | } 47 | 48 | public Neuron getRawNeuron() { 49 | return neuron; 50 | } 51 | 52 | public synchronized N getNeuron(Model m) { 53 | if (neuron == null) { 54 | neuron = m.getNeuron(id); 55 | neuron.increaseRefCount(refType); 56 | } 57 | 58 | return (N) neuron; 59 | } 60 | 61 | public void suspendNeuron() { 62 | assert neuron != null; 63 | 64 | neuron.decreaseRefCount(refType); 65 | neuron = null; 66 | } 67 | 68 | public String toString() { 69 | return "p(" + (neuron != null ? neuron : id + ":" + "SUSPENDED") + ")"; 70 | } 71 | 72 | public String toKeyString() { 73 | return "p(" + (neuron != null ? neuron.toKeyString() : id + ":" + "SUSPENDED") + ")"; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /core/src/main/java/network/aika/neurons/RefType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.neurons; 18 | 19 | /** 20 | * 21 | * @author Lukas Molzberger 22 | */ 23 | public enum RefType { 24 | NEURON_EXTERNAL, 25 | PERSISTENCE, 26 | SYNAPSE_IN, 27 | SYNAPSE_OUT, 28 | PROPAGABLE, 29 | TEMPLATE, 30 | CATEGORY, 31 | TEMPLATE_MODEL, 32 | OTHER 33 | } 34 | -------------------------------------------------------------------------------- /core/src/main/java/network/aika/queue/ElementStep.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.queue; 18 | 19 | import network.aika.Element; 20 | import network.aika.queue.keys.FiredQueueKey; 21 | 22 | /** 23 | * 24 | * @author Lukas Molzberger 25 | */ 26 | public abstract class ElementStep extends Step { 27 | 28 | private E element; 29 | 30 | public ElementStep(E element) { 31 | this.element = element; 32 | } 33 | 34 | @Override 35 | public Queue getQueue() { 36 | return element.getQueue(); 37 | } 38 | 39 | @Override 40 | public void createQueueKey(Timestamp timestamp, int round) { 41 | queueKey = new FiredQueueKey( 42 | round, 43 | getPhase(), 44 | element, 45 | timestamp 46 | ); 47 | } 48 | 49 | public E getElement() { 50 | return element; 51 | } 52 | 53 | @Override 54 | public String toString() { 55 | return "" + getElement(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /core/src/main/java/network/aika/queue/Phase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.queue; 18 | 19 | /** 20 | * 21 | * @author Lukas Molzberger 22 | */ 23 | public enum Phase implements ProcessingPhase { 24 | INFERENCE(), 25 | FIRED(), 26 | INSTANTIATION_TRIGGER(true), 27 | TRAINING(true), 28 | INACTIVE_LINKS(true), 29 | SAVE(true); 30 | 31 | 32 | boolean delayed; 33 | 34 | Phase() { 35 | } 36 | 37 | Phase(boolean delayed) { 38 | this.delayed = delayed; 39 | } 40 | 41 | @Override 42 | public int rank() { 43 | return ordinal(); 44 | } 45 | 46 | @Override 47 | public boolean isDelayed() { 48 | return delayed; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /core/src/main/java/network/aika/queue/keys/FiredQueueKey.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.queue.keys; 18 | 19 | import network.aika.Element; 20 | import network.aika.queue.ProcessingPhase; 21 | import network.aika.queue.Timestamp; 22 | 23 | import java.util.Comparator; 24 | 25 | import static network.aika.queue.Timestamp.NOT_SET; 26 | 27 | /** 28 | * @author Lukas Molzberger 29 | */ 30 | public class FiredQueueKey extends QueueKey { 31 | 32 | protected static Comparator COMPARATOR = Comparator 33 | .comparing(k -> k.fired) 34 | .thenComparing(k -> k.created); 35 | 36 | private final Timestamp created; 37 | private final Timestamp fired; 38 | 39 | public FiredQueueKey(int round, ProcessingPhase phase, Element element, Timestamp currentTimestamp) { 40 | super(round, phase, currentTimestamp); 41 | this.created = element.getCreated(); 42 | this.fired = element.getFired(); 43 | } 44 | 45 | public Timestamp getFired() { 46 | return fired; 47 | } 48 | 49 | public Timestamp getCreated() { 50 | return created; 51 | } 52 | 53 | @Override 54 | public String toString() { 55 | String firedStr = getFired() == NOT_SET ? 56 | "NOT_FIRED" : "" + 57 | getFired(); 58 | 59 | return "[r:" + getRoundStr() + 60 | ",p:" + getPhaseStr() + 61 | ",f:" + firedStr + 62 | ",c:" + getCreated() + 63 | ",ts:" + getCurrentTimestamp() + 64 | "]"; 65 | } 66 | 67 | @Override 68 | public int compareTo(QueueKey qk) { 69 | return COMPARATOR.compare(this, (FiredQueueKey) qk); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /core/src/main/java/network/aika/queue/steps/Fired.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.queue.steps; 18 | 19 | import network.aika.activations.Activation; 20 | import network.aika.queue.Phase; 21 | import network.aika.queue.Queue; 22 | import network.aika.queue.Timestamp; 23 | import network.aika.queue.Step; 24 | import network.aika.queue.keys.FieldQueueKey; 25 | import network.aika.utils.ApproximateComparisonValueUtil; 26 | import network.aika.utils.StringUtils; 27 | 28 | import static network.aika.queue.Phase.FIRED; 29 | 30 | /** 31 | * 32 | * @author Lukas Molzberger 33 | */ 34 | public class Fired extends Step { 35 | 36 | private final Activation act; 37 | 38 | private double net; 39 | 40 | private int sortValue; 41 | 42 | public Fired(Activation act) { 43 | this.act = act; 44 | } 45 | 46 | @Override 47 | public void createQueueKey(Timestamp timestamp, int round) { 48 | queueKey = new FieldQueueKey( 49 | round, 50 | getPhase(), 51 | sortValue, 52 | timestamp 53 | ); 54 | } 55 | 56 | @Override 57 | public void process() { 58 | Activation act = getElement(); 59 | 60 | act.setFired(); 61 | 62 | // Only once the activation is fired, will it be visible to other neurons. 63 | act.getBindingSignals() 64 | .values(). 65 | forEach(bs -> 66 | bs.addActivation(act) 67 | ); 68 | 69 | act.linkOutgoing(); 70 | } 71 | 72 | public void updateNet(double net) { 73 | this.net = net; 74 | sortValue = ApproximateComparisonValueUtil.convert(net); 75 | } 76 | 77 | @Override 78 | public Phase getPhase() { 79 | return FIRED; 80 | } 81 | 82 | @Override 83 | public Activation getElement() { 84 | return act; 85 | } 86 | 87 | @Override 88 | public Queue getQueue() { 89 | return act.getDocument(); 90 | } 91 | 92 | @Override 93 | public String toString() { 94 | return "" + getElement() + " net:" + StringUtils.doubleToString(net); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /core/src/main/java/network/aika/queue/steps/Save.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.queue.steps; 18 | 19 | import network.aika.neurons.Neuron; 20 | import network.aika.queue.ElementStep; 21 | import network.aika.queue.Phase; 22 | import network.aika.queue.Step; 23 | 24 | /** 25 | * Store model 26 | * 27 | * @author Lukas Molzberger 28 | */ 29 | public class Save extends ElementStep { 30 | 31 | public static void add(Neuron n) { 32 | Step.add(new Save(n)); 33 | } 34 | 35 | private Save(Neuron n) { 36 | super(n); 37 | } 38 | 39 | @Override 40 | public Phase getPhase() { 41 | return Phase.SAVE; 42 | } 43 | 44 | @Override 45 | public void process() { 46 | getElement() 47 | .save(); 48 | } 49 | } -------------------------------------------------------------------------------- /core/src/main/java/network/aika/typedefs/EdgeDefinition.java: -------------------------------------------------------------------------------- 1 | package network.aika.typedefs; 2 | 3 | import network.aika.activations.Link; 4 | import network.aika.type.TypeRegistry; 5 | import network.aika.neurons.Synapse; 6 | 7 | public class EdgeDefinition { 8 | 9 | public final SynapseDefinition synapse; 10 | 11 | public final LinkDefinition link; 12 | 13 | public EdgeDefinition(TypeRegistry registry, String name) { 14 | synapse = new SynapseDefinition(registry, name + "Synapse"); 15 | link = new LinkDefinition(registry, name + "Link"); 16 | 17 | synapse.setLink(link); 18 | link.setSynapse(synapse); 19 | } 20 | 21 | public EdgeDefinition addParent(EdgeDefinition parent) { 22 | synapse.addParent(parent.synapse); 23 | link.addParent(parent.link); 24 | return this; 25 | } 26 | 27 | public EdgeDefinition setInput(NodeDefinition node) { 28 | synapse.setInput(node.neuron); 29 | link.setInput(node.activation); 30 | return this; 31 | } 32 | 33 | public EdgeDefinition setOutput(NodeDefinition node) { 34 | synapse.setOutput(node.neuron); 35 | link.setOutput(node.activation); 36 | return this; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /core/src/main/java/network/aika/typedefs/NeuronDefinition.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.typedefs; 18 | 19 | import network.aika.Model; 20 | import network.aika.activations.Activation; 21 | import network.aika.type.Type; 22 | import network.aika.type.TypeRegistry; 23 | import network.aika.type.relations.Relation; 24 | import network.aika.type.relations.RelationMany; 25 | import network.aika.neurons.Neuron; 26 | import network.aika.neurons.Synapse; 27 | import network.aika.type.relations.RelationSelf; 28 | 29 | import java.util.List; 30 | import java.util.Set; 31 | import java.util.stream.Stream; 32 | 33 | /** 34 | * 35 | * @author Lukas Molzberger 36 | */ 37 | public class NeuronDefinition extends Type { 38 | 39 | public static final RelationSelf SELF = new RelationSelf(0, "NEURON-SELF"); 40 | 41 | public static final RelationMany INPUT = new RelationMany(1, "NEURON-INPUT"); 42 | public static final RelationMany OUTPUT = new RelationMany(2, "NEURON-OUTPUT"); 43 | public static final RelationMany ACTIVATION = new RelationMany(3, "NEURON-ACTIVATION"); 44 | 45 | public static final Relation[] RELATIONS = {SELF, INPUT, OUTPUT, ACTIVATION}; 46 | 47 | static { 48 | ACTIVATION.setReversed(ActivationDefinition.NEURON); 49 | INPUT.setReversed(SynapseDefinition.OUTPUT); 50 | OUTPUT.setReversed(SynapseDefinition.INPUT); 51 | } 52 | 53 | private ActivationDefinition activation; 54 | 55 | public NeuronDefinition(TypeRegistry registry, String name) { 56 | super(registry, name); 57 | } 58 | 59 | @Override 60 | public Relation[] getRelations() { 61 | return RELATIONS; 62 | } 63 | 64 | public Neuron instantiate(Model m) { 65 | return new Neuron(this, m); 66 | } 67 | 68 | public ActivationDefinition getActivation() { 69 | return activation != null ? 70 | activation : 71 | getFromParent(p -> ((NeuronDefinition)p).getActivation()); 72 | } 73 | 74 | public NeuronDefinition setActivation(ActivationDefinition activation) { 75 | this.activation = activation; 76 | 77 | return this; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /core/src/main/java/network/aika/typedefs/NodeDefinition.java: -------------------------------------------------------------------------------- 1 | package network.aika.typedefs; 2 | 3 | import network.aika.activations.Activation; 4 | import network.aika.type.TypeRegistry; 5 | import network.aika.neurons.Neuron; 6 | 7 | public class NodeDefinition { 8 | 9 | public final NeuronDefinition neuron; 10 | 11 | public final ActivationDefinition activation; 12 | 13 | public NodeDefinition(TypeRegistry registry, String name) { 14 | neuron = new NeuronDefinition(registry, name + "Neuron"); 15 | activation = new ActivationDefinition(registry, name + "Activation"); 16 | 17 | neuron.setActivation(activation); 18 | activation.setNeuron(neuron); 19 | } 20 | 21 | public NodeDefinition addParent(NodeDefinition parent) { 22 | neuron.addParent(parent.neuron); 23 | activation.addParent(parent.activation); 24 | return this; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /core/src/test/java/network/aika/activations/AbstractActivationTest.java: -------------------------------------------------------------------------------- 1 | package network.aika.activations; 2 | 3 | import network.aika.Document; 4 | import network.aika.Model; 5 | import network.aika.type.TypeRegistry; 6 | import network.aika.neurons.Neuron; 7 | import network.aika.typedefs.NodeDefinition; 8 | import org.junit.jupiter.api.BeforeEach; 9 | 10 | import static network.aika.neurons.RefType.NEURON_EXTERNAL; 11 | import static org.mockito.Mockito.mock; 12 | 13 | public abstract class AbstractActivationTest { 14 | 15 | TypeRegistry typeRegistry; 16 | NodeDefinition nodeDef; 17 | Model model; 18 | Neuron neuron; 19 | Document doc; 20 | 21 | @BeforeEach 22 | public void init() { 23 | typeRegistry = mock(TypeRegistry.class); 24 | nodeDef = new NodeDefinition(typeRegistry, "test"); 25 | 26 | nodeDef.activation.initFlattenedType(); 27 | nodeDef.neuron.initFlattenedType(); 28 | 29 | doc = mock(Document.class); 30 | model = mock(Model.class); 31 | 32 | neuron = nodeDef.neuron.instantiate(model); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /core/src/test/java/network/aika/activations/ConjunctiveActivationTest.java: -------------------------------------------------------------------------------- 1 | package network.aika.activations; 2 | 3 | 4 | import network.aika.bindingsignal.BindingSignal; 5 | import network.aika.neurons.ConjunctiveSynapse; 6 | import network.aika.neurons.Neuron; 7 | import network.aika.typedefs.EdgeDefinition; 8 | import network.aika.typedefs.NodeDefinition; 9 | import network.aika.typedefs.SynapseDefinition; 10 | import org.junit.jupiter.api.Assertions; 11 | import org.junit.jupiter.api.BeforeEach; 12 | import org.junit.jupiter.api.Test; 13 | import org.junit.jupiter.api.extension.ExtendWith; 14 | import org.mockito.junit.jupiter.MockitoExtension; 15 | 16 | import java.util.Map; 17 | 18 | import static network.aika.activations.TestBSTypes.A; 19 | import static network.aika.activations.TestBSTypes.B; 20 | import static network.aika.activations.TestUtils.getInputLink; 21 | import static network.aika.bindingsignal.Transition.of; 22 | import static network.aika.neurons.RefType.NEURON_EXTERNAL; 23 | 24 | 25 | @ExtendWith(MockitoExtension.class) 26 | public class ConjunctiveActivationTest extends AbstractActivationTest { 27 | 28 | Neuron inputNeuron; 29 | 30 | ConjunctiveSynapse synapse; 31 | 32 | @BeforeEach 33 | @Override 34 | public void init() { 35 | super.init(); 36 | 37 | NodeDefinition inputNodeDef = new NodeDefinition(typeRegistry, "input"); 38 | 39 | EdgeDefinition firstInputEdgeDef = new EdgeDefinition(typeRegistry, "test") 40 | .setInput(inputNodeDef) 41 | .setOutput(nodeDef); 42 | 43 | SynapseDefinition synapseDefinition = firstInputEdgeDef.synapse 44 | .setTransition(of(A, B)); 45 | 46 | inputNodeDef.neuron.initFlattenedType(); 47 | inputNodeDef.activation.initFlattenedType(); 48 | firstInputEdgeDef.synapse.initFlattenedType(); 49 | firstInputEdgeDef.link.initFlattenedType(); 50 | 51 | inputNeuron = inputNodeDef.neuron.instantiate(model); 52 | synapse = (ConjunctiveSynapse) synapseDefinition.instantiate(inputNeuron, neuron); 53 | } 54 | 55 | @Test 56 | public void testLinkIncoming() { 57 | BindingSignal bs0 = new BindingSignal(0, doc); 58 | 59 | Activation iAct = inputNeuron.createActivation(null, doc, Map.of(A, bs0)); 60 | Activation oAct = neuron.createActivation(null, doc, Map.of(B, bs0)); 61 | 62 | Assertions.assertNull(getInputLink(oAct, 0)); 63 | 64 | bs0.addActivation(iAct); 65 | 66 | Assertions.assertNull(getInputLink(oAct, 0)); 67 | 68 | oAct.linkIncoming(null); 69 | 70 | Assertions.assertEquals(iAct, getInputLink(oAct, 0).getInput()); 71 | 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /core/src/test/java/network/aika/activations/MinimalNetworkTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.activations; 18 | 19 | import network.aika.Config; 20 | import network.aika.Document; 21 | import network.aika.Model; 22 | import network.aika.activations.model.TestTypeModel; 23 | import network.aika.fields.defs.FieldDefinition; 24 | import network.aika.neurons.Neuron; 25 | import network.aika.neurons.Synapse; 26 | import network.aika.typedefs.ActivationDefinition; 27 | import network.aika.typedefs.NeuronDefinition; 28 | import network.aika.typedefs.SynapseDefinition; 29 | import org.junit.jupiter.api.Assertions; 30 | import org.junit.jupiter.api.Test; 31 | 32 | import static network.aika.activations.TestBSTypes.A; 33 | import static network.aika.neurons.RefType.NEURON_EXTERNAL; 34 | 35 | /** 36 | * 37 | * @author Lukas Molzberger 38 | */ 39 | public class MinimalNetworkTest { 40 | 41 | 42 | @Test 43 | public void minNetworkTest() { 44 | TestTypeModel typeModel = new TestTypeModel(); 45 | FieldDefinition bias = typeModel.getNeuron().getBias(); 46 | FieldDefinition weight = typeModel.getNeuron().getWeight(); 47 | FieldDefinition net = typeModel.getNeuron().getNet(); 48 | 49 | typeModel.flattenTypeHierarchy(); 50 | 51 | Model m = new Model(typeModel) 52 | .setConfig(new Config()); 53 | 54 | System.out.println("Begin test\n"); 55 | 56 | Neuron inputNeuron = typeModel 57 | .getNeuron() 58 | .getNeuron() 59 | .instantiate(m); 60 | /* 61 | Neuron outputNeuron = typeModel 62 | .getNeuron() 63 | .getNeuron() 64 | .instantiate(m) 65 | .setFieldValue(bias, 1.0); 66 | 67 | Synapse synapse = typeModel 68 | .getNeuron() 69 | .getSynapse() 70 | .instantiate(inputNeuron, outputNeuron) 71 | .setFieldValue(weight, 10.0) 72 | .setPropagable(m, true); 73 | 74 | 75 | Document doc = new Document(m, 4); 76 | Activation iAct = doc.addToken(inputNeuron, A, 0) 77 | .setFieldValue(net, 5.0); 78 | 79 | Assertions.assertEquals(5.0, iAct.getFieldValue(net)); 80 | 81 | doc.process(); 82 | 83 | System.out.println("Dump results:"); 84 | 85 | Activation oAct = doc.getActivationByNeuron(outputNeuron); 86 | 87 | Assertions.assertEquals(10.0, oAct.getFieldValue(net)); 88 | 89 | doc.disconnect();*/ 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /core/src/test/java/network/aika/activations/TestBSTypes.java: -------------------------------------------------------------------------------- 1 | package network.aika.activations; 2 | 3 | import network.aika.bindingsignal.BSType; 4 | 5 | public enum TestBSTypes implements BSType { 6 | A, 7 | B; 8 | } 9 | -------------------------------------------------------------------------------- /core/src/test/java/network/aika/activations/TestUtils.java: -------------------------------------------------------------------------------- 1 | package network.aika.activations; 2 | 3 | public class TestUtils { 4 | 5 | public static Link getInputLink(Activation oAct, int synId) { 6 | return oAct.getInputLinks() 7 | .filter(l -> l.getSynapse().getSynapseId() == synId) 8 | .findAny() 9 | .orElse(null); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /core/src/test/java/network/aika/activations/model/TestActivationFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.activations.model; 18 | 19 | import network.aika.fields.ActivationFunction; 20 | 21 | /** 22 | * 23 | * @author Lukas Molzberger 24 | */ 25 | public enum TestActivationFunction implements ActivationFunction { 26 | 27 | LIMITED_RECTIFIED_LINEAR_UNIT( 28 | x -> Math.max(0.0, Math.min(1.0, x)), 29 | x -> x >= 0.0 && x <= 1.0 ? 1.0 : 0.0 30 | ); 31 | 32 | private final Function f; 33 | private final Function outerGrad; 34 | 35 | TestActivationFunction(Function f, Function outerGrad) { 36 | this.f = f; 37 | this.outerGrad = outerGrad; 38 | } 39 | 40 | public double f(double x) { 41 | return f.f(x); 42 | } 43 | 44 | public double outerGrad(double x) { 45 | return outerGrad.f(x); 46 | } 47 | 48 | interface Function { 49 | double f(double x); 50 | } 51 | } -------------------------------------------------------------------------------- /core/src/test/java/network/aika/activations/model/TestTypeModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.activations.model; 18 | 19 | 20 | import network.aika.Config; 21 | import network.aika.type.TypeRegistryImpl; 22 | 23 | import java.util.stream.Stream; 24 | 25 | 26 | /** 27 | * 28 | * @author Lukas Molzberger 29 | */ 30 | public class TestTypeModel extends TypeRegistryImpl { 31 | 32 | final TestNeuronDef neuron = new TestNeuronDef(this); 33 | 34 | public TestTypeModel() { 35 | neuron.init(); 36 | } 37 | 38 | public TestNeuronDef getNeuron() { 39 | return neuron; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /core/src/test/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | %msg%n 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /debug/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | aika-project 7 | network.aika 8 | ${revision} 9 | 10 | 4.0.0 11 | 12 | aika-debug 13 | ${revision} 14 | 15 | 16 | 17 | network.aika 18 | aika-core 19 | 20 | 21 | 22 | network.aika 23 | aika-fields 24 | 25 | 26 | 27 | commons-io 28 | commons-io 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /debug/src/main/java/network/aika/debug/OutputConfig.java: -------------------------------------------------------------------------------- 1 | package network.aika.debug; 2 | 3 | 4 | public class OutputConfig { 5 | 6 | private boolean showActivationFields = true; 7 | private boolean showLinkFields = true; 8 | private boolean showInputLinks = true; 9 | private boolean showFieldLinkProperties; 10 | 11 | 12 | public boolean isShowActivationFields() { 13 | return showActivationFields; 14 | } 15 | 16 | public OutputConfig setShowActivationFields(boolean showActivationFields) { 17 | this.showActivationFields = showActivationFields; 18 | return this; 19 | } 20 | 21 | public boolean isShowLinkFields() { 22 | return showLinkFields; 23 | } 24 | 25 | public OutputConfig setShowLinkFields(boolean showLinkFields) { 26 | this.showLinkFields = showLinkFields; 27 | return this; 28 | } 29 | 30 | public boolean isShowInputLinks() { 31 | return showInputLinks; 32 | } 33 | 34 | public OutputConfig setShowInputLinks(boolean showInputLinks) { 35 | this.showInputLinks = showInputLinks; 36 | return this; 37 | } 38 | 39 | public boolean isShowFieldLinkProperties() { 40 | return showFieldLinkProperties; 41 | } 42 | 43 | public OutputConfig setShowFieldLinkProperties(boolean showFieldLinkProperties) { 44 | this.showFieldLinkProperties = showFieldLinkProperties; 45 | return this; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /fields/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | network.aika 8 | aika-project 9 | ${revision} 10 | 11 | 12 | aika-fields 13 | 14 | 15 | 16 | org.apache.commons 17 | commons-math3 18 | 19 | 20 | 21 | org.apache.commons 22 | commons-lang3 23 | 24 | 25 | 26 | 27 | org.slf4j 28 | slf4j-api 29 | 30 | 31 | 32 | 33 | org.junit.jupiter 34 | junit-jupiter-engine 35 | test 36 | 37 | 38 | 39 | org.junit.jupiter 40 | junit-jupiter-params 41 | test 42 | 43 | 44 | 45 | org.junit.jupiter 46 | junit-jupiter-api 47 | test 48 | 49 | 50 | 51 | ch.qos.logback 52 | logback-core 53 | test 54 | 55 | 56 | 57 | ch.qos.logback 58 | logback-classic 59 | test 60 | 61 | 62 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/exceptions/TimeoutException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.exceptions; 18 | 19 | /** 20 | * 21 | * @author Lukas Molzberger 22 | */ 23 | public class TimeoutException extends RuntimeException { 24 | } 25 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/AbstractFunctionDefinition.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields; 18 | 19 | import network.aika.fields.defs.FieldLinkDefinitionOutputSide; 20 | import network.aika.fields.defs.FixedArgumentsFieldDefinition; 21 | import network.aika.fields.field.Field; 22 | import network.aika.fields.defs.FieldLinkDefinition; 23 | import network.aika.type.Obj; 24 | import network.aika.type.Type; 25 | 26 | /** 27 | * 28 | * @author Lukas Molzberger 29 | */ 30 | public abstract class AbstractFunctionDefinition extends FixedArgumentsFieldDefinition { 31 | 32 | public AbstractFunctionDefinition(Type objectType, String name, int numArgs) { 33 | super(objectType, name, numArgs); 34 | } 35 | 36 | public AbstractFunctionDefinition(Type objectType, String name, int numArgs, double tolerance) { 37 | super(objectType, name, numArgs, tolerance); 38 | } 39 | 40 | protected abstract double computeUpdate(Obj obj, FieldLinkDefinitionOutputSide fl, double u); 41 | 42 | @Override 43 | public void transmit(Field targetField, FieldLinkDefinitionOutputSide fl, double u) { 44 | double update = computeUpdate(targetField.getObject(), fl, u); 45 | 46 | receiveUpdate(targetField, update); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/ActivationFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields; 18 | 19 | /** 20 | * 21 | * @author Lukas Molzberger 22 | */ 23 | public interface ActivationFunction { 24 | 25 | double f(double x); 26 | 27 | double outerGrad(double x); 28 | } 29 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/Addition.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields; 18 | 19 | import network.aika.fields.defs.FieldLinkDefinitionOutputSide; 20 | import network.aika.type.Type; 21 | import network.aika.type.Obj; 22 | 23 | /** 24 | * @author Lukas Molzberger 25 | */ 26 | public class Addition extends AbstractFunctionDefinition { 27 | 28 | public static Addition add(Type ref, String name) { 29 | return new Addition( 30 | ref, 31 | name 32 | ); 33 | } 34 | 35 | public Addition(Type ref, String name) { 36 | super(ref, name, 2); 37 | } 38 | 39 | @Override 40 | protected double computeUpdate(Obj obj, FieldLinkDefinitionOutputSide fl, double u) { 41 | return u; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/Division.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields; 18 | 19 | import network.aika.fields.defs.FieldLinkDefinitionOutputSide; 20 | import network.aika.fields.field.Field; 21 | import network.aika.type.Obj; 22 | import network.aika.type.Type; 23 | 24 | /** 25 | * @author Lukas Molzberger 26 | */ 27 | public class Division extends AbstractFunctionDefinition { 28 | 29 | public static Division div(Type ref, String name) { 30 | return new Division( 31 | ref, 32 | name 33 | ); 34 | } 35 | 36 | public Division(Type ref, String name) { 37 | super(ref, name, 2); 38 | } 39 | 40 | @Override 41 | public void initializeField(Field field) { 42 | Obj toObj = field.getObject(); 43 | double dividend = getInputValueByArg(toObj, 0); 44 | double divisor = getInputValueByArg(toObj, 1); 45 | 46 | if(divisor == 0.0) 47 | return; 48 | 49 | field.setValue(dividend / divisor); 50 | } 51 | 52 | @Override 53 | protected double computeUpdate(Obj obj, FieldLinkDefinitionOutputSide fl, double u) { 54 | if(fl.getArgument() == 0) { 55 | double divisor = getInputValueByArg(obj, 1); 56 | if(divisor == 0.0) 57 | return 0.0; 58 | 59 | return u / divisor; 60 | } else { 61 | double dividend = getInputValueByArg(obj, 0); 62 | double divisor = getUpdatedInputValueByArg(obj,1); 63 | if(divisor == 0.0) 64 | return 0.0; 65 | 66 | double oldValue = obj.getFieldValue(this); 67 | return (dividend / divisor) - oldValue; 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/EventListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields; 18 | 19 | import network.aika.fields.defs.FieldDefinition; 20 | import network.aika.fields.defs.FixedArgumentsFieldDefinition; 21 | import network.aika.fields.field.Field; 22 | import network.aika.type.Obj; 23 | import network.aika.type.Type; 24 | 25 | import java.util.function.BiConsumer; 26 | 27 | /** 28 | * @author Lukas Molzberger 29 | */ 30 | public class EventListener extends FixedArgumentsFieldDefinition { 31 | 32 | private BiConsumer triggerFunction; 33 | 34 | public static EventListener eventListener(Type ref, String name, BiConsumer triggerFunction, Double tolerance) { 35 | return new EventListener( 36 | ref, 37 | name, 38 | triggerFunction, 39 | tolerance 40 | ); 41 | } 42 | 43 | public EventListener(Type ref, String name, BiConsumer triggerFunction, Double tolerance) { 44 | super(ref, name, 1, tolerance); 45 | 46 | this.triggerFunction = triggerFunction; 47 | } 48 | 49 | @Override 50 | public void receiveUpdate(Field field, double u) { 51 | super.receiveUpdate(field, u); 52 | // Activation act = (Activation) obj; 53 | 54 | //Field field = obj.getFieldOrCreate(this); 55 | //act.updateFiredStep(field); 56 | 57 | triggerFunction.accept(this, field.getObject()); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/ExponentialFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields; 18 | 19 | import network.aika.fields.defs.FieldLinkDefinitionOutputSide; 20 | import network.aika.fields.field.Field; 21 | import network.aika.type.Obj; 22 | import network.aika.type.Type; 23 | 24 | /** 25 | * @author Lukas Molzberger 26 | */ 27 | public class ExponentialFunction extends AbstractFunctionDefinition { 28 | 29 | public static ExponentialFunction exp(Type ref, String name) { 30 | return new ExponentialFunction( 31 | ref, 32 | name 33 | ); 34 | } 35 | 36 | public ExponentialFunction(Type ref, String name) { 37 | super(ref, name, 1); 38 | } 39 | 40 | @Override 41 | public void initializeField(Field field) { 42 | double valueArg0 = getInputValueByArg(field.getObject(), 0); 43 | 44 | field.setValue(Math.exp(valueArg0)); 45 | } 46 | 47 | @Override 48 | protected double computeUpdate(Obj obj, FieldLinkDefinitionOutputSide fl, double u) { 49 | return Math.exp(getUpdatedInputValueByArg(obj, 0)) - obj.getFieldValue(this); 50 | } 51 | } -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/FieldActivationFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields; 18 | 19 | import network.aika.fields.defs.FieldLinkDefinitionOutputSide; 20 | import network.aika.type.Obj; 21 | import network.aika.type.Type; 22 | 23 | /** 24 | * @author Lukas Molzberger 25 | */ 26 | public class FieldActivationFunction extends AbstractFunctionDefinition { 27 | 28 | private ActivationFunction actFunction; 29 | 30 | 31 | public static FieldActivationFunction actFunc(Type ref, String name, ActivationFunction actF, Double tolerance) { 32 | return new FieldActivationFunction( 33 | ref, 34 | name, 35 | actF, 36 | tolerance 37 | ); 38 | } 39 | 40 | public FieldActivationFunction(Type ref, String name, ActivationFunction actFunction, Double tolerance) { 41 | super(ref, name, 1); 42 | 43 | this.tolerance = tolerance; 44 | this.actFunction = actFunction; 45 | } 46 | 47 | @Override 48 | protected double computeUpdate(Obj obj, FieldLinkDefinitionOutputSide fl, double u) { 49 | double value = obj.getOrCreateFieldInput(this).getValue(); 50 | return actFunction.f(fl.getUpdatedInputValue(obj)) - value; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/IdentityFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields; 18 | 19 | import network.aika.fields.defs.FieldLinkDefinitionOutputSide; 20 | import network.aika.type.Type; 21 | import network.aika.type.Obj; 22 | 23 | /** 24 | * @author Lukas Molzberger 25 | */ 26 | public class IdentityFunction extends AbstractFunctionDefinition { 27 | 28 | 29 | public static IdentityFunction identity(Type ref, String name) { 30 | return new IdentityFunction( 31 | ref, 32 | name 33 | ); 34 | } 35 | 36 | public IdentityFunction(Type ref, String name) { 37 | super(ref, name, 1); 38 | } 39 | 40 | @Override 41 | protected double computeUpdate(Obj obj, FieldLinkDefinitionOutputSide fl, double u) { 42 | return u; 43 | } 44 | } -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/InputField.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields; 18 | 19 | import network.aika.fields.defs.FieldLinkDefinitionOutputSide; 20 | import network.aika.type.Type; 21 | import network.aika.type.Obj; 22 | 23 | /** 24 | * @author Lukas Molzberger 25 | */ 26 | public class InputField extends AbstractFunctionDefinition { 27 | 28 | 29 | public static InputField inputField(Type ref, String name) { 30 | return new InputField( 31 | ref, 32 | name 33 | ); 34 | } 35 | 36 | public InputField(Type ref, String name) { 37 | super(ref, name, 0); 38 | } 39 | 40 | @Override 41 | protected double computeUpdate(Obj obj, FieldLinkDefinitionOutputSide fl, double u) { 42 | return 0; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/InvertFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields; 18 | 19 | import network.aika.fields.defs.FieldLinkDefinitionOutputSide; 20 | import network.aika.type.Type; 21 | import network.aika.type.Obj; 22 | 23 | /** 24 | * @author Lukas Molzberger 25 | */ 26 | public class InvertFunction extends AbstractFunctionDefinition { 27 | 28 | public static InvertFunction invert(Type ref, String name) { 29 | return new InvertFunction( 30 | ref, 31 | name 32 | ); 33 | } 34 | 35 | public InvertFunction(Type ref, String name) { 36 | super(ref, name, 1); 37 | 38 | // setInitialValue(1.0); 39 | } 40 | 41 | @Override 42 | protected double computeUpdate(Obj obj, FieldLinkDefinitionOutputSide fl, double u) { 43 | double value = obj.getOrCreateFieldInput(this).getValue(); 44 | return (1.0 - fl.getUpdatedInputValue(obj)) - value; 45 | } 46 | } -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/Multiplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields; 18 | 19 | import network.aika.fields.defs.FieldLinkDefinitionOutputSide; 20 | import network.aika.fields.field.Field; 21 | import network.aika.type.Type; 22 | import network.aika.type.Obj; 23 | 24 | /** 25 | * @author Lukas Molzberger 26 | */ 27 | public class Multiplication extends AbstractFunctionDefinition { 28 | 29 | public static Multiplication mul(Type ref, String name) { 30 | return new Multiplication( 31 | ref, 32 | name 33 | ); 34 | } 35 | 36 | public Multiplication(Type ref, String name) { 37 | super(ref, name, 2); 38 | } 39 | 40 | @Override 41 | public void initializeField(Field field) { 42 | Obj toObj = field.getObject(); 43 | double valueArg0 = getInputValueByArg(toObj, 0); 44 | double valueArg1 = getInputValueByArg(toObj, 1); 45 | 46 | field.setValue(valueArg0 * valueArg1); 47 | } 48 | 49 | @Override 50 | protected double computeUpdate(Obj obj, FieldLinkDefinitionOutputSide fl, double u) { 51 | return u * getInputValueByArg( 52 | obj, 53 | fl.getArgument() == 0 ? 54 | 1 : 55 | 0 56 | ); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/ScaleFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields; 18 | 19 | import network.aika.fields.defs.FieldLinkDefinitionOutputSide; 20 | import network.aika.type.Type; 21 | import network.aika.type.Obj; 22 | 23 | /** 24 | * @author Lukas Molzberger 25 | */ 26 | public class ScaleFunction extends AbstractFunctionDefinition { 27 | 28 | public static ScaleFunction scale(Type ref, String name, double scale) { 29 | return new ScaleFunction( 30 | ref, 31 | name, 32 | scale 33 | ); 34 | } 35 | 36 | private final double scale; 37 | 38 | public ScaleFunction(Type ref, String name, double scale) { 39 | super(ref, name, 1); 40 | 41 | this.scale = scale; 42 | } 43 | 44 | @Override 45 | protected double computeUpdate(Obj obj, FieldLinkDefinitionOutputSide fl, double u) { 46 | return scale * u; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/SoftmaxFields.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields; 18 | 19 | import network.aika.fields.defs.FieldDefinition; 20 | import network.aika.type.Obj; 21 | import network.aika.type.Type; 22 | import network.aika.type.relations.RelationMany; 23 | import network.aika.type.relations.RelationOne; 24 | 25 | import static network.aika.fields.Division.div; 26 | import static network.aika.fields.ExponentialFunction.exp; 27 | import static network.aika.fields.SumField.sum; 28 | 29 | /** 30 | * @author Lukas Molzberger 31 | */ 32 | public class SoftmaxFields { 33 | 34 | public static SoftmaxFields softmax( 35 | Type inputRef, 36 | Type normRef, 37 | Type outputRef, 38 | RelationMany normInputRelation, 39 | RelationOne normOutputRelation, 40 | RelationOne inputRelation, 41 | String name 42 | ) { 43 | return new SoftmaxFields( 44 | inputRef, 45 | normRef, 46 | outputRef, 47 | normInputRelation, 48 | normOutputRelation, 49 | inputRelation, 50 | name); 51 | } 52 | 53 | private final ExponentialFunction inputs; 54 | private final FieldDefinition norm; 55 | private final FieldDefinition outputs; 56 | 57 | 58 | public SoftmaxFields( 59 | Type inputRef, 60 | Type normRef, 61 | Type outputRef, 62 | RelationMany normInputRelation, 63 | RelationOne normOutputRelation, 64 | RelationOne inputRelation, 65 | String name 66 | ) { 67 | inputs = exp(inputRef, name); 68 | norm = sum(normRef, name) 69 | .in(normInputRelation, inputs); 70 | 71 | outputs = div(outputRef, name) 72 | .in(inputRelation, inputs, 0) 73 | .in(normOutputRelation, norm, 1); 74 | } 75 | 76 | public ExponentialFunction getInputs() { 77 | return inputs; 78 | } 79 | 80 | public FieldDefinition getNorm() { 81 | return norm; 82 | } 83 | 84 | public FieldDefinition getOutputs() { 85 | return outputs; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/Subtraction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields; 18 | 19 | import network.aika.fields.defs.FieldLinkDefinitionOutputSide; 20 | import network.aika.type.Type; 21 | import network.aika.type.Obj; 22 | 23 | /** 24 | * @author Lukas Molzberger 25 | */ 26 | public class Subtraction extends AbstractFunctionDefinition { 27 | 28 | public static Subtraction sub(Type ref, String name) { 29 | return new Subtraction( 30 | ref, 31 | name 32 | ); 33 | } 34 | 35 | public Subtraction(Type ref, String name) { 36 | super(ref, name, 2); 37 | } 38 | 39 | @Override 40 | protected double computeUpdate(Obj obj, FieldLinkDefinitionOutputSide fl, double u) { 41 | return fl.getArgument() == 0 ? u : -u; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/SumField.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields; 18 | 19 | import network.aika.fields.defs.VariableArgumentsFieldDefinition; 20 | import network.aika.type.Obj; 21 | import network.aika.type.Type; 22 | 23 | import static network.aika.utils.ToleranceUtils.TOLERANCE; 24 | 25 | /** 26 | * @author Lukas Molzberger 27 | */ 28 | public class SumField extends VariableArgumentsFieldDefinition { 29 | 30 | 31 | public static SumField sum(Type ref, String name) { 32 | return new SumField( 33 | ref, 34 | name, 35 | TOLERANCE 36 | ); 37 | } 38 | 39 | public SumField(Type ref, String name, double tolerance) { 40 | super(ref, name, tolerance); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/ThresholdOperator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields; 18 | 19 | import network.aika.fields.defs.FieldLinkDefinitionOutputSide; 20 | import network.aika.type.Obj; 21 | import network.aika.type.Type; 22 | 23 | 24 | /** 25 | * @author Lukas Molzberger 26 | */ 27 | public class ThresholdOperator extends AbstractFunctionDefinition { 28 | 29 | public static ThresholdOperator threshold(Type ref, String name, double threshold, Comparison type) { 30 | return new ThresholdOperator(ref, name, threshold, type); 31 | } 32 | 33 | public static ThresholdOperator threshold(Type ref, String name, double threshold, Comparison type, boolean isFinal) { 34 | return new ThresholdOperator(ref, name, threshold, type, isFinal); 35 | } 36 | 37 | private double threshold; 38 | private ThresholdOperator.Comparison comparison; 39 | private boolean isFinal; 40 | 41 | public ThresholdOperator(Type ref, String name, double threshold, Comparison type) { 42 | super(ref, name, 1); 43 | this.threshold = threshold; 44 | this.comparison = type; 45 | } 46 | 47 | public ThresholdOperator(Type ref, String name, double threshold, Comparison type, boolean isFinal) { 48 | super(ref, name, 1); 49 | this.threshold = threshold; 50 | this.comparison = type; 51 | this.isFinal = isFinal; 52 | } 53 | 54 | public enum Comparison { 55 | ABOVE, 56 | BELOW, 57 | BELOW_OR_EQUAL, 58 | ABOVE_ABS 59 | } 60 | 61 | @Override 62 | protected double computeUpdate(Obj obj, FieldLinkDefinitionOutputSide fl, double u) { 63 | double value = obj.getOrCreateFieldInput(this).getValue(); 64 | if(isFinal && value > 0.5) 65 | return 0.0; 66 | 67 | return threshold(fl.getUpdatedInputValue(obj)) - value; 68 | } 69 | 70 | protected double threshold(double x) { 71 | return switch (comparison) { 72 | case ABOVE -> x > threshold ? 1.0 : 0.0; 73 | case BELOW -> x < threshold ? 1.0 : 0.0; 74 | case BELOW_OR_EQUAL -> x <= threshold ? 1.0 : 0.0; 75 | case ABOVE_ABS -> Math.abs(x) > threshold ? 1.0 : 0.0; 76 | }; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/defs/FieldLinkDefinition.java: -------------------------------------------------------------------------------- 1 | package network.aika.fields.defs; 2 | 3 | import network.aika.fields.direction.Direction; 4 | import network.aika.fields.field.Field; 5 | import network.aika.type.Type; 6 | import network.aika.type.relations.Relation; 7 | import network.aika.type.Obj; 8 | import network.aika.type.relations.RelationOne; 9 | 10 | 11 | public abstract class FieldLinkDefinition { 12 | 13 | private final FieldDefinition originFD; 14 | private final FieldDefinition relatedFD; 15 | private final Relation relation; 16 | private final Direction direction; 17 | 18 | private final Integer argument; 19 | 20 | public static void link(FieldDefinition input, FieldDefinition output, Relation relation, Integer argument) { 21 | FieldLinkDefinitionOutputSide flo = new FieldLinkDefinitionOutputSide(output, input, relation.getReverse(), Direction.OUTPUT, argument); 22 | FieldLinkDefinitionInputSide fli = new FieldLinkDefinitionInputSide(input, output, relation, Direction.INPUT, argument); 23 | 24 | output.addInput(flo); 25 | input.addOutput(fli); 26 | 27 | flo.setInputSide(fli); 28 | fli.setOutputSide(flo); 29 | } 30 | 31 | public FieldLinkDefinition( 32 | FieldDefinition originFD, 33 | FieldDefinition relatedFD, 34 | Relation relation, 35 | Direction direction, 36 | Integer argument 37 | ) { 38 | this.originFD = originFD; 39 | this.relatedFD = relatedFD; 40 | this.relation = relation; 41 | this.direction = direction; 42 | this.argument = argument; 43 | } 44 | 45 | public FieldLinkDefinition( 46 | FieldDefinition originFD, 47 | FieldDefinition relatedFD, 48 | Relation relation, 49 | Direction direction 50 | ) { 51 | this(originFD, relatedFD, relation, direction, null); 52 | } 53 | 54 | public FieldDefinition getOriginFD() { 55 | return originFD; 56 | } 57 | 58 | public FieldDefinition getRelatedFD() { 59 | return relatedFD; 60 | } 61 | 62 | public Relation getRelation() { 63 | return relation; 64 | } 65 | 66 | public Direction getDirection() { 67 | return direction; 68 | } 69 | 70 | public int getArgument() { 71 | return argument; 72 | } 73 | 74 | @Override 75 | public String toString() { 76 | return originFD + " -- (" + relation + ") -> " + relatedFD; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/defs/FieldLinkDefinitionInputSide.java: -------------------------------------------------------------------------------- 1 | package network.aika.fields.defs; 2 | 3 | import network.aika.fields.direction.Direction; 4 | import network.aika.type.Obj; 5 | import network.aika.type.Type; 6 | import network.aika.type.relations.Relation; 7 | 8 | 9 | public class FieldLinkDefinitionInputSide extends FieldLinkDefinition { 10 | 11 | private FieldLinkDefinitionOutputSide outputSide; 12 | 13 | public FieldLinkDefinitionInputSide(FieldDefinition input, FieldDefinition output, Relation relation, Direction direction, Integer argument) { 14 | super(input, output, relation, direction, argument); 15 | } 16 | 17 | public FieldLinkDefinitionInputSide(FieldDefinition input, FieldDefinition output, Relation relation, Direction direction) { 18 | super(input, output, relation, direction); 19 | } 20 | 21 | public FieldLinkDefinitionOutputSide getOutputSide() { 22 | return outputSide; 23 | } 24 | 25 | public void setOutputSide(FieldLinkDefinitionOutputSide outputSide) { 26 | this.outputSide = outputSide; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/defs/FieldLinkDefinitionOutputSide.java: -------------------------------------------------------------------------------- 1 | package network.aika.fields.defs; 2 | 3 | import network.aika.fields.direction.Direction; 4 | import network.aika.fields.field.Field; 5 | import network.aika.type.Obj; 6 | import network.aika.type.relations.Relation; 7 | import network.aika.type.relations.RelationOne; 8 | 9 | public class FieldLinkDefinitionOutputSide extends FieldLinkDefinition { 10 | 11 | private FieldLinkDefinitionInputSide inputSide; 12 | 13 | public FieldLinkDefinitionOutputSide(FieldDefinition output, FieldDefinition input, Relation relation, Direction direction, Integer argument) { 14 | super(output, input, relation, direction, argument); 15 | } 16 | 17 | public FieldLinkDefinitionOutputSide(FieldDefinition output, FieldDefinition input, Relation relation, Direction direction) { 18 | super(output, input, relation, direction); 19 | } 20 | 21 | public Field getInputField(Obj obj) { 22 | var rt = (RelationOne) getRelation(); 23 | Obj inputObj = rt.followOne(obj); 24 | 25 | return inputObj.getFieldOutput(getRelatedFD()); 26 | } 27 | 28 | public double getInputValue(Obj obj) { 29 | Field f = getInputField(obj); 30 | return f != null ? 31 | f.getValue() : 32 | 0.0; 33 | } 34 | 35 | public double getUpdatedInputValue(Obj obj) { 36 | Field f = getInputField(obj); 37 | 38 | return f != null ? 39 | f.getUpdatedValue() : 40 | 0.0; 41 | } 42 | 43 | public FieldLinkDefinitionInputSide getInputSide() { 44 | return inputSide; 45 | } 46 | 47 | public void setInputSide(FieldLinkDefinitionInputSide inputSide) { 48 | this.inputSide = inputSide; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/defs/FixedArgumentsFieldDefinition.java: -------------------------------------------------------------------------------- 1 | package network.aika.fields.defs; 2 | 3 | /* 4 | * Licensed to the Apache Software Foundation (ASF) under one or more 5 | * contributor license agreements. See the NOTICE file distributed with 6 | * this work for additional information regarding copyright ownership. 7 | * The ASF licenses this file to You under the Apache License, Version 2.0 8 | * (the "License"); you may not use this file except in compliance with 9 | * the License. You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | import network.aika.fields.direction.Direction; 20 | import network.aika.type.relations.RelationOne; 21 | import network.aika.type.Obj; 22 | import network.aika.type.Type; 23 | 24 | import java.util.Objects; 25 | import java.util.stream.Stream; 26 | 27 | import static network.aika.fields.defs.FieldLinkDefinition.link; 28 | 29 | /** 30 | * 31 | * @author Lukas Molzberger 32 | */ 33 | public abstract class FixedArgumentsFieldDefinition extends FieldDefinition { 34 | 35 | private final FieldLinkDefinitionOutputSide[] inputs; 36 | 37 | public FixedArgumentsFieldDefinition(Type objectType, String name, int numArgs) { 38 | super(objectType, name); 39 | 40 | inputs = new FieldLinkDefinitionOutputSide[numArgs]; 41 | } 42 | 43 | public FixedArgumentsFieldDefinition(Type objectType, String name, int numArgs, double tolerance) { 44 | super(objectType, name, tolerance); 45 | 46 | inputs = new FieldLinkDefinitionOutputSide[numArgs]; 47 | } 48 | 49 | @Override 50 | public Stream getInputs() { 51 | return Stream.of(inputs) 52 | .filter(Objects::nonNull); 53 | } 54 | 55 | @Override 56 | public void addInput(FieldLinkDefinitionOutputSide fl) { 57 | inputs[fl.getArgument()] = fl; 58 | } 59 | 60 | public double getInputValueByArg(Obj obj, int arg) { 61 | var fl = inputs[arg]; 62 | return fl.getInputValue(obj); 63 | } 64 | 65 | public double getUpdatedInputValueByArg(Obj obj, int arg) { 66 | var fl = inputs[arg]; 67 | return fl.getUpdatedInputValue(obj); 68 | } 69 | 70 | public FixedArgumentsFieldDefinition in(RelationOne relation, FieldDefinition input, int arg) { 71 | link(input, this, relation.getReverse(), arg); 72 | 73 | assert relation != null || objectType.isInstanceOf(input.objectType) || input.objectType.isInstanceOf(objectType); 74 | 75 | return this; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/defs/VariableArgumentsFieldDefinition.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields.defs; 18 | 19 | import network.aika.fields.direction.Direction; 20 | import network.aika.type.Obj; 21 | import network.aika.type.relations.Relation; 22 | import network.aika.type.Type; 23 | 24 | import java.util.ArrayList; 25 | import java.util.List; 26 | import java.util.stream.Stream; 27 | 28 | import static network.aika.fields.defs.FieldLinkDefinition.link; 29 | 30 | /** 31 | * 32 | * @author Lukas Molzberger 33 | */ 34 | public class VariableArgumentsFieldDefinition extends FieldDefinition { 35 | 36 | protected List inputs = new ArrayList<>(); 37 | 38 | public VariableArgumentsFieldDefinition(Type objectType, String name) { 39 | super(objectType, name); 40 | } 41 | 42 | public VariableArgumentsFieldDefinition(Type objectType, String name, double tolerance) { 43 | super(objectType, name, tolerance); 44 | } 45 | 46 | public VariableArgumentsFieldDefinition in(Relation relation, FieldDefinition input) { 47 | 48 | link(input, this, relation.getReverse(), null); 49 | 50 | return this; 51 | } 52 | 53 | @Override 54 | public Stream getInputs() { 55 | return inputs.stream(); 56 | } 57 | 58 | public int size() { 59 | return inputs.size(); 60 | } 61 | 62 | public void addInput(FieldLinkDefinitionOutputSide fl) { 63 | inputs.add(fl); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/direction/Direction.java: -------------------------------------------------------------------------------- 1 | package network.aika.fields.direction; 2 | 3 | import network.aika.fields.defs.FieldDefinition; 4 | import network.aika.fields.field.Field; 5 | import network.aika.fields.defs.FieldLinkDefinition; 6 | import network.aika.type.FlattenedType; 7 | import network.aika.type.Obj; 8 | import network.aika.type.Type; 9 | import network.aika.type.relations.Relation; 10 | 11 | import java.util.stream.Stream; 12 | 13 | 14 | public interface Direction { 15 | 16 | Direction INPUT = new Input(); 17 | Direction OUTPUT = new Output(); 18 | 19 | int getDirectionId(); 20 | 21 | Direction invert(); 22 | 23 | Stream getFieldLinkDefinitions(FieldDefinition fd); 24 | 25 | FlattenedType getFlattenedType(Type type); 26 | 27 | void transmit(Field originField, FieldLinkDefinition fl, Obj relatedObject); 28 | } 29 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/direction/Input.java: -------------------------------------------------------------------------------- 1 | package network.aika.fields.direction; 2 | 3 | import network.aika.fields.defs.FieldDefinition; 4 | import network.aika.fields.defs.FieldLinkDefinitionOutputSide; 5 | import network.aika.fields.field.Field; 6 | import network.aika.fields.defs.FieldLinkDefinition; 7 | import network.aika.type.FlattenedType; 8 | import network.aika.type.Obj; 9 | import network.aika.type.Type; 10 | import network.aika.type.relations.Relation; 11 | 12 | import java.util.stream.Stream; 13 | 14 | public class Input implements Direction { 15 | 16 | @Override 17 | public int getDirectionId() { 18 | return 0; 19 | } 20 | 21 | @Override 22 | public Direction invert() { 23 | return Direction.OUTPUT; 24 | } 25 | 26 | @Override 27 | public Stream getFieldLinkDefinitions(FieldDefinition fd) { 28 | return fd.getInputs(); 29 | } 30 | 31 | public FlattenedType getFlattenedType(Type type) { 32 | return type.getFlattenedTypeInputSide(); 33 | } 34 | 35 | @Override 36 | public void transmit(Field originField, FieldLinkDefinition fl, Obj relatedObject) { 37 | double inputFieldValue = relatedObject.getFieldValue(fl.getRelatedFD()); 38 | fl.getOriginFD().transmit(originField, (FieldLinkDefinitionOutputSide) fl, inputFieldValue); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/direction/Output.java: -------------------------------------------------------------------------------- 1 | package network.aika.fields.direction; 2 | 3 | import network.aika.fields.defs.FieldDefinition; 4 | import network.aika.fields.defs.FieldLinkDefinitionInputSide; 5 | import network.aika.fields.defs.FieldLinkDefinitionOutputSide; 6 | import network.aika.fields.field.Field; 7 | import network.aika.fields.defs.FieldLinkDefinition; 8 | import network.aika.type.FlattenedType; 9 | import network.aika.type.Obj; 10 | import network.aika.type.Type; 11 | import network.aika.type.relations.Relation; 12 | 13 | import java.util.stream.Stream; 14 | 15 | 16 | public class Output implements Direction { 17 | 18 | @Override 19 | public int getDirectionId() { 20 | return 1; 21 | } 22 | 23 | @Override 24 | public Direction invert() { 25 | return Direction.INPUT; 26 | } 27 | 28 | @Override 29 | public Stream getFieldLinkDefinitions(FieldDefinition fd) { 30 | return fd.getOutputs(); 31 | } 32 | 33 | public FlattenedType getFlattenedType(Type type) { 34 | return type.getFlattenedTypeOutputSide(); 35 | } 36 | 37 | @Override 38 | public void transmit(Field originField, FieldLinkDefinition fl, Obj relatedObject) { 39 | FieldLinkDefinitionOutputSide flo = ((FieldLinkDefinitionInputSide)fl).getOutputSide(); 40 | fl.getRelatedFD().transmit( 41 | relatedObject.getOrCreateFieldInput(fl.getRelatedFD()), 42 | flo, 43 | originField.getUpdate() 44 | ); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/field/FieldInput.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields.field; 18 | 19 | /** 20 | * 21 | * @author Lukas Molzberger 22 | */ 23 | public interface FieldInput extends UpdateListener { 24 | } 25 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/field/FieldOutput.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields.field; 18 | 19 | import network.aika.fields.defs.FieldDefinition; 20 | import network.aika.type.Obj; 21 | 22 | 23 | /** 24 | * @author Lukas Molzberger 25 | */ 26 | public interface FieldOutput { 27 | 28 | String getName(); 29 | 30 | FieldDefinition getFieldDefinition(); 31 | 32 | Obj getObject(); 33 | 34 | String getValueString(); 35 | 36 | double getValue(); 37 | 38 | double getUpdatedValue(); 39 | 40 | boolean isWithinUpdate(); 41 | 42 | default boolean exceedsThreshold() { 43 | return getUpdatedValue() > 0.0; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/field/QueueInterceptor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields.field; 18 | 19 | import network.aika.queue.ProcessingPhase; 20 | import network.aika.queue.Queue; 21 | import network.aika.queue.steps.FieldUpdate; 22 | import network.aika.queue.Step; 23 | 24 | /** 25 | * 26 | * @author Lukas Molzberger 27 | */ 28 | public class QueueInterceptor { 29 | 30 | private ProcessingPhase phase; 31 | 32 | private FieldUpdate step; 33 | 34 | private Field field; 35 | 36 | private Queue queue; 37 | 38 | private boolean isNextRound; 39 | 40 | public QueueInterceptor(Queue q, Field f, ProcessingPhase phase, boolean isNextRound) { 41 | this.queue = q; 42 | this.field = f; 43 | this.phase = phase; 44 | this.isNextRound = isNextRound; 45 | } 46 | 47 | public FieldUpdate getStep() { 48 | return step; 49 | } 50 | 51 | public Field getField() { 52 | return field; 53 | } 54 | 55 | public boolean isNextRound() { 56 | return isNextRound; 57 | } 58 | 59 | private FieldUpdate getOrCreateStep() { 60 | if(step == null) 61 | step = new FieldUpdate<>(phase, this); 62 | 63 | return step; 64 | } 65 | 66 | public void receiveUpdate(double u, boolean replaceUpdate) { 67 | FieldUpdate s = getOrCreateStep(); 68 | s.updateDelta(u, replaceUpdate); 69 | 70 | if(u != 0.0 && !s.isQueued()) { 71 | if(!Step.add(s)) { 72 | process(s); 73 | } 74 | } 75 | } 76 | 77 | public void process(FieldUpdate s) { 78 | step = null; 79 | field.triggerUpdate(s.getDelta()); 80 | } 81 | 82 | public Queue getQueue() { 83 | return queue; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/field/UpdateListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields.field; 18 | 19 | /** 20 | * @author Lukas Molzberger 21 | */ 22 | public interface UpdateListener { 23 | 24 | void receiveUpdate(double u); 25 | } 26 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/sign/Negative.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields.sign; 18 | 19 | 20 | /** 21 | * 22 | * @author Lukas Molzberger 23 | */ 24 | public class Negative implements Sign { 25 | 26 | @Override 27 | public Sign invert() { 28 | return POS; 29 | } 30 | 31 | @Override 32 | public int index() { 33 | return 1; 34 | } 35 | 36 | public String toString() { 37 | return "NEG"; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/sign/Positive.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields.sign; 18 | 19 | /** 20 | * 21 | * @author Lukas Molzberger 22 | */ 23 | public class Positive implements Sign { 24 | @Override 25 | public Sign invert() { 26 | return NEG; 27 | } 28 | 29 | 30 | @Override 31 | public int index() { 32 | return 0; 33 | } 34 | 35 | public String toString() { 36 | return "POS"; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/fields/sign/Sign.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields.sign; 18 | 19 | /** 20 | * 21 | * @author Lukas Molzberger 22 | */ 23 | public interface Sign { 24 | 25 | Positive POS = new Positive(); 26 | Negative NEG = new Negative(); 27 | 28 | Sign[] SIGNS = new Sign[] {POS, NEG}; 29 | 30 | Sign invert(); 31 | 32 | static Sign getSign(double x) { 33 | return x >= 0.0 ? POS : NEG; 34 | } 35 | 36 | int index(); 37 | } 38 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/queue/ProcessingPhase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.queue; 18 | 19 | 20 | /** 21 | * 22 | * @author Lukas Molzberger 23 | */ 24 | public interface ProcessingPhase { 25 | 26 | int rank(); 27 | 28 | boolean isDelayed(); 29 | } 30 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/queue/QueueProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.queue; 18 | 19 | /** 20 | * 21 | * @author Lukas Molzberger 22 | */ 23 | public interface QueueProvider { 24 | 25 | Queue getQueue(); 26 | 27 | } 28 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/queue/Step.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.queue; 18 | 19 | import network.aika.queue.keys.QueueKey; 20 | 21 | 22 | /** 23 | * @author Lukas Molzberger 24 | */ 25 | public abstract class Step { 26 | 27 | protected boolean isQueued; 28 | protected QueueKey queueKey; 29 | 30 | public void setQueued(boolean queued) { 31 | isQueued = queued; 32 | } 33 | 34 | public boolean isQueued() { 35 | return isQueued; 36 | } 37 | 38 | public QueueKey getQueueKey() { 39 | return queueKey; 40 | } 41 | 42 | public abstract Queue getQueue(); 43 | 44 | public boolean incrementRound() { 45 | return false; 46 | } 47 | 48 | public abstract void createQueueKey(Timestamp timestamp, int round); 49 | 50 | public abstract void process(); 51 | 52 | public abstract ProcessingPhase getPhase(); 53 | 54 | public static boolean add(Step s) { 55 | Queue q = s.getQueue(); 56 | if(q == null) 57 | return false; 58 | 59 | q.addStep(s); 60 | return true; 61 | } 62 | 63 | public abstract E getElement(); 64 | } 65 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/queue/Timestamp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.queue; 18 | 19 | /** 20 | * 21 | * @author Lukas Molzberger 22 | */ 23 | public class Timestamp implements Comparable { 24 | 25 | public static Timestamp MIN = new Timestamp(0); 26 | public static Timestamp MAX = new Timestamp(Long.MAX_VALUE); 27 | public static Timestamp NOT_SET = new Timestamp(Long.MAX_VALUE); 28 | 29 | private long timestamp; 30 | 31 | public Timestamp(long ts) { 32 | this.timestamp = ts; 33 | } 34 | 35 | public long getTimestamp() { 36 | return timestamp; 37 | } 38 | 39 | public String toString() { 40 | if(this == NOT_SET) 41 | return "NOT_SET"; 42 | 43 | if(this == MIN) 44 | return "MIN"; 45 | 46 | if(this == MAX) 47 | return "MAX"; 48 | 49 | return "" + timestamp; 50 | } 51 | 52 | @Override 53 | public int compareTo(Timestamp ts) { 54 | return Long.compare(timestamp, ts.timestamp); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/queue/keys/FieldQueueKey.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.queue.keys; 18 | 19 | import network.aika.queue.ProcessingPhase; 20 | import network.aika.queue.Timestamp; 21 | 22 | import java.util.Comparator; 23 | 24 | 25 | /** 26 | * @author Lukas Molzberger 27 | */ 28 | public class FieldQueueKey extends QueueKey { 29 | 30 | Comparator COMPARATOR = Comparator 31 | .comparingInt(k -> -k.sortValue); 32 | 33 | private final int sortValue; 34 | 35 | 36 | public FieldQueueKey(int round, ProcessingPhase phase, int sortValue, Timestamp currentTimestamp) { 37 | super(round, phase, currentTimestamp); 38 | this.sortValue = sortValue; 39 | } 40 | 41 | public int getSortValue() { 42 | return sortValue; 43 | } 44 | 45 | private String getSortValueAsString() { 46 | return getSortValue() == Integer.MAX_VALUE ? 47 | "MAX" : 48 | "" + getSortValue(); 49 | } 50 | 51 | @Override 52 | public int compareTo(QueueKey qk) { 53 | return COMPARATOR.compare(this, (FieldQueueKey) qk); 54 | } 55 | 56 | @Override 57 | public String toString() { 58 | return "[r:" + getRoundStr() + 59 | ",p:" + getPhaseStr() + 60 | ",sv:" + getSortValueAsString() + 61 | ",ts:" + getCurrentTimestamp() + 62 | "]"; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/queue/keys/QueueKey.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.queue.keys; 18 | 19 | 20 | import network.aika.queue.ProcessingPhase; 21 | import network.aika.queue.Timestamp; 22 | import network.aika.utils.StringUtils; 23 | 24 | import java.util.Comparator; 25 | import java.util.function.Function; 26 | 27 | /** 28 | * @author Lukas Molzberger 29 | */ 30 | public abstract class QueueKey implements Comparable { 31 | 32 | public static final int MAX_ROUND = Integer.MAX_VALUE; 33 | 34 | public static final Comparator COMPARATOR = Comparator 35 | .comparingInt(k -> k.round) 36 | .thenComparingInt(k -> k.getPhase().rank()) 37 | .thenComparing(Function.identity()) 38 | .thenComparing(QueueKey::getCurrentTimestamp); 39 | 40 | private final int round; 41 | 42 | private final ProcessingPhase phase; 43 | 44 | private final Timestamp currentTimestamp; 45 | 46 | public QueueKey(int round, ProcessingPhase phase, Timestamp currentTimestamp) { 47 | this.round = round; 48 | this.phase = phase; 49 | this.currentTimestamp = currentTimestamp; 50 | } 51 | 52 | public int getRound() { 53 | return round; 54 | } 55 | 56 | protected String getRoundStr() { 57 | return StringUtils.roundToString(getRound()); 58 | } 59 | 60 | public ProcessingPhase getPhase() { 61 | return phase; 62 | } 63 | 64 | protected String getPhaseStr() { 65 | return getPhase() + "-" + getPhase(); 66 | } 67 | 68 | public Timestamp getCurrentTimestamp() { 69 | return currentTimestamp; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/type/FlattenedTypeRelation.java: -------------------------------------------------------------------------------- 1 | package network.aika.type; 2 | 3 | import network.aika.fields.defs.FieldDefinition; 4 | import network.aika.fields.direction.Direction; 5 | import network.aika.fields.field.Field; 6 | import network.aika.fields.defs.FieldLinkDefinition; 7 | 8 | import java.util.List; 9 | import java.util.Map; 10 | import java.util.stream.Collectors; 11 | 12 | public class FlattenedTypeRelation { 13 | 14 | FieldLinkDefinition[][] fieldLinks; 15 | 16 | public FlattenedTypeRelation(FlattenedType flattenedType, List fls) { 17 | Map> groupedByOriginFD = 18 | fls.stream() 19 | .collect(Collectors.groupingBy(fl -> 20 | fl.getOriginFD().getId()) 21 | ); 22 | 23 | fieldLinks = new FieldLinkDefinition[flattenedType.getFieldsReverse().length][]; 24 | for(short i = 0; i < fieldLinks.length; i++) { 25 | for(FieldDefinition fd : flattenedType.getFieldsReverse()[i]) { 26 | List list = groupedByOriginFD.get(fd.getId()); 27 | if (list != null) { 28 | fieldLinks[i] = list.toArray(new FieldLinkDefinition[0]); 29 | } 30 | } 31 | } 32 | } 33 | 34 | public void followLinks(Direction direction, Obj relatedObj, Field field) { 35 | FieldLinkDefinition[] fls = fieldLinks[field.getId()]; 36 | if(fls != null) { 37 | for (FieldLinkDefinition fl : fls) { 38 | direction.transmit( 39 | field, 40 | fl, 41 | relatedObj 42 | ); 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/type/Obj.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package network.aika.type; 19 | 20 | import network.aika.fields.defs.FieldDefinition; 21 | import network.aika.fields.field.Field; 22 | import network.aika.queue.Queue; 23 | import network.aika.type.relations.Relation; 24 | 25 | import java.util.stream.Stream; 26 | 27 | /** 28 | * 29 | * @author Lukas Molzberger 30 | */ 31 | public interface Obj { 32 | 33 | void initFields(); 34 | 35 | Type getType(); 36 | 37 | Stream followManyRelation(Relation rel); 38 | 39 | Obj followSingleRelation(Relation rel); 40 | 41 | Field getOrCreateFieldInput(FieldDefinition fd); 42 | 43 | Field getFieldOutput(FieldDefinition fd); 44 | 45 | Stream getFields(); 46 | 47 | Obj setFieldValue(FieldDefinition fd, double v); 48 | 49 | double getFieldValue(FieldDefinition fd); 50 | 51 | double getFieldUpdatedValue(FieldDefinition fd); 52 | 53 | Queue getQueue(); 54 | 55 | String toKeyString(); 56 | 57 | boolean isInstanceOf(Type objectType); 58 | } 59 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/type/TypeRegistry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.type; 18 | 19 | import java.util.List; 20 | 21 | /** 22 | * 23 | * @author Lukas Molzberger 24 | */ 25 | public interface TypeRegistry { 26 | 27 | short register(Type type); 28 | 29 | List getTypes(); 30 | 31 | int createFieldId(); 32 | 33 | Type getType(short typeId); 34 | 35 | int getNumberOfFieldDefinitions(); 36 | 37 | void flattenTypeHierarchy(); 38 | } 39 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/type/TypeRegistryImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.type; 18 | 19 | import java.util.ArrayList; 20 | import java.util.List; 21 | import java.util.TreeSet; 22 | 23 | import static network.aika.type.Type.TYPE_COMPARATOR; 24 | 25 | /** 26 | * 27 | * @author Lukas Molzberger 28 | */ 29 | public class TypeRegistryImpl implements TypeRegistry { 30 | 31 | private List types = new ArrayList<>(); 32 | 33 | private int fieldIdCounter = 0; 34 | 35 | @Override 36 | public short register(Type type) { 37 | short id = (short) types.size(); 38 | types.add(type); 39 | return id; 40 | } 41 | 42 | @Override 43 | public Type getType(short typeId) { 44 | return types.get(typeId); 45 | } 46 | 47 | @Override 48 | public List getTypes() { 49 | return types; 50 | } 51 | 52 | @Override 53 | public int createFieldId() { 54 | return fieldIdCounter++; 55 | } 56 | 57 | @Override 58 | public int getNumberOfFieldDefinitions() { 59 | return fieldIdCounter; 60 | } 61 | 62 | @Override 63 | public void flattenTypeHierarchy() { 64 | TreeSet sortedTypes = new TreeSet<>(TYPE_COMPARATOR); 65 | 66 | sortedTypes.addAll(types); 67 | 68 | sortedTypes 69 | .forEach(Type::initFlattenedType); 70 | 71 | sortedTypes 72 | .forEach(t -> { 73 | t.getFlattenedTypeInputSide().flatten(); 74 | t.getFlattenedTypeOutputSide().flatten(); 75 | }); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/type/relations/AbstractRelation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.type.relations; 18 | 19 | import network.aika.type.Type; 20 | import network.aika.type.Obj; 21 | 22 | /** 23 | * 24 | * @author Lukas Molzberger 25 | */ 26 | public abstract class AbstractRelation implements Relation { 27 | 28 | private int relationId; 29 | protected String relationName; 30 | 31 | private Relation reversed; 32 | 33 | public AbstractRelation(int relationId, String relationName) { 34 | this.relationId = relationId; 35 | this.relationName = relationName; 36 | } 37 | 38 | @Override 39 | public int getRelationId() { 40 | return relationId; 41 | } 42 | 43 | @Override 44 | public void setReversed(Relation reversed) { 45 | this.reversed = reversed; 46 | } 47 | 48 | public Relation getReverse() { 49 | return reversed; 50 | } 51 | 52 | @Override 53 | public String toString() { 54 | return getRelationLabel() + " -> " + getReverse().getRelationLabel(); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/type/relations/Relation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.type.relations; 18 | 19 | import network.aika.type.Obj; 20 | 21 | import java.util.stream.Stream; 22 | 23 | /** 24 | * 25 | * @author Lukas Molzberger 26 | */ 27 | public interface Relation { 28 | 29 | int getRelationId(); 30 | 31 | void setReversed(Relation reversed); 32 | 33 | Relation getReverse(); 34 | 35 | Stream followMany(Obj fromObj); 36 | 37 | boolean testRelation(Obj fromObj, Obj toObj); 38 | 39 | String getRelationLabel(); 40 | } 41 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/type/relations/RelationMany.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.type.relations; 18 | 19 | import network.aika.type.Type; 20 | import network.aika.type.Obj; 21 | 22 | import java.util.function.Function; 23 | import java.util.stream.Stream; 24 | 25 | /** 26 | * 27 | * @author Lukas Molzberger 28 | */ 29 | public class RelationMany extends AbstractRelation { 30 | 31 | public RelationMany(int relationId, String relationName) { 32 | super(relationId, relationName); 33 | } 34 | 35 | public Stream followMany(Obj fromObj) { 36 | return fromObj.followManyRelation(this); 37 | } 38 | 39 | @Override 40 | public boolean testRelation(Obj fromObj, Obj toObj) { 41 | return getReverse().testRelation(toObj, fromObj); 42 | } 43 | 44 | @Override 45 | public String getRelationLabel() { 46 | return relationName + " (Many)"; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/type/relations/RelationOne.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.type.relations; 18 | 19 | import network.aika.type.Type; 20 | import network.aika.type.Obj; 21 | 22 | import java.util.function.Function; 23 | import java.util.stream.Stream; 24 | 25 | /** 26 | * 27 | * @author Lukas Molzberger 28 | */ 29 | public class RelationOne extends AbstractRelation { 30 | 31 | public RelationOne(int relationId, String relationName) { 32 | super(relationId, relationName); 33 | } 34 | 35 | public Obj followOne(Obj fromObj) { 36 | return fromObj.followSingleRelation(this); 37 | } 38 | 39 | @Override 40 | public Stream followMany(Obj fromObj) { 41 | Obj toObj = followOne(fromObj); 42 | return toObj != null ? 43 | Stream.of(toObj) : 44 | Stream.empty(); 45 | } 46 | 47 | @Override 48 | public boolean testRelation(Obj fromObj, Obj toObj) { 49 | return followOne(fromObj) == toObj; 50 | } 51 | 52 | @Override 53 | public String getRelationLabel() { 54 | return relationName + " (One)"; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/type/relations/RelationSelf.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.type.relations; 18 | 19 | import network.aika.type.Obj; 20 | import network.aika.type.Type; 21 | 22 | import java.util.stream.Stream; 23 | 24 | /** 25 | * 26 | * @author Lukas Molzberger 27 | */ 28 | public class RelationSelf extends RelationOne { 29 | 30 | public RelationSelf(int relationId, String relationName) { 31 | super(relationId, relationName); 32 | } 33 | 34 | @Override 35 | public void setReversed(Relation reversed) { 36 | throw new UnsupportedOperationException(); 37 | } 38 | 39 | @Override 40 | public Relation getReverse() { 41 | return this; 42 | } 43 | 44 | @Override 45 | public Obj followOne(Obj fromObj) { 46 | return fromObj; 47 | } 48 | 49 | @Override 50 | public Stream followMany(Obj fromObj) { 51 | return Stream.of(fromObj); 52 | } 53 | 54 | @Override 55 | public boolean testRelation(Obj fromObj, Obj toObj) { 56 | return fromObj == toObj; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/utils/ApproximateComparisonValueUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.utils; 18 | 19 | 20 | /** 21 | * @author Lukas Molzberger 22 | * 23 | */ 24 | public class ApproximateComparisonValueUtil { 25 | 26 | public static final double PRECISION = 1000.0; 27 | 28 | public static int convert(double newSortValue) { 29 | return (int) (PRECISION * newSortValue); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/utils/ArrayUtils.java: -------------------------------------------------------------------------------- 1 | package network.aika.utils; 2 | 3 | public class ArrayUtils { 4 | 5 | public static boolean isAllNull(Object[] array) { 6 | for (Object element : array) { 7 | if (element != null) { 8 | return false; // As soon as a non-null element is found, return false 9 | } 10 | } 11 | return true; // If no non-null element is found, return true 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/utils/FieldWritable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.utils; 18 | 19 | import java.io.DataInput; 20 | import java.io.DataOutput; 21 | import java.io.IOException; 22 | 23 | /** 24 | * 25 | * @author Lukas Molzberger 26 | */ 27 | public interface FieldWritable { 28 | 29 | /** 30 | * Serialize the fields of this object to out. 31 | * 32 | * @param out DataOuput to serialize this object into. 33 | * @throws IOException 34 | */ 35 | void write(DataOutput out) throws IOException; 36 | 37 | /** 38 | * Deserialize the fields of this object from in. 39 | * 40 | *

For efficiency, implementations should attempt to re-use storage in the 41 | * existing object where possible.

42 | * 43 | * @param in DataInput to deseriablize this object from. 44 | * @throws Exception 45 | */ 46 | void readFields(DataInput in) throws Exception; 47 | 48 | } -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/utils/StringUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.utils; 18 | 19 | import java.math.RoundingMode; 20 | import java.text.DecimalFormat; 21 | import java.text.DecimalFormatSymbols; 22 | import java.util.Locale; 23 | 24 | import static network.aika.queue.keys.QueueKey.MAX_ROUND; 25 | 26 | /** 27 | * 28 | * @author Lukas Molzberger 29 | */ 30 | public class StringUtils { 31 | 32 | public static double TOLERANCE = 0.001; 33 | 34 | 35 | public static String doubleToString(Double x) { 36 | if(x == null) 37 | return "--"; 38 | return doubleToString(x, "#.######"); 39 | } 40 | 41 | public static String floatToString(Float d, String format) { 42 | if(d == null) 43 | return "--"; 44 | DecimalFormat formatter = new DecimalFormat(format, DecimalFormatSymbols.getInstance(Locale.ENGLISH)); 45 | formatter.setRoundingMode( RoundingMode.DOWN ); 46 | return formatter.format(d); 47 | } 48 | 49 | public static String doubleToString(Double d, String format) { 50 | if(d == null) 51 | return "--"; 52 | DecimalFormat formatter = new DecimalFormat(format, DecimalFormatSymbols.getInstance(Locale.ENGLISH)); 53 | formatter.setRoundingMode( RoundingMode.DOWN ); 54 | return formatter.format(d); 55 | } 56 | 57 | public static String roundToString(int r) { 58 | return r == MAX_ROUND ? "MAX" : "" + r; 59 | } 60 | 61 | public static String depthToSpace(int depth) { 62 | StringBuilder sb = new StringBuilder(); 63 | for(int i = 0; i < depth; i++) 64 | sb.append(" "); 65 | return sb.toString(); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/utils/ToleranceUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.utils; 18 | 19 | /** 20 | * 21 | * @author Lukas Molzberger 22 | */ 23 | public class ToleranceUtils { 24 | 25 | public static double TOLERANCE = 0.001; 26 | 27 | 28 | public static double sum(double[] a) { 29 | double sum = 0; 30 | for(int i = 0; i < a.length; i++) 31 | sum += a[i]; 32 | return sum; 33 | } 34 | 35 | public static boolean belowTolerance(Double tolerance, double[] x) { 36 | if(x == null) 37 | return true; 38 | 39 | if(tolerance == null) 40 | return false; 41 | 42 | return Math.abs(sum(x)) < tolerance; 43 | } 44 | 45 | public static boolean belowTolerance(Double tolerance, double x) { 46 | if(x == 0.0) 47 | return true; 48 | 49 | if(tolerance == null) 50 | return false; 51 | 52 | return Math.abs(x) < tolerance; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /fields/src/main/java/network/aika/utils/Writable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.utils; 18 | 19 | import java.io.DataInput; 20 | import java.io.DataOutput; 21 | import java.io.IOException; 22 | 23 | 24 | public interface Writable { 25 | 26 | /** 27 | * Serialize the fields of this object to out. 28 | * 29 | * @param out DataOuput to serialize this object into. 30 | * @throws IOException 31 | */ 32 | void write(DataOutput out) throws IOException; 33 | 34 | /** 35 | * Deserialize the fields of this object from in. 36 | * 37 | *

For efficiency, implementations should attempt to re-use storage in the 38 | * existing object where possible.

39 | * 40 | * @param in DataInput to deseriablize this object from. 41 | * @throws Exception 42 | */ 43 | void readFields(DataInput in, M m) throws Exception; 44 | 45 | } -------------------------------------------------------------------------------- /fields/src/test/java/network/aika/fields/manyobjects/OneToManyRelationTest.java: -------------------------------------------------------------------------------- 1 | package network.aika.fields.manyobjects; 2 | 3 | import network.aika.fields.defs.FieldDefinition; 4 | import network.aika.type.TypeRegistry; 5 | import network.aika.type.TypeRegistryImpl; 6 | import org.junit.jupiter.api.Assertions; 7 | import org.junit.jupiter.api.BeforeEach; 8 | import org.junit.jupiter.api.Test; 9 | 10 | import static network.aika.fields.InputField.inputField; 11 | import static network.aika.fields.SumField.sum; 12 | import static network.aika.fields.manyobjects.TestObjectMany.linkObjects; 13 | import static network.aika.fields.manyobjects.TestTypeMany.TEST_RELATION_FROM; 14 | 15 | 16 | public class OneToManyRelationTest { 17 | 18 | protected TypeRegistry registry; 19 | protected TestTypeOne typeA; 20 | protected TestTypeMany typeB; 21 | 22 | 23 | @BeforeEach 24 | public void init() { 25 | registry = new TypeRegistryImpl(); 26 | 27 | typeA = new TestTypeOne(registry, "A"); 28 | 29 | typeB = new TestTypeMany(registry, "B"); 30 | } 31 | 32 | @Test 33 | public void testInitFields() { 34 | 35 | FieldDefinition fieldA = inputField(typeA, "a"); 36 | FieldDefinition fieldB = inputField(typeA, "b"); 37 | 38 | FieldDefinition fieldC = sum(typeB, "b") 39 | .in(TEST_RELATION_FROM, fieldA) 40 | .in(TEST_RELATION_FROM, fieldB); 41 | 42 | registry.flattenTypeHierarchy(); 43 | 44 | // Object and Field initialization 45 | 46 | TestObjectOne objA = new TestObjectOne(typeA); 47 | objA.setFieldValue(fieldA, 5.0); 48 | objA.setFieldValue(fieldB, 5.0); 49 | 50 | TestObjectMany objB = new TestObjectMany(typeB); 51 | 52 | linkObjects(objA, objB); 53 | objB.initFields(); 54 | 55 | Assertions.assertEquals( 56 | 10.0, 57 | objB.getFieldOutput(fieldC).getValue() 58 | ); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /fields/src/test/java/network/aika/fields/manyobjects/TestObjectMany.java: -------------------------------------------------------------------------------- 1 | package network.aika.fields.manyobjects; 2 | 3 | import network.aika.type.Obj; 4 | import network.aika.type.ObjImpl; 5 | import network.aika.type.TypeRegistry; 6 | import network.aika.type.relations.Relation; 7 | 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | import java.util.stream.Stream; 11 | 12 | public class TestObjectMany extends ObjImpl { 13 | 14 | List relatedTestObjects = new ArrayList<>(); 15 | 16 | public TestObjectMany(TestTypeMany type) { 17 | super(type); 18 | } 19 | 20 | public Stream getRelatedTestObjects() { 21 | return relatedTestObjects.stream(); 22 | } 23 | 24 | public static void linkObjects(TestObjectOne objA, TestObjectMany objB) { 25 | objA.relatedTestObject = objB; 26 | objB.relatedTestObjects.add(objA); 27 | } 28 | 29 | @Override 30 | public Stream followManyRelation(Relation rel) { 31 | return relatedTestObjects.stream(); 32 | } 33 | 34 | @Override 35 | public Obj followSingleRelation(Relation rel) { 36 | return null; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /fields/src/test/java/network/aika/fields/manyobjects/TestObjectOne.java: -------------------------------------------------------------------------------- 1 | package network.aika.fields.manyobjects; 2 | 3 | import network.aika.type.ObjImpl; 4 | import network.aika.type.TypeRegistry; 5 | 6 | 7 | public class TestObjectOne extends ObjImpl { 8 | 9 | TestObjectMany relatedTestObject; 10 | 11 | public TestObjectOne(TestTypeOne type) { 12 | super(type); 13 | } 14 | 15 | public TestObjectMany getRelatedTestObject() { 16 | return relatedTestObject; 17 | } 18 | 19 | public static void linkObjects(TestObjectMany objA, TestObjectOne objB) { 20 | objA.relatedTestObjects.add(objB); 21 | objB.relatedTestObject = objA; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /fields/src/test/java/network/aika/fields/manyobjects/TestTypeMany.java: -------------------------------------------------------------------------------- 1 | package network.aika.fields.manyobjects; 2 | 3 | import network.aika.type.Type; 4 | import network.aika.type.TypeRegistry; 5 | import network.aika.type.relations.Relation; 6 | import network.aika.type.relations.RelationMany; 7 | import network.aika.type.relations.RelationOne; 8 | 9 | import java.util.List; 10 | 11 | import static network.aika.fields.manyobjects.TestTypeOne.TEST_RELATION_TO; 12 | 13 | 14 | public class TestTypeMany extends Type { 15 | 16 | public static RelationMany TEST_RELATION_FROM = new RelationMany(0, "TEST_FROM"); 17 | 18 | static { 19 | TEST_RELATION_FROM.setReversed(TEST_RELATION_TO); 20 | } 21 | 22 | public TestTypeMany(TypeRegistry registry, String name) { 23 | super(registry, name); 24 | } 25 | 26 | @Override 27 | public Relation[] getRelations() { 28 | return new Relation[] {TEST_RELATION_FROM}; 29 | } 30 | 31 | public TestObjectMany instantiate() { 32 | return new TestObjectMany(this); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /fields/src/test/java/network/aika/fields/manyobjects/TestTypeOne.java: -------------------------------------------------------------------------------- 1 | package network.aika.fields.manyobjects; 2 | 3 | 4 | import network.aika.type.Type; 5 | import network.aika.type.TypeRegistry; 6 | import network.aika.type.relations.Relation; 7 | import network.aika.type.relations.RelationMany; 8 | import network.aika.type.relations.RelationOne; 9 | 10 | import java.util.List; 11 | 12 | import static network.aika.fields.manyobjects.TestTypeMany.TEST_RELATION_FROM; 13 | 14 | 15 | public class TestTypeOne extends Type { 16 | 17 | public static RelationOne TEST_RELATION_TO = new RelationOne(0, "TEST_TO"); 18 | 19 | static { 20 | TEST_RELATION_TO.setReversed(TEST_RELATION_FROM); 21 | } 22 | 23 | public TestTypeOne(TypeRegistry registry, String name) { 24 | super(registry, name); 25 | } 26 | 27 | @Override 28 | public Relation[] getRelations() { 29 | return new Relation[] {TEST_RELATION_TO}; 30 | } 31 | 32 | public TestObjectOne instantiate() { 33 | return new TestObjectOne(this); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /fields/src/test/java/network/aika/fields/oneobject/AbstractTestWithObjects.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields.oneobject; 18 | 19 | import network.aika.type.TypeRegistry; 20 | import network.aika.type.TypeRegistryImpl; 21 | import network.aika.type.relations.RelationOne; 22 | 23 | /** 24 | * 25 | * @author Lukas Molzberger 26 | */ 27 | public abstract class AbstractTestWithObjects { 28 | 29 | protected TypeRegistry registry; 30 | 31 | protected TestType typeA; 32 | protected TestType typeB; 33 | 34 | public void init() { 35 | registry = new TypeRegistryImpl(); 36 | 37 | typeA = new TestType(registry, "A"); 38 | typeB = new TestType(registry, "B"); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /fields/src/test/java/network/aika/fields/oneobject/DivisionTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields.oneobject; 18 | 19 | import network.aika.fields.defs.FieldDefinition; 20 | import org.junit.jupiter.api.Assertions; 21 | import org.junit.jupiter.api.BeforeEach; 22 | import org.junit.jupiter.params.ParameterizedTest; 23 | import org.junit.jupiter.params.provider.ValueSource; 24 | 25 | import static network.aika.fields.Division.div; 26 | import static network.aika.fields.InputField.inputField; 27 | import static network.aika.fields.oneobject.TestObject.linkObjects; 28 | import static network.aika.fields.oneobject.TestType.TEST_RELATION_FROM; 29 | 30 | 31 | /** 32 | * @author Lukas Molzberger 33 | */ 34 | public class DivisionTest extends AbstractTestWithObjects { 35 | 36 | 37 | @BeforeEach 38 | public void init() { 39 | super.init(); 40 | } 41 | 42 | @ParameterizedTest 43 | @ValueSource(ints = {0, 1, 2}) 44 | public void testDivision(int linkingPos) { 45 | 46 | // Type and Math Model initialization 47 | 48 | FieldDefinition a = inputField(typeA, "a"); 49 | FieldDefinition b = inputField(typeA, "b"); 50 | 51 | FieldDefinition c = div(typeB, "c") 52 | .in(TEST_RELATION_FROM, a, 0) 53 | .in(TEST_RELATION_FROM, b, 1); 54 | 55 | registry.flattenTypeHierarchy(); 56 | 57 | // Object and Field initialization 58 | 59 | TestObject oa = typeA.instantiate(); 60 | TestObject ob = typeB.instantiate(); 61 | 62 | if(linkingPos == 0) { 63 | linkObjects(oa, ob); 64 | ob.initFields(); 65 | } 66 | 67 | oa.setFieldValue(a, 25.0); 68 | 69 | if(linkingPos == 1) { 70 | linkObjects(oa, ob); 71 | ob.initFields(); 72 | } 73 | 74 | Assertions.assertEquals(0.0, ob.getFieldValue(c)); 75 | 76 | oa.setFieldValue(b, 5.0); 77 | 78 | if(linkingPos == 2) { 79 | linkObjects(oa, ob); 80 | ob.initFields(); 81 | } 82 | 83 | Assertions.assertEquals( 84 | 5.0, 85 | ob.getFieldOutput(c).getValue() 86 | ); 87 | 88 | oa.setFieldValue(b, 10.0); 89 | 90 | Assertions.assertEquals( 91 | 2.5, 92 | ob.getFieldOutput(c).getValue() 93 | ); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /fields/src/test/java/network/aika/fields/oneobject/ExponentialFunctionTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields.oneobject; 18 | 19 | import network.aika.fields.defs.FieldDefinition; 20 | import network.aika.type.Obj; 21 | import network.aika.type.ObjImpl; 22 | import network.aika.type.TypeRegistry; 23 | import network.aika.type.TypeRegistryImpl; 24 | import org.junit.jupiter.api.Assertions; 25 | import org.junit.jupiter.api.Test; 26 | 27 | import static network.aika.fields.ExponentialFunction.exp; 28 | import static network.aika.fields.InputField.inputField; 29 | import static network.aika.fields.oneobject.TestType.SELF; 30 | 31 | 32 | /** 33 | * @author Lukas Molzberger 34 | */ 35 | @SuppressWarnings({"unchecked", "rawtypes"}) 36 | public class ExponentialFunctionTest { 37 | 38 | @Test 39 | public void testExponentialFunction() { 40 | TypeRegistry registry = new TypeRegistryImpl(); 41 | 42 | TestType type = new TestType(registry, "test"); 43 | 44 | FieldDefinition a = inputField(type, "a"); 45 | FieldDefinition b = exp(type, "b") 46 | .in(SELF, a, 0); 47 | 48 | registry.flattenTypeHierarchy(); 49 | 50 | Obj o = type.instantiate(); 51 | 52 | Assertions.assertNull(o.getFieldOutput(a)); 53 | Assertions.assertNull(o.getFieldOutput(b)); 54 | 55 | o.setFieldValue(a, 5.0); 56 | 57 | Assertions.assertNotNull(o.getFieldOutput(b)); 58 | 59 | Assertions.assertEquals( 60 | 148.413159102576603, 61 | o.getFieldOutput(b).getValue() 62 | ); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /fields/src/test/java/network/aika/fields/oneobject/FieldInstantiationWithObjectsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields.oneobject; 18 | 19 | import network.aika.fields.defs.FieldDefinition; 20 | import org.junit.jupiter.api.Assertions; 21 | import org.junit.jupiter.api.BeforeEach; 22 | import org.junit.jupiter.api.Test; 23 | 24 | import static network.aika.fields.InputField.inputField; 25 | import static network.aika.fields.SumField.sum; 26 | import static network.aika.fields.oneobject.TestObject.linkObjects; 27 | import static network.aika.fields.oneobject.TestType.TEST_RELATION_TO; 28 | 29 | 30 | /** 31 | * @author Lukas Molzberger 32 | */ 33 | public class FieldInstantiationWithObjectsTest extends AbstractTestWithObjects { 34 | 35 | 36 | private FieldDefinition fieldA; 37 | private FieldDefinition fieldB; 38 | 39 | 40 | @BeforeEach 41 | public void init() { 42 | // Type and Math Model initialization 43 | super.init(); 44 | 45 | fieldA = inputField(typeA, "a"); 46 | fieldB = sum(typeB, "b") 47 | .in(TEST_RELATION_TO, fieldA); 48 | 49 | registry.flattenTypeHierarchy(); 50 | } 51 | 52 | @Test 53 | public void testPropagateValue() { 54 | // Object and Field initialization 55 | 56 | TestObject objA = typeA.instantiate(); 57 | TestObject objB = typeB.instantiate(); 58 | linkObjects(objA, objB); 59 | objB.initFields(); 60 | 61 | objA.setFieldValue(fieldA, 5.0); 62 | 63 | Assertions.assertEquals( 64 | 5.0, 65 | objB.getOrCreateFieldInput(fieldB).getValue() 66 | ); 67 | } 68 | 69 | @Test 70 | public void testInitFields() { 71 | 72 | // Object and Field initialization 73 | 74 | TestObject objA = new TestObject(typeA); 75 | objA.setFieldValue(fieldA, 5.0); 76 | 77 | TestObject objB = new TestObject(typeB); 78 | 79 | linkObjects(objA, objB); 80 | objB.initFields(); 81 | 82 | Assertions.assertEquals( 83 | 5.0, 84 | objB.getFieldOutput(fieldB).getValue() 85 | ); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /fields/src/test/java/network/aika/fields/oneobject/MultiplicationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields.oneobject; 18 | 19 | import network.aika.fields.defs.FieldDefinition; 20 | import org.junit.jupiter.api.Assertions; 21 | import org.junit.jupiter.api.BeforeEach; 22 | import org.junit.jupiter.params.ParameterizedTest; 23 | import org.junit.jupiter.params.provider.ValueSource; 24 | 25 | import static network.aika.fields.InputField.inputField; 26 | import static network.aika.fields.Multiplication.mul; 27 | import static network.aika.fields.oneobject.TestObject.linkObjects; 28 | import static network.aika.fields.oneobject.TestType.TEST_RELATION_FROM; 29 | 30 | 31 | /** 32 | * @author Lukas Molzberger 33 | */ 34 | public class MultiplicationTest extends AbstractTestWithObjects { 35 | 36 | 37 | @BeforeEach 38 | public void init() { 39 | super.init(); 40 | } 41 | 42 | @ParameterizedTest 43 | @ValueSource(ints = {0, 1, 2}) 44 | public void testMultiplication(int linkingPos) { 45 | 46 | // Type and Math Model initialization 47 | 48 | FieldDefinition a = inputField(typeA, "a"); 49 | FieldDefinition b = inputField(typeA, "b"); 50 | 51 | FieldDefinition c = mul(typeB, "c") 52 | .in(TEST_RELATION_FROM, a, 0) 53 | .in(TEST_RELATION_FROM, b, 1); 54 | 55 | registry.flattenTypeHierarchy(); 56 | 57 | 58 | // Object and Field initialization 59 | 60 | TestObject oa = typeA.instantiate(); 61 | TestObject ob = typeB.instantiate(); 62 | 63 | if(linkingPos == 0) { 64 | linkObjects(oa, ob); 65 | ob.initFields(); 66 | } 67 | 68 | oa.setFieldValue(a, 5.0); 69 | 70 | if(linkingPos == 1) { 71 | linkObjects(oa, ob); 72 | ob.initFields(); 73 | } 74 | 75 | Assertions.assertEquals(0.0, ob.getFieldValue(c)); 76 | 77 | oa.setFieldValue(b, 5.0); 78 | 79 | if(linkingPos == 2) { 80 | linkObjects(oa, ob); 81 | ob.initFields(); 82 | } 83 | 84 | Assertions.assertEquals( 85 | 25.0, 86 | ob.getFieldOutput(c).getValue() 87 | ); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /fields/src/test/java/network/aika/fields/oneobject/ObjectInstantiationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields.oneobject; 18 | 19 | import org.junit.jupiter.api.Assertions; 20 | import org.junit.jupiter.api.BeforeEach; 21 | import org.junit.jupiter.api.Test; 22 | 23 | /** 24 | * 25 | * @author Lukas Molzberger 26 | */ 27 | public class ObjectInstantiationTest extends AbstractTestWithObjects { 28 | 29 | @BeforeEach 30 | public void init() { 31 | super.init(); 32 | } 33 | 34 | @Test 35 | public void testObjectInstantiation() { 36 | registry.flattenTypeHierarchy(); 37 | 38 | TestObject oa = typeA.instantiate(); 39 | 40 | Assertions.assertNotNull(oa); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /fields/src/test/java/network/aika/fields/oneobject/SubtractionTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields.oneobject; 18 | 19 | import network.aika.fields.defs.FieldDefinition; 20 | import org.junit.jupiter.api.Assertions; 21 | import org.junit.jupiter.api.BeforeEach; 22 | import org.junit.jupiter.params.ParameterizedTest; 23 | import org.junit.jupiter.params.provider.ValueSource; 24 | 25 | import static network.aika.fields.InputField.inputField; 26 | import static network.aika.fields.Subtraction.sub; 27 | import static network.aika.fields.oneobject.TestObject.linkObjects; 28 | import static network.aika.fields.oneobject.TestType.TEST_RELATION_FROM; 29 | 30 | 31 | /** 32 | * @author Lukas Molzberger 33 | */ 34 | public class SubtractionTest extends AbstractTestWithObjects { 35 | 36 | 37 | @BeforeEach 38 | public void init() { 39 | super.init(); 40 | } 41 | 42 | 43 | @ParameterizedTest 44 | @ValueSource(ints = {0, 1, 2}) 45 | public void testSubtraction(int linkingPos) { 46 | 47 | // Type and Math Model initialization 48 | 49 | FieldDefinition a = inputField(typeA, "a"); 50 | FieldDefinition b = inputField(typeA, "b"); 51 | 52 | FieldDefinition c = sub(typeB, "c") 53 | .in(TEST_RELATION_FROM, a, 0) 54 | .in(TEST_RELATION_FROM, b, 1); 55 | 56 | registry.flattenTypeHierarchy(); 57 | 58 | // Object and Field initialization 59 | 60 | TestObject oa = typeA.instantiate(); 61 | TestObject ob = typeB.instantiate(); 62 | 63 | if(linkingPos == 0) { 64 | linkObjects(oa, ob); 65 | ob.initFields(); 66 | } 67 | 68 | oa.setFieldValue(a, 50.0); 69 | 70 | if(linkingPos == 1) { 71 | linkObjects(oa, ob); 72 | ob.initFields(); 73 | } 74 | 75 | oa.setFieldValue(b, 20.0); 76 | 77 | if(linkingPos == 2) { 78 | linkObjects(oa, ob); 79 | ob.initFields(); 80 | } 81 | 82 | Assertions.assertEquals( 83 | 30.0, 84 | ob.getFieldOutput(c).getValue() 85 | ); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /fields/src/test/java/network/aika/fields/oneobject/TestObject.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields.oneobject; 18 | 19 | import network.aika.type.Obj; 20 | import network.aika.type.ObjImpl; 21 | import network.aika.type.TypeRegistry; 22 | import network.aika.type.relations.Relation; 23 | 24 | import java.util.stream.Stream; 25 | 26 | import static network.aika.fields.oneobject.TestType.TEST_RELATION_FROM; 27 | import static network.aika.fields.oneobject.TestType.TEST_RELATION_TO; 28 | import static network.aika.fields.softmax.SoftmaxInputType.CORRESPONDING_OUTPUT_LINK; 29 | import static network.aika.fields.softmax.SoftmaxInputType.INPUT_TO_NORM; 30 | 31 | /** 32 | * 33 | * @author Lukas Molzberger 34 | */ 35 | public class TestObject extends ObjImpl { 36 | 37 | TestObject relatedTestObject; 38 | 39 | public TestObject(TestType type) { 40 | super(type); 41 | } 42 | 43 | @Override 44 | public Obj followSingleRelation(Relation rel) { 45 | if(rel == TEST_RELATION_FROM) 46 | return getRelatedTestObject(); 47 | else if(rel == TEST_RELATION_TO) 48 | return getRelatedTestObject(); 49 | else 50 | throw new RuntimeException("Invalid Relation"); 51 | } 52 | 53 | public TestObject getRelatedTestObject() { 54 | return relatedTestObject; 55 | } 56 | 57 | public static void linkObjects(TestObject objA, TestObject objB) { 58 | objA.relatedTestObject = objB; 59 | objB.relatedTestObject = objA; 60 | } 61 | 62 | @Override 63 | public Stream followManyRelation(Relation rel) { 64 | return Stream.of(relatedTestObject); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /fields/src/test/java/network/aika/fields/oneobject/TestType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields.oneobject; 18 | 19 | import network.aika.type.Type; 20 | import network.aika.type.TypeRegistry; 21 | import network.aika.type.relations.RelationOne; 22 | import network.aika.type.relations.RelationSelf; 23 | 24 | import java.util.List; 25 | 26 | /** 27 | * 28 | * @author Lukas Molzberger 29 | */ 30 | public class TestType extends Type { 31 | 32 | public static final RelationSelf SELF = new RelationSelf(0, "TEST_SELF"); 33 | 34 | public static final RelationOne TEST_RELATION_FROM = new RelationOne(1, "TEST_FROM"); 35 | public static final RelationOne TEST_RELATION_TO = new RelationOne(2, "TEST_TO"); 36 | 37 | static { 38 | TEST_RELATION_TO.setReversed(TEST_RELATION_FROM); 39 | TEST_RELATION_FROM.setReversed(TEST_RELATION_TO); 40 | } 41 | 42 | public TestType(TypeRegistry registry, String name) { 43 | super(registry, name); 44 | 45 | relations.addAll(List.of(SELF, TEST_RELATION_FROM, TEST_RELATION_TO)); 46 | } 47 | 48 | public TestObject instantiate() { 49 | return new TestObject(this); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /fields/src/test/java/network/aika/fields/softmax/SoftmaxInputObj.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields.softmax; 18 | 19 | import network.aika.type.Obj; 20 | import network.aika.type.ObjImpl; 21 | import network.aika.type.TypeRegistry; 22 | import network.aika.type.relations.Relation; 23 | 24 | import java.util.stream.Stream; 25 | 26 | import static network.aika.fields.softmax.SoftmaxInputType.CORRESPONDING_OUTPUT_LINK; 27 | import static network.aika.fields.softmax.SoftmaxInputType.INPUT_TO_NORM; 28 | 29 | /** 30 | * 31 | * @author Lukas Molzberger 32 | */ 33 | public class SoftmaxInputObj extends ObjImpl { 34 | 35 | SoftmaxNormObj normObject; 36 | Integer bsId; 37 | 38 | public SoftmaxInputObj(SoftmaxInputType type, Integer bsId) { 39 | super(type); 40 | this.bsId = bsId; 41 | } 42 | 43 | @Override 44 | public Obj followSingleRelation(Relation rel) { 45 | if(rel == INPUT_TO_NORM) 46 | return getNormObject(); 47 | else if(rel == CORRESPONDING_OUTPUT_LINK) 48 | return getCorrespondingOutputLink(); 49 | else 50 | throw new RuntimeException("Invalid Relation"); 51 | } 52 | 53 | public SoftmaxNormObj getNormObject() { 54 | return normObject; 55 | } 56 | 57 | public void setNormObject(SoftmaxNormObj normObject) { 58 | this.normObject = normObject; 59 | } 60 | 61 | public SoftmaxOutputObj getCorrespondingOutputLink() { 62 | if(normObject == null || normObject.outputs.isEmpty()) 63 | return null; 64 | 65 | return normObject.getOutput(bsId); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /fields/src/test/java/network/aika/fields/softmax/SoftmaxInputType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields.softmax; 18 | 19 | 20 | import network.aika.type.Type; 21 | import network.aika.type.TypeRegistry; 22 | import network.aika.type.relations.Relation; 23 | import network.aika.type.relations.RelationMany; 24 | import network.aika.type.relations.RelationOne; 25 | 26 | import java.util.List; 27 | 28 | import static network.aika.fields.softmax.SoftmaxNormType.NORM_TO_INPUT; 29 | import static network.aika.fields.softmax.SoftmaxOutputType.CORRESPONDING_INPUT_LINK; 30 | 31 | /** 32 | * 33 | * @author Lukas Molzberger 34 | */ 35 | public class SoftmaxInputType extends Type { 36 | 37 | public static RelationOne INPUT_TO_NORM = new RelationOne(0, "INPUT_TO_NORM"); 38 | public static RelationOne CORRESPONDING_OUTPUT_LINK = new RelationOne(1, "CORRESPONDING_OUTPUT_LINK"); 39 | 40 | static { 41 | INPUT_TO_NORM.setReversed(NORM_TO_INPUT); 42 | CORRESPONDING_OUTPUT_LINK.setReversed(CORRESPONDING_INPUT_LINK); 43 | } 44 | 45 | public SoftmaxInputType(TypeRegistry registry, String name) { 46 | super(registry, name); 47 | } 48 | 49 | @Override 50 | public Relation[] getRelations() { 51 | return new Relation[] {INPUT_TO_NORM, CORRESPONDING_OUTPUT_LINK}; 52 | } 53 | 54 | public SoftmaxInputObj instantiate(int bsId) { 55 | return new SoftmaxInputObj(this, bsId); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /fields/src/test/java/network/aika/fields/softmax/SoftmaxNormObj.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields.softmax; 18 | 19 | import network.aika.type.Obj; 20 | import network.aika.type.ObjImpl; 21 | import network.aika.type.TypeRegistry; 22 | import network.aika.type.relations.Relation; 23 | 24 | import java.util.ArrayList; 25 | import java.util.List; 26 | import java.util.stream.Stream; 27 | 28 | import static network.aika.fields.softmax.SoftmaxInputType.CORRESPONDING_OUTPUT_LINK; 29 | import static network.aika.fields.softmax.SoftmaxInputType.INPUT_TO_NORM; 30 | import static network.aika.fields.softmax.SoftmaxNormType.NORM_TO_INPUT; 31 | import static network.aika.fields.softmax.SoftmaxNormType.NORM_TO_OUTPUT; 32 | 33 | /** 34 | * 35 | * @author Lukas Molzberger 36 | */ 37 | public class SoftmaxNormObj extends ObjImpl { 38 | 39 | List inputs = new ArrayList<>(); 40 | List outputs = new ArrayList<>(); 41 | 42 | public SoftmaxNormObj(SoftmaxNormType type) { 43 | super(type); 44 | } 45 | 46 | public static void linkObjects(SoftmaxInputObj[] inputsObjs, SoftmaxNormObj normObj) { 47 | for (int i = 0; i < inputsObjs.length; i++) { 48 | SoftmaxInputObj inputObj = inputsObjs[i]; 49 | inputObj.normObject = normObj; 50 | normObj.inputs.add(inputObj); 51 | } 52 | } 53 | 54 | @Override 55 | public Stream followManyRelation(Relation rel) { 56 | if(rel == NORM_TO_INPUT) 57 | return getInputs().map(o -> o) ; 58 | else if(rel == NORM_TO_OUTPUT) 59 | return getOutputs().map(o -> o) ; 60 | else 61 | throw new RuntimeException("Invalid Relation"); 62 | } 63 | 64 | public SoftmaxInputObj getInput(int bsId) { 65 | return inputs.get(bsId); 66 | } 67 | 68 | public Stream getInputs() { 69 | return inputs.stream(); 70 | } 71 | 72 | public SoftmaxOutputObj getOutput(int bsId) { 73 | return outputs.get(bsId); 74 | } 75 | 76 | public Stream getOutputs() { 77 | return outputs.stream(); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /fields/src/test/java/network/aika/fields/softmax/SoftmaxNormType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields.softmax; 18 | 19 | import network.aika.type.Type; 20 | import network.aika.type.TypeRegistry; 21 | import network.aika.type.relations.Relation; 22 | import network.aika.type.relations.RelationMany; 23 | import network.aika.type.relations.RelationOne; 24 | 25 | import java.util.List; 26 | 27 | import static network.aika.fields.softmax.SoftmaxInputType.INPUT_TO_NORM; 28 | import static network.aika.fields.softmax.SoftmaxOutputType.OUTPUT_TO_NORM; 29 | 30 | /** 31 | * 32 | * @author Lukas Molzberger 33 | */ 34 | public class SoftmaxNormType extends Type { 35 | 36 | public static RelationMany NORM_TO_INPUT = new RelationMany( 0, "NORM_TO_INPUT"); 37 | public static RelationMany NORM_TO_OUTPUT = new RelationMany( 1, "NORM_TO_OUTPUT"); 38 | 39 | static { 40 | NORM_TO_INPUT.setReversed(INPUT_TO_NORM); 41 | NORM_TO_OUTPUT.setReversed(OUTPUT_TO_NORM); 42 | } 43 | 44 | public SoftmaxNormType(TypeRegistry registry, String name) { 45 | super(registry, name); 46 | } 47 | 48 | public SoftmaxNormObj instantiate() { 49 | return new SoftmaxNormObj(this); 50 | } 51 | 52 | @Override 53 | public Relation[] getRelations() { 54 | return new Relation[] {NORM_TO_INPUT, NORM_TO_OUTPUT}; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /fields/src/test/java/network/aika/fields/softmax/SoftmaxOutputObj.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields.softmax; 18 | 19 | import network.aika.type.Obj; 20 | import network.aika.type.ObjImpl; 21 | import network.aika.type.TypeRegistry; 22 | import network.aika.type.relations.Relation; 23 | 24 | import static network.aika.fields.softmax.SoftmaxInputType.CORRESPONDING_OUTPUT_LINK; 25 | import static network.aika.fields.softmax.SoftmaxInputType.INPUT_TO_NORM; 26 | import static network.aika.fields.softmax.SoftmaxOutputType.CORRESPONDING_INPUT_LINK; 27 | import static network.aika.fields.softmax.SoftmaxOutputType.OUTPUT_TO_NORM; 28 | 29 | /** 30 | * 31 | * @author Lukas Molzberger 32 | */ 33 | public class SoftmaxOutputObj extends ObjImpl { 34 | 35 | SoftmaxNormObj normObj; 36 | Integer bsId; 37 | 38 | public SoftmaxOutputObj(SoftmaxOutputType type, Integer bsId) { 39 | super(type); 40 | this.bsId = bsId; 41 | } 42 | 43 | @Override 44 | public Obj followSingleRelation(Relation rel) { 45 | if(rel == OUTPUT_TO_NORM) 46 | return getNormObj(); 47 | else if(rel == CORRESPONDING_INPUT_LINK) 48 | return getCorrespondingInputLink(); 49 | else 50 | throw new RuntimeException("Invalid Relation"); 51 | } 52 | 53 | public SoftmaxNormObj getNormObj() { 54 | return normObj; 55 | } 56 | 57 | public static void linkObjects(SoftmaxNormObj normObj, SoftmaxOutputObj[] outputObjs) { 58 | for(int i = 0; i < outputObjs.length; i++) { 59 | SoftmaxOutputObj outputObj = outputObjs[i]; 60 | normObj.outputs.add(outputObj); 61 | outputObj.normObj = normObj; 62 | } 63 | } 64 | 65 | public SoftmaxInputObj getCorrespondingInputLink() { 66 | if(normObj == null) 67 | return null; 68 | 69 | return normObj.getInput(bsId); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /fields/src/test/java/network/aika/fields/softmax/SoftmaxOutputType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package network.aika.fields.softmax; 18 | 19 | 20 | import network.aika.type.Type; 21 | import network.aika.type.TypeRegistry; 22 | import network.aika.type.relations.Relation; 23 | import network.aika.type.relations.RelationOne; 24 | 25 | import java.util.List; 26 | 27 | import static network.aika.fields.softmax.SoftmaxInputType.CORRESPONDING_OUTPUT_LINK; 28 | import static network.aika.fields.softmax.SoftmaxNormType.NORM_TO_OUTPUT; 29 | 30 | /** 31 | * 32 | * @author Lukas Molzberger 33 | */ 34 | public class SoftmaxOutputType extends Type { 35 | 36 | public static RelationOne OUTPUT_TO_NORM = new RelationOne(0, "OUTPUT_TO_NORM"); 37 | public static RelationOne CORRESPONDING_INPUT_LINK = new RelationOne(1, "CORRESPONDING_INPUT_LINK"); 38 | 39 | static { 40 | OUTPUT_TO_NORM.setReversed(NORM_TO_OUTPUT); 41 | CORRESPONDING_INPUT_LINK.setReversed(CORRESPONDING_OUTPUT_LINK); 42 | } 43 | 44 | public SoftmaxOutputType(TypeRegistry registry, String name) { 45 | super(registry, name); 46 | } 47 | 48 | public SoftmaxOutputObj instantiate(int bsId) { 49 | return new SoftmaxOutputObj(this, bsId); 50 | } 51 | 52 | @Override 53 | public Relation[] getRelations() { 54 | return new Relation[] {OUTPUT_TO_NORM, CORRESPONDING_INPUT_LINK}; 55 | } 56 | } 57 | --------------------------------------------------------------------------------