Exemplo n.º 1
0
        public void TestFormulaWithList()
        {
            N3Writer instance = (N3Writer)CreateInstance();

            BNode listId      = new BNode();
            BNode listTwoId   = new BNode();
            BNode listThreeId = new BNode();

            instance.Add(new Statement(listId, Predicate.RdfFirst, (Entity)"A"));
            instance.Add(new Statement(listId, Predicate.RdfRest, listTwoId));
            instance.Add(new Statement(listTwoId, Predicate.RdfFirst, (Entity)"B"));
            instance.Add(new Statement(listTwoId, Predicate.RdfRest, listThreeId));
            instance.Add(new Statement(listThreeId, Predicate.RdfFirst, (Entity)"C"));
            instance.Add(new Statement(listThreeId, Predicate.RdfRest, Identifier.RdfNil));

            BNode statementXId = new BNode();
            BNode statementYId = new BNode();

            instance.Add(new Statement(listId, "p", (Entity)"X", statementXId));
            instance.Add(new Statement(listId, "p", (Entity)"Y", statementYId));
            instance.Add(new Statement(statementXId, Predicate.LogImplies, statementYId));
            instance.Close();

            string expected = "{(<A> <B> <C>) <p> <X>.} => {(<A> <B> <C>) <p> <Y>.}.";

            Assert.AreEqual(expected, writer.ToString());
        }
Exemplo n.º 2
0
        public void TestListWithFormulaElements()
        {
            N3Writer instance = (N3Writer)CreateInstance();

            BNode statementXId = new BNode();
            BNode statementYId = new BNode();
            BNode statementZId = new BNode();

            instance.Add(new Statement("S", "p", (Entity)"X", statementXId));
            instance.Add(new Statement("S", "p", (Entity)"Y", statementYId));
            instance.Add(new Statement("S", "p", (Entity)"Z", statementZId));

            BNode listId      = new BNode();
            BNode listTwoId   = new BNode();
            BNode listThreeId = new BNode();

            instance.Add(new Statement(listId, Predicate.RdfType, (Entity)"List"));
            instance.Add(new Statement(listId, Predicate.RdfFirst, statementXId));
            instance.Add(new Statement(listId, Predicate.RdfRest, listTwoId));
            instance.Add(new Statement(listTwoId, Predicate.RdfFirst, statementYId));
            instance.Add(new Statement(listTwoId, Predicate.RdfRest, listThreeId));
            instance.Add(new Statement(listThreeId, Predicate.RdfFirst, statementZId));
            instance.Add(new Statement(listThreeId, Predicate.RdfRest, Identifier.RdfNil));
            instance.Close();

            string expected = "({<S> <p> <X>.}\n {<S> <p> <Y>.}\n {<S> <p> <Z>.}) a <List>.";

            Assert.AreEqual(expected, writer.ToString());
        }
Exemplo n.º 3
0
 protected void ExportToN3(DalcRdfStore store)
 {
     using (var n3wr = new N3Writer(Console.Out)) {
         n3wr.Namespaces.AddNamespace("http://www.nreco.qsh.eu/rdf/", "nreco");
         n3wr.Namespaces.AddNamespace("http://www.nreco.qsh.eu/rdf/persons#", "p");
         n3wr.Namespaces.AddNamespace(NS.Rdf.BASE, "rdf");
         n3wr.Namespaces.AddNamespace(NS.Rdfs.BASE, "rdfs");
         store.Select(n3wr);
     }
 }
Exemplo n.º 4
0
        public void TestNamespaces()
        {
            N3Writer instance = (N3Writer)CreateInstance();

            instance.Add(new Statement("http://example.org/A", Predicate.RdfType, (Entity)"X"));
            instance.Namespaces.AddNamespace("http://example.org/", "ex");
            instance.Close();

            string expected = "@prefix ex: <http://example.org/>.\nex:A a <X>.";

            Assert.AreEqual(expected, writer.ToString());
        }
Exemplo n.º 5
0
    public static void Main()
    {
        MemoryStore store = new MemoryStore();

        store.Import(new RdfXmlReader(Console.In));

        // The 'using' is important because it is necessary
        // to Close or Dispose the writer once writing is
        // complete so that the final statement is closed
        // with a period.
        using (RdfWriter writer = new N3Writer(Console.Out))
            writer.Write(store);
    }
Exemplo n.º 6
0
        public void TestListWithOneElement()
        {
            N3Writer instance = (N3Writer)CreateInstance();

            BNode listId = new BNode();

            instance.Add(new Statement(listId, Predicate.RdfType, (Entity)"List"));
            instance.Add(new Statement(listId, Predicate.RdfFirst, (Entity)"A"));
            instance.Add(new Statement(listId, Predicate.RdfRest, Identifier.RdfNil));
            instance.Close();

            string expected = "(<A>) a <List>.";

            Assert.AreEqual(expected, writer.ToString());
        }
Exemplo n.º 7
0
        public void TestFormulaMultipleOccurences()
        {
            N3Writer instance = (N3Writer)CreateInstance();

            BNode statementId = new BNode();

            instance.Add(new Statement("S", "p", (Entity)"O", statementId));
            instance.Add(new Statement("MyTheory", "contains", statementId));
            instance.Add(new Statement(statementId, "is", (Entity)"True"));
            instance.Close();

            string expected = "<MyTheory> <contains> {<S> <p> <O>.}.\n{<S> <p> <O>.} <is> <True>.";

            Assert.AreEqual(expected, writer.ToString());
        }
Exemplo n.º 8
0
        public void TestFormulaSingleOccurence()
        {
            N3Writer instance = (N3Writer)CreateInstance();

            BNode statementXId = new BNode();
            BNode statementYId = new BNode();

            instance.Add(new Statement("S", "p", (Entity)"X", statementXId));
            instance.Add(new Statement("S", "p", (Entity)"Y", statementYId));
            instance.Add(new Statement(statementXId, Predicate.LogImplies, statementYId));
            instance.Close();

            string expected = "{<S> <p> <X>.} => {<S> <p> <Y>.}.";

            Assert.AreEqual(expected, writer.ToString());
        }
Exemplo n.º 9
0
        public void TestFormulaMixedWithRegularStatements()
        {
            N3Writer instance = (N3Writer)CreateInstance();

            BNode statementXId = new BNode();
            BNode statementYId = new BNode();

            instance.Add(new Statement("S", "b", (Entity)"B"));
            instance.Add(new Statement("S", "p", (Entity)"X", statementXId));
            instance.Add(new Statement("S", "p", (Entity)"Y", statementYId));
            instance.Add(new Statement(statementXId, Predicate.LogImplies, statementYId));
            instance.Close();

            string expected = "<S> <b> <B>.\n{<S> <p> <X>.} => {<S> <p> <Y>.}.";

            Assert.AreEqual(expected, writer.ToString());
        }
Exemplo n.º 10
0
        public void TestFormulaWithMultipleStatementsSingleOccurence()
        {
            N3Writer instance = (N3Writer)CreateInstance();

            BNode formulaId    = new BNode();
            BNode statementZId = new BNode();

            instance.Add(new Statement("S", "p", (Entity)"X", formulaId));
            instance.Add(new Statement("S", "p", (Entity)"Y", formulaId));
            instance.Add(new Statement("S", "p", (Entity)"Z", statementZId));
            instance.Add(new Statement(formulaId, Predicate.LogImplies, statementZId));
            instance.Close();

            string expected = "{\n <S> <p> <X>.\n <S> <p> <Y>.\n} => {<S> <p> <Z>.}.";

            Assert.AreEqual(expected, writer.ToString());
        }
Exemplo n.º 11
0
        public void TestFormulaWithinFormula()
        {
            N3Writer instance = (N3Writer)CreateInstance();

            BNode statementXId    = new BNode();
            BNode statementYId    = new BNode();
            BNode statementImplId = new BNode();

            instance.Add(new Statement("S", "p", (Entity)"X", statementXId));
            instance.Add(new Statement("S", "p", (Entity)"Y", statementYId));
            instance.Add(new Statement(statementXId, Predicate.LogImplies, statementYId, statementImplId));
            instance.Add(new Statement(statementImplId, Predicate.LogImplies, (Entity)"Truth"));
            instance.Close();

            string expected = "{\n{<S> <p> <X>.}\n=>\n{<S> <p> <Y>.}.\n} => <Truth>.";

            Assert.AreEqual(expected, writer.ToString());
        }
Exemplo n.º 12
0
        public void TestListWithThreeElementsWithLastUnbound()
        {
            N3Writer instance = (N3Writer)CreateInstance();

            BNode listId      = new BNode();
            BNode listTwoId   = new BNode();
            BNode listThreeId = new BNode();

            instance.Add(new Statement(listId, Predicate.RdfType, (Entity)"List"));
            instance.Add(new Statement(listId, Predicate.RdfFirst, (Entity)"A"));
            instance.Add(new Statement(listId, Predicate.RdfRest, listTwoId));
            instance.Add(new Statement(listTwoId, Predicate.RdfFirst, (Entity)"B"));
            instance.Add(new Statement(listTwoId, Predicate.RdfRest, listThreeId));
            instance.Add(new Statement(listThreeId, Predicate.RdfFirst, (Entity)"C"));
            instance.Close();

            string expected = @"\(<A> <B> <C> _:bnode\d+\) a <List>.";

            Assert.That(writer.ToString(), Text.Matches(expected));
        }
Exemplo n.º 13
0
        public override string ToString()
        {
            System.Text.StringBuilder ret = new System.Text.StringBuilder();
            ret.Append('"');
            ret.Append(N3Writer.Escape(Value));
            ret.Append('"');

            if (Language != null)
            {
                ret.Append('@');
                ret.Append(N3Writer.Escape(Language));
            }

            if (DataType != null)
            {
                ret.Append("^^<");
                ret.Append(N3Writer.Escape(DataType));
                ret.Append(">");
            }
            return(ret.ToString());
        }
Exemplo n.º 14
0
    public static void Main()
    {
        MemoryStore store = new MemoryStore();

        Entity container = new Entity("http://www.example.org/#container");

        store.Add(new Statement(container, RDF+"type", (Entity)(RDF+"Bag")));
        store.Add(new Statement(container, RDF+"_3", (Literal)"Three"));
        store.Add(new Statement(container, RDF+"_2", (Literal)"Two"));
        store.Add(new Statement(container, RDF+"_1", (Literal)"One"));

        // use the rdfs:member property to match for any rdf:_### predicates.
        Entity rdfs_member = (Entity)(RDFS+"member");

        using (RdfWriter writer = new N3Writer(Console.Out)) {
            writer.Namespaces.AddNamespace(RDF, "rdf");
            store.Select(new Statement(container, rdfs_member, null), writer);
        }

        foreach (Resource r in store.SelectObjects(container, rdfs_member))
            Console.WriteLine(r);
    }
Exemplo n.º 15
0
		public void Describe(SelectableSource source, TextWriter output) {
			using (RdfWriter w = new N3Writer(output))
				Describe(source, w);
		}
Exemplo n.º 16
0
        public void InteropSemWebInMemoryStoreSource()
        {
            MicrosoftSqlStoreManager mssql = new MicrosoftSqlStoreManager("dotnetrdf_experimental", "sa", "20sQl08");
            InMemoryStoreSource source = new InMemoryStoreSource(new SqlTripleStore(mssql));

            N3Writer writer = new N3Writer(Console.Out);

            Console.WriteLine("Outputting all Triples of the form ?s rdf:type ?type");
            Statement template = new Statement(new Variable(), new Entity(RdfSpecsHelper.RdfType), new Variable());
            source.Select(template, writer);
            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("Outputting all Triples of the form ?s rdf:type ?car");
            template = new Statement(new Variable(), new Entity(RdfSpecsHelper.RdfType), new Entity("http://example.org/vehicles/Car"));
            source.Select(template, writer);
            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("Outputting all Triples for Cars and Planes");
            SelectFilter filter = new SelectFilter();
            filter.Predicates = new Entity[] { new Entity(RdfSpecsHelper.RdfType) };
            filter.Objects = new Entity[] { new Entity("http://example.org/vehicles/Car"), new Entity("http://example.org/vehicles/Plane") };
            source.Select(filter, writer);
            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("Outputting the Speeds of all Cards");
            Variable car = new Variable("car");
            Statement[] pattern = new Statement[] 
            {
                new Statement(car, new Entity(RdfSpecsHelper.RdfType), new Entity("http://example.org/vehicles/Car")),
                new Statement(car, new Entity("http://example.org/vehicles/Speed"), new Variable("speed"))
            };
            source.Query(pattern, new QueryOptions(), new SemWebResultsConsolePrinter());
            Console.WriteLine();

            writer.Close();
        }
Exemplo n.º 17
0
        public void InteropSemWebGraphSource()
        {
            Graph g = new Graph();
            //FileLoader.Load(g, "InferenceTest.ttl");
            GraphSource source = new GraphSource(g);

            Console.WriteLine("Reading the input using SemWeb");
            N3Reader reader = new N3Reader("InferenceTest.ttl");
            reader.Select(source);
            Console.WriteLine();

            Console.WriteLine("Outputting all Triples using N3Writer");
            N3Writer writer = new N3Writer(Console.Out);
            source.Select(writer);
            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("Outputting all Triples of the form ?s rdf:type ?type");
            Statement template = new Statement(new Variable(), new Entity(RdfSpecsHelper.RdfType), new Variable());
            source.Select(template, writer);
            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("Outputting all Triples of the form ?s rdf:type ?car");
            template = new Statement(new Variable(), new Entity(RdfSpecsHelper.RdfType), new Entity("http://example.org/vehicles/Car"));
            source.Select(template, writer);
            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("Outputting all Triples for Cars and Planes");
            SelectFilter filter = new SelectFilter();
            filter.Predicates = new Entity[] { new Entity(RdfSpecsHelper.RdfType) };
            filter.Objects = new Entity[] { new Entity("http://example.org/vehicles/Car"), new Entity("http://example.org/vehicles/Plane") };
            source.Select(filter, writer);
            Console.WriteLine();
            Console.WriteLine();

            writer.Close();
        }
Exemplo n.º 18
0
    public Hashtable Validate()
    {
        string content = HttpContext.Current.Request.Form["content"];
        string format = HttpContext.Current.Request.Form["format"];
        if (content == null || content.Trim() == "" || format == null) {
            HttpContext.Current.Response.Redirect("index.xpd");
            throw new InvalidOperationException();
        }

        StringWriter output = new StringWriter();

        RdfReader reader;
        RdfWriter writer;

        Hashtable response = new Hashtable();

        response["InDocument"] = AddLineNumbers(content);

        if (format == "xml") {
            reader = new RdfXmlReader(new StringReader(content));
            writer = new N3Writer(output);
            response["InFormat"] = "RDF/XML";
            response["OutFormat"] = "Notation 3";
        } else if (format == "n3") {
            reader = new N3Reader(new StringReader(content));
            writer = new RdfXmlWriter(output);
            response["OutFormat"] = "RDF/XML";
            response["InFormat"] = "Notation 3";
        } else {
            throw new Exception("Invalid format.");
        }

        response["Validation"] = "Syntax validated OK.";

        response["OutDocument"] = "";
        response["Triples"] = "";

        MemoryStore data = new MemoryStore();
        try {
            data.Import(reader);
        } catch (Exception e) {
            response["Validation"] = "Validation failed: " + e.Message + ".";
            return response;
        } finally {
            if (reader.Warnings.Count > 0) {
                response["Validation"] += "  There were warnings: ";
                foreach (string warning in reader.Warnings)
                    response["Validation"] += " " + warning + ".";
            }
        }

        writer.Namespaces.AddFrom(reader.Namespaces);

        try {
            writer.Write(data);
            writer.Close();
            response["OutDocument"] = output.ToString();
        } catch (Exception e) {
            response["OutDocument"] = e.Message;
        }

        StringWriter triplesoutput = new StringWriter();
        using (NTriplesWriter tripleswriter = new NTriplesWriter(triplesoutput)) {
            tripleswriter.Write(data);
        }
        response["Triples"] = triplesoutput.ToString();

        return response;
    }
Exemplo n.º 19
0
		
		public void Write(System.IO.TextWriter writer) {
			using (RdfWriter w = new N3Writer(writer)) {
				Select(w);
			}
Exemplo n.º 20
0
		private static object Create(string spec, bool output) {
			string[] multispecs = spec.Split('\n', '|');
			if (multispecs.Length > 1) {
				SemWeb.Stores.MultiStore multistore = new SemWeb.Stores.MultiStore();
				foreach (string mspec in multispecs) {
					object mstore = Create(mspec.Trim(), output);
					if (mstore is SelectableSource) {
						multistore.Add((SelectableSource)mstore);
					} else if (mstore is StatementSource) {
						MemoryStore m = new MemoryStore((StatementSource)mstore);
						multistore.Add(m);
					}
				}
				return multistore;
			}
		
			string type = spec;
			
			int c = spec.IndexOf(':');
			if (c != -1) {
				type = spec.Substring(0, c);
				spec = spec.Substring(c+1);
			} else {
				spec = "";
			}
			
			Type ttype;
			
			switch (type) {
				case "mem":
					return new MemoryStore();
				case "xml":
					if (spec == "") throw new ArgumentException("Use: xml:filename");
					if (output) {
						return new RdfXmlWriter(spec);
					} else {
						return new RdfXmlReader(spec);
					}
				case "n3":
				case "ntriples":
				case "nt":
				case "turtle":
					if (spec == "") throw new ArgumentException("Use: format:filename");
					if (output) {
						N3Writer ret = new N3Writer(spec); // turtle is default format
						switch (type) {
							case "nt": case "ntriples":
								ret.Format = N3Writer.Formats.NTriples;
								break;
						}
						return ret;
					} else {
						return new N3Reader(spec);
					}
				/*case "file":
					if (spec == "") throw new ArgumentException("Use: format:filename");
					if (output) throw new ArgumentException("The FileStore does not support writing.");
					return new SemWeb.Stores.FileStore(spec);*/
				case "sqlite":
				case "mysql":
				case "postgresql":
					if (spec == "") throw new ArgumentException("Use: sqlite|mysql|postgresql:table:connection-string");
				
					c = spec.IndexOf(':');
					if (c == -1) throw new ArgumentException("Invalid format for SQL spec parameter (table:constring).");
					string table = spec.Substring(0, c);
					spec = spec.Substring(c+1);
					
					string classtype = null;
					if (type == "sqlite") {
						classtype = "SemWeb.Stores.SqliteStore, SemWeb.SqliteStore";
						spec = spec.Replace(";", ",");
					} else if (type == "mysql") {
						classtype = "SemWeb.Stores.MySQLStore, SemWeb.MySQLStore";
					} else if (type == "postgresql") {
						classtype = "SemWeb.Stores.PostgreSQLStore, SemWeb.PostgreSQLStore";
					}
					ttype = Type.GetType(classtype);
					if (ttype == null)
						throw new NotSupportedException("The storage type in <" + classtype + "> could not be found.");
					return Activator.CreateInstance(ttype, new object[] { spec, table });
				/*case "bdb":
					return new SemWeb.Stores.BDBStore(spec);*/
				case "sparql-http":
					return new SemWeb.Remote.SparqlHttpSource(spec);
				case "class":
					ttype = Type.GetType(spec);
					if (ttype == null)
						throw new NotSupportedException("The class <" + spec + "> could not be found.");
					return Activator.CreateInstance(ttype);
				default:
					throw new ArgumentException("Unknown parser type: " + type);
			}
		}
Exemplo n.º 21
0
		public void Construct(SelectableSource source, TextWriter output) {
			using (RdfWriter w = new N3Writer(output))
				Construct(source, w);
		}
Exemplo n.º 22
0
        private static object Create(string spec, bool output)
        {
            string[] multispecs = spec.Split('\n', '|');
            if (multispecs.Length > 1)
            {
                SemWeb.Stores.MultiStore multistore = new SemWeb.Stores.MultiStore();
                foreach (string mspec in multispecs)
                {
                    object mstore = Create(mspec.Trim(), output);
                    if (mstore is SelectableSource)
                    {
                        multistore.Add((SelectableSource)mstore);
                    }
                    else if (mstore is StatementSource)
                    {
                        MemoryStore m = new MemoryStore((StatementSource)mstore);
                        multistore.Add(m);
                    }
                }
                return(multistore);
            }

            string type = spec;

            int c = spec.IndexOf(':');

            if (c != -1)
            {
                type = spec.Substring(0, c);
                spec = spec.Substring(c + 1);
            }
            else
            {
                spec = "";
            }

            Type ttype;

            switch (type)
            {
            case "mem":
                return(new MemoryStore());

            case "xml":
                if (spec == "")
                {
                    throw new ArgumentException("Use: xml:filename");
                }
                if (output)
                {
                    return(new RdfXmlWriter(spec));
                }
                else
                {
                    return(new RdfXmlReader(spec));
                }

            case "n3":
            case "ntriples":
            case "nt":
            case "turtle":
                if (spec == "")
                {
                    throw new ArgumentException("Use: format:filename");
                }
                if (output)
                {
                    N3Writer ret = new N3Writer(spec);                             // turtle is default format
                    switch (type)
                    {
                    case "nt":
                    case "ntriples":
                        ret.Format = N3Writer.Formats.NTriples;
                        break;
                    }
                    return(ret);
                }
                else
                {
                    return(new N3Reader(spec));
                }

            /*case "file":
             *      if (spec == "") throw new ArgumentException("Use: format:filename");
             *      if (output) throw new ArgumentException("The FileStore does not support writing.");
             *      return new SemWeb.Stores.FileStore(spec);*/
            case "sqlite":
            case "mysql":
            case "postgresql":
                if (spec == "")
                {
                    throw new ArgumentException("Use: sqlite|mysql|postgresql:table:connection-string");
                }

                c = spec.IndexOf(':');
                if (c == -1)
                {
                    throw new ArgumentException("Invalid format for SQL spec parameter (table:constring).");
                }
                string table = spec.Substring(0, c);
                spec = spec.Substring(c + 1);

                string classtype = null;
                if (type == "sqlite")
                {
                    classtype = "SemWeb.Stores.SqliteStore, SemWeb.SqliteStore";
                    spec      = spec.Replace(";", ",");
                }
                else if (type == "mysql")
                {
                    classtype = "SemWeb.Stores.MySQLStore, SemWeb.MySQLStore";
                }
                else if (type == "postgresql")
                {
                    classtype = "SemWeb.Stores.PostgreSQLStore, SemWeb.PostgreSQLStore";
                }
                ttype = Type.GetType(classtype);
                if (ttype == null)
                {
                    throw new NotSupportedException("The storage type in <" + classtype + "> could not be found.");
                }
                return(Activator.CreateInstance(ttype, new object[] { spec, table }));

            /*case "bdb":
             *      return new SemWeb.Stores.BDBStore(spec);*/
            case "sparql-http":
                return(new SemWeb.Remote.SparqlHttpSource(spec));

            case "class":
                ttype = Type.GetType(spec);
                if (ttype == null)
                {
                    throw new NotSupportedException("The class <" + spec + "> could not be found.");
                }
                return(Activator.CreateInstance(ttype));

            default:
                throw new ArgumentException("Unknown parser type: " + type);
            }
        }
Exemplo n.º 23
0
	public static void query (SelectableSource source, Statement filter) {
		using (RdfWriter writer = new N3Writer (System.Console.Out))
			source.Select (filter, writer);
	}
Exemplo n.º 24
0
 public void Write(System.IO.TextWriter writer)
 {
     using (RdfWriter w = new N3Writer(writer)) {
         Select(w);
     }
 }