public void test9(string file) { IOwlParser parser = new OwlXmlParser(); IOwlGraph graph = parser.ParseOwl(file); Console.WriteLine("Retrieving some specific data:"); // Here we will retrieve the enumerator in order to get all the nodes from the file IDictionaryEnumerator nEnumerator = (IDictionaryEnumerator)graph.Nodes.GetEnumerator(); while (nEnumerator.MoveNext()) { // Get the node from the graph OwlNode node = (OwlNode)graph.Nodes[(nEnumerator.Key).ToString()]; // We will cast the node to a OwlIndividual because we are looking for individuals OwlIndividual indNode = node as OwlIndividual; // If indNode is different from null, then we are dealing with an OwlIndividual -> OK // If the indNode is not anonymous, means that we have an individual with a proper name -> OK if ((indNode != null) && (!indNode.IsAnonymous())) { // So, now we have a good owl-individual Console.WriteLine(indNode.ID); } } }
public MindMapOntology(string fileName) { Concepts = new Dictionary <string, MindMapConcept>(); IOwlGraph graph; IOwlParser parser = new OwlXmlParser(); object o = parser.ParseOwl(fileName); graph = (IOwlGraph)o; foreach (DictionaryEntry entry in graph.Nodes) { IOwlNode node = (IOwlNode)entry.Value; if (node.GetType() == typeof(OwlAnnotationProperty) && node.ID != eMaplex && node.ID != eDefinition) { annotations.Add(node.ID, ((string)entry.Key).Substring(eAnnotation.Length)); } } foreach (DictionaryEntry entry in graph.Literals) { IOwlLiteral literal = (IOwlLiteral)entry.Value; if (literal.ParentEdges[0].ParentNode.ChildEdges[eLabel, 0] == null || ((IOwlLiteral)(literal.ParentEdges[0].ParentNode.ChildEdges[eLabel, 0].ChildNode)) != literal) { continue; } MindMapConcept concept = new MindMapConcept(); concept.Name = literal.ID; concept.Defintion = GetDefinition(literal); concept.VisualProperties = GetVisualFeatures(literal); concept.NonVisualProperties = GetNonVisualFeatures(literal); concept.Maplex = GetMaplex(literal); if (concept.Name.ToUpper() != "THING" && GetParent(literal) != null) { concept.ParentConceptName = GetParent(literal).ID; } else { concept.ParentConceptName = ""; } Concepts.Add(concept.Name, concept); //Concepts.Add(concept.Name.Split('^')[0], concept); } foreach (KeyValuePair <string, MindMapConcept> concept in Concepts) { if (concept.Value.Name.ToUpper() != "THING" && concept.Value.ParentConceptName != null && concept.Value.ParentConceptName != "") { try { concept.Value.Parent = Concepts[concept.Value.ParentConceptName]; } catch (Exception) { concept.Value.Parent = null; } } else { concept.Value.Parent = null; } } }
public void test6(string file) { IOwlParser parser = new OwlXmlParser(); IOwlGraph graph = parser.ParseOwl(file); IOwlGenerator generator = new OwlXmlGenerator(); generator.GenerateOwl(graph, @"c:\travelnew.owl"); }
private void LoadOntology() { LogWriter.Clear(); LogWriter.Write("Загрузка онтологии: " + Path); OwlXmlParser parser = new OwlXmlParser(); LogWriter.Write("Парсинг файла онтологии..."); OwlGraph = (OwlGraph)parser.ParseOwl(Path); LogWriter.Write(string.Format("Парсинг онтологии завершен с {0} ошибками и {1} предупреждениями.", ((OwlParser)parser).Errors.Count, ((OwlParser)parser).Warnings.Count)); LogWriter.Write(string.Format("Онтологический граф построен и содержит {0} вершин и {1} граней.", OwlGraph.Nodes.Count, OwlGraph.Edges.Count)); ClassificationOwlNodes(); WriteTree(); UpdateComboBox(); }
public void test5(string file) { IOwlParser parser = new OwlXmlParser(); IOwlGraph graph = parser.ParseOwl(file); string baseUri = "http://www.owl-ontologies.com/travel.owl#"; OwlClass hotelNode = (OwlClass)graph.Nodes["http://www.owl-ontologies.com/travel.owl#LuxuryHotel"]; OwlIndividual newHotel = new OwlIndividual(baseUri + "PellensPalace", hotelNode); graph.Nodes.Add(newHotel); IOwlGenerator generator = new OwlXmlGenerator(); generator.GenerateOwl(graph, @"c:\travelnew.owl"); }
public void test7(string file) { // First of all, we will create the parser object and parse the // file that we want IOwlParser parser = new OwlXmlParser(); IOwlGraph graph = parser.ParseOwl(file); // Lookup the instance of course for which we want to search for // the prerequisites, in our ontology, this is CourseA OwlIndividual instanceCourseA = (OwlIndividual)graph.Nodes["http://www.owl-ontologies.com/test.owl#CourseA"]; // With this loop, we will go through all the nodes in the graph IDictionaryEnumerator nEnumerator = (IDictionaryEnumerator)graph.Nodes.GetEnumerator(); while (nEnumerator.MoveNext()) { OwlNode node = (OwlNode)graph.Nodes[(nEnumerator.Key).ToString()]; // If the node we are encountering is an individual (so an // instance, then we will continue OwlIndividual i = node as OwlIndividual; if (i != null) { Console.WriteLine("Course: " + i.ID); // For this node, we will now look for all the edges that // we want, in our case the isPrerequisite edges IOwlEdgeList prerequisiteEdges = (IOwlEdgeList)node.ChildEdges["http://www.owl-ontologies.com/test.owl#isPrerequisite"]; if (prerequisiteEdges != null) { // Finally, a loop over all the edges and if we // encounter one which has an equal id to our // instance, then print the OK. foreach (OwlEdge s in prerequisiteEdges) { if (s.ChildNode.ID == instanceCourseA.ID) { Console.WriteLine("-- Ok"); } } } } } }
public void test10(string file) { IOwlParser parser = new OwlXmlParser(); IOwlGraph graph = parser.ParseOwl(file); IOwlGenerator generator = new OwlXmlGenerator(); ArrayList errors = ((OwlGenerator)generator).Errors; ArrayList warnings = ((OwlGenerator)generator).Warnings; ArrayList messages = ((OwlGenerator)generator).Messages; FileStream info = new FileStream("c:/info.txt", FileMode.OpenOrCreate); StreamWriter sw = new StreamWriter(info); sw.AutoFlush = true; generator.GenerateOwl(graph, @"c:\test.owl"); foreach (string msg in messages) { sw.WriteLine(msg); } }
public void test3(string file) { IOwlParser parser = new OwlXmlParser(); IOwlGraph graph = parser.ParseOwl(file); Console.WriteLine("The nodes of the graph are:"); IDictionaryEnumerator nEnumerator = (IDictionaryEnumerator)graph.Nodes.GetEnumerator(); while (nEnumerator.MoveNext()) { OwlNode node = (OwlNode)graph.Nodes[(nEnumerator.Key).ToString()]; if (!node.IsAnonymous()) { Console.WriteLine(node.ID); } } Console.WriteLine(Environment.NewLine); Console.WriteLine("Retrieving some specific data:"); IOwlNode hotelNode = (IOwlNode)graph.Nodes["http://www.owl-ontologies.com/travel.owl#Hotel"]; Console.WriteLine(Environment.NewLine); Console.WriteLine("The edges are: "); OwlEdgeCollection edges = (OwlEdgeCollection)hotelNode.ChildEdges; foreach (OwlEdge e in edges) { Console.WriteLine(e.ID); } Console.WriteLine("The subClassOf edges are:"); IOwlEdgeList subclassEdges = (IOwlEdgeList)hotelNode.ChildEdges["http://www.w3.org/2000/01/rdf-schema#subClassOf"]; foreach (OwlEdge s in subclassEdges) { Console.WriteLine(s.ChildNode.ID); } }
public void test4(string file) { IOwlParser parser = new OwlXmlParser(); IOwlGraph graph = parser.ParseOwl(file); Console.WriteLine("Retrieving some specific data:"); // Here we will retrieve the enumerator in order to get all the nodes from the file IDictionaryEnumerator nEnumerator = (IDictionaryEnumerator)graph.Nodes.GetEnumerator(); while (nEnumerator.MoveNext()) { // Get the node from the graph OwlNode node = (OwlNode)graph.Nodes[(nEnumerator.Key).ToString()]; // We will cast the node to a OwlClass because we are looking for classes OwlClass clsNode = node as OwlClass; // If clsNode is different from null, then we are dealing with an OwlClass -> OK // If the clsNode is not anonymous, means that we have a class with a proper name -> OK if ((clsNode != null) && (!clsNode.IsAnonymous())) { // So, now we have a good owl-class, we will look for any subClassOf relations (edges) IOwlEdgeList subclassEdges = (IOwlEdgeList)node.ChildEdges["http://www.w3.org/2000/01/rdf-schema#subClassOf"]; if (subclassEdges != null) { // We will list all the edges and check if the target of the edge is the class we want to // have as the superclass foreach (OwlEdge s in subclassEdges) { if (s.ChildNode.ID == "http://www.owl-ontologies.com/travel.owl#Accommodation") { Console.WriteLine(node.ID); } } } } } }
public void test1(string file) { IOwlParser parser = new OwlXmlParser(); IOwlGraph graph = parser.ParseOwl(file); ArrayList errors = ((OwlParser)parser).Errors; ArrayList warnings = ((OwlParser)parser).Warnings; ArrayList messages = ((OwlParser)parser).Messages; //FileStream info = new FileStream("c:/info.txt", FileMode.OpenOrCreate); //StreamWriter sw = new StreamWriter(info); //sw.AutoFlush = true; //IOwlGenerator generator = new OwlXmlGenerator(); //generator.GenerateOwl(graph, @"c:\example1.owl"); //info = new FileStream("C:/generated.txt", FileMode.OpenOrCreate); //sw = new StreamWriter(info); //sw.AutoFlush = true; // foreach(string msg in messages) // { // sw.WriteLine(msg); // } // // Console.WriteLine("Graph parsed successfully with {0} errors and {1} warnings\n\n",errors.Count,warnings.Count); // // foreach(string err in errors) // { // Console.WriteLine("Error: "+err); // } // // foreach(string war in warnings) // { // Console.WriteLine("Warning: "+war); // } // // Console.WriteLine("The graph contains {0} node(s) and {1} edge(s).", graph.Nodes.Count, graph.Edges.Count); string baseuri = graph.NameSpaces["xmlns"]; Console.WriteLine("The basuri is " + baseuri); OwlClass owlAnnotation = new OwlClass(baseuri + "Annotation"); //Create a parent (Annotation) node to relate to OwlGraph newGraph = new OwlGraph(); newGraph.NameSpaces["xmlns:" + OwlNamespaceCollection.OwlNamespacePrefix] = OwlNamespaceCollection.OwlNamespace; newGraph.NameSpaces["xmlns:" + OwlNamespaceCollection.RdfSchemaNamespacePrefix] = OwlNamespaceCollection.RdfSchemaNamespace; newGraph.NameSpaces["xmlns:daml"] = "http://www.daml.org/2001/03/daml+oil#"; newGraph.NameSpaces["xmlns:domain"] = "http://www.owl-ontologies.com/domain.owl#"; newGraph.NameSpaces["xmlns:dc"] = "http://purl.org/dc/elements/1.1/"; newGraph.NameSpaces["xmlns"] = "http://www.owl-ontologies.com/test.owl#"; newGraph.NameSpaces["xml:base"] = "http://www.owl-ontologies.com/test.owl"; newGraph.NameSpaces["xmlns:meta"] = baseuri; string newUri = "http://www.owl-ontologies.com/test.owl#"; OwlIndividual pp = new OwlIndividual(newUri + "Test", owlAnnotation); //Create the annotation newGraph.Nodes.Add(pp); IOwlGenerator generator = new OwlXmlGenerator(); generator.GenerateOwl(newGraph, @"c:\example2.owl"); }
public void OWLtoPIM() { Dictionary <string, Assoc> Assocs = new Dictionary <string, Assoc>(); Dictionary <string, Attr> Attrs = new Dictionary <string, Attr>(); Dictionary <string, PIMClass> Classes = new Dictionary <string, PIMClass>(); Dictionary <string, Property> Attributes = new Dictionary <string, Property>(); Dictionary <string, Association> Associations = new Dictionary <string, Association>(); List <Generalization> Generalizations = new List <Generalization>(); List <Comment> Comments = new List <Comment>(); OpenFileDialog D = new OpenFileDialog(); D.Filter = "OWL File|*.owl|All files|*.*"; D.Title = "Select OWL file to import"; D.CheckFileExists = true; if (D.ShowDialog() != true) { return; } IOwlParser parser = new OwlXmlParser(); IOwlGraph graph = parser.ParseOwl(D.FileName); ArrayList errors = ((OwlParser)parser).Errors; ArrayList warnings = ((OwlParser)parser).Warnings; ArrayList messages = ((OwlParser)parser).Messages; /*IDictionaryEnumerator nEnumerator = (IDictionaryEnumerator)graph.Nodes.GetEnumerator(); * while (nEnumerator.MoveNext()) * { * OwlNode node = (OwlNode)graph.Nodes[(nEnumerator.Key).ToString()]; * IOwlClass C = node as IOwlClass; * if (C == null || C is IOwlRestriction) continue; * PIMClass pimClass = controller.ModelController.Model.AddClass() as PIMClass; * //TODO: Catch duplicit IDs * Classes.Add(C.ID, pimClass); * pimClass.Name = CutID(C.ID); * pimClass.OntologyEquivalent = C.ID; * }*/ OwlEdgeList TypeEdges = (OwlEdgeList)graph.Edges["http://www.w3.org/1999/02/22-rdf-syntax-ns#type"]; foreach (OwlEdge E in TypeEdges) { if (E.ChildNode.ID == "http://www.w3.org/2002/07/owl#ObjectProperty" && !Assocs.ContainsKey(E.ParentNode.ID)) { Assocs.Add(E.ParentNode.ID, new Assoc() { id = E.ParentNode.ID }); } else if (E.ChildNode.ID == "http://www.w3.org/2002/07/owl#DatatypeProperty" && !Attrs.ContainsKey(E.ParentNode.ID)) { Attrs.Add(E.ParentNode.ID, new Attr() { id = E.ParentNode.ID }); } else if (E.ChildNode.ID == "http://www.w3.org/2002/07/owl#Class") { if (!Classes.ContainsKey(E.ParentNode.ID)) { PIMClass pimClass = controller.ModelController.Model.AddClass() as PIMClass; Classes.Add(E.ParentNode.ID, pimClass); pimClass.Name = CutID(E.ParentNode.ID); pimClass.OntologyEquivalent = E.ParentNode.ID; if (E.ParentNode.ID.IndexOf('#') != -1) { controller.Project.Schema.XMLNamespace = E.ParentNode.ID.Substring(0, E.ParentNode.ID.IndexOf('#')); } } } } OwlEdgeList DomainEdges = (OwlEdgeList)graph.Edges["http://www.w3.org/2000/01/rdf-schema#domain"]; foreach (OwlEdge E in DomainEdges) { if (Assocs.ContainsKey(E.ParentNode.ID)) { Assocs[E.ParentNode.ID].from = E.ChildNode.ID; } if (Attrs.ContainsKey(E.ParentNode.ID)) { Attrs[E.ParentNode.ID].owner = E.ChildNode.ID; } } OwlEdgeList RangeEdges = (OwlEdgeList)graph.Edges["http://www.w3.org/2000/01/rdf-schema#range"]; foreach (OwlEdge E in RangeEdges) { if (Assocs.ContainsKey(E.ParentNode.ID)) { Assocs[E.ParentNode.ID].to = E.ChildNode.ID; } if (Attrs.ContainsKey(E.ParentNode.ID)) { Attrs[E.ParentNode.ID].type = E.ChildNode.ID; } } Report R = new Report(); foreach (Attr A in Attrs.Values) { if (A.owner == null) { R.lb1.Items.Add("Attribute " + A.id + " doesn't have an owner."); continue; } if (!Classes.ContainsKey(A.owner)) { R.lb1.Items.Add("Attribute " + A.id + ": Owner " + A.owner + " not found."); continue; } Property P = Classes[A.owner].AddAttribute(); P.OntologyEquivalent = A.id; Attributes.Add(A.id, P); P.Name = CutID(A.id); P.Default = CutID(A.type); } foreach (Assoc A in Assocs.Values) { if (A.from == null || A.to == null) { R.lb2.Items.Add("Association " + A.id + ": doesn't have from or to."); continue; } List <PIMClass> L = new List <PIMClass>(); if (Classes.ContainsKey(A.from)) { L.Add(Classes[A.from]); } else { R.lb2.Items.Add("Association " + A.id + ": From: " + A.from + " doesn't exist."); continue; } if (Classes.ContainsKey(A.to)) { L.Add(Classes[A.to]); } else { R.lb2.Items.Add("Association " + A.id + ": To: " + A.to + " doesn't exist."); continue; } Association Assoc = controller.ModelController.Model.Schema.AssociateClasses(L); Assoc.OntologyEquivalent = A.id; Assoc.Name = CutID(A.id); Associations.Add(A.id, Assoc); } OwlEdgeList SubClassEdges = (OwlEdgeList)graph.Edges["http://www.w3.org/2000/01/rdf-schema#subClassOf"]; foreach (OwlEdge E in SubClassEdges) { if (Classes.ContainsKey(E.ParentNode.ID) && Classes.ContainsKey(E.ChildNode.ID)) { Generalizations.Add(controller.ModelController.Model.Schema.SetGeneralization(Classes[E.ParentNode.ID], Classes[E.ChildNode.ID])); } else if (!Classes.ContainsKey(E.ChildNode.ID)) { R.lb2.Items.Add(E.ParentNode.ID + " subclassOf " + E.ChildNode.ID + ": Child doesn't exist."); continue; } else if (!Classes.ContainsKey(E.ParentNode.ID)) { R.lb2.Items.Add(E.ParentNode.ID + " subclassOf " + E.ChildNode.ID + ": Parent doesn't exist."); continue; } } OwlEdgeList CommentEdges = (OwlEdgeList)graph.Edges["http://www.w3.org/2000/01/rdf-schema#comment"]; if (CommentEdges != null) { foreach (OwlEdge E in CommentEdges) { if (Classes.ContainsKey(E.ParentNode.ID)) { Comments.Add(Classes[E.ParentNode.ID].AddComment(E.ChildNode.ID)); } else if (Associations.ContainsKey(E.ParentNode.ID)) { Comments.Add(Associations[E.ParentNode.ID].AddComment(E.ChildNode.ID)); } else if (Attributes.ContainsKey(E.ParentNode.ID)) { R.lb2.Items.Add("Comment of " + E.ParentNode.ID + ": XCase doesn't support attribute comments."); } else { R.lb2.Items.Add("Comment of " + E.ParentNode.ID + ": Class/Association doesn't exist."); continue; } } } int i = 0; foreach (PIMClass pimClass in Classes.Values) { ClassViewHelper VH = new ClassViewHelper(controller.Diagram); double rows = Math.Abs(Math.Sqrt(Classes.Count)) + 1; VH.X = (i * 200 + 10) % ((int)rows * 200 + 10); VH.Y = (i / (int)rows) * 200 + 10; controller.Diagram.AddModelElement(pimClass, VH); i++; } foreach (Association A in Associations.Values) { AssociationViewHelper VH = new AssociationViewHelper(controller.Diagram); controller.Diagram.AddModelElement(A, VH); } foreach (Generalization G in Generalizations) { GeneralizationViewHelper VH = new GeneralizationViewHelper(controller.Diagram); controller.Diagram.AddModelElement(G, VH); } foreach (Comment C in Comments) { CommentViewHelper VH = new CommentViewHelper(controller.Diagram); controller.Diagram.AddModelElement(C, VH); } R.Show(); }
private IEnumerator OpenOwl() { string filename = m_OwlFile; IOwlParser parser = new OwlXmlParser(); m_OwlGraph = parser.ParseOwl(filename); m_numNodes = m_OwlGraph.Nodes.Count; m_winText.text += "There are " + m_numNodes + " node(s) in the ontology '" + m_OwlFile + "'\n"; string oldText = m_winText.text; IDictionaryEnumerator nodeIter = (IDictionaryEnumerator)m_OwlGraph.Nodes.GetEnumerator(); int cnt = 0; while (nodeIter.MoveNext()) { //string owlKey = (nodeIter.Key).ToString(); string owlKey = ((OwlNode)nodeIter.Value).ID; OwlNode owlNode = (OwlNode)nodeIter.Value; if (owlNode.IsAnonymous()) { continue; } cnt++; NodeInstance graphNode = new NodeInstance(); graphNode.m_owlNode = owlNode; graphNode.m_pathSegments = new ArrayList(); //Debug.Log("owlKey = <" + owlKey + ">"); //Debug.Log("owlNode = " + owlNode); var uri = new Uri(owlKey); //Debug.Log("absURI = " + uri.AbsoluteUri); //Debug.Log("path = " + uri.PathAndQuery); //Debug.Log("host = " + uri.Host); // build up the node's path segments graphNode.m_pathSegments.Add(uri.Host); foreach (string element in uri.Segments) { //if (element == "/") continue; graphNode.m_pathSegments.Add(element);//.TrimEnd('/')); } if (uri.Fragment != null) { graphNode.m_pathSegments[graphNode.m_pathSegments.Count - 1] += (uri.Fragment); } OwlTreeNode owlTreeNode = m_owlNodeTree.addNode(graphNode); graphNode.m_treeNode = owlTreeNode; //statsElem.mNode = graphNode; m_NodeDictionary.Add(owlKey, graphNode); if (cnt % m_addElementInterval == 0) { string[] dots = { ".", "..", "..." }; for (int jj = 0; jj < 3; jj++) { m_winText.text = oldText + "Opening OWL file" + dots[cnt % 3]; } //Debug.Log("cnt = " + cnt); if (m_testing) { yield break; } else { yield return(null);// new WaitForSeconds(0.052f); } } } m_winText.text = oldText; yield return(null); }