@Override
  @SuppressWarnings({"unchecked", "rawtypes"})
  protected boolean evaluateAndPopulateValueStores(
      GraphRewrite event, EvaluationContext context, FrameCreationContext frameCreationContext) {
    ParameterStore store = DefaultParameterStore.getInstance(context);

    QueryBuilderFrom query = Query.fromType(ParameterWiringTestModel.class);
    if (!StringUtils.isBlank(fromVarname)) {
      query = Query.from(fromVarname);
    }

    Pattern compiledPattern = pattern.getCompiledPattern(store);
    query.withProperty(
        ParameterWiringTestModel.VALUE,
        QueryPropertyComparisonType.REGEX,
        compiledPattern.pattern());

    String uuid = UUID.randomUUID().toString();
    query.as(uuid);

    List<WindupVertexFrame> allFrameResults = new ArrayList<>();
    if (query.evaluate(event, context)) {
      Iterable<? extends WindupVertexFrame> frames = Variables.instance(event).findVariable(uuid);
      for (WindupVertexFrame frame : frames) {
        ParameterWiringTestModel model = (ParameterWiringTestModel) frame;

        ParameterizedPatternResult parseResult = pattern.parse(model.getValue());
        if (parseResult.matches()) {
          Map<String, List<WindupVertexFrame>> variables = new LinkedHashMap<>();
          frameCreationContext.beginNew((Map) variables);
          if (parseResult.submit(event, context)) {
            allFrameResults.add(model);
            Maps.addListValue(variables, varname, model);
          } else {
            System.out.println("nope: " + model);
            frameCreationContext.rollback();
          }
        } else {
          System.out.println("nope: " + model);
        }
      }
      Variables.instance(event).removeVariable(uuid);
      Variables.instance(event).setVariable(varname, allFrameResults);
      return true;
    }

    return false;
  }
  @Override
  protected boolean evaluateWithValueStore(
      GraphRewrite event, EvaluationContext context, FrameContext frameContext) {
    ParameterStore store = DefaultParameterStore.getInstance(context);

    QueryBuilderFrom query = Query.fromType(ParameterWiringTestModel.class);
    if (!StringUtils.isBlank(fromVarname)) {
      query = Query.from(fromVarname);
    }

    Pattern compiledPattern = pattern.getCompiledPattern(store);
    query.withProperty(
        ParameterWiringTestModel.VALUE,
        QueryPropertyComparisonType.REGEX,
        compiledPattern.pattern());

    String uuid = UUID.randomUUID().toString();
    query.as(uuid);
    if (query.evaluate(event, context)) {
      boolean result = false;
      List<WindupVertexFrame> results = new ArrayList<>();
      Iterable<? extends WindupVertexFrame> frames = Variables.instance(event).findVariable(uuid);
      for (WindupVertexFrame frame : frames) {
        ParameterWiringTestModel model = (ParameterWiringTestModel) frame;

        String value = model.getValue();
        ParameterizedPatternResult parseResult = pattern.parse(value);
        if (parseResult.submit(event, context)) {
          result = true;
          results.add(model);
        }
      }

      Variables.instance(event).removeVariable(uuid);
      if (result) {
        Variables.instance(event).setVariable(varname, results);
        return true;
      }
    }

    frameContext.reject();
    return false;
  }
예제 #3
0
파일: JavaClass.java 프로젝트: Maarc/windup
  private boolean evaluate(
      GraphRewrite event, EvaluationContext context, EvaluationStrategy evaluationStrategy) {
    try {
      ExecutionStatistics.get().begin("JavaClass.evaluate");

      final ParameterStore store = DefaultParameterStore.getInstance(context);
      final Pattern compiledPattern = referencePattern.getCompiledPattern(store);

      /*
       * Only set in the case of a query with no "from" variable.
       */
      String initialQueryID = null;

      QueryBuilderFrom query;
      initialQueryID = "iqi." + UUID.randomUUID().toString();

      // prepare initialQueryID
      if (!StringUtils.isBlank(getInputVariablesName())) {
        QueryBuilderFrom fromQuery = Query.from(getInputVariablesName());
        QueryBuilderPiped piped =
            fromQuery.piped(
                new QueryGremlinCriterion() {
                  @Override
                  public void query(GraphRewrite event, GremlinPipeline<Vertex, Vertex> pipeline) {
                    pipeline
                        .out(FileReferenceModel.FILE_MODEL)
                        .in(FileReferenceModel.FILE_MODEL)
                        .has(
                            JavaTypeReferenceModel.RESOLVED_SOURCE_SNIPPIT,
                            Text.REGEX,
                            compiledPattern.toString());
                  }
                });
        piped.as(initialQueryID).evaluate(event, context);
      } else {
        GremlinPipeline<Vertex, Vertex> resolvedTextSearch =
            new GremlinPipeline<>(event.getGraphContext().getGraph());
        resolvedTextSearch.V();
        resolvedTextSearch.has(
            JavaTypeReferenceModel.RESOLVED_SOURCE_SNIPPIT, Text.REGEX, titanify(compiledPattern));

        if (!resolvedTextSearch.iterator().hasNext()) return false;

        Variables.instance(event)
            .setVariable(
                initialQueryID,
                new FramedVertexIterable<>(
                    event.getGraphContext().getFramed(),
                    resolvedTextSearch,
                    JavaTypeReferenceModel.class));
      }
      query = Query.from(initialQueryID);

      if (lineMatchPattern != null) {
        final Pattern compiledLineMatchPattern = lineMatchPattern.getCompiledPattern(store);
        query.withProperty(
            JavaTypeReferenceModel.SOURCE_SNIPPIT,
            QueryPropertyComparisonType.REGEX,
            compiledLineMatchPattern.pattern());
      }
      String uuid = UUID.randomUUID().toString();
      query.as(uuid);

      if (typeFilterPattern != null) {
        Pattern compiledTypeFilterPattern = typeFilterPattern.getCompiledPattern(store);
        query.piped(new TypeFilterCriterion(compiledTypeFilterPattern));
      }
      if (!locations.isEmpty())
        query.withProperty(JavaTypeReferenceModel.REFERENCE_TYPE, locations);

      List<WindupVertexFrame> results = new ArrayList<>();
      if (query.evaluate(event, context)) {
        Iterable<? extends WindupVertexFrame> frames = Variables.instance(event).findVariable(uuid);
        for (WindupVertexFrame frame : frames) {
          FileModel fileModel = ((FileReferenceModel) frame).getFile();
          Iterable<JavaClassModel> javaClasses = null;
          if (fileModel instanceof AbstractJavaSourceModel)
            javaClasses = ((AbstractJavaSourceModel) fileModel).getJavaClasses();
          else if (fileModel instanceof JavaClassFileModel)
            javaClasses = Arrays.asList(((JavaClassFileModel) fileModel).getJavaClass());

          for (JavaClassModel javaClassModel : javaClasses) {
            if (typeFilterPattern == null
                || typeFilterPattern.parse(javaClassModel.getQualifiedName()).matches()) {
              JavaTypeReferenceModel model = (JavaTypeReferenceModel) frame;
              ParameterizedPatternResult referenceResult =
                  referencePattern.parse(model.getResolvedSourceSnippit());
              if (referenceResult.matches()) {
                evaluationStrategy.modelMatched();

                if (referenceResult.submit(event, context)
                    && (typeFilterPattern == null
                        || typeFilterPattern
                            .parse(javaClassModel.getQualifiedName())
                            .submit(event, context))) {
                  boolean annotationMatched =
                      matchAnnotationConditions(event, context, evaluationStrategy, model);
                  if (!annotationMatched) {
                    evaluationStrategy.modelSubmissionRejected();
                  } else {
                    results.add(model);
                    evaluationStrategy.modelSubmitted(model);
                  }
                } else {
                  evaluationStrategy.modelSubmissionRejected();
                }
              }
            }
          }
        }
        Variables.instance(event).removeVariable(uuid);
        if (initialQueryID != null) Variables.instance(event).removeVariable(initialQueryID);

        setResults(event, getVarname(), results);
        return !results.isEmpty();
      }
      return false;
    } finally {
      ExecutionStatistics.get().end("JavaClass.evaluate");
    }
  }