trinity-rdf icon indicating copy to clipboard operation
trinity-rdf copied to clipboard

Include behavior

Open IgorKaplya opened this issue 3 years ago • 3 comments

Good day @all, I need to get individuals with a couple of nested properties from a SPARQL endpoint. This code produces an enormous amount of queries (1 + 41 * 3) which tears the productivity down to 20+ seconds for 41 individuals of ExecutableExercise.

        private static void RealLinqTest()
        {
            // Create a temporary memory store.
            IStore store = StoreFactory.CreateSparqlEndpointStore(new Uri("..."));

            // Uncomment to log all executed queries to the console.
            store.Log = (query) => Console.WriteLine(query);

            // A model is where we collect resources logically belonging together
            var model = store.GetModel(new Uri("http://example.com/model"));

            // SPARQL Query [0]
            var executableExercises = model
                .AsQueryable<ExecutableExercise>()
                .Take(50)
                .ToList(); // without ToList() I get an exception on the next line :(
            Console.WriteLine($"------Executables count : {executableExercises.Count()}");

            foreach (var executableExercise in executableExercises)
            {
                // No SPARQL Query
                Console.WriteLine($"------Executable: {executableExercise.Uri}");
                // SPARQL Query [1]
                Console.WriteLine($"------Method: {executableExercise.Method.Uri}");
                // No SPARQL Query 
                Console.WriteLine($"------------Execution: {executableExercise.Method.ExecutionString}");
                // SPARQL Query [2]
                Console.WriteLine($"------Equipment: {executableExercise.Equipment.Uri}");
                // SPARQL Query [3]
                Console.WriteLine($"------Exercise: {executableExercise.Exercise.Uri}");
            }

            Console.WriteLine();
            Console.WriteLine("Press ANY key to close..");
            Console.ReadLine();
        }
    }

Is there a way to improve performance? Like, include or eager loading in EF?

Models
   // SCHEMA is a custom ontology consistent and coherent (according to Protege )
  
    [RdfClass(SCHEMA.Exercise)]
    public class Exercise : Resource
    {
        [RdfProperty(RDFS.label), NotifyPropertyChanged]
        public string Label { get; set; }

        public Exercise(Uri uri) : base(uri)
        {
        }
    }

    [RdfClass(SCHEMA.ExecutableExercise)]
    public class ExecutableExercise: Resource
    {
        [RdfProperty(SCHEMA.usesEquipment), NotifyPropertyChanged] 
        public Equipment Equipment { get; set; }
        
        [RdfProperty(SCHEMA.usesExercise)] 
        public Exercise Exercise { get; set; }

        [RdfProperty(SCHEMA.usesMethod)]
        public Method Method { get; set; }

        public ExecutableExercise(Uri uri) : base(uri)
        {
        }
    }

    [RdfClass(SCHEMA.Equipment)]
    public class Equipment : Resource
    {
        [RdfProperty(RDFS.label), NotifyPropertyChanged]
        public string Label { get; set; }

        public Equipment(Uri uri) : base(uri)
        {
        }
    }

    [RdfClass(SCHEMA.Method)]
    public class Method : Resource
    {
        [RdfProperty(SCHEMA.hasExecutionString), NotifyPropertyChanged]
        public string ExecutionString { get; set; }

        public Method(Uri uri) : base(uri)
        {
        }
    }

IgorKaplya avatar Oct 22 '21 18:10 IgorKaplya