删除Prolog中的单引号/引号

时间:2016-01-13 15:35:08

标签: string prolog swi-prolog quotation-marks single-quotes

我有一个外部API发送信息到我的Prolog应用程序,我发现创建我的事实有问题。

当收到的信息非常广泛时,Prolog会自动将package com.fastlaneinnovations.chartexample; import com.codename1.charts.ChartComponent; import com.codename1.charts.models.XYMultipleSeriesDataset; import com.codename1.charts.models.XYSeries; import com.codename1.charts.renderers.XYMultipleSeriesRenderer; import com.codename1.charts.renderers.XYSeriesRenderer; import com.codename1.charts.util.ColorUtil; import com.codename1.charts.views.LineChart; import com.codename1.components.ShareButton; import com.codename1.io.FileSystemStorage; import com.codename1.io.Log; import com.codename1.ui.Container; import com.codename1.ui.Display; import com.codename1.ui.Form; import com.codename1.ui.Image; import com.codename1.ui.Label; import com.codename1.ui.events.ActionEvent; import com.codename1.ui.events.ActionListener; import com.codename1.ui.layouts.BorderLayout; import com.codename1.ui.plaf.UIManager; import com.codename1.ui.util.ImageIO; import com.codename1.ui.util.Resources; import java.io.IOException; import java.io.OutputStream; import java.util.Hashtable; import java.util.Set; public class ChartExample { private Form current; private Resources theme; private XYMultipleSeriesRenderer chartRenderer; private Container rpmChartContainer; private XYSeries series; private LineChart lineChart; private ChartComponent chart; private Hashtable<Long, Integer> chartData; private String chartName = "Chart"; private String path; private Object os; private int chartHeight; private int chartWidth; private ShareButton share; private Set<Long> keys; public void init(Object context) { theme = UIManager.initFirstTheme("/theme"); // Pro only feature, uncomment if you have a pro subscription // Log.bindCrashProtection(true); } public void start() { if (current != null) { current.show(); return; } Form form = new Form(); form.setLayout(new BorderLayout()); chartData = new Hashtable<Long, Integer>(); for (int i = 0; i < 8000; i += 100) { chartData.put(Long.valueOf(i), Integer.valueOf(i)); } rpmChartContainer = new Container(); rpmChartContainer.setLayout(new BorderLayout()); series = new XYSeries("RPM"); chartRenderer = createChartRenderer(); setChartSettings(chartRenderer, "RPM vs Time", "Time (s)", "RPM", 0.0, 10.0, 0.0, 8000.0, ColorUtil.YELLOW, ColorUtil.WHITE); lineChart = new LineChart(buildDataSet(series), chartRenderer); chart = new ChartComponent(lineChart); chart.setUIID("ChartComponent"); rpmChartContainer.addComponent(BorderLayout.CENTER, chart); updateChart(); share = new ShareButton(); share.setName(chartName); share.setText("Share this chart"); share.setTextToShare("RPM chart created by R-P-M Control Center app"); rpmChartContainer.add(BorderLayout.SOUTH, share); form.add(BorderLayout.CENTER, rpmChartContainer); form.show(); createShareButton(); } public void stop() { current = Display.getInstance().getCurrent(); } public void destroy() { } public void updateChart() { keys = chartData.keySet(); series.clear(); for (Long key : keys) { series.add((key / 1000.0), (chartData.get(key) / 1.0)); } } private XYMultipleSeriesRenderer createChartRenderer() { XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer(); renderer.setPointSize(3f); XYSeriesRenderer seriesRenderer = new XYSeriesRenderer(); seriesRenderer.setColor(ColorUtil.GREEN); // seriesRenderer.setPointStyle(PointStyle.CIRCLE); // seriesRenderer.setFillPoints(true); seriesRenderer.setLineWidth(5f); seriesRenderer.setShowLegendItem(true); renderer.addSeriesRenderer(seriesRenderer); return renderer; } private XYMultipleSeriesDataset buildDataSet(XYSeries dataSeries) { XYMultipleSeriesDataset multiDataSet = new XYMultipleSeriesDataset(); multiDataSet.addSeries(dataSeries); return multiDataSet; } private void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle, String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor, int labelsColor) { renderer.setChartTitle(title); renderer.setXTitle(xTitle); renderer.setYTitle(yTitle); renderer.setXAxisMin(xMin); // renderer.setXAxisMax(xMax); renderer.setYAxisMin(yMin); renderer.setYAxisMax(yMax); renderer.setAxesColor(axesColor); renderer.setLabelsColor(labelsColor); renderer.setMarginsColor(ColorUtil.BLACK); renderer.setGridColor(ColorUtil.BLACK); } public void createShareButton() { rpmChartContainer.revalidate(); chartWidth = rpmChartContainer.getWidth(); chartHeight = rpmChartContainer.getHeight(); Image chartAsImage = Image.createImage(chartWidth, chartHeight); rpmChartContainer.paint(chartAsImage.getGraphics()); setShareImage(share, chartAsImage); } public void setShareImage(ShareButton shareButton, Image shareImage) { OutputStream os = null; final String shareImagePath = FileSystemStorage.getInstance() .getAppHomePath() + shareButton.getName(); FileSystemStorage.getInstance().delete(shareImagePath); try { os = FileSystemStorage.getInstance().openOutputStream( shareImagePath); ImageIO.getImageIO().save(shareImage, os, ImageIO.FORMAT_JPEG, 0.95f); } catch (IOException e) { e.printStackTrace(); } finally { try { os.close(); } catch (IOException ex) { ex.printStackTrace(); } } shareButton.setImageToShare(shareImagePath, "image/jpeg"); } } (单引号)添加到该信息中。

示例:收到数据后,我创建的事实是:

'

我想创建的事实是

object(ObjectID,ObjectName,'[(1,09:00,12:00),(2,10:00,12:00)]',anotherID)

在列表前没有object(ObjectID,ObjectName,[(1,09:00,12:00),(2,10:00,12:00)] ,anotherID)

有谁知道如何解决这个问题?使用谓词接收'并返回'[(1,09:00,12:00),(2,10:00,12:00)]'

1 个答案:

答案 0 :(得分:2)

你看到的是 atom ,你想将它转换为 term 我认为。

如果您使用,则可以使用内置term_to_atom/2

  

如果Atom描述与Term统一的字词,则为真。实例化Atom后,将解析Atom,并将结果与​​Term统一。

示例:

?- term_to_atom(X,'[(1,09:00,12:00),(2,10:00,12:00)]').
X = [ (1, 9:0, 12:0), (2, 10:0, 12:0)].

因此,在右侧,您输入 atom ,在左侧输入“等效”术语。但请注意,例如00被解释为数字,因此等于0,这可能是意外的行为。

因此,您可以将谓词翻译为:

translate(object(A,B,C,D),object(A,B,CT,D)) :-
    term_to_atom(CT,C).

由于您没有完全指定如何获取此数据,因此我不知道您将如何转换它。但上述方式可能会有所帮助。