X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.graph%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Frefactoring%2FGraphRefactoringUtils.java;h=90b134e33762b5f991baa5288a2974906f2bad7b;hp=55b9cbe2efb4164a4091b36595217037b8b539d5;hb=1cc487c3f6cf4b46b1fdd727183a9483e3bc05bb;hpb=0ae2b770234dfc3cbb18bd38f324125cf0faca07 diff --git a/bundles/org.simantics.graph/src/org/simantics/graph/refactoring/GraphRefactoringUtils.java b/bundles/org.simantics.graph/src/org/simantics/graph/refactoring/GraphRefactoringUtils.java index 55b9cbe2e..90b134e33 100644 --- a/bundles/org.simantics.graph/src/org/simantics/graph/refactoring/GraphRefactoringUtils.java +++ b/bundles/org.simantics.graph/src/org/simantics/graph/refactoring/GraphRefactoringUtils.java @@ -1,7 +1,8 @@ package org.simantics.graph.refactoring; -import java.util.ArrayList; +import java.util.Arrays; +import org.simantics.databoard.util.URIStringUtils; import org.simantics.graph.query.Path; import org.simantics.graph.query.PathChild; import org.simantics.graph.query.TransferableGraphConversion; @@ -9,21 +10,19 @@ import org.simantics.graph.query.UriUtils; import org.simantics.graph.refactoring.MappingSpecification.MappingRule; import org.simantics.graph.representation.External; import org.simantics.graph.representation.Identity; -import org.simantics.graph.representation.IdentityDefinition; import org.simantics.graph.representation.Internal; -import org.simantics.graph.representation.Optional; import org.simantics.graph.representation.Root; import org.simantics.graph.representation.TransferableGraph1; import org.simantics.graph.representation.TransferableGraphUtils; -import org.simantics.graph.representation.old.OldTransferableGraph1; -import org.simantics.graph.representation.old.OldValue1; +import org.simantics.graph.store.GraphStore; import org.simantics.graph.store.IdentityStore; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -import gnu.trove.list.array.TIntArrayList; import gnu.trove.set.hash.TIntHashSet; public class GraphRefactoringUtils { - + private static final Logger LOGGER = LoggerFactory.getLogger(GraphRefactoringUtils.class); /** * Moves an external resource. Returns true if did something. * @param parentsAffected @@ -44,7 +43,7 @@ public class GraphRefactoringUtils { // Find parent id int toParentId = ids.createPathToId(to.parent); if(ids.hasChild(toParentId, to.name)) { - System.err.println("refactor statements from " + from + " to " + to); + LOGGER.info("refactor statements from " + from + " to " + to); //throw new GraphRefactoringException("External reference to " + to + " already exists."); int toId = ids.pathToId(to); int[] statements = tg.statements; @@ -64,7 +63,7 @@ public class GraphRefactoringUtils { if(!(rule.to instanceof PathChild)) throw new GraphRefactoringException("Invalid target URI " + rule.to); if(!moveExternal(tg, ids, rule.from, (PathChild)rule.to, parentsAffected)) - System.err.println("Didn't find " + rule.from); + LOGGER.warn("Didn't find " + rule.from); } } @@ -89,6 +88,20 @@ public class GraphRefactoringUtils { } + private static Identity recursePath(TransferableGraph1 tg, String path) { + + Identity extId = TransferableGraphUtils.findExternal(tg, path); + if(extId != null) return extId; + if("http://".equals(path)) return TransferableGraphUtils.findRootWithName(tg, ""); + String[] parts = URIStringUtils.splitURI(path); + Identity parentId = recursePath(tg, parts[0]); + tg.identities = Arrays.copyOf(tg.identities, tg.identities.length+1); + Identity childIdentity = new Identity(tg.resourceCount++, new External(parentId.resource, parts[1])); + tg.identities[tg.identities.length-1] = childIdentity; + return childIdentity; + + } + public static void fixOntologyRoot(TransferableGraph1 tg, boolean tryToFix) { Identity[] ids = tg.identities; @@ -97,37 +110,52 @@ public class GraphRefactoringUtils { if(id.definition instanceof Root) { Root ext = (Root)id.definition; if(ext.name.startsWith("http://")) { - String rootName = ext.name.substring(ext.name.lastIndexOf("/")+1); - String path = ext.name.substring(0, ext.name.lastIndexOf("/")); - Identity pathId = TransferableGraphUtils.findExternal(tg, path); - System.err.println("GraphRefactoringUtils.rootName=" + rootName); - System.err.println("GraphRefactoringUtils.path2=" + path); - if(pathId == null) { - if(!tryToFix) return; - IdentityStore idStore = TransferableGraphConversion.extractIdentities(tg); - idStore.createPathToId(UriUtils.uriToPath(path)); - tg.resourceCount = idStore.getResourceCount(); - tg.identities = idStore.toArray(); - fixOntologyRoot(tg, false); - return; - } else { - id.definition = new Internal(pathId.resource, rootName); - TIntArrayList stms = new TIntArrayList(tg.statements); - Identity consistsOf = TransferableGraphUtils.findExternal(tg, "http://www.simantics.org/Layer0-1.1/ConsistsOf"); - Identity partOf = TransferableGraphUtils.findExternal(tg, "http://www.simantics.org/Layer0-1.1/PartOf"); - stms.add(id.resource); - stms.add(partOf.resource); - stms.add(consistsOf.resource); - stms.add(pathId.resource); - tg.statements = stms.toArray(); - return; - } + String[] parts = URIStringUtils.splitURI(ext.name); + Identity path = recursePath(tg, parts[0]); + id.definition = new Internal(path.resource, parts[1]); + + GraphStore store = TransferableGraphConversion.convert(tg); + int rootId = store.identities.createPathToId(UriUtils.uriToPath(ext.name)); + propagateNewMarks(store.identities, rootId); + + TransferableGraph1 tgNew = TransferableGraphConversion.convert(store); + + tg.resourceCount = tgNew.resourceCount; + tg.identities = tgNew.identities; + tg.values = tgNew.values; + tg.statements = tgNew.statements; + + return; + + } else if (ext.type.startsWith("http://")) { + String first = "http://Projects/Development Project"; + Identity path = recursePath(tg, first); + id.definition = new Internal(path.resource, ext.name); + + GraphStore store = TransferableGraphConversion.convert(tg); + int rootId = store.identities.createPathToId(UriUtils.uriToPath(first + "/" + ext.name)); + propagateNewMarks(store.identities, rootId); + + TransferableGraph1 tgNew = TransferableGraphConversion.convert(store); + + tg.resourceCount = tgNew.resourceCount; + tg.identities = tgNew.identities; + tg.values = tgNew.values; + tg.statements = tgNew.statements; + } } } } + + private static void propagateNewMarks(IdentityStore identities, int resource) { + if(identities.markNew(resource)) { + for(int child : identities.getChildren(resource)) + propagateNewMarks(identities, child); + } + } public static void unfixIncorrectRoot(Identity[] ids) { for(int i=0;i newIdentities = new ArrayList(tg.identities.length); - for(Identity id : tg.identities) { - if(removed.contains(id.resource)) - continue; - else - newIdentities.add(id); - id.resource = map[id.resource]; - IdentityDefinition def = id.definition; - if(def instanceof External) { - External d = (External)def; - d.parent = map[d.parent]; - } - else if(def instanceof Internal) { - External d = (External)def; - d.parent = map[d.parent]; - } - else if(def instanceof Optional) { - External d = (External)def; - d.parent = map[d.parent]; - } - } - tg.identities = newIdentities.toArray(new Identity[newIdentities.size()]); - int[] statements = tg.statements; - for(int i=0;i= 0) - statements[i] = map[r]; - } - for(OldValue1 value : tg.values) - value.resource = map[value.resource]; - tg.resourceCount = resourceCount; - } - } - }