private void loadFileToolStripMenuItem_Click(object sender, EventArgs e) { //SimpleGraphAdapter test, load cdbOpenModel.Filter = "Text Files (*.txt)|*.txt"; DialogResult d = cdbOpenModel.ShowDialog(); if (d != DialogResult.Cancel) { this.Cursor = Cursors.WaitCursor; Application.DoEvents(); try { oGraphAdapter = new SimpleGraphAdapter(); layoutControl1.SetAndShowGraph(oGraphAdapter.LoadGraphFromFile(cdbOpenModel.FileName)); } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } this.Cursor = Cursors.Default; //Application.DoEvents(); } }
protected void Configure(IDomainModel domainModel) { DebugContract.Requires(domainModel); if (_domainModel != null) { return; } _trace = domainModel.Resolve <IHyperstoreTrace>(false) ?? new EmptyHyperstoreTrace(); _domainModel = domainModel; var kv = _services.Resolve <IKeyValueStore>() ?? new Hyperstore.Modeling.MemoryStore.TransactionalMemoryStore(domainModel); _storage = kv; if (kv is IDomainService) { ((IDomainService)kv).SetDomain(domainModel); } _indexManager = new Hyperstore.Modeling.HyperGraph.Index.MemoryIndexManager(this); // TODO lier avec TransactionalMemoryStore _loader = _services.Resolve <IGraphAdapter>(); if (_loader is IDomainService) { ((IDomainService)_loader).SetDomain(domainModel); } _lazyLoader = _loader as ISupportsLazyLoading; }
public static IList <Node> FindPath <Node>(IGraphAdapter <Node> graph, Node start, Node goal) { var comparer = EqualityComparer <Node> .Default; var searchSpace = new PriorityQueue <Node>(); var pathMap = new Dictionary <Node, Node>(); var costSoFar = new Dictionary <Node, int>(); searchSpace.Enqueue(start, 0); costSoFar[start] = 0; while (searchSpace.Count != 0) { var current = searchSpace.Dequeue(); if (comparer.Equals(current, goal)) { break; } var linked = graph.GetLinked(current); foreach (var linkedNode in linked) { var newCost = costSoFar[current] + graph.GetMoveCost(current, linkedNode); if (newCost < costSoFar.GetOrDefault(linkedNode, int.MaxValue)) { costSoFar[linkedNode] = newCost; searchSpace.Enqueue(linkedNode, newCost + graph.GetScore(linkedNode, goal)); pathMap[linkedNode] = current; } } } return(ResolvePath(pathMap, start, goal)); }
/// <summary> /// Computes the immediate dominator of each basic block. /// The default implementation directs the task to the Cooper-Harvey-Kennedy algorithm. /// </summary> protected virtual void ComputeDominators() { IGraphAdapter <BasicBlock <Ti> > a = BasicBlock <Ti> .DominanceAnalysisAdapter; a.InvertRelation(a.Succs, a.Preds, BasicBlocks); a.ComputeImmediateDominators(BasicBlocks, EntryCB); }
/// <summary> /// Creates a new list for each temporary list storage property. /// </summary> /// <param name="a">the graph adapter</param> /// <param name="nodes">enumeration of nodes whose temporary list storage properties are to be initialized</param> public static void CreateDefaultTempStorage <T>(this IGraphAdapter <T> a, IEnumerable <T> nodes) { a.TempList.RequireAccess(EAccess.WriteOnly); foreach (T node in nodes) { a.TempList[node] = new List <T>(); } }
/// <summary> /// Analyzes the loop nesting structure of the control-flow graph. /// The default implementation directs the tasks to Havlak's algorithm. /// </summary> protected virtual void AnalyzeLoops() { IGraphAdapter <BasicBlock <Ti> > a = BasicBlock <Ti> .LoopAnalysisAdapter; BasicBlock <Ti>[] reachable = a.GetPreOrder(BasicBlocks, EntryCB); a.InvertRelation(a.Succs, a.Preds, reachable); a.AnalyzeLoops(reachable, EntryCB); }
public void TearDown() { m_oGraphAdapter = null; if ( File.Exists(m_sTempFileName) ) { File.Delete(m_sTempFileName); } }
/// <summary> /// Computes a the immediate dominators of all specified graph nodes. /// </summary> /// <param name="a">graph adapter</param> /// <param name="nodes">nodes to consider</param> /// <param name="entry">entry node</param> public static void ComputeImmediateDominators <T>(this IGraphAdapter <T> a, IList <T> nodes, T entry) where T : class { ComputeImmediateDominators(a.Succs, a.Preds, a.PostOrderIndex, a.IDom, nodes, entry); a.IDom[entry] = null; a.InvertRelation(a.IDom, a.IDoms, nodes); a.IDom[entry] = entry; }
/// <summary> /// Constructor</summary> /// <param name="renderer">Graph renderer</param> /// <param name="graphAdapter">Graph adapter</param> /// <param name="transformAdapter">Transform adapter</param> public GraphEdgeEditAdapter( GraphRenderer <TNode, TEdge, TEdgeRoute> renderer, IGraphAdapter <TNode, TEdge, TEdgeRoute> graphAdapter, ITransformAdapter transformAdapter) { m_renderer = renderer; m_graphAdapter = graphAdapter; m_transformAdapter = transformAdapter; }
/// <summary> /// Performs a depth-first search on the successor relation, updating the pre-order index /// and pre-order last index relations of the graph adapter. /// </summary> /// <param name="a">graph adapter</param> /// <param name="nodes">nodes to be considered</param> /// <param name="start">start node</param> /// <returns>all visited nodes in pre-order</returns> public static T[] GetPreOrder <T>(this IGraphAdapter <T> a, IList <T> nodes, T start) { a.PreOrderIndex.Reset(nodes, -1); T[] result = new T[nodes.Count]; int index = 0; a.PreOrderDFS(result, start, ref index); // if some nodes are unreachable, the last elements of the result // array are null. These should be eliminated: result = result.Where(x => x != null).ToArray(); return(result); }
private static void PreOrderDFS <T>(this IGraphAdapter <T> a, T[] result, T node, ref int index) { Contract.Requires <ArgumentNullException>(a != null); Contract.Requires <ArgumentNullException>(result != null); Contract.Requires <ArgumentException>(index >= 0); Contract.Requires <ArgumentException>(index < result.Length); result[index] = node; a.PreOrderIndex[node] = index++; foreach (T child in a.Succs[node]) { if (a.PreOrderIndex[child] < 0) { a.PreOrderDFS(result, child, ref index); } } a.PreOrderLast[node] = result[index - 1]; }
/// <summary> /// Inverts a property map-based relation. /// </summary> /// <param name="a">graph adapter providing temporary list storage</param> /// <param name="srcrel">source property map</param> /// <param name="dstrel">destination property map for inverted relation</param> /// <param name="nodes">nodes to be considered for the inversion</param> public static void InvertRelation <T>(this IGraphAdapter <T> a, IPropMap <T, T[]> srcrel, IPropMap <T, T[]> dstrel, IEnumerable <T> nodes) { a.CreateDefaultTempStorage(nodes); srcrel.RequireAccess(EAccess.ReadOnly); dstrel.RequireAccess(EAccess.WriteOnly); foreach (T node in nodes) { foreach (T adj in srcrel[node]) { a.TempList[adj].Add(node); } } foreach (T node in nodes) { dstrel[node] = a.TempList[node].ToArray(); } a.TempList.Clear(nodes); }
///------------------------------------------------------------------------------------------------- /// <summary> /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged /// resources. /// </summary> ///------------------------------------------------------------------------------------------------- public void Dispose() { if (_disposed) { return; } if (_storage is IDisposable) { ((IDisposable)_storage).Dispose(); } if (_loader is IDisposable) { ((IDisposable)_loader).Dispose(); } _loader = null; _storage = null; _disposed = true; _domainModel = null; }
///------------------------------------------------------------------------------------------------- /// <summary> /// Loads. /// </summary> /// <param name="adapter"> /// The adapter. /// </param> /// <param name="query"> /// The query. /// </param> /// <param name="option"> /// The option. /// </param> /// <returns> /// Number of nodes loaded /// </returns> ///------------------------------------------------------------------------------------------------- public Task <int> LoadAsync(Query query = null, MergeOption option = MergeOption.OverwriteChanges, IGraphAdapter adapter = null) { return(InnerGraph.LoadNodes(query ?? new Query { DomainModel = this.Name }, option, adapter, false)); }
/// <summary> /// Returns <c>true</c> if <paramref name="w"/> is an ancestor of <c>v</c>. /// </summary> public static bool IsAncestor <T>(this IGraphAdapter <T> a, T w, T v) { return(IsAncestor(a.PreOrderIndex, a.PreOrderLast, w, v)); }
/// <summary> /// Determines the loop nesting structure of the given (control-flow) graph. /// </summary> /// <remarks> /// This is an implementation of Paul Havlak's loop analysis algorithm. /// Reference: ACM Transactions on Programming Languages and Systems, Vol. 19, No. 4, July 1997, Pages 557-567. /// </remarks> /// <param name="a">graph adapter</param> /// <param name="nodes">nodes to consider</param> /// <param name="start">entry node</param> public static void AnalyzeLoops <T>(this IGraphAdapter <T> a, IList <T> nodes, T start) { T[] order = a.GetPreOrder(nodes, start); // fix_loops part #if false foreach (T w in order) { a.RedBackIn[w] = new HashSet <T>(); a.OtherIn[w] = new HashSet <T>(); foreach (T v in a.Preds[w]) { if (a.PreOrderIndex[w] < a.PreOrderIndex[v]) { a.RedBackIn[w].Add(v); } else { a.OtherIn[w].Add(v); } } if (a.RedBackIn[w].Count > 0 && a.OtherIn[w].Count > 1) { throw new NotImplementedException(); } } #endif foreach (T w in order /*nodes*/) { a.BackPreds[w] = new HashSet <T>(); a.NonBackPreds[w] = new HashSet <T>(); a.Header[w] = start; a.Type[w] = ENodeType.Nonheader; foreach (T v in a.Preds[w]) { if (a.IsAncestor(w, v)) { a.BackPreds[w].Add(v); } else { a.NonBackPreds[w].Add(v); } } } a.Header[start] = default(T); HashSet <T> P = new HashSet <T>(); UnionFind <T> uf = new PreOrderSetAdapter <T>(a).CreateUnionFind(order); for (int iw = order.Length - 1; iw >= 0; iw--) { T w = order[iw]; P.Clear(); foreach (T v in a.BackPreds[w]) { if (!v.Equals(w)) { P.Add(uf.Find(v)); } else { a.Type[w] = ENodeType.Self; } } Queue <T> worklist = new Queue <T>(P); if (P.Count > 0) { a.Type[w] = ENodeType.Reducible; } while (worklist.Count > 0) { T x = worklist.Dequeue(); foreach (T y in a.NonBackPreds[x]) { T y_ = uf.Find(y); if (!a.IsAncestor(w, y_)) { a.Type[w] = ENodeType.Irreducible; a.NonBackPreds[w].Add(y_); } else if (!P.Contains(y_) && !w.Equals(y_)) { P.Add(y_); worklist.Enqueue(y_); } } } foreach (T x in P) { a.Header[x] = w; uf.Union(x, w); } } }
public void SetUp() { m_oGraphAdapter = new PajekGraphAdapter(); m_sTempFileName = Path.GetTempFileName(); }
//************************************************************************* // Constructor: PajekGraphAdapterTest() // /// <summary> /// Initializes a new instance of the <see cref="PajekGraphAdapterTest" /> /// class. /// </summary> //************************************************************************* public PajekGraphAdapterTest() { m_oGraphAdapter = null; m_sTempFileName = null; }
public PreOrderSetAdapter(IGraphAdapter <T> a) { Contract.Requires <ArgumentNullException>(a != null); Index = a.PreOrderIndex; }
//************************************************************************* // Constructor: GraphMLGraphAdapterTest() // /// <summary> /// Initializes a new instance of the <see cref="GraphMLGraphAdapterTest" /> /// class. /// </summary> //************************************************************************* public GraphMLGraphAdapterTest() { m_oGraphAdapter = null; m_sTempFileName = null; }
public Task <int> LoadNodes(Query query, MergeOption option, IGraphAdapter adapter, bool lazyLoading) { if (adapter == null) { adapter = _loader; } var tcs = new TaskCompletionSource <int>(); var cx = 0; if (adapter == null) { tcs.TrySetResult(cx); return(tcs.Task); } var oldLazyLoader = _lazyLoader; try { // Disable lazy loading _lazyLoader = null; var q = adapter is ISupportsLazyLoading && lazyLoading ? ((ISupportsLazyLoading)adapter).LazyLoadingNodes(query) : adapter.LoadNodes(query); using (var session = this.Store.BeginSession(new SessionConfiguration { Mode = SessionMode.Loading | SessionMode.SkipConstraints })) { foreach (var result in q) { if (Session.Current != null && Session.Current.TrackingData.GetTrackedElementState(result.Id) == TrackingState.Removed) { continue; } cx++; var newInCache = false; var nodeMetaclass = result.SchemaInfo; // Si ce noeud n'existe pas dans le cache, on le met GraphNode graphNode; GetGraphNode(result.Id, result.NodeType, out graphNode); var node = graphNode as GraphNode; if (node == null) { if (result.NodeType == NodeType.Edge) { var rSchema = nodeMetaclass as ISchemaRelationship; node = CreateRelationship(result.Id, rSchema, result.StartId, result.EndId ) as GraphNode; } else { node = CreateEntity(result.Id, nodeMetaclass as ISchemaEntity) as GraphNode; } newInCache = true; } if (option == MergeOption.AppendOnly && newInCache || option == MergeOption.OverwriteChanges) { foreach (var edge in result.Outgoings) { var edgeSchema = _domainModel.Store.GetSchemaRelationship(edge.SchemaId); node = node.AddEdge(edge.Id, edgeSchema, Direction.Outgoing, edge.EndId); } foreach (var edge in result.Incomings) { var edgeSchema = _domainModel.Store.GetSchemaRelationship(edge.SchemaId); node = node.AddEdge(edge.Id, edgeSchema, Direction.Incoming, edge.EndId); } } var ctx = new SerializationContext(_domainModel, result.SchemaInfo, result); var mel = (IModelElement)result.SchemaInfo.Deserialize(ctx); if (mel != null) { if (result.Properties != null) { foreach (var property in result.Properties) { // Mise à jour des propriétés lues if (option == MergeOption.AppendOnly && newInCache || option == MergeOption.OverwriteChanges) { SetPropertyValue(mel, property.Key, property.Value.Value, property.Value.CurrentVersion); } else if (option == MergeOption.PreserveChanges) { if (GetPropertyValue(node.Id, property.Key) == null) { SetPropertyValue(mel, property.Key, property.Value.Value, property.Value.CurrentVersion); } } } } } } session.AcceptChanges(); } tcs.TrySetResult(cx); } catch (Exception ex) { tcs.SetException(ex); } finally { _lazyLoader = oldLazyLoader; } return(tcs.Task); }
/// <summary> /// Inverts a parent relation, taking the successor relation as target. /// </summary> /// <param name="a">graph adapter providing the relations</param> /// <param name="nodes">nodes to be considered</param> public static void ComputeSuccessorsFromParent <T>(this IGraphAdapter <T> a, IEnumerable <T> nodes) { a.InvertRelation(a.Parent, a.Succs, nodes); }
/// <summary> /// Inverts a successor relation, taking the predecessor relation as target. /// </summary> /// <param name="a">graph adapter providing the relations</param> /// <param name="nodes">nodes to be considered</param> public static void ComputePredecessorsFromSuccessors <T>(this IGraphAdapter <T> a, IEnumerable <T> nodes) { a.InvertRelation(a.Succs, a.Preds, nodes); }