public void SelectTriplesBySubjectTest()
        {
            var testGraph = new RDFGraph(defaultTriples);
            var old       = new List <RDFTriple>()
            {
                defaultTriples[0]
            };
            var res     = testGraph.SelectTriplesBySubject(TestModelObject.subRes[0]);
            var newList = new List <RDFTriple>();

            foreach (var triple in res)
            {
                newList.Add(triple);
            }

            Assert.Equal(old, newList);
        }
Beispiel #2
0
        private void WorkingWithRdfModels()
        {
            // CREATE RESOURCE
            var donaldduck = new RDFResource("http://www.waltdisney.com/donald_duck");

            // CREATE BLANK RESOURCE
            var disney_group = new RDFResource();

            // CREATE PLAIN LITERAL
            // "Donald Duck"
            var donaldduck_name = new RDFPlainLiteral("Donald Duck");
            // CREATE PLAIN LITERAL WITH LANGUAGE TAG
            // "Donald Duck"@en-US
            var donaldduck_name_enusLiteral = new RDFPlainLiteral("Donald Duck", "en-US");
            // CREATE TYPED LITERAL
            // "85"^^xsd:integer
            var mickeymouse_age = new RDFTypedLiteral("85", RDFModelEnums.RDFDatatypes.XSD_INTEGER);


            // CREATE TRIPLES
            // "Mickey Mouse is 85 years old"
            RDFTriple mickeymouse_is85yr
                = new RDFTriple(
                      new RDFResource("http://www.waltdisney.com/mickey_mouse"),
                      new RDFResource("http://xmlns.com/foaf/0.1/age"),
                      new RDFTypedLiteral("85", RDFModelEnums.RDFDatatypes.XSD_INTEGER));

            // "Donald Duck has english (US) name "Donald Duck""
            RDFTriple donaldduck_name_enus = new RDFTriple(
                new RDFResource("http://www.waltdisney.com/donald_duck"),
                new RDFResource("http://xmlns.com/foaf/0.1/name"),
                new RDFPlainLiteral("Donald Duck", "en-US"));


            // CREATE EMPTY GRAPH
            var another_graph     = new RDFGraph();
            var waltdisney_filled = new RDFGraph();

            // CREATE GRAPH FROM A LIST OF TRIPLES
            var triples = new List <RDFTriple> {
                mickeymouse_is85yr, donaldduck_name_enus
            };
            var waltdisney = new RDFGraph(triples);

            // SET CONTEXT OF A GRAPH
            waltdisney.SetContext(new Uri("http://waltdisney.com/"));

            // GET A DATATABLE FROM A GRAPH
            var waltdisney_table = waltdisney.ToDataTable();
            // GET A GRAPH FROM A DATATABLE
            var waltdisney_newgraph = RDFGraph.FromDataTable(waltdisney_table);

            // ITERATE TRIPLES OF A GRAPH WITH FOREACH
            foreach (var t in waltdisney)
            {
                Console.WriteLine("Triple: " + t);
                Console.WriteLine(" Subject: " + t.Subject);
                Console.WriteLine(" Predicate: " + t.Predicate);
                Console.WriteLine(" Object: " + t.Object);
            }

            // ITERATE TRIPLES OF A GRAPH WITH ENUMERATOR
            var triplesEnum = waltdisney.TriplesEnumerator;

            while (triplesEnum.MoveNext())
            {
                Console.WriteLine("Triple: " + triplesEnum.Current);
                Console.WriteLine(" Subject: " + triplesEnum.Current.Subject);
                Console.WriteLine(" Predicate: " + triplesEnum.Current.Predicate);
                Console.WriteLine(" Object: " + triplesEnum.Current.Object);
            }

            // GET COUNT OF TRIPLES CONTAINED IN A GRAPH
            var triplesCount = waltdisney.TriplesCount;

            // MULTIPLE SELECTIONS
            var multiple_selections_graph =
                waltdisney.SelectTriplesBySubject(new RDFResource("http://www.waltdisney.com/donald_duck"))
                .SelectTriplesByPredicate(new RDFResource("http://xmlns.com/foaf/0.1/name"));

            // SET OPERATIONS
            var set_operations_graph = waltdisney.IntersectWith(waltdisney_filled).UnionWith(another_graph);



            /*
             * var ntriplesFormat = RDFModelEnums.RDFFormats.NTriples;
             * // READ N-TRIPLES FILE
             * var graph = RDFGraph.FromFile(ntriplesFormat, "C:\\file.nt");
             * // READ N-TRIPLES STREAM
             * var graph = RDFGraph.FromStream(ntriplesFormat, inStream);
             * // WRITE N-TRIPLES FILE
             * graph.ToFile(ntriplesFormat, "C:\\newfile.nt");
             * // WRITE N-TRIPLES STREAM
             * graph.ToStream(ntriplesFormat, outStream);
             */

            /*
             * var turtleFormat = RDFModelEnums.RDFFormats.Turtle;
             * // READ TURTLE FILE
             * var graph = RDFGraph.FromFile(turtleFormat, "C:\\file.ttl");
             * // READ TURTLE STREAM
             * var graph = RDFGraph.FromStream(turtleFormat, inStream);
             * // WRITE TURTLE FILE
             * graph.ToFile(turtleFormat, "C:\\newfile.ttl");
             * // WRITE TURTLE STREAM
             * graph.ToStream(turtleFormat, outStream);
             */

            /*
             * var xmlFormat = RDFModelEnums.RDFFormats.RdfXml;
             * // READ RDF/XML FILE
             * var graph = RDFGraph.FromFile(xmlFormat, "C:\\file.rdf");
             * // READ RDF/XML STREAM
             * var graph = RDFGraph.FromStream(xmlFormat, inStream);
             * // WRITE RDF/XML FILE
             * graph.ToFile(xmlFormat, "C:\\newfile.rdf");
             * // WRITE RDF/XML STREAM
             * graph.ToStream(xmlFormat, outStream);
             */

            // CREATE NAMESPACE
            var waltdisney_ns = new RDFNamespace("wd", "http://www.waltdisney.com/");

            // USE NAMESPACE IN RESOURCE CREATION
            var duckburg = new RDFResource(waltdisney_ns + "duckburg");
            var mouseton = new RDFResource(waltdisney_ns + "mouseton");


            RDFNamespaceRegister.AddNamespace(waltdisney_ns);

            // Retrieves a namespace by seeking presence of its prefix (null if not found). Supports prefix.cc
            var ns1 = RDFNamespaceRegister.GetByPrefix("dbpedia", false); //local search
            var ns2 = RDFNamespaceRegister.GetByPrefix("dbpedia", true);  //search prefix.cc service if no result

            // GET DEFAULT NAMESPACE
            var nSpace = RDFNamespaceRegister.DefaultNamespace;

            // SET DEFAULT NAMESPACE
            RDFNamespaceRegister.SetDefaultNamespace(waltdisney_ns); //new graphs will default to this context

            // ITERATE NAMESPACES OF REGISTER WITH FOREACH
            foreach (var ns in RDFNamespaceRegister.Instance)
            {
                Console.WriteLine("Prefix: " + ns.NamespacePrefix);
                Console.WriteLine("Namespace: " + ns.NamespaceUri);
            }

            // ITERATE NAMESPACES OF REGISTER WITH ENUMERATOR
            var nspacesEnum = RDFNamespaceRegister.NamespacesEnumerator;

            while (nspacesEnum.MoveNext())
            {
                Console.WriteLine("Prefix: " + nspacesEnum.Current.NamespacePrefix);
                Console.WriteLine("Namespace: " + nspacesEnum.Current.NamespaceUri);
            }


            // CREATE TRIPLES WITH VOCABULARY FACILITIES
            // "Goofy Goof is 82 years old"
            RDFTriple goofygoof_is82yr = new RDFTriple(
                new RDFResource(new Uri("http://www.waltdisney.com/goofy_goof").ToString()),
                RDFVocabulary.FOAF.AGE,
                new RDFPlainLiteral("82")
                );

            // "Donald Duck knows Goofy Goof"
            RDFTriple donaldduck_knows_goofygoof = new RDFTriple(
                new RDFResource(new Uri("http://www.waltdisney.com/donald_duck").ToString()),
                RDFVocabulary.FOAF.KNOWS,
                new RDFResource(new Uri("http://www.waltdisney.com/goofy_goof").ToString())
                );

            // CREATE TYPED LITERALS
            var myAge      = new RDFTypedLiteral("34", RDFModelEnums.RDFDatatypes.XSD_INT);
            var myDate     = new RDFTypedLiteral("2017-01-07", RDFModelEnums.RDFDatatypes.XSD_DATE);
            var myDateTime = new RDFTypedLiteral("2017-01-07T23:11:05", RDFModelEnums.RDFDatatypes.XSD_DATETIME);
            var myXml      = new RDFTypedLiteral("<book>title</book>", RDFModelEnums.RDFDatatypes.RDF_XMLLITERAL);
            var myLiteral  = new RDFTypedLiteral("generic literal", RDFModelEnums.RDFDatatypes.RDFS_LITERAL);

            /*
             * The given list of items may be incomplete.
             * A container is semantically opened to the possibility of having further elements
             *
             * Alt: unordered semantic, duplicates not allowed;
             * Bag: unordered semantic, duplicates allowed;
             * Seq: ordered semantic, duplicates allowed;
             */

            // CREATE CONTAINER AND ADD ITEMS
            RDFContainer beatles_cont = new RDFContainer(RDFModelEnums.RDFContainerTypes.Bag, RDFModelEnums.RDFItemTypes.Resource);

            beatles_cont.AddItem(new RDFResource("http://beatles.com/ringo_starr"));
            beatles_cont.AddItem(new RDFResource("http://beatles.com/john_lennon"));
            beatles_cont.AddItem(new RDFResource("http://beatles.com/paul_mc_cartney"));
            beatles_cont.AddItem(new RDFResource("http://beatles.com/george_harrison"));

            /*
             * The given list of items may not be incomplete.
             * A collection is semantically closed to the possibility of having further elements
             */

            // CREATE COLLECTION AND ADD ITEMS
            RDFCollection beatles_coll = new RDFCollection(RDFModelEnums.RDFItemTypes.Resource);

            beatles_coll.AddItem(new RDFResource("http://beatles.com/ringo_starr"));
            beatles_coll.AddItem(new RDFResource("http://beatles.com/john_lennon"));
            beatles_coll.AddItem(new RDFResource("http://beatles.com/paul_mc_cartney"));
            beatles_coll.AddItem(new RDFResource("http://beatles.com/george_harrison"));


            // ADD CONTAINER/COLLECTION TO GRAPH
            waltdisney.AddContainer(beatles_cont);
            waltdisney.AddCollection(beatles_coll);



            // REIFY TRIPLE AND MERGE IT INTO A GRAPH
            RDFGraph reifGraph = goofygoof_is82yr.ReifyTriple();

            waltdisney = waltdisney.UnionWith(reifGraph);

            // ASSERT SOMETHING ABOUT REIFIED TRIPLE
            waltdisney.AddTriple(new RDFTriple(
                                     new RDFResource("http://www.wikipedia.com/"),
                                     new RDFResource("http://example.org/verb_state"),
                                     goofygoof_is82yr.ReificationSubject
                                     ));


            var existingGraph = new RDFGraph();


            // REIFY CONTAINER
            existingGraph.AddContainer(beatles_cont);

            existingGraph.AddTriple(new RDFTriple(
                                        new RDFResource("http://www.thebeatles.com/"),
                                        RDFVocabulary.FOAF.GROUP,
                                        beatles_cont.ReificationSubject
                                        ));

            // REIFY COLLECTION
            existingGraph.AddCollection(beatles_coll);

            existingGraph.AddTriple(new RDFTriple(
                                        new RDFResource("http://www.thebeatles.com/"),
                                        RDFVocabulary.FOAF.GROUP,
                                        beatles_coll.ReificationSubject
                                        ));

            // WORKING WITH RDF STORES

            // CREATE CONTEXT FROM STRING
            var wdisney_ctx = new RDFContext("http://www.waltdisney.com/");
            // CREATE CONTEXT FROM URI
            var wdisney_ctx_uri = new RDFContext(new Uri("http://www.waltdisney.com/"));
            // CREATE DEFAULT CONTEXT (DEFAULT NAMESPACE)
            var wdisney_ctx_default = new RDFContext();

            // CREATE QUADRUPLES
            // "From Wikipedia.com: Mickey Mouse is 85 years old"
            RDFQuadruple wk_mickeymouse_is85yr = new RDFQuadruple(
                new RDFContext("http://www.wikipedia.com/"),
                new RDFResource("http://www.waltdisney.com/mickey_mouse"),
                RDFVocabulary.FOAF.AGE,
                new RDFTypedLiteral("85", RDFModelEnums.RDFDatatypes.XSD_INTEGER)
                );

            // "From WaltDisney.com: Mickey Mouse is 85 years old"
            RDFQuadruple wd_mickeymouse_is85yr = new RDFQuadruple(
                new RDFContext("http://www.waltdisney.com/"),
                new RDFResource("http://www.waltdisney.com/mickey_mouse"),
                RDFVocabulary.FOAF.AGE,
                new RDFTypedLiteral("85", RDFModelEnums.RDFDatatypes.XSD_INTEGER)
                );

            // "From Wikipedia.com: Donald Duck has english name "Donald Duck""
            RDFQuadruple wk_donald_duck_name_enus = new RDFQuadruple(
                new RDFContext("http://www.wikipedia.com/"),
                new RDFResource("http://www.waltdisney.com/donald_duck"),
                RDFVocabulary.FOAF.NAME,
                new RDFPlainLiteral("Donald Duck", "en")
                );

            // CREATE EMPTY MEMORY STORE
            var wdStore = new RDFMemoryStore();

            // CREATE MEMORY STORE FROM A LIST OF QUADRUPLES
            var quadruples = new List <RDFQuadruple> {
                wk_mickeymouse_is85yr, wk_mickeymouse_is85yr
            };
            var wdStoreFilled = new RDFMemoryStore();

            foreach (var q in quadruples)
            {
                wdStoreFilled.AddQuadruple(q);
            }

            // GET A DATATABLE FROM A MEMORY STORE (any kind of store can be exported to datatable)
            var wdStore_table = wdStoreFilled.ToDataTable();
            // GET A MEMORY STORE FROM A DATATABLE
            var wdStore_new = RDFMemoryStore.FromDataTable(wdStore_table);


            // ITERATE QUADRUPLES OF A MEMORY STORE WITH FOREACH
            foreach (var q in wdStore)
            {
                Console.WriteLine("Quadruple: " + q);
                Console.WriteLine(" Context: " + q.Context);
                Console.WriteLine(" Subject: " + q.Subject);
                Console.WriteLine(" Predicate: " + q.Predicate);
                Console.WriteLine(" Object: " + q.Object);
            }

            // ITERATE QUADRUPLES OF A MEMORY STORE WITH ENUMERATOR
            var quadruplesEnum = wdStore.QuadruplesEnumerator;

            while (quadruplesEnum.MoveNext())
            {
                Console.WriteLine("Quadruple: " + quadruplesEnum.Current);
                Console.WriteLine(" Context: " + quadruplesEnum.Current.Context);
                Console.WriteLine(" Subject: " + quadruplesEnum.Current.Subject);
                Console.WriteLine(" Predicate: " + quadruplesEnum.Current.Predicate);
                Console.WriteLine(" Object: " + quadruplesEnum.Current.Object);
            }

            var nquadsFormat = RDFStoreEnums.RDFFormats.NQuads;

            // READ N-QUADS FILE
            //var myStore = RDFMemoryStore.FromFile(nquadsFormat, "C:\\file.nq");
            // READ N-QUADS STREAM
            //var myStore = RDFMemoryStore.FromStream(nquadsFormat, inStream);
            // WRITE N-QUADS FILE
            wdStoreFilled.ToFile(nquadsFormat, @"C:\TEMP\newfile.nq");
            // WRITE N-QUADS STREAM
            //myStore.ToStream(nquadsFormat, outStream);


            var trixFormat = RDFStoreEnums.RDFFormats.TriX;

            // READ TRIX FILE
            //var memStore = RDFMemoryStore.FromFile(trixFormat, "C:\\file.trix");
            // READ TRIX STREAM
            //var memStore = RDFMemoryStore.FromStream(trixFormat, inStream);
            // WRITE TRIX FILE
            wdStoreFilled.ToFile(trixFormat, @"C:\TEMP\newfile.trix");
            // WRITE TRIX STREAM
            //myStore.ToStream(trixFormat, outStream);


            // CONNECT TO SQLSERVER STORE WITH CONNECTION STRING
            //var sqlServer = new RDFSQLServerStore(sqlServerConnectionString);


            // CREATE EMPTY FEDERATION
            var fed = new RDFFederation();

            /*
             * // CREATE FEDERATION FROM A LIST OF STORES
             * var stores = new List<RDFStore>{ waltDisneyStore, waltDisneyStoreFilled };
             * var fedFilled = new RDFFederation();
             * foreach(var store in stores)
             * {
             *  fedFilled.AddStore(store);
             * }
             */

            // ITERATE STORES OF A FEDERATION
            foreach (var s in fed)
            {
                Console.WriteLine("Store: " + s);
                Console.WriteLine(" Type: " + s.StoreType);
            }
        }
        /// <summary>
        /// Updates the previously detected restrictions to their specific types
        /// </summary>
        internal static void RefineRestrictions(RDFGraph ontGraph, RDFOntology ontology) {
            var changeLog      = new Dictionary<Int64, RDFOntologyClass>();
            foreach (var c in ontology.Model.ClassModel) {
                if (c.IsRestrictionClass()) {

                    #region CardinalityRestriction
                    Int32 minC = 0;
                    var crMin  = ontGraph.SelectTriplesBySubject((RDFResource)c.Value)
                                         .SelectTriplesByPredicate(RDFVocabulary.OWL.MIN_CARDINALITY)
                                         .FirstOrDefault();
                    if (crMin != null && crMin.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                        if (crMin.Object is RDFPlainLiteral) {
                            if (Regex.IsMatch(crMin.Object.ToString(), @"^[0-9]+$")) {
                                minC = Int32.Parse(crMin.Object.ToString());
                            }
                        }
                        else {
                            if (((RDFTypedLiteral)crMin.Object).Datatype.Category == RDFModelEnums.RDFDatatypeCategory.Numeric) {
                                if (Regex.IsMatch(((RDFTypedLiteral)crMin.Object).Value, @"^[0-9]+$")) {
                                    minC = Int32.Parse(((RDFTypedLiteral)crMin.Object).Value);
                                }
                            }
                        }
                    }

                    Int32 maxC = 0;
                    var crMax  = ontGraph.SelectTriplesBySubject((RDFResource)c.Value)
                                         .SelectTriplesByPredicate(RDFVocabulary.OWL.MAX_CARDINALITY)
                                         .FirstOrDefault();
                    if (crMax != null && crMax.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                        if (crMax.Object is RDFPlainLiteral) {
                            if (Regex.IsMatch(crMax.Object.ToString(), @"^[0-9]+$")) {
                                maxC = Int32.Parse(crMax.Object.ToString());
                            }
                        }
                        else {
                            if (((RDFTypedLiteral)crMax.Object).Datatype.Category == RDFModelEnums.RDFDatatypeCategory.Numeric) {
                                if (Regex.IsMatch(((RDFTypedLiteral)crMax.Object).Value, @"^[0-9]+$")) {
                                    maxC = Int32.Parse(((RDFTypedLiteral)crMax.Object).Value);
                                }
                            }
                        }
                    }

                    if ((minC > 0 || maxC > 0) && !changeLog.ContainsKey(c.PatternMemberID)) {
                        var restr                = new RDFOntologyCardinalityRestriction(((RDFOntologyRestriction)c).OnProperty, minC, maxC);
                        restr.Value              = c.Value;
                        restr.PatternMemberID    = c.PatternMemberID;
                        restr.DirectSubClasses   = c.DirectSubClasses;
                        restr.DirectSuperClasses = c.DirectSuperClasses;
                        restr.DisjointClasses    = c.DisjointClasses;
                        restr.EquivalentClasses  = c.EquivalentClasses;
                        changeLog.Add(restr.PatternMemberID, restr);
                    }
                    #endregion

                    #region HasValueRestriction
                    var hvRes      = ontGraph.SelectTriplesBySubject((RDFResource)c.Value)
                                             .SelectTriplesByPredicate(RDFVocabulary.OWL.HAS_VALUE)
                                             .FirstOrDefault();
                    if (hvRes     != null) {
                        var hvFct  = ontology.Data.SelectFact(hvRes.Object.ToString());
                        if (hvFct != null) {
                            if (!changeLog.ContainsKey(c.PatternMemberID)) {
                                var restr                = new RDFOntologyHasValueRestriction(((RDFOntologyRestriction)c).OnProperty, hvFct);
                                restr.Value              = c.Value;
                                restr.PatternMemberID    = c.PatternMemberID;
                                restr.DirectSubClasses   = c.DirectSubClasses;
                                restr.DirectSuperClasses = c.DirectSuperClasses;
                                restr.DisjointClasses    = c.DisjointClasses;
                                restr.EquivalentClasses  = c.EquivalentClasses;
                                changeLog.Add(restr.PatternMemberID, restr);
                            }
                        }
                        else {
                            throw new RDFSemanticsException("Cannot create hasValue restriction '" + c.Value + "' from graph, because its required fact '" + hvRes.Object + "' has not been found in the ontology data.");
                        }
                    }
                    #endregion

                    #region ValuesFromRestriction
                    var avfRes      = ontGraph.SelectTriplesBySubject((RDFResource)c.Value)
                                              .SelectTriplesByPredicate(RDFVocabulary.OWL.ALL_VALUES_FROM)
                                              .FirstOrDefault();
                    if (avfRes     != null) {
                        var avfCls  = ontology.Model.ClassModel.SelectClass(avfRes.Object.ToString());
                        if (avfCls != null) {
                            if (!changeLog.ContainsKey(c.PatternMemberID)) {
                                var restr                = new RDFOntologyValuesFromRestriction(((RDFOntologyRestriction)c).OnProperty, RDFSemanticsEnums.RDFOntologyValuesFromRestrictionCategory.AllValuesFrom, avfCls);
                                restr.Value              = c.Value;
                                restr.PatternMemberID    = c.PatternMemberID;
                                restr.DirectSubClasses   = c.DirectSubClasses;
                                restr.DirectSuperClasses = c.DirectSuperClasses;
                                restr.DisjointClasses    = c.DisjointClasses;
                                restr.EquivalentClasses  = c.EquivalentClasses;
                                changeLog.Add(restr.PatternMemberID, restr);
                            }
                        }
                        else {
                            throw new RDFSemanticsException("Cannot create allValuesFrom restriction '" + c.Value + "' from graph, because its fromClass '" + avfRes.Object + "' has not been found in the ontology model.");
                        }
                    }

                    var svfRes      = ontGraph.SelectTriplesBySubject((RDFResource)c.Value)
                                              .SelectTriplesByPredicate(RDFVocabulary.OWL.SOME_VALUES_FROM)
                                              .FirstOrDefault();
                    if (svfRes     != null) {
                        var svfCls  = ontology.Model.ClassModel.SelectClass(svfRes.Object.ToString());
                        if (svfCls != null) {
                            if (!changeLog.ContainsKey(c.PatternMemberID)) {
                                var restr                = new RDFOntologyValuesFromRestriction(((RDFOntologyRestriction)c).OnProperty, RDFSemanticsEnums.RDFOntologyValuesFromRestrictionCategory.SomeValuesFrom, svfCls);
                                restr.Value              = c.Value;
                                restr.PatternMemberID    = c.PatternMemberID;
                                restr.DirectSubClasses   = c.DirectSubClasses;
                                restr.DirectSuperClasses = c.DirectSuperClasses;
                                restr.DisjointClasses    = c.DisjointClasses;
                                restr.EquivalentClasses  = c.EquivalentClasses;
                                changeLog.Add(restr.PatternMemberID, restr);
                            }
                        }
                        else {
                            throw new RDFSemanticsException("Cannot create someValuesFrom restriction '" + c.Value + "' from graph, because its fromClass '" + svfRes.Object + "' has not been found in the ontology model.");
                        }
                    }
                    #endregion

                }
            }
            foreach (var cl in changeLog) {
                ontology.Model.ClassModel.Classes[cl.Key] = cl.Value;
            }
        }
Beispiel #4
0
        //source: http://dadev.cloudapp.net/Datos%20Abiertos/PDF/ReferenceGuide.pdf
        static void Main(string[] args)
        {
            //create resource from string
            RDFResource donaldduck = new RDFResource("http://www.waltdisney.com/donald_duck");

            //create resource from uri
            RDFResource goofygoof = new RDFResource(new Uri("http://www.waltdisney.com/goofy_goof").ToString());

            //create plain literal
            // "Donald Duck"
            RDFPlainLiteral donaldduck_name = new RDFPlainLiteral("Donald Duck");
            //create typed literal
            // "85"^^xsd:integer
            RDFTypedLiteral mickeymouse_age = new RDFTypedLiteral("85", RDFModelEnums.RDFDatatypes.XSD_INTEGER);


            //create triples
            // "Mickey Mouse is 85 years old"
            RDFTriple mickeymouse_is85yr = new RDFTriple(new RDFResource("http://www.waltdisney.com/mickey_mouse"),
                                                         new RDFResource("http://xmlns.com/foaf/0.1/age"),
                                                         mickeymouse_age);

            // "Donald Duck has English-US name "Donald Duck""
            RDFTriple donaldduck_name_enus_triple = new RDFTriple(donaldduck,
                                                                  new RDFResource("http://xmlns.com/foaf/0.1/name"),
                                                                  donaldduck_name);

            // "Goofy Goof is 82 years old"
            RDFTriple goofygoof_is82yr = new RDFTriple(goofygoof,
                                                       RDFVocabulary.FOAF.AGE,
                                                       new RDFPlainLiteral("82"));
            // "Donald Duck knows Goofy Goof"
            RDFTriple donaldduck_knows_goofygoof = new RDFTriple(donaldduck,
                                                                 RDFVocabulary.FOAF.KNOWS,
                                                                 goofygoof);

            //create graph from a list of triples
            List <RDFTriple> triples = new List <RDFTriple>
            {
                mickeymouse_is85yr,
                donaldduck_name_enus_triple,
                goofygoof_is82yr,
                donaldduck_knows_goofygoof
            };
            RDFGraph waltdisney = new RDFGraph(triples);

            //set context of a graph
            waltdisney.SetContext(new Uri("http://waltdisney.com/"));

            //iterate triples of a graph
            foreach (RDFTriple t in waltdisney)
            {
                Console.WriteLine($"Triple: {t}\n");
                Console.WriteLine($"Subject: {t.Subject}");
                Console.WriteLine($"Predicate: {t.Predicate}");
                Console.WriteLine($"Object: {t.Object}");
            }

            //compose multiple selections
            RDFGraph triples_by_subject_and_predicate =
                waltdisney.SelectTriplesBySubject(donaldduck)
                .SelectTriplesByPredicate(new RDFResource("http://xmlns.com/foaf/0.1/name"));

            Console.WriteLine("Number of triples where the subject is Donald Duck and the predicate is foaf:name: " + triples_by_subject_and_predicate.TriplesCount);
            Console.WriteLine();

            //create namespace
            RDFNamespace waltdisney_ns = new RDFNamespace("wd", "http://www.waltdisney.com/");

            //set default namespace
            RDFNamespaceRegister.SetDefaultNamespace(waltdisney_ns); //new graphs will default to this context

            //iterate namespaces
            foreach (RDFNamespace ns in RDFNamespaceRegister.Instance)
            {
                Console.WriteLine($"Prefix: {ns.NamespacePrefix}\n");
                Console.WriteLine($"Namespace: {ns.NamespaceUri}\n");
            }
            Console.ReadKey();
        }
        /// <summary>
        /// Loads the class taxonomies from the given graph into the given ontology
        /// </summary>
        internal static void LoadClassTaxonomies(RDFGraph ontGraph, RDFOntology ontology) {
            foreach (var c in ontology.Model.ClassModel) {

                #region Hierarchy
                foreach (var t in ontGraph.SelectTriplesBySubject((RDFResource)c.Value)
                                          .SelectTriplesByPredicate(RDFVocabulary.RDFS.SUB_CLASS_OF)) {

                    //Check conditions to accept the class
                    var superClass  = ontology.Model.ClassModel.SelectClass(t.Object.ToString());
                    if (superClass != null) {
                        c.AddSuperClass(superClass);
                    }
                    else {
                        throw new RDFSemanticsException("Cannot load taxonomy of class '" + c.Value + "', because its superclass '" + t.Object + "' has not been found in the ontology model.");
                    }

                }
                #endregion

                #region Equivalence
                foreach (var t in ontGraph.SelectTriplesBySubject((RDFResource)c.Value)
                                          .SelectTriplesByPredicate(RDFVocabulary.OWL.EQUIVALENT_CLASS)) {

                    //Check conditions to accept the class
                    var equivClass  = ontology.Model.ClassModel.SelectClass(t.Object.ToString());
                    if (equivClass != null) {
                        c.AddEquivalentClass(equivClass);
                    }
                    else {
                        throw new RDFSemanticsException("Cannot load taxonomy of class '" + c.Value + "', because its equivalent class '" + t.Object + "' has not been found in the ontology model.");
                    }

                }
                #endregion

                #region Disjointness
                foreach (var t in ontGraph.SelectTriplesBySubject((RDFResource)c.Value)
                                          .SelectTriplesByPredicate(RDFVocabulary.OWL.DISJOINT_WITH)) {

                    //Check conditions to accept the class
                    var disjClass  = ontology.Model.ClassModel.SelectClass(t.Object.ToString());
                    if (disjClass != null) {
                        c.AddDisjointClass(disjClass);
                    }
                    else {
                        throw new RDFSemanticsException("Cannot load taxonomy of class '" + c.Value + "', because its disjoint class '" + t.Object + "' has not been found in the ontology model.");
                    }

                }
                #endregion

            }
        }
        /// <summary>
        /// Loads the enumerate classes from the griven graph into the given ontology
        /// </summary>
        internal static void LoadEnumerates(RDFGraph ontGraph, RDFOntology ontology) {
            var changeLog = new Dictionary<Int64, RDFOntologyClass>();
            foreach (var c in ontology.Model.ClassModel) {

                #region Enumerate
                var onOf  = ontGraph.SelectTriplesBySubject((RDFResource)c.Value)
                                    .SelectTriplesByPredicate(RDFVocabulary.OWL.ONE_OF)
                                    .FirstOrDefault();
                if (onOf != null) {
                    var nilFound        = false;
                    var itemRest        = (RDFResource)onOf.Object;
                    while (!nilFound) {

                        #region rdf:first
                        var first       = ontGraph.SelectTriplesBySubject(itemRest)
                                                  .SelectTriplesByPredicate(RDFVocabulary.RDF.FIRST)
                                                  .FirstOrDefault();
                        if (first != null) {
                            var enMemb  = ontology.Data.SelectFact(first.Object.ToString());
                            if (enMemb != null) {
                                if (!changeLog.ContainsKey(c.PatternMemberID)) {
                                    var ontEClass             = new RDFOntologyEnumerateClass(enMemb.IsObjectFact() ?
                                                                    RDFSemanticsEnums.RDFOntologyEnumerateClassCategory.ResourceEnumeration :
                                                                    RDFSemanticsEnums.RDFOntologyEnumerateClassCategory.LiteralEnumeration);
                                    ontEClass.Value           = c.Value;
                                    ontEClass.PatternMemberID = c.PatternMemberID;
                                    ontEClass.AddEnumerateMember(enMemb);
                                    changeLog.Add(c.PatternMemberID, ontEClass);
                                }
                                else {
                                    if ((((RDFOntologyEnumerateClass)changeLog[c.PatternMemberID]).Category  == RDFSemanticsEnums.RDFOntologyEnumerateClassCategory.ResourceEnumeration && enMemb.IsObjectFact()) ||
                                         (((RDFOntologyEnumerateClass)changeLog[c.PatternMemberID]).Category == RDFSemanticsEnums.RDFOntologyEnumerateClassCategory.LiteralEnumeration  && enMemb.IsLiteralFact())) {
                                        ((RDFOntologyEnumerateClass)changeLog[c.PatternMemberID]).AddEnumerateMember(enMemb);
                                    }
                                }
                            }
                            else {
                                throw new RDFSemanticsException("Cannot load enumerate class '" + c.Value + "' from graph, because its enumerate fact member '" + first.Object + "' has not been found in the ontology data.");
                            }

                            #region rdf:rest
                            var rest    = ontGraph.SelectTriplesBySubject(itemRest)
                                                  .SelectTriplesByPredicate(RDFVocabulary.RDF.REST)
                                                  .FirstOrDefault();
                            if (rest != null) {
                                if (rest.Object.Equals(RDFVocabulary.RDF.NIL)) {
                                    nilFound = true;
                                }
                                else {
                                    itemRest = (RDFResource)rest.Object;
                                }
                            }
                            #endregion

                        }
                        #endregion

                    }
                }
                #endregion

            }
            foreach (var cl in changeLog) {
                ontology.Model.ClassModel.Classes[cl.Key] = cl.Value;
            }
        }
        /// <summary>
        /// Loads the restrictions from the given graph into the given ontology
        /// </summary>
        internal static void LoadRestrictions(RDFGraph ontGraph, RDFOntology ontology, RDFGraph rdfType) {
            foreach (var t in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.RESTRICTION)) {

                #region onProperty
                var p = ontGraph.SelectTriplesBySubject((RDFResource)t.Subject)
                                .SelectTriplesByPredicate(RDFVocabulary.OWL.ON_PROPERTY)
                                .FirstOrDefault();
                if (p != null) {
                    var onProp  = ontology.Model.PropertyModel.SelectProperty(p.Object.ToString());
                    if (onProp != null) {
                        var ontRestr             = new RDFOntologyRestriction(onProp);
                        ontRestr.Value           = t.Subject;
                        ontRestr.PatternMemberID = t.Subject.PatternMemberID;
                        ontology.Model.ClassModel.AddRestriction(ontRestr);
                    }
                    else {
                        throw new RDFSemanticsException("Cannot load restriction '" + t.Subject + "' from graph, because its applied property '" + p.Object + "' has not been found in the ontology model.");
                    }
                }
                #endregion

            }
        }
        /// <summary>
        /// Loads the composite classes from the given graph into the given ontology
        /// </summary>
        internal static void LoadComposites(RDFGraph ontGraph, RDFOntology ontology) {
            var changeLog = new Dictionary<Int64, RDFOntologyClass>();
            foreach (var c in ontology.Model.ClassModel) {

                #region Union
                var unOf  = ontGraph.SelectTriplesBySubject((RDFResource)c.Value)
                                    .SelectTriplesByPredicate(RDFVocabulary.OWL.UNION_OF)
                                    .FirstOrDefault();
                if(unOf  != null) {
                    if (unOf.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var nilFound       = false;
                        var itemRest       = (RDFResource)unOf.Object;
                        while (!nilFound)  {

                            #region rdf:first
                            var first      = ontGraph.SelectTriplesBySubject(itemRest)
                                                     .SelectTriplesByPredicate(RDFVocabulary.RDF.FIRST)
                                                     .FirstOrDefault();
                            if( first     != null) {
                                var compClass  = ontology.Model.ClassModel.SelectClass(first.Object.ToString());
                                if (compClass != null) {
                                    if (!changeLog.ContainsKey(c.PatternMemberID)) {
                                        var ontUClass             = new RDFOntologyUnionClass();
                                        ontUClass.Value           = c.Value;
                                        ontUClass.PatternMemberID = c.PatternMemberID;
                                        ontUClass.AddCompositingClass(compClass);
                                        changeLog.Add(c.PatternMemberID, ontUClass);
                                    }
                                    else {
                                        ((RDFOntologyUnionClass)changeLog[c.PatternMemberID]).AddCompositingClass(compClass);
                                    }
                                }
                                else {
                                    throw new RDFSemanticsException("Cannot load union class from graph, because its compositing class '" + first.Object +"' has not been found in the ontology model.");
                                }

                                #region rdf:rest
                                var rest   = ontGraph.SelectTriplesBySubject(itemRest)
                                                     .SelectTriplesByPredicate(RDFVocabulary.RDF.REST)
                                                     .FirstOrDefault();
                                if(rest   != null) {
                                    if (rest.Object.Equals(RDFVocabulary.RDF.NIL)) {
                                        nilFound = true;
                                    }
                                    else {
                                        itemRest = (RDFResource)rest.Object;
                                    }
                                }
                                #endregion

                            }
                            #endregion

                        }
                    }
                }
                #endregion

                #region Intersection
                var inOf  = ontGraph.SelectTriplesBySubject((RDFResource)c.Value)
                                    .SelectTriplesByPredicate(RDFVocabulary.OWL.INTERSECTION_OF)
                                    .FirstOrDefault();
                if (inOf != null) {
                    if (inOf.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var nilFound       = false;
                        var itemRest       = (RDFResource)inOf.Object;
                        while (!nilFound) {

                            #region rdf:first
                            var first      = ontGraph.SelectTriplesBySubject(itemRest)
                                                     .SelectTriplesByPredicate(RDFVocabulary.RDF.FIRST)
                                                     .FirstOrDefault();
                            if (first     != null) {
                                var compClass  = ontology.Model.ClassModel.SelectClass(first.Object.ToString());
                                if (compClass != null) {
                                    if (!changeLog.ContainsKey(c.PatternMemberID)) {
                                        var ontIClass             = new RDFOntologyIntersectionClass();
                                        ontIClass.Value           = c.Value;
                                        ontIClass.PatternMemberID = c.PatternMemberID;
                                        ontIClass.AddCompositingClass(compClass);
                                        changeLog.Add(c.PatternMemberID, ontIClass);
                                    }
                                    else {
                                        ((RDFOntologyIntersectionClass)changeLog[c.PatternMemberID]).AddCompositingClass(compClass);
                                    }
                                }
                                else {
                                    throw new RDFSemanticsException("Cannot load intersection class from graph, because its compositing class '" + first.Object + "' has not been found in the ontology model.");
                                }

                                #region rdf:rest
                                var rest   = ontGraph.SelectTriplesBySubject(itemRest)
                                                     .SelectTriplesByPredicate(RDFVocabulary.RDF.REST)
                                                     .FirstOrDefault();
                                if (rest  != null) {
                                    if (rest.Object.Equals(RDFVocabulary.RDF.NIL)) {
                                        nilFound = true;
                                    }
                                    else {
                                        itemRest = (RDFResource)rest.Object;
                                    }
                                }
                                #endregion

                            }
                            #endregion

                        }
                    }
                }
                #endregion

                #region Complement
                var cmOf  = ontGraph.SelectTriplesBySubject((RDFResource)c.Value)
                                    .SelectTriplesByPredicate(RDFVocabulary.OWL.COMPLEMENT_OF)
                                    .FirstOrDefault();
                if (cmOf != null) {
                    if (cmOf.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var compClass      = ontology.Model.ClassModel.SelectClass(cmOf.Object.ToString());
                        if (compClass     != null) {
                            if (!changeLog.ContainsKey(c.PatternMemberID)) {
                                var ontCClass             = new RDFOntologyComplementClass(compClass);
                                ontCClass.Value           = c.Value;
                                ontCClass.PatternMemberID = c.PatternMemberID;
                                changeLog.Add(c.PatternMemberID, ontCClass);
                            }
                        }
                        else {
                            throw new RDFSemanticsException("Cannot load complement class from graph, because its complemented class '" + cmOf.Object + "' has not been found in the ontology model.");
                        }
                    }
                }
                #endregion

            }
            foreach (var cl in changeLog) {
                ontology.Model.ClassModel.Classes[cl.Key] = cl.Value;
            }
        }
        /// <summary>
        /// Loads the property taxonomies from the given graph into the given ontology
        /// </summary>
        internal static void LoadPropertyTaxonomies(RDFGraph ontGraph, RDFOntology ontology) {
            foreach (var p  in ontology.Model.PropertyModel) {

                #region Hierarchy
                foreach (var t in ontGraph.SelectTriplesBySubject((RDFResource)p.Value)
                                          .SelectTriplesByPredicate(RDFVocabulary.RDFS.SUB_PROPERTY_OF)) {

                    //Check conditions to accept the property
                    var superProp  = ontology.Model.PropertyModel.SelectProperty(t.Object.ToString());
                    if (superProp != null) {
                        p.AddSuperProperty(superProp);
                    }
                    else {
                        throw new RDFSemanticsException("Cannot load taxonomy of property '" + p.Value + "', because its super property '" + t.Object + "' has not been found in the ontology model.");
                    }

                }
                #endregion

                #region Equivalence
                foreach (var t in ontGraph.SelectTriplesBySubject((RDFResource)p.Value)
                                          .SelectTriplesByPredicate(RDFVocabulary.OWL.EQUIVALENT_PROPERTY)) {

                    //Check conditions to accept the property
                    var equivProp  = ontology.Model.PropertyModel.SelectProperty(t.Object.ToString());
                    if (equivProp != null) {
                        p.AddEquivalentProperty(equivProp);
                    }
                    else {
                        throw new RDFSemanticsException("Cannot load taxonomy of property '" + p.Value + "', because its equivalent property '" + t.Object + "' has not been found in the ontology model.");
                    }

                }
                #endregion

                #region Inverse
                if (p.IsObjectProperty()) {
                    foreach (var t in ontGraph.SelectTriplesBySubject((RDFResource)p.Value)
                                              .SelectTriplesByPredicate(RDFVocabulary.OWL.INVERSE_OF)) {

                        //Check conditions to accept the property
                        var invProp  = ontology.Model.PropertyModel.SelectProperty(t.Object.ToString());
                        if (invProp != null) {
                            if (invProp.IsObjectProperty()) {
                                ((RDFOntologyObjectProperty)p).AddInverseProperty((RDFOntologyObjectProperty)invProp);
                            }
                            else {
                                throw new RDFSemanticsException("Cannot load taxonomy of property '" + p.Value + "', because its inverse property '" + t.Object + "' is not an object property.");
                            }
                        }
                        else {
                            throw new RDFSemanticsException("Cannot load taxonomy of property '" + p.Value + "', because its inverse property '" + t.Object + "' has not been found in the ontology model.");
                        }

                    }
                }
                #endregion

            }
        }
        internal static void LoadOntology(RDFGraph ontGraph, RDFOntology ontology, RDFGraph rdfType,
                                          RDFGraph owlVersInfo, RDFGraph rdfsComment, RDFGraph rdfsLabel,
                                          RDFGraph rdfsSeeAlso, RDFGraph rdfsIsDefBy, RDFGraph customAnnot) {
            var o  = rdfType.SelectTriplesByObject(RDFVocabulary.OWL.ONTOLOGY)
                            .FirstOrDefault();
            if (o != null) {
                ontology.Value           = o.Subject;
                ontology.PatternMemberID = o.Subject.PatternMemberID;

                #region Annotations

                #region VersionInfo
                foreach (var t in owlVersInfo.SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the literal fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFLiteral)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        ontology.AddVersionInfo(fact);
                    }
                }
                #endregion

                #region Comment
                foreach (var t in rdfsComment.SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the literal fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFLiteral)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        ontology.AddComment(fact);
                    }
                }
                #endregion

                #region Label
                foreach (var t in rdfsLabel.SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the literal fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFLiteral)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        ontology.AddLabel(fact);
                    }
                }
                #endregion

                #region SeeAlso
                foreach (var t in rdfsSeeAlso.SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the resource fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFResource)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        ontology.AddSeeAlso(fact);
                    }
                }
                #endregion

                #region IsDefinedBy
                foreach (var t in rdfsIsDefBy.SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the resource fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFResource)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        ontology.AddIsDefinedBy(fact);
                    }
                }
                #endregion

                #region Custom Annotations
                foreach (var ap in customAnnot) {
                    var annotationProp          = (RDFOntologyAnnotationProperty)ontology.Model.PropertyModel.SelectProperty(ap.Subject.ToString());

                    foreach (var t in ontGraph.SelectTriplesBySubject((RDFResource)ontology.Value)
                                                .SelectTriplesByPredicate((RDFResource)annotationProp.Value)) {
                        var fact                = ontology.Data.SelectFact(t.Object.ToString());

                        //If the resource fact is not found in the ontology data, update the ontology data
                        if (fact               == null) {
                            if (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                                fact            = new RDFOntologyFact((RDFResource)t.Object);
                            }
                            else {
                                fact            = new RDFOntologyFact((RDFLiteral)t.Object);
                            }
                            ontology.Data.AddFact(fact);
                        }

                        ontology.AddCustomAnnotation(annotationProp, fact);
                    }

                }
                #endregion

                #endregion

                #region Versioning

                #region Imports
                foreach (var t in ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.IMPORTS)
                                          .SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if (t.TripleFlavor   == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var fact          = ontology.Data.SelectFact(t.Object.ToString());

                        //If the object fact is not found in the ontology data, update the ontology data
                        if (fact         == null) {
                            fact          = new RDFOntologyFact((RDFResource)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        ontology.ImportOntology(new RDFOntology((RDFResource)fact.Value, false));
                    }
                }
                #endregion

                #region BackwardCompatibleWith
                foreach (var t in ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.BACKWARD_COMPATIBLE_WITH)
                                          .SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if (t.TripleFlavor   == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var fact          = ontology.Data.SelectFact(t.Object.ToString());

                        //If the object fact is not found in the ontology data, update the ontology data
                        if (fact         == null) {
                            fact          = new RDFOntologyFact((RDFResource)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        ontology.AddBackwardCompatibleWith(fact);
                    }
                }
                #endregion

                #region IncompatibleWith
                foreach (var t in ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.INCOMPATIBLE_WITH)
                                          .SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if (t.TripleFlavor   == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var fact          = ontology.Data.SelectFact(t.Object.ToString());

                        //If the object fact is not found in the ontology data, update the ontology data
                        if (fact         == null) {
                            fact          = new RDFOntologyFact((RDFResource)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        ontology.AddIncompatibleWith(fact);
                    }
                }
                #endregion

                #region PriorVersion
                foreach (var t in ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.PRIOR_VERSION)
                                          .SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if (t.TripleFlavor   == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var fact          = ontology.Data.SelectFact(t.Object.ToString());

                        //If the object fact is not found in the ontology data, update the ontology data
                        if (fact         == null) {
                            fact          = new RDFOntologyFact((RDFResource)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        ontology.AddPriorVersion(fact);
                    }
                }
                #endregion

                #endregion

            }
        }
        /// <summary>
        /// Loads the object properties from the given graph into the given ontology
        /// </summary>
        internal static void LoadObjectProperties(RDFGraph ontGraph, RDFOntology ontology, RDFGraph rdfType) {

            #region Explicit ObjectProperty
            foreach (var t in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.OBJECT_PROPERTY)) {
                var objProp = new RDFOntologyObjectProperty((RDFResource)t.Subject);
                ontology.Model.PropertyModel.AddProperty(objProp);

                #region FunctionalProperty
                var fp      = rdfType.SelectTriplesBySubject((RDFResource)objProp.Value)
                                     .SelectTriplesByObject(RDFVocabulary.OWL.FUNCTIONAL_PROPERTY)
                                     .FirstOrDefault();
                if (fp     != null) {
                    objProp.SetFunctional();
                }
                #endregion

                #region InverseFunctionalProperty
                var ifp     = rdfType.SelectTriplesBySubject((RDFResource)objProp.Value)
                                     .SelectTriplesByObject(RDFVocabulary.OWL.INVERSE_FUNCTIONAL_PROPERTY)
                                     .FirstOrDefault();
                if (ifp    != null) {
                    objProp.SetInverseFunctional();
                }
                #endregion

                #region SymmetricProperty
                var sp      = rdfType.SelectTriplesBySubject((RDFResource)objProp.Value)
                                     .SelectTriplesByObject(RDFVocabulary.OWL.SYMMETRIC_PROPERTY)
                                     .FirstOrDefault();
                if (sp     != null) {
                    objProp.SetSymmetric();
                }
                #endregion

                #region TransitiveProperty
                var tp      = rdfType.SelectTriplesBySubject((RDFResource)objProp.Value)
                                     .SelectTriplesByObject(RDFVocabulary.OWL.TRANSITIVE_PROPERTY)
                                     .FirstOrDefault();
                if (tp     != null) {
                    objProp.SetTransitive();
                }
                #endregion

            }
            #endregion

            #region Implicit ObjectProperty

            #region InverseFunctionalProperty
            foreach (var t  in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.INVERSE_FUNCTIONAL_PROPERTY)) {
                var ivfProp  = ontology.Model.PropertyModel.SelectProperty(t.Subject.ToString());
                if(ivfProp  == null) {
                    ivfProp  = new RDFOntologyObjectProperty((RDFResource)t.Subject);
                    ontology.Model.PropertyModel.AddProperty(ivfProp);

                    #region FunctionalProperty
                    var fp   = rdfType.SelectTriplesBySubject((RDFResource)ivfProp.Value)
                                      .SelectTriplesByObject(RDFVocabulary.OWL.FUNCTIONAL_PROPERTY)
                                      .FirstOrDefault();
                    if (fp  != null) {
                        ivfProp.SetFunctional();
                    }
                    #endregion

                }
                ((RDFOntologyObjectProperty)ivfProp).SetInverseFunctional();
            }
            #endregion

            #region SymmetricProperty
            foreach (var t  in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.SYMMETRIC_PROPERTY)) {
                var symProp  = ontology.Model.PropertyModel.SelectProperty(t.Subject.ToString());
                if (symProp == null) {
                    symProp  = new RDFOntologyObjectProperty((RDFResource)t.Subject);
                    ontology.Model.PropertyModel.AddProperty(symProp);

                    #region FunctionalProperty
                    var fp   = rdfType.SelectTriplesBySubject((RDFResource)symProp.Value)
                                      .SelectTriplesByObject(RDFVocabulary.OWL.FUNCTIONAL_PROPERTY)
                                      .FirstOrDefault();
                    if (fp  != null) {
                        symProp.SetFunctional();
                    }
                    #endregion

                }
                ((RDFOntologyObjectProperty)symProp).SetSymmetric();
            }
            #endregion

            #region TransitiveProperty
            foreach (var t  in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.TRANSITIVE_PROPERTY)) {
                var trnProp  = ontology.Model.PropertyModel.SelectProperty(t.Subject.ToString());
                if (trnProp == null) {
                    trnProp  = new RDFOntologyObjectProperty((RDFResource)t.Subject);
                    ontology.Model.PropertyModel.AddProperty(trnProp);

                    #region FunctionalProperty
                    var fp   = rdfType.SelectTriplesBySubject((RDFResource)trnProp.Value)
                                      .SelectTriplesByObject(RDFVocabulary.OWL.FUNCTIONAL_PROPERTY)
                                      .FirstOrDefault();
                    if (fp  != null) {
                        trnProp.SetFunctional();
                    }
                    #endregion

                }
                ((RDFOntologyObjectProperty)trnProp).SetTransitive();
            }
            #endregion

            #endregion

        }
        /// <summary>
        /// Loads the datatype properties from the given graph into the given ontology
        /// </summary>
        internal static void LoadDatatypeProperties(RDFGraph ontGraph, RDFOntology ontology, RDFGraph rdfType) {
            foreach (var t in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.DATATYPE_PROPERTY)) {
                var dtProp  = new RDFOntologyDatatypeProperty((RDFResource)t.Subject);
                ontology.Model.PropertyModel.AddProperty(dtProp);

                #region FunctionalProperty
                var fp      = rdfType.SelectTriplesBySubject((RDFResource)dtProp.Value)
                                     .SelectTriplesByObject(RDFVocabulary.OWL.FUNCTIONAL_PROPERTY)
                                     .FirstOrDefault();
                if (fp     != null) {
                    dtProp.SetFunctional();
                }
                #endregion

            }
        }
        /// <summary>
        /// Loads the annotations from the given graph into the given ontology
        /// </summary>
        internal static void LoadAnnotations(RDFGraph ontGraph,    RDFOntology ontology, RDFGraph rdfType,
                                             RDFGraph owlVersInfo, RDFGraph rdfsComment, RDFGraph rdfsLabel,
                                             RDFGraph rdfsSeeAlso, RDFGraph rdfsIsDefBy, RDFGraph customAnnot) {

            #region Classes
            foreach (var c in ontology.Model.ClassModel) {

                #region VersionInfo
                foreach (var t in owlVersInfo.SelectTriplesBySubject((RDFResource)c.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the literal fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFLiteral)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        c.AddVersionInfo(fact);
                    }
                }
                #endregion

                #region Comment
                foreach (var t in rdfsComment.SelectTriplesBySubject((RDFResource)c.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the literal fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFLiteral)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        c.AddComment(fact);
                    }
                }
                #endregion

                #region Label
                foreach (var t in rdfsLabel.SelectTriplesBySubject((RDFResource)c.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the literal fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFLiteral)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        c.AddLabel(fact);
                    }
                }
                #endregion

                #region SeeAlso
                foreach (var t in rdfsSeeAlso.SelectTriplesBySubject((RDFResource)c.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the resource fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFResource)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        c.AddSeeAlso(fact);
                    }
                }
                #endregion

                #region IsDefinedBy
                foreach (var t in rdfsIsDefBy.SelectTriplesBySubject((RDFResource)c.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the resource fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFResource)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        c.AddIsDefinedBy(fact);
                    }
                }
                #endregion

                #region Custom Annotations
                foreach (var ap in customAnnot) {
                    var annotationProp          = (RDFOntologyAnnotationProperty)ontology.Model.PropertyModel.SelectProperty(ap.Subject.ToString());

                    foreach (var t   in ontGraph.SelectTriplesBySubject((RDFResource)c.Value)
                                                .SelectTriplesByPredicate((RDFResource)annotationProp.Value)) {
                        var fact                = ontology.Data.SelectFact(t.Object.ToString());

                        //If the resource fact is not found in the ontology data, update the ontology data
                        if (fact               == null) {
                            if (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                                fact            = new RDFOntologyFact((RDFResource)t.Object);
                            }
                            else {
                                fact            = new RDFOntologyFact((RDFLiteral)t.Object);
                            }
                            ontology.Data.AddFact(fact);
                        }

                        c.AddCustomAnnotation(annotationProp, fact);
                    }

                }
                #endregion

            }
            #endregion

            #region Properties
            foreach (var p in ontology.Model.PropertyModel) {

                #region VersionInfo
                foreach (var t in owlVersInfo.SelectTriplesBySubject((RDFResource)p.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the literal fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFLiteral)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        p.AddVersionInfo(fact);
                    }
                }
                #endregion

                #region Comment
                foreach (var t in rdfsComment.SelectTriplesBySubject((RDFResource)p.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the literal fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFLiteral)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        p.AddComment(fact);
                    }
                }
                #endregion

                #region Label
                foreach (var t in rdfsLabel.SelectTriplesBySubject((RDFResource)p.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the literal fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFLiteral)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        p.AddLabel(fact);
                    }
                }
                #endregion

                #region SeeAlso
                foreach (var t in rdfsSeeAlso.SelectTriplesBySubject((RDFResource)p.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the resource fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFResource)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        p.AddSeeAlso(fact);
                    }
                }
                #endregion

                #region IsDefinedBy
                foreach (var t in rdfsIsDefBy.SelectTriplesBySubject((RDFResource)p.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var fact         = ontology.Data.SelectFact(t.Object.ToString());

                        //If the resource fact is not found in the ontology data, update the ontology data
                        if (fact        == null) {
                            fact         = new RDFOntologyFact((RDFResource)t.Object);
                            ontology.Data.AddFact(fact);
                        }

                        p.AddIsDefinedBy(fact);
                    }
                }
                #endregion

                #region Custom Annotations
                foreach (var ap in customAnnot) {
                    var annotationProp          = (RDFOntologyAnnotationProperty)ontology.Model.PropertyModel.SelectProperty(ap.Subject.ToString());

                    foreach (var t   in ontGraph.SelectTriplesBySubject((RDFResource)p.Value)
                                                .SelectTriplesByPredicate((RDFResource)annotationProp.Value)) {
                        var fact                = ontology.Data.SelectFact(t.Object.ToString());

                        //If the resource fact is not found in the ontology data, update the ontology data
                        if (fact               == null) {
                            if (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                                fact            = new RDFOntologyFact((RDFResource)t.Object);
                            }
                            else {
                                fact            = new RDFOntologyFact((RDFLiteral)t.Object);
                            }
                            ontology.Data.AddFact(fact);
                        }

                        p.AddCustomAnnotation(annotationProp, fact);
                    }

                }
                #endregion

            }
            #endregion

            #region Facts
            var changeLog = new Dictionary<Int64, RDFOntologyFact>();
            foreach (var  f in ontology.Data) {
                if (f.IsObjectFact()) {

                    #region VersionInfo
                    foreach (var t in owlVersInfo.SelectTriplesBySubject((RDFResource)f.Value)) {
                        if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                            var fact         = ontology.Data.SelectFact(t.Object.ToString());

                            //If the literal fact is not found in the ontology data, update the ontology data
                            if (fact        == null) {
                                fact         = new RDFOntologyFact((RDFLiteral)t.Object);
                                if (!changeLog.ContainsKey(fact.PatternMemberID)) {
                                    changeLog.Add(fact.PatternMemberID, fact);
                                }
                            }

                            f.AddVersionInfo(fact);
                        }
                    }
                    #endregion

                    #region Comment
                    foreach (var t in rdfsComment.SelectTriplesBySubject((RDFResource)f.Value)) {
                        if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                            var fact         = ontology.Data.SelectFact(t.Object.ToString());

                            //If the literal fact is not found in the ontology data, update the ontology data
                            if (fact        == null) {
                                fact         = new RDFOntologyFact((RDFLiteral)t.Object);
                                if (!changeLog.ContainsKey(fact.PatternMemberID)) {
                                    changeLog.Add(fact.PatternMemberID, fact);
                                }
                            }

                            f.AddComment(fact);
                        }
                    }
                    #endregion

                    #region Label
                    foreach (var t in rdfsLabel.SelectTriplesBySubject((RDFResource)f.Value)) {
                        if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                            var fact         = ontology.Data.SelectFact(t.Object.ToString());

                            //If the literal fact is not found in the ontology data, update the ontology data
                            if (fact        == null) {
                                fact         = new RDFOntologyFact((RDFLiteral)t.Object);
                                if (!changeLog.ContainsKey(fact.PatternMemberID)) {
                                    changeLog.Add(fact.PatternMemberID, fact);
                                }
                            }

                            f.AddLabel(fact);
                        }
                    }
                    #endregion

                    #region SeeAlso
                    foreach (var t in rdfsSeeAlso.SelectTriplesBySubject((RDFResource)f.Value)) {
                        if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPO) {
                            var fact         = ontology.Data.SelectFact(t.Object.ToString());

                            //If the resource fact is not found in the ontology data, update the ontology data
                            if (fact        == null) {
                                fact         = new RDFOntologyFact((RDFResource)t.Object);
                                if (!changeLog.ContainsKey(fact.PatternMemberID)) {
                                    changeLog.Add(fact.PatternMemberID, fact);
                                }
                            }

                            f.AddSeeAlso(fact);
                        }
                    }
                    #endregion

                    #region IsDefinedBy
                    foreach (var t in rdfsIsDefBy.SelectTriplesBySubject((RDFResource)f.Value)) {
                        if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPO) {
                            var fact         = ontology.Data.SelectFact(t.Object.ToString());

                            //If the resource fact is not found in the ontology data, update the ontology data
                            if (fact        == null) {
                                fact         = new RDFOntologyFact((RDFResource)t.Object);
                                if (!changeLog.ContainsKey(fact.PatternMemberID)) {
                                    changeLog.Add(fact.PatternMemberID, fact);
                                }
                            }

                            f.AddIsDefinedBy(fact);
                        }
                    }
                    #endregion

                    #region Custom Annotations
                    foreach (var ap in customAnnot) {
                        var annotationProp          = (RDFOntologyAnnotationProperty)ontology.Model.PropertyModel.SelectProperty(ap.Subject.ToString());

                        foreach (var t   in ontGraph.SelectTriplesBySubject((RDFResource)f.Value)
                                                    .SelectTriplesByPredicate((RDFResource)annotationProp.Value)) {
                            var fact                = ontology.Data.SelectFact(t.Object.ToString());

                            //If the resource fact is not found in the ontology data, update the ontology data
                            if (fact               == null) {
                                if (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                                    fact            = new RDFOntologyFact((RDFResource)t.Object);
                                }
                                else {
                                    fact            = new RDFOntologyFact((RDFLiteral)t.Object);
                                }
                                if (!changeLog.ContainsKey(fact.PatternMemberID)) {
                                    changeLog.Add(fact.PatternMemberID, fact);
                                }
                            }

                            f.AddCustomAnnotation(annotationProp, fact);
                        }

                    }
                    #endregion

                }
            }
            foreach (var cl in changeLog) {
                ontology.Data.AddFact(cl.Value);
            }
            #endregion

        }
        /// <summary>
        /// Loads the "rdfs:domain" and "rdfs:range" informations from the given graph into the given ontology
        /// </summary>
        internal static void LoadPropertyDomainRange(RDFGraph ontGraph, RDFOntology ontology) {
            foreach (var p in ontology.Model.PropertyModel) {

                #region Domain
                var d  = ontGraph.SelectTriplesBySubject((RDFResource)p.Value)
                                 .SelectTriplesByPredicate(RDFVocabulary.RDFS.DOMAIN)
                                 .FirstOrDefault();
                if (d != null) {
                    var domainClass  = ontology.Model.ClassModel.SelectClass(d.Object.ToString());
                    if (domainClass != null) {
                        p.SetDomain(domainClass);
                    }
                    else {
                        throw new RDFSemanticsException("Cannot load domain class '" + d.Object + "' of property '" + p.Value + "', because it has not been found in the ontology model.");
                    }
                }
                #endregion

                #region Range
                var r  = ontGraph.SelectTriplesBySubject((RDFResource)p.Value)
                                 .SelectTriplesByPredicate(RDFVocabulary.RDFS.RANGE)
                                 .FirstOrDefault();
                if (r != null) {
                    var rangeClass  = ontology.Model.ClassModel.SelectClass(r.Object.ToString());
                    if (rangeClass != null) {
                        p.SetRange(rangeClass);
                    }
                    else {
                        throw new RDFSemanticsException("Cannot load range class '" + r.Object + "' of property '" + p.Value + "', because it has not been found in the ontology model.");
                    }
                }
                #endregion

            }
        }
        /// <summary>
        /// Gets an ontology representation of the given graph
        /// </summary>
        internal static RDFOntology FromRDFGraph(RDFGraph ontGraph) {
            RDFOntology ontology  = null;
            if (ontGraph         != null) {
                ontology          = new RDFOntology(new RDFResource(ontGraph.Context));

                #region Prefetch
                var versionInfo   = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.VERSION_INFO);
                var comment       = ontGraph.SelectTriplesByPredicate(RDFVocabulary.RDFS.COMMENT);
                var label         = ontGraph.SelectTriplesByPredicate(RDFVocabulary.RDFS.LABEL);
                var seeAlso       = ontGraph.SelectTriplesByPredicate(RDFVocabulary.RDFS.SEE_ALSO);
                var isDefinedBy   = ontGraph.SelectTriplesByPredicate(RDFVocabulary.RDFS.IS_DEFINED_BY);
                var imports       = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.IMPORTS);
                var bcwcompWith   = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.BACKWARD_COMPATIBLE_WITH);
                var incompWith    = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.INCOMPATIBLE_WITH);
                var priorVersion  = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.PRIOR_VERSION);

                var rdfType       = ontGraph.SelectTriplesByPredicate(RDFVocabulary.RDF.TYPE);
                var subclassOf    = ontGraph.SelectTriplesByPredicate(RDFVocabulary.RDFS.SUB_CLASS_OF);
                var subpropOf     = ontGraph.SelectTriplesByPredicate(RDFVocabulary.RDFS.SUB_PROPERTY_OF);
                var equivclassOf  = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.EQUIVALENT_CLASS);
                var equivpropOf   = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.EQUIVALENT_PROPERTY);
                var disjclassWith = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.DISJOINT_WITH);
                var domain        = ontGraph.SelectTriplesByPredicate(RDFVocabulary.RDFS.DOMAIN);
                var range         = ontGraph.SelectTriplesByPredicate(RDFVocabulary.RDFS.RANGE);
                var onProperty    = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.ON_PROPERTY);
                var oneOf         = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.ONE_OF);
                var unionOf       = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.UNION_OF);
                var intersectionOf= ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.INTERSECTION_OF);
                var complementOf  = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.COMPLEMENT_OF);
                var inverseOf     = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.INVERSE_OF);
                var allvaluesFrom = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.ALL_VALUES_FROM);
                var somevaluesFrom= ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.SOME_VALUES_FROM);
                var hasvalue      = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.HAS_VALUE);
                var cardinality   = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.CARDINALITY);
                var mincardinality= ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.MIN_CARDINALITY);
                var maxcardinality= ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.MAX_CARDINALITY);
                var sameAs        = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.SAME_AS);
                var differentFrom = ontGraph.SelectTriplesByPredicate(RDFVocabulary.OWL.DIFFERENT_FROM);
                #endregion

                #region Load

                #region Ontology
                if (!rdfType.ContainsTriple(new RDFTriple((RDFResource)ontology.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.ONTOLOGY))) {
                     var ont     = rdfType.SelectTriplesByObject(RDFVocabulary.OWL.ONTOLOGY).FirstOrDefault();
                     if (ont    != null) {
                         ontology.Value           = ont.Subject;
                         ontology.PatternMemberID = ontology.Value.PatternMemberID;
                     }
                }
                #endregion

                #region OntologyModel

                #region PropertyModel

                #region AnnotationProperty
                foreach (var ap in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.ANNOTATION_PROPERTY)) {
                    ontology.Model.PropertyModel.AddProperty(new RDFOntologyAnnotationProperty((RDFResource)ap.Subject));
                }
                #endregion

                #region DatatypeProperty
                foreach (var dp in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.DATATYPE_PROPERTY)) {
                    var dtp   = new RDFOntologyDatatypeProperty((RDFResource)dp.Subject);
                    ontology.Model.PropertyModel.AddProperty(dtp);

                    #region DeprecatedProperty
                    if (ontGraph.ContainsTriple(new RDFTriple((RDFResource)dtp.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.DEPRECATED_PROPERTY))) {
                        dtp.SetDeprecated(true);
                    }
                    #endregion

                    #region FunctionalProperty
                    if (ontGraph.ContainsTriple(new RDFTriple((RDFResource)dtp.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.FUNCTIONAL_PROPERTY))) {
                        dtp.SetFunctional(true);
                    }
                    #endregion

                }
                #endregion

                #region ObjectProperty
                foreach (var op in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.OBJECT_PROPERTY)) {
                    var obp  = new RDFOntologyObjectProperty((RDFResource)op.Subject);
                    ontology.Model.PropertyModel.AddProperty(obp);

                    #region DeprecatedProperty
                    if (ontGraph.ContainsTriple(new RDFTriple((RDFResource)obp.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.DEPRECATED_PROPERTY))) {
                        obp.SetDeprecated(true);
                    }
                    #endregion

                    #region FunctionalProperty
                    if (ontGraph.ContainsTriple(new RDFTriple((RDFResource)obp.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.FUNCTIONAL_PROPERTY))) {
                        obp.SetFunctional(true);
                    }
                    #endregion

                    #region SymmetricProperty
                    if (ontGraph.ContainsTriple(new RDFTriple((RDFResource)obp.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.SYMMETRIC_PROPERTY))) {
                        obp.SetSymmetric(true);
                    }
                    #endregion

                    #region TransitiveProperty
                    if (ontGraph.ContainsTriple(new RDFTriple((RDFResource)obp.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.TRANSITIVE_PROPERTY))) {
                        obp.SetTransitive(true);
                    }
                    #endregion

                    #region InverseFunctionalProperty
                    if (ontGraph.ContainsTriple(new RDFTriple((RDFResource)obp.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.INVERSE_FUNCTIONAL_PROPERTY))) {
                        obp.SetInverseFunctional(true);
                    }
                    #endregion

                }

                #region SymmetricProperty
                foreach (var sp in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.SYMMETRIC_PROPERTY)) {
                    var syp  = ontology.Model.PropertyModel.SelectProperty(sp.Subject.ToString());
                    if (syp == null) {
                        syp  = new RDFOntologyObjectProperty((RDFResource)sp.Subject);
                        ontology.Model.PropertyModel.AddProperty(syp);

                        #region DeprecatedProperty
                        if (ontGraph.ContainsTriple(new RDFTriple((RDFResource)syp.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.DEPRECATED_PROPERTY))) {
                            syp.SetDeprecated(true);
                        }
                        #endregion

                        #region FunctionalProperty
                        if (ontGraph.ContainsTriple(new RDFTriple((RDFResource)syp.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.FUNCTIONAL_PROPERTY))) {
                            syp.SetFunctional(true);
                        }
                        #endregion

                    }
                    ((RDFOntologyObjectProperty)syp).SetSymmetric(true);
                }
                #endregion

                #region TransitiveProperty
                foreach (var tp in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.TRANSITIVE_PROPERTY)) {
                    var trp  = ontology.Model.PropertyModel.SelectProperty(tp.Subject.ToString());
                    if (trp == null) {
                        trp  = new RDFOntologyObjectProperty((RDFResource)tp.Subject);
                        ontology.Model.PropertyModel.AddProperty(trp);

                        #region DeprecatedProperty
                        if (ontGraph.ContainsTriple(new RDFTriple((RDFResource)trp.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.DEPRECATED_PROPERTY))) {
                            trp.SetDeprecated(true);
                        }
                        #endregion

                        #region FunctionalProperty
                        if (ontGraph.ContainsTriple(new RDFTriple((RDFResource)trp.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.FUNCTIONAL_PROPERTY))) {
                            trp.SetFunctional(true);
                        }
                        #endregion

                    }
                    ((RDFOntologyObjectProperty)trp).SetTransitive(true);
                }
                #endregion

                #region InverseFunctionalProperty
                foreach (var ip in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.INVERSE_FUNCTIONAL_PROPERTY)) {
                    var ifp  = ontology.Model.PropertyModel.SelectProperty(ip.Subject.ToString());
                    if (ifp == null) {
                        ifp  = new RDFOntologyObjectProperty((RDFResource)ip.Subject);
                        ontology.Model.PropertyModel.AddProperty(ifp);

                        #region DeprecatedProperty
                        if (ontGraph.ContainsTriple(new RDFTriple((RDFResource)ifp.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.DEPRECATED_PROPERTY))) {
                            ifp.SetDeprecated(true);
                        }
                        #endregion

                        #region FunctionalProperty
                        if (ontGraph.ContainsTriple(new RDFTriple((RDFResource)ifp.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.FUNCTIONAL_PROPERTY))) {
                            ifp.SetFunctional(true);
                        }
                        #endregion

                    }
                    ((RDFOntologyObjectProperty)ifp).SetInverseFunctional(true);
                }
                #endregion
                #endregion

                #endregion

                #region ClassModel

                #region Class
                foreach (var c   in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.CLASS)) {
                    var ontClass  = new RDFOntologyClass((RDFResource)c.Subject);
                    ontology.Model.ClassModel.AddClass(ontClass);
                    if   (ontGraph.ContainsTriple(new RDFTriple((RDFResource)ontClass.Value, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.DEPRECATED_CLASS))) {
                          ontClass.SetDeprecated(true);
                    }
                }
                #endregion

                #region DeprecatedClass
                foreach (var dc  in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.DEPRECATED_CLASS)) {
                    var ontClass  = new RDFOntologyClass((RDFResource)dc.Subject);
                    ontClass.SetDeprecated(true);
                    ontology.Model.ClassModel.AddClass(ontClass);
                }
                #endregion

                #region Restriction
                foreach (var r in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.RESTRICTION)) {

                    #region OnProperty
                    var op   = onProperty.SelectTriplesBySubject((RDFResource)r.Subject).FirstOrDefault();
                    if (op  != null) {
                        var onProp     = ontology.Model.PropertyModel.SelectProperty(op.Object.ToString());
                        if (onProp    != null) {
                            var restr  = new RDFOntologyRestriction((RDFResource)r.Subject, onProp);
                            ontology.Model.ClassModel.AddClass(restr);
                        }
                        else {

                            //Raise warning event to inform the user: restriction cannot be imported from 
                            //graph, because definition of its applied property is not found in the model
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Restriction '{0}' cannot be imported from graph, because definition of its applied property '{1}' is not found in the model.", r.Subject, op.Object));

                        }
                    }
                    #endregion

                }
                #endregion

                #region DataRange
                foreach (var dr  in rdfType.SelectTriplesByObject(RDFVocabulary.OWL.DATA_RANGE)) {
                    ontology.Model.ClassModel.AddClass(new RDFOntologyDataRangeClass((RDFResource)dr.Subject));
                }
                #endregion

                #region Composite

                #region Union
                foreach (var u in unionOf) {
                    if  (u.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                         var uc          = ontology.Model.ClassModel.SelectClass(u.Subject.ToString());
                         if (uc         != null) {

                            #region ClassToUnionClass
                            if (!(uc    is RDFOntologyUnionClass)) {
                                  uc     = new RDFOntologyUnionClass((RDFResource)u.Subject);
                                  ontology.Model.ClassModel.Classes[uc.PatternMemberID] = uc;
                            }
                            #endregion

                            #region DeserializeUnionCollection
                            var nilFound = false;
                            var itemRest = (RDFResource)u.Object;
                            while (!nilFound) {

                                #region rdf:first
                                var first  = ontGraph.SelectTriplesBySubject(itemRest)
                                                     .SelectTriplesByPredicate(RDFVocabulary.RDF.FIRST)
                                                     .FirstOrDefault();
                                if (first != null   && first.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                                    var compClass    = ontology.Model.ClassModel.SelectClass(first.Object.ToString());
                                    if (compClass   != null) {
                                        ontology.Model.ClassModel.AddUnionOfRelation((RDFOntologyUnionClass)uc, compClass);
                                    }
                                    else {

                                        //Raise warning event to inform the user: union class cannot be completely imported
                                        //from graph, because definition of its compositing class is not found in the model
                                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("UnionClass '{0}' cannot be completely imported from graph, because definition of its compositing class '{1}' is not found in the model.", u.Subject, first.Object));
                                    
                                    }

                                    #region rdf:rest
                                    var rest         = ontGraph.SelectTriplesBySubject(itemRest)
                                                               .SelectTriplesByPredicate(RDFVocabulary.RDF.REST)
                                                               .FirstOrDefault();
                                    if (rest        != null) {
                                        if (rest.Object.Equals(RDFVocabulary.RDF.NIL)) {
                                            nilFound = true;
                                        }
                                        else {
                                            itemRest = (RDFResource)rest.Object;
                                        }
                                    }
                                    #endregion

                                }
                                else {
                                    nilFound = true;
                                }
                                #endregion

                            }
                            #endregion

                         }
                    }
                }
                #endregion

                #region Intersection
                foreach (var i in intersectionOf) {
                    if  (i.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                         var ic          = ontology.Model.ClassModel.SelectClass(i.Subject.ToString());
                         if (ic         != null) {

                            #region ClassToIntersectionClass
                            if (!(ic    is RDFOntologyIntersectionClass)) {
                                  ic     = new RDFOntologyIntersectionClass((RDFResource)i.Subject);
                                  ontology.Model.ClassModel.Classes[ic.PatternMemberID] = ic;
                            }
                            #endregion

                            #region DeserializeIntersectionCollection
                            var nilFound   = false;
                            var itemRest   = (RDFResource)i.Object;
                            while (!nilFound) {

                                #region rdf:first
                                var first  = ontGraph.SelectTriplesBySubject(itemRest)
                                                     .SelectTriplesByPredicate(RDFVocabulary.RDF.FIRST)
                                                     .FirstOrDefault();
                                if (first != null   && first.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                                    var compClass    = ontology.Model.ClassModel.SelectClass(first.Object.ToString());
                                    if (compClass   != null) {
                                        ontology.Model.ClassModel.AddIntersectionOfRelation((RDFOntologyIntersectionClass)ic, compClass);
                                    }
                                    else {

                                        //Raise warning event to inform the user: intersection class cannot be completely imported
                                        //from graph, because definition of its compositing class is not found in the model
                                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("IntersectionClass '{0}' cannot be completely imported from graph, because definition of its compositing class '{1}' is not found in the model.", i.Subject, first.Object));
                                    
                                    }


                                    #region rdf:rest
                                    var rest         = ontGraph.SelectTriplesBySubject(itemRest)
                                                               .SelectTriplesByPredicate(RDFVocabulary.RDF.REST)
                                                               .FirstOrDefault();
                                    if (rest        != null) {
                                        if (rest.Object.Equals(RDFVocabulary.RDF.NIL)) {
                                            nilFound = true;
                                        }
                                        else {
                                            itemRest = (RDFResource)rest.Object;
                                        }
                                    }
                                    #endregion

                                }
                                else {
                                    nilFound = true;
                                }
                                #endregion

                            }
                            #endregion

                         }
                    }
                }
                #endregion

                #region Complement
                foreach (var c in complementOf) {
                    if  (c.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                         var cc  = ontology.Model.ClassModel.SelectClass(c.Subject.ToString());
                         if (cc != null) {
                             var compClass  = ontology.Model.ClassModel.SelectClass(c.Object.ToString());
                             if (compClass != null) {
                                 cc         = new RDFOntologyComplementClass((RDFResource)c.Subject, compClass);
                                 ontology.Model.ClassModel.Classes[cc.PatternMemberID] = cc;
                             }
                             else {

                                 //Raise warning event to inform the user: complement class cannot be imported
                                 //from graph, because definition of its complemented class is not found in the model
                                 RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Class '{0}' cannot be imported from graph, because definition of its complement class '{1}' is not found in the model.", c.Subject, c.Object));

                             }
                         }
                         else {

                             //Raise warning event to inform the user: complement class cannot be imported 
                             //from graph, because its definition is not found in the model
                             RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Class '{0}' cannot be imported from graph, because its definition is not found in the model.", c.Subject));

                         }
                    }
                }
                #endregion

                #endregion

                #endregion

                #endregion

                #region OntologyData

                #region Fact
                foreach (var c  in ontology.Model.ClassModel) {
                    foreach (var t in rdfType.SelectTriplesByObject((RDFResource)c.Value)) {
                        var f    = ontology.Data.SelectFact(t.Subject.ToString());
                        if (f   == null) {
                            f    = new RDFOntologyFact((RDFResource)t.Subject);
                            ontology.Data.AddFact(f);
                        }
                        ontology.Data.AddClassTypeRelation(f, c);
                    }
                }
                #endregion

                #endregion

                #region Finalization

                #region Restriction
                var restrictions = ontology.Model.ClassModel.Where(c => c.IsRestrictionClass()).ToList();
                foreach (var     r in restrictions) {

                    #region Cardinality
                    Int32 exC    = 0;
                    var  crEx    = cardinality.SelectTriplesBySubject((RDFResource)r.Value).FirstOrDefault();
                    if (crEx    != null   && crEx.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                        if (crEx.Object   is RDFPlainLiteral) {
                            if (Regex.IsMatch(crEx.Object.ToString(), @"^[0-9]+$")) {
                                exC        = Int32.Parse(crEx.Object.ToString());
                            }
                        }
                        else {
                            if (((RDFTypedLiteral)crEx.Object).Datatype.Category == RDFModelEnums.RDFDatatypeCategory.Numeric) {
                                if (Regex.IsMatch(((RDFTypedLiteral)crEx.Object).Value, @"^[0-9]+$")) {
                                    exC    = Int32.Parse(((RDFTypedLiteral)crEx.Object).Value);
                                }
                            }
                        }
                    }
                    if (exC > 0) {
                        var cardRestr      = new RDFOntologyCardinalityRestriction((RDFResource)r.Value, ((RDFOntologyRestriction)r).OnProperty, exC, exC);
                        ontology.Model.ClassModel.Classes[r.PatternMemberID] = cardRestr;
                        continue;
                    }

                    Int32 minC   = 0;
                    var  crMin   = mincardinality.SelectTriplesBySubject((RDFResource)r.Value).FirstOrDefault();
                    if (crMin   != null   && crMin.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                        if (crMin.Object  is RDFPlainLiteral) {
                            if (Regex.IsMatch(crMin.Object.ToString(), @"^[0-9]+$")) {
                                minC       = Int32.Parse(crMin.Object.ToString());
                            }
                        }
                        else {
                            if (((RDFTypedLiteral)crMin.Object).Datatype.Category == RDFModelEnums.RDFDatatypeCategory.Numeric) {
                                if (Regex.IsMatch(((RDFTypedLiteral)crMin.Object).Value, @"^[0-9]+$")) {
                                    minC   = Int32.Parse(((RDFTypedLiteral)crMin.Object).Value);
                                }
                            }
                        }
                    }

                    Int32 maxC   = 0;
                    var  crMax   = maxcardinality.SelectTriplesBySubject((RDFResource)r.Value).FirstOrDefault();
                    if (crMax   != null   && crMax.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                        if (crMax.Object  is RDFPlainLiteral) {
                            if (Regex.IsMatch(crMax.Object.ToString(), @"^[0-9]+$")) {
                                maxC       = Int32.Parse(crMax.Object.ToString());
                            }
                        }
                        else {
                            if (((RDFTypedLiteral)crMax.Object).Datatype.Category == RDFModelEnums.RDFDatatypeCategory.Numeric) {
                                if (Regex.IsMatch(((RDFTypedLiteral)crMax.Object).Value, @"^[0-9]+$")) {
                                    maxC   = Int32.Parse(((RDFTypedLiteral)crMax.Object).Value);
                                }
                            }
                        }
                    }
                    if (minC > 0  ||  maxC > 0) {
                        var cardRestr      = new RDFOntologyCardinalityRestriction((RDFResource)r.Value, ((RDFOntologyRestriction)r).OnProperty, minC, maxC);
                        ontology.Model.ClassModel.Classes[r.PatternMemberID] = cardRestr;
                        continue;
                    }
                    #endregion

                    #region HasValue
                    var hvRes    = hasvalue.SelectTriplesBySubject((RDFResource)r.Value).FirstOrDefault();
                    if (hvRes   != null) {
                        if (hvRes.TripleFlavor   == RDFModelEnums.RDFTripleFlavors.SPO) {
                            var hvFct             = ontology.Data.SelectFact(hvRes.Object.ToString());
                            if (hvFct            != null) {
                                var hasvalueRestr = new RDFOntologyHasValueRestriction((RDFResource)r.Value, ((RDFOntologyRestriction)r).OnProperty, hvFct);
                                ontology.Model.ClassModel.Classes[r.PatternMemberID] = hasvalueRestr;
                                continue;
                            }
                            else {

                                //Raise warning event to inform the user: hasvalue restriction cannot be imported
                                //from graph, because definition of its required fact is not found in the data
                                RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Restriction '{0}' cannot be imported from graph, because definition of its required fact '{1}' is not found in the data.", r.Value, hvRes.Object));

                            }
                        }
                        else {
                            var hasvalueRestr     = new RDFOntologyHasValueRestriction((RDFResource)r.Value, ((RDFOntologyRestriction)r).OnProperty, new RDFOntologyLiteral((RDFLiteral)hvRes.Object));
                            ontology.Model.ClassModel.Classes[r.PatternMemberID] = hasvalueRestr;
                            continue;
                        }
                    }
                    #endregion

                    #region AllValuesFrom
                    var avfRes       = allvaluesFrom.SelectTriplesBySubject((RDFResource)r.Value).FirstOrDefault();
                    if (avfRes      != null   && avfRes.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var avfCls   = ontology.Model.ClassModel.SelectClass(avfRes.Object.ToString());
                        if (avfCls  != null) {
                            var allvaluesfromRestr = new RDFOntologyAllValuesFromRestriction((RDFResource)r.Value, ((RDFOntologyRestriction)r).OnProperty, avfCls);
                            ontology.Model.ClassModel.Classes[r.PatternMemberID] = allvaluesfromRestr;
                            continue;
                        }
                        else {

                            //Raise warning event to inform the user: allvaluesfrom restriction cannot be imported
                            //from graph, because definition of its required class is not found in the model
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Restriction '{0}' cannot be imported from graph, because definition of its required class '{1}' is not found in the model.", r.Value, avfRes.Object));

                        }
                    }
                    #endregion

                    #region SomeValuesFrom
                    var svfRes      = somevaluesFrom.SelectTriplesBySubject((RDFResource)r.Value).FirstOrDefault();
                    if (svfRes     != null   && svfRes.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var svfCls  = ontology.Model.ClassModel.SelectClass(svfRes.Object.ToString());
                        if (svfCls != null) {
                            var somevaluesfromRestr = new RDFOntologySomeValuesFromRestriction((RDFResource)r.Value, ((RDFOntologyRestriction)r).OnProperty, svfCls);
                            ontology.Model.ClassModel.Classes[r.PatternMemberID] = somevaluesfromRestr;
                            continue;
                        }
                        else {

                            //Raise warning event to inform the user: somevaluesfrom restriction cannot be imported
                            //from graph, because definition of its required class is not found in the model
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Restriction '{0}' cannot be imported from graph, because definition of its required class '{1}' is not found in the model.", r.Value, svfRes.Object));

                        }
                    }
                    #endregion

                }
                #endregion

                #region Enumerate
                foreach (var e  in  oneOf) {
                    if  (e.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                         var ec          = ontology.Model.ClassModel.SelectClass(e.Subject.ToString());
                         if (ec         != null && !ec.IsDataRangeClass()) {

                            #region ClassToEnumerateClass
                            if (!ec.IsEnumerateClass()) {
                                 ec      = new RDFOntologyEnumerateClass((RDFResource)e.Subject);
                                 ontology.Model.ClassModel.Classes[ec.PatternMemberID] = ec;
                            }
                            #endregion

                            #region DeserializeEnumerateCollection
                            var nilFound   = false;
                            var itemRest   = (RDFResource)e.Object;
                            while (!nilFound) {

                                #region rdf:first
                                var first  = ontGraph.SelectTriplesBySubject(itemRest)
                                                     .SelectTriplesByPredicate(RDFVocabulary.RDF.FIRST)
                                                     .FirstOrDefault();
                                if (first != null   && first.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                                    var enumMember   = ontology.Data.SelectFact(first.Object.ToString());
                                    if (enumMember  != null) {
                                        ontology.Model.ClassModel.AddOneOfRelation((RDFOntologyEnumerateClass)ec, enumMember);
                                    }
									else {

                                        //Raise warning event to inform the user: enumerate class cannot be completely imported
                                        //from graph, because definition of its fact member is not found in the data
                                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("EnumerateClass '{0}' cannot be completely imported from graph, because definition of its fact member '{1}' is not found in the data.", e.Subject, first.Object));

			                        }

                                    #region rdf:rest
                                    var rest         = ontGraph.SelectTriplesBySubject(itemRest)
                                                               .SelectTriplesByPredicate(RDFVocabulary.RDF.REST)
                                                               .FirstOrDefault();
                                    if (rest        != null) {
                                        if (rest.Object.Equals(RDFVocabulary.RDF.NIL)) {
                                            nilFound = true;
                                        }
                                        else {
                                            itemRest = (RDFResource)rest.Object;
                                        }
                                    }
                                    #endregion

                                }
                                else {
                                    nilFound = true;
                                }
                                #endregion

                            }
                            #endregion

                         }
                         else {
                             if (ec     == null) {

                                 //Raise warning event to inform the user: enumerate class cannot be imported
                                 //from graph, because its definition is not found in the model
                                 RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("EnumerateClass '{0}' cannot be imported from graph, because its definition is not found in the model.", e.Subject));

                             }
                         }
                    }
                }
                #endregion

                #region DataRange
                foreach (var d in oneOf) {
                    if  (d.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                         var dr          = ontology.Model.ClassModel.SelectClass(d.Subject.ToString());
                         if (dr         != null && !dr.IsEnumerateClass()) {

                            #region ClassToDataRangeClass
                            if (!dr.IsDataRangeClass()) {
                                 dr      = new RDFOntologyDataRangeClass((RDFResource)d.Subject);
                                 ontology.Model.ClassModel.Classes[dr.PatternMemberID] = dr;
                            }
                            #endregion

                            #region DeserializeDataRangeCollection
                            var nilFound   = false;
                            var itemRest   = (RDFResource)d.Object;
                            while (!nilFound) {

                                #region rdf:first
                                var first  = ontGraph.SelectTriplesBySubject(itemRest)
                                                     .SelectTriplesByPredicate(RDFVocabulary.RDF.FIRST)
                                                     .FirstOrDefault();
                                if (first != null   && first.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                                    ontology.Model.ClassModel.AddOneOfRelation((RDFOntologyDataRangeClass)dr, new RDFOntologyLiteral((RDFLiteral)first.Object));

                                    #region rdf:rest
                                    var rest         = ontGraph.SelectTriplesBySubject(itemRest)
                                                               .SelectTriplesByPredicate(RDFVocabulary.RDF.REST)
                                                               .FirstOrDefault();
                                    if (rest        != null) {
                                        if (rest.Object.Equals(RDFVocabulary.RDF.NIL)) {
                                            nilFound = true;
                                        }
                                        else {
                                            itemRest = (RDFResource)rest.Object;
                                        }
                                    }
                                    #endregion

                                }
                                else {
                                    nilFound = true;
                                }
                                #endregion

                            }
                            #endregion

                         }
                         else {
                             if (dr     == null) {

                                 //Raise warning event to inform the user: datarange class cannot be imported from
                                 //graph, because its definition is not found in the model
                                 RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("DataRangeClass '{0}' cannot be imported from graph, because its definition is not found in the model.", d.Subject));

                             }
                         }
                    }
                }
                #endregion

                #region Domain/Range
                foreach (var p in ontology.Model.PropertyModel) {
                    
                    #region Domain
                    var d   = domain.SelectTriplesBySubject((RDFResource)p.Value).FirstOrDefault();
                    if (d  != null   && d.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var domainClass  = ontology.Model.ClassModel.SelectClass(d.Object.ToString());
                        if (domainClass != null) {
                            p.SetDomain(domainClass);
                        }
                        else {

                            //Raise warning event to inform the user: domain constraint cannot be imported from graph, 
                            //because definition of required class is not found in the model
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Domain constraint on property '{0}' cannot be imported from graph, because definition of required class '{1}' is not found in the model.", p.Value, d.Object));

                        }
                    }
                    #endregion

                    #region Range
                    var r   = range.SelectTriplesBySubject((RDFResource)p.Value).FirstOrDefault();
                    if (r  != null  && r.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                        var rangeClass  = ontology.Model.ClassModel.SelectClass(r.Object.ToString());
                        if (rangeClass != null) {
                            p.SetRange(rangeClass);
                        }
                        else {

                            //Raise warning event to inform the user: range constraint cannot be imported from graph, 
                            //because definition of required class is not found in the model
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Range constraint on property '{0}' cannot be imported from graph, because definition of required class '{1}' is not found in the model.", p.Value, r.Object));

                        }
                    }
                    #endregion

                }
                #endregion

                #region PropertyModel Relations
                foreach (var p in ontology.Model.PropertyModel) {
                    
                    #region SubPropertyOf
                    foreach (var spof in subpropOf.SelectTriplesBySubject((RDFResource)p.Value)) {
                        if  (spof.TripleFlavor   == RDFModelEnums.RDFTripleFlavors.SPO) {
                             var superProp        = ontology.Model.PropertyModel.SelectProperty(spof.Object.ToString());
                             if (superProp       != null) {
                                 if (p.IsObjectProperty()        && superProp.IsObjectProperty()) {
                                     ontology.Model.PropertyModel.AddSubPropertyOfRelation((RDFOntologyObjectProperty)p,   (RDFOntologyObjectProperty)superProp);
                                 }
                                 else if (p.IsDatatypeProperty() && superProp.IsDatatypeProperty()) {
                                     ontology.Model.PropertyModel.AddSubPropertyOfRelation((RDFOntologyDatatypeProperty)p, (RDFOntologyDatatypeProperty)superProp);
                                 }
                             }
                             else {

                                 //Raise warning event to inform the user: subpropertyof relation cannot be imported
                                 //from graph, because definition of property is not found in the model
                                 RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("SubPropertyOf relation on property '{0}' cannot be imported from graph, because definition of property '{1}' is not found in the model.", p.Value, spof.Object));

                             }
                        }
                    }
                    #endregion

                    #region EquivalentProperty
                    foreach (var eqpr in equivpropOf.SelectTriplesBySubject((RDFResource)p.Value)) {
                        if  (eqpr.TripleFlavor   == RDFModelEnums.RDFTripleFlavors.SPO) {
                             var equivProp        = ontology.Model.PropertyModel.SelectProperty(eqpr.Object.ToString());
                             if (equivProp       != null) {
                                 if (p.IsObjectProperty()        && equivProp.IsObjectProperty()) {
                                     ontology.Model.PropertyModel.AddEquivalentPropertyRelation((RDFOntologyObjectProperty)p,   (RDFOntologyObjectProperty)equivProp);
                                 }
                                 else if (p.IsDatatypeProperty() && equivProp.IsDatatypeProperty()) {
                                     ontology.Model.PropertyModel.AddEquivalentPropertyRelation((RDFOntologyDatatypeProperty)p, (RDFOntologyDatatypeProperty)equivProp);
                                 }
                             }
                             else {

                                 //Raise warning event to inform the user: equivalentproperty relation cannot be imported
                                 //from graph, because definition of property is not found in the model
                                 RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("EquivalentProperty relation on property '{0}' cannot be imported from graph, because definition of property '{1}' is not found in the model.", p.Value, eqpr.Object));

                             }
                        }
                    }
                    #endregion

                    #region InverseOf
                    if (p.IsObjectProperty()) {
                        foreach (var inof in inverseOf.SelectTriplesBySubject((RDFResource)p.Value)) {
                            if  (inof.TripleFlavor   == RDFModelEnums.RDFTripleFlavors.SPO) {
                                 var invProp          = ontology.Model.PropertyModel.SelectProperty(inof.Object.ToString());
                                 if (invProp != null && invProp.IsObjectProperty()) {
                                     ontology.Model.PropertyModel.AddInverseOfRelation((RDFOntologyObjectProperty)p, (RDFOntologyObjectProperty)invProp);
                                 }
                                 else {

                                     //Raise warning event to inform the user: inverseof relation cannot be imported
                                     //from graph, because definition of property is not found in the model
                                     RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("InverseOf relation on property '{0}' cannot be imported from graph, because definition of property '{1}' is not found in the model.", p.Value, inof.Object));

                                 }
                            }
                        }
                    }
                    #endregion

                }
                #endregion

                #region ClassModel Relations
                foreach (var c in ontology.Model.ClassModel) {

                    #region SubClassOf
                    foreach (var scof in subclassOf.SelectTriplesBySubject((RDFResource)c.Value)) {
                        if  (scof.TripleFlavor   == RDFModelEnums.RDFTripleFlavors.SPO) {
                             var superClass       = ontology.Model.ClassModel.SelectClass(scof.Object.ToString());
                             if (superClass      != null) {
                                 ontology.Model.ClassModel.AddSubClassOfRelation(c, superClass);
                             }
                             else {

                                 //Raise warning event to inform the user: subclassof relation cannot be imported
                                 //from graph, because definition of class is not found in the model
                                 RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("SubClassOf relation on class '{0}' cannot be imported from graph, because definition of class '{1}' is not found in the model.", c.Value, scof.Object));

                             }
                        }
                    }
                    #endregion

                    #region EquivalentClass
                    foreach (var eqcl in equivclassOf.SelectTriplesBySubject((RDFResource)c.Value)) {
                        if  (eqcl.TripleFlavor   == RDFModelEnums.RDFTripleFlavors.SPO) {
                             var equivClass       = ontology.Model.ClassModel.SelectClass(eqcl.Object.ToString());
                             if (equivClass      != null) {
                                 ontology.Model.ClassModel.AddEquivalentClassRelation(c, equivClass);
                             }
                             else {

                                 //Raise warning event to inform the user: equivalentclass relation cannot be imported
                                 //from graph, because definition of class is not found in the model
                                 RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("EquivalentClass relation on class '{0}' cannot be imported from graph, because definition of class '{1}' is not found in the model.", c.Value, eqcl.Object));

                             }
                        }
                    }
                    #endregion

                    #region DisjointWith
                    foreach (var djwt in disjclassWith.SelectTriplesBySubject((RDFResource)c.Value)) {
                        if  (djwt.TripleFlavor   == RDFModelEnums.RDFTripleFlavors.SPO) {
                             var disjWith         = ontology.Model.ClassModel.SelectClass(djwt.Object.ToString());
                             if (disjWith        != null) {
                                 ontology.Model.ClassModel.AddDisjointWithRelation(c, disjWith);
                             }
                             else {

                                 //Raise warning event to inform the user: disjointwith relation cannot be imported
                                 //from graph, because definition of class is not found in the model
                                 RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("DisjointWith relation on class '{0}' cannot be imported from graph, because definition of class '{1}' is not found in the model.", c.Value, djwt.Object));

                             }
                        }
                    }
                    #endregion

                }
                #endregion

                #region Data Relations

                #region SameAs
                foreach (var t in sameAs) {
                    if  (t.TripleFlavor   == RDFModelEnums.RDFTripleFlavors.SPO) {
                         var subjFct       = ontology.Data.SelectFact(t.Subject.ToString());
                         if (subjFct      != null) {
                             var objFct    = ontology.Data.SelectFact(t.Object.ToString());
                             if (objFct   != null) {
                                 ontology.Data.AddSameAsRelation(subjFct, objFct);
                             }
                             else {

                                 //Raise warning event to inform the user: sameas relation cannot be imported
                                 //from graph, because definition of fact is not found in the data
                                 RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("SameAs relation on fact '{0}' cannot be imported from graph, because definition of fact '{1}' is not found in the data.", t.Subject, t.Object));

                             }
                         }
                         else {

                             //Raise warning event to inform the user: sameas relation cannot be imported
                             //from graph, because definition of fact is not found in the data
                             RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("SameAs relation on fact '{0}' cannot be imported from graph, because its definition is not found in the data.", t.Subject));

                         }
                    }
                }
                #endregion

                #region DifferentFrom
                foreach (var t in differentFrom) {
                    if  (t.TripleFlavor   == RDFModelEnums.RDFTripleFlavors.SPO) {
                         var subjFct       = ontology.Data.SelectFact(t.Subject.ToString());
                         if (subjFct      != null) {
                             var objFct    = ontology.Data.SelectFact(t.Object.ToString());
                             if (objFct   != null) {
                                 ontology.Data.AddDifferentFromRelation(subjFct, objFct);
                             }
                             else {

                                 //Raise warning event to inform the user: differentfrom relation cannot be imported
                                 //from graph, because definition of fact is not found in the data
                                 RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("DifferentFrom relation on fact '{0}' cannot be imported from graph, because definition of fact '{1}' is not found in the data.", t.Subject, t.Object));

                             }
                         }
                         else {

                             //Raise warning event to inform the user: differentfrom relation cannot be imported
                             //from graph, because its definition is not found in the data
                             RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("DifferentFrom relation on fact '{0}' cannot be imported from graph, because its definition is not found in the data.", t.Subject));

                         }
                    }
                }
                #endregion

                #region Assertion
                foreach (var p      in ontology.Model.PropertyModel) {
                    foreach (var t  in ontGraph.SelectTriplesByPredicate((RDFResource)p.Value)) {
                        var subjFct  = ontology.Data.SelectFact(t.Subject.ToString());
                        if (subjFct != null) {
                            if (p.IsObjectProperty()) {
                                if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPO) {
                                    var objFct       = ontology.Data.SelectFact(t.Object.ToString());
                                    if (objFct      != null) {
                                        ontology.Data.AddAssertionRelation(subjFct, (RDFOntologyObjectProperty)p, objFct);
                                    }
                                    else {

                                        //Raise warning event to inform the user: assertion relation cannot be imported
                                        //from graph, because definition of fact is not found in the data
                                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Assertion relation on fact '{0}' cannot be imported from graph, because definition of fact '{1}' is not found in the data.", t.Subject, t.Object));

                                    }
                                }
                                else {

                                    //Raise warning event to inform the user: assertion relation cannot be imported
                                    //from graph, because object property links to a literal
                                    RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Assertion relation on fact '{0}' cannot be imported from graph, because object property '{1}' links to a literal.", t.Subject, p));

                                }
                            }
                            else if (p.IsDatatypeProperty()) {
                                 if (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                                     ontology.Data.AddAssertionRelation(subjFct, (RDFOntologyDatatypeProperty)p, new RDFOntologyLiteral((RDFLiteral)t.Object));
                                 }
                                 else {

                                     //Raise warning event to inform the user: assertion relation cannot be imported
                                     //from graph, because datatype property links to a fact
                                     RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Assertion relation on fact '{0}' cannot be imported from graph, because datatype property '{1}' links to a fact.", t.Subject, p));

                                 }
                            }
                        }
                        else {

                            //Raise warning event to inform the user: assertion relation cannot be imported
                            //from graph, because definition of fact is not found in the data
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Assertion relation on fact '{0}' cannot be imported from graph, because definition of the fact is not found in the data. Ensure its classtype relation is specified.", t.Subject));

                        }
                    }
                }
                #endregion

                #endregion

                #region Annotations

                #region Ontology

                #region VersionInfo
                foreach (var t in versionInfo.SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                         ontology.AddVersionInfoAnnotation(new RDFOntologyLiteral((RDFLiteral)t.Object));
                    }
                    else {

                        //Raise warning event to inform the user: versioninfo annotation on ontology 
                        //cannot be imported from graph, because it does not link a literal
                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("VersionInfo annotation on ontology '{0}' cannot be imported from graph, because it does not link a literal.", ontology.Value, t.Object));

                    }
                }
                #endregion

                #region Comment
                foreach (var t in comment.SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                         ontology.AddCommentAnnotation(new RDFOntologyLiteral((RDFLiteral)t.Object));
                    }
                    else {

                        //Raise warning event to inform the user: comment annotation on ontology 
                        //cannot be imported from graph, because it does not link a literal
                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Comment annotation on ontology '{0}' cannot be imported from graph, because it does not link a literal.", ontology.Value, t.Object));

                    }
                }
                #endregion

                #region Label
                foreach (var t in label.SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                         ontology.AddLabelAnnotation(new RDFOntologyLiteral((RDFLiteral)t.Object));
                    }
                    else {

                        //Raise warning event to inform the user: label annotation on ontology 
                        //cannot be imported from graph, because it does not link a literal
                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Label annotation on ontology '{0}' cannot be imported from graph, because it does not link a literal.", ontology.Value, t.Object));

                    }
                }
                #endregion

                #region SeeAlso
                foreach (var t in seeAlso.SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                         ontology.AddSeeAlsoAnnotation(new RDFOntologyLiteral((RDFLiteral)t.Object));
                    }
                    else {
                        RDFOntologyResource resource = ontology.Model.ClassModel.SelectClass(t.Object.ToString());
                        if (resource         == null) {
                            resource          = ontology.Model.PropertyModel.SelectProperty(t.Object.ToString());
                            if (resource     == null) {
                                resource      = ontology.Data.SelectFact(t.Object.ToString());
                                if (resource == null) {
                                    resource  = new RDFOntologyResource();
                                    resource.Value           = t.Object;
                                    resource.PatternMemberID = t.Object.PatternMemberID;

                                    //Raise warning event to inform the user: seealso annotation on ontology 
                                    //has been imported from graph, but linking an unknown generic resource
                                    RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("SeeAlso annotation on ontology '{0}' has been imported from graph, but linking an unknown generic resource '{1}'.", ontology.Value, t.Object));

                                }
                            }
                        }
                        ontology.AddSeeAlsoAnnotation(resource);
                    }
                }
                #endregion

                #region IsDefinedBy
                foreach (var t in isDefinedBy.SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                         ontology.AddIsDefinedByAnnotation(new RDFOntologyLiteral((RDFLiteral)t.Object));
                    }
                    else {
                        RDFOntologyResource isDefBy = ontology.Model.ClassModel.SelectClass(t.Object.ToString());
                        if (isDefBy         == null) {
                            isDefBy          = ontology.Model.PropertyModel.SelectProperty(t.Object.ToString());
                            if (isDefBy     == null) {
                                isDefBy      = ontology.Data.SelectFact(t.Object.ToString());
                                if (isDefBy == null) {
                                    isDefBy  = new RDFOntologyResource();
                                    isDefBy.Value           = t.Object;
                                    isDefBy.PatternMemberID = t.Object.PatternMemberID;

                                    //Raise warning event to inform the user: isdefinedby annotation on ontology 
                                    //has been imported from graph, but linking an unknown generic resource
                                    RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("IsDefinedBy annotation on ontology '{0}' has been imported from graph, but linking an unknown generic resource '{1}'.", ontology.Value, t.Object));

                                }
                            }
                        }
                        ontology.AddIsDefinedByAnnotation(isDefBy);
                    }
                }
                #endregion

                #region BackwardCompatibleWith
                foreach (var t in bcwcompWith.SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                         ontology.AddBackwardCompatibleWithAnnotation(new RDFOntology((RDFResource)t.Object));
                    }
                    else {

                        //Raise warning event to inform the user: backwardcompatiblewith annotation on ontology 
                        //cannot be imported from graph, because it does not link a resource
                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("BackwardCompatibleWith annotation on ontology '{0}' cannot be imported from graph, because it does not link a resource.", ontology.Value, t.Object));

                    }
                }
                #endregion

                #region IncompatibleWith
                foreach (var t in incompWith.SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                         ontology.AddIncompatibleWithAnnotation(new RDFOntology((RDFResource)t.Object));
                    }
                    else {

                        //Raise warning event to inform the user: incompatiblewith annotation on ontology 
                        //cannot be imported from graph, because it does not link a resource
                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("IncompatibleWith annotation on ontology '{0}' cannot be imported from graph, because it does not link a resource.", ontology.Value, t.Object));

                    }
                }
                #endregion

                #region PriorVersion
                foreach (var t in priorVersion.SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) {
                         ontology.AddPriorVersionAnnotation(new RDFOntology((RDFResource)t.Object));
                    }
                    else {

                        //Raise warning event to inform the user: priorversion annotation on ontology 
                        //cannot be imported from graph, because it does not link a resource
                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("PriorVersion annotation on ontology '{0}' cannot be imported from graph, because it does not link a resource.", ontology.Value, t.Object));

                    }
                }
                #endregion

                #region Imports
                foreach(var t in imports.SelectTriplesBySubject((RDFResource)ontology.Value)) {
                    if (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPO) {
                        ontology.AddImportsAnnotation(new RDFOntology((RDFResource)t.Object));
                    }
                    else {

                        //Raise warning event to inform the user: imports annotation on ontology 
                        //cannot be imported from graph, because it does not link a resource
                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Imports annotation on ontology '{0}' cannot be imported from graph, because it does not link a resource.", ontology.Value, t.Object));

                    }
                }
                #endregion

                #region CustomAnnotations
                var annotProps   = ontology.Model.PropertyModel.AnnotationPropertiesEnumerator;
                while (annotProps.MoveNext()) {
                    foreach (var t in ontGraph.SelectTriplesBySubject((RDFResource)ontology.Value)
                                              .SelectTriplesByPredicate((RDFResource)annotProps.Current.Value)) {
                        if  (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.AddCustomAnnotation((RDFOntologyAnnotationProperty)annotProps.Current, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {
                            RDFOntologyResource custAnn = ontology.Model.ClassModel.SelectClass(t.Object.ToString());
                            if (custAnn         == null) {
                                custAnn          = ontology.Model.PropertyModel.SelectProperty(t.Object.ToString());
                                if (custAnn     == null) {
                                    custAnn      = ontology.Data.SelectFact(t.Object.ToString());
                                    if (custAnn == null) {
                                        custAnn  = new RDFOntologyResource();
                                        custAnn.Value           = t.Object;
                                        custAnn.PatternMemberID = t.Object.PatternMemberID;

                                        //Raise warning event to inform the user: custom annotation on ontology 
                                        //has been imported from graph, but linking an unknown generic resource
                                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Custom annotation '{0}' on ontology '{1}' has been imported from graph, but linking an unknown generic resource '{2}'.", annotProps.Current.Value, ontology.Value, t.Object));

                                    }
                                }
                            }
                            ontology.AddCustomAnnotation((RDFOntologyAnnotationProperty)annotProps.Current, custAnn);
                        }

                    }
                }
                #endregion

                #endregion

                #region Classes
                foreach (var c in ontology.Model.ClassModel) {

                    #region VersionInfo
                    foreach (var t in versionInfo.SelectTriplesBySubject((RDFResource)c.Value)) {
                        if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Model.ClassModel.AddVersionInfoAnnotation(c, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {

                            //Raise warning event to inform the user: versioninfo annotation on class 
                            //cannot be imported from graph, because it does not link a literal
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("VersionInfo annotation on class '{0}' cannot be imported from graph, because it does not link a literal.", c.Value, t.Object));

                        }
                    }
                    #endregion

                    #region Comment
                    foreach (var t in comment.SelectTriplesBySubject((RDFResource)c.Value)) {
                        if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Model.ClassModel.AddCommentAnnotation(c, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {

                            //Raise warning event to inform the user: comment annotation on class 
                            //cannot be imported from graph, because it does not link a literal
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Comment annotation on class '{0}' cannot be imported from graph, because it does not link a literal.", c.Value, t.Object));

                        }
                    }
                    #endregion

                    #region Label
                    foreach (var t in label.SelectTriplesBySubject((RDFResource)c.Value)) {
                        if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Model.ClassModel.AddLabelAnnotation(c, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {

                            //Raise warning event to inform the user: label annotation on class 
                            //cannot be imported from graph, because it does not link a literal
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Label annotation on class '{0}' cannot be imported from graph, because it does not link a literal.", c.Value, t.Object));

                        }
                    }
                    #endregion

                    #region SeeAlso
                    foreach (var t in seeAlso.SelectTriplesBySubject((RDFResource)c.Value)) {
                        if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Model.ClassModel.AddSeeAlsoAnnotation(c, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {
                            RDFOntologyResource resource = ontology.Model.ClassModel.SelectClass(t.Object.ToString());
                            if (resource         == null) {
                                resource          = ontology.Model.PropertyModel.SelectProperty(t.Object.ToString());
                                if (resource     == null) {
                                    resource      = ontology.Data.SelectFact(t.Object.ToString());
                                    if (resource == null) {
                                        resource  = new RDFOntologyResource();
                                        resource.Value           = t.Object;
                                        resource.PatternMemberID = t.Object.PatternMemberID;

                                        //Raise warning event to inform the user: seealso annotation on class 
                                        //has been imported from graph, but linking an unknown generic resource
                                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("SeeAlso annotation on class '{0}' has been imported from graph, but linking an unknown generic resource '{1}'.", c.Value, t.Object));

                                    }
                                }
                            }
                            ontology.Model.ClassModel.AddSeeAlsoAnnotation(c, resource);
                        }
                    }
                    #endregion

                    #region IsDefinedBy
                    foreach (var t in isDefinedBy.SelectTriplesBySubject((RDFResource)c.Value)) {
                        if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Model.ClassModel.AddIsDefinedByAnnotation(c, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {
                            RDFOntologyResource isDefBy = ontology.Model.ClassModel.SelectClass(t.Object.ToString());
                            if (isDefBy         == null) {
                                isDefBy          = ontology.Model.PropertyModel.SelectProperty(t.Object.ToString());
                                if (isDefBy     == null) {
                                    isDefBy      = ontology.Data.SelectFact(t.Object.ToString());
                                    if (isDefBy == null) {
                                        isDefBy  = new RDFOntologyResource();
                                        isDefBy.Value           = t.Object;
                                        isDefBy.PatternMemberID = t.Object.PatternMemberID;

                                        //Raise warning event to inform the user: isdefinedby annotation on class 
                                        //has been imported from graph, but linking an unknown generic resource
                                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("IsDefinedBy annotation on class '{0}' has been imported from graph, but linking an unknown generic resource '{1}'.", c.Value, t.Object));

                                    }
                                }
                            }
                            ontology.Model.ClassModel.AddIsDefinedByAnnotation(c, isDefBy);
                        }
                    }
                    #endregion

                    #region CustomAnnotations
                    annotProps       = ontology.Model.PropertyModel.AnnotationPropertiesEnumerator;
                    while (annotProps.MoveNext()) {
                        foreach (var t in ontGraph.SelectTriplesBySubject((RDFResource)c.Value)
                                                  .SelectTriplesByPredicate((RDFResource)annotProps.Current.Value)) {
                            if  (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                                 ontology.Model.ClassModel.AddCustomAnnotation(c, (RDFOntologyAnnotationProperty)annotProps.Current, new RDFOntologyLiteral((RDFLiteral)t.Object));
                            }
                            else {
                                RDFOntologyResource custAnn = ontology.Model.ClassModel.SelectClass(t.Object.ToString());
                                if (custAnn         == null) {
                                    custAnn          = ontology.Model.PropertyModel.SelectProperty(t.Object.ToString());
                                    if (custAnn     == null) {
                                        custAnn      = ontology.Data.SelectFact(t.Object.ToString());
                                        if (custAnn == null) {
                                            custAnn  = new RDFOntologyResource();
                                            custAnn.Value           = t.Object;
                                            custAnn.PatternMemberID = t.Object.PatternMemberID;

                                            //Raise warning event to inform the user: custom annotation on class 
                                            //has been imported from graph, but linking an unknown generic resource
                                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Custom annotation '{0}' on class '{1}' has been imported from graph, but linking an unknown generic resource '{2}'.", annotProps.Current.Value, c.Value, t.Object));

                                        }
                                    }
                                }
                                ontology.Model.ClassModel.AddCustomAnnotation(c, (RDFOntologyAnnotationProperty)annotProps.Current, custAnn);
                            }

                        }
                    }
                    #endregion

                }
                #endregion

                #region Properties
                foreach (var p in ontology.Model.PropertyModel) {

                    #region VersionInfo
                    foreach (var t in versionInfo.SelectTriplesBySubject((RDFResource)p.Value)) {
                        if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Model.PropertyModel.AddVersionInfoAnnotation(p, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {

                            //Raise warning event to inform the user: versioninfo annotation on property 
                            //cannot be imported from graph, because it does not link a literal
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("VersionInfo annotation on property '{0}' cannot be imported from graph, because it does not link a literal.", p.Value, t.Object));

                        }
                    }
                    #endregion

                    #region Comment
                    foreach (var t in comment.SelectTriplesBySubject((RDFResource)p.Value)) {
                        if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Model.PropertyModel.AddCommentAnnotation(p, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {

                            //Raise warning event to inform the user: comment annotation on property 
                            //cannot be imported from graph, because it does not link a literal
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Comment annotation on property '{0}' cannot be imported from graph, because it does not link a literal.", p.Value, t.Object));

                        }
                    }
                    #endregion

                    #region Label
                    foreach (var t in label.SelectTriplesBySubject((RDFResource)p.Value)) {
                        if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Model.PropertyModel.AddLabelAnnotation(p, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {

                            //Raise warning event to inform the user: label annotation on property 
                            //cannot be imported from graph, because it does not link a literal
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Label annotation on property '{0}' cannot be imported from graph, because it does not link a literal.", p.Value, t.Object));

                        }
                    }
                    #endregion

                    #region SeeAlso
                    foreach (var t in seeAlso.SelectTriplesBySubject((RDFResource)p.Value)) {
                        if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Model.PropertyModel.AddSeeAlsoAnnotation(p, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {
                            RDFOntologyResource resource = ontology.Model.ClassModel.SelectClass(t.Object.ToString());
                            if (resource         == null) {
                                resource          = ontology.Model.PropertyModel.SelectProperty(t.Object.ToString());
                                if (resource     == null) {
                                    resource      = ontology.Data.SelectFact(t.Object.ToString());
                                    if (resource == null) {
                                        resource  = new RDFOntologyResource();
                                        resource.Value           = t.Object;
                                        resource.PatternMemberID = t.Object.PatternMemberID;

                                        //Raise warning event to inform the user: seealso annotation on property 
                                        //has been imported from graph, but linking an unknown generic resource
                                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("SeeAlso annotation on property '{0}' has been imported from graph, but linking an unknown generic resource '{1}'.", p.Value, t.Object));

                                    }
                                }
                            }
                            ontology.Model.PropertyModel.AddSeeAlsoAnnotation(p, resource);
                        }
                    }
                    #endregion

                    #region IsDefinedBy
                    foreach (var t in isDefinedBy.SelectTriplesBySubject((RDFResource)p.Value)) {
                        if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Model.PropertyModel.AddIsDefinedByAnnotation(p, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {
                            RDFOntologyResource isDefBy = ontology.Model.ClassModel.SelectClass(t.Object.ToString());
                            if (isDefBy         == null) {
                                isDefBy          = ontology.Model.PropertyModel.SelectProperty(t.Object.ToString());
                                if (isDefBy     == null) {
                                    isDefBy      = ontology.Data.SelectFact(t.Object.ToString());
                                    if (isDefBy == null) {
                                        isDefBy  = new RDFOntologyResource();
                                        isDefBy.Value           = t.Object;
                                        isDefBy.PatternMemberID = t.Object.PatternMemberID;

                                        //Raise warning event to inform the user: isdefinedby annotation on property 
                                        //has been imported from graph, but linking an unknown generic resource
                                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("IsDefinedBy annotation on property '{0}' has been imported from graph, but linking an unknown generic resource '{1}'.", p.Value, t.Object));

                                    }
                                }
                            }
                            ontology.Model.PropertyModel.AddIsDefinedByAnnotation(p, isDefBy);
                        }
                    }
                    #endregion

                    #region CustomAnnotations
                    annotProps       = ontology.Model.PropertyModel.AnnotationPropertiesEnumerator;
                    while (annotProps.MoveNext()) {
                        foreach (var t in ontGraph.SelectTriplesBySubject((RDFResource)p.Value)
                                                  .SelectTriplesByPredicate((RDFResource)annotProps.Current.Value)) {
                            if  (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                                 ontology.Model.PropertyModel.AddCustomAnnotation(p, (RDFOntologyAnnotationProperty)annotProps.Current, new RDFOntologyLiteral((RDFLiteral)t.Object));
                            }
                            else {
                                RDFOntologyResource custAnn = ontology.Model.ClassModel.SelectClass(t.Object.ToString());
                                if (custAnn         == null) {
                                    custAnn          = ontology.Model.PropertyModel.SelectProperty(t.Object.ToString());
                                    if (custAnn     == null) {
                                        custAnn      = ontology.Data.SelectFact(t.Object.ToString());
                                        if (custAnn == null) {
                                            custAnn  = new RDFOntologyResource();
                                            custAnn.Value           = t.Object;
                                            custAnn.PatternMemberID = t.Object.PatternMemberID;

                                            //Raise warning event to inform the user: custom annotation on property 
                                            //has been imported from graph, but linking an unknown generic resource
                                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Custom annotation '{0}' on property '{1}' has been imported from graph, but linking an unknown generic resource '{2}'.", annotProps.Current.Value, p.Value, t.Object));

                                        }
                                    }
                                }
                                ontology.Model.PropertyModel.AddCustomAnnotation(p, (RDFOntologyAnnotationProperty)annotProps.Current, custAnn);
                            }

                        }
                    }
                    #endregion

                }
                #endregion

                #region Facts
                foreach (var f in ontology.Data) {

                    #region VersionInfo
                    foreach (var t in versionInfo.SelectTriplesBySubject((RDFResource)f.Value)) {
                        if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Data.AddVersionInfoAnnotation(f, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {

                            //Raise warning event to inform the user: versioninfo annotation on fact 
                            //cannot be imported from graph, because it does not link a literal
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("VersionInfo annotation on fact '{0}' cannot be imported from graph, because it does not link a literal.", f.Value, t.Object));

                        }
                    }
                    #endregion

                    #region Comment
                    foreach (var t in comment.SelectTriplesBySubject((RDFResource)f.Value)) {
                        if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Data.AddCommentAnnotation(f, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {

                            //Raise warning event to inform the user: comment annotation on fact 
                            //cannot be imported from graph, because it does not link a literal
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Comment annotation on fact '{0}' cannot be imported from graph, because it does not link a literal.", f.Value, t.Object));

                        }
                    }
                    #endregion

                    #region Label
                    foreach (var t in label.SelectTriplesBySubject((RDFResource)f.Value)) {
                        if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Data.AddLabelAnnotation(f, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {

                            //Raise warning event to inform the user: label annotation on fact 
                            //cannot be imported from graph, because it does not link a literal
                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Label annotation on fact '{0}' cannot be imported from graph, because it does not link a literal.", f.Value, t.Object));

                        }
                    }
                    #endregion

                    #region SeeAlso
                    foreach (var t in seeAlso.SelectTriplesBySubject((RDFResource)f.Value)) {
                        if  (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Data.AddSeeAlsoAnnotation(f, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {
                            RDFOntologyResource resource = ontology.Model.ClassModel.SelectClass(t.Object.ToString());
                            if (resource         == null) {
                                resource          = ontology.Model.PropertyModel.SelectProperty(t.Object.ToString());
                                if (resource     == null) {
                                    resource      = ontology.Data.SelectFact(t.Object.ToString());
                                    if (resource == null) {
                                        resource  = new RDFOntologyResource();
                                        resource.Value           = t.Object;
                                        resource.PatternMemberID = t.Object.PatternMemberID;

                                        //Raise warning event to inform the user: seealso annotation on fact 
                                        //has been imported from graph, but linking an unknown generic resource
                                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("SeeAlso annotation on fact '{0}' has been imported from graph, but linking an unknown generic resource '{1}'.", f.Value, t.Object));

                                    }
                                }
                            }
                            ontology.Data.AddSeeAlsoAnnotation(f, resource);
                        }
                    }
                    #endregion

                    #region IsDefinedBy
                    foreach (var t in isDefinedBy.SelectTriplesBySubject((RDFResource)f.Value)) {
                        if  (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL) {
                             ontology.Data.AddIsDefinedByAnnotation(f, new RDFOntologyLiteral((RDFLiteral)t.Object));
                        }
                        else {
                            RDFOntologyResource isDefBy = ontology.Model.ClassModel.SelectClass(t.Object.ToString());
                            if (isDefBy         == null) {
                                isDefBy          = ontology.Model.PropertyModel.SelectProperty(t.Object.ToString());
                                if (isDefBy     == null) {
                                    isDefBy      = ontology.Data.SelectFact(t.Object.ToString());
                                    if (isDefBy == null) {
                                        isDefBy  = new RDFOntologyResource();
                                        isDefBy.Value           = t.Object;
                                        isDefBy.PatternMemberID = t.Object.PatternMemberID;

                                        //Raise warning event to inform the user: isdefinedby annotation on fact 
                                        //has been imported from graph, but linking an unknown generic resource
                                        RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("IsDefinedBy annotation on fact '{0}' has been imported from graph, but linking an unknown generic resource '{1}'.", f.Value, t.Object));

                                    }
                                }
                            }
                            ontology.Data.AddIsDefinedByAnnotation(f, isDefBy);
                        }
                    }
                    #endregion

                    #region CustomAnnotations
                    annotProps       = ontology.Model.PropertyModel.AnnotationPropertiesEnumerator;
                    while (annotProps.MoveNext()) {
                        foreach (var t in ontGraph.SelectTriplesBySubject((RDFResource)f.Value)
                                                  .SelectTriplesByPredicate((RDFResource)annotProps.Current.Value)) {
                            if  (t.TripleFlavor  == RDFModelEnums.RDFTripleFlavors.SPL) {
                                 ontology.Data.AddCustomAnnotation(f, (RDFOntologyAnnotationProperty)annotProps.Current, new RDFOntologyLiteral((RDFLiteral)t.Object));
                            }
                            else {
                                RDFOntologyResource custAnn = ontology.Model.ClassModel.SelectClass(t.Object.ToString());
                                if (custAnn         == null) {
                                    custAnn          = ontology.Model.PropertyModel.SelectProperty(t.Object.ToString());
                                    if (custAnn     == null) {
                                        custAnn      = ontology.Data.SelectFact(t.Object.ToString());
                                        if (custAnn == null) {
                                            custAnn  = new RDFOntologyResource();
                                            custAnn.Value           = t.Object;
                                            custAnn.PatternMemberID = t.Object.PatternMemberID;

                                            //Raise warning event to inform the user: custom annotation on fact 
                                            //has been imported from graph, but linking an unknown generic resource
                                            RDFSemanticsEvents.RaiseSemanticsWarning(String.Format("Custom annotation '{0}' on fact '{1}' has been imported from graph, but linking an unknown generic resource '{2}'.", annotProps.Current.Value, f.Value, t.Object));

                                        }
                                    }
                                }
                                ontology.Data.AddCustomAnnotation(f, (RDFOntologyAnnotationProperty)annotProps.Current, custAnn);
                            }

                        }
                    }
                    #endregion

                }
                #endregion

                #endregion

                #endregion

                #endregion

            }
            return ontology;
        }