]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.debug.browser/src/org/simantics/debug/browser/internal/rewriters/TypeHierarchyCreator.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.debug.browser / src / org / simantics / debug / browser / internal / rewriters / TypeHierarchyCreator.java
1 /*******************************************************************************
2  * Copyright (c) 2016 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     THTH ry - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.debug.browser.internal.rewriters;
13
14 import java.util.ArrayList;
15
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.debug.browser.content.ResourceBrowserContent;
20 import org.simantics.debug.browser.content.ResourceBrowserRewriter;
21 import org.simantics.debug.browser.sections.RawStatementsSection;
22 import org.simantics.debug.browser.sections.TypeHierarchySection;
23 import org.simantics.debug.browser.sections.TypeHierarchySection.Node;
24 import org.simantics.debug.browser.utils.ResourceInfo;
25 import org.simantics.layer0.Layer0;
26
27 public enum TypeHierarchyCreator implements ResourceBrowserRewriter {
28     INSTANCE;
29
30     @Override
31     public void rewrite(ReadGraph graph, ResourceBrowserContent content)
32             throws DatabaseException {
33         content.putSection(TypeHierarchySection.class,
34                 new TypeHierarchySection(
35                         new Node(null, null, getSuper(graph, content.resource))
36                         ));
37         
38         // Remove raw statements that this section already shows
39         Layer0 L0 = Layer0.getInstance(graph);
40         RawStatementsSection rawStatementsSection = content.getSection(RawStatementsSection.class);
41         rawStatementsSection.statementsByPredicates.remove(L0.InstanceOf);
42         rawStatementsSection.statementsByPredicates.remove(L0.SubrelationOf);
43         rawStatementsSection.statementsByPredicates.remove(L0.Inherits);
44     }
45     
46     public Node[] getSuper(ReadGraph graph, Resource resource) throws DatabaseException {
47         ArrayList<Node> nodes = new ArrayList<Node>();
48         Layer0 L0 = Layer0.getInstance(graph);
49         for(Resource s : graph.getObjects(resource, L0.InstanceOf))
50             nodes.add(new Node(":", new ResourceInfo(graph, s), getSuper(graph, s)));
51         for(Resource s : graph.getObjects(resource, L0.Inherits)) {
52             if(s.equals(L0.Entity))
53                 continue;
54             nodes.add(new Node("&lt;T", new ResourceInfo(graph, s), getSuper(graph, s)));
55         }
56         for(Resource s : graph.getObjects(resource, L0.SubrelationOf)) {
57             if(s.equals(L0.IsWeaklyRelatedTo))
58                 continue;
59             nodes.add(new Node("&lt;R", new ResourceInfo(graph, s), getSuper(graph, s)));
60         }
61         return nodes.toArray(new Node[nodes.size()]);
62     }
63
64     @Override
65     public double getPriority() {
66         return 1;
67     }
68 }