X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.graph%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Fstore%2FGraphStore.java;fp=bundles%2Forg.simantics.graph%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Fstore%2FGraphStore.java;h=9cd54fbeff0224702d5820140bb5479dc48fc8a9;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.graph/src/org/simantics/graph/store/GraphStore.java b/bundles/org.simantics.graph/src/org/simantics/graph/store/GraphStore.java new file mode 100644 index 000000000..9cd54fbef --- /dev/null +++ b/bundles/org.simantics.graph/src/org/simantics/graph/store/GraphStore.java @@ -0,0 +1,146 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.graph.store; + +import gnu.trove.list.array.TIntArrayList; +import gnu.trove.map.hash.THashMap; +import gnu.trove.map.hash.TIntIntHashMap; +import gnu.trove.procedure.TIntProcedure; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.concurrent.Callable; + +import org.simantics.graph.query.Path; +import org.simantics.graph.query.Res; +import org.simantics.graph.utils.GraphExecutor; + +public class GraphStore { + public StatementStore statements; + public IdentityStore identities; + public ValueStore values; + protected THashMap, IStore> stores; + + public GraphStore(StatementStore statements, IdentityStore identities, ValueStore values, THashMap, IStore> stores) { + this.statements = statements; + this.identities = identities; + this.values = values; + this.stores = stores; + } + + public GraphStore() { + this(new StatementStore(), new IdentityStore(), new ValueStore(), new THashMap, IStore>()); + } + + public GraphStore(GraphStore store) { + this(store.statements, store.identities, store.values, store.stores); + } + + public void map(final TIntIntHashMap map) { + ArrayList> tasks = new ArrayList>(); + tasks.add(new IStore.MapTask(statements, map)); + tasks.add(new IStore.MapTask(values, map)); + for(IStore store : stores.values()) + tasks.add(new IStore.MapTask(store, map)); + tasks.add(new IStore.MapTask(identities, map)); + + try { + GraphExecutor.EXECUTOR.invokeAll(tasks); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + + public void addStore(Class clazz, T store) { + stores.put(clazz, store); + } + + @SuppressWarnings("unchecked") + public T getStore(Class clazz) { + return (T)stores.get(clazz); + } + + public int resToId(Res res) { + if(res instanceof Path) + return identities.pathToId((Path)res); + else { + IdRes idRes = (IdRes)res; + if(idRes.fragment != this) + return -1; + else + return idRes.id; + } + } + + public int createResToId(Res res) { + if(res instanceof Path) + return identities.createPathToId((Path)res); + else { + IdRes idRes = (IdRes)res; + if(idRes.fragment != this) + throw new RuntimeException("Cannot crate reference to an internal resource of other graph"); + else + return idRes.id; + } + } + + public Res idToRes(int id) { + Path path = identities.idToPath(id); + if(path == null) + return new IdRes(this, id); + else + return path; + } + + public void addIdsToResult(TIntArrayList ids, final Collection result) { + ids.forEach(new TIntProcedure() { + + @Override + public boolean execute(int value) { + result.add(idToRes(value)); + return true; + } + }); + } + + public void collectReferences(boolean[] set) { + statements.collectReferences(set); + identities.collectReferences(set); + values.collectReferences(set); + } + + @Override + public String toString() { + final StringBuilder b = new StringBuilder(); + statements.forStatements(new IStatementProcedure() { + + public String name(int id) { + Path path = identities.idToPath(id); + if(path == null) + return "(" + id + ")"; + else + return path + "(" + id + ")"; + } + + @Override + public void execute(int s, int p, int o) { + b.append(name(s)); + b.append(" "); + b.append(name(p)); + b.append(" "); + b.append(name(o)); + b.append("\n"); + } + }); + return b.toString(); + } +}