mdesalvo / RDFSharp

Lightweight and friendly .NET library for realizing Semantic Web applications
Apache License 2.0
121 stars 26 forks source link

Checking RDFOntologyData #186

Closed constnick closed 3 years ago

constnick commented 3 years ago

Hi Marco,

imagine we have:

RDFOntology fullContextOntology; // represents the "world"
RDFOntologyData situationData; // represents the "situation"

What is the best way to check whether the "situation" actually exists within the "world"? (I know it can be checked via sparql query, but it's not convenient in my case.)

Possible solution:

RDFOntologyData dataIntersection = fullContextOntology.Data.IntersectWith(situationData);
RDFOntologyData dataDiff = situationData.DifferenceWith(dataIntersection);
// situation exists if situationData and dataIntersection are equal by content, i.e. dataDiff must be empty:
bool situationExists = dataDiff.FactsCount == 0  
                    && dataDiff.LiteralsCount == 0 
                    && dataDiff.Relations.ClassType.EntriesCount == 0
                    && dataDiff.Relations.SameAs.EntriesCount == 0
                    && dataDiff.Relations.DifferentFrom.EntriesCount == 0
                    && dataDiff.Relations.Assertions.EntriesCount == 0
                    && dataDiff.Relations.NegativeAssertions.EntriesCount == 0;

Is it correct and the most efficient way (also in terms of performance)? It looks a bit bulky.

mdesalvo commented 3 years ago

Hi, at first I had to draw a Venn diagram to depict this use case :) What you do is correct, I suggest you can optimize by directly checking situationData against fullContextOntology.Data:

RDFOntologyData dataDiff = situationData.DifferenceWith(fullContextOntology.Data);

// situation exists if situationData is entirely contained in fullContextOntology.Data (no differences remain):
bool situationExists = dataDiff.FactsCount == 0  
                    && dataDiff.LiteralsCount == 0 
                    && dataDiff.Relations.ClassType.EntriesCount == 0
                    && dataDiff.Relations.SameAs.EntriesCount == 0
                    && dataDiff.Relations.DifferentFrom.EntriesCount == 0
                    && dataDiff.Relations.Assertions.EntriesCount == 0
                    && dataDiff.Relations.NegativeAssertions.EntriesCount == 0;
constnick commented 3 years ago

OK, thanks!