示例#1
0
        protected override void onInstanceActivated(ConceptInstance instance, BeamGenerator generator)
        {
            var subject             = generator.GetValue(instance, Concept2.Subject);
            var property            = generator.GetValue(instance, Concept2.Property);
            var inheritedProperties = generator.GetPropertyValues(subject, includeInstanceProps: false);

            var value = generator.GetValue(subject, property.Concept);

            if (value == null)
            {
                var refutation  = new ConceptInstance(Concept2.KnowledgeRefutation);
                var refutedInfo = new ConceptInstance(subject.Concept);
                generator.SetValue(refutedInfo, property.Concept, new ConceptInstance(Concept2.Something));
                generator.SetValue(refutation, Concept2.Subject, refutedInfo);
                generator.SetValue(refutation, Concept2.Property, property);
                generator.Push(new InstanceOutputEvent(refutation));
            }
            else
            {
                var confirmation  = new ConceptInstance(Concept2.KnowledgeConfirmed);
                var confirmedInfo = new ConceptInstance(subject.Concept);

                generator.SetValue(confirmedInfo, property.Concept, value);
                generator.SetValue(confirmation, Concept2.Subject, confirmedInfo);
                generator.Push(new InstanceOutputEvent(confirmation));
            }
        }
示例#2
0
        internal static List <ConceptInstance> FindRelevantConcepts(BeamGenerator generator, HashSet <Concept2> requiredProperties)
        {
            //TODO better matching logic
            var concepts = generator.GetDefinedConcepts();
            var result   = new List <ConceptInstance>();

            foreach (var concept in concepts)
            {
                var conceptInstance   = new ConceptInstance(concept);
                var missingProperties = new HashSet <Concept2>(requiredProperties);

                foreach (var propertyValue in generator.GetPropertyValues(conceptInstance))
                {
                    var property = propertyValue.Key;
                    var value    = propertyValue.Value;

                    missingProperties.Remove(value.Concept);
                }

                if (missingProperties.Count == 0)
                {
                    result.Add(conceptInstance);
                }
            }

            return(result);
        }
示例#3
0
        protected string singularWithProperty(ConceptInstance instance)
        {
            var properties = _generator.GetPropertyValues(instance, includeInheritedProps: false);

            foreach (var property in properties.Keys.ToArray())
            {
                var value = properties[property];
                if (value.Concept == Concept2.Something)
                {
                    continue;
                }

                //properties.Remove(property);
            }

            if (properties.Count == 0)
            {
                return(instance.ToPrintable());
            }

            var reportedProperty = properties.Keys.First();
            var reportedValue    = properties[reportedProperty];
            var propertyName     = reportedProperty.Name;

            if (reportedValue.Concept == Concept2.Something)
            {
                return(instance.ToPrintable() + " " + propertyName);
            }
            else
            {
                return(instance.ToPrintable() + " " + propertyName + " is " + reportedValue.ToPrintable());
            }
        }
示例#4
0
        protected override void onInstanceActivated(ConceptInstance instance, BeamGenerator generator)
        {
            var subject                = generator.GetValue(instance, Concept2.Subject);
            var inheritedProperties    = generator.GetPropertyValues(subject, includeInstanceProps: false);
            var pureInstanceProperties = generator.GetPropertyValues(subject, includeInheritedProps: false);

            if (!pureInstanceProperties.Any())
            {
                var confirmation = new ConceptInstance(Concept2.KnowledgeConfirmed);
                generator.SetValue(confirmation, Concept2.Subject, subject);
                generator.Push(new InstanceOutputEvent(confirmation));
                return;
            }

            var unknown = new List <KeyValuePair <Concept2, ConceptInstance> >();

            foreach (var propertyValue in pureInstanceProperties)
            {
                if (!inheritedProperties.TryGetValue(propertyValue.Key, out var knownValue) || knownValue.Concept != propertyValue.Value.Concept)
                {
                    unknown.Add(propertyValue);
                }
            }

            if (unknown.Count == 0)
            {
                var confirmation2 = new ConceptInstance(Concept2.KnowledgeConfirmed);
                generator.SetValue(confirmation2, Concept2.Subject, subject);
                generator.Push(new InstanceOutputEvent(confirmation2));
                return;
            }

            var unknownReport = new ConceptInstance(subject.Concept);

            foreach (var propertyValue in unknown)
            {
                generator.SetValue(unknownReport, propertyValue.Key, propertyValue.Value);
            }

            var confirmation3 = new ConceptInstance(Concept2.KnowledgeRefutation);

            generator.SetValue(confirmation3, Concept2.Subject, subject);
            generator.Push(new InstanceOutputEvent(unknownReport));
        }
示例#5
0
        protected override void onInstanceActivated(ConceptInstance instance, BeamGenerator generator)
        {
            var criterion       = generator.GetValue(instance, _parameter);
            var criterionValues = generator.GetPropertyValues(criterion);

            criterionValues.Remove(Concept2.OnSetListener); // TODO internal property removal should be done in more systematic way
            criterionValues.Remove(Concept2.HasProperty);
            criterionValues.Remove(Concept2.HasPropertyValue);

            var requiredProperties = new HashSet <Concept2>(criterionValues.Values.Select(i => i.Concept));

            requiredProperties.Add(criterion.Concept);

            var result         = FindRelevantConcepts(generator, requiredProperties);
            var isSubjectClass = generator.GetInverseConceptValues(Concept2.InstanceOf, criterion).Any();


            if (result.Count == 0)
            {
                if (isSubjectClass)
                {
                    generator.Push(new StaticScoreEvent(0.1));
                }
                else
                {
                    generator.Push(new StaticScoreEvent(-0.1));
                }

                generator.Push(new InstanceOutputEvent(new ConceptInstance(Concept2.NotFound)));
            }
            else if (result.Count == 1)
            {
                generator.Push(new StaticScoreEvent(0.5));
                generator.Push(new InstanceOutputEvent(result.First()));
            }
            else
            {
                if (generator.IsProperty(criterion.Concept))
                {
                    generator.Push(new StaticScoreEvent(-0.15));
                }

                var needRefinementInstance = new ConceptInstance(Concept2.NeedsRefinement);
                generator.SetValue(needRefinementInstance, Concept2.Subject, criterion);
                generator.SetValue(criterion, Concept2.OnSetListener, instance);
                generator.Push(new InstanceOutputEvent(needRefinementInstance));
            }
        }
示例#6
0
        private IEnumerable <ConceptInstance> getRelevantInstances(ConceptInstance instance, Concept2 relevanceCriterion, BeamGenerator beam)
        {
            var relevantCandidates = beam.GetInstances();

            foreach (var relevantCandidate in relevantCandidates)
            {
                if (relevantCandidate == instance)
                {
                    // prevent self reference
                    continue;
                }

                var values     = beam.GetPropertyValues(relevantCandidate);
                var isRelevant = relevantCandidate.Concept == relevanceCriterion || values.Any(v => v.Key == relevanceCriterion || v.Value.Concept == relevanceCriterion);

                if (isRelevant)
                {
                    yield return(relevantCandidate);
                }
            }
        }
示例#7
0
        private Dictionary <Concept2, HashSet <Concept2> > getRelevantProperties(ConceptInstance targetInstance, BeamGenerator generator)
        {
            var properties      = new Dictionary <Concept2, HashSet <Concept2> >();
            var definedConcepts = new HashSet <Concept2>(generator.GetDefinedConcepts());

            foreach (var conceptCandidate in definedConcepts)
            {
                var conceptCandidateInstance = new ConceptInstance(conceptCandidate);

                var instanceOf = generator.GetValue(conceptCandidateInstance, Concept2.InstanceOf);
                if (conceptCandidate != targetInstance.Concept && instanceOf?.Concept != targetInstance.Concept)
                {
                    continue;
                }

                var instanceProperties = generator.GetPropertyValues(conceptCandidateInstance);
                foreach (var instanceProperty in instanceProperties)
                {
                    if (!definedConcepts.Contains(instanceProperty.Value.Concept))
                    {
                        continue;
                    }

                    if (!definedConcepts.Contains(instanceProperty.Key))
                    {
                        continue;
                    }

                    if (!properties.TryGetValue(instanceProperty.Key, out var propertyValues))
                    {
                        properties[instanceProperty.Key] = propertyValues = new HashSet <Concept2>();
                    }

                    propertyValues.Add(instanceProperty.Value.Concept);
                }
            }

            return(properties);
        }
示例#8
0
        private List <ConceptInstance> getAnswer(ConceptInstance subject, Concept2 property, BeamGenerator generator)
        {
            var result = new List <ConceptInstance>();

            var directValue = generator.GetValue(subject, property);

            if (directValue != null)
            {
                result.Add(directValue);
                return(result);
            }

            var criterionValues = generator.GetPropertyValues(subject);

            criterionValues.Remove(Concept2.OnSetListener); // TODO internal property removal should be done in more systematic way


            var requiredProperties = new HashSet <Concept2>(criterionValues.Values.Select(i => i.Concept));

            requiredProperties.Add(subject.Concept);

            var relevantConcepts = FindProvider.FindRelevantConcepts(generator, requiredProperties);

            foreach (var concept in relevantConcepts)
            {
                var searchedPropertyValue = generator.GetValue(concept, property);
                if (searchedPropertyValue == null)
                {
                    continue;
                }

                result.Add(searchedPropertyValue);
            }

            return(result);
        }