- protected Identity getRootIdentity(DomainProcessorState state, SerialisationSupport support, Resource rootLibrary) throws DatabaseException {
- return new Identity(state.ids.get(support.getTransientId(rootLibrary)), new External(-1, ""));
- }
-
- @Override
- public void forIdentities(ReadGraph graph, TransferableGraphSourceProcedure<Identity> procedure) throws Exception {
-
- SerialisationSupport support = graph.getService(SerialisationSupport.class);
- Layer0 L0 = Layer0.getInstance(graph);
-
- // TODO: this should be Root with name ""
- procedure.execute(getRootIdentity(state, support, graph.getRootLibrary()));
-
- TIntObjectMap<Identity> internalMap = new TIntObjectHashMap<>(100, 0.5f, Integer.MIN_VALUE);
-
- // Declare internal and external roots
- for(SeedSpec r : configuration.seeds) {
- if(SeedSpecType.INTERNAL.equals(r.specType)) continue;
- String typeId = r.type;
- if (typeId == null) {
- Resource type = graph.getPossibleType(r.resource, L0.Entity);
- typeId = type != null ? graph.getURI(type) : Layer0.URIs.Entity;
- }
- int id = state.ids.get(support.getTransientId(r.resource));
- Root root = new Root(r.name, typeId);
- Identity rootId = new Identity(id,root);
- internalMap.put(id, rootId);
- procedure.execute(rootId);
- }
-
- for(int i = 0; i < state.externals.size() ; i++) {
- int parent = externalParents.get(i);
- String name = externalNames.get(i);
- procedure.execute(new Identity(externalBase + i, new External(parent,name)));
- }
-
- if(state.internalEntries != null) {
- for(ConsistsOfProcessEntry ie : state.internalEntries) {
- if(ie.parent != null) {
- if(ie.name != null) {
- procedure.execute(resolveInternal(graph, support, ie, internalMap));
- } else {
- // In this case there is a child that has no HasName => this should be treated as a blank
- }
- } else {
- procedure.execute(resolveInternal(graph, support, ie, internalMap));
- }
- }
- }
-
- }
-
- private Identity resolveInternal(ReadGraph graph, SerialisationSupport ss, ConsistsOfProcessEntry entry, TIntObjectMap<Identity> internalMap) throws DatabaseException {
- int id = state.ids.get(ss.getTransientId(entry.resource));
- Identity existing = internalMap.get(id);
- if(existing != null) return existing;
-
- if(entry.parent == null) {
- Layer0 L0 = Layer0.getInstance(graph);
- Resource possibleParent = graph.getPossibleObject(entry.resource, L0.PartOf);
- if(possibleParent == null) throw new DatabaseException("Invalid root or internal parent path: " + entry.resource);
- int externalId = state.ids.get(ss.getTransientId(possibleParent));
- Identity result = new Identity(id,
- new Internal(externalId, entry.name));
- internalMap.put(id, result);
- return result;
- } else {
- Identity parent = resolveInternal(graph, ss, entry.parent, internalMap);
- Identity result = new Identity(id,
- new Internal(parent.resource, entry.name));
- internalMap.put(id, result);
- return result;
- }
- }
-
- @Override
- public TreeMap<String, Variant> getExtensions() {
- return state.extensions;
- }
-
- public File[] getFiles() {
- return files;
- }
-
- private static <T> T tryClose(T c) throws IOException {
- if (c != null && c instanceof Closeable)
- ((Closeable) c).close();
- return null;
- }
-
- public void closeStreams() throws IOException {
- state.valueInput = tryClose(state.valueInput);
- state.otherStatementsInput = tryClose(state.otherStatementsInput);
- state.statementsOutput = tryClose(state.statementsOutput);
- state.valueOutput = tryClose(state.valueOutput);
- }
-
- @Override
- public void reset() throws Exception {
- throw new UnsupportedOperationException();
- }
-
- public long[] getResourceArray(ReadGraph graph) throws DatabaseException {
- final SerialisationSupport ss = graph.getService(SerialisationSupport.class);
- final long[] result = new long[state.ids.size()];
- state.ids.forEachEntry(new TIntIntProcedure() {
-
- @Override
- public boolean execute(int a, int b) {
-
- try {
- Resource r = ss.getResource(a);
- result[b] = r.getResourceId();
- } catch (DatabaseException e) {
- e.printStackTrace();
- }
-
- return true;
-
- }
- });
- return result;
- }
-
- public DomainProcessorState getState() {
- return state;
- }
-
- public void forResourceStatements(ReadGraph graph, TransferableGraphSourceProcedure<int[]> procedure) throws Exception {
-
- int[] value = new int[4];
- long length = state.otherStatementsInput.length();
- state.otherStatementsInput.position(0);
-
- while(state.otherStatementsInput.position() < length) {
-
- int s = state.otherStatementsInput.readInt();
- int subjectId = state.ids.get(s);
-
- boolean exclude = subjectId == -1;
-
- int size = state.otherStatementsInput.readInt();
- for(int i=0;i<size;i++) {
- int p = state.otherStatementsInput.readInt();
- int o = state.otherStatementsInput.readInt();
- if(!exclude) {
- if(state.excludedShared.contains(o)) {
- System.err.println("excluding shared " + s + " " + p + " " + o);
- } else {
-
- int objectId = getExistingId(graph, o);
- // The statement can be denied still
- if(objectId != -2) {
- value[0] = s;
- value[1] = p;
- int inverse = state.inverses.get(p);
- if(inverse != 0) {
- value[2] = inverse;
- } else {
- value[2] = -1;
- }
- value[3] = o;
-
- procedure.execute(value);
-
- } else {
- System.err.println("Denied (" + NameUtils.getSafeName(graph, getResource(graph, s)) + ", " + NameUtils.getSafeName(graph, getResource(graph, p)) + "," + NameUtils.getSafeName(graph, getResource(graph, o)) + ")");
- }
-
- }
- } else {
- System.err.println("excluding shared " + s);
- }
- }
- }
- }
-
- public void forValueResources(ReadGraph graph, TransferableGraphSourceProcedure<int[]> procedure) throws Exception {
- int[] value = { 0 };
- long length = state.valueInput.length();
- while (state.valueInput.position() < length) {
- value[0] = state.valueInput.readInt();
- procedure.execute(value);
- }
- }
-
- public TransferableGraphConfiguration2 getConfiguration() {
- return configuration;
- }
-
- @Override
- public void close() throws IOException {
- synchronized (this) {
- if (closed)
- return;
- closed = true;
- }
- closeStreams();
- if (files != null) {
- for (File f : files) {
- Files.deleteIfExists(f.toPath());
- }
- }
- }