From b6ff25be58dc033a5bf3b03ca1b012ace7da48b8 Mon Sep 17 00:00:00 2001 From: lempinen Date: Fri, 12 Jul 2013 13:57:06 +0000 Subject: [PATCH] Simupedia! (refs #3511) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@27706 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../handlers/exports/ExportModelHandler.java | 13 ++++ org.simantics.sysdyn/scl/Sysdyn.scl | 16 +++++ .../sysdyn/utils/DocumentationUtils.java | 68 +++++++++++++++++++ .../sysdyn/utils/imports/ImportUtils.java | 13 +++- 4 files changed, 108 insertions(+), 2 deletions(-) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportModelHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportModelHandler.java index e82df858..8d2ec6d2 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportModelHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportModelHandler.java @@ -12,6 +12,7 @@ package org.simantics.sysdyn.ui.handlers.exports; import java.io.File; +import java.util.HashMap; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; @@ -28,6 +29,7 @@ import org.simantics.db.Resource; import org.simantics.db.common.primitiverequest.PossibleRelatedValue; import org.simantics.db.common.request.ReadRequest; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.adapter.SubgraphExtent.ExtentStatus; import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest; import org.simantics.db.layer0.util.TransferableGraphConfiguration2; import org.simantics.db.request.Read; @@ -100,7 +102,18 @@ public class ExportModelHandler extends AbstractHandler { @Override public void run(ReadGraph graph) throws DatabaseException { + HashMap map = new HashMap(); + + Resource relation = graph.getPossibleResource("http://www.simantics.org/Documentation-1.1/createdBy"); + if(relation != null) { + Resource createdBy = graph.getPossibleObject(model, relation); + if(createdBy != null) + map.put(createdBy, ExtentStatus.EXCLUDED); + } + TransferableGraphConfiguration2 conf = new TransferableGraphConfiguration2(graph, model); + conf.preStatus.putAll(map); + TransferableGraphSource s = graph.syncRequest(new ModelTransferableGraphSourceRequest(conf)); try { TransferableGraphs.writeTransferableGraph(graph, "sysdynModel", 1, s,new File(selected)); diff --git a/org.simantics.sysdyn/scl/Sysdyn.scl b/org.simantics.sysdyn/scl/Sysdyn.scl index 72217d49..d0c69402 100644 --- a/org.simantics.sysdyn/scl/Sysdyn.scl +++ b/org.simantics.sysdyn/scl/Sysdyn.scl @@ -12,6 +12,15 @@ importJava "org.simantics.sysdyn.utils.DocumentationUtils" where @JavaName getRoleVariables getRoleVariables :: Variable -> [Variable] + @JavaName getDocumentVariables + getDocumentVariables :: Variable -> [Variable] + + @JavaName getIndexDocuments + getIndexDocuments :: Variable -> [Variable] + + @JavaName getResultVariables + getResultVariables :: Variable -> [Variable] + @JavaName getConfiguration getConfiguration :: Resource -> Resource @@ -67,5 +76,12 @@ importJava "org.simantics.sysdyn.utils.DocumentationUtils" where @JavaName numberOfModuleTypes numberOfModuleTypes :: Variable -> Integer + + @JavaName base64Encode + base64Encode :: Variable -> String -> String + + @JavaName formatDate + formatDate :: Long -> String + \ No newline at end of file diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/DocumentationUtils.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/DocumentationUtils.java index 5d468db0..17c5aa35 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/DocumentationUtils.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/DocumentationUtils.java @@ -1,10 +1,14 @@ package org.simantics.sysdyn.utils; +import java.text.DateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; +import java.util.Date; import java.util.List; +import org.simantics.databoard.Bindings; +import org.simantics.databoard.util.Base64; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.request.ObjectsWithSupertype; @@ -101,6 +105,57 @@ public class DocumentationUtils { return result; } + public static List getDocumentVariables(ReadGraph graph, org.simantics.db.layer0.variable.Variable model) throws DatabaseException { + if(model == null) + return Collections.emptyList(); + + Resource documentType = graph.getPossibleResource("http://www.simantics.org/Documentation-1.1/Document"); + if(documentType == null) + return Collections.emptyList(); + + ArrayList result = new ArrayList(); + for(org.simantics.db.layer0.variable.Variable part : model.getChildren(graph)) { + if(graph.isInstanceOf(part.getRepresents(graph), documentType)) { + result.add(part); + } + } + + return result; + } + + public static List getResultVariables(ReadGraph graph, org.simantics.db.layer0.variable.Variable model) throws DatabaseException { + if(model == null) + return Collections.emptyList(); + + org.simantics.db.layer0.variable.Variable library = model.getPossibleChild(graph, "PDFLibrary"); + if(library == null) + return Collections.emptyList(); + + + return new ArrayList(library.getChildren(graph)); + } + + public static List getIndexDocuments(ReadGraph graph, org.simantics.db.layer0.variable.Variable project) throws DatabaseException { + Resource indexRelation = graph.getPossibleResource("http://www.simantics.org/Documentation-1.1/indexDocument"); + + if(indexRelation == null) + return Collections.emptyList(); + + ArrayList result = new ArrayList(); + for(org.simantics.db.layer0.variable.Variable model : project.getChildren(graph)) { + Resource modelResource = model.getRepresents(graph); + if(modelResource == null) + continue; + + Resource indexDocumentResource = graph.getPossibleObject(modelResource, indexRelation); + if(indexDocumentResource != null) { + org.simantics.db.layer0.variable.Variable indexDoc = Variables.getVariable(graph, indexDocumentResource); + result.add(indexDoc); + } + } + return result; + } + public static Resource getConfiguration(ReadGraph graph, Resource module) throws DatabaseException { if(module == null) return null; @@ -391,4 +446,17 @@ public class DocumentationUtils { .size(); } + public static String base64Encode(ReadGraph graph, org.simantics.db.layer0.variable.Variable variable, String property) throws DatabaseException { + byte[] fileBArray = variable.getPossiblePropertyValue(graph, property, Bindings.BYTE_ARRAY); + if(fileBArray != null) + return Base64.encode(fileBArray); + else + return ""; + } + + public static String formatDate(long timeMillis) { + Date date = new Date(timeMillis); + String result = DateFormat.getDateInstance().format(date); + return result; + } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/imports/ImportUtils.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/imports/ImportUtils.java index a9f6d1df..2f2632e8 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/imports/ImportUtils.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/imports/ImportUtils.java @@ -57,10 +57,11 @@ import org.simantics.layer0.Layer0; import org.simantics.layer0.utils.direct.GraphUtils; import org.simantics.modeling.utils.BatchValidations; import org.simantics.operation.Layer0X; +import org.simantics.scl.runtime.function.Function1; import org.simantics.simulation.ontology.SimulationResource; import org.simantics.structural.stubs.StructuralResource2; -import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.Activator; +import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.utils.ModelUtils; /** @@ -135,6 +136,9 @@ public class ImportUtils { /* ******* IMPORT MODEL ********* */ /* ********************************* */ public static IStatus importModelFile(String path, final IProgressMonitor monitor) { + return importModelFile(path, monitor, null); + } + public static IStatus importModelFile(String path, final IProgressMonitor monitor, Function1 callback) { final Resource project = Simantics.getProject().get(); if(project == null) return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import model: project not found", null); @@ -245,6 +249,12 @@ public class ImportUtils { worked(monitor, 1); + if(callback != null) { + WriteRequest userRequest = callback.apply(result); + if(userRequest != null) + Simantics.sync(userRequest); + } + } catch (DatabaseException e) { e.printStackTrace(); return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import model failed: Model could not be imported.", e); @@ -256,7 +266,6 @@ public class ImportUtils { protected static void addSCLMain(WriteGraph graph, Resource modelRoot) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); - SysdynResource SR = SysdynResource.getInstance(graph); Collection SCLModules = graph.syncRequest(new ObjectsWithType(modelRoot, L0.ConsistsOf, L0.SCLModule)); -- 2.47.1