/// <summary> /// Creates a new Union Triple Collection which is a union of any number of collections /// </summary> /// <param name="baseTriples">Base Triple Collection</param> /// <param name="additionalTriples">Additional Triple Collection(s)</param> public UnionTripleCollection(BaseTripleCollection baseTriples, IEnumerable<BaseTripleCollection> additionalTriples) { if (baseTriples == null) throw new ArgumentNullException("baseTriple"); this._collections.Add(baseTriples); this._collections.AddRange(additionalTriples); this._baseCollection = baseTriples; }
private static void CompareTripleCollections(BaseTripleCollection actualTriples, BaseTripleCollection expectedTriples, bool reduced) { var actualTripleList = new List <Triple>(actualTriples); var expectedTripleList = new List <Triple>(expectedTriples); var alreadySeen = new HashSet <Triple>(); var bnodeMap = new Dictionary <string, string>(); while (actualTripleList.Count > 0) { var at = actualTripleList[0]; actualTripleList.Remove(at); if (alreadySeen.Contains(at)) { continue; } var match = expectedTripleList.Find(x => TripleMatch(x, at, bnodeMap)); if (match == null) { Assert.Fail("No match found for actual triple {0} in expected triples set", at); } expectedTripleList.Remove(match); alreadySeen.Add(at); } Assert.IsTrue(actualTripleList.Count == 0); Assert.IsTrue(expectedTripleList.Count == 0, "Left with some unmatched triples in the expected triples set: {0}", String.Join(",", expectedTripleList.Select(t => t.ToString()))); }
/// <summary> /// Creates a new Union Triple Collection which is a union of two collections /// </summary> /// <param name="baseTriples">Base Triple Collection</param> /// <param name="additionalTriples">Additional Triple Collection</param> public UnionTripleCollection(BaseTripleCollection baseTriples, BaseTripleCollection additionalTriples) { if (baseTriples == null) throw new ArgumentNullException("baseTriple"); if (additionalTriples == null) throw new ArgumentNullException("additionalTriples"); this._collections.Add(baseTriples); this._collections.Add(additionalTriples); this._baseCollection = baseTriples; }
private static void CompareTripleCollections(BaseTripleCollection actualTriples, BaseTripleCollection expectedTriples, bool reduced) { var actualGraph = new Graph(actualTriples); var expectedGraph = new Graph(expectedTriples); Assert.IsTrue(expectedGraph.Equals(actualGraph), "Result graphs do not match.r\nExpected:\r\n{0}\r\nActual:\r\n{1}", String.Join(",", expectedTriples.Select(t => t.ToString())), String.Join(",", actualTriples.Select(t => t.ToString()))); }
public void AssertTriplesRetracted(BaseTripleCollection tripleCollection, Uri graphIri) { foreach (var t in tripleCollection) { Retracted.Should().Contain(x => x.Item1.Equals(t.Subject) && x.Item2.Equals(t.Predicate) && x.Item3.Equals(t.Object) && x.Item4.Equals(graphIri), "Expected a quad ({0}, {1}, {2}, {3}) to have been retracted but no matching quad was found.", t.Subject, t.Predicate, t.Object, graphIri); } }
public void ConfigurationLoadObjectTripleCollection1() { String graph = ConfigLookupTests.Prefixes + @" _:a a dnr:TripleCollection ; dnr:type ""VDS.RDF.TreeIndexedTripleCollection"" ."; Graph g = new Graph(); g.LoadFromString(graph); BaseTripleCollection collection = ConfigurationLoader.LoadObject(g, g.GetBlankNode("a")) as BaseTripleCollection; Assert.IsNotNull(collection); Assert.AreEqual(typeof(TreeIndexedTripleCollection), collection.GetType()); }
private static void CompareTripleCollections(BaseTripleCollection actualTriples, BaseTripleCollection expectedTriples, bool reduced) { var alreadySeen = new HashSet <Triple>(); foreach (var expectedTriple in expectedTriples) { if (reduced && alreadySeen.Contains(expectedTriple)) { continue; } Assert.IsTrue(actualTriples.Contains(expectedTriple), "Could not find expected triple '{0}' in results set.", expectedTriple); } foreach (var actualTriple in actualTriples) { Assert.IsTrue(expectedTriples.Contains(actualTriple), "Unexpected result set triple '{0}'", actualTriple); } }
/// <summary> /// Creates a new instance of a Graph using the given Triple Collection /// </summary> /// <param name="tripleCollection">Triple Collection</param> public Graph(BaseTripleCollection tripleCollection) : base(tripleCollection) { }
public static Dictionary <INode, OutputRdfCollection> FindCollections(IGraph g, BaseTripleCollection triplesDone) { throw new NotSupportedException("No longer supported"); }
/// <summary> /// Helper method for detaching the necessary event Handlers from a Triple Collection /// </summary> /// <param name="tripleCollection">Triple Collection</param> /// <remarks> /// May be useful if you replace the Triple Collection after instantiation e.g. as done in <see cref="Query.SparqlView">SparqlView</see>'s /// </remarks> protected void DetachEventHandlers(BaseTripleCollection tripleCollection) { tripleCollection.TripleAdded -= this.TripleAddedHandler; tripleCollection.TripleRemoved -= this.TripleRemovedHandler; }
private static void CompareTripleCollections(BaseTripleCollection actualTriples, BaseTripleCollection expectedTriples, bool reduced) { var alreadySeen = new HashSet<Triple>(); foreach (var expectedTriple in expectedTriples) { if (reduced && alreadySeen.Contains(expectedTriple)) continue; Assert.IsTrue(actualTriples.Contains(expectedTriple), "Could not find expected triple '{0}' in results set.", expectedTriple); } foreach(var actualTriple in actualTriples) { Assert.IsTrue(expectedTriples.Contains(actualTriple), "Unexpected result set triple '{0}'", actualTriple); } }
protected BaseGraph(BaseTripleCollection tripleCollection, BaseNodeCollection nodeCollection) { this._triples = tripleCollection; this._nodes = nodeCollection; this._bnodemapper = new BlankNodeMapper(); this._nsmapper = new NamespaceMapper(); //Create Event Handlers and attach to the Triple Collection this.TripleAddedHandler = new TripleEventHandler(this.OnTripleAsserted); this.TripleRemovedHandler = new TripleEventHandler(this.OnTripleRetracted); this.AttachEventHandlers(this._triples); }
/// <summary> /// Creates a new Base Graph which uses the given Triple Collection and the default <see cref="HashedNodeCollection">HashedNodeCollection</see> as the Node Collection /// </summary> /// <param name="tripleCollection">Triple Collection to use</param> protected BaseGraph(BaseTripleCollection tripleCollection) : this(tripleCollection, new HashedNodeCollection()) { }
/// <summary> /// Creates a new instance of a Graph using the given Triple and Node Collections /// </summary> /// <param name="tripleCollection">Triple Collection</param> /// <param name="nodeCollection">Node Collection</param> public Graph(BaseTripleCollection tripleCollection, BaseNodeCollection nodeCollection) : base(tripleCollection, nodeCollection) { }
/// <summary> /// Tries to load a Graph based on information from the Configuration Graph. /// </summary> /// <param name="g">Configuration Graph.</param> /// <param name="objNode">Object Node.</param> /// <param name="targetType">Target Type.</param> /// <param name="obj">Output Object.</param> /// <returns></returns> public bool TryLoadObject(IGraph g, INode objNode, Type targetType, out object obj) { obj = null; IGraph output; // Check whether to use a specific Triple Collection INode collectionNode = ConfigurationLoader.GetConfigurationNode(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyUsingTripleCollection))); try { if (collectionNode == null) { // Simple Graph creation output = (IGraph)Activator.CreateInstance(targetType); } else { // Graph with custom triple collection BaseTripleCollection tripleCollection = ConfigurationLoader.LoadObject(g, collectionNode) as BaseTripleCollection; if (tripleCollection == null) { throw new DotNetRdfConfigurationException("Unable to load the Graph identified by the Node '" + objNode.ToString() + "' as the dnr:usingTripleCollection points to an object which cannot be loaded as an instance of the required type BaseTripleCollection"); } output = (IGraph)Activator.CreateInstance(targetType, new Object[] { tripleCollection }); } } catch { // Any error means this loader can't load this type return(false); } // Now we want to find out where the data for the Graph is coming from // Data Source loading order is Graphs, Files, Strings, Databases, Stores, URIs IEnumerable <INode> sources; // Load from Graphs sources = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyFromGraph))); foreach (INode source in sources) { ConfigurationLoader.CheckCircularReference(objNode, source, "dnr:fromGraph"); Object graph = ConfigurationLoader.LoadObject(g, source); if (graph is IGraph) { output.Merge((IGraph)graph); } else { throw new DotNetRdfConfigurationException("Unable to load data from another Graph for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:fromGraph property points to an Object that cannot be loaded as an object which implements the IGraph interface"); } } // Load from Embedded Resources sources = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyFromEmbedded))); foreach (INode source in sources) { if (source.NodeType == NodeType.Literal) { EmbeddedResourceLoader.Load(output, ((ILiteralNode)source).Value); } else { throw new DotNetRdfConfigurationException("Unable to load data from an Embedded Resource for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:fromEmbedded property is not a Literal Node as required"); } } // Load from Files sources = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyFromFile))); foreach (INode source in sources) { if (source.NodeType == NodeType.Literal) { FileLoader.Load(output, ConfigurationLoader.ResolvePath(((ILiteralNode)source).Value)); } else { throw new DotNetRdfConfigurationException("Unable to load data from a file for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:fromFile property is not a Literal Node as required"); } } // Load from Strings sources = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyFromString))); foreach (INode source in sources) { if (source.NodeType == NodeType.Literal) { StringParser.Parse(output, ((ILiteralNode)source).Value); } else { throw new DotNetRdfConfigurationException("Unable to load data from a string for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:fromString property is not a Literal Node as required"); } } IEnumerable <Object> connections; // Load from Stores IEnumerable <INode> stores = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyFromStore))); stores.All(s => !ConfigurationLoader.CheckCircularReference(objNode, s, "dnr:fromStore")); connections = stores.Select(s => ConfigurationLoader.LoadObject(g, s)); sources = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyWithUri))); foreach (Object store in connections) { if (store is IStorageProvider) { foreach (INode source in sources) { if (source.NodeType == NodeType.Uri || source.NodeType == NodeType.Literal) { ((IStorageProvider)store).LoadGraph(output, source.ToString()); } else { throw new DotNetRdfConfigurationException("Unable to load data from a Generic Store for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:withUri property is not a URI/Literal Node as required"); } } } else if (store is ITripleStore) { foreach (INode source in sources) { if (source.NodeType == NodeType.Uri) { output.Merge(((ITripleStore)store)[((IUriNode)source).Uri]); } else if (source.NodeType == NodeType.Literal) { output.Merge(((ITripleStore)store)[UriFactory.Create(((ILiteralNode)source).Value)]); } else { throw new DotNetRdfConfigurationException("Unable to load data from a Store for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:withUri property is not a URI/Literal Node as required"); } } } else { throw new DotNetRdfConfigurationException("Unable to load data from a Store for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values of the dnr:fromStore property points to an Object which cannot be loaded as an object which implements either the IStorageProvider/ITripleStore interface"); } } // Load from Datasets IEnumerable <INode> ds = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyFromDataset))); ds.All(d => !ConfigurationLoader.CheckCircularReference(objNode, d, ConfigurationLoader.PropertyFromDataset)); IEnumerable <Object> datasets = ds.Select(d => ConfigurationLoader.LoadObject(g, d)); sources = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyWithUri))); foreach (Object dataset in datasets) { if (dataset is ISparqlDataset) { foreach (INode source in sources) { if (source.NodeType == NodeType.Uri) { output.Merge(((ISparqlDataset)dataset)[((IUriNode)sources).Uri]); } else if (source.NodeType == NodeType.Literal) { output.Merge(((ISparqlDataset)dataset)[UriFactory.Create(((ILiteralNode)source).Value)]); } else { throw new DotNetRdfConfigurationException("Unable to load data from a Dataset for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:withUri property is not a URI/Literal Node as required"); } } } else { throw new DotNetRdfConfigurationException("Unable to load data from a Dataset for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values of the dnr:fromDataset property points to an Object which cannot be loaded as an object which implements the required ISparqlDataset interface"); } } // Finally load from Remote URIs sources = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyFromUri))); foreach (INode source in sources) { if (source.NodeType == NodeType.Uri) { UriLoader.Load(output, ((IUriNode)source).Uri); } else if (source.NodeType == NodeType.Literal) { UriLoader.Load(output, UriFactory.Create(((ILiteralNode)source).Value)); } else { throw new DotNetRdfConfigurationException("Unable to load data from a URI for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:fromUri property is not a URI/Literal Node as required"); } } // Then are we assigning a Base URI to this Graph which overrides any existing Base URI? INode baseUri = ConfigurationLoader.GetConfigurationNode(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyAssignUri))); if (baseUri != null) { if (baseUri.NodeType == NodeType.Uri) { output.BaseUri = ((IUriNode)baseUri).Uri; } else if (baseUri.NodeType == NodeType.Literal) { output.BaseUri = UriFactory.Create(((ILiteralNode)baseUri).Value); } else { throw new DotNetRdfConfigurationException("Unable to assign a new Base URI for the Graph identified by the Node '" + objNode.ToString() + "' as the value for the dnr:assignUri property is not a URI/Literal Node as required"); } } // Finally we'll apply any reasoners IEnumerable <INode> reasoners = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyReasoner))); foreach (INode reasoner in reasoners) { Object temp = ConfigurationLoader.LoadObject(g, reasoner); if (temp is IInferenceEngine) { ((IInferenceEngine)temp).Apply(output); } else { throw new DotNetRdfConfigurationException("Unable to apply a reasoner for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:reasoner property points to an Object which cannot be loaded as an object which implements the IInferenceEngine interface"); } } obj = output; return(true); }
private static void CompareTripleCollections(BaseTripleCollection actualTriples, BaseTripleCollection expectedTriples, bool reduced) { var actualTripleList = new List<Triple>(actualTriples); var expectedTripleList = new List<Triple>(expectedTriples); var alreadySeen = new HashSet<Triple>(); var bnodeMap = new Dictionary<string, string>(); while (actualTripleList.Count > 0) { var at = actualTripleList[0]; actualTripleList.Remove(at); if (alreadySeen.Contains(at)) continue; var match = expectedTripleList.Find(x => TripleMatch(x, at, bnodeMap)); if (match == null) { Assert.Fail("No match found for actual triple {0} in expected triples set", at); } expectedTripleList.Remove(match); alreadySeen.Add(at); } Assert.IsTrue(actualTripleList.Count == 0); Assert.IsTrue(expectedTripleList.Count == 0, "Left with some unmatched triples in the expected triples set: {0}", String.Join(",", expectedTripleList.Select(t => t.ToString()))); }
public List <Tuple <string, string, string> > GetTriplesStringList(BaseTripleCollection triples) { List <Tuple <string, string, string> > temp = new List <Tuple <string, string, string> >(); foreach (var triple in triples) { Tuple <string, string, string> tempTuple; string item1 = ""; string item2 = ""; string item3 = ""; if (triple.Subject.ToString().StartsWith("urn:myClasses:")) { item1 = "Class:" + triple.Subject.ToString().Remove(0, 14); } if (triple.Subject.ToString().StartsWith("urn:myObjects:")) { item1 = "Object:" + triple.Subject.ToString().Remove(0, 14); } if (triple.Object.ToString().StartsWith("urn:myObjects:")) { item3 = "Object:" + triple.Object.ToString().Remove(0, 14); } if (triple.Object.ToString().StartsWith("urn:myClasses:")) { item3 = "Class:" + triple.Object.ToString().Remove(0, 14); } if (triple.Object.ToString().StartsWith("http://www.w3.org/2000/01/rdf-schema#Class")) { item3 = "rdfs:Class" + triple.Object.ToString().Remove(0, 42); } if (triple.Predicate.ToString().StartsWith(@"http://www.w3.org/1999/02/22-rdf-syntax-ns#type")) { item2 = "type"; } if (triple.Predicate.ToString().StartsWith(@"http://www.w3.org/2000/01/rdf-schema#subClassOf")) { item2 = "subClassOf"; } if (triple.Predicate.ToString().StartsWith(@"http://www.wikidata.org/wiki/Property:P1542")) { item2 = "whatFor"; } if (triple.Predicate.ToString().StartsWith(@"http://www.wikidata.org/wiki/Property:P2868")) { item2 = "causesWhy"; } if (triple.Subject.ToString().StartsWith("urn:myProperties:")) { item1 = "Property:" + triple.Subject.ToString().Remove(0, 17); } if (triple.Object.ToString().StartsWith(@"http://www.w3.org/1999/02/22-rdf-syntax-ns#Property")) { item3 = "Property"; } temp.Add(new Tuple <string, string, string>(item1, item2, item3)); } return(temp); }
public static Dictionary<INode, OutputRdfCollection> FindCollections(IGraph g, BaseTripleCollection triplesDone) { throw new NotSupportedException("No longer supported"); }
/// <summary> /// Creates a new instance of a Graph using the given Triple Collection and an optionally empty Namespace Map /// </summary> /// <param name="tripleCollection">Triple Collection</param> /// <param name="emptyNamespaceMap">Whether the Namespace Map should be empty</param> public Graph(BaseTripleCollection tripleCollection, bool emptyNamespaceMap) : base(tripleCollection) { if (emptyNamespaceMap) this._nsmapper.Clear(); }