public ElementList createElementList(Element pattern) {
		final List<Resource> members = new LinkedList<Resource>();
		if(pattern != null) {
			pattern.visit(new AbstractElementVisitor() {
				
				private boolean first = true;
	
				@Override
				public void visit(ElementAssign assign) {
					RDFNode expression = createExpression(assign.getExpr());
					Variable variable = getVariable(assign.getVar().getName()).as(Variable.class);
					members.add(SPINFactory.createBind(model, variable, expression));
				}
				
				@Override
				public void visit(ElementBind bind) {
					RDFNode expression = createExpression(bind.getExpr());
					Variable variable = getVariable(bind.getVar().getName()).as(Variable.class);
					members.add(SPINFactory.createBind(model, variable, expression));
				}
				
				
				@Override
				public void visit(ElementFilter filter) {
					RDFNode expression = createExpression(filter.getExpr());
					members.add(SPINFactory.createFilter(model, expression));
				}
	
				
				@Override
				public void visit(ElementGroup group) {
					if(first) {
						first = false;
						super.visit(group);
					}
					else {
						ElementList list = createElementList(group);
						members.add(list);
					}
				}


				@Override
				public void visit(ElementMinus minus) {
					Element element = minus.getMinusElement();
					ElementList body = createElementList(element);
					Minus spinMinus = SPINFactory.createMinus(model, body);
					members.add(spinMinus);
				}


				@Override
				public void visit(ElementNamedGraph namedGraph) {
					Resource graphNameNode;
					Node nameNode = namedGraph.getGraphNameNode();
					if(nameNode.isVariable()) {
						graphNameNode = getVariable(nameNode.getName());
					}
					else {
						graphNameNode = model.getResource(nameNode.getURI());
					}
					Element element = namedGraph.getElement();
					RDFList elements = createElementList(element);
					NamedGraph ng = SPINFactory.createNamedGraph(model, graphNameNode, elements);
					members.add(ng);
				}


				@Override
				public void visit(ElementNotExists notExists) {
					Element element = notExists.getElement();
					ElementList body = createElementList(element);
					NotExists ne = SPINFactory.createNotExists(model, body);
					members.add(ne);
				}


				@Override
				public void visit(ElementOptional optional) {
					Element element = optional.getOptionalElement();
					ElementList body = createElementList(element);
					Optional o = SPINFactory.createOptional(model, body);
					members.add(o);
				}
	
				
				public void visit(ElementPathBlock block) {
					visitElements(block.patternElts());
				}
	
				
				@Override
				public void visit(ElementService service) {
					Node node = service.getServiceNode();
					Resource uri;
					if(node.isVariable()) {
						uri = getVariable(node.getName());
					}
					else {
						uri = model.getResource(node.getURI());
					}
					Element element = service.getElement();
					ElementList body = createElementList(element);
					members.add(SPINFactory.createService(model, uri, body));
				}


				public void visit(ElementSubQuery subQuery) {
					Query arq = subQuery.getQuery();
					org.topbraid.spin.model.Query spinQuery = createQuery(arq, null);
					members.add(SPINFactory.createSubQuery(model, spinQuery));
				}


				public void visit(ElementTriplesBlock el) {
					visitElements(el.patternElts());
				}


				@Override
				public void visit(ElementUnion arqUnion) {
					List<Element> arqElements = arqUnion.getElements();
					List<RDFNode> elements = new LinkedList<RDFNode>();
					for(Element arqElement : arqElements) {
						RDFList element = createElementList(arqElement);
						elements.add(element);
					}
					Union union = model.createResource(SP.Union).as(Union.class);
					union.addProperty(SP.elements, model.createList(elements.iterator()));
					members.add(union);
				}

				
				@SuppressWarnings("rawtypes")
				private void visitElements(Iterator it) {
					while(it.hasNext()) {
						Object next = it.next();
						if(next instanceof TriplePath) {
							TriplePath path = (TriplePath)next;
							if(path.isTriple()) {
								next = path.asTriple();
							}
							else {
								Path p = path.getPath();
								Resource pathResource = createPath(p);
								Resource subject = (Resource) getNode(path.getSubject());
								RDFNode object = getNode(path.getObject());
								org.topbraid.spin.model.TriplePath triplePath = SPINFactory.createTriplePath(model, subject, pathResource, object);
								members.add(triplePath);
							}
						}
						if(next instanceof Triple) {
							Triple triple = (Triple) next;
							Resource subject = (Resource) getNode(triple.getSubject());
							Resource predicate = (Resource) getNode(triple.getPredicate());
							RDFNode object = getNode(triple.getObject());
							members.add(SPINFactory.createTriplePattern(model, subject, predicate, object));
						}
					}
				}
			});
		}
		return model.createList(members.iterator()).as(ElementList.class);
	}