Exemplo n.º 1
0
        public static RdfWriter Create(string type, TextWriter output, RdfXmlWriter.Options options)
        {
            switch (RdfReader.NormalizeMimeType(type))
            {
            case "xml":
                                        #if !SILVERLIGHT
                return(new RdfXmlWriter(output, options));
                                        #else
                throw new NotSupportedException("RDF/XML output is not supported by the Silverlight build of the SemWeb library.");
                                        #endif
            case "n3":
                                        #if DOTNET2
                return(new N3Writer(output));
                                        #endif
            case "turtle":
                return(new TurtleWriter(output));

            case "nt":
            case "ntriples":
                return(new NTriplesWriter(output));

            case "dot":
                return(new GraphVizWriter(output));

            default:
                throw new ArgumentException("Unknown parser or MIME type: " + type);
            }
        }
        private void ButtonGroupAdd_Click(object sender, EventArgs e)
        {
            if (ListViewCandidates.SelectedItems.Count < 2)
            {
                MessageBox.Show("To use the Group add functionality you have to select at least 2 candidates from the list");
                return;
            }

            Store tempStore = new MemoryStore();
            foreach (ListViewItem item in ListViewCandidates.SelectedItems)
            {
                string label = (item.Tag as Tuple<string, double>).Item1;
                string uri = GetSuggestedConceptUri(label);
                if (_ao.ConceptUriToDescription.ContainsKey(uri))
                {
                    MessageBox.Show("The concept with uri " + uri + " already exists. Ignoring it.");
                    continue;
                }
                if (_ao.ConceptLabelToUri.ContainsKey(label.ToLower()))
                {
                    MessageBox.Show("The label " + label + " is already assigned to concept " + _ao.ConceptLabelToUri[label.ToLower()] + ". Skipping the label.");
                    continue;
                }
                tempStore.Add(new Statement(new Entity(uri), AnnotationOntology.RelRdfsLabel, (Literal)label));
            }

            // remove the added items
            List<ListViewItem> selectedItems = new List<ListViewItem>(from ListViewItem item in ListViewCandidates.SelectedItems select item);
            foreach (var item in selectedItems)
                ListViewCandidates.Items.Remove(item);

            // send the statements as an event
            System.IO.StringWriter strWriter = new System.IO.StringWriter();
            RdfXmlWriter.Options options = new RdfXmlWriter.Options();
            options.EmbedNamedNodes = false;
            using (RdfWriter writer = new RdfXmlWriter(strWriter, options))
                writer.Write(tempStore);
            string newRDFData = strWriter.ToString();
            SendRDFData(newRDFData);
            _ao.AddNewRDFData(newRDFData, true);
        }
        private void ButtonAddSelectedConcept_Click(object sender, EventArgs e)
        {
            string candidate = TextBoxLabels.Text;
            Debug.Assert(!string.IsNullOrEmpty(candidate));

            if (string.IsNullOrEmpty(TextBoxConceptUri.Text))
            {
                MessageBox.Show("You have to specify a URI of the concept.");
                return;
            }

            if (_ao.ConceptUriToDescription.ContainsKey(TextBoxConceptUri.Text) && _editingExistingConcept == false)
            {
                MessageBox.Show("The concept with uri " + TextBoxConceptUri.Text + " already exists. Please specify a unique URI.");
                return;
            }

            // compute what are all the labels for the concept
            List<string> labels = candidate.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            labels = (from label in labels select label.Trim()).ToList();

            if (labels.Count == 0)
            {
                MessageBox.Show("You have to specify at least one label for the concept.");
                return;
            }

            string uri = TextBoxConceptUri.Text;
            Entity concept = new Entity(uri);

            Store tempStore = new MemoryStore();
            foreach (string label in labels)
            {
                if (_ao.ConceptLabelToUri.ContainsKey(label.ToLower()) && _editingExistingConcept == false)
                {
                    MessageBox.Show("The label " + label + " is already assigned to concept " + _ao.ConceptLabelToUri[label.ToLower()] + ". Skipping the label.");
                    continue;
                }
                tempStore.Add(new Statement(concept, AnnotationOntology.RelRdfsLabel, (Literal)label));
            }

            if (!string.IsNullOrEmpty(TextBoxDescription.Text))
                tempStore.Add(new Statement(concept, AnnotationOntology.RelRdfsComment, (Literal)TextBoxDescription.Text));

            foreach (ListViewItem item in ListViewRelated.Items)
            {
                string conceptUri = (string)item.Tag;
                string relationStr = item.SubItems[2].Text;
                tempStore.Add(new Statement(concept, (Entity)_relationToUri[relationStr], new Entity(conceptUri)));
            }

            // send the statements as an event
            System.IO.StringWriter strWriter = new System.IO.StringWriter();
            RdfXmlWriter.Options options = new RdfXmlWriter.Options();
            options.EmbedNamedNodes = false;
            using (RdfWriter writer = new RdfXmlWriter(strWriter, options))
                writer.Write(tempStore);
            string newRDFData = strWriter.ToString();

            SendRDFData(newRDFData);
            _ao.AddNewRDFData(newRDFData, true);

            // remove the added items
            List<ListViewItem> selectedItems = new List<ListViewItem>(from ListViewItem item in ListViewCandidates.SelectedItems select item);
            foreach (var item in selectedItems)
                ListViewCandidates.Items.Remove(item);
            TextBoxLabels.Text = "";
            TextBoxConceptUri.Text = "";
            TextBoxDescription.Text = "";
            ListViewRelated.Items.Clear();
        }
        /// <summary>
        /// new ontology concepts can be added while KEUI is running. we have to store the new data
        /// </summary>
        public void SaveAnnotationOntology(string fileName)
        {
            try
            {
                string RDFS = "http://www.w3.org/2000/01/rdf-schema#";
                string RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
                Entity rdfValue = RDF + "value";
                Entity rdfsComment = RDFS + "comment";

                // remove existing value properties
                _conceptsStore.Remove(new Statement(null, rdfValue, null));

                // create new value properties with updated values
                foreach (Statement s in _conceptsStore.Select(new Statement(null, rdfsComment, null)))
                {
                    int value = GetConceptFrequencyCount(s.Subject.Uri);
                    if (value > 0)
                        _conceptsStore.Add(new Statement(s.Subject.Uri, rdfValue, (Literal)value.ToString()));
                }

                string tempName = fileName + ".temp";
                RdfXmlWriter.Options options = new RdfXmlWriter.Options();
                options.EmbedNamedNodes = false;
                using (RdfWriter writer = new RdfXmlWriter(tempName, options))
                {
                    writer.Namespaces.AddNamespace("http://example.org/", "ex");
                    writer.Namespaces.AddNamespace("http://www.w3.org/2002/27/owl#", "owl");
                    writer.Namespaces.AddNamespace("http://dbpedia.org/ontology/", "DBpedia");
                    writer.Namespaces.AddNamespace("http://ailab.ijs.si/alert/predicate/", "ailab");
                    writer.Namespaces.AddNamespace("http://purl.org/dc/terms/", "purl");
                    writer.Namespaces.AddNamespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdf");
                    writer.Namespaces.AddNamespace("http://www.w3.org/2000/01/rdf-schema#", "rdfs");

                    writer.Write(_conceptsStore);
                }

                // if we successfully saved the ontology to the temp name only then rename the temp file to the correct name
                if (System.IO.File.Exists(fileName))
                    System.IO.File.Delete(fileName);
                System.IO.File.Move(tempName, fileName);
            }
            catch (Exception ex)
            {
                AddEvent("Exception while saving the annotation ontology. Error: " + ex.Message);
                GenLib.Log.LogService.LogException("Exception while saving the annotation ontology", ex);
            }
        }
        public string GetAnnotationOntologyRDF(bool includeComment, bool includeLinksTo)
        {
            try
            {
                Store smallStore = new MemoryStore();
                foreach (Statement s in _conceptsStore.Select(new Statement(null, null, null)))
                {
                    if (s.Predicate == RelRdfsComment && includeComment == false)
                        continue;
                    if (s.Predicate == RelDbAbstract && includeComment == false)
                        continue;
                    if (s.Predicate == RelLinksTo && includeLinksTo == false)
                        continue;
                    smallStore.Add(s);
                }

                System.IO.StringWriter strWriter = new System.IO.StringWriter();
                RdfXmlWriter.Options options = new RdfXmlWriter.Options();
                options.EmbedNamedNodes = false;
                using (RdfWriter writer = new RdfXmlWriter(strWriter, options))
                {
                    writer.Namespaces.AddNamespace("http://example.org/", "ex");
                    writer.Namespaces.AddNamespace("http://www.w3.org/2002/27/owl#", "owl");
                    writer.Namespaces.AddNamespace("http://dbpedia.org/ontology/", "DBpedia");
                    writer.Namespaces.AddNamespace("http://ailab.ijs.si/alert/predicate/", "ailab");
                    writer.Namespaces.AddNamespace("http://purl.org/dc/terms/", "purl");
                    writer.Namespaces.AddNamespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdf");
                    writer.Namespaces.AddNamespace("http://www.w3.org/2000/01/rdf-schema#", "rdfs");

                    writer.Write(smallStore);
                }
                return strWriter.ToString();
            }
            catch (Exception ex)
            {
                AddEvent("Exception while sending the annotation ontology as string. Error: " + ex.Message);
                GenLib.Log.LogService.LogException("Exception while sending the annotation ontology as string", ex);
                return "";
            }
        }
Exemplo n.º 6
0
 public void Describe(SelectableSource source, TextWriter output)
 {
     RdfXmlWriter.Options opts = new RdfXmlWriter.Options();
     opts.KeepRoots = true;
     using (RdfWriter w = RdfWriter.Create(MimeType, output, opts))
         Describe(source, w);
 }