Search in sources :

Example 6 with MVolatile

use of gov.sandia.n2a.db.MVolatile in project n2a by frothga.

the class SafeTextTransferHandler method importData.

public boolean importData(TransferSupport support) {
    try {
        String data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
        if (data.startsWith("N2A.schema")) {
            if (safeTypes == null)
                return false;
            Schema schema = new Schema();
            MNode nodes = new MVolatile();
            BufferedReader br = new BufferedReader(new StringReader(data));
            schema.read(br);
            if (schema.type.startsWith("Clip"))
                schema.type = schema.type.substring(4);
            if (!safeTypes.contains(schema.type)) {
                br.close();
                return false;
            }
            nodes.read(br);
            br.close();
            // Process into a suitable string
            for (MNode n : nodes) {
                String key = n.key();
                String value = n.get();
                if (key.startsWith("@"))
                    data = value + key;
                else if (value.isEmpty())
                    data = key;
                else
                    data = key + "=" + value;
                // Only process the first node
                break;
            }
        }
        JTextComponent comp = (JTextComponent) support.getComponent();
        InputContext ic = comp.getInputContext();
        if (ic != null)
            ic.endComposition();
        comp.replaceSelection(data);
        return true;
    } catch (UnsupportedFlavorException | IOException e) {
    }
    return false;
}
Also used : Schema(gov.sandia.n2a.db.Schema) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) InputContext(java.awt.im.InputContext) JTextComponent(javax.swing.text.JTextComponent) IOException(java.io.IOException) MNode(gov.sandia.n2a.db.MNode) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) MVolatile(gov.sandia.n2a.db.MVolatile)

Example 7 with MVolatile

use of gov.sandia.n2a.db.MVolatile in project n2a by frothga.

the class ImportJob method postprocess.

/**
 *        Perform data manipulations that must wait until all nodes are read.
 */
public void postprocess() {
    for (Entry<String, ComponentType> e : components.entrySet()) e.getValue().finish1();
    for (Entry<String, ComponentType> e : components.entrySet()) e.getValue().finish2();
    for (Entry<String, Cell> e : cells.entrySet()) e.getValue().finish();
    for (Entry<String, Network> e : networks.entrySet()) e.getValue().finish1();
    for (Entry<String, Network> e : networks.entrySet()) e.getValue().finish2();
    // (Obscure case) Find spike sources that reference a target synapse but did not get used.
    // Need to construct fused parts so they are functional on their own.
    List<String> kill = new ArrayList<String>();
    for (MNode spikeSource : models.child(modelName)) {
        String synapseName = spikeSource.get("$metadata", "backend.lems.synapse");
        // not a spike source
        if (synapseName.isEmpty())
            continue;
        // this memo is no longer needed
        spikeSource.clear("$metadata", "backend.lems.synapse");
        // It got used somewhere, so we're done.
        if (spikeSource.getInt("$count") > 0)
            continue;
        MNode synapse = models.child(modelName, synapseName);
        if (synapse == null)
            continue;
        MNode fused = new MVolatile();
        // Leave the original synapse part alone. Just duplicate it.
        fused.merge(synapse);
        fused.set("$metadata", "backend.lems.id", synapseName);
        MNode A = fused.set("A", "");
        A.merge(spikeSource);
        A.set("$metadata", "backend.lems.id", spikeSource.key());
        removeDependency(spikeSource, spikeSource.get("$inherit").replace("\"", ""));
        spikeSource.clear();
        spikeSource.merge(fused);
        addDependency(spikeSource, spikeSource.get("$inherit").replace("\"", ""));
        A = spikeSource.child("A");
        // Don't do "FromConnection", because we've already injected the part.
        addDependency(A, A.get("$inherit").replace("\"", ""));
        if (synapse.getInt("$count") == 0 && synapse.child("$lems") == null)
            kill.add(synapseName);
    }
    for (String k : kill) models.clear(modelName, k);
    // Select the prime model
    if (primaryModel.isEmpty()) {
        if (networks.size() == 1)
            primaryModel = networks.entrySet().iterator().next().getValue().id;
    // Otherwise there is no prime model. Could pick one arbitrarily (with preference for higher-level parts).
    // It may be cleaner to keep them all bundled as subparts and let the user pull them out as needed.
    }
    // System.out.println (models);  // Most useful for debugging structure.
    while (dependents.size() > 0) resolve(dependents.iterator().next());
    // Move heavy-weight parts into separate models
    Iterator<MNode> it = models.child(modelName).iterator();
    while (it.hasNext()) {
        MNode part = it.next();
        String key = part.key();
        int count = part.getInt("$count");
        boolean lems = part.child("$lems") != null;
        boolean proxyFound = part.get("$proxy").equals("found");
        part.clear("$count");
        part.clear("$connected");
        part.clear("$lems");
        part.clear("$lemsUses");
        part.clear("$proxy");
        if (count == 0 && lems && !key.equals(primaryModel))
            count = -3;
        if (count < 0) {
            if (count == -3 && !proxyFound) {
                MNode model = models.childOrCreate(modelName + " " + key);
                model.merge(part);
            }
            if (count > -4)
                it.remove();
        }
    }
    // Move primary model up to top level
    if (!primaryModel.isEmpty()) {
        MNode source = models.child(modelName, primaryModel);
        if (source == null)
            return;
        models.clear(modelName, primaryModel);
        for (MNode p : source) {
            MNode dest = models.childOrCreate(modelName, p.key());
            dest.merge(p);
        }
        models.set(modelName, "$metadata", "backend.lems.id", primaryModel);
    }
    if (models.child(modelName).size() == 0)
        models.clear(modelName);
    ExpressionParser.namedUnits = null;
}
Also used : ArrayList(java.util.ArrayList) MNode(gov.sandia.n2a.db.MNode) MVolatile(gov.sandia.n2a.db.MVolatile)

Aggregations

MVolatile (gov.sandia.n2a.db.MVolatile)7 MNode (gov.sandia.n2a.db.MNode)5 BufferedReader (java.io.BufferedReader)2 IOException (java.io.IOException)2 Schema (gov.sandia.n2a.db.Schema)1 Undoable (gov.sandia.n2a.ui.Undoable)1 FilteredTreeModel (gov.sandia.n2a.ui.eq.FilteredTreeModel)1 AddAnnotation (gov.sandia.n2a.ui.eq.undo.AddAnnotation)1 AddDoc (gov.sandia.n2a.ui.eq.undo.AddDoc)1 AddInherit (gov.sandia.n2a.ui.eq.undo.AddInherit)1 AddPart (gov.sandia.n2a.ui.eq.undo.AddPart)1 AddReference (gov.sandia.n2a.ui.eq.undo.AddReference)1 AddVariable (gov.sandia.n2a.ui.eq.undo.AddVariable)1 ChangeInherit (gov.sandia.n2a.ui.eq.undo.ChangeInherit)1 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)1 InputContext (java.awt.im.InputContext)1 FileReader (java.io.FileReader)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 JTextComponent (javax.swing.text.JTextComponent)1