1 /*******************************************************************************
2 * Copyright (c) 2016 Association for Decentralized Information Management
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
10 * THTH ry - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.debug.browser.internal.rewriters;
14 import java.util.ArrayList;
16 import org.simantics.databoard.Bindings;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.Statement;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.debug.browser.content.ResourceBrowserContent;
22 import org.simantics.debug.browser.content.ResourceBrowserRewriter;
23 import org.simantics.debug.browser.sections.BreadcrumbSection;
24 import org.simantics.debug.browser.sections.RawStatementsSection;
25 import org.simantics.debug.browser.sections.BreadcrumbSection.Node;
26 import org.simantics.layer0.Layer0;
28 public enum BreadcrumbCreator implements ResourceBrowserRewriter {
32 public void rewrite(ReadGraph graph, ResourceBrowserContent content)
33 throws DatabaseException {
34 ArrayList<Node> nodes = new ArrayList<Node>();
35 collectBreadcrumbs(graph, nodes, content.resource);
36 if(!nodes.isEmpty()) {
37 content.putSection(BreadcrumbSection.class,
38 new BreadcrumbSection(nodes.toArray(new Node[nodes.size()])));
40 Layer0 L0 = Layer0.getInstance(graph);
41 RawStatementsSection rawStatementsSection = content.getSection(RawStatementsSection.class);
42 rawStatementsSection.statementsByPredicates.remove(L0.HasName);
43 rawStatementsSection.statementsByPredicates.remove(L0.PartOf);
47 private static void collectBreadcrumbs(ReadGraph graph, ArrayList<Node> nodes,
48 Resource resource) throws DatabaseException {
49 if(resource.equals(graph.getRootLibrary())) {
50 nodes.add(new Node("", "http:/", resource.getResourceId()));
53 Layer0 L0 = Layer0.getInstance(graph);
54 Resource parent = graph.getPossibleObject(resource, L0.PartOf);
56 collectBreadcrumbs(graph, nodes, parent);
57 String name = graph.getPossibleRelatedValue(resource, L0.HasName, Bindings.STRING);
59 nodes.add(new Node("/", name, resource.getResourceId()));
61 nodes.add(new Node("/", String.valueOf(resource.getResourceId()), resource.getResourceId()));
64 Statement propertyInverseStatement = graph.getPossibleStatement(resource, L0.PropertyOf);
65 if(propertyInverseStatement != null) {
66 collectBreadcrumbs(graph, nodes, propertyInverseStatement.getObject());
67 String name = graph.getPossibleRelatedValue(graph.getInverse(propertyInverseStatement.getPredicate()),
68 L0.HasName, Bindings.STRING);
69 nodes.add(new Node("#", name, resource.getResourceId()));
72 Resource assertion = graph.getPossibleObject(resource, L0.HasObjectInverse);
73 if(assertion != null) {
74 parent = graph.getPossibleObject(assertion, L0.Asserts_Inverse);
76 collectBreadcrumbs(graph, nodes, parent);
77 Resource predicate = graph.getPossibleObject(assertion, L0.HasPredicate);
78 String name = predicate == null ? null
79 : (String)graph.getPossibleRelatedValue(predicate, L0.HasName, Bindings.STRING);
81 nodes.add(new Node("#assertion#", name, resource.getResourceId()));
83 nodes.add(new Node("#assertion#", String.valueOf(resource.getResourceId()), resource.getResourceId()));
90 public double getPriority() {