public static List <CompoundIndex> GetExisting(IEnumerable <CompoundIndex> indexes, out List <string> errors, IShape root) { errors = new List <string>(); var result = new List <CompoundIndex>(); CompoundIndex previous = null; foreach (var compoundIndex in indexes) { try { if (compoundIndex.Equals(previous)) { continue; } root.GetShapeAt(compoundIndex); result.Add(compoundIndex); previous = compoundIndex; } catch { errors.Add(compoundIndex.ToString()); } } result.Sort(); result.Reverse(); return(result); }
public static CompoundIndex[] GetNodeChildCompoundIndexes() { int[] nodeChildIndexes = GetNodeChildIndexes(); CompoundIndex[] nodeChildCompoundIndexes = new CompoundIndex[nodeChildIndexes.Length]; foreach (var nodeChildCompoundIndex in nodeChildCompoundIndexes) { nodeChildCompoundIndex.treeIndex = nodePointer.treeIndex; } return nodeChildCompoundIndexes; }
public void RemoveAt(CompoundIndex index) { if (index.Size != 1) { Shapes[index.Top].RemoveAt(index.Sub); } else { Shapes.RemoveAt(index.Top); } }
public static void RefreshStoryObjects(CompoundIndex nodePointer) { foreach (var storyObject in storyObjects) { foreach (var activeInterval in storyObject.activeIntervals) { if (Story.IsNodeBetweenConnection(nodePointer, activeInterval)) { storyObject.storyObject.SetActive(true); } else { storyObject.storyObject.SetActive(false); } } } }
static void testNars() { CompoundAndTermContext compoundAndTermContext = new CompoundAndTermContext(); RuleDispatcher.compoundAndTermContext = compoundAndTermContext; // for debugging PrototypingInput prototypingInput = new PrototypingInput(compoundAndTermContext); Nar nar = Nar.make(compoundAndTermContext, new MetaNix.nars.config.RuntimeParameters()); CompoundIndex compoundIndex1 = prototypingInput.makeInheritance("a", "b"); ClassicalSentence.MakeByTermPunctuationTruthStampNormalizeParameters task1SentenceParameters = new ClassicalSentence.MakeByTermPunctuationTruthStampNormalizeParameters(); task1SentenceParameters.term = compoundAndTermContext.accessCompoundByIndex(compoundIndex1).thisTermReferer; task1SentenceParameters.truth = TruthValue.make(1.0f, 0.5f); task1SentenceParameters.stamp = Stamp.makeWithPresentTense(nar.memory); task1SentenceParameters.punctation = ClassicalSentence.EnumPunctation.JUDGMENT; ClassicalSentence task1Sentence = ClassicalSentence.makeByTermPunctuationTruthStampNormalize(task1SentenceParameters); ClassicalTask.MakeParameters task1MakeParameters = new ClassicalTask.MakeParameters(); task1MakeParameters.sentence = task1Sentence; task1MakeParameters.budget = new ClassicalBudgetValue(0.5f, 0.5f, 0.5f); nar.inputTask(ClassicalTask.make(task1MakeParameters)); CompoundIndex compoundIndex2 = prototypingInput.makeInheritance("b", "c"); ClassicalSentence.MakeByTermPunctuationTruthStampNormalizeParameters task2SentenceParameters = new ClassicalSentence.MakeByTermPunctuationTruthStampNormalizeParameters(); task2SentenceParameters.term = compoundAndTermContext.accessCompoundByIndex(compoundIndex2).thisTermReferer; task2SentenceParameters.truth = TruthValue.make(1.0f, 0.5f); task2SentenceParameters.stamp = Stamp.makeWithPresentTense(nar.memory); task2SentenceParameters.punctation = ClassicalSentence.EnumPunctation.JUDGMENT; ClassicalSentence task2Sentence = ClassicalSentence.makeByTermPunctuationTruthStampNormalize(task2SentenceParameters); ClassicalTask.MakeParameters task2MakeParameters = new ClassicalTask.MakeParameters(); task2MakeParameters.sentence = task2Sentence; task2MakeParameters.budget = new ClassicalBudgetValue(0.5f, 0.5f, 0.5f); nar.inputTask(ClassicalTask.make(task2MakeParameters)); for (;;) { nar.cycle(); } int here = 5; }
public static IEnumerable <CompoundIndex> ParseIndexes( IEnumerable <string> arguments, out List <string> errors) { errors = new List <string>(); var result = new List <CompoundIndex>(); foreach (var argument in arguments) { if (CompoundIndex.TryParse(argument, out var parsedIndex)) { result.Add(parsedIndex); } else { errors.Add(argument); } } return(result); }
public void insert(CompoundIndex indexValue) { /* * version(DEBUG) { * static if (WithCompoundId) { * assert(reasonerInstance.accessCompoundByIndex(index).cachedHashWithCompoundIdValid); * } * else { * assert(reasonerInstance.accessCompoundByIndex(index).cachedHashWithoutCompoundIdValid); * } * } */ HashWithIndex hashWithIndex = new HashWithIndex(); hashWithIndex.index = indexValue.index; if (withCompoundId) { hashWithIndex.hash = compoundAndTermContext.accessCompoundByIndex(indexValue).cachedHashWithCompoundId; } else { hashWithIndex.hash = compoundAndTermContext.accessCompoundByIndex(indexValue).cachedHashWithoutCompoundId; } // insert into hashtable if (hashtable.ContainsKey(hashWithIndex.hash)) { hashtable[hashWithIndex.hash].Add(hashWithIndex); } else { hashtable[hashWithIndex.hash] = new List <HashWithIndex>() { hashWithIndex }; } }
public Root(Storage db) { _listings = db.CreateIndex <Listing>(new Type[] { typeof(int), typeof(int) }, false); _employers = db.CreateArray <Employer>(); _defaultSettings = db.CreateFieldIndex <string, DefaultSettings>("_id", true); }
static void InitializeStory() { nodePointer = new CompoundIndex(0, 0); nodePointerStack = new Stack<CompoundIndex>(); SaveStory(); DataSL.SaveData<bool>(initializedFlagKey, true); }
public static void LoadStory() { if (!DataSL.LoadData<bool>(initializedFlagKey)) { InitializeStory(); return; } nodePointer = new CompoundIndex(DataSL.LoadData<int>(nodePointerTreeIndexKey), DataSL.LoadData<int>(nodePointerNodeIndexKey)); nodePointerStack = new Stack<CompoundIndex>(); int[] nodePointerStackTreeIndexes = DataSL.LoadArray<int>(nodePointerStackTreeIndexesKey); int[] nodePointerStackNodeIndexes = DataSL.LoadArray<int>(nodePointerStackNodeIndexesKey); for (int i = 0; i < nodePointerStackTreeIndexes.Length; i++) { nodePointerStack.Push(new CompoundIndex(nodePointerStackTreeIndexes[i], nodePointerStackNodeIndexes[i])); } }
public static bool JumpToTree(int treeIndex) { if (!IsTreeAccessible(treeIndex)) { return false; } nodePointerStack.Push(nodePointer); nodePointer = new CompoundIndex(treeIndex, Forest.entryTreeIndex); StoryChangeEvent(nodePointer); return true; }
public static bool JumpBack() { if (nodePointerStack.Count == 0) { return false; } nodePointer = nodePointerStack.Pop(); StoryChangeEvent(nodePointer); return true; }
public static bool IsNodeBetweenConnection(CompoundIndex nodeIndex, NodeConnection nodeConnection) { if (nodeIndex.treeIndex != nodeConnection.treeIndex) { return false; } int[] path = forest.trees[nodeIndex.treeIndex].FindPath( nodeConnection.startNodeIndex, nodeConnection.endNodeIndex); foreach (var node in path) { if (node == nodeIndex.nodeIndex) { return true; } } return false; }
public IShape GetShapeAt(CompoundIndex index) { throw new InvalidOperationException("Non-compound shape do not contain sub elements!"); }
static void insertDerivedCompoundTerm(CompoundAndTermContext compoundAndTermContext, TemporaryDerivedCompoundWithDecoration derivedCompoundTerm) { // TODO< good place to hook in (compound)term compression > // the index of the compound in the compound table is returned with the decoration of the argument "derivedCompoundTerm" void innerFnInsertIfItDoesntExistRecursivly(TemporaryDerivedCompoundWithDecoration derivedCompoundTermInner) { // recurse to children if (derivedCompoundTermInner.type == TemporaryDerivedCompoundWithDecoration.EnumType.COMPOUND) { innerFnInsertIfItDoesntExistRecursivly(derivedCompoundTermInner.leftChildren); innerFnInsertIfItDoesntExistRecursivly(derivedCompoundTermInner.rightChildren); } // create if not exist if (derivedCompoundTermInner.type == TemporaryDerivedCompoundWithDecoration.EnumType.COMPOUND) // is a Temporary compound(term) { bool innerFnEarlyTestExistsCompound() { var childrenCompoundsWithDecorations = new TemporaryDerivedCompoundWithDecoration[] { derivedCompoundTermInner.leftChildren, derivedCompoundTermInner.rightChildren }; return(compoundAndTermContext.existTermTuple(new List <TermOrCompoundTermOrVariableReferer>(childrenCompoundsWithDecorations.Select(n => n.decoration.returnReferer(compoundAndTermContext))))); } // returns compoundIndex CompoundIndex innerFnCreateCompoundAndAddToReasoner(TermTupleIndex termTupleIndex) { // create compound ulong compoundIndex2 = compoundAndTermContext.getCompoundCreateIndex(); // get the index where the compound will be created Compound.MakeParameters compoundMakeParameters2 = new Compound.MakeParameters(); compoundMakeParameters2.termComplexity = derivedCompoundTermInner.termComplexity; compoundMakeParameters2.flagsOfCopula = derivedCompoundTermInner.flagsOfCopula; compoundMakeParameters2.thisTermReferer = TermOrCompoundTermOrVariableReferer.makeNonatomic(/* 32 bit conversion*/ (int)compoundIndex2); // create new referer based on the compound index compoundMakeParameters2.termTupleIndex = termTupleIndex; Compound createdCompound = compoundAndTermContext.createCompound(compoundMakeParameters2); // insert compound compoundAndTermContext.addCompound(createdCompound); return(CompoundIndex.make(/*quick and dirty conversation*/ (uint)compoundIndex2)); } // build the term, calculate the hash, try to lookup the compound based on the hash // if it exists we return the compoundIndex of the found compound // if it doesn't exist we create a new Compound and return it if (innerFnEarlyTestExistsCompound()) // if the term tuple exists then the compound could exist // build compound and calc hash and try to look it up and compare it to the built compound { var childrenCompoundsWithDecorations = new TemporaryDerivedCompoundWithDecoration[] { derivedCompoundTermInner.leftChildren, derivedCompoundTermInner.rightChildren }; TermTupleIndex termTupleIndex = compoundAndTermContext.getTermTupleIndexByReferers(childrenCompoundsWithDecorations.Select(n => n.decoration.returnReferer(compoundAndTermContext)).ToArray <TermOrCompoundTermOrVariableReferer>()); // * build compound Compound.MakeParameters compoundMakeParameters = new Compound.MakeParameters(); compoundMakeParameters.termComplexity = derivedCompoundTermInner.termComplexity; compoundMakeParameters.flagsOfCopula = derivedCompoundTermInner.flagsOfCopula; //compoundMakeParameters.compoundId; we don't need to initialize it because the hash computation for this lookup doesn't use it // the hash computation doesn't use it because we don't know it //compoundMakeParameters.thisTermReferer we don't need to intialize it because it's not used for hash computation compoundMakeParameters.termTupleIndex = termTupleIndex; Compound createdCompoundForHash = Compound.make(compoundMakeParameters); // * calc hash createdCompoundForHash.updateHash(/*with compoundId*/ false); // * lookup hash and compare if possible // if the compound doesn't exist we create and add it CompoundIndex compoundIndex2; if (compoundAndTermContext.existsCompoundWithoutCompoundId(createdCompoundForHash, out compoundIndex2)) { // set as the result the compound derivedCompoundTermInner.decoration.compoundIndex = compoundIndex2; } else { // create and add compound compoundIndex2 = innerFnCreateCompoundAndAddToReasoner(termTupleIndex); // set as the result the compound derivedCompoundTermInner.decoration.compoundIndex = compoundIndex2; } } else // if the term tuple doesn't exist then the compound can't exist so we have to create and return it // create termTuple { var childrenCompoundsWithDecorations = new TemporaryDerivedCompoundWithDecoration[] { derivedCompoundTermInner.leftChildren, derivedCompoundTermInner.rightChildren }; TermTupleIndex termTupleIndex = compoundAndTermContext.addTermTupleByReferers(childrenCompoundsWithDecorations.Select(n => n.decoration.returnReferer(compoundAndTermContext)).ToArray()); // create and add compound CompoundIndex compoundIndex = innerFnCreateCompoundAndAddToReasoner(termTupleIndex); // set as the result the compound derivedCompoundTermInner.decoration.compoundIndex = compoundIndex; } } else if ( derivedCompoundTermInner.type == TemporaryDerivedCompoundType <TemporaryDerivedCompoundDecoration> .EnumType.LEAF || derivedCompoundTermInner.type == TemporaryDerivedCompoundType <TemporaryDerivedCompoundDecoration> .EnumType.INDEPENDENTVARIABLE || derivedCompoundTermInner.type == TemporaryDerivedCompoundType <TemporaryDerivedCompoundDecoration> .EnumType.DEPENDENTVARIABLE ) { if (derivedCompoundTermInner.type == TemporaryDerivedCompoundType <TemporaryDerivedCompoundDecoration> .EnumType.LEAF) { TermOrCompoundTermOrVariableReferer termReferer = derivedCompoundTermInner.termReferer; derivedCompoundTermInner.decoration.referer = termReferer; } /* commented because we have to figure out how to translate the variable id's * * else if( derivedCompoundTermInner.type == TemporaryDerivedCompoundType<TemporaryDerivedCompoundDecoration>.EnumType.INDEPENDENTVARIABLE ) { * compoundId = derivedCompoundTermInner.independentVariableId; // ASSUMPTION< compoundId is the id of the variable > * } * else if( derivedCompoundTermInner.type == TemporaryDerivedCompoundType<TemporaryDerivedCompoundDecoration>.EnumType.DEPENDENTVARIABLE ) { * compoundId = derivedCompoundTermInner.dependentVariableId; // ASSUMPTION< compoundId is the id of the variable > * } */ else { throw new Exception("Internal error"); } } else { throw new Exception("Internal error"); } } // for now we just check for the existence and insert it recursivly innerFnInsertIfItDoesntExistRecursivly(derivedCompoundTerm); }
public IShape GetShapeAt(CompoundIndex index) { return(index.Size == 1 ? Shapes[index.Top] : Shapes[index.Top].GetShapeAt(index.Sub)); }
public Node GetNode(CompoundIndex index) { return GetNode(index.treeIndex, index.nodeIndex); }