Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. Be respectful, on topic and if you see a problem, Flag it.

If you would like to contact our Community Manager personally, feel free to send a private message or an email.

Operator form queries

konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 1,221 ✭✭✭✭✭
edited November 2017 in FeatureScript
Would like to share my thoughts about compound query syntax. If you have to apply several query filters you getting coplex nested construction with arguments on the right side and subqueries on the left which is rather hard to read and edit like that:
qVertexAdjacent(qNthElement(qOwnedByBody(definition.testQuery,EntityType.VERTEX),1),EntityType.EDGE) 
and when you finnaly want to view a debug display of that query you need to assign it to separate variable.

And i decided to create an operator-form query syntax, which is based on operator overloading for (query*lambda) expression, lambda wrappers on built-in query functions, and wrapping built-in debug function in lambda that returns it's argument. Now you can use a simple, consistent syntax for compound query expressions like that:

<code> debug(context)*<br>           qVertexAdjacent(EntityType.EDGE) *
           debug(context);qOwnedByBody(definition.testQuery) *<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; qEntityFilter(EntityType.VERTEX) *<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; qNthElement(1) *
The evaluation order was inherited from * operator and goes from left to the right (from top to bottom for vertical formatting).
Each wrapper returns a lambda function of built-in query with a user-defined second parameter. That labmda processes the result of evaluation of prevous functions which always is of Query type. The first equation of this conveyor should allways be of Query type as well. And finally you can add specially overloaded debug operator at any stage of evaluation, and will not brake the evaluation because internally it is wrapper over built-in debug function that calls it and then returns the argument back to evaluation process.

Some examples of code that provides these capabilities:
* operator overloading
<code>lambda's by "*" operator
export operator*(query is Query, queryFunction is function)
{
return queryFunction(query);
}//Rule for query interaction with

debug function wrapping:
//debug<br>export function debug(context is Context)<br>{<br>&nbsp;&nbsp;&nbsp; return function(value)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; debug(context, value);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return value;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };<br>}
lambda-wrappers for some commonly used built-in query functions:


</code><code>//return lambda for qBodyType(subquery, BodyType)<br>export function qBodyType(bodyType is BodyType)<br>{<br> return function(subquery)<br> {<br> return qBodyType(subquery, BodyType);<br> };<br>}<br><br>//return lambda for qBodyType(subquery, array of BodyType's)<br>export function qBodyType(bodyTypes is array)<br>{<br> return function(subquery)<br> {<br> return qBodyType(subquery, bodyTypes);<br> };<br>}<br><br>//return lambda for qEntityFilter(subquery, EntityType)<br>export function qEntityFilter(entityType is EntityType)<br>{<br> return function(subquery)<br> {<br> return qEntityFilter(subquery, entityType);<br> };<br>}<br><br>//return lambda for qNthElement (subquery is Query, n is number)<br>export function qNthElement(n is number)<br>{<br> return function(subquery)<br> {<br> return qNthElement(subquery, n);<br> };<br>}<br><br>//return lambda for qAttributeFilter (subquery is Query, attributePattern) returns Query<br>export function qAttributeFilter(attributePattern)<br>{<br> return function(subquery)<br> {<br> return qAttributeFilter(subquery, attributePattern);<br> };<br>}<br><br>//return lambda for qOwnedByBody (body is Query, entityType is EntityType) returns Query<br>export function qOwnedByBody(entityType is EntityType)<br>{<br> return function(body)<br> {<br> return qOwnedByBody(body, entityType);<br> };<br>}<br><br>// qVertexAdjacent (query is Query, entityType is EntityType) returns Query<br>export function qVertexAdjacent(entityType is EntityType)<br>{<br> return function(query)<br> {<br> return qVertexAdjacent(query, entityType);<br> };<br>}<br><br>// qEdgeAdjacent (query is Query, entityType is EntityType) returns Query<br>export function qEdgeAdjacent(entityType is EntityType)<br>{<br> return function(query)<br> {<br> return qEdgeAdjacent(query, entityType);<br> };<br>}<br><br>// qConstructionFilter (subquery is Query, constructionFilter is ConstructionObject) returns Query<br>export function qConstructionFilter(constructionFilter is ConstructionObject)<br>{<br> return function(subquery)<br> {<br> return qConstructionFilter(subquery, constructionFilter);<br> };<br>}<br><br>// qSketchFilter (subquery is Query, sketchObjectFilter is SketchObject) returns Query<br>export function qSketchFilter(sketchObjectFilter is SketchObject)<br>{<br> return function(subquery)<br> {<br> return qSketchFilter(subquery, sketchObjectFilter);<br> };<br>}<br><br>// qContainsPoint (subquery is Query, point is Vector) returns Query<br>export function qContainsPoint(point is Vector)<br>{<br> return function(subquery)<br> {<br> return qContainsPoint(subquery, point);<br> };<br>}<br><br>


link to the document:
https://cad.onshape.com/documents/b2bef3c68edf40c9d2bff9cd/w/b5d571e7d6216fbb6e7b56b8/e/4e50b5b6d035682501ff1fde

Comments

  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,173
    Nice!

    Originally I resisted the urge to do this type of thing because it is a bit of an abuse of notation, but it's hard to argue with your example -- it is certainly way more readable with the overloads.  I'm going to think about putting something like that in std.

    Things I would probably do a little differently / in addition:
    1. I would add a QueryFilter type and tag the lambdas with it -- that way, you don't accidentally multiply with some arbitrary function.
    2. I would (maybe -- I'm not certain) flip the order to be consistent with function composition -- that would require an overload of multiplying two QueryFilters
    3. I would probably add + for qUnion and - for qSubtraction.  qIntersection would be left without an operator, but it is much more rare (currently 26 std occurrences vs. 63 qSubtraction and 267 qUnion).  The algebra doesn't quite work out (i.e., a + b - c is not the same as a - c + b) but it's close.
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 1,221 ✭✭✭✭✭
    edited November 2017
    Thanks @ilya_baran
    Initially I thought about making a function
    compoundQuery(subquery is Query, filterSequence is map) returns Query
    where filter sequence has a structure like {"queryFilterName1": filterCondition1,...}. but without autocomplete of filterSequence fields this is not very usefull.
    I agree with your first point. About evaluation order - it is so because i didn't ever thought that I can affect on it by overloading functions interaction, though i can find some convenience in it now.
    About other query functions that combine two or more arguments of query type I don't see much use of overloading them, because it is not a big deal to have some finishing short line of code.
    But appending array by += operator would be handy.
  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,173
    @kevin_o_toole_1 has also proposed an alternative approach: we could add
    a -> methodCall(b, c)
    as syntactic sugar for
    methodCall(a, b, c)
    And I think maybe something like
    b -> methodCall(a, ^, c)
    for the same thing (except b would be evaluated first), to make debug work.  With this, your example with no library changes would read:

    <code> debug(context, ^)-><br>           qVertexAdjacent(EntityType.EDGE)->
               debug(context, ^);qOwnedByBody(definition.testQuery)-><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; qEntityFilter(EntityType.VERTEX)-><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; qNthElement(1)->
    Which is about as readable as the original example and doesn't suffer from the weirdness of overloaded multiplication and left-to-right vs. right-to-left questions.  At the same time, the downside is that it's suggestive of an actual method defined on the class (or worse, a lambda in some languages), rather than just a function call.
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • lemon1324lemon1324 Member, Developers Posts: 223 EDU
    edited November 2017
    I'm a fan of some sort of syntactic sugar approach like this.  When @konstantin_shiriazdanov first suggested standardized notation in a response to one of my posts, my first thought was something more like the method chaining you get in JavaScript, where one defines the query functions as methods of Query objects returning Query, and then further chained method calls further filter the query (or perform a debug and return the same query).  With the same example, this would look like:

    <code> .debug(context)<br>           .qVertexAdjacent(EntityType.EDGE)
               .debug(context);qOwnedByBody(definition.testQuery)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .qEntityFilter(EntityType.VERTEX)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .qNthElement(1)
    Where context doesn't need the caret since it's operating on the current Query at that point; similarly, qUnion would take one parameter or an array, and add to the query, etc.  I prefer this notation a bit over the arrow notation, but that's just me.

    Does Onshape currently evaluate a non-null Query as true, or throw an error?  If currently an error, overloading Query & Query for intersection and Query ^ Query for exclusive-or/symmetric difference along with + and - for union and subtraction would seem to make sense. I don't see overloading the non-commutative operations as a huge problem.
    Arul Suresh
    PhD, Mechanical Engineering, Stanford University
  • konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 1,221 ✭✭✭✭✭
    edited November 2017
    Since those functions on subqueries behave like filters i would be happy to use any symbol to qlue them in one conveyor, will it be dot or arrow or &, but implementing object-oriented approach seem to be more usefull in other applications, for not only queries.
    By the way Kevin's approach reminds me multy-argument function curring from Haskell. Looks like there going to begin "OOP vs Functional" battle :D
  • MBartlett21MBartlett21 Member, OS Professional, Developers Posts: 2,034 EDU
    Nice!

    Originally I resisted the urge to do this type of thing because it is a bit of an abuse of notation, but it's hard to argue with your example -- it is certainly way more readable with the overloads.  I'm going to think about putting something like that in std.

    Things I would probably do a little differently / in addition:
    1. I would add a QueryFilter type and tag the lambdas with it -- that way, you don't accidentally multiply with some arbitrary function.
    2. I would (maybe -- I'm not certain) flip the order to be consistent with function composition -- that would require an overload of multiplying two QueryFilters
    3. I would probably add + for qUnion and - for qSubtraction.  qIntersection would be left without an operator, but it is much more rare (currently 26 std occurrences vs. 63 qSubtraction and 267 qUnion).  The algebra doesn't quite work out (i.e., a + b - c is not the same as a - c + b) but it's close.

    @ilya_baran
    Would you be able to use * for intersection, as that is how the QueryFilter functions would work with a Query.

    Also, the code formatting on this page appears to have mucked up:

    mb - draftsman - also FS author: View FeatureScripts
    IR for AS/NZS 1100
Sign In or Register to comment.