Exemplo n.º 1
0
 public Store Create(IEnumerable sources)
 {
     var store = new Store();
     foreach (var s in sources)
         if (s is SelectableSource)
             store.AddSource((SelectableSource)s);
     return store;
 }
Exemplo n.º 2
0
        public void Extract(string appBinDir, Store rdfStore)
        {
            string[] assemblyFiles = Directory.GetFiles( appBinDir, "*.dll");
            foreach (var assemblyFile in assemblyFiles) {

                var assembly = Assembly.LoadFrom(assemblyFile);
                Extract(assembly, rdfStore);
            }
        }
		private XPathSemWebNavigator(Entity root, Store model, NamespaceManager namespaces, string exapandThisPredicate) {
			this.model = model;
			
			if (!(namespaces is SemWeb.IO.AutoPrefixNamespaceManager))
				namespaces = new SemWeb.IO.AutoPrefixNamespaceManager(namespaces);
			this.nsmgr = namespaces;
			nswrap = new NSWrap(nsmgr);
			
			Position start = new Position();
			start.FirstChild = true;
			start.LastChild = true;
			start.Predicate = root; // a trick to make sure the URI info for the root reflects the root
			start.Object = root;
			if (exapandThisPredicate != null)
				Expand(start, exapandThisPredicate);
			current = start;
		}
Exemplo n.º 4
0
 public void Extract(Assembly assembly, Store rdfStore)
 {
     var types = assembly.GetExportedTypes();
     foreach (var t in types) {
         // store class hierarchy
         var typeEntity = GetEntity(t);
         if (t.IsInterface) {
             var stType = new Statement(typeEntity, NS.Rdf.typeEntity, NS.CSO.interfaceEntity);
             if (!rdfStore.Contains(stType)) {
                 rdfStore.Add(stType);
                 rdfStore.AddLabel(typeEntity, t.FullName);
             }
         } else if (t.IsClass) {
             var stType = new Statement(typeEntity, NS.Rdf.typeEntity, NS.CSO.classEntity);
             if (!rdfStore.Contains(stType)) {
                 rdfStore.Add(stType);
                 rdfStore.AddLabel(typeEntity, t.FullName);
             }
         } else
             continue;
         if (t.BaseType != null)
             rdfStore.Add(new Statement(typeEntity, NS.Rdfs.subClassOfEntity, GetEntity(t.BaseType) ));
         // store info about interfaces
         var ifaces = t.GetInterfaces();
         foreach (var iType in ifaces) {
             rdfStore.Add(new Statement(typeEntity, NS.CSO.Implements, GetEntity(iType)));
             rdfStore.Add(new Statement(typeEntity, NS.Rdfs.subClassOfEntity, GetEntity(iType)));
         }
         // store info about properties
         var props = t.GetProperties(BindingFlags.Public|BindingFlags.SetProperty|BindingFlags.DeclaredOnly|BindingFlags.Instance);
         foreach (var p in props) {
             var propEntity = NS.DotNet.GetPropertyEntity(p.Name);
             rdfStore.Add(new Statement(propEntity, NS.Rdf.typeEntity, NS.Rdf.PropertyEntity));
             rdfStore.AddLabel(propEntity, p.Name);
             rdfStore.Add(new Statement(propEntity, NS.Rdfs.domainEntity, typeEntity));
         }
     }
 }
Exemplo n.º 5
0
		public static void MakeLean(Store store, SelectableSource relativeTo) {
			MakeLean(store, relativeTo, null);
		}
Exemplo n.º 6
0
			SelectFilter filter;
			public Multi(Store source, SelectFilter filter)
				: base(source) {
				this.filter = filter;
Exemplo n.º 7
0
		// A graph g is not lean if it can be decomposed
		// into a and b such that a entails b.  (where
		// 'decomposed' means a and b don't overlap
		// and their union is g.)
		// One graph a entails another graph b when:
		//   Let V be the set of variables, which is the
		//   set of blank nodes that are in b but not in a.
		//   Let M be a mapping from nodes to nodes taking
		//   nodes that aren't in V to themselves.
		//   Let M* be a mapping from graphs to graphs that
		//   maps a graph to the same graph except where 
		//   each node x is replaced by M(x).
		//   If there exists an M such that M*(b) is a
		//   subgraph of a, then a entails b.
		// Let a and b be a decomposition of g, and V be
		// the variables in b w.r.t. a (as defined above).

		// Assume a entails b.
		// |a| >= |b|.
		// Since a and b are nonoverlapping, every statement
		// in b must have a variable.  b therefore contains
		// all and only the statements in g that mention a
		// variable.  (If b had a statement without a variable,
		// M*(b) would still have that statement, so it could
		// not be a subgraph of a.)
		
		// Define a N-decomposition as a decomposition of
		//   a graph g into g1 and g2 such that the nodes of
		//   N each appear in either g1 or g2, but not both.
		//   In such a decomposition, there is no statement
		//   in g that mentions a node from N and g1 and
		//   also mention a node from N and g2.
		
		// Assume b has a V-decomposition into b1 and b2.
		// Then if a entails b, a entails b1 and a entails b2.
		// Thus, if b has a V-decomposition, b need not be
		// considered as its decomposed parts will be considered.
		
		// Define 'directly connected' as a relation between
		// two nodes and a graph that is true iff there is
		// a statement in the graph that mentions both nodes.
		// Define connected (generally) as a relation between
		// two nodes x and y, a graph g, and a set S that is true
		// iff x and y are directly connected in g or else there
		// exists another node z in S such that x and z are
		// connected and z and y are connected, in g with S.
		
		// If b has a V-decomposition, then V can be decomposed
		// into V1 and V2 and b can be decomposed into b1 and b2
		// such that all nodes in V1 appear in b1 and all nodes
		// in V2 appear in b2.  It can be seen that a node in
		// V1 cannot be connected to a node in V2 w.r.t. b and V.
		// Therefore iff every node in V is connected to every
		// other node in V, then b has no V-decomposition.
		
		// The only b's to consider are those whose variables V
		// are all connected to each other in b w.r.t. V.
		
		// The plan then is first to consider MSGs, and then
		// look at their subgraphs.
	
		public static void MakeLean(Store store) {
			MakeLean(store, null, null);
		}
		public override bool Filter(Resource resource, Store targetModel) {
			string v = ((Literal)resource).Value;
			int c = v.CompareTo(pattern);
			if (compare == 0) return (c == 0) ^ !eq;
			return c == compare || (c == 0 && eq);
		}
Exemplo n.º 9
0
			Statement template;
			public Single(Store source, Statement template) : base(source) {
				this.template = template;
Exemplo n.º 10
0
		private static void MakeLeanMSG2(Store msg, ResSet predicates, StatementSink removed,
			ResSet nodesremoved, BNode startingnode) {
			
			// Find every pair of two distinct outgoing edges from startingnode
			// with the same predicate, targeting entities only.
			
			MultiMap edges = new MultiMap();
			
			foreach (Statement s in msg.Select(new Statement(startingnode, null, null)))
				if (s.Object is Entity)
					edges.Put(new Edge(true, startingnode, s.Predicate, null), s.Object);
			foreach (Statement s in msg.Select(new Statement(null, null, startingnode)))
				edges.Put(new Edge(false, startingnode, s.Predicate, null), s.Subject);
			
			foreach (Edge e in edges.Keys) {
				// Make sure we have a distinct set of targets.
				ResSet targets_set = new ResSet();
				foreach (Entity r in edges.Get(e))
					targets_set.Add(r);
				if (targets_set.Count == 1) continue;
				
				IList targets = targets_set.ToEntityArray();
				
				// Take every pair of targets, provided
				// one is a bnode that can be a variable.
				for (int i = 0; i < targets.Count; i++) {
					if (!(targets[i] is BNode) || predicates.Contains((BNode)targets[i])) continue;
					if (nodesremoved.Contains((BNode)targets[i])) continue;
					for (int j = 0; j < targets.Count; j++) {
						if (i == j) continue;
						// Create a new synchronous-path object.
						SyncPath p = new SyncPath();
						p.FixedNodes.Add((Resource)targets[j]);
						p.FrontierVariables.Add((Resource)targets[i]);
						p.Mapping[targets[i]] = targets[j];
						p.Path[new Edge(e.Direction, e.Start, e.Predicate, (BNode)targets[i])] = p.Path;
						if (MakeLeanMSG3(msg, predicates, removed, nodesremoved, p))
							break; // the target was removed
					}
				}
			}
		}
Exemplo n.º 11
0
			public Sink(ResSet variables, Store store) {
				this.variables = variables;
				this.store = store;
			}
		public override bool Filter(Resource resource, Store targetModel) {
			string v = ((Literal)resource).Value;
			try {
				TimeSpan i = TimeSpan.Parse(v);
				int c = i.CompareTo(timespan);
				if (compare == 0) return (c == 0) ^ !eq;
				return c == compare || (c == 0 && eq);
			} catch (Exception e) {
				return false;
			}
		}
		public void AddReasoning(ReasoningEngine engine) {
			mainstore = new InferenceStore(mainstore, engine);
		}
		public XPathSemWebNavigator(Store model, NamespaceManager namespaces) : this(ExpandEntitiesOfType, model, namespaces, null) { }
		public XPathSemWebNavigator(Entity root, Store model, NamespaceManager namespaces) : this(root, model, namespaces, null) { }
		public override bool MoveTo (XPathNavigator other) {
			XPathSemWebNavigator clone = other as XPathSemWebNavigator;
			if (clone == null) return false;
			this.model = clone.model;
			this.nsmgr = clone.nsmgr;
			this.nswrap = clone.nswrap;
			this.stack = (ArrayList)clone.stack.Clone();
			this.current = clone.current;
			return true;
		}
Exemplo n.º 17
0
        protected void TestReasoner(DalcRdfStore dalcStore)
        {
            var store = new Store(dalcStore);
            Euler engine = new Euler(new N3Reader(new StringReader(eulerRules)));
            store.AddReasoner(engine);

            Console.WriteLine(
                "Is Adam a parent of Bill? "+
                store.Contains(
                    new Statement(baseNs + "persons#1", baseNs + "terms#is_parent", (Entity)(baseNs + "persons#4"))).ToString());
            Console.WriteLine(
                "Is Eve a parent of Bill? " +
                store.Contains(
                    new Statement(baseNs + "persons#2", baseNs + "terms#is_parent", (Entity)(baseNs + "persons#4"))).ToString());

            Console.Write("Children of Eve: ");
            var res = store.Select(new Statement(null, baseNs + "terms#is_child", (Entity)(baseNs + "persons#2") ));
            foreach (var st in res) {
                var nameRes = store.Select( new Statement( st.Subject.Uri, ns_foaf_name, null) );
                Console.Write(String.Format("({0})", dalcStore.GetDataKey(st.Subject).Id));
                foreach (var nameSt in nameRes)
                    Console.Write( nameSt.Object.ToString() + " ");
            }
            Console.WriteLine();
        }
		public override bool Filter(Resource resource, Store targetModel) {
			string v = ((Literal)resource).Value;
			return v.IndexOf(pattern) != -1;
		}
Exemplo n.º 19
0
		public static void MakeLean(Store store, SelectableSource relativeTo, StatementSink removed) {
			// Break the data source into MSGs.  Make each MSG
			// lean first (in isolation).  Then check each lean MSG
			// to see if it's already entailed by the whole store,
			// or by relativeTo if it's provided (not null).
		
			MSG.Graph[] msgs = MSG.FindMSGs(store, true);
			
			foreach (MSG.Graph msgg in msgs) {
				// Load the MSG into memory.
				MemoryStore msg = new MemoryStore(msgg); // unnecessary duplication...

				// Make this MSG lean.  The "right" thing to do is
				// to consider all of the 'connected' subgraphs of MSG
				// against the whole store, rather than the MSG in
				// isolation.  But that gets much too expensive.
				MemoryStore msgremoved = new MemoryStore();
				MakeLeanMSG(new Store(msg), msgg.GetBNodes(), msgremoved);
				
				// Whatever was removed from msg, remove it from the main graph.
				store.RemoveAll(msgremoved.ToArray());
				
				// And track what was removed.
				if (removed != null) msgremoved.Select(removed);
				
				// If this MSG is now (somehow) empty (shouldn't happen,
				// but one never knows), don't test for entailment.
				if (msg.StatementCount == 0) continue;

				// Remove this MSG if it is already entailed.
				
				// The GraphMatch will treat all blank nodes in
				// msg as variables.
				GraphMatch match = new GraphMatch(msg);
				QueryResultBuffer sink = new QueryResultBuffer();
				match.Run(new SubtractionSource(store, msg), sink);
				if (sink.Bindings.Count > 0) {
					// This MSG can be removed.
					store.RemoveAll(msg.ToArray());
					if (removed != null) msg.Select(removed);
				} else if (relativeTo != null) {
					match.Run(relativeTo, sink);
					if (sink.Bindings.Count > 0) {
						// This MSG can be removed.
						store.RemoveAll(msg.ToArray());
						if (removed != null) msg.Select(removed);
					}
				}
			}
		}
Exemplo n.º 20
0
 public RdfDalc(Store rdfStore)
 {
     RdfStore = rdfStore;
 }
Exemplo n.º 21
0
		private static void MakeLeanMSG(Store msg, ICollection bnodecollection, StatementSink removed) {
			// To make any graph lean, we try to eliminate duplicate
			// paths through the graph, where duplicate means we
			// take some subset of the bnodes and call them variables,
			// and we relabel them as other bnodes from the remaining
			// set (the fixed nodes).  But there are 2^N subsets of bnodes
			// we could choose as variables (N=number of bnodes), so we can't
			// reasonably iterate through them.
			
			// I'll make a simplifying assumption that bnode predicates
			// in the graph will be considered always fixed.
			// This lets us view the graph as actually a graph (with
			// nodes and edges), and then we can make the observation that
			// if variable node V is part of a subgraph that can be removed,
			// if V directly connects to fixed node F via an edge labeled P,
			// then F must connect to a fixed node G via an edge also
			// labeled P.  That is, we can start our search looking for
			// nodes that project two edges with the same label.
			
			// Also, we only want to consider contiguous 'paths' -- subsets
			// of the bnodes connected only through those nodes --
			// to see if there is another path in the MSG if we
			// map bnodes in the first path to nodes in the MSG.
			
			// So the strategy is to start at each node in the graph
			// and consider it fixed.  If it has two outgoing
			// edges with the same property and one terminates on a
			// bnode, this is the beginning of a possible pair
			// of redundant paths (the one with the bnode being
			// eliminable).
			
			// However, the path with the bnode
			// has to be incremented with all of that bnode's
			// outgoing edges.  The other path has to be
			// incremented in parallel, following the same predicates
			// to other nodes.  If that can't be done, then these
			// paths are not duplicates.  If the parallel predicates
			// terminate on the very same nodes, the bnode and its edges can
			// be removed.
			
			// From there, each of the nodes the bnode edges terminate on,
			// besides the initial node, can be considered fixed or
			// a variable.  If it's a variable it might be able to have
			// one of many possible values, but then the path has to
			// be expanded to include all of the outgoing edges for this
			// variable.
		
			// Ok, here we go.
			
			// If there is only one bnode in the MSG, then
			// there are no subgraphs to check.  That's nice.
			if (bnodecollection.Count == 1) return;
			
			// Remember which bnodes have been removed in
			// due course.
			ResSet nodesremoved = new ResSet();
			
			// Remember which nodes are predicates and can't
			// be considered variable.
			ResSet predicates = new ResSet();
			foreach (Statement s in msg.Select(Statement.All))
				predicates.Add(s.Predicate);
			
			// Start with each bnode to consider fixed.
			foreach (BNode b in bnodecollection) {
				if (nodesremoved.Contains(b)) continue;
				MakeLeanMSG2(msg, predicates, removed, nodesremoved, b);
			}
		}
 public void LoadAnnotationOntologyFromString(string rdfData)
 {
     _conceptsStore = new MemoryStore();
     RdfXmlReader reader = new RdfXmlReader(new System.IO.StringReader(rdfData));
     _conceptsStore.Import(reader);
     UpdateDictionaries();
 }
Exemplo n.º 23
0
		private static bool MakeLeanMSG3(Store msg, ResSet predicates, StatementSink removed,
			ResSet nodesremoved, SyncPath path) {
			// The variable path has to be expanded by including the statements
			// connected to the variables on the frontier.  Statements
			// mentioning a variable node have already been considered.
			// The target of each such statement can be considered fixed
			// or variable. If a variable is considered fixed, the edge
			// must exist in the MSG substituting the variables for their
			// values.  If it's variable, it has to have at least one
			// match in the MSG but not as any of the variable nodes.
			// If all targets are considered fixed (and have matches),
			// then the variables so far (and their edges) can all be
			// removed and no more processing needs to be done.
			// There are (2^N)-1 other considerations.  For each of those,
			// the targets considered variables all become the new
			// frontier, and this is repeated. 
			
			// First, get a list of edges from the frontier that we
			// haven't considered yet.
			
			ArrayList alledges = new ArrayList();
			foreach (BNode b in path.FrontierVariables) {
				// Make sure all edges are kept because even the ones
				// to literals have to be removed when duplication is found.
				foreach (Statement s in msg.Select(new Statement(b, null, null)))
					alledges.Add(new Edge(true, b, s.Predicate, s.Object));
				foreach (Statement s in msg.Select(new Statement(null, null, b)))
					alledges.Add(new Edge(false, b, s.Predicate, s.Subject));
			}
			
			ArrayList newedges = new ArrayList();
			ResSet alltargets = new ResSet();
			ResSet fixabletargetsset = new ResSet(); // can be fixed
			ResSet variabletargetsset = new ResSet(); // must be variable
			foreach (Edge e in alledges) {
				if (path.Path.ContainsKey(e)) continue;
				path.Path[e] = e;
				
				// This checks if we can keep the target of this edge
				// fixed, given the variable mappings we have so far.
				bool isTargetFixable =
					msg.Contains(e.AsStatement().Replace(path.Mapping));

				// If the target of e is any of the following, we
				// can check immediately if the edge is supported
				// by the MSG under the variable mapping we have so far:
				//    a named node, literal, fixed node, or predicate
				//    a variable we've seen already
				// If it's not supported, this path fails.  If it is
				// supported, we're done with this edge.
				if (!(e.End is BNode)
					|| path.FixedNodes.Contains(e.End)
					|| predicates.Contains(e.End)
					|| path.VariableNodes.Contains(e.End)) {
					if (!isTargetFixable) return false;
					continue; // this edge is supported, so we can continue
				}
				
				// The target of e is a new BNode.
				// If this target is not fixable via this edge, it's
				// not fixable at all.
				
				if (!isTargetFixable) {
					fixabletargetsset.Remove(e.End);
					variabletargetsset.Add(e.End);
				}
				
				if (!alltargets.Contains(e.End)) {
					alltargets.Add(e.End);
					fixabletargetsset.Add(e.End);
				}
				
				newedges.Add(e);
			}
			
			// If all of the targets were fixable (trivially true also
			// if there simple were no new edges/targets), then we've reached
			// the end of this path.  We can immediately remove
			// the edges we've seen so far, under the variable mapping
			// we've chosen.
			if (variabletargetsset.Count == 0) {
				foreach (Edge e in path.Path.Keys) {
					Statement s = e.AsStatement();
					msg.Remove(s);
					if (removed != null) removed.Add(s);
				}
				foreach (Entity e in path.Mapping.Keys)
					nodesremoved.Add(e);
				return true;
			}
			
			// At this point, at least one target must be a variable
			// and we'll have to expand the path in that direction.
			// We might want to permute through the ways we can
			// take fixable nodes as either fixed or variable, but
			// we'll be greedy and assume everything fixable is
			// fixed and everything else is a variable.
			
			path.FixedNodes.AddRange(fixabletargetsset);
			path.VariableNodes.AddRange(variabletargetsset);

			// But we need to look at all the ways each variable target
			// can be mapped to a new value, which means intersecting
			// the possible matches for each relevant edge.
			Entity[] variables = variabletargetsset.ToEntityArray();
			ResSet[] values = new ResSet[variables.Length];
			Entity[][] values_array = new Entity[variables.Length][];
			int[] choices = new int[variables.Length];
			for (int i = 0; i < variables.Length; i++) {
				foreach (Edge e in newedges) {
					if (e.End != variables[i]) continue;
					
					// Get the possible values this edge allows
					Resource[] vr;
					if (e.Direction)
						vr = msg.SelectObjects((Entity)path.Mapping[e.Start], e.Predicate);
					else
						vr = msg.SelectSubjects(e.Predicate, (Entity)path.Mapping[e.Start]);
					
					// Filter out literals and any variables
					// on the path!  The two paths can't intersect
					// except at fixed nodes.
					ResSet v = new ResSet();
					foreach (Resource r in vr) {
						if (r is Literal) continue;
						if (path.Mapping.ContainsKey(r)) continue;
						v.Add(r);
					}
					
					// Intersect these with the values we have already.
					if (values[i] == null)
						values[i] = v;
					else
						values[i].RetainAll(v);
						
					// If no values are available for this variable,
					// we're totally done.
					if (values[i].Count == 0) return false;
				}
				
				choices[i] = values[i].Count;
				values_array[i] = values[i].ToEntityArray();
			}
			
			// Now we have to permute through the choice of values.
			// Make an array of the number of choices for each variable.
			Permutation p = new Permutation(choices);
			int[] pstate;
			while ((pstate = p.Next()) != null) {
				SyncPath newpath = new SyncPath();
				newpath.FixedNodes.AddRange(path.FixedNodes);
				newpath.VariableNodes.AddRange(path.VariableNodes);
				newpath.Mapping = (Hashtable)path.Mapping.Clone();
				newpath.Path = (Hashtable)path.Path.Clone();
				
				newpath.FrontierVariables = variabletargetsset;
				
				for (int i = 0; i < variables.Length; i++) {
					Entity value = values_array[i][pstate[i]];
					newpath.Mapping[variables[i]] = value;
					newpath.FixedNodes.Add(value);
				}

				if (MakeLeanMSG3(msg, predicates, removed,
					nodesremoved, newpath)) return true;
			}
			
			return false;
		}
		public void Add(Store storage) {
			Storage.Add(storage);
		}
Exemplo n.º 25
0
			public DupCheckSink(Store store) { this.store = store; }
		public abstract bool Filter(Resource resource, Store targetModel);
 public void LoadAnnotationOntologyFromFile(string filename)
 {
     _conceptsStore = new MemoryStore();
     AddEventAndLog("Loading annotation ontology from file: " + filename);
     _conceptsStore.Import(RdfReader.LoadFromUri(new Uri(filename)));
     UpdateDictionaries();
 }
Exemplo n.º 28
0
 public Bindings(string targetschemauri, Store schemas, Hashtable bindingmap)
 {
     this.targetschemauri = targetschemauri;
     this.schemas = schemas;
     this.bindingmap = bindingmap;
 }
Exemplo n.º 29
0
 public DublinCoreConverter(Store store)
 {
     Store = store;
     RdfDocument = new RdfDocument(store);
 }
Exemplo n.º 30
0
		MemoryStore ms;