[rkward-cvs] SF.net SVN: rkward: [799] trunk/rkward/rkward

tfry at users.sourceforge.net tfry at users.sourceforge.net
Tue Oct 3 15:04:22 UTC 2006


Revision: 799
          http://svn.sourceforge.net/rkward/?rev=799&view=rev
Author:   tfry
Date:     2006-10-03 08:04:07 -0700 (Tue, 03 Oct 2006)

Log Message:
-----------
Deal with objects placed outside the default package namespace. Minor additional fixes

Modified Paths:
--------------
    trunk/rkward/rkward/core/renvironmentobject.cpp
    trunk/rkward/rkward/core/renvironmentobject.h
    trunk/rkward/rkward/core/robject.cpp
    trunk/rkward/rkward/core/robject.h
    trunk/rkward/rkward/core/robjectlist.cpp
    trunk/rkward/rkward/core/robjectlist.h
    trunk/rkward/rkward/dialogs/rkloadlibsdialog.cpp
    trunk/rkward/rkward/rbackend/rpackages/rkward/R/internal.R
    trunk/rkward/rkward/rkward.cpp

Modified: trunk/rkward/rkward/core/renvironmentobject.cpp
===================================================================
--- trunk/rkward/rkward/core/renvironmentobject.cpp	2006-10-03 15:02:35 UTC (rev 798)
+++ trunk/rkward/rkward/core/renvironmentobject.cpp	2006-10-03 15:04:07 UTC (rev 799)
@@ -42,14 +42,18 @@
 	RK_TRACE (OBJECTS);
 
 	if (type & ToplevelEnv) return ("as.environment (\"" + name + "\")");
-	return (parent->makeChildName (name));
+	return parent->makeChildName (name, type & Misplaced);
 }
 
-QString REnvironmentObject::makeChildName (const QString &short_child_name) {
+QString REnvironmentObject::makeChildName (const QString &short_child_name, bool misplaced) {
 	RK_TRACE (OBJECTS);
 
 	if (type & GlobalEnv) return (short_child_name);
-	if (type & ToplevelEnv) return (namespace_name + "::" + RObject::rQuote (short_child_name));
+	if (type & ToplevelEnv) {
+/* Some items are placed outside of their native namespace. E.g. in package:boot item "motor". It can be retrieved using as.environment ("package:boot")$motor. This is extremly ugly. We need to give them (and only them) this special treatment. */
+		if (misplaced) return (getFullName () + "$" + RObject::rQuote (short_child_name));
+		return (namespace_name + "::" + RObject::rQuote (short_child_name));
+	}
 	return (name + "$" + short_child_name);
 }
 
@@ -62,10 +66,11 @@
 
 void REnvironmentObject::updateFromR () {
 	RK_TRACE (OBJECTS);
-	QString envlevel;
-	if (type & GlobalEnv) envlevel = ", -1";	// in the .GlobalEnv recurse one more level
+	QString options;
+	if (type & GlobalEnv) options = ", envleve=-1";	// in the .GlobalEnv recurse one more level
+	if (type & ToplevelEnv) options.append (", namespacename=" + rQuote (namespace_name));
 
-	RCommand *command = new RCommand (".rk.get.structure (" + getFullName () + ", " + rQuote (getShortName ()) + envlevel + ")", RCommand::App | RCommand::Sync | RCommand::GetStructuredData, QString::null, this, ROBJECT_UDPATE_STRUCTURE_COMMAND);
+	RCommand *command = new RCommand (".rk.get.structure (" + getFullName () + ", " + rQuote (getShortName ()) + options + ")", RCommand::App | RCommand::Sync | RCommand::GetStructuredData, QString::null, this, ROBJECT_UDPATE_STRUCTURE_COMMAND);
 	RKGlobals::rInterface ()->issueCommand (command, RObjectList::getObjectList ()->getUpdateCommandChain ());
 }
 

Modified: trunk/rkward/rkward/core/renvironmentobject.h
===================================================================
--- trunk/rkward/rkward/core/renvironmentobject.h	2006-10-03 15:02:35 UTC (rev 798)
+++ trunk/rkward/rkward/core/renvironmentobject.h	2006-10-03 15:04:07 UTC (rev 799)
@@ -35,34 +35,11 @@
 	void updateFromR ();
 
 	QString getFullName ();
-	QString makeChildName (const QString &short_child_name);
+	QString makeChildName (const QString &short_child_name, bool misplaced=false);
 /** reimplemented from RContainerObject: If this is an environment var, call RContainerObject::writeMetaData (). Else, do nothing. An environment has no meta data. */
 	void writeMetaData (RCommandChain *chain);
 
 	bool isGlobalEnv () { return (type & GlobalEnv); };
-/** 
-# search ()
-or rather
-# loadedNamespaces?! No, namespaces are evil! maybe rename to RKNamespaceObject? No. Let's deal with envirs for now.
-# ls (envir=as.environment ("package:base"))
-name is base::something
-
-How to deal with those names?
-You can't assign like this:
-envir::object <- something
-Probably it's best not to support assignments outside the .GlobalEnv at all.
-It's important to find out, when objects are masked, however. If they are, reading should return a full qualified name. Writing should return a simple name, to allow creation of a masking object in .GlobalEnv
-
-What should be the algorithm?
-1) Maybe we should always return the full qualified name.
-2) When editing an object in any other env, *first* (suggest to?) make a copy to the .GlobalEnv, and edit that copy
-3) Essentially objects in any other environment remain read-only
-
-4) check whether objects are masked, and warn when viewing / editing (only needed for objects outside the global env)
-
-RContainerObject::findObjectsMatching (...) for code completion popups
-RContainerObject::canonifyName
-*/
 protected:
 	friend class RObjectList;
 	friend class RContainerObject;

Modified: trunk/rkward/rkward/core/robject.cpp
===================================================================
--- trunk/rkward/rkward/core/robject.cpp	2006-10-03 15:02:35 UTC (rev 798)
+++ trunk/rkward/rkward/core/robject.cpp	2006-10-03 15:04:07 UTC (rev 799)
@@ -59,7 +59,7 @@
 
 QString RObject::getFullName () {
 	RK_TRACE (OBJECTS);
-	return parent->makeChildName (RObject::name);
+	return parent->makeChildName (RObject::name, type & Misplaced);
 }
 
 QString RObject::getLabel () {
@@ -151,7 +151,7 @@
 	return false;
 }
 
-QString RObject::makeChildName (const QString &short_child_name) {
+QString RObject::makeChildName (const QString &short_child_name, bool) {
 	RK_TRACE (OBJECTS);
 	return (getFullName () + "[[" + rQuote (short_child_name) + "]]");
 }
@@ -282,6 +282,7 @@
 
 	bool changed = false;
 	int new_type = new_data->getIntVector ()[0];
+	if (type & Misplaced) new_type |= Misplaced;
 	if (type != new_type) {
 		changed = true;
 		type = new_type;

Modified: trunk/rkward/rkward/core/robject.h
===================================================================
--- trunk/rkward/rkward/core/robject.h	2006-10-03 15:02:35 UTC (rev 798)
+++ trunk/rkward/rkward/core/robject.h	2006-10-03 15:04:07 UTC (rev 799)
@@ -55,7 +55,8 @@
 		Environment=256,
 		GlobalEnv=512,
 		ToplevelEnv=1024,
-		HasMetaObject=2048
+		HasMetaObject=2048,
+		Misplaced=4096		/** < the object is not in the namespace where it would be expected */
 	};
 
 	#define ROBJECT_TYPE_INTERNAL_MASK (RObject::Container | RObject::Variable | RObject::Workspace | RObject::Environment | RObject::Function)
@@ -171,7 +172,7 @@
 	unsigned int num_classes;
 
 /** generates a (full) name for a child of this object with the given name. */
-	virtual QString makeChildName (const QString &short_child_name);
+	virtual QString makeChildName (const QString &short_child_name, bool misplaced=false);
 
 /** Update object to reflect the structure passed in the new_data argument. If the data is mismatching (i.e. can not be accommodated by this type of object) false is returned (calls canAccommodateStructure () internally). In this case you should delete the object, and create a new one.
 @returns true if the changes could be done, false if this  */

Modified: trunk/rkward/rkward/core/robjectlist.cpp
===================================================================
--- trunk/rkward/rkward/core/robjectlist.cpp	2006-10-03 15:02:35 UTC (rev 798)
+++ trunk/rkward/rkward/core/robjectlist.cpp	2006-10-03 15:04:07 UTC (rev 799)
@@ -109,7 +109,7 @@
 	// we could as well iterate over the childmap, but this is easier
 	for (unsigned int i = 0; i < num_toplevel_environments; ++i) {
 		bool found = false;
-		for (unsigned int j = 0; i < env_count; ++j) {
+		for (unsigned int j = 0; j < env_count; ++j) {
 			if (toplevel_environments[i]->getShortName () == env_names[j]) {
 				found = true;
 				break;
@@ -120,7 +120,8 @@
 
 	// remove the environments which are gone
 	for (QValueList<REnvironmentObject *>::const_iterator it = removelist.constBegin (); it != removelist.constEnd (); ++it) {
-		removeChild (*it, true);
+		RK_DO (qDebug ("removing toplevel environment %s from list", (*it)->getShortName ().latin1 ()), OBJECTS, DL_INFO);
+		RKGlobals::tracker ()->removeObject (*it, 0, true);
 	}
 
 	// find which items are new
@@ -237,7 +238,7 @@
 QString RObjectList::renameChildCommand (RObject *object, const QString &new_name) {
 	RK_TRACE (OBJECTS);
 
-	return (makeChildName (new_name) + " <- " + object->getFullName () + "\n" + removeChildCommand (object));
+	return (makeChildName (new_name, false) + " <- " + object->getFullName () + "\n" + removeChildCommand (object));
 }
 
 QString RObjectList::removeChildCommand (RObject *object) {
@@ -267,4 +268,18 @@
 	}
 }
 
+//static
+REnvironmentObject *RObjectList::getGlobalEnv () {
+	RK_TRACE (OBJECTS);
+
+	RObjectList *list = getObjectList ();
+	RK_ASSERT (list);
+
+	REnvironmentObject *envobj = list->toplevel_environments[0];
+	RK_ASSERT (envobj);
+	RK_ASSERT (envobj->isType (RObject::GlobalEnv));
+
+	return envobj;
+}
+
 #include "robjectlist.moc"

Modified: trunk/rkward/rkward/core/robjectlist.h
===================================================================
--- trunk/rkward/rkward/core/robjectlist.h	2006-10-03 15:02:35 UTC (rev 798)
+++ trunk/rkward/rkward/core/robjectlist.h	2006-10-03 15:04:07 UTC (rev 799)
@@ -48,7 +48,7 @@
 	void updateFromR ();
 	
 	QString getFullName () { return QString::null; };
-	QString makeChildName (const QString &short_child_name) { return short_child_name; };
+	QString makeChildName (const QString &short_child_name, bool) { return short_child_name; };
 	/** reimplemented from RContainerObject: do nothing. The object-list has no meta data. */
 	void writeMetaData (RCommandChain *) {};
 	
@@ -65,6 +65,7 @@
 	KURL getWorkspaceURL () { return current_url; };
 
 	static RObjectList *getObjectList () { return object_list; };
+	static REnvironmentObject *getGlobalEnv ();
 public slots:
 	void timeout ();
 signals:

Modified: trunk/rkward/rkward/dialogs/rkloadlibsdialog.cpp
===================================================================
--- trunk/rkward/rkward/dialogs/rkloadlibsdialog.cpp	2006-10-03 15:02:35 UTC (rev 798)
+++ trunk/rkward/rkward/dialogs/rkloadlibsdialog.cpp	2006-10-03 15:04:07 UTC (rev 799)
@@ -34,6 +34,7 @@
 #include "../rbackend/rinterface.h"
 #include "../settings/rksettingsmodulegeneral.h"
 #include "../settings/rksettings.h"
+#include "../core/robjectlist.h"
 #include "../misc/rkprogresscontrol.h"
 
 #include "../debug.h"
@@ -283,7 +284,9 @@
 		RData *libpath = command->getStructureVector ()[3];
 
 		unsigned int count = package->getDataLength ();
-		RK_ASSERT (count == title->getDataLength () == version->getDataLength () == libpath->getDataLength ()); 
+		RK_ASSERT (count == title->getDataLength ());
+		RK_ASSERT (count == version->getDataLength ());
+		RK_ASSERT (count == libpath->getDataLength ());
 		for (unsigned int i=0; i < count; ++i) {
 			new QListViewItem (installed_view, package->getStringVector ()[i], title->getStringVector ()[i], version->getStringVector ()[i], libpath->getStringVector ()[i]);
 		}
@@ -393,6 +396,7 @@
 	}
 
 	control->doNonModal (true);
+	RObjectList::getObjectList ()->updateFromR ();
 }
 
 void LoadUnloadWidget::apply () {

Modified: trunk/rkward/rkward/rbackend/rpackages/rkward/R/internal.R
===================================================================
--- trunk/rkward/rkward/rbackend/rpackages/rkward/R/internal.R	2006-10-03 15:02:35 UTC (rev 798)
+++ trunk/rkward/rkward/rbackend/rpackages/rkward/R/internal.R	2006-10-03 15:04:07 UTC (rev 799)
@@ -271,7 +271,7 @@
 	invisible (TRUE)
 }
 
-".rk.get.structure" <- function (x, name, envlevel=0) {
+".rk.get.structure" <- function (x, name, envlevel=0, namespacename=NULL, misplaced=FALSE) {
 	fun <- FALSE
 	cont <- FALSE
 	type <- 0
@@ -300,6 +300,7 @@
 		cont <- TRUE
 	}
 	if (!is.null (attr (x, ".rk.meta"))) type = type + 2048
+	if (misplaced) type <- type + 4096
 	ret$type <- as.integer (type)
 
 # 3: classes
@@ -319,7 +320,7 @@
 # 6: Special info valid for some objects ony. This should always be last in the returned structure, as the number of fields may vary
 	if (cont) {		# a container
 		if (is.environment (x)) {
-			ret$sub <- .rk.get.environment.children (x, envlevel+1)
+			ret$sub <- .rk.get.environment.children (x, envlevel+1, namespacename)
 		} else {
 			nms <- names (x)
 			if (!is.null (nms)) {
@@ -340,13 +341,20 @@
 	ret
 }
 
-".rk.get.environment.children" <- function (x, envlevel=0) {
+".rk.get.environment.children" <- function (x, envlevel=0, namespacename=NULL) {
 	ret <- list ()
 
 	if (envlevel < 2) {		# prevent infinite recursion
 		lst <- ls (x, all.names=TRUE)
 		for (childname in lst) {
-			ret[[childname]] <- .rk.get.structure (get (childname, envir=x), childname, envlevel)
+			misplaced <- FALSE
+			if (!is.null (namespacename)) {
+				tst <- try ({eval (parse (text=paste (namespacename, childname, sep="::")))}, silent=TRUE)
+				if (class (tst) == "try-error") {
+					misplaced <- TRUE
+				}
+			}
+			ret[[childname]] <- .rk.get.structure (get (childname, envir=x), childname, envlevel, misplaced=misplaced)
 		}
 	}
 

Modified: trunk/rkward/rkward/rkward.cpp
===================================================================
--- trunk/rkward/rkward/rkward.cpp	2006-10-03 15:02:35 UTC (rev 798)
+++ trunk/rkward/rkward/rkward.cpp	2006-10-03 15:04:07 UTC (rev 799)
@@ -59,6 +59,7 @@
 #include "settings/rksettingsmoduleoutput.h"
 #include "rbackend/rinterface.h"
 #include "core/robjectlist.h"
+#include "core/renvironmentobject.h"
 #include "rkglobals.h"
 #include "robjectbrowser.h"
 #include "dialogs/startupdialog.h"
@@ -460,7 +461,7 @@
 	slotSetStatusBarText (i18n ("Exiting..."));
 	saveOptions ();
 
-	if (!RObjectList::getObjectList ()->isEmpty ()) {
+	if (!RObjectList::getGlobalEnv ()->isEmpty ()) {
 		int res;
 		res = KMessageBox::questionYesNoCancel (this, i18n ("Quitting RKWard: Do you want to save the workspace?\nRKWard will remain open if you press Cancel"), i18n ("Save Workspace?"));
 		if (res == KMessageBox::Yes) {


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the rkward-tracker mailing list