[x3d-public] X3D Java SAI. Declarative style

Don Brutzman brutzman at nps.edu
Sat Jan 21 16:43:37 PST 2017

1. Am trying out new mega-acronym:

	X3D Java Scene Access Interface Library (X3DJSAIL)


2. Great feedback from you and Joe, very helpful, thank you.  Added some capabilities, details follow:

On 1/19/2017 8:32 AM, John Carlson wrote:
> While I appreciate the style, I think it's harder to debug and edit from a human point of view.   I am not compromising to make it easier for the java compiler.   I like to make it easier for humans.

Couldn't agree more!  8)

I think this takes a little getting used to.  Multiple benefits: concise, no side effects, avoiding need to declare each and every object, automatically parallelizable at run time.  The book explains these well.

> What this style lacks is tag names, which are vital for debugging.  That is, what type do I need to add this method to if the compiler fails.  Since the SAI can support both styles, I suggest we keep yours as an optimization in some serializer somewhere.   Let's discuss.   In other words, having distinct variable names gives you a handle to work with debugging.

Very good.  I've added some helpful methods:


* Demonstrated declarative programming style in HelloWorldProgram.java method createDeclarativeShapeTests(),
   supported by utility methods findNodeByDEF(String DEFvalue) and findElementByNameValue(String nameValue).

Example snippet, updated:

   ShapeObject declarativeShape = new ShapeObject("DeclarativeShapeExample")
	.setAppearance(new AppearanceObject()
		.setMaterial(new MaterialObject(innerMaterialNodeDEF)
	.setGeometry(new ConeObject()
	.setMetadata(new MetadataStringObject("FindableMetadataStringTest")
		.setName(innerMetadataStringName).setValue("test case"));
   X3DConcreteElement nodeFoundByDEF = x3dModel.findNodeByDEF(innerMaterialNodeDEF);
   if  (nodeFoundByDEF != null)
	 createDeclarativeShapeTestResults.addComments("Test success: x3dModel.findNodeByDEF(" + innerMaterialNodeDEF + ") = " +
   else createDeclarativeShapeTestResults.addComments("Test failure: x3dModel.findNodeByDEF(" + innerMaterialNodeDEF + ") = null");

resulting in

<Shape DEF='DeclarativeShapeExample'>
	<MetadataString DEF='FindableMetadataStringTest' name='findThisNameValue' value='"test case"'/>
		<Material DEF='DeclarativeMaterialExample' diffuseColor='0.1254902 0.69803923 0.6666667'/>
	<Cone bottom='false' bottomRadius='0.05' height='0.1'/>
<!-- Test success: x3dModel.findNodeByDEF(DeclarativeMaterialExample) = <Material DEF='DeclarativeMaterialExample' diffuseColor='0.1254902 0.69803923 0.6666667'/> -->
<!-- Test success: x3dModel.findElementByNameValue(findThisNameValue) = <MetadataString DEF='FindableMetadataStringTest' name='findThisNameValue' value='"test case"'/> -->

and similarly for .wrl .x3dv .json etc.


These Java methods let you find nodes programmatically.  Constraint: current design returns first successful result, which is probably a good thing (let validation handle validation alerts).

> If the was a programming language which supported both styles, it would be ideal.

Done.  The example program is mostly in the old style.  So a Java programmer can easily use (and mix) each style.

> Also, some debuggers allow you to select expressions to look at results of.  Variables are easier to pick off a list, but the list can get a bit big.   Reducing code size should be a priority for me.

Agreed, I think this works.

> Don, can the Java object model self-print?  Then we could have our cake and eat it too.   Maybe that's what you are angling at?

Yes.  The Netbeans debugger is very powerful and seems to work well.  Also the toStringX3D() methods can be used at any level of the scene graph (witness the test comments above).

> Maybe have a Java package per type of serialization?

In java parlance, we have invocable methods for each type of serialization.  Since recursive, they too can be used at any level of the scene graph.

On 1/19/2017 9:29 AM, Joe D Williams wrote:>
> So that it is the current data structures being advanced move toward data storage in forms convient for the back end rather than human readability or functional groupings ...  more toward machine convenience, to me, kind of like the direction of vulcan and xflow definitions. This gives certain advantages of reusability, perhaps, but usually means that a basic human-readable form is unlikely or needs a highly level editing tool.

Maybe... code is code.  This is designed for Java programmers.

DesignGoals: make it easy to create and use and produce valid X3D in Java, make it difficult to create an error.  Not a goal: rendering, that is handled elsewhere.


> On Jan 19, 2017 9:19 AM, "Don Brutzman" <brutzman at nps.edu <mailto:brutzman at nps.edu>> wrote:
>     I read a very interesting book on the flight to Korea.  This cleared up several mysteries for me... highly recommended.
>     *Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions, Venkat Subramaniam, Pragmatic Bookshelf, Dallas Texas, 2014.
>       https://pragprog.com/book/vsjava8/functional-programming-in-java <https://pragprog.com/book/vsjava8/functional-programming-in-java>
>     John, here is a new style of coding that mostly eliminates the need for declaring each and every object.
>     Benefits include more concise, no side effects, no need to keep track of names for object instances, automatically parallelizable by compiler.  Syntax looks a lot like node.js style.
>     I picked a scene that shows ProtoDeclare, Script, fields, IS/connect and CDATA source code to get a good workout.  The only object member is "scene", everything else is local.
>     New code block in HelloWorldProgram.java:
>     =============================================


>     =============================================
>     Pretty darn cool.  Some more work to follow on my part to tighten up need to repeat addChildren calls for every individual child and source text, but this is a big step.
>     When ready I recommend you shift to declarative style to avoid need to generate individual objects with unique suffixes.

all the best, Don
Don Brutzman  Naval Postgraduate School, Code USW/Br       brutzman at nps.edu
Watkins 270,  MOVES Institute, Monterey CA 93943-5000 USA   +1.831.656.2149
X3D graphics, virtual worlds, navy robotics http://faculty.nps.edu/brutzman

More information about the x3d-public mailing list