//TODO add more properties to Stroke public Thing AddStrokeToUKS(PointPlus P1, PointPlus P2) { ModuleUKS uks = (ModuleUKS)FindModuleByType(typeof(ModuleUKS)); if (uks is ModuleUKS UKS) { if (uks.Labeled("AbsStroke") == null) { uks.AddThing("AbsStroke", "Visual"); } Thing t1 = uks.Valued(P1); if (t1 == null) { t1 = UKS.AddThing("p" + pCount++, new Thing[] { UKS.Labeled("Point") }, P1); } Thing t2 = uks.Valued(P2); if (t2 == null) { t2 = UKS.AddThing("p" + pCount++, new Thing[] { UKS.Labeled("Point") }, P2); } Thing newThing = UKS.AddThing("s" + strokeCount++, new Thing[] { UKS.Labeled("AbsStroke") }, null, new Thing[] { t1, t2 }); return(newThing); } return(null); }
//fill this method in with code which will execute //once for each cycle of the engine public override void Fire() { Init(); //be sure to leave this here ModuleView naSource = theNeuronArray.FindModuleByLabel("UKS"); if (naSource == null) { return; } uks = (ModuleUKS)naSource.TheModule; Thing mentalModel = uks.GetOrAddThing("MentalModel", "Visual"); Thing wordParent = uks.GetOrAddThing("Word", "Audible"); Thing attn = uks.GetOrAddThing("ATTN", "Thing"); Thing attnVisualTarget = attn.GetReferenceWithAncestor(mentalModel); Thing attnAudibleTarget = attn.GetReferenceWithAncestor(wordParent); AssociateWordsWithVisuals(attnAudibleTarget); //if you want the dlg to update, use the following code whenever any parameter changes if (dlg != null && !((ModuleAssociationDlg)dlg).busy) { UpdateDialog(); } }
//fill this method in with code which will execute //once for each cycle of the engine public override void Fire() { Init(); //be sure to leave this here ModuleView naSource = theNeuronArray.FindModuleByLabel("UKS"); if (naSource == null) { return; } uks = (ModuleUKS)naSource.TheModule; GetSegmentsFromUKS(); if (boundaries.Count > 120) { return; //TODO startup hack } FindCorners(); ConnectCorners(); FindAreas(); GetAreaGreatesLengths(); GetAreaCentroids(); GetAreaColors(); SetAreasToUKS(); //if you want the dlg to update, use the following code whenever any parameter changes // UpdateDialog(); }
//fill this method in with code which will execute once //when the module is added, when "initialize" is selected from the context menu, //or when the engine restart button is pressed public override void Initialize() { ModuleView naSource = theNeuronArray.FindModuleByLabel("UKS"); if (naSource == null) { return; } uks = (ModuleUKS)naSource.TheModule; uks.GetOrAddThing("Area", "Shape"); }
//fill this method in with code which will execute //once for each cycle of the engine public override void Fire() { Init(); //be sure to leave this here ModuleView naSource = theNeuronArray.FindModuleByLabel("UKS"); if (naSource == null) { return; } uks = (ModuleUKS)naSource.TheModule; Thing areaParent = uks.GetOrAddThing("CurrentlyVisible", "Visual"); Thing colorParent = uks.GetOrAddThing("Color", "Visual"); foreach (Thing area in areaParent.Children) { var values = uks.GetValues(area); float hue = values["Hue+"]; float sat = values["Sat+"]; float lum = values["Lum+"]; Thing theColor = null; foreach (Thing color in colorParent.Children) { var valuesc = uks.GetValues(color); float huec = valuesc["Hue+"]; float satc = valuesc["Sat+"]; float lumc = valuesc["Lum+"]; //TODO add better match algorithm if (huec == hue && Abs(satc - sat) < 0.2f && Abs(lumc - lum) < 0.2f) { theColor = color; break; } } if (theColor == null) { theColor = uks.AddThing("c*", colorParent); uks.SetValue(theColor, hue, "Hue", 2); uks.SetValue(theColor, sat, "Sat", 2); uks.SetValue(theColor, lum, "Lum", 2); } area.AddReference(theColor); } //if you want the dlg to update, use the following code whenever any parameter changes // UpdateDialog(); }
//fill this method in with code which will execute //once for each cycle of the engine public override void Fire() { Init(); //be sure to leave this here ModuleView naSource = theNeuronArray.FindModuleByLabel("UKS"); if (naSource == null) { return; } uks = (ModuleUKS)naSource.TheModule; Thing knownObjectParent = uks.GetOrAddThing("KnownObject", "Visual"); Thing currentlyVisibleParent = uks.GetOrAddThing("CurrentlyVisible", "Visual"); //List<Thing> visibleKnownThings = new List<Thing>(); foreach (Thing area in currentlyVisibleParent.Children) { Thing knownObject1 = null; foreach (Thing knownObject in knownObjectParent.Children) { Thing knownObjectShape = knownObject.HasReferenceWithParent(uks.Labeled("Area")); Thing areaShape = area.HasReferenceWithParent(uks.Labeled("Area")); Thing knownObjectColor = knownObject.HasReferenceWithParent(uks.Labeled("Color")); Thing areaColor = area.HasReferenceWithParent(uks.Labeled("Color")); //if critical properties match, object is already known if (knownObjectShape == areaShape && knownObjectColor == areaColor) { knownObject1 = knownObject; break; } } if (knownObject1 == null) { //The object is unknown, add it to known objects for future reference knownObject1 = area.Clone(); knownObject1.AddParent(knownObjectParent); knownObject1.Label = "KnownShape*"; } //visibleKnownThings.Add(knownObject1); uks.DeleteAllChilden(area); area.AddChild(knownObject1); //knownObject1.Label = area.Label; } //if you want the dlg to update, use the following code whenever any parameter changes // UpdateDialog(); }
//fill this method in with code which will execute //once for each cycle of the engine public override void Fire() { bool visualTargetChanged = false; Init(); //be sure to leave this here ModuleView naSource = theNeuronArray.FindModuleByLabel("UKS"); if (naSource == null) { return; } uks = (ModuleUKS)naSource.TheModule; DeleteUnusedRelationships(); Thing attn = uks.GetOrAddThing("ATTN", "Thing"); Thing mentalModel = uks.GetOrAddThing("MentalModel", "Visual"); if (attn.References.Count == 0) { return; } Thing newVisualTarget = attn.GetReferenceWithAncestor(mentalModel); if (newVisualTarget != attnVisualTarget) { visualTargetChanged = true; } if (visualTargetChanged) { prevAttnTarget = attnVisualTarget; attnVisualTarget = newVisualTarget; } if (prevAttnTarget != null && attnVisualTarget != null) { if (visualTargetChanged && mentalModel.Children.Count > 1) { //add all the relationships to the uks AddRelationshipsToUKS(prevAttnTarget, attnVisualTarget); } } UpdateDialog(); return; }
void DeletePreviousStrokes() { ModuleUKS uks = (ModuleUKS)FindModuleByType(typeof(ModuleUKS)); if (uks.Labeled("AbsStroke") == null) { uks.AddThing("AbsStroke", "Visual"); } List <Thing> strokes = uks.Labeled("AbsStroke").Children; while (strokes.Count != 0) { uks.DeleteThing(strokes[0]); } }
private void Tvi_MouseRightButtonDown(object sender, MouseButtonEventArgs e) { if (sender is TreeViewItem tvi) { string thingLabel = LeftOfColon(tvi.Header.ToString()).Trim(); ModuleUKS parent = (ModuleUKS)base.ParentModule; if (parent.FindModuleByType(typeof(ModuleAudible)) is ModuleAudible mda) { mda.SpeakThing(thingLabel); } //textBox1.Text = LeftOfColon(tvi.Header.ToString()); //Button_Click(null, null); e.Handled = true; } }
//match against stored relative clusters (shapes) void MatchClustersAgainstStoredShapes() { ModuleUKS uks = (ModuleUKS)FindModuleByType(typeof(ModuleUKS)); if (uks.Labeled("Cluster") == null) { uks.AddThing("Cluster", "Shape"); } Thing tempShape = uks.Labeled("TempShape"); List <Thing> clusters = uks.Labeled("Cluster").Children; //for each stored shape, calculate error from cluster Thing bestThing = null; int bestMatch = int.MinValue; int bestCount = -10; for (int i = 0; i < clusters.Count; i++) { Thing t = clusters[i]; int matchCount = ClusterMatch(t, tempShape); if (matchCount > bestMatch) { bestMatch = matchCount; bestThing = t; bestCount = i; } } //add shape if not found if (bestThing == null || bestMatch < 98) { tempShape.Label = "Shape" + shapeCount++; uks.Labeled("Cluster").AddChild(tempShape); } else { //TODO add linkage to original strokes //delete the tempShape SetNeuronValue(null, 0, bestCount, 1); while (tempShape.Children.Count != 0) { uks.DeleteThing(tempShape.Children[0]); } uks.DeleteThing(tempShape); } }
void ConvertClusterToRelativeStrokes() { ModuleUKS uks = (ModuleUKS)FindModuleByType(typeof(ModuleUKS)); if (uks.Labeled("AbsStroke") == null) { uks.AddThing("AbsStroke", "Visual"); } List <Thing> strokes = uks.Labeled("AbsStroke").Children; List <Thing> relations = uks.Labeled("Relation").Children; //create a tempShape Thing tempShape = uks.AddThing("TempShape", "Shape"); //create relative strokes for (int i = 0; i < strokes.Count; i++) { uks.AddThing("ts" + strokeCount++, tempShape); } List <Thing> relStrokes = uks.Labeled("TempShape").Children; //foreach stroke-pair in cluster for (int i = 0; i < strokes.Count; i++) { for (int j = 0; j < strokes.Count; j++) { if (j != i) { //foreach relation, does pair meet relation foreach (Thing tRelation in relations) { if (StrokesHaveRelation(strokes[i], strokes[j], tRelation)) { //add relation in tempShape relStrokes[i].AddReference(relStrokes[j]); relStrokes[i].AddReference(tRelation); } } } } } }
//fill this method in with code which will execute //once for each cycle of the engine public override void Fire() { Init(); //be sure to leave this here //return; ModuleView naSource = theNeuronArray.FindModuleByLabel("UKS"); if (naSource == null) { return; } uks = (ModuleUKS)naSource.TheModule; Thing x = uks.Labeled("CurrentlyVisible"); Thing currentlyVisibleParent = uks.GetOrAddThing("CurrentlyVisible", "Visual"); foreach (Thing t in currentlyVisibleParent.Children) { Thing tempShape = CreateTempShapeFromArea(t); if (tempShape == null) { continue; } Thing bestMatch = MatchAreaToStoredShapes(tempShape, out float score); if (bestMatch != null && score < 0.5) { t.AddReference(bestMatch); DeleteTempShape(tempShape); } else { AddShapeFromArea(tempShape); tempShape.Label = "SavedShape'*"; t.AddReference(tempShape); } } //if you want the dlg to update, use the following code whenever any parameter changes // UpdateDialog(); }
private void Button_Click(object sender, RoutedEventArgs e) { string root = textBox1.Text; ModuleUKS parent = (ModuleUKS)base.ParentModule; expandedItems.Clear(); FindExpandedItems(theTreeView.Items, root); theTreeView.Items.Clear(); List <Thing> KB = parent.GetTheKB(); Thing t = parent.Labeled(root); if (t != null) { TreeViewItem tvi = new TreeViewItem { Header = t.Label }; tvi.IsExpanded = true; //always expand the top-level item theTreeView.Items.Add(tvi); tvi.MouseRightButtonDown += Tvi_MouseRightButtonDown; AddChildren(t, tvi, 0); } }
public Thing AddSegmentToUKS(PointPlus P1, PointPlus P2, int theColor, PointPlus motion = null, bool addToModel = true) { ModuleUKS nmUKS = (ModuleUKSN)FindModuleByType(typeof(ModuleUKSN)); if (nmUKS is ModuleUKS UKS) { Thing t1, t2; Thing t3 = null; t1 = UKS.AddThing("p" + pCount++, new Thing[] { UKS.Labeled("Point") }, P1); t2 = UKS.AddThing("p" + pCount++, new Thing[] { UKS.Labeled("Point") }, P2); if (addToModel) { t1.AddParent(UKS.Labeled("ModelThing")); t2.AddParent(UKS.Labeled("ModelThing")); } if (motion != null) { t3 = UKS.AddThing("m" + mCount++, new Thing[] { UKS.Labeled("Motion") }, motion); } Thing color = UKS.Valued(theColor); if (color == null) { color = UKS.AddThing("c" + cCount++, new Thing[] { UKS.Labeled("Color") }, theColor); } Thing newThing = null; if (motion != null) { newThing = UKS.AddThing("s" + sCount++, new Thing[] { UKS.Labeled("Segment") }, null, new Thing[] { t1, t2, color, t3 }); } else { newThing = UKS.AddThing("s" + sCount++, new Thing[] { UKS.Labeled("Segment") }, null, new Thing[] { t1, t2, color }); } return(newThing); } return(null); }
//fill this method in with code which will execute //once for each cycle of the engine public override void Fire() { Init(); //be sure to leave this here ModuleView naSource = theNeuronArray.FindModuleByLabel("UKS"); if (naSource == null) { return; } uks = (ModuleUKS)naSource.TheModule; Thing mentalModel = uks.GetOrAddThing("MentalModel", "Visual"); if (mentalModel.Children.Count == 0) { return; } if (autoAttention > 0) { autoAttention--; return; } if (autoAttention < 0) { return; } //if an object moved, it gets attention Thing currentMotionParent = uks.GetOrAddThing("Motion", "Visual"); Thing newAttention = null; foreach (Thing t in currentMotionParent.Children) { if (t.Label.StartsWith("Moved")) { newAttention = t.Children[0]; break; } } if (newAttention != null) { currentAttention = newAttention; } else { currentAttention = mentalModel.Children[rand.Next(mentalModel.Children.Count)]; currentAttention.useCount++; } AddAtttention(currentAttention); foreach (Link l in currentAttention.References) { if (l is Relationship r) { } // r.relationshipType.SetFired(); else { l.T.SetFired(); } } }
//fill this method in with code which will execute //once for each cycle of the engine public override void Fire() { Init(); //be sure to leave this here ModuleView naSource = theNeuronArray.FindModuleByLabel("UKS"); if (naSource == null) { return; } uks = (ModuleUKS)naSource.TheModule; Thing mentalModel = uks.GetOrAddThing("MentalModel", "Visual"); Thing currentlyVisibleParent = uks.GetOrAddThing("CurrentlyVisible", "Visual"); Thing motionParent = uks.GetOrAddThing("Motion", "Visual"); //Things which disappeared in the last cycle need special treatment to remove from mental model & relationships but not the known object foreach (Thing t in motionParent.Children) { if (t.Label.Contains("Disappeared")) { Thing thingToRemove = t.Children[0]; for (int i = thingToRemove.Children.Count - 1; i >= 0; i--) { Thing child = thingToRemove.Children[i]; thingToRemove.RemoveChild(child); } uks.DeleteThing(thingToRemove); } } uks.DeleteAllChilden(motionParent); //build little lists of things which moved or appeared so we can also be left with things which things which disappeared List <Thing> matchedThings = new List <Thing>(); List <Thing> movedThings = new List <Thing>(); List <Thing> movedThingsNew = new List <Thing>(); List <Thing> newThings = new List <Thing>(); foreach (Thing visibleArea in currentlyVisibleParent.Children) { Thing matchedThing = null; foreach (Thing storedArea in mentalModel.Children) { Debug.Assert(visibleArea.Children.Count == 1); if (storedArea.Children.Count != 1) { continue; } if (storedArea.Children[0] == visibleArea.Children[0]) //are these instances of the same object type? { var values = uks.GetValues(visibleArea); var prevValues = uks.GetValues(storedArea); Point c1 = new Point(uks.GetValue(visibleArea, "CtrX+"), uks.GetValue(visibleArea, "CtrY+")); Point c2 = new Point(uks.GetValue(storedArea, "CtrX+"), uks.GetValue(storedArea, "CtrY+")); float dist = (float)(c1 - c2).Length; if (dist < (uks.GetValue(visibleArea, "Siz+") + uks.GetValue(storedArea, "Siz+")) / 2) { //these are probably the same object...is motion detected? var changes = uks.GetChangedValues(visibleArea, storedArea); if (changes.ContainsKey("Siz+") || changes.ContainsKey("CtrX+") || changes.ContainsKey("CtrY+") || changes.ContainsKey("Ang+")) { Thing t1 = uks.AddThing("Moved", motionParent); t1.AddChild(storedArea); foreach (var change in changes) { uks.SetValue(t1, change.Value, change.Key, 0); } movedThings.Add(storedArea); movedThingsNew.Add(visibleArea); } matchedThing = storedArea; goto objectMatch; } } } //there is no matching object, this just appeared! newThings.Add(visibleArea); Thing m = uks.AddThing("Appeared*", motionParent); m.AddChild(visibleArea); continue; objectMatch :; if (matchedThing != null) { matchedThings.Add(matchedThing); } } for (int i = mentalModel.Children.Count - 1; i >= 0; i--) { Thing t = mentalModel.Children[i]; if (!matchedThings.Contains(t)) { Thing m = uks.AddThing("Disappeared*", motionParent); m.AddChild(t); mentalModel.RemoveChild(t); Thing attn = uks.Labeled("ATTN"); if (attn != null) { attn.RemoveReference(t); } } } if (motionParent.Children.Count > 1) { //analyze motion //if only a few objects moved, they moved //if all objects moved in same direction, it's becaue POV turned //if all objects moved in directions radiating to or from a point, POV is moving toward or away from that point. //TODO if all objects moved in same direction but varying magnitudes, POV has moved sideways...further objects move less //calculate the motion vector for all associated objects List <Segment> motions = new List <Segment>(); foreach (Thing t in motionParent.Children) { if (t.Label.StartsWith("Moved")) //ignore appeared/disappeared { var values = uks.GetValues(t); PointPlus p = new PointPlus(0, (float)0); if (values.ContainsKey("CtrX++")) { p.X = values["CtrX++"]; } if (values.ContainsKey("CtrY++")) { p.Y = values["CtrY++"]; } Segment s = new Segment(); s.P1 = p; //This is a relative motion value values = uks.GetValues(t.Children[0]); p = new PointPlus(0, (float)0); if (values.ContainsKey("CtrX+")) { p.X = values["CtrX+"]; } if (values.ContainsKey("CtrY+")) { p.Y = values["CtrY+"]; } s.P2 = p; //This is an absolute position motions.Add(s); } } if (motions.Count > 1) { //do most move the same? ANGULAR POV MOTION //TODO: use clustering to get most likely motions PointPlus pMotion = new PointPlus(motions[0].P1); int count = 0; foreach (Segment s in motions) { if (s.P1.Near(pMotion, .1f)) { count++; } } if (count > motions.Count - 2) //only two objects can be exceptions { for (int i = 0; i < motions.Count; i++) { if (motions[i].P1.Near(pMotion, .1f)) { motions.RemoveAt(i); Thing t = motionParent.Children[i]; uks.DeleteAllChilden(t); uks.DeleteThing(t); i--; } else { //an object has its own motion too...subtract off the POV motion Thing t = motionParent.Children[i]; float x = uks.GetValue(t, "CtrX+"); float y = uks.GetValue(t, "CtrY+"); x -= motions[i].P1.X; y -= motions[i].P1.Y; uks.SetValue(t, x, "CtrX", 3); uks.SetValue(t, y, "CtrY", 3); } } Thing POVMotion = uks.AddThing("POVMotion*", "Motion"); uks.SetValue(POVMotion, pMotion.X, "CtrX", 3); uks.SetValue(POVMotion, pMotion.Y, "CtrY", 3); } else { //this is not a rotation...is it a motion toward/away? //TODO there are no exceptions allowed foreach (var m in motions) { m.P1 += m.P2; } List <PointPlus> intersections = new List <PointPlus>(); for (int i = 0; i < motions.Count; i++) { for (int j = i + 1; j < motions.Count; j++) { Utils.FindIntersection(motions[i].P1, motions[i].P2, motions[j].P1, motions[j].P2, out Point intersection); intersections.Add(intersection); } } //testing clustering... //TODO try all this with points of interest rather than centers double[][] rawData = new double[intersections.Count][]; for (int i = 0; i < intersections.Count; i++) { rawData[i] = new double[2] { intersections[i].X, intersections[i].Y } } ; int[] clusters = KMeansClustering.Cluster(rawData, 2); //now ask if there is a single cluster with most of the data PointPlus aveIntersection = new PointPlus(); for (int i = 0; i < intersections.Count; i++) { aveIntersection.X += intersections[i].X; aveIntersection.Y += intersections[i].Y; } aveIntersection.X /= intersections.Count; aveIntersection.Y /= intersections.Count; float dist = 0; float dir = 0; for (int i = 0; i < intersections.Count; i++) { dist += (intersections[i] - aveIntersection).R; //moving toward or away? dir += (motions[i].P1 - intersections[i]).R - (motions[i].P2 - intersections[i]).R; } dist /= intersections.Count; dir /= intersections.Count; if (dist < 10) // ??? { uks.DeleteAllChilden(motionParent); Thing POVMotion = uks.AddThing("POVMotion*", "Motion"); uks.SetValue(POVMotion, aveIntersection.X, "CtrX", 3); uks.SetValue(POVMotion, aveIntersection.Y, "CtrY", 3); uks.SetValue(POVMotion, dir, "CtrZ", 3); } } } } //transfer currently visible objects to previously visible Debug.Assert(movedThings.Count == movedThingsNew.Count); for (int i = 0; i < movedThings.Count; i++) { uks.SetValue(movedThings[i], uks.GetValue(movedThingsNew[i], "CtrX+"), "CtrX", 0); uks.SetValue(movedThings[i], uks.GetValue(movedThingsNew[i], "CtrY+"), "CtrY", 0); uks.SetValue(movedThings[i], uks.GetValue(movedThingsNew[i], "Siz+"), "Siz", 0); uks.SetValue(movedThings[i], uks.GetValue(movedThingsNew[i], "Ang+"), "Ang", 0); } foreach (var t in newThings) { t.AddParent(mentalModel); //TODO, this may leave multiple things in the mental model with the same label } //This hack puts predictable labels on things in the mental model so we can select them more easily in the description //Area0-n from top->bottom, left->right List <sortable> vals = new List <sortable>(); foreach (Thing storedArea in mentalModel.Children) { var values = uks.GetValues(storedArea); vals.Add(new sortable { t = storedArea, x = values["CtrX+"], y = values["CtrY+"], }); } vals = vals.OrderBy(w => w.y * 1000 + w.x).ToList(); for (int i = 0; i < vals.Count; i++) { vals[i].t.Label = "Area" + i; } //for debugging: //if there is a new image, cancel the existing relationships ModuleImageFile mif = (ModuleImageFile)FindModule("ImageFile"); if (mif == null) { return; } string curPath = mif.GetFilePath(); if (curPath != prevPath) { foreach (Thing t in mentalModel.Children) { var refs = t.GetRelationshipsByType(null, null); foreach (Link ref1 in refs) { t.RemoveReference(ref1.T); } } prevPath = curPath; } //if you want the dlg to update, use the following code whenever any parameter changes // UpdateDialog(); }
private void AddChildren(Thing t, TreeViewItem tvi, int depth, string parentLabel) { if (totalItemCount > 3000) { return; } IList <Thing> theChildren; if ((bool)checkBoxSort.IsChecked) { theChildren = t.Children.OrderByDescending(x => x.useCount).ToList(); } else { theChildren = t.Children; } for (int i = 0; i < theChildren.Count; i++) { Thing child = theChildren[i]; string header = child.Label + ":" + child.useCount; if (child.References.Count > 0) { header += " ("; ModuleUKS parent = (ModuleUKS)base.ParentModule; Thing best = parent.FindBestReference(child); List <Link> refs = child.References.OrderBy(x => - x.Value1).ToList(); for (int j = 0; j < refs.Count; j++)// child.References.Count; j++)// each (Link L in child.References) { if (header.Length - header.LastIndexOf('\n') > charsPerLine - 7 * depth) { header += "\n"; } Link l = refs[j];// child.References[j]; if (l is Relationship r) { header += r.relationshipType.Label + "->" + r.T.Label + ", "; } else { if (l.T == best) { header += "*"; } if (l.weight < 0) { header += "-"; } header += l.T.Label + ", "; } } //header = header.Substring(0, header.Length - 2); header += ")"; } if (child.V != null) { if (child.V is int iVal) { header += " : " + iVal.ToString("X"); } else { header += " : " + child.V.ToString(); } } TreeViewItem tviChild = new TreeViewItem { Header = header }; if (child.lastFired == MainWindow.theNeuronArray.Generation) { tviChild.Background = new SolidColorBrush(Colors.LightGreen); } if (expandedItems.Contains("|" + parentLabel + "|" + LeftOfColon(header))) { tviChild.IsExpanded = true; } tvi.Items.Add(tviChild); totalItemCount++; tviChild.MouseRightButtonDown += Tvi_MouseRightButtonDown; if (depth < maxDepth) { AddChildren(child, tviChild, depth + 1, parentLabel + "|" + child.Label); AddReferences(child, tviChild, parentLabel); AddReferencedBy(child, tviChild, parentLabel); } } }
public override bool Draw(bool checkDrawTimer) { if (!base.Draw(checkDrawTimer)) { return(false); } //this has a timer so that no matter how often you might call draw, the dialog //only updates 10x per second ModuleAttention parent = (ModuleAttention)base.ParentModule; theCanvas.Children.Clear(); Point windowSize = new Point(theCanvas.ActualWidth, theCanvas.ActualHeight); ModuleView naSource = MainWindow.theNeuronArray.FindModuleByLabel("UKS"); if (naSource == null) { return(false); } uks = (ModuleUKS)naSource.TheModule; Thing mentalModel = uks.GetOrAddThing("MentalModel", "Visual"); if (mentalModel == null || mentalModel.Children.Count == 0) { return(false); } Thing attn = uks.Labeled("ATTN"); if (attn == null || attn.References.Count == 0) { return(false); } Thing attnTarget = attn.GetReferenceWithAncestor(uks.Labeled("Visual")); var values = uks.GetValues(attnTarget); HSLColor c1 = new HSLColor(values["Hue+"], values["Sat+"], values["Lum+"]); Color fillColor = c1.ToColor(); try { double largest = 0; foreach (Thing area in mentalModel.Children) { var areaValues = uks.GetValues(area); Thing theShape = area.Children[0]; var shapeValues = uks.GetValues(theShape); PointPlus pOffset = new PointPlus(areaValues["CtrX+"] - shapeValues["CtrX+"], areaValues["CtrY+"] - shapeValues["CtrY+"]); foreach (Thing corner in theShape.Children) { Point p = (Point)corner.Children[0].V; p = p + pOffset; largest = Math.Max(largest, p.X); largest = Math.Max(largest, p.Y); } } largest += 10; //a little margin float scale = (float)Math.Min(windowSize.X, windowSize.Y) / (float)largest; if (scale == 0) { return(false); } theCanvas.Children.Clear(); foreach (Thing area in mentalModel.Children) { PointCollection pts = new PointCollection(); var areaValues = uks.GetValues(area); Thing theShape = area.Children[0]; var shapeValues = uks.GetValues(theShape); //These corrections are needed because known objects are stored at the location and size when they were first seen //now the viewed object will have a different size and location PointPlus pAreaCtr = new PointPlus(areaValues["CtrX+"], areaValues["CtrY+"]); PointPlus pShapeCtr = new PointPlus(shapeValues["CtrX+"], shapeValues["CtrY+"]); float areaSize = areaValues["Siz+"]; float shapeSize = shapeValues["Siz+"]; Angle areaAngle = areaValues["Ang+"]; Angle shapeAngle = shapeValues["Ang+"]; Angle rotation = areaAngle - shapeAngle; foreach (Thing corner in theShape.Children) { PointPlus p = new PointPlus((Point)corner.Children[0].V); p = p - pShapeCtr; p.Theta += rotation; float ratio = areaSize / shapeSize; p.X *= ratio; p.Y *= ratio; p = p + pAreaCtr; p.X *= scale; p.Y *= scale; pts.Add(p); } Polygon poly = new Polygon { Points = pts, Stroke = new SolidColorBrush(Colors.AliceBlue) }; poly.ToolTip = area.Label; poly.Fill = this.Background; if (attnTarget == area) { poly.Fill = new SolidColorBrush(fillColor); poly.Stroke = new SolidColorBrush(fillColor); poly.Fill.Opacity = 1; } poly.MouseDown += Poly_MouseDown; poly.SetValue(AttentionObjectProperty, area); theCanvas.Children.Add(poly); } } #pragma warning disable 168 catch (Exception e) //sometimes useful for debugging #pragma warning restore 168 { } return(true); }
private void AddChildren(Thing t, TreeViewItem tvi, int depth) { List <Thing> theChildren; if ((bool)checkBoxSort.IsChecked) { theChildren = t.Children.OrderByDescending(x => x.useCount).ToList(); } else { theChildren = t.Children; } for (int i = 0; i < theChildren.Count; i++) { Thing child = theChildren[i]; string header = child.Label + ":" + child.useCount; if (child.References.Count > 0) { header += " ("; ModuleUKS parent = (ModuleUKS)base.ParentModule; Thing best = parent.FindBestReference(child); foreach (Link L in child.References) { if (L.T == best) { header += "*"; } if (L.weight < 0) { header += "-"; } header += L.T.Label + ", "; } header = header.Substring(0, header.Length - 2); header += ")"; } if (child.V != null) { if (child.V is int iVal) { header += " : " + iVal.ToString("X"); } else { header += " : " + child.V.ToString(); } } TreeViewItem tviChild = new TreeViewItem { Header = header }; if (expandedItems.Contains(LeftOfColon(header))) { tviChild.IsExpanded = true; } tvi.Items.Add(tviChild); tviChild.MouseRightButtonDown += Tvi_MouseRightButtonDown; if (depth < maxDepth) { AddChildren(child, tviChild, depth + 1); AddReferences(child, tviChild); AddReferencedBy(child, tviChild); } } }
public override bool Draw(bool checkDrawTimer) { if (busy) { return(false); } //the datagridview is so slow, always wait for the timer if (!base.Draw(checkDrawTimer)) { return(false); } if (mouseInWindow) { return(true); } ModuleView naSource = MainWindow.theNeuronArray.FindModuleByLabel("UKS"); if (naSource == null) { return(false); } uks = (ModuleUKS)naSource.TheModule; Thing mentalModel = uks.GetOrAddThing("MentalModel", "Visual"); if (mentalModel == null || mentalModel.Children.Count == 0) { return(false); } Thing attn = uks.Labeled("ATTN"); if (attn == null || attn.References.Count == 0) { return(false); } busy = true; Thing attnTarget = attn.GetReferenceWithAncestor(uks.Labeled("Visual")); DataTable dt = new DataTable(); dt.Columns.Add("*", typeof(string)); IList <Thing> words = uks.Labeled("Word").Children; foreach (Thing child in words) { dt.Columns.Add(child.Label, typeof(string)); } List <Thing> properties = uks.Labeled("Color").Children.ToList(); properties.AddRange(uks.Labeled("Area").Children.ToList()); //properties = properties.OrderBy(x => x.Label).ToList(); IList <Thing> relationships = uks.Labeled("Relationship").Children; //relationships = relationships.OrderBy(x => x.Label.Substring(1)).ToList(); //collect all the values in a single spot float[,] values = ((ModuleAssociation)base.ParentModule).GetAssociations(); int row = 0; foreach (Thing property in properties) { DataRow dr = dt.NewRow(); dr[0] = property.Label; List <Link> refs = property.GetReferencedByWithAncestor(uks.Labeled("Word")); for (int i = 0; i < words.Count; i++) { Link l = words[i].HasReference(property); if (l != null) { if (cbRawValues.IsChecked == true) { dr[i + 1] = l.hits + "/" + l.misses; } else { dr[i + 1] = l.Value1.ToString("f2"); } } } dt.Rows.Add(dr); row++; } foreach (Thing relationship in relationships) { DataRow dr = dt.NewRow(); dr[0] = relationship.Label; List <Link> refs = relationship.GetReferencedByWithAncestor(uks.Labeled("Word")); for (int i = 0; i < words.Count; i++) { Link l = words[i].HasReference(relationship); if (l != null) { if (cbRawValues.IsChecked == true) { dr[i + 1] = l.hits + "/" + l.misses; } else { dr[i + 1] = l.Value1.ToString("f2"); } } } dt.Rows.Add(dr); row++; } theGrid.ItemsSource = dt.DefaultView; theGrid.SelectionUnit = DataGridSelectionUnit.Cell; float[] maxInRow = new float[values.GetLength(0)]; float[] maxInCol = new float[values.GetLength(1)]; for (int i = 0; i < values.GetLength(0); i++) { for (int j = 0; j < values.GetLength(1); j++) { if (values[i, j] > maxInRow[i]) { maxInRow[i] = values[i, j]; } } } for (int j = 0; j < values.GetLength(1); j++) { for (int i = 0; i < values.GetLength(0); i++) { if (values[i, j] > maxInCol[j]) { maxInCol[j] = values[i, j]; } } } //set the background colors of the significant elements for (int j = 0; j < values.GetLength(1); j++) { for (int i = 0; i < values.GetLength(0); i++) { if (values[i, j] == maxInRow[i] && values[i, j] == maxInCol[j]) { SetCellBackground(i, j + 1, Colors.LightGreen); } else if (values[i, j] == maxInRow[i]) { SetCellBackground(i, j + 1, Colors.LightGoldenrodYellow); } else if (values[i, j] == maxInCol[j]) { SetCellBackground(i, j + 1, Colors.Yellow); } else { SetCellBackground(i, j + 1, Colors.White); } } } theGrid.FrozenColumnCount = 1; busy = false; return(true); }
private void Button_Click(object sender, RoutedEventArgs e) { //figure out how wide the treeview is so we can wrap the text var typeFace = new System.Windows.Media.Typeface(theTreeView.FontFamily.ToString()); var ft = new FormattedText("xxxxxxxxxx", System.Globalization. CultureInfo.CurrentCulture, theTreeView.FlowDirection, typeFace, theTreeView.FontSize, theTreeView.Foreground, VisualTreeHelper.GetDpi(this).PixelsPerDip); charsPerLine = (int)(10 * theTreeView.ActualWidth / ft.Width); charsPerLine -= 10; //leave a little margin...the indent is calculated for individual entries try { string root = textBox1.Text; ModuleUKS parent = (ModuleUKS)base.ParentModule; if (!updateFailed) { expandedItems.Clear(); FindExpandedItems(theTreeView.Items, ""); } updateFailed = false; theTreeView.Items.Clear(); int childCount = 0; int refCount = 0; List <Thing> uks = parent.GetTheUKS(); foreach (Thing t1 in uks) { childCount += t1.Children.Count; refCount += t1.References.Count; } statusLabel.Content = uks.Count + " Nodes " + (childCount + refCount) + " Edges"; Thing t = parent.Labeled(root); if (t != null) { totalItemCount = 0; TreeViewItem tvi = new TreeViewItem { Header = t.Label }; tvi.IsExpanded = true; //always expand the top-level item theTreeView.Items.Add(tvi); totalItemCount++; tvi.MouseRightButtonDown += Tvi_MouseRightButtonDown; AddChildren(t, tvi, 0, t.Label); } else if (root == "") //search for unattached Things { try //ignore problems of collection modified { foreach (Thing t1 in uks) { if (t1.Parents.Count == 0) { TreeViewItem tvi = new TreeViewItem { Header = t1.Label }; theTreeView.Items.Add(tvi); } } } catch { updateFailed = true; } } } catch { updateFailed = true; } }
//fill this method in with code which will execute //once for each cycle of the engine public override void Fire() { Init(); //be sure to leave this here ModuleView naSource = theNeuronArray.FindModuleByLabel("UKS"); if (naSource == null) { return; } uks = (ModuleUKS)naSource.TheModule; ModuleView attnSource = theNeuronArray.FindModuleByLabel("Attention"); if (attnSource == null) { return; } ModuleAttention m = (ModuleAttention)attnSource.TheModule; Thing mentalModelParent = uks.GetOrAddThing("MentalModel", "Visual"); Thing wordParent = uks.GetOrAddThing("Word", "Audible"); Thing attn = uks.GetOrAddThing("ATTN", "Thing"); if (words.Count > 0) { if (currentWord != "") { attn.RemoveReferencesWithAncestor(wordParent); } currentWord = words[0]; //is this a reference to a visible object? does it contain a digit? if (words[0].IndexOfAny(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }) != -1) { Thing t = uks.Labeled(words[0], mentalModelParent.Children); if (t != null) { if (naSource != null) { m.SetAttention(t, -1); } } } else { //process a single word & add it to the current phrase currentWord = char.ToUpper(currentWord[0]) + currentWord.Substring(1); Thing theWord = uks.GetOrAddThing("w" + currentWord, wordParent); attn.AddReference(theWord); } //delete the word from the top of the list words.RemoveAt(0); } else { if (attn.HasReferenceWithParent(wordParent) != null) { attn.RemoveReferencesWithAncestor(wordParent); m.SetAttention(null, 0); } } InnerMonologue(); //if you want the dlg to update, use the following code whenever any parameter changes UpdateDialog(); }
public Thing GetBestAssociation(Thing t) { if (uks == null) { ModuleView naSource = theNeuronArray.FindModuleByLabel("UKS"); if (naSource == null) { return(null); } uks = (ModuleUKS)naSource.TheModule; } IList <Thing> words = uks.Labeled("Word").Children; List <Thing> properties = uks.Labeled("Color").Children.ToList(); properties.AddRange(uks.Labeled("Area").Children.ToList()); properties = properties.OrderBy(x => x.Label).ToList(); IList <Thing> relationships = uks.Labeled("Relationship").Children; relationships = relationships.OrderBy(x => x.Label.Substring(1)).ToList(); float[,] values = GetAssociations(); int row = -1; row = properties.IndexOf(t); if (row == -1) { row = relationships.IndexOf(t); if (row != -1) { row += properties.Count; } } if (row == -1) { return(null); } float max = 0; int bestCol = -1; Thing best = null; for (int i = 0; i < values.GetLength(1); i++) { if (values[row, i] > max) { max = values[row, i]; best = words[i]; bestCol = i; } } if (bestCol != -1) { for (int i = 0; i < values.GetLength(0); i++) { if (values[i, bestCol] > max) { return(null); } } } return(best); }