1 /*******************************************************************************
2 * Copyright (c) 2007, 2019 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 * VTT Technical Research Centre of Finland - initial API and implementation
11 * Semantum Oy - Reorganization
12 *******************************************************************************/
13 package org.simantics.issues.common;
15 import java.util.Collection;
16 import java.util.Collections;
19 import org.simantics.Simantics;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.common.request.BinaryRead;
23 import org.simantics.db.common.request.ObjectsWithType;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.adapter.Instances;
26 import org.simantics.db.layer0.variable.Variable;
27 import org.simantics.db.layer0.variable.Variables;
28 import org.simantics.issues.common.preferences.IssuePrefs;
29 import org.simantics.issues.ontology.IssueResource;
30 import org.simantics.layer0.Layer0;
31 import org.simantics.operation.Layer0X;
32 import org.simantics.simulation.ontology.SimulationResource;
34 import gnu.trove.map.hash.TObjectByteHashMap;
35 import gnu.trove.set.hash.THashSet;
38 * @author Tuukka Lehtonen
40 public class ModelVisibleIssues extends BinaryRead<Resource, Boolean, Set<Variable>> {
42 public ModelVisibleIssues(Resource model) {
43 super(model, Boolean.FALSE);
46 public ModelVisibleIssues(Resource model, boolean onlyUnresolved) {
47 super(model, onlyUnresolved);
51 public Set<Variable> perform(ReadGraph graph) throws DatabaseException {
53 //System.err.println("ModelVisibleIssues for " + graph.getURI(parameter));
55 Layer0 L0 = Layer0.getInstance(graph);
56 IssueResource ISSUE = IssueResource.getInstance(graph);
58 Resource project = Simantics.getProjectResource();
59 boolean showHidden = false;
60 boolean showNormal = true;
61 boolean showUser = true;
62 if (project != null) {
63 showHidden = IssuePrefs.showHiddenIssues(graph, project);
64 showNormal = IssuePrefs.showNormalIssues(graph, project);
65 showUser = IssuePrefs.showUserIssues(graph, project);
68 Instances issueIndex = graph.getPossibleAdapter(ISSUE.Issue, Instances.class);
70 // Cache for source activeness.
71 // 0 == not in cache, 1 == false, 2 == true
72 TObjectByteHashMap<Resource> sourceActivenessCache = new TObjectByteHashMap<Resource>();
74 Set<Variable> result = new THashSet<>(1013);
76 Collection<Resource> modelIssues = graph.syncRequest(new ObjectsWithType(parameter, L0.ConsistsOf, ISSUE.Issue));
77 Collection<Resource> indexedIssues = issueIndex != null ? issueIndex.find(graph, parameter) : Collections.<Resource>emptyList();
78 Collection<Resource> issues = !indexedIssues.isEmpty() ? new THashSet<Resource>(modelIssues.size() + indexedIssues.size()) : modelIssues;
79 if (!indexedIssues.isEmpty()) {
80 issues.addAll(modelIssues);
81 issues.addAll(indexedIssues);
84 for (Resource issue : issues) {
85 // Filter out unwanted material
86 boolean resolved = graph.hasStatement(issue, ISSUE.Resolved);
87 if (parameter2 && resolved)
89 boolean hidden = graph.hasStatement(issue, ISSUE.Hidden);
90 boolean user = graph.hasStatement(issue, ISSUE.UserIssue);
91 boolean normal = !hidden && !user;
92 if (!showHidden && hidden)
94 if (!showUser && user)
96 if (!showNormal && normal)
99 Resource source = graph.getPossibleObject(issue, ISSUE.IssueSource_Manages_Inverse);
100 if (source != null) {
101 byte cache = sourceActivenessCache.get(source);
102 boolean active = cache == 2 ? true : false;
104 active = Boolean.TRUE.equals(graph.getPossibleRelatedValue(source, ISSUE.IssueSource_active));
105 sourceActivenessCache.put(source, active ? (byte) 2 : (byte) 1);
111 Variable var = Variables.getPossibleVariable(graph, issue);
116 // System.out.println("ModelVisibleIssues returned " + result.size());